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

vetCodec_BMP.cpp

Go to the documentation of this file.
00001 
00017 #include "vetCodec_BMP.h"
00018 
00019 #include <new>          // bad_alloc exception
00020 
00021 #include <string.h>     // memcpy
00022 #include "external_BMP/MicrosoftBMP_RT.h"
00023 #include "external_BMP/MicrosoftRCBMP_RT.h"
00024 #include "external_BMP/MicrosoftMonoBMP_RT.h"
00025 
00026 #include <stdlib.h> // itoa()
00027 
00028 
00029 
00030 vetCodec_BMP::vetCodec_BMP(const char *filename, FileFormat format) : vetFrameRGB96(), vetCodec()
00039  {
00040         DEBUGMSG("vetCodec_BMP::vetCodec_BMP(char *filename, FileFormat format) [CONTRUCTOR] ", *filename)
00041 
00042         myParams = NULL;
00043         reset();
00044         setParameters(NULL);
00045 
00046         load( filename, format );
00047 
00048         strcpy(myParams->fileNameBase, filename);
00049         myParams->fileFormat = format;
00050  }
00051 
00052 vetCodec_BMP::vetCodec_BMP( vetCodec_BMPParameters* initParams ) : vetFrameRGB96(), vetCodec()
00053  {
00054         DEBUGMSG("vetCodec_BMP::vetCodec_BMP(vetCodec_BMPParameters* initParams) [CONTRUCTOR] ", *filename)
00055 
00056         myParams = NULL;
00057         reset();
00058         setParameters(initParams);
00059  }
00060 
00061 VETRESULT vetCodec_BMP::reset()
00062  {
00063         INFO("int vetCodec_BMP::reset() [SET DEFAULT PARAMETERS]")
00064 
00065         setName("Bitmap Coder");
00066         setDescription("Read or write images to bitmap format.");
00067         setVersion(1.0);
00068 
00069         if (myParams != NULL)
00070          {
00071                 myParams->reset();
00072                 strcpy(fileNameBuffer, myParams->fileNameBase);
00073          }
00074 
00075         return VETRET_OK;
00076  }
00077 
00078 
00079 VETRESULT vetCodec_BMP::setParameters (vetCodec_BMPParameters* initParams)
00080  {
00081 
00082         if ( initParams == NULL )
00083                 myParams = new vetCodec_BMPParameters();
00084         else
00085                 myParams = initParams;
00086 
00087         return VETRET_OK;
00088  }
00089 
00090 bool vetCodec_BMP::EoF()        // buggy
00091  {
00092          if (data == NULL)
00093                 return true;
00094          return false;
00095  }
00096 
00097 VETRESULT vetCodec_BMP::extractTo(vetFrameYUV420& img)
00098  {
00099         DEBUGMSG("int vetCodec_BMP::extractTo(vetFrameYUV420& img) [pushing data]", doBuffering)
00100 
00101         VETRESULT ret = VETRET_OK;
00102 
00103         if ( myParams->autoOuput && !myParams->doBuffering )
00104          {
00105                 doFileNameCurrent();
00106 
00107 
00108                 ret = load(img, fileNameBuffer, myParams->fileFormat);
00109 
00110                 if ( myParams->fileNameProgression )
00111                         myParams->fileNameIndex++;
00112 
00113                 return ret;
00114          }
00115 
00116         if ( myParams->autoInput )
00117          {
00118                 doFileNameCurrent();
00119 
00120                 ret = load(fileNameBuffer, myParams->fileFormat);
00121 
00122                 if ( myParams->fileNameProgression )
00123                         myParams->fileNameIndex++;
00124          }
00125 
00126 
00127         vetFrameRGB96::operator >> (img);
00128 
00129         return ret;
00130  }
00131 
00132 VETRESULT vetCodec_BMP::extractTo(vetFrameRGB24& img)
00133  {
00134         DEBUGMSG("int vetCodec_BMP::extractTo(vetFrameRGB24& img) [pushing data]", doBuffering)
00135 
00136         VETRESULT ret = VETRET_OK;
00137 
00138         if ( myParams->autoOuput && !myParams->doBuffering )
00139          {
00140                 doFileNameCurrent();
00141 
00142 
00143                 ret = load(img, fileNameBuffer, myParams->fileFormat);
00144 
00145                 if ( myParams->fileNameProgression )
00146                         myParams->fileNameIndex++;
00147 
00148                 return ret;
00149          }
00150 
00151         if ( myParams->autoInput )
00152          {
00153                 doFileNameCurrent();
00154 
00155                 ret = load(fileNameBuffer, myParams->fileFormat);
00156 
00157                 if ( myParams->fileNameProgression )
00158                         myParams->fileNameIndex++;
00159          }
00160 
00161 
00162         vetFrameRGB96::operator >> (img);
00163 
00164         return ret;
00165  }
00166 
00167 
00168 VETRESULT vetCodec_BMP::extractTo(vetFrameT<unsigned char>& img)
00169  {
00170         DEBUGMSG("int vetCodec_BMP::extractTo(vetFrameT<unsigned char>& img) [pushing data]", doBuffering)
00171 
00172         return VETRET_NOT_IMPLEMENTED;
00173  }
00174 
00175 
00176 
00177 VETRESULT vetCodec_BMP::importFrom(vetFrameYUV420& img)
00178  {
00179         DEBUGMSG("int vetCodec_BMP::importFrom(vetFrameYUV420& img) [reading data]", doBuffering)
00180 
00181         VETRESULT ret = VETRET_OK;
00182 
00183         if ( myParams->autoOuput && !myParams->doBuffering )
00184          {
00185                 doFileNameCurrent();
00186 
00187                 ret = save(img, fileNameBuffer, myParams->fileFormat);
00188 
00189                 if ( myParams->fileNameProgression )
00190                         myParams->fileNameIndex++;
00191 
00192                 return ret;
00193          }
00194 
00195         vetFrameRGB96::operator << (img);
00196 
00197         if ( myParams->autoOuput )
00198          {
00199                 doFileNameCurrent();
00200 
00201                 ret = save(fileNameBuffer, myParams->fileFormat);
00202 
00203                 if ( myParams->fileNameProgression )
00204                         myParams->fileNameIndex++;
00205          }
00206 
00207         return ret;
00208  }
00209 
00210 
00211 VETRESULT vetCodec_BMP::importFrom(vetFrameRGB24& img)
00212  {
00213         DEBUGMSG("int vetCodec_BMP::importFrom(vetFrameRGB24& img) [reading data]", doBuffering)
00214 
00215         VETRESULT ret = VETRET_OK;
00216 
00217         if ( myParams->autoOuput && !myParams->doBuffering )
00218          {
00219                 doFileNameCurrent();
00220 
00221                 ret = save(img, fileNameBuffer, myParams->fileFormat);
00222 
00223                 if ( myParams->fileNameProgression )
00224                         myParams->fileNameIndex++;
00225 
00226                 return ret;
00227          }
00228 
00229         vetFrameRGB96::operator << (img);
00230 
00231         if ( myParams->autoOuput )
00232          {
00233                 doFileNameCurrent();
00234 
00235                 ret = save(fileNameBuffer, myParams->fileFormat);
00236 
00237                 if ( myParams->fileNameProgression )
00238                         myParams->fileNameIndex++;
00239          }
00240 
00241         return ret;
00242  }
00243 
00244 VETRESULT vetCodec_BMP::importFrom(vetFrameT<unsigned char>& img)
00245  {
00246         DEBUGMSG("int vetCodec_BMP::importFrom(vetFrameT<unsigned char>& img) [reading data]", doBuffering)
00247 
00248         return VETRET_NOT_IMPLEMENTED;
00249 
00250  }
00251 
00252 void vetCodec_BMP::setFileName(const char *filename)
00253  {
00254         strncpy(myParams->fileNameBase, filename, 64);
00255         strcpy(fileNameBuffer, myParams->fileNameBase);
00256  }
00257 
00258 
00259 void vetCodec_BMP::doFileNameCurrent()
00260  {
00261 
00262         sprintf( fileNameIndexBuffer, "%d", myParams->fileNameIndex );
00263 
00264         strcpy( fileNameBuffer, myParams->fileNameBase );
00265 
00266         if ( myParams->fileNameIndex != -1 )
00267                 strcat( fileNameBuffer, fileNameIndexBuffer );
00268 
00269         strcat( fileNameBuffer, (const char*)".bmp" );
00270 
00271         DEBUGMSG("void vetCodec_BMP::doFileNameCurrent()", fileNameBuffer);
00272 
00273  }
00274 
00275 
00276 void vetCodec_BMP::getFileNameCurrent(char* filename)
00277  {
00278         doFileNameCurrent();
00279         strcpy( filename, fileNameBuffer );
00280  }
00281 
00282 
00283 
00284 VETRESULT vetCodec_BMP::save()
00285  {
00286         INFO("int vetCodec_BMP::save() [saving buffered data]")
00287 
00288         VETRESULT ret = VETRET_OK;
00289 
00290         doFileNameCurrent();
00291         ret = save(reinterpret_cast<vetFrameRGB96&>(*this ), fileNameBuffer, myParams->fileFormat);
00292 
00293         return ret;
00294 }
00295 
00296 VETRESULT vetCodec_BMP::save(const char *filename, FileFormat format)
00297  {
00298         DEBUGMSG("int vetCodec_BMP::save(char *filename, FileFormat format) [saving buffered data]", filename)
00299 
00300         VETRESULT ret = VETRET_OK;
00301 
00302         ret = save(reinterpret_cast<vetFrameRGB96&>(*this), filename, format);
00303 
00304         return ret;
00305  }
00306 
00307 
00308 VETRESULT vetCodec_BMP::load()
00309  {
00310         INFO("int vetCodec_BMP::load() [loading data to buffer]")
00311 
00312         VETRESULT ret = VETRET_OK;
00313 
00314         doFileNameCurrent();
00315         ret = load(reinterpret_cast<vetFrameRGB96&>(*this), fileNameBuffer, myParams->fileFormat);
00316 
00317         return ret;
00318 }
00319 
00320 
00321 VETRESULT vetCodec_BMP::load(const char *filename, FileFormat format)
00322  {
00323         DEBUGMSG("int vetCodec_BMP::load(char *filename, FileFormat format) [loading data to buffer]", filename)
00324 
00325         VETRESULT ret = VETRET_OK;
00326 
00327         ret = load(reinterpret_cast<vetFrameRGB96&>(*this), filename, format);
00328 
00329         return ret;
00330  }
00331 
00332 
00334 //STATICs
00335 
00336 
00337 VETRESULT vetCodec_BMP::load(vetFrameRGB96& source, const char *filename, FileFormat format)
00338  {
00339         DEBUGMSG("int vetCodec_BMP::load(vetFrameCache& source, char *filename, ..) [loading file data to frame]", filename)
00340 
00341         VETRESULT ret = VETRET_OK;
00342 
00343         switch (format)
00344          {
00345                 case FORMAT_BMP_24:
00346                                 {
00347                                 MicrosoftBMP_RT bmp(filename);
00348 
00349                                 if ( !bmp.read() )
00350                                         throw("Invalid Bitmap file specified");
00351 
00352                                 source.reAllocCanvas(bmp.ImageWidth, bmp.ImageHeight);
00353                                 bmp.extract_data( (int *)source.data );
00354                                 }
00355                                 break;
00356 
00357                 case FORMAT_BMP_RC:
00358                                 {
00359                                 MicrosoftRCBMP_RT bmp(filename);
00360 
00361                                 if ( !bmp.read() )
00362                                         throw("Invalid Bitmap file specified");
00363 
00364                                 source.reAllocCanvas(bmp.ImageWidth, bmp.ImageHeight);
00365                                 bmp.extract_data( (int *)source.data );
00366                                 }
00367                                 break;
00368 
00369                 case FORMAT_BMP_MONO:
00370                                 {
00371                                 MicrosoftMonoBMP_RT bmp(filename);
00372 
00373                                 if ( !bmp.read() )
00374                                         throw("Invalid Bitmap file specified");
00375 
00376                                 source.reAllocCanvas(bmp.ImageWidth, bmp.ImageHeight);
00377                                 bmp.extract_data( (int *)source.data );
00378                                 }
00379                                 break;
00380 
00381                 default:        // FORMAT_BMP_ANY
00382                                 {
00383                                 // Check all formats
00384                                 MicrosoftBMP_RT* bmp;
00385                                 MicrosoftRCBMP_RT *bmp_rc;
00386                                 MicrosoftMonoBMP_RT *bmp_mono;
00387 
00388                                 if ( ( bmp = new  MicrosoftBMP_RT(filename) ) != NULL && bmp->read() )
00389                                  {
00390                                         // 24 bit bmp
00391                                         source.reAllocCanvas(bmp->ImageWidth, bmp->ImageHeight);
00392                                         bmp->extract_data( (int *)source.data );
00393 
00394                                  }
00395                                 else if ( (bmp_rc = new  MicrosoftRCBMP_RT(filename) ) != NULL && bmp_rc->read() )
00396                                  {
00397                                          // Reduced clour (palette) bmp
00398                                         source.reAllocCanvas(bmp->ImageWidth, bmp->ImageHeight);
00399                                         bmp->extract_data( (int *)source.data );
00400 
00401                                 }
00402                                 else if ( (bmp_mono = new  MicrosoftMonoBMP_RT(filename) ) != NULL && bmp_mono->read() )
00403                                  {
00404                                         // Monochrome (b+w) bmp
00405                                         source.reAllocCanvas(bmp->ImageWidth, bmp->ImageHeight);
00406                                         bmp->extract_data( (int *)source.data ) ;
00407 
00408                                  }
00409                                 }
00410                                 break;
00411 
00412          }
00413 
00414         return ret;
00415  }
00416 
00417 VETRESULT vetCodec_BMP::load(vetFrameRGB24& source, const char *filename, FileFormat format)
00418  {
00419         DEBUGMSG("int vetCodec_BMP::load(vetFrameRGB24& source, char *filename, ..) [loading file data to frame]", filename)
00420 
00421         VETRESULT ret = VETRET_OK;
00422 
00423         vetFrameRGB96 temp;
00424         ret = load(temp, filename, format);
00425 
00426         if ( ret == VETRET_OK )
00427                 source << temp; // todo better !!
00428 
00429         return ret;
00430  }
00431 
00432 VETRESULT vetCodec_BMP::load(vetFrameYUV420& source, const char *filename, FileFormat format)
00433  {
00434         DEBUGMSG("int vetCodec_BMP::load(vetFrameRGB96& source, char *filename, ..) [loading file data to frame]", filename)
00435 
00436         VETRESULT ret = VETRET_OK;
00437 
00438         return ret;
00439  }
00440 
00441 
00442 VETRESULT vetCodec_BMP::save(vetFrameRGB96& source, const char *filename, FileFormat format)
00443  {
00444         DEBUGMSG("int vetCodec_BMP::save(vetFrameRGB96& source, char *filename, FileFormat format) [saving frame to file]", filename)
00445 
00446         if ( source.width == 0 || source.height == 0 || source.data == NULL )
00447                 return VETRET_PARAM_ERR;
00448 
00449         VETRESULT ret = VETRET_OK;
00450 
00451         switch (format)
00452          {
00453                 case vetCodec_BMP::FORMAT_BMP_RC:
00454                                 {
00455                                 // Greyscale bitmap
00456                                 MicrosoftRCBMP_RT *btmp;
00457                                 btmp = new MicrosoftRCBMP_RT( filename );
00458                                 if ( btmp == NULL )
00459                                         ret = VETRET_CODER_FILEOUT_ER;
00460                                 else if ( !btmp->set_size(source.width, source.height) )
00461                                         ret = VETRET_CODER_SIZE_ER;
00462                                 else if ( !btmp->set_palette_grey256() )
00463                                         ret = VETRET_CODER_PALETTE_ER;
00464                                 else {
00465                                         btmp->put_data( (int *)source.data );
00466                                                                 if ( !btmp->write() )
00467                                                         ret = VETRET_CODER_WRITE_ER;
00468                                  }
00469                                 delete btmp ;
00470                                 break;
00471                                 }
00472                 case vetCodec_BMP::FORMAT_BMP_MONO:
00473                                 {
00474                                 // Monochrome bitmap
00475                                 MicrosoftMonoBMP_RT *btmp;
00476                                 btmp = new MicrosoftMonoBMP_RT( filename );
00477                                 if( btmp == NULL )
00478                                         ret = VETRET_CODER_FILEOUT_ER;
00479                                 else if ( !btmp->set_size(source.width, source.height) )
00480                                         ret = VETRET_CODER_SIZE_ER;
00481                                 else {
00482                                         btmp->put_data( (int *)source.data );
00483                                                                 if ( !btmp->write() )
00484                                                         ret = VETRET_CODER_WRITE_ER;
00485                                  }
00486                                 delete btmp ;
00487                                 break;
00488                                 }
00489 
00490                 default:        // vetCodec_BMP::FORMAT_BMP_24 or vetCodec_BMP::FORMAT_BMP_ANY
00491                                 {
00492                                 // 24 Bit (full colour) bitmap (+default)
00493                                 MicrosoftBMP_RT *btmp;
00494                                 btmp = new MicrosoftBMP_RT( filename );
00495 
00496                                 if ( btmp == NULL )
00497                                         ret = VETRET_CODER_FILEOUT_ER;
00498                                 else if ( !btmp->set_size(source.width, source.height) )
00499                                         ret = VETRET_CODER_SIZE_ER;
00500                                 else {
00501                                         btmp->put_data( (int *)source.data );
00502                                                                 if ( !btmp->write() )
00503                                                         ret = VETRET_CODER_WRITE_ER;
00504                                  }
00505                                 delete btmp ;
00506                                 break;
00507                                 }
00508 
00509          }
00510 
00511         return ret;
00512 }
00513 
00514 VETRESULT vetCodec_BMP::save(vetFrameRGB24& source, const char *filename, FileFormat format)
00515  {
00516         DEBUGMSG("int vetCodec_BMP::save(vetFrameRGB24& source, char *filename, FileFormat format) [saving frame to file]", filename)
00517 
00518         if ( source.getWidth() == 0 || source.getHeight() == 0 )
00519                 return VETRET_PARAM_ERR;
00520 
00521         VETRESULT ret = VETRET_OK;
00522         vetFrameRGB96 temp;
00523 
00524         source >> temp; // todobetter
00525 
00526         ret = save(temp, filename, format);
00527 
00528         return ret;
00529  }
00530 
00531 
00532 //slow
00533 VETRESULT vetCodec_BMP::save(vetFrameYUV420& source, const char *filename, FileFormat format)
00534  {
00535         DEBUGMSG("int vetCodec_BMP::save(vetFrameRGB96& source, char *filename, FileFormat format) [saving frame to file]", filename)
00536 
00537         if ( source.getWidth() == 0 || source.getHeight() == 0 )
00538                 return VETRET_PARAM_ERR;
00539 
00540         VETRESULT ret = VETRET_OK;
00541 
00542         return ret;
00543  }
00544 
00545 
00546 
00555 void vetCodec_BMP::setFileNameProgression(bool value) { myParams->fileNameProgression = value; }
00556 
00569 void vetCodec_BMP::setAutoOutputEnabled(bool value) { myParams->autoOuput = value; }
00570 
00583 void vetCodec_BMP::setAutoInputEnabled(bool value) { myParams->autoInput = value; }
00584 
00597 void vetCodec_BMP::setDoBuffering(bool value) { myParams->doBuffering = value; }
00598 
00599 void vetCodec_BMP::setFileFormat(FileFormat format) { myParams->fileFormat = format; }
00600 
00612 int vetCodec_BMP::getFileNameIndex() { return myParams->fileNameIndex; }
00621 bool vetCodec_BMP::isFileNameProgressionEnabled() { return myParams->fileNameProgression; }
00622 
00633 bool vetCodec_BMP::isAutoInputEnabled() { return myParams->autoInput; }
00634 
00647 bool vetCodec_BMP::isAutoOutputEnabled() { return myParams->autoOuput; }
00648 
00662 bool vetCodec_BMP::isBufferingEnabled() { return myParams->doBuffering; }
00663 
00675 vetCodec_BMP::FileFormat vetCodec_BMP::getFileFormat()
00676  {
00677   return myParams->fileFormat;
00678  }
00679 
00680 
00681 
00682 
00683 
00684 
00685 
00686 
00687 vetCodec_BMPParameters::vetCodec_BMPParameters()
00688  {
00689         reset();
00690  }
00691 
00692 vetCodec_BMPParameters::vetCodec_BMPParameters(const char* filename, vetCodec_BMP::FileFormat format)
00693  {
00694         reset();
00695         setFileName(filename);
00696         setFileFormat(format);
00697 }
00698 
00699 void vetCodec_BMPParameters::reset()
00700  {
00701         strcpy(fileNameBase, (const char*)"OUTPUT_1\0");
00702         setFileFormat();
00703         setDoBuffering(true);
00704         setFileNameProgression(true);
00705         setFileNameIndex(-1);
00706         setAutoInputEnabled(false);
00707         setAutoOutputEnabled(true);
00708  }
00709 
00710 
00711 
00712 void vetCodec_BMPParameters::setFileName(const char *filename)
00713  {
00714         strncpy(fileNameBase, filename, 64);
00715  }
00716 
00717 
00718 void vetCodec_BMPParameters::getFileName(char *filename)
00719  {
00720         strcpy(filename, fileNameBase);
00721  }
00722 
00723 
00724 int vetCodec_BMPParameters::saveToStreamXML(FILE *fp)
00725  {
00726         if ( fp == NULL )
00727                 return VETRET_PARAM_ERR;
00728 
00729         if( fprintf(fp, "<vetCodec_BMPParameters>\n") == EOF )
00730                 return VETRET_INTERNAL_ERR;
00731 
00732         if ( fprintf(fp, "  <filename value=\"%s\" />\n", fileNameBase) == EOF)
00733                 return VETRET_INTERNAL_ERR;
00734 
00735         if ( fprintf(fp, "  <fileFormat value=\"%i\" />\n", (int)fileFormat) == EOF)
00736                 return VETRET_INTERNAL_ERR;
00737 
00738         if ( fprintf(fp, "  <fileNameIndex value=\"%i\" />\n", fileNameIndex) == EOF)
00739                 return VETRET_INTERNAL_ERR;
00740 
00741         if ( fprintf(fp, "  <doBuffering value=\"%u\" />\n", (int)doBuffering) == EOF)
00742                 return VETRET_INTERNAL_ERR;
00743 
00744         if ( fprintf(fp, "  <autoOuput value=\"%u\" />\n", (int)autoOuput) == EOF)
00745                 return VETRET_INTERNAL_ERR;
00746 
00747         if ( fprintf(fp, "  <autoInput value=\"%u\" />\n", (int)autoInput) == EOF)
00748                 return VETRET_INTERNAL_ERR;
00749 
00750         if( fprintf(fp, "</vetCodec_BMPParameters>\n") == EOF )
00751                 return VETRET_INTERNAL_ERR;
00752 
00753         return VETRET_OK;
00754  }
00755 
00756 
00757 int vetCodec_BMPParameters::loadFromStreamXML(FILE *fp)
00758  {
00759         if ( fscanf(fp, "<vetCodec_BMPParameters>\n") == EOF )
00760                 throw "error in XML file, unable to import data.";
00761 
00762         if ( fscanf(fp, "  <filename value=\"%s\" />\n", fileNameBase) == EOF )
00763                 throw "error in XML file, unable to import data.";
00764 
00765         if ( fscanf(fp, "  <fileNameIndex value=\"%i\" />\n", &fileNameIndex) == EOF )
00766                 throw "error in XML file, unable to import data.";
00767 
00768         int boolTmp = 1;
00769         if ( fscanf(fp, "  <doBuffering value=\"%u\" />\n", &boolTmp) == EOF )
00770                 throw "error in XML file, unable to import data.";
00771 
00772         if (boolTmp == 0)
00773                 doBuffering = false;
00774         else
00775                 doBuffering = true;
00776 
00777         boolTmp = 0;
00778         if ( fscanf(fp, "  <autoOuput value=\"%u\" />\n", &boolTmp) == EOF )
00779                 throw "error in XML file, unable to import data.";
00780 
00781         if (boolTmp == 0)
00782                 autoOuput = false;
00783         else
00784                 autoOuput = true;
00785 
00786         boolTmp = 0;
00787         if ( fscanf(fp, "  <autoInput value=\"%u\" />\n", &boolTmp) == EOF )
00788                 throw "error in XML file, unable to import data.";
00789 
00790         if (boolTmp == 0)
00791                 autoInput = false;
00792         else
00793                 autoInput = true;
00794 
00795         return VETRET_OK;
00796  }
00797 
00798 
00799 
00800 

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