void pxBuffer::blit(pxSurfaceNative s, int dstLeft, int dstTop, int dstWidth, int dstHeight, int srcLeft, int srcTop) { Rect pr; MacSetRect(&pr, 0, 0, width(), height()); GWorldPtr gworld; NewGWorldFromPtr (&gworld, 32, &pr, NULL, NULL, 0, (char*)base(), 4*width()); Rect dr, sr; MacSetRect(&dr, dstLeft, dstTop, dstLeft + dstWidth, dstTop + dstHeight); MacSetRect(&sr, srcLeft, srcTop, srcLeft + dstWidth, srcTop + dstHeight); CopyBits((BitMapPtr)*GetGWorldPixMap(gworld),(BitMapPtr)*GetGWorldPixMap(s),&sr,&dr,srcCopy,NULL); DisposeGWorld(gworld); }
void qtCanvas::Impl::setupImage() { MacSetRect(&mFrame, 0, 0, mWidth, mHeight); mImageStride = mWidth * 4; mImageData = new unsigned char[mImageStride * mHeight]; OSErr err; err = QTNewGWorldFromPtr(&mGWorld, k32ARGBPixelFormat, &mFrame, nil, nil, (GWorldFlags)0, mImageData, mImageStride); CheckError(err, "QTNewGWorldFromPtr"); }
//------------------------------------------------------------------------ void pixel_map::draw(WindowRef window, const Rect *device_rect, const Rect *pmap_rect) const { if(m_pmap == nil || m_buf == NULL) return; PixMapHandle pm = GetGWorldPixMap (m_pmap); CGrafPtr port = GetWindowPort (window); Rect dest_rect; // Again, I used the Quicktime version. // Good old 'CopyBits' does better interpolation when scaling // but does not support all pixel depths. MacSetRect (&dest_rect, 0, 0, this->width(), this->height()); ImageDescriptionHandle image_description; MakeImageDescriptionForPixMap (pm, &image_description); if (image_description != nil) { DecompressImage (GetPixBaseAddr (pm), image_description, GetPortPixMap (port), nil, &dest_rect, ditherCopy, nil); DisposeHandle ((Handle) image_description); } }
void onMouseUp(int x, int y, unsigned long flags) { // Demonstrate doing some native drawing outside of the paint loop pxSurfaceNative s; if (PX_OK == beginNativeDrawing(s)) { #if defined(PX_PLATFORM_WIN) HGDIOBJ oldBrush = SelectObject(s, ::GetStockObject(NULL_BRUSH)); Ellipse(s, x-50, y-50, x+50, y+50); SelectObject(s, oldBrush); #elif defined(PX_PLATFORM_MAC) SetPort(s); Rect r; MacSetRect(&r, x-50, y-50, x+50, y+50); FrameOval(&r); #elif defined(PX_PLATFORM_X11) XDrawArc(s->display, s->drawable, s->gc, x-50, y-50, 100, 100, 0, 360*64); #endif endNativeDrawing(s); } }
//------------------------------------------------------------------------ void pixel_map::create(unsigned width, unsigned height, org_e org, unsigned clear_val) { destroy(); if(width == 0) width = 1; if(height == 0) height = 1; m_bpp = org; Rect r; int row_bytes = calc_row_len (width, m_bpp); MacSetRect(&r, 0, 0, width, height); m_buf = new unsigned char[m_img_size = row_bytes * height]; // The Quicktime version for creating GWorlds is more flexible than the classical function. QTNewGWorldFromPtr (&m_pmap, m_bpp, &r, nil, nil, 0, m_buf, row_bytes); // create_gray_scale_palette(m_pmap); I didn't care about gray scale palettes so far. if(clear_val <= 255) { memset(m_buf, clear_val, m_img_size); } }
void ofQtVideoSaver::setup( int width , int height, string movieName){ w = width; h = height; fileName = (ofToDataPath(movieName)); //pszFlatFilename = flatFileName; initializeQuicktime(); /* Load the FSSpec structure to describe the receiving file. For a description of this and related calls see http://developer.apple.com/quicktime/icefloe/dispatch004.html. ================================================================ */ #ifdef TARGET_WIN32 //FILE * pFile = NULL; //pFile = fopen (fileName.c_str(),"w"); //fclose (pFile); char fileNameStr[255]; sprintf(fileNameStr, "%s", fileName.c_str()); osErr = NativePathNameToFSSpec (fileNameStr, &fsSpec, 0); #endif #ifdef TARGET_OSX /// kill a file and make a new one if needed: FILE * pFile; pFile = fopen (fileName.c_str(),"w"); fclose (pFile); Boolean isdir; osErr = FSPathMakeRef((const UInt8*)fileName.c_str(), &fsref, &isdir); osErr = FSGetCatalogInfo(&fsref, kFSCatInfoNone, NULL, NULL, &fsSpec, NULL); #endif if (osErr && (osErr != fnfErr)) /* File-not-found error is ok */ { printf ("getting FSS spec failed %d\n", osErr); goto bail; } /* Step 1: Create a new, empty movie file and a movie that references that file (CreateMovieFile). ======================================================================== */ osErr = CreateMovieFile ( &fsSpec, /* FSSpec specifier */ FOUR_CHAR_CODE('TVOD'), /* file creator type, TVOD = QT player*/ smCurrentScript, /* movie script system to use */ createMovieFileDeleteCurFile /* movie file creation flags */ | createMovieFileDontCreateResFile, &sResRefNum, /* returned file ref num to data fork */ &movie /* returned handle to open empty movie*/ /* that references the created file */ ); if (osErr) { printf ("CreateMovieFile failed %d\n", osErr); goto bail; } /* Step 2: Add a new track to that movie (NewMovieTrack). ======================================================= */ track = NewMovieTrack ( movie, /* the movie to add track to */ ((long) w << 16), /* width of track in pixels (Fixed) */ FixRatio (h, 1), /* height of track in pixels (Fixed) */ kNoVolume /* default volume level */ ); osErr = GetMoviesError (); if (osErr) { printf ("NewMovieTrack failed %d\n", osErr); goto bail; } /* Step 3: Add a new media to that track (NewTrackMedia). ======================================================= */ media = NewTrackMedia ( track, /* the track to add the media to */ VideoMediaType, /* media type, e.g. SoundMediaType */ 600, /* num media time units that elapse/sec*/ NULL, /* ptr to file that holds media sampls*/ 0 /* type of ptr to media samples */ ); osErr = GetMoviesError (); if (osErr) { printf ("NewTrackMedia failed %d\n", osErr); goto bail; } /* Step 4: Add media samples to the media. ======================================== */ BeginMediaEdits (media); /* Inform the Movie Toolbox that we */ /* want to change the media samples */ /* referenced by a track's media. */ /* This opens the media container */ /* and makes it ready to receive */ /* and/or remove sample data. */ // Step 5: setup graphics port for qt movie and compression type --- /* Create a new offscreen graphics world that will hold the movie's drawing surface. draw_image() copies the image of IceFlow to this surface with varying amounts of transparency. ================================================================= */ MacSetRect (&rect, 0, 0, w, h); osErr = NewGWorld ( &pMovieGWorld, /* receives the new GWorld. */ 24, /* pixel depth in bits/pixel */ &rect, /* desired size of the GWorld. */ NULL, NULL, (GWorldFlags) 0 ); if (osErr != noErr) { printf ("NewGWorld 1 failed %d\n", osErr); goto bail; } /* Retrieve the pixel map associated with that graphics world and lock the pixel map in memory. GetMaxCompressionSize() and CompressImage() only operate on pixel maps, not graphics worlds. ===================================================================== */ pixMapHandle = GetGWorldPixMap (pMovieGWorld); if (pixMapHandle == NULL) { printf ("GetGWorldPixMap failed\n"); goto bail; } LockPixels (pixMapHandle); /* Get the maximum number of bytes required to hold an image having the specified characteristics compressed using the specified compressor. ==================================================================== */ osErr = GetMaxCompressionSize ( pixMapHandle, /* the pixel map to compress from. */ &rect, /* the image rectangle. */ 0, /* let ICM choose image bit depth. */ codecHighQuality, /* compression quality specifier. */ kRawCodecType, /* desired compression type */ // < set to RAW in case we set to a new compression type... (CompressorComponent) anyCodec, /* codec specifier. */ &lMaxCompressionSize /* receives max bytes needed for cmp. */ ); if (osErr != noErr) { printf ("GetMaxCompressionSize failed %d\n", osErr); goto bail; } /* Allocate a buffer to hold the compressed image data by creating a new handle. ===================================================================== */ hCompressedData = NewHandle (lMaxCompressionSize); if (hCompressedData == NULL) { printf ("NewHandle(%ld) failed\n", lMaxCompressionSize); goto bail; } /* Lock the handle and then dereference it to obtain a pointer to the data buffer because CompressImage() wants us to pass it a pointer, not a handle. ======================================================================= */ HLockHi (hCompressedData); pCompressedData = *hCompressedData; /* Create an image description object in memory of minimum size to pass to CompressImage(). CompressImage() will resize the memory as necessary so create it small here. ==================================================================== */ hImageDescription = (ImageDescriptionHandle) NewHandle (4); if (hImageDescription == NULL) { printf ("NewHandle(4) failed\n"); goto bail; } bSetupForRecordingMovie = true; return; bail: printf("got to bail somehows \n"); if (sResRefNum != 0) CloseMovieFile (sResRefNum); if (movie != NULL) DisposeMovie (movie); //ExitMovies (); /* Finalize Quicktime */ return; }
//-------------------------------------------------------------------- bool ofVideoGrabber::initGrabber(int w, int h, bool setUseTexture){ bUseTexture = setUseTexture; //--------------------------------- #ifdef OF_VIDEO_CAPTURE_QUICKTIME //--------------------------------- //---------------------------------- 1 - open the sequence grabber if( !qtInitSeqGrabber() ){ ofLog(OF_LOG_ERROR, "error: unable to initialize the seq grabber"); return false; } //---------------------------------- 2 - set the dimensions width = w; height = h; MacSetRect(&videoRect, 0, 0, width, height); //---------------------------------- 3 - buffer allocation // Create a buffer big enough to hold the video data, // make sure the pointer is 32-byte aligned. // also the rgb image that people will grab offscreenGWorldPixels = (unsigned char*)malloc(4 * width * height + 32); pixels = new unsigned char[width*height*3]; QTNewGWorldFromPtr (&videogworld, k32ARGBPixelFormat, &videoRect, NULL, NULL, 0, offscreenGWorldPixels, 4 * width); LockPixels(GetGWorldPixMap(videogworld)); SetGWorld (videogworld, NULL); SGSetGWorld(gSeqGrabber, videogworld, nil); //---------------------------------- 4 - device selection bool didWeChooseADevice = bChooseDevice; bool deviceIsSelected = false; //if we have a device selected then try first to setup //that device if(didWeChooseADevice){ deviceIsSelected = qtSelectDevice(deviceID, true); if(!deviceIsSelected && bVerbose) ofLog(OF_LOG_WARNING, "unable to open device[%i] - will attempt other devices", deviceID); } //if we couldn't select our required device //or we aren't specifiying a device to setup //then lets try to setup ANY device! if(deviceIsSelected == false){ //lets list available devices listDevices(); setDeviceID(0); deviceIsSelected = qtSelectDevice(deviceID, false); } //if we still haven't been able to setup a device //we should error and stop! if( deviceIsSelected == false){ goto bail; } //---------------------------------- 5 - final initialization steps OSStatus err; err = SGSetChannelUsage(gVideoChannel,seqGrabPreview); if ( err != noErr ) goto bail; err = SGSetChannelBounds(gVideoChannel, &videoRect); if ( err != noErr ) goto bail; err = SGPrepare(gSeqGrabber, true, false); //theo swapped so preview is true and capture is false if ( err != noErr ) goto bail; err = SGStartPreview(gSeqGrabber); if ( err != noErr ) goto bail; bGrabberInited = true; loadSettings(); ofLog(OF_LOG_NOTICE,"end setup ofVideoGrabber"); ofLog(OF_LOG_NOTICE,"-------------------------------------\n"); //---------------------------------- 6 - setup texture if needed if (bUseTexture){ // create the texture, set the pixels to black and // upload them to the texture (so at least we see nothing black the callback) tex.allocate(width,height,GL_RGB); memset(pixels, 0, width*height*3); tex.loadData(pixels, width, height, GL_RGB); } // we are done return true; //--------------------- (bail) something's wrong ----- bail: ofLog(OF_LOG_ERROR, "***** ofVideoGrabber error *****"); ofLog(OF_LOG_ERROR, "-------------------------------------\n"); //if we don't close this - it messes up the next device! if(bSgInited) qtCloseSeqGrabber(); bGrabberInited = false; return false; //--------------------------------- #endif //--------------------------------- //--------------------------------- #ifdef OF_VIDEO_CAPTURE_DIRECTSHOW //--------------------------------- if (bChooseDevice){ device = deviceID; ofLog(OF_LOG_NOTICE, "choosing %i", deviceID); } else { device = 0; } width = w; height = h; bGrabberInited = false; bool bOk = VI.setupDevice(device, width, height); int ourRequestedWidth = width; int ourRequestedHeight = height; if (bOk == true){ bGrabberInited = true; width = VI.getWidth(device); height = VI.getHeight(device); if (width == ourRequestedWidth && height == ourRequestedHeight){ bDoWeNeedToResize = false; } else { bDoWeNeedToResize = true; width = ourRequestedWidth; height = ourRequestedHeight; } pixels = new unsigned char[width * height * 3]; if (bUseTexture){ // create the texture, set the pixels to black and // upload them to the texture (so at least we see nothing black the callback) tex.allocate(width,height,GL_RGB); memset(pixels, 0, width*height*3); tex.loadData(pixels, width, height, GL_RGB); } return true; } else { ofLog(OF_LOG_ERROR, "error allocating a video device"); ofLog(OF_LOG_ERROR, "please check your camera with AMCAP or other software"); bGrabberInited = false; return false; } //--------------------------------- #endif //--------------------------------- //--------------------------------- #ifdef OF_VIDEO_CAPTURE_UNICAP //-------------------------------- if( !bGrabberInited ){ if ( !bChooseDevice ){ deviceID = 0; } width = w; height = h; pixels = new unsigned char[width * height * 3]; if (bUseTexture){ // create the texture, set the pixels to black and // upload them to the texture (so at least we see nothing black the callback) tex.allocate(width,height,GL_RGB); memset(pixels, 0, width*height*3); tex.loadData(pixels, width, height, GL_RGB); } bGrabberInited = ucGrabber.open_device (deviceID); if( bGrabberInited ){ ofLog(OF_LOG_NOTICE, "choosing device %i: %s", deviceID,ucGrabber.device_identifier()); ucGrabber.set_format(w,h); ucGrabber.start_capture(); } } return bGrabberInited; //--------------------------------- #endif //--------------------------------- //--------------------------------- #ifdef OF_VIDEO_CAPTURE_GSTREAMER //-------------------------------- if(gstUtils.initGrabber(w,h)){ if ( !bChooseDevice ){ deviceID = 0; } width = w; height = h; if (bUseTexture){ // create the texture, set the pixels to black and // upload them to the texture (so at least we see nothing black the callback) tex.allocate(width,height,GL_RGB); tex.loadData(gstUtils.getPixels(), width, height, GL_RGB); } bGrabberInited = true; ofLog(OF_LOG_VERBOSE, "ofVideoGrabber: initied"); }else{ bGrabberInited = false; ofLog(OF_LOG_ERROR, "ofVideoGrabber: couldn't init"); } return bGrabberInited; //--------------------------------- #endif //--------------------------------- //--------------------------------- #ifdef OF_VIDEO_CAPTURE_V4L //-------------------------------- if (bChooseDevice){ device = deviceID; } else { device = 0; } sprintf(dev_name, "/dev/video%i", device); ofLog(OF_LOG_NOTICE, "choosing device %s",dev_name); bool bOk = initV4L(w, h, dev_name); if (bOk == true){ bV4LGrabberInited = true; width = getV4L_Width(); height = getV4L_Height(); pixels = new unsigned char[width * height * 3]; if (bUseTexture){ // create the texture, set the pixels to black and // upload them to the texture (so at least we see nothing black the callback) tex.allocate(width,height,GL_RGB); //memset(pixels, 0, width*height*3); //tex.loadData(pixels, width, height, GL_RGB); } ofLog(OF_LOG_NOTICE, "success allocating a video device "); return true; } else { ofLog(OF_LOG_ERROR, "error allocating a video device"); ofLog(OF_LOG_ERROR, "please check your camera and verify that your driver is correctly installed."); return false; } //--------------------------------- //--------------------------------- #endif //--------------------------------- }
//-------------------------------------------------------------------- bool ofQuickTimeGrabber::initGrabber(int w, int h){ //--------------------------------- #ifdef OF_VIDEO_CAPTURE_QUICKTIME //--------------------------------- //---------------------------------- 1 - open the sequence grabber if( !qtInitSeqGrabber() ){ ofLogError("ofQuickTimeGrabber") << "initGrabber(): unable to initialize the seq grabber"; return false; } //---------------------------------- 2 - set the dimensions //width = w; //height = h; MacSetRect(&videoRect, 0, 0, w, h); //---------------------------------- 3 - buffer allocation // Create a buffer big enough to hold the video data, // make sure the pointer is 32-byte aligned. // also the rgb image that people will grab offscreenGWorldPixels = (unsigned char*)malloc(4 * w * h + 32); pixels.allocate(w, h, OF_IMAGE_COLOR); #if defined(TARGET_OSX) && defined(__BIG_ENDIAN__) QTNewGWorldFromPtr (&(videogworld), k32ARGBPixelFormat, &(videoRect), NULL, NULL, 0, (offscreenGWorldPixels), 4 * w); #else QTNewGWorldFromPtr (&(videogworld), k24RGBPixelFormat, &(videoRect), NULL, NULL, 0, (pixels.getPixels()), 3 * w); #endif LockPixels(GetGWorldPixMap(videogworld)); SetGWorld (videogworld, NULL); SGSetGWorld(gSeqGrabber, videogworld, nil); //---------------------------------- 4 - device selection bool didWeChooseADevice = bChooseDevice; bool deviceIsSelected = false; //if we have a device selected then try first to setup //that device if(didWeChooseADevice){ deviceIsSelected = qtSelectDevice(deviceID, true); if(!deviceIsSelected && bVerbose) ofLogError("ofQuickTimeGrabber") << "initGrabber(): unable to open device[" << deviceID << "], will attempt other devices"; } //if we couldn't select our required device //or we aren't specifiying a device to setup //then lets try to setup ANY device! if(deviceIsSelected == false){ //lets list available devices listDevices(); setDeviceID(0); deviceIsSelected = qtSelectDevice(deviceID, false); } //if we still haven't been able to setup a device //we should error and stop! if( deviceIsSelected == false){ goto bail; } //---------------------------------- 5 - final initialization steps OSStatus err; err = SGSetChannelUsage(gVideoChannel,seqGrabPreview); if ( err != noErr ) goto bail; //----------------- callback method for notifying new frame err = SGSetChannelRefCon(gVideoChannel, (long)&bHavePixelsChanged ); if(!err) { VideoBottles vb; /* get the current bottlenecks */ vb.procCount = 9; err = SGGetVideoBottlenecks(gVideoChannel, &vb); if (!err) { myGrabCompleteProc = NewSGGrabCompleteBottleUPP(frameIsGrabbedProc); vb.grabCompleteProc = myGrabCompleteProc; /* add our GrabFrameComplete function */ err = SGSetVideoBottlenecks(gVideoChannel, &vb); } } err = SGSetChannelBounds(gVideoChannel, &videoRect); if ( err != noErr ) goto bail; err = SGPrepare(gSeqGrabber, true, false); //theo swapped so preview is true and capture is false if ( err != noErr ) goto bail; err = SGStartPreview(gSeqGrabber); if ( err != noErr ) goto bail; bGrabberInited = true; loadSettings(); if( attemptFramerate >= 0 ){ err = SGSetFrameRate(gVideoChannel, IntToFixed(attemptFramerate) ); if ( err != noErr ){ ofLogError("ofQuickTimeGrabber") << "initGrabber: couldn't setting framerate to " << attemptFramerate << ": OSStatus " << err; } } ofLogNotice("ofQuickTimeGrabber") << " inited grabbed "; ofLogNotice("ofQuickTimeGrabber") << "-------------------------------------"; // we are done return true; //--------------------- (bail) something's wrong ----- bail: ofLogError("ofQuickTimeGrabber") << "***** ofQuickTimeGrabber error *****"; ofLogError("ofQuickTimeGrabber") << "------------------------------------"; //if we don't close this - it messes up the next device! if(bSgInited) qtCloseSeqGrabber(); bGrabberInited = false; return false; //--------------------------------- #else //--------------------------------- return false; //--------------------------------- #endif //--------------------------------- }
// Draws the current ATSUI data. Takes a GrafPtr as an argument so // that it can handle printing as well as drawing into a window. // void DrawATSUIStuff(GrafPtr drawingPort) { GrafPtr savedPort; Rect portBounds, quarterRect2, quarterRect3; float windowHeight, quarter; CGContextRef context; TXNTextBoxOptionsData optionsData; Boolean needToUseCGStrokeMethod; // Set up the GrafPort GetPort(&savedPort); SetPort(drawingPort); GetPortBounds(drawingPort, &portBounds); EraseRect(&portBounds); // Divide the window into vertical quarters, and draw the text in the middle two quarters windowHeight = portBounds.bottom - portBounds.top; quarter = windowHeight / 4.0; MacSetRect(&quarterRect2, portBounds.left, portBounds.top + quarter, portBounds.right, portBounds.bottom - (quarter * 2.0)); FrameRect(&quarterRect2); MacSetRect(&quarterRect3, portBounds.left, portBounds.top + (quarter * 2.0), portBounds.right, portBounds.bottom - quarter); FrameRect(&quarterRect3); // Set up the CGContext if (gNewCG) QDBeginCGContext(drawingPort, &context); else CreateCGContextForPort(drawingPort, &context); // Setup the options to pass into TXNDrawUnicodeTextBox optionsData.optionTags = kTXNUseCGContextRefMask | kTXNSetFlushnessMask | kTXNUseFontFallBackMask; optionsData.flushness = X2Frac(0.5); // Center the text horizontally, just for this demo. optionsData.options = (void *)context; // This parameter really needs to be renamed, see 3198383. // Draw the text once without the extr bold verify_noerr( TXNDrawUnicodeTextBox(gText, gLength, &quarterRect2, gStyle, &optionsData) ); // ---------------------------------------------------------- // // Here is where we change the setting to do the extra stroke // The value of gStrokeThicknessFactor determines how thick the extra stroke is. // The "standard" value used by ATSUI is 0.024; // this was changed to 0.044 for bug 3189696, // and will probably be changed back, so if you // want the extra stroke, you will have to do it // manually, as is done below. // // The extra stroke method: // - will look good on-screen when CG anti-aliasing is ON // - will look good when printing // - will *NOT* look good on-screen when CG anti-aliasing is OFF // (just use kATSUQDBoldfaceTag in that case) // needToUseCGStrokeMethod = gCurrentlyPrinting || IsAntiAliased(gPointSize); if ( needToUseCGStrokeMethod ) { CGContextSaveGState(context); CGContextSetTextDrawingMode(context, kCGTextFillStroke); CGContextSetLineWidth(context, gStrokeThicknessFactor * Fix2X(gPointSize)); // You might want to call CGContextSetStrokeColor() here, // just to make certain it is the same as the text/fill color. } else MySetBoldfaceTag(gStyle); // This will look very strong on-screen when CG anti-aliasing is off // Draw the text again with the extra bold for comparison verify_noerr( TXNDrawUnicodeTextBox(gText, gLength, &quarterRect3, gStyle, &optionsData) ); // Undo the previous CG text mode setting if ( needToUseCGStrokeMethod ) CGContextRestoreGState(context); else MyClearBoldfaceTag(gStyle); // Tear down the CGContext since we are done with it if (gNewCG) QDEndCGContext(drawingPort, &context); else CGContextRelease(context); // Restore the GrafPort SetPort(savedPort); }
GLuint platformLoadTextureFile(string theFilename, bool generateMipmaps, int *width, int *height) { #ifdef QuickTimeInstalled GLuint textureName; // the "name" by which OpenGL knows the texture unsigned char *pImageBuffer; // the buffer that contains the image data GWorldPtr pGWorld; // a gworld to load the image into int imageDepth; FSSpec fsspecImage; // FSSpec of the image to load #ifdef __APPLE__ CFURLRef imageURL; FSRef imagefsRef; #endif OSStatus err = noErr; // err return value long rowStride; // length, in bytes, of a pixel row in the image GraphicsImportComponent giComp; // component for importing image MatrixRecord matrix; Rect rectImage; // rectangle of source image PixMapHandle hPixMap; // handle to image pix map ImageDescriptionHandle hImageDesc; // handle to image description used to get image depth long imageWidth; long imageHeight; float imageAspect; long textureWidth; long textureHeight; // get the full path to the texture file string fileLocation; fileLocation = pathToResourceDirectory() + theFilename; #ifdef __APPLE__ // create a URL to the file from the C string imageURL = CFURLCreateWithBytes (kCFAllocatorDefault, (UInt8 *)fileLocation.c_str(), fileLocation.length(), kCFStringEncodingASCII, NULL); if (imageURL == NULL) { cout << "error getting URL for image file (image file may not exist): " << theFilename << endl; return 0; } // get a FSRef from the URL if (!CFURLGetFSRef(imageURL, &imagefsRef)) { cout << "error getting FSRef for image file:: " << theFilename << endl; CFRelease(imageURL); return 0; } // get the FSSpec from the FSRef if (FSGetCatalogInfo (&imagefsRef, kFSCatInfoNone, NULL, NULL, &fsspecImage, NULL)) { cout << "error getting FSSpec for image file: " << theFilename << endl; CFRelease(imageURL); return 0; } // release the URL (not needed any more) CFRelease(imageURL); #endif #ifdef _WIN32 // get an FSSpec from the pathname if (NativePathNameToFSSpec ((char *)fileLocation.c_str(), &fsspecImage, kErrorIfFileNotFound)) { cout << "error getting FSSpec for image file: " << fileLocation << endl; return 0; } #endif // save the onscreen graphics port GDHandle origDevice; CGrafPtr origPort; GetGWorld (&origPort, &origDevice); // get an importer for the file err = GetGraphicsImporterForFileWithFlags (&fsspecImage, &giComp, kDontUseValidateToFindGraphicsImporter); if (err != noErr) return 0; // get the image bounds err = GraphicsImportGetNaturalBounds (giComp, &rectImage); if (err != noErr) return 0; // create a handle for the image description and lock it hImageDesc = (ImageDescriptionHandle) NewHandle (sizeof (ImageDescriptionHandle)); HLock ((Handle) hImageDesc); // retrieve the image description err = GraphicsImportGetImageDescription (giComp, &hImageDesc); if (err != noErr) return 0; // find width and height imageWidth = (int) (rectImage.right - rectImage.left); imageHeight = (int) (rectImage.bottom - rectImage.top); // now calculate the aspect ratio (width/height), used to restore image correctly imageAspect = ((float) imageWidth) / ((float) imageHeight); // get the image's pixel depth imageDepth = (**hImageDesc).depth; // find nearest acceptable texture size (width and height) for the image textureWidth = GetTextureDimFromImageDim (imageWidth); textureHeight = GetTextureDimFromImageDim (imageHeight); // pass the optimised (and scaled) size back out to the caller *width = textureWidth; *height = textureHeight; // set texture rectangle for creation of GWorld #ifdef __APPLE__ SetRect (&rectImage, 0, 0, (int) textureWidth, (int) textureHeight); #endif #ifdef _WIN32 MacSetRect (&rectImage, 0, 0, (int) textureWidth, (int) textureHeight); #endif // set stride in bytes width of image * 4 bytes per pixel rowStride = textureWidth * 4; // build new buffer exact size of image (stride * height) pImageBuffer = (unsigned char *) NewPtr (rowStride * textureHeight); // check we got the buffer we wanted if (pImageBuffer == NULL) { // failed - release the component and return an error CloseComponent(giComp); return 0; } // create a new gworld using our unpadded buffer, setting the pixel type correctly for the expected image depth #ifdef __BIG_ENDIAN__ QTNewGWorldFromPtr (&(pGWorld), k32ARGBPixelFormat, &rectImage, NULL, NULL, 0, pImageBuffer, rowStride); #else QTNewGWorldFromPtr (&(pGWorld), k32RGBAPixelFormat, &rectImage, NULL, NULL, 0, pImageBuffer, rowStride); #endif // could we allocate the GWorld? if (pGWorld == NULL) { // failed - release the buffer, component and return an error DisposePtr ((Ptr) pImageBuffer); CloseComponent(giComp); return 0; } // build a transformation matrix to scale the image to the texture size // this also flips the image horizontally, so that texture coordinates map correctly float horizontalScale = (float) textureWidth / (float) imageWidth; float verticalScale = (float) textureHeight / (float) imageHeight; SetIdentityMatrix (&matrix); ScaleMatrix (&matrix, X2Fix(horizontalScale), X2Fix(-verticalScale), 0, 0); TranslateMatrix (&matrix, 0, X2Fix((float)textureHeight)); // set the matrix as the importer matrix err = GraphicsImportSetMatrix(giComp, &matrix); // set the destination of the importer component if (err == noErr) err = GraphicsImportSetGWorld (giComp, pGWorld, NULL); // ensure lossless decompression (if the CODEC supports this) if (err == noErr) err = GraphicsImportSetQuality(giComp, codecLosslessQuality); if (err != noErr) { // failed - release the GWorld, buffer, component and return an error DisposeGWorld (pGWorld); DisposePtr ((Ptr) pImageBuffer); CloseComponent(giComp); return 0; } // get the address of the GWorld's pixmap hPixMap = GetGWorldPixMap (pGWorld); // if everything looks good draw the image to the locked pixmap if ((hPixMap) && (LockPixels (hPixMap))) GraphicsImportDraw (giComp); else { // the pixmap doesn't exist, or we couldn't lock it // release the GWorld, buffer, component and return an error DisposeGWorld (pGWorld); DisposePtr ((Ptr) pImageBuffer); CloseComponent(giComp); return 0; } // for images without an alpha channel, initialise the alpha bytes since QuickTime won't if (imageDepth < 32) { #ifdef __BIG_ENDIAN__ for( unsigned char *p = pImageBuffer; p < pImageBuffer + (rowStride * textureHeight); p+=4) *p = 0xFF; #else for( unsigned char *p = pImageBuffer+3; p < pImageBuffer + (rowStride * textureHeight) +3; p+=4) *p = 0xFF; #endif } // unlock the pixmap UnlockPixels (hPixMap); // dump the component CloseComponent(giComp); // set image width in groups (pixels), accounts for border this ensures proper image alignment row to row glPixelStorei (GL_UNPACK_ROW_LENGTH, textureWidth); // generate a "name" for the texture glGenTextures (1, &textureName); // create the texture in OpenGL glBindTexture(GL_TEXTURE_2D, textureName); // tell OpenGL about the texture and have GLU build mipmaps #ifdef __BIG_ENDIAN__ glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pImageBuffer); #else glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pImageBuffer); #endif if (generateMipmaps) #ifdef __BIG_ENDIAN__ if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, textureWidth, textureHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pImageBuffer) != 0) #else if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, textureWidth, textureHeight, GL_RGBA, GL_UNSIGNED_BYTE, pImageBuffer) != 0) #endif // good time to check for errors? openGLCheckError(); // finished with the GWorld but not the image buffer (it's used by OpenGL as the "live" image buffer) DisposeGWorld (pGWorld); // restore the current graphics port we saved earlier SetGWorld(origPort, origDevice); // all done - return the texture name return textureName; #endif // QuickTimeInstalled }
static void QTDR_DrawFrame (short theTrackWidth, short theTrackHeight, long theNumSample, GWorldPtr theGWorld) { Handle myHandle = NULL; char myData[kPICTFileHeaderSize]; static PicHandle myPicture = NULL; static GWorldPtr myGWorld = NULL; static GraphicsImportComponent myImporter = NULL; Rect myRect; RGBColor myColor; ComponentResult myErr = noErr; MacSetRect(&myRect, 0, 0, theTrackWidth, theTrackHeight); if (myPicture == NULL) { myErr = NewGWorld(&myGWorld, kPixelDepth, &myRect, NULL, NULL, (GWorldFlags)0); if (myErr != noErr) goto bail; // read a picture from our resource file myPicture = GetPicture(kPictureID); if (myPicture == NULL) goto bail; // use Munger to prepend a 512-byte header onto the picture data; this converts the PICT // resource data into in-memory PICT file data (see Ice Floe 14 for an explanation of this) myHandle = (Handle)myPicture; Munger(myHandle, 0, NULL, 0, myData, kPICTFileHeaderSize); // get a graphics importer for the picture myErr = OpenADefaultComponent(GraphicsImporterComponentType, kQTFileTypePicture, &myImporter); if (myErr != noErr) goto bail; // configure the graphics importer myErr = GraphicsImportSetGWorld(myImporter, myGWorld, NULL); if (myErr != noErr) goto bail; myErr = GraphicsImportSetDataHandle(myImporter, myHandle); if (myErr != noErr) goto bail; myErr = GraphicsImportSetBoundsRect(myImporter, &myRect); if (myErr != noErr) goto bail; // draw the picture into the source GWorld myErr = GraphicsImportDraw(myImporter); if (myErr != noErr) goto bail; } // set the blend amount (0 = fully transparent; 0xffff = fully opaque) myColor.red = (theNumSample - 1) * (0xffff / kNumVideoFrames - 1); myColor.green = (theNumSample - 1) * (0xffff / kNumVideoFrames - 1); myColor.blue = (theNumSample - 1) * (0xffff / kNumVideoFrames - 1); OpColor(&myColor); // blend the picture (in the source GWorld) into the empty rectangle (in the destination GWorld) CopyBits((BitMapPtr)*GetGWorldPixMap(myGWorld), (BitMapPtr)*GetGWorldPixMap(theGWorld), &myRect, &myRect, blend, NULL); if (theNumSample == kNumVideoFrames) goto bail; return; bail: if (myHandle != NULL) DisposeHandle(myHandle); if (myPicture != NULL) ReleaseResource((Handle)myPicture); if (myImporter != NULL) CloseComponent(myImporter); }
static OSErr QTDR_AddVideoSamplesToMedia (Media theMedia, short theTrackWidth, short theTrackHeight) { GWorldPtr myGWorld = NULL; PixMapHandle myPixMap = NULL; CodecType myCodecType = kJPEGCodecType; long myNumSample; long myMaxComprSize = 0L; Handle myComprDataHdl = NULL; Ptr myComprDataPtr = NULL; ImageDescriptionHandle myImageDesc = NULL; CGrafPtr mySavedPort = NULL; GDHandle mySavedDevice = NULL; Rect myRect; OSErr myErr = noErr; MacSetRect(&myRect, 0, 0, theTrackWidth, theTrackHeight); myErr = NewGWorld(&myGWorld, kPixelDepth, &myRect, NULL, NULL, (GWorldFlags)0); if (myErr != noErr) goto bail; myPixMap = GetGWorldPixMap(myGWorld); if (myPixMap == NULL) goto bail; LockPixels(myPixMap); myErr = GetMaxCompressionSize( myPixMap, &myRect, 0, // let ICM choose depth codecNormalQuality, myCodecType, (CompressorComponent)anyCodec, &myMaxComprSize); if (myErr != noErr) goto bail; myComprDataHdl = NewHandle(myMaxComprSize); if (myComprDataHdl == NULL) goto bail; HLockHi(myComprDataHdl); #if TARGET_CPU_68K myComprDataPtr = StripAddress(*myComprDataHdl); #else myComprDataPtr = *myComprDataHdl; #endif myImageDesc = (ImageDescriptionHandle)NewHandle(4); if (myImageDesc == NULL) goto bail; GetGWorld(&mySavedPort, &mySavedDevice); SetGWorld(myGWorld, NULL); for (myNumSample = 1; myNumSample <= kNumVideoFrames; myNumSample++) { EraseRect(&myRect); QTDR_DrawFrame(theTrackWidth, theTrackHeight, myNumSample, myGWorld); myErr = CompressImage( myPixMap, &myRect, codecNormalQuality, myCodecType, myImageDesc, myComprDataPtr); if (myErr != noErr) goto bail; myErr = AddMediaSample( theMedia, myComprDataHdl, 0, // no offset in data (**myImageDesc).dataSize, kVideoFrameDuration, // frame duration (SampleDescriptionHandle)myImageDesc, 1, // one sample 0, // self-contained samples NULL); if (myErr != noErr) goto bail; } bail: SetGWorld(mySavedPort, mySavedDevice); if (myImageDesc != NULL) DisposeHandle((Handle)myImageDesc); if (myComprDataHdl != NULL) DisposeHandle(myComprDataHdl); if (myGWorld != NULL) DisposeGWorld(myGWorld); return(myErr); }
// ###################################################################### QuickTimeGrabber::Impl::Impl(const Dims& dims) : itsSeqGrab(0, &CloseComponent), itsSGChanVideo(&itsSeqGrab.it), itsDrawSeq(0), itsTimeScale(0), itsTimeBase(0), itsQueuedFrameCount(0), itsSkipFrameCount(0), itsSkipFrameCountTotal(0), itsPrevTime(0), itsFrameCount(0), itsGWorld(0), itsGotFrame(false), itsCurrentImage(), itsErrorMsg(), itsStreamStarted(false) { OSErr err; EnterMovies(); // open the default sequence grabber itsSeqGrab.it = OpenDefaultComponent(SeqGrabComponentType, 0); if (itsSeqGrab.it == NULL) LFATAL("OpenDefaultComponent() failed"); // initialize the default sequence grabber component if (noErr != (err = SGInitialize(itsSeqGrab.it))) LFATAL("SGInitialize() failed (err=%ld)", (long) err); Rect scaleRect; MacSetRect(&scaleRect, 0, 0, dims.w(), dims.h()); ASSERT(itsGWorld == 0); QTNewGWorld(&itsGWorld, k32ARGBPixelFormat, &scaleRect, NULL, NULL, kNativeEndianPixMap); // set its graphics world if (noErr != (err = SGSetGWorld(itsSeqGrab.it, itsGWorld, NULL))) LFATAL("SGSetGWorld() failed (err=%ld)", (long) err); // specify the destination data reference for a record operation // tell it we're not making a movie if the flag seqGrabDontMakeMovie // is used, the sequence grabber still calls your data function, but // does not write any data to the movie file writeType will always // be set to seqGrabWriteAppend if (noErr != (err = SGSetDataRef(itsSeqGrab.it, 0, 0, seqGrabDontMakeMovie | seqGrabDataProcIsInterruptSafe))) LFATAL("SGSetDataRef() failed (err=%ld)", (long) err); Impl::SGChannelHolder sgchanSound(&itsSeqGrab.it); if (noErr != (err = SGNewChannel(itsSeqGrab.it, VideoMediaType, &itsSGChanVideo.it))) LFATAL("SGNewChannel(video) failed (err=%ld)", (long) err); if (noErr != (err = SGNewChannel(itsSeqGrab.it, SoundMediaType, &sgchanSound.it))) { // don't care if we couldn't get a sound channel sgchanSound.it = NULL; LERROR("SGNewChannel(audio) failed (err=%ld)", (long) err); } // get the active rectangle Rect srcBounds; if (noErr != (err = SGGetSrcVideoBounds(itsSGChanVideo.it, &srcBounds))) LFATAL("SGGetSrcVideoBounds() failed (err=%ld)", (long) err); // we always want all the source setVideoChannelBounds(itsSGChanVideo.it, &srcBounds, &srcBounds); // set usage for new video channel to avoid playthrough // note we don't set seqGrabPlayDuringRecord if (noErr != (err = SGSetChannelUsage(itsSGChanVideo.it, seqGrabRecord | seqGrabLowLatencyCapture | seqGrabAlwaysUseTimeBase))) LFATAL("SGSetChannelUsage(video) failed (err=%ld)", (long) err); if (noErr != (err = SGSetChannelUsage(sgchanSound.it, seqGrabRecord | //seqGrabPlayDuringRecord | seqGrabLowLatencyCapture | seqGrabAlwaysUseTimeBase))) LERROR("SGSetChannelUsage(audio) failed (err=%ld)", (long) err); // specify a sequence grabber data function if (noErr != (err = SGSetDataProc(itsSeqGrab.it, NewSGDataUPP(Impl::grabDataProc), (long)(this)))) LFATAL("SGSetDataProc() failed (err=%ld)", (long) err); SGSetChannelRefCon(itsSGChanVideo.it, (long)(this)); // set up the video bottlenecks so we can get our queued frame count VideoBottles vb = { 0 }; if (noErr != (err = SGGetVideoBottlenecks(itsSGChanVideo.it, &vb))) LFATAL("SGGetVideoBottlenecks() failed (err=%ld)", (long) err); vb.procCount = 9; // there are 9 bottleneck procs; this must be filled in vb.grabCompressCompleteProc = NewSGGrabCompressCompleteBottleUPP (Impl::grabCompressCompleteBottle); if (noErr != (err = SGSetVideoBottlenecks(itsSGChanVideo.it, &vb))) LFATAL("SGSetVideoBottlenecks() failed (err=%ld)", (long) err); SGSetFrameRate(itsSGChanVideo.it, FixRatio(30, 1)); }