Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGImageFileType.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 // System declarations
00039 #include <OSGConfig.h>
00040 #include <iostream>
00041 
00042 // Class declarations
00043 #include "OSGImageFileType.h"
00044 #include "OSGImageFileHandler.h"
00045 #include "OSGImageGenericAtt.h"
00046 #include "OSGLog.h"
00047 #include "OSGBaseFunctions.h"
00048 
00049 OSG_USING_NAMESPACE
00050 
00051 
00057 //-------------------------------------------------------------------------
00061 bool ImageFileType::Head::netToHost(void)
00062 {
00063     pixelFormat    = osgntohs(pixelFormat);
00064     width          = osgntohs(width);
00065     height         = osgntohs(height);
00066     depth          = osgntohs(depth);
00067     mipmapCount    = osgntohs(mipmapCount);
00068     frameCount     = osgntohs(frameCount);
00069     frameDelay     = osgntohs(frameDelay);
00070     sideCount      = osgntohs(sideCount);
00071     dataType       = osgntohs(dataType);
00072     _reserved3     = 0;
00073     _reserved4     = 0;
00074     attachmentSize = osgntohs(attachmentSize);
00075 
00076     return true;
00077 }
00078 
00079 
00080 //-------------------------------------------------------------------------
00084 bool ImageFileType::Head::hostToNet(void)
00085 {
00086     pixelFormat    = osghtons(pixelFormat);
00087     width          = osghtons(width);
00088     height         = osghtons(height);
00089     depth          = osghtons(depth);
00090     mipmapCount    = osghtons(mipmapCount);
00091     frameCount     = osghtons(frameCount);
00092     frameDelay     = osghtons(frameDelay);
00093     sideCount      = osghtons(sideCount);
00094     dataType       = osghtons(dataType);
00095     _reserved3     = 0;
00096     _reserved4     = 0;
00097     attachmentSize = osghtons(attachmentSize);
00098 
00099     return true;
00100 }
00101 
00102 //-------------------------------------------------------------------------
00106 const Char8 *ImageFileType::getMimeType(void) const
00107 {
00108     return _mimeType.str();
00109 }
00110 
00111 //-------------------------------------------------------------------------
00116 UInt32 ImageFileType::getFlags(void) const
00117 {
00118     return _flags;
00119 }
00120 
00121 //-------------------------------------------------------------------------
00125 const std::list<IDString> &ImageFileType::getSuffixList(void) const
00126 {
00127     return _suffixList; 
00128 }
00129 
00130 //-------------------------------------------------------------------------
00131 
00132 bool ImageFileType::validateHeader( const Char8 *fileName, bool &implemented)
00133 {
00134     implemented = false;
00135     return true;
00136 }
00137 
00138 //-------------------------------------------------------------------------
00143 ImageFileType::ImageFileType( const char *mimeType,
00144                               const Char8 *suffixArray[], 
00145                               UInt16 suffixByteCount,
00146                               UInt32 flags)
00147 {
00148     Int32 suffixCount = suffixByteCount / sizeof(const Char8 *);
00149     Int32 i = 0;
00150     std::list<IDString>::iterator sI;
00151 
00152     if (!mimeType) {
00153       FFATAL (("ImageFileType without valid mimeType\n"));
00154     }
00155     _mimeType.set(mimeType);
00156 
00157     _suffixList.resize(suffixCount);
00158     for(sI = _suffixList.begin(); sI != _suffixList.end(); sI++)
00159     {
00160         sI->set(suffixArray[i++]);
00161         SINFO << "add image suffix: " << *sI << endLog;
00162     }
00163 
00164     _flags = flags;
00165     
00166     ImageFileHandler::addImageFileType(*this);
00167 }
00168 
00169 //-------------------------------------------------------------------------
00173 ImageFileType::ImageFileType(const ImageFileType &obj) 
00174   : _suffixList(obj._suffixList)
00175 {
00176     SWARNING << "In ImageFileType copy constructor" << std::endl;
00177 }
00178 
00179 //-------------------------------------------------------------------------
00183 ImageFileType::~ImageFileType(void)
00184 {
00185   return;
00186 }
00187 
00188 //-------------------------------------------------------------------------
00193 UInt64 ImageFileType::restoreData(   ImagePtr  &, 
00194                                   const UChar8 *OSG_CHECK_ARG(buffer ),
00195                                         Int32   OSG_CHECK_ARG(memSize))
00196 {
00197   FWARNING(("ImageXFileType::restoreData() not impl. for mimeType %s\n",
00198             getMimeType()));
00199 
00200   return 0;
00201 }
00202 
00203 //-------------------------------------------------------------------------
00208 UInt64 ImageFileType::storeData(const ImagePtr &, 
00209                                       UChar8   *OSG_CHECK_ARG(buffer ),
00210                                       Int32     OSG_CHECK_ARG(memSize))
00211 {
00212   FWARNING(("ImageXFileType::storeData() not impl. for mimeType %s\n",
00213             getMimeType()));
00214   
00215   return 0;
00216 }
00217 
00218 //-------------------------------------------------------------------------
00223 UInt64 ImageFileType::restore( ImagePtr &image, 
00224                                const UChar8 *buffer, Int32 memSize)
00225 {
00226     unsigned long   imageSize, headSize = sizeof(Head);
00227     unsigned long   size = 0, attachmentSize;
00228     Head            head;
00229     const UChar8    *data = buffer ? (buffer + headSize) : 0;
00230     ImageFileType   *type;
00231     const char      *mimeType;
00232     Image::Type     dataType;
00233 
00234     if ((image != osg::NullFC) && buffer && (memSize >= headSize)) {
00235 
00236         // Copy header. Otherwise netToHost would change the original
00237         // data structur.
00238         memcpy(&head,buffer,sizeof(Head));
00239         head.netToHost();
00240         mimeType = head.mimeType;
00241         
00242         if((type = ImageFileHandler::the().getFileType(mimeType, 0)))
00243         {
00244             if (head.dataType)
00245               dataType = Image::Type(head.dataType);
00246             else
00247               dataType = Image::OSG_UINT8_IMAGEDATA;
00248 
00249             image->set(Image::PixelFormat(head.pixelFormat), head.width,
00250                        head.height, head.depth, head.mipmapCount,
00251                        head.frameCount, float(head.frameDelay) / 1000.0, 0,
00252                        dataType,true,head.sideCount );
00253             imageSize = static_cast<unsigned long>(
00254                 type->restoreData(image, data, memSize - headSize));
00255             attachmentSize = 0; // head->attachmentSize;
00256 
00257             /*
00258             if ((attachmentSize = head->attachmentSize))
00259             {
00260                 attData = (char*)(buffer + headSize + imageSize);
00261                 attKey = attData;
00262                 attValue = 0;
00263                 for (i = 0; i < (attachmentSize-1); i++) {
00264                     if (attData[i] == 0) 
00265                         if (attKey) {
00266                             attValue = &(attData[i+1]);
00267                             image->setAttachmentField (attKey,attValue);
00268                             attKey = attValue = 0;
00269                         }
00270                         else
00271                             attKey = &(attData[i+1]);
00272                 }
00273                 if (attKey || attValue) {
00274                     FFATAL (("Attachment restore error\n"));
00275                 }
00276             }
00277             */
00278 
00279             size = headSize + imageSize + attachmentSize;
00280       
00281             FDEBUG (( "Restore image data: %lu (%lu/%lu/%lu)\n",
00282                       size, headSize, imageSize, attachmentSize ));
00283 
00284         }
00285         else
00286         {
00287             imageSize = 0;
00288             FWARNING(("Can not restore image data, invalid mimeType: %s\n",
00289                       mimeType ? mimeType : "Unknown"));
00290         }
00291 
00292       
00293     }
00294 
00295     return size;
00296 }
00297 
00298 //-------------------------------------------------------------------------
00304 UInt64 ImageFileType::store(const ImagePtr &image, const char *mimeType,
00305                             UChar8 *buffer, Int32 memSize)
00306 {
00307   ImageFileType   *type = ImageFileHandler::the().getFileType(mimeType);
00308   
00309   return type ? type->store(image, buffer, memSize) : 0;
00310 }
00311 
00312 //-------------------------------------------------------------------------
00318 UInt64 ImageFileType::store(const ImagePtr &image,
00319                             UChar8 *buffer, Int32 memSize)
00320 {
00321     Head            *head;
00322     unsigned long   dataSize = 0, headSize = sizeof(Head);
00323     unsigned long   attachmentSize;
00324     UChar8          *dest;
00325     const UChar8    *src = image->getData();
00326     std::map<std::string, std::string>::const_iterator aI;
00327     std::string     value;
00328 
00329     attachmentSize = 0;
00330 
00331     // get attachment size
00332     /*
00333     ImageGenericAttPtr att=ImageGenericAttPtr::dcast(
00334         const_cast<Image*>(image.getCPtr())->findAttachment(
00335             ImageGenericAtt::getClassType().getGroupId()));
00336     if(att != NullFC)
00337     {
00338         for(i = 0; i < (att->getType().getNumFieldDescs()-1); ++i)
00339         {
00340             FieldDescription *fieldDesc=att->getType().getFieldDescription(i);
00341             Field *field=att->getField(i);
00342             if (fieldDesc && field) 
00343             {
00344                 field->getValueByStr(value);
00345                 attachmentSize += strlen( fieldDesc->getName().str() ) + 1;
00346                 attachmentSize += value.length() + 1;
00347               
00348                 std::cout << fieldDesc->getName().str() << std::endl; 
00349                 std::cout << value << std::endl; 
00350             }
00351             else 
00352             {
00353                 FFATAL (("Invalid Attachment in ImageFileType::store()\n"));
00354             }
00355         }
00356     }
00357     */
00358 
00359     if (buffer) 
00360     {
00361         head = (Head *)buffer;
00362 
00363         head->pixelFormat    = image->getPixelFormat();
00364         head->width          = image->getWidth();
00365         head->height         = image->getHeight();
00366         head->depth          = image->getDepth();
00367         head->mipmapCount    = image->getMipMapCount();
00368         head->frameCount     = image->getFrameCount();
00369         head->frameDelay     = short(image->getFrameDelay() * 1000.0);
00370         head->sideCount      = image->getSideCount();
00371         head->dataType       = image->getDataType();
00372         head->attachmentSize = static_cast<unsigned short>(attachmentSize);
00373         head->hostToNet();
00374       
00375         strcpy(head->mimeType, getMimeType());
00376       
00377         dest = (UChar8 *) (buffer + headSize);
00378 
00379         if (src) 
00380             dataSize = static_cast<unsigned long>(
00381                 storeData(image, dest, memSize - headSize));
00382 
00383         dest = (UChar8 *) (buffer + headSize + dataSize);
00384 
00385         /*
00386         if(att != NullFC)
00387         {
00388             for(i = 0; i < (att->getType().getNumFieldDescs()-1); ++i)
00389             {
00390                 FieldDescription *fieldDesc=att->getType().getFieldDescription(i);
00391                 Field *field=att->getField(i);
00392                 if (field && fieldDesc) 
00393                 {
00394                     field->getValueByStr(value);
00395 
00396                     l = strlen( fieldDesc->getName().str() );
00397                     for (i = 0; i < l; i++)
00398                       *dest++ = fieldDesc->getName().str()[i];
00399                     *dest++ = 0;
00400                     l = value.length();
00401                     for (i = 0; i < l; i++)
00402                       *dest++ = value[i];
00403                     *dest++ = 0;
00404                 }
00405                 else
00406                 {
00407                     FFATAL (("Invalid Attachment in ImageFileType::store()\n"));
00408                 }
00409             }
00410         }
00411         */
00412 
00413         FDEBUG (( "Store image data: %lu (%lu/%lu/%lu)\n",
00414                   headSize + dataSize + attachmentSize, headSize, dataSize, 
00415                   attachmentSize ));
00416     }
00417     else {
00418         FFATAL (("Invalid buffer in ImageFileType::store()\n"));
00419     }
00420   
00421     return (headSize + dataSize + attachmentSize);
00422 
00423 }
00424 
00425 //-------------------------------------------------------------------------
00430 UInt64 ImageFileType::maxBufferSize(const ImagePtr &image)
00431 {
00432     std::string value;
00433     unsigned long size, attachmentSize;
00434     unsigned long imageSize = image->getSize(), headSize = sizeof(Head);
00435 
00436     std::map<std::string, std::string>::const_iterator aI;
00437 
00438     attachmentSize = 0;
00439 
00440     // get attachment size
00441         /*
00442     ImageGenericAttPtr att=ImageGenericAttPtr::dcast(
00443         const_cast<Image*>(image.getCPtr())->findAttachment(
00444             ImageGenericAtt::getClassType().getGroupId()));
00445     if(att != NullFC)
00446     {
00447         for(i = 0; i < (att->getType().getNumFieldDescs()-1); ++i)
00448         {
00449             FieldDescription *fieldDesc=att->getType().getFieldDescription(i);
00450             Field *field=att->getField(i);
00451             if (field && fieldDesc) 
00452             {
00453                 field->getValueByStr(value);
00454                 attachmentSize += strlen( fieldDesc->getName().str() ) + 1;
00455                 attachmentSize += value.length() + 1;
00456             }
00457             else 
00458             {
00459                 FFATAL (("Invalid Attachment in ImageFileType::maxBufferSize()\n"));
00460             }
00461         }
00462     }
00463         */
00464 
00465     size = headSize + imageSize + attachmentSize;
00466   
00467     FINFO (( "ImageFileType::maxBufferSize(): %lu (%lu/%lu/%lu)\n", 
00468              size, headSize, imageSize, attachmentSize ));
00469   
00470     return size;
00471 }
00472 
00473 //-------------------------------------------------------------------------
00477 void ImageFileType::dump(void)
00478 {
00479     std::list<IDString>::iterator    sI;
00480 
00481     SLOG << getMimeType();
00482 
00483     if(_suffixList.empty())
00484     {
00485         SLOG << ": Suffix: ";
00486         for(sI = _suffixList.begin(); sI != _suffixList.end(); sI++)
00487         {
00488             Log().stream(OSG::LOG_DEBUG) << sI->str() << " ";
00489         }
00490     }
00491 
00492     std::cerr << std::endl;
00493 }

Generated on Thu Aug 25 04:06:20 2005 for OpenSG by  doxygen 1.4.3