00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <OSGConfig.h>
00040 #include <iostream>
00041
00042
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
00237
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;
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
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
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
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
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
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
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
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 }