- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgPlugins/quicktime/QTImportExport.cpp
r12292 r13041 21 21 public: 22 22 QTImportExportException(int err, const std::string& msg) : std::exception(), _err(err), _msg(msg) {} 23 23 24 24 virtual const char* what() { return _msg.c_str(); } 25 25 int getErrorCode() { return _err; } 26 26 27 27 virtual ~QTImportExportException() throw () {} 28 28 29 29 private: 30 30 int _err; … … 32 32 }; 33 33 34 QuicktimeImportExport::QuicktimeImportExport() 35 : _error(0), 34 QuicktimeImportExport::QuicktimeImportExport() 35 : _error(0), 36 36 _lastError("") 37 37 { … … 43 43 // ---------------------------------------------------------------------------------------------------------- 44 44 45 void QuicktimeImportExport::flipImage(unsigned char* pixels, int bytesPerPixel, unsigned int width, unsigned height) 45 void QuicktimeImportExport::flipImage(unsigned char* pixels, int bytesPerPixel, unsigned int width, unsigned height) 46 46 { 47 47 // Flip the image … … 70 70 unsigned char *srcp = buffer; 71 71 unsigned int i, j; 72 72 73 73 int roffset, goffset, boffset, aoffset; 74 74 aoffset = -1; 75 75 int sourceStep; 76 76 77 77 switch (bytesPerPixel) { 78 78 case 1: … … 93 93 boffset = 0; 94 94 break; 95 96 } 97 98 for (i = 0; i < height; ++i ) 95 96 } 97 98 for (i = 0; i < height; ++i ) 99 99 { 100 for (j = 0; j < width; ++j ) 100 for (j = 0; j < width; ++j ) 101 101 { 102 102 dstp[0] = (aoffset < 0) ? 0 : srcp[aoffset]; … … 106 106 srcp+=sourceStep; 107 107 dstp+=4; 108 109 } 110 } 111 108 109 } 110 } 111 112 112 flipImage(pixels, bytesPerPixel, width, height); 113 113 return pixels; … … 121 121 // ---------------------------------------------------------------------------------------------------------- 122 122 123 unsigned char* QuicktimeImportExport::prepareBufferForQuicktime(unsigned char* buffer, GLenum pixelFormat, int bytesPerPixel, unsigned int width, unsigned int height) 123 unsigned char* QuicktimeImportExport::prepareBufferForQuicktime(unsigned char* buffer, GLenum pixelFormat, int bytesPerPixel, unsigned int width, unsigned int height) 124 124 { 125 125 unsigned char *pixels = new unsigned char [height * width * 4]; … … 127 127 unsigned char *srcp = buffer; 128 128 unsigned int i, j; 129 129 130 130 int roffset, goffset, boffset, aoffset; 131 131 aoffset = -1; 132 132 int sourceStep; 133 133 134 134 switch (bytesPerPixel) { 135 135 case 1: … … 152 152 boffset = 2; 153 153 break; 154 154 155 155 case GL_BGRA: 156 156 aoffset = 0; … … 161 161 } 162 162 } 163 164 165 for (i = 0; i < height; ++i ) 163 164 165 for (i = 0; i < height; ++i ) 166 166 { 167 for (j = 0; j < width; ++j ) 167 for (j = 0; j < width; ++j ) 168 168 { 169 169 dstp[0] = (aoffset < 0) ? 0 : srcp[aoffset]; … … 173 173 srcp+=sourceStep; 174 174 dstp+=4; 175 176 } 177 } 178 175 176 } 177 } 178 179 179 flipImage(pixels, 4, width, height); 180 180 181 181 return pixels; 182 182 … … 187 187 // ---------------------------------------------------------------------------------------------------------- 188 188 189 osg::Image* QuicktimeImportExport::readFromStream(std::istream & inStream, const std::string& fileTypeHint, long sizeHint) 189 osg::Image* QuicktimeImportExport::readFromStream(std::istream & inStream, const std::string& fileTypeHint, long sizeHint) 190 190 { 191 191 char* content = NULL; 192 192 long length = 0; 193 if (sizeHint != 0) 193 if (sizeHint != 0) 194 194 { 195 195 length = sizeHint; … … 197 197 inStream.read (content,length); 198 198 } 199 else 199 else 200 200 { 201 201 int readBytes(0), newBytes(0); 202 202 203 203 char buffer[10240]; 204 204 205 205 while (!inStream.eof()) { 206 206 inStream.read(buffer, 10240); … … 208 208 if (newBytes > 0) { 209 209 char* newcontent = new char[readBytes + newBytes]; 210 210 211 211 if (readBytes > 0) 212 212 memcpy(newcontent, content, readBytes); 213 213 214 214 memcpy(&newcontent[readBytes], &buffer, newBytes); 215 215 readBytes += newBytes; … … 220 220 length = readBytes; 221 221 } 222 222 223 223 osg::Image* img = doImport(reinterpret_cast<unsigned char*>(content), length, fileTypeHint); 224 224 225 225 if (content) delete[] content; 226 226 return img; 227 227 } 228 229 228 229 230 230 Handle getPtrDataRef(unsigned char *data, unsigned int size, const std::string &filename) 231 231 { … … 236 236 ComponentInstance dataRefHandler; 237 237 unsigned char pstr[255]; 238 238 239 239 ptrDataRefRec.data = data; 240 240 ptrDataRefRec.dataLength = size; 241 241 242 242 /*err = */PtrToHand(&ptrDataRefRec, &dataRef, sizeof(PointerDataRefRecord)); 243 243 244 244 // Open a Data Handler for the Data Reference 245 245 /*err = */OpenADataHandler(dataRef, PointerDataHandlerSubType, NULL, 246 246 (OSType)0, NULL, kDataHCanRead, &dataRefHandler); 247 247 248 248 // Convert From CString in filename to a PascalString in pstr 249 249 if (filename.length() > 255) { … … 253 253 } 254 254 CopyCStringToPascal(filename.c_str(), pstr); 255 255 256 256 // Add filename extension 257 257 /*err = */PtrToHand(pstr, &fileNameHandle, filename.length() + 1); … … 259 259 kDataRefExtensionFileName); 260 260 DisposeHandle(fileNameHandle); 261 261 262 262 // Release old handler which does not have the extensions 263 263 DisposeHandle(dataRef); 264 264 265 265 // Grab the SAFE_NEW version of the data ref from the data handler 266 266 /*err = */ DataHGetDataRef(dataRefHandler, &dataRef); 267 267 268 268 CloseComponent(dataRefHandler); 269 269 270 270 return dataRef; 271 271 } … … 285 285 unsigned int xsize, ysize; 286 286 unsigned char* imageData = 0; 287 287 288 288 // Data Handle for file data ( & load data from file ) 289 289 Handle dataRef = getPtrDataRef(data, sizeData, fileTypeHint); 290 290 291 291 try { 292 292 OSErr err = noErr; 293 293 294 294 // GraphicsImporter - Get Importer for our filetype 295 295 GetGraphicsImporterForDataRef(dataRef, 'ptr ', &gicomp); … … 299 299 if (err != noErr) { 300 300 throw QTImportExportException(err, "GraphicsImportGetNaturalBounds failed"); 301 301 302 302 } 303 303 xsize = (unsigned int)(rectImage.right - rectImage.left); … … 312 312 // ImageDescription - Get Bit Depth 313 313 HLock(reinterpret_cast<char **>(desc)); 314 314 315 315 316 316 // GWorld - Pixel Format stuff … … 348 348 throw QTImportExportException(0, "GetGWorldPixMap failed"); 349 349 } 350 351 350 351 352 352 //*** Draw GWorld into our Memory Texture! 353 353 GraphicsImportDraw(gicomp); 354 354 355 355 // Clean up 356 356 UnlockPixels(GetGWorldPixMap(gworld)); … … 360 360 DisposeHandle(reinterpret_cast<char **>(desc)); 361 361 DisposeHandle(dataRef); 362 } 363 catch (QTImportExportException& e) 362 } 363 catch (QTImportExportException& e) 364 364 { 365 365 setError(e.what()); 366 366 367 367 if (gworld) { 368 368 UnlockPixels(GetGWorldPixMap(gworld)); … … 372 372 if (gicomp) 373 373 CloseComponent(gicomp); 374 if (desc) 374 if (desc) 375 375 DisposeHandle(reinterpret_cast<char **>(desc)); 376 377 if (imageData) 376 377 if (imageData) 378 378 delete[] imageData; 379 379 if (dataRef) 380 380 DisposeHandle(dataRef); 381 381 382 382 return NULL; 383 383 } 384 385 386 384 385 386 387 387 unsigned int bytesPerPixel = depth / 8; 388 388 unsigned int glpixelFormat; … … 400 400 break; 401 401 } 402 402 403 403 unsigned char* swizzled = pepareBufferForOSG(imageData, bytesPerPixel, xsize, ysize); 404 404 405 405 delete[] imageData; 406 406 407 407 osg::Image* image = new osg::Image(); 408 408 image->setFileName(fileTypeHint.c_str()); … … 413 413 swizzled, 414 414 osg::Image::USE_NEW_DELETE ); 415 416 415 416 417 417 return image; 418 418 } 419 419 420 420 421 void QuicktimeImportExport::writeToStream(std::ostream& outStream, osg::Image* image, const std::string& fileTypeHint) 421 void QuicktimeImportExport::writeToStream(std::ostream& outStream, osg::Image* image, const std::string& fileTypeHint) 422 422 { 423 423 … … 439 439 } 440 440 441 441 442 442 std::map<std::string, OSType>::iterator cur = extmap.find(ext); 443 443 444 444 // can not handle this type of file, perhaps a movie? 445 445 if (cur == extmap.end()) 446 446 return; 447 447 448 448 unsigned int numBytes = image->computeNumComponents(image->getPixelFormat()); 449 449 unsigned char* pixels = prepareBufferForQuicktime( 450 450 image->data(), 451 image->getPixelFormat(), 451 image->getPixelFormat(), 452 452 numBytes, 453 453 image->s(), 454 454 image->t() 455 455 ); 456 457 456 457 458 458 OSType desiredType = cur->second; 459 459 GraphicsExportComponent geComp = NULL; … … 465 465 OSErr err = OpenADefaultComponent(GraphicsExporterComponentType, desiredType, &geComp); 466 466 Rect bounds = {0,0, image->t(), image->s()}; 467 467 468 468 err = QTNewGWorldFromPtr(&gw, k32ARGBPixelFormat, &bounds, 0,0,0, pixels, image->s()*4); 469 469 if (err != noErr) { 470 470 throw QTImportExportException(err, "could not create gworld for type " + ext); 471 471 } 472 472 473 473 err = GraphicsExportSetInputGWorld(geComp, gw); 474 474 if (err != noErr) { 475 475 throw QTImportExportException(err, "could not set input gworld for type " + ext); 476 476 } 477 477 478 478 err = GraphicsExportSetOutputHandle( geComp, dataHandle); 479 479 if (err != noErr) { 480 480 throw QTImportExportException(err, "could not set output file for type " + ext); 481 } 482 481 } 482 483 483 // Set the compression quality (needed for JPEG, not necessarily for other formats) 484 484 if (desiredType == kQTFileTypeJPEG) { … … 488 488 } 489 489 } 490 490 491 491 if(4 == numBytes) 492 492 { … … 494 494 } 495 495 // else k24RGBPixelFormat??? 496 496 497 497 // do the export 498 498 err = GraphicsExportDoExport(geComp, NULL); 499 499 if (err != noErr) { 500 500 throw QTImportExportException(err, "could not do the export for type " + ext); 501 } 502 501 } 502 503 503 if (geComp != NULL) 504 504 CloseComponent(geComp); 505 505 506 506 if (gw) DisposeGWorld (gw); 507 507 if (pixels) free(pixels); 508 508 509 509 outStream.write(*dataHandle, GetHandleSize(dataHandle)); 510 510 DisposeHandle(dataHandle); 511 511 } 512 513 514 catch (QTImportExportException& e) 512 513 514 catch (QTImportExportException& e) 515 515 { 516 516 setError(e.what()); 517 518 if (geComp != NULL) CloseComponent(geComp); 517 518 if (geComp != NULL) CloseComponent(geComp); 519 519 if (gw != NULL) DisposeGWorld (gw); 520 520 if (pixels) free(pixels); 521 521 522 522 DisposeHandle(dataHandle); 523 523
