Main Page | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages | Examples

vetCodec_IMG.cpp

Go to the documentation of this file.
00001 
00019 #include "vetCodec_IMG.h"
00020 
00021 
00022 #include <string.h>     // memcpy
00023 #include <stdlib.h> // itoa()
00024 
00025 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <time.h>
00030 
00031 
00032 #include <sys/types.h>
00033 
00034 
00035 #include <magick/api.h>
00036 
00037 
00038 void* vetCodec_IMG::im_load(char *filename, unsigned int &w, unsigned int &h, char* prog_path)
00039  {
00040         Image *image ;
00041         ExceptionInfo exception;
00042 
00043         ImageInfo *image_info;
00044 
00045         /*
00046         Initialize the image info structure and read an image.
00047         */
00048         InitializeMagick(prog_path); // Should really pass argv[0] (only req'd under windows)
00049         GetExceptionInfo(&exception);
00050         image_info=CloneImageInfo((ImageInfo *) NULL);
00051 
00052         sprintf(image_info->filename,"%s", filename);
00053         image=ReadImage(image_info,&exception);
00054 
00055         if (image == (Image *) NULL)
00056                 return NULL ;
00057 
00058         w = image->columns ;
00059         h = image->rows ;
00060 
00061 //printf("%d\n", w);
00062 //printf("%d\n", h);
00063 
00064         // Tidy up
00065         DestroyImageInfo(image_info);
00066         DestroyExceptionInfo(&exception);
00067 
00068         return (void*)image ;
00069  }
00070 
00071 void vetCodec_IMG::im_get_data(void *img, int *rgb)
00072 {
00073     unsigned int cnt ;
00074     PixelPacket *pp ;
00075     int *optr ;
00076     PixelPacket *iptr ;
00077     unsigned int sz ;
00078     Image *image ;
00079 
00080     image = (Image*)img ;
00081 
00082     pp =  GetImagePixels( image, 0, 0, image->columns, image->rows) ;
00083 
00084     sz = image->columns*image->rows ;
00085 
00086     for(cnt=0,iptr=pp, optr=rgb ; cnt<sz ; cnt++, iptr++){
00087         *(optr++) = iptr->red >> 8 ;
00088         *(optr++) = iptr->green >> 8 ;// should be 16
00089         *(optr++) = iptr->blue >> 8 ;
00090     }
00091 }
00092 void vetCodec_IMG::im_get_data(void *img, unsigned char *rgb)
00093 {
00094     unsigned int cnt ;
00095     PixelPacket *pp ;
00096     unsigned char *optr ;
00097     PixelPacket *iptr ;
00098     unsigned int sz ;
00099     Image *image ;
00100 
00101     image = (Image*)img ;
00102 
00103     pp =  GetImagePixels( image, 0, 0, image->columns, image->rows) ;
00104 
00105     sz = image->columns*image->rows ;
00106 
00107     for(cnt=0,iptr=pp, optr=rgb ; cnt<sz ; cnt++, iptr++){
00108         *(optr++) = (unsigned char)( iptr->red >> 8 );
00109         *(optr++) = (unsigned char)( iptr->green >> 8 );
00110         *(optr++) = (unsigned char)( iptr->blue >> 8 );
00111     }
00112 }
00113 
00114 
00115 void vetCodec_IMG::im_get_data_grey(void *img, int *d)
00116 {
00117     unsigned int cnt ;
00118     PixelPacket *pp ;
00119     int *optr ;
00120     PixelPacket *iptr ;
00121     unsigned int sz ;
00122     Image *image ;
00123 
00124     image = (Image*)img ;
00125 
00126     pp =  GetImagePixels( image, 0, 0, image->columns, image->rows) ;
00127 
00128     sz = image->columns*image->rows ;
00129 
00130     for(cnt=0,iptr=pp, optr=d ; cnt<sz ; cnt++, iptr++, optr++){
00131         *optr = iptr->red >> 8 ;
00132         *optr += iptr->green >> 8 ;
00133         *optr += iptr->blue >> 8 ;
00134         *optr /= 3 ;
00135     }
00136 }
00137 
00138 void vetCodec_IMG::im_free(void *img)
00139 {
00140     Image *image ;
00141 
00142     image = (Image*)img ;
00143 
00144     DestroyImage(image);
00145 
00146 }
00147 
00148 
00149 
00150 
00151 vetCodec_IMG::vetCodec_IMG(char *filename) : vetCodec()
00160  {
00161         DEBUGMSG("vetCodec_IMG::vetCodec_IMG(char *filename, FileFormat format) [CONTRUCTOR] ", *filename)
00162 
00163         myParams = NULL;
00164         reset();
00165         setParameters(NULL);
00166 
00167         load( filename );
00168 
00169         strcpy(myParams->fileNameBase, filename);
00170  }
00171 
00172 vetCodec_IMG::vetCodec_IMG( vetCodec_IMGParameters* initParams ) : vetCodec()
00173  {
00174         DEBUGMSG("vetCodec_IMG::vetCodec_IMG(vetCodec_IMGParameters* initParams) [CONTRUCTOR] ", *filename)
00175 
00176         myParams = NULL;
00177         reset();
00178         setParameters(initParams);
00179  }
00180 
00181 VETRESULT vetCodec_IMG::reset()
00182  {
00183         INFO("VETRESULT vetCodec_IMG::reset() [SET DEFAULT PARAMETERS]")
00184 
00185         setName("Bitmap Coder");
00186         setDescription("Read or write images to bitmap format.");
00187         setVersion(1.0);
00188 
00189         if (myParams != NULL)
00190          {
00191                 myParams->reset();
00192                 strcpy(fileNameBuffer, myParams->fileNameBase);
00193          }
00194 
00195         return VETRET_OK;
00196  }
00197 
00198 
00199 VETRESULT vetCodec_IMG::setParameters (vetCodec_IMGParameters* initParams)
00200  {
00201 
00202         if ( initParams == NULL )
00203                 myParams = new vetCodec_IMGParameters();
00204         else
00205                 myParams = initParams;
00206 
00207         return VETRET_OK;
00208  }
00209 
00210 bool vetCodec_IMG::EoF()        // buggy
00211  {
00212          return false;
00213  }
00214 
00215 
00216 VETRESULT vetCodec_IMG::extractTo(vetFrameYUV420& img)
00217  {
00218         DEBUGMSG("VETRESULT vetCodec_IMG::extractTo(vetFrameYUV420& img) [pushing data]", doBuffering)
00219 
00220         return VETRET_NOT_IMPLEMENTED;
00221 
00222  }
00223 
00224 VETRESULT vetCodec_IMG::extractTo(vetFrameRGB24& img)
00225  {
00226         DEBUGMSG("VETRESULT vetCodec_IMG::extractTo(vetFrameRGB24& img) [pushing data]", doBuffering)
00227 
00228         int ret = VETRET_OK;
00229 
00230         if ( myParams->autoOuput && !myParams->doBuffering )
00231          {
00232                 doFileNameCurrent();
00233 
00234 
00235                 ret = load(img, fileNameBuffer);
00236 
00237                 if ( myParams->fileNameProgression )
00238                         myParams->fileNameIndex++;
00239 
00240                 return ret;
00241          }
00242 
00243         if ( myParams->autoInput )
00244          {
00245                 doFileNameCurrent();
00246 
00247                 ret = load(fileNameBuffer);
00248 
00249                 if ( myParams->fileNameProgression )
00250                         myParams->fileNameIndex++;
00251          }
00252 
00253 
00254 //      vetFrameRGB::operator >> (img);
00255 
00256         return ret;
00257  }
00258 
00259 
00260 VETRESULT vetCodec_IMG::extractTo(vetFrameT<unsigned char>& img)
00261  {
00262         DEBUGMSG("VETRESULT vetCodec_IMG::extractTo(vetFrameT& img) [pushing data]", doBuffering)
00263 
00264         return VETRET_NOT_IMPLEMENTED;
00265 
00266  }
00267 
00268 
00269 
00270 VETRESULT vetCodec_IMG::importFrom(vetFrameYUV420& img)
00271  {
00272         DEBUGMSG("VETRESULT vetCodec_IMG::importFrom(vetFrameYUV420& img) [reading data]", doBuffering)
00273 
00274         return VETRET_NOT_IMPLEMENTED;
00275 
00276  }
00277 
00278 
00279 VETRESULT vetCodec_IMG::importFrom(vetFrameRGB24& img)
00280  {
00281         DEBUGMSG("VETRESULT vetCodec_IMG::importFrom(vetFrameRGB24& img) [reading data]", doBuffering)
00282 
00283         int ret = VETRET_OK;
00284 
00285         if ( myParams->autoOuput && !myParams->doBuffering )
00286          {
00287                 doFileNameCurrent();
00288 
00289                 ret = save(img, fileNameBuffer);
00290 
00291                 if ( myParams->fileNameProgression )
00292                         myParams->fileNameIndex++;
00293 
00294                 return ret;
00295          }
00296 
00297 //      vetFrameRGB::operator << (img);
00298 
00299         if ( myParams->autoOuput )
00300          {
00301                 doFileNameCurrent();
00302 
00303                 ret = save(fileNameBuffer);
00304 
00305                 if ( myParams->fileNameProgression )
00306                         myParams->fileNameIndex++;
00307          }
00308 
00309         return ret;
00310  }
00311 
00312 VETRESULT vetCodec_IMG::importFrom(vetFrameT<unsigned char>& img)
00313  {
00314         DEBUGMSG("VETRESULT vetCodec_IMG::importFrom(vetFrameYUV420& img) [reading data]", doBuffering)
00315 
00316         return VETRET_NOT_IMPLEMENTED;
00317 
00318  }
00319 
00320 void vetCodec_IMG::setFileName(const char *filename)
00321  {
00322         strncpy(myParams->fileNameBase, filename, 64);
00323         strcpy(fileNameBuffer, myParams->fileNameBase);
00324  }
00325 
00326 
00327 void vetCodec_IMG::doFileNameCurrent()
00328  {
00329 
00330         sprintf( fileNameIndexBuffer, "%d", myParams->fileNameIndex );
00331 
00332         strcpy( fileNameBuffer, myParams->fileNameBase );
00333 
00334         if ( myParams->fileNameIndex != -1 )
00335                 strcat( fileNameBuffer, fileNameIndexBuffer );
00336 
00337         strcat( fileNameBuffer, (const char*)".bmp" );
00338 
00339         DEBUGMSG("void vetCodec_IMG::doFileNameCurrent()", fileNameBuffer);
00340 
00341  }
00342 
00343 
00344 void vetCodec_IMG::getFileNameCurrent(char* filename)
00345  {
00346         doFileNameCurrent();
00347         strcpy( filename, fileNameBuffer );
00348  }
00349 
00350 
00351 
00352 VETRESULT vetCodec_IMG::save()
00353  {
00354         INFO("VETRESULT vetCodec_IMG::save() [saving buffered data]")
00355 
00356         int ret = VETRET_OK;
00357 
00358 
00359         doFileNameCurrent();
00360 //      ret = save(tmp, fileNameBuffer);
00361         return VETRET_NOT_IMPLEMENTED;
00362 
00363         return ret;
00364 }
00365 
00366 VETRESULT vetCodec_IMG::save(char *filename)
00367  {
00368         DEBUGMSG("VETRESULT vetCodec_IMG::save(char *filename, FileFormat format) [saving buffered data]", filename)
00369 
00370         return VETRET_NOT_IMPLEMENTED;
00371 
00372  }
00373 
00374 
00375 VETRESULT vetCodec_IMG::load()
00376  {
00377         INFO("VETRESULT vetCodec_IMG::load() [loading data to buffer]")
00378 
00379         int ret = VETRET_OK;
00380 
00381         vetFrameYUV420 tmp;
00382         tmp.autoFreeData = false;// tmp used as a struct..
00383 
00384         doFileNameCurrent();
00385         ret = load(tmp, fileNameBuffer);
00386 
00387 
00388 
00389         return ret;
00390 }
00391 
00392 
00393 VETRESULT vetCodec_IMG::load(char *filename)
00394  {
00395         DEBUGMSG("VETRESULT vetCodec_IMG::load(char *filename, FileFormat format) [loading data to buffer]", filename)
00396 
00397         int ret = VETRET_OK;
00398         vetFrameYUV420 tmp;
00399         tmp.autoFreeData = false;// tmp used as a struct.. UMH=????????=??????????????????????
00400 
00401         ret = load(tmp, filename);
00402 
00403 
00404         return ret;
00405  }
00406 
00407 
00408 
00409 //STATIC
00410 
00411 
00412 VETRESULT vetCodec_IMG::load(vetFrameYUV420& source, const char *filename)
00413  {
00414         DEBUGMSG("VETRESULT vetCodec_IMG::load(vetFrameYUV420& source, char *filename, ..) [loading file data to frame]", filename)
00415 
00416         return VETRET_NOT_IMPLEMENTED;
00417 
00418  }
00419 
00420 VETRESULT vetCodec_IMG::load(vetFrameRGB24& source, const char *filename)
00421  {
00422         DEBUGMSG("VETRESULT vetCodec_IMG::load(vetFrameRGB24& source, char *filename, ..) [loading file data to frame]", filename)
00423 
00424         int ret = VETRET_OK;
00425         void* img = NULL;
00426         bool ok;
00427 
00428         unsigned int w, h;
00429 
00430     if ( (img=im_load((char *)filename, w, h, NULL) ) == NULL )
00431 
00432         ok = false ;
00433 
00434     else
00435          {
00436                 if ( w*h == 0 )
00437 
00438                         return VETRET_PARAM_ERR;
00439 
00440                 else if (w*h != source.width*source.height)
00441                  {
00442                         if (source.data != NULL)
00443                                 delete source.data;
00444                         source.data = new PixelRGB24[w*h];
00445                  }
00446                 source.width = w;
00447                 source.height = h;
00448             im_get_data(img, (unsigned char*)source.data[0]) ;
00449          }
00450 
00451         return ret;
00452  }
00453 
00454 
00455 
00456 VETRESULT vetCodec_IMG::load(vetFrameT<unsigned char>& source, const char *filename)
00457  {
00458         DEBUGMSG("VETRESULT vetCodec_IMG::load(vetFrameT& source, char *filename, ..) [loading file data to frame]", filename)
00459 
00460         if ( source.getWidth() != 0 || source.getHeight() != 0 )
00461                 return VETRET_PARAM_ERR;
00462 
00463         int ret = VETRET_OK;
00464 
00465 
00466         return VETRET_NOT_IMPLEMENTED;
00467  }
00468 
00469 
00470 VETRESULT vetCodec_IMG::save(vetFrameYUV420& source, const char *filename)
00471  {
00472         DEBUGMSG("VETRESULT vetCodec_IMG::save(vetFrameYUV420& source, char *filename, FileFormat format) [saving frame to file]", filename)
00473 /*
00474         if ( source.width == 0 || source.height == 0 || source.data == NULL )
00475                 return VETRET_PARAM_ERR;
00476 
00477         int ret = VETRET_OK;
00478 
00479 
00480         ImageInfo *image_info;
00481         image_info=CloneImageInfo((ImageInfo *) NULL);
00482 
00483         ExceptionInfo exception;
00484 
00485         GetExceptionInfo(&exception);
00486 
00487 
00488         Image* myI = ConstituteImage(source.width,source.height, "RGB", IntegerPixel, source.data[0], &exception);
00489 
00490     pp =  GetImagePixels( &myI, 0, 0, source.width,source.height) ;
00491 
00492     sz = source.width,source.height ;
00493 
00494     for(cnt=0,iptr=pp, optr=source.data[0] ; cnt<sz ; cnt++, iptr++){
00495         iptr->red = *(optr++);
00496         iptr->green = *(optr++);
00497         iptr->blue = *(optr++);
00498     }
00499 
00500         sprintf(image_info->filename,"%s", filename);
00501 
00502         WriteImage(image_info, myI);
00503 
00504         DestroyImageInfo(image_info);
00505         DestroyExceptionInfo(&exception);
00506         return ret;
00507 */
00508 
00509         return VETRET_NOT_IMPLEMENTED;
00510 
00511 }
00512 
00513 VETRESULT vetCodec_IMG::save(vetFrameRGB24& source, const char *filename)
00514  {
00515         DEBUGMSG("VETRESULT vetCodec_IMG::save(vetFrameRGB24& source, char *filename, FileFormat format) [saving frame to file]", filename)
00516 
00517         if ( source.getWidth() == 0 || source.getHeight() == 0 )
00518                 return VETRET_PARAM_ERR;
00519 
00520         int ret = VETRET_OK;
00521 
00522         ImageInfo *image_info;
00523         image_info=CloneImageInfo((ImageInfo *) NULL);
00524 
00525         ExceptionInfo exception;
00526 
00527         GetExceptionInfo(&exception);
00528 
00529 
00530         Image* myI = ConstituteImage(source.width,source.height, "RGB", CharPixel, source.data[0], &exception);
00531 /*
00532     pp =  GetImagePixels( &myI, 0, 0, source.width,source.height) ;
00533 
00534     sz = source.width,source.height ;
00535 
00536     for(cnt=0,iptr=pp, optr=source.data[0] ; cnt<sz ; cnt++, iptr++){
00537         iptr->red = *(optr++);
00538         iptr->green = *(optr++);
00539         iptr->blue = *(optr++);
00540     }
00541 */
00542         sprintf(image_info->filename,"%s", filename);
00543 
00544         WriteImage(image_info, myI);
00545 
00546         DestroyImageInfo(image_info);
00547         DestroyExceptionInfo(&exception);
00548 
00549         return ret;
00550 }
00551 
00552 
00553 VETRESULT vetCodec_IMG::save(vetFrameT<unsigned char>& source, const char *filename)
00554  {
00555         DEBUGMSG("VETRESULT vetCodec_IMG::save(vetFrameT& source, char *filename, FileFormat format) [saving frame to file]", filename)
00556 
00557         return VETRET_NOT_IMPLEMENTED;
00558 
00559  }
00560 
00561 
00562 
00571 void vetCodec_IMG::setFileNameProgression(bool value) { myParams->fileNameProgression = value; }
00572 
00585 void vetCodec_IMG::setAutoOutputEnabled(bool value) { myParams->autoOuput = value; }
00586 
00599 void vetCodec_IMG::setAutoInputEnabled(bool value) { myParams->autoInput = value; }
00600 
00613 void vetCodec_IMG::setDoBuffering(bool value) { myParams->doBuffering = value; }
00614 
00615 
00626 int vetCodec_IMG::getFileNameIndex() { return myParams->fileNameIndex; }
00635 bool vetCodec_IMG::isFileNameProgressionEnabled() { return myParams->fileNameProgression; }
00636 
00647 bool vetCodec_IMG::isAutoInputEnabled() { return myParams->autoInput; }
00648 
00661 bool vetCodec_IMG::isAutoOutputEnabled() { return myParams->autoOuput; }
00662 
00676 bool vetCodec_IMG::isBufferingEnabled() { return myParams->doBuffering; }
00677 
00693 vetCodec_IMGParameters::vetCodec_IMGParameters()
00694  {
00695         reset();
00696  }
00697 
00698 vetCodec_IMGParameters::vetCodec_IMGParameters(char* filename)
00699  {
00700         reset();
00701         setFileName(filename);
00702 }
00703 
00704 void vetCodec_IMGParameters::reset()
00705  {
00706         strcpy(fileNameBase, (const char*)"OUTPUT_1\0");
00707         setDoBuffering(true);
00708         setFileNameProgression(true);
00709         setFileNameIndex(-1);
00710         setAutoInputEnabled(false);
00711         setAutoOutputEnabled(true);
00712  }
00713 
00714 
00715 
00716 void vetCodec_IMGParameters::setFileName(const char *filename)
00717  {
00718         strncpy(fileNameBase, filename, 64);
00719  }
00720 
00721 
00722 void vetCodec_IMGParameters::getFileName(char *filename)
00723  {
00724         strcpy(filename, fileNameBase);
00725  }
00726 
00727 
00728 int vetCodec_IMGParameters::saveToStreamXML(FILE *fp)
00729  {
00730         if ( fp == NULL )
00731                 return VETRET_PARAM_ERR;
00732 
00733         if( fprintf(fp, "<vetCodec_IMGParameters>\n") == EOF )
00734                 return VETRET_INTERNAL_ERR;
00735 
00736         if ( fprintf(fp, "  <filename value=\"%s\" />\n", fileNameBase) == EOF)
00737                 return VETRET_INTERNAL_ERR;
00738 
00739         if ( fprintf(fp, "  <fileNameIndex value=\"%i\" />\n", fileNameIndex) == EOF)
00740                 return VETRET_INTERNAL_ERR;
00741 
00742         if ( fprintf(fp, "  <doBuffering value=\"%u\" />\n", (int)doBuffering) == EOF)
00743                 return VETRET_INTERNAL_ERR;
00744 
00745         if ( fprintf(fp, "  <autoOuput value=\"%u\" />\n", (int)autoOuput) == EOF)
00746                 return VETRET_INTERNAL_ERR;
00747 
00748         if ( fprintf(fp, "  <autoInput value=\"%u\" />\n", (int)autoInput) == EOF)
00749                 return VETRET_INTERNAL_ERR;
00750 
00751         if( fprintf(fp, "</vetCodec_IMGParameters>\n") == EOF )
00752                 return VETRET_INTERNAL_ERR;
00753 
00754         return VETRET_OK;
00755  }
00756 
00757 
00758 int vetCodec_IMGParameters::loadFromStreamXML(FILE *fp)
00759  {
00760         if ( fscanf(fp, "<vetCodec_IMGParameters>\n") == EOF )
00761                 throw "error in XML file, unable to import data.";
00762 
00763         if ( fscanf(fp, "  <filename value=\"%s\" />\n", fileNameBase) == EOF )
00764                 throw "error in XML file, unable to import data.";
00765 
00766         if ( fscanf(fp, "  <fileNameIndex value=\"%i\" />\n", &fileNameIndex) == EOF )
00767                 throw "error in XML file, unable to import data.";
00768 
00769         int boolTmp = 1;
00770         if ( fscanf(fp, "  <doBuffering value=\"%u\" />\n", &boolTmp) == EOF )
00771                 throw "error in XML file, unable to import data.";
00772 
00773         if (boolTmp == 0)
00774                 doBuffering = false;
00775         else
00776                 doBuffering = true;
00777 
00778         boolTmp = 0;
00779         if ( fscanf(fp, "  <autoOuput value=\"%u\" />\n", &boolTmp) == EOF )
00780                 throw "error in XML file, unable to import data.";
00781 
00782         if (boolTmp == 0)
00783                 autoOuput = false;
00784         else
00785                 autoOuput = true;
00786 
00787         boolTmp = 0;
00788         if ( fscanf(fp, "  <autoInput value=\"%u\" />\n", &boolTmp) == EOF )
00789                 throw "error in XML file, unable to import data.";
00790 
00791         if (boolTmp == 0)
00792                 autoInput = false;
00793         else
00794                 autoInput = true;
00795 
00796         return VETRET_OK;
00797  }
00798 
00799 
00800 
00801 

Generated on Tue Jan 24 11:58:58 2006 for VETLib by  doxygen 1.4.4