void EraseRectAndAlpha(GWorldPtr gWorld, Rect *pRect) { PixMapHandle pixMap = GetGWorldPixMap(gWorld); long rows; Ptr rowBaseAddr; LockPixels(pixMap); rows = pRect->bottom - pRect->top; rowBaseAddr = GetPixBaseAddr(pixMap) + (GetPixRowBytes(pixMap) & 0x3fff) * pRect->top + pRect->left * GetPixDepth(pixMap) / 8; do { long cols; UInt32 *baseAddr; cols = pRect->right - pRect->left; baseAddr = (UInt32*)rowBaseAddr; rowBaseAddr += (**pixMap).rowBytes & 0x3fff; do { *baseAddr++ = 0; } while (--cols); } while (--rows); UnlockPixels(pixMap); } // EraseRectAndAlpha
void drawGraphic( CGContextRef context, float x, float y ) { static GWorldPtr imageGW = NULL; static CGImageRef imageRef = NULL; static CGDataProviderRef dataProviderRef = NULL; Rect bounds; static size_t width; static size_t height; size_t bitsPerComponent; size_t bitsPerPixel; size_t bytesPerRow; PixMapHandle pmh; // Load the image if we haven't already if ( NULL == imageGW ) { // Load and create the GWorld imageGW = OpenImage(); if ( imageGW != NULL ) { GetPortBounds( imageGW, &bounds ); width = bounds.right - bounds.left; height = bounds.bottom - bounds.top; pmh = GetPortPixMap( imageGW ); bitsPerComponent = (**pmh).cmpSize; bitsPerPixel = (**pmh).pixelSize; bytesPerRow = GetPixRowBytes( pmh ); LockPixels( pmh ); dataProviderRef = CGDataProviderCreateWithData( NULL, GetPixBaseAddr( pmh ), height * bytesPerRow, releaseData ); // Create the imageRef for that GWorld imageRef = CGImageCreate( width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst/*kCGImageAlphaNone*/, dataProviderRef, NULL, 0, kCGRenderingIntentDefault ); } } // Draw the image at 0,0 CGContextDrawImage( context, CGRectMake( x - 20, y, 40, 40 * height / width ), imageRef ); }
static pascal OSErr DrawCompleteProc (Movie theMovie, long refCon) { long int h; long int w; int y, x; GWorldPtr offWorld = (GWorldPtr) refCon; Rect bounds; Ptr baseAddr; long rowBytes; uint8_t* imbuf; mwSize dims[3]; GetPixBounds(GetGWorldPixMap(offWorld), &bounds); baseAddr = GetPixBaseAddr(GetGWorldPixMap(offWorld)); rowBytes = GetPixRowBytes(GetGWorldPixMap(offWorld)); h = rint(bounds.bottom - bounds.top); w = rint(bounds.right - bounds.left); dims[0] = h; dims[1] = w; dims[2] = 3; framedata = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL); imbuf = (uint8_t*) mxGetData(framedata); // Retrieve the pixel data, unpack the RGB values and copy for (y = 0; y < h; ++y) { long *p; p = (long *) (baseAddr + rowBytes * (long) y); for (x = 0; x < w; ++x) { UInt32 color = *(long *)((long) p + 4 * (long) x);; long B = (color & 0xFF000000) >> 24; long G = (color & 0x00FF0000) >> 16; long R = (color & 0x0000FF00) >> 8; imbuf[y + x * h + 0 * (h * w)] = R; imbuf[y + x * h + 1 * (h * w)] = G; imbuf[y + x * h + 2 * (h * w)] = B; } } return noErr; }
static OSErr data_proc (SGChannel c, Ptr p, long len, long *offset, long chRefCon, TimeValue time, short writeType, long refCon) { GstOSXVideoSrc *self; gint fps_n, fps_d; GstClockTime duration, timestamp, latency; CodecFlags flags; ComponentResult err; PixMapHandle hPixMap; Rect portRect; int pix_rowBytes; void *pix_ptr; int pix_height; int pix_size; self = GST_OSX_VIDEO_SRC (refCon); if (self->buffer != NULL) { gst_buffer_unref (self->buffer); self->buffer = NULL; } err = DecompressSequenceFrameS (self->dec_seq, p, len, 0, &flags, NULL); if (err != noErr) { GST_ERROR_OBJECT (self, "DecompressSequenceFrameS returned %d", (int) err); return err; } hPixMap = GetGWorldPixMap (self->world); LockPixels (hPixMap); GetPortBounds (self->world, &portRect); pix_rowBytes = (int) GetPixRowBytes (hPixMap); pix_ptr = GetPixBaseAddr (hPixMap); pix_height = (portRect.bottom - portRect.top); pix_size = pix_rowBytes * pix_height; GST_DEBUG_OBJECT (self, "num=%5d, height=%d, rowBytes=%d, size=%d", self->seq_num, pix_height, pix_rowBytes, pix_size); fps_n = FRAMERATE; fps_d = 1; duration = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n); latency = duration; timestamp = gst_clock_get_time (GST_ELEMENT_CAST (self)->clock); timestamp -= gst_element_get_base_time (GST_ELEMENT_CAST (self)); if (timestamp > latency) timestamp -= latency; else timestamp = 0; self->buffer = gst_buffer_new_and_alloc (pix_size); GST_BUFFER_OFFSET (self->buffer) = self->seq_num; GST_BUFFER_TIMESTAMP (self->buffer) = timestamp; memcpy (GST_BUFFER_DATA (self->buffer), pix_ptr, pix_size); self->seq_num++; UnlockPixels (hPixMap); return noErr; }
OSStatus DrawPage(PrintingLogicPtr printJob,CGContextRef printingContext) { OSStatus status = noErr; Rect dstRect = { 0, 0, 0, 0 }; Rect srcRect = { 0, 0, 0, 0 }; static CGColorSpaceRef colorspace = NULL; if (colorspace == NULL) { // Get the Systems Profile for the main display CMProfileRef sysprof = NULL; if (CMGetSystemProfile(&sysprof) == noErr) { // Create a colorspace with the systems profile colorspace = CGColorSpaceCreateWithPlatformColorSpace(sysprof); CMCloseProfile(sysprof); } else colorspace = CGColorSpaceCreateDeviceRGB(); } dstRect.top = printJob->offsetHeight; dstRect.left = printJob->offsetWidth; dstRect.right = printJob->width*printJob->scaleW + printJob->offsetWidth; dstRect.bottom = printJob->height*printJob->scaleH + printJob->offsetHeight; if (printJob->formBitMap != nil) { srcRect.right = printJob->width; srcRect.bottom = printJob->height; HLock((Handle)stPixMap); (*stPixMap)->baseAddr = (void *) printJob->formBitMap; (*stPixMap)->rowBytes = (((((printJob->width * printJob->depth) + 31) / 32) * 4) & 0x1FFF) | 0x8000; (*stPixMap)->bounds = srcRect; (*stPixMap)->pixelSize = printJob->depth; if (printJob->depth<=8) { (*stPixMap)->cmpSize = printJob->depth; (*stPixMap)->cmpCount = 1; } else if (printJob->depth==16) { (*stPixMap)->cmpSize = 5; (*stPixMap)->cmpCount = 3; } else if (printJob->depth==32) { (*stPixMap)->cmpSize = 8; (*stPixMap)->cmpCount = 3; } { PixMapHandle thePix; int pitch; CGDataProviderRef provider; CGImageRef image; CGRect clip; Ptr baseAddr; if (printJob->depth == 32) { pitch = (((((printJob->width * printJob->depth) + 31) / 32) * 4) & 0x1FFF); baseAddr = (void *) printJob->formBitMap; } else { if (printJob->aGWorld == NULL) NewGWorld(&printJob->aGWorld, 32, &srcRect, stColorTable, NULL, keepLocal+useTempMem+pixelsLocked); thePix = GetGWorldPixMap (printJob->aGWorld); CopyBits((BitMap *) *stPixMap, (BitMap *) *thePix, &srcRect, &srcRect, srcCopy, NULL); pitch = GetPixRowBytes(thePix); baseAddr = GetPixBaseAddr(thePix); } provider = CGDataProviderCreateDirectAccess((void*)baseAddr, pitch * (srcRect.bottom-srcRect.top), &gProviderCallbacks); image = CGImageCreate( srcRect.right-srcRect.left, srcRect.bottom-srcRect.top, 8 /* bitsPerComponent */, 32 /* bitsPerPixel */, pitch, colorspace, kCGImageAlphaNoneSkipFirst | (printJob->depth==32 ? kCGBitmapByteOrder32Host : 0), provider, NULL, 0, kCGRenderingIntentDefault); clip = CGRectMake(dstRect.left+(printJob->pageRect.left-printJob->paperRect.left), (printJob->paperRect.bottom-printJob->pageRect.bottom) + (printJob->pageRect.bottom - printJob->pageRect.top) - (dstRect.bottom-dstRect.top) - dstRect.top, dstRect.right-dstRect.left, dstRect.bottom-dstRect.top); CGContextDrawImage(printingContext, clip, image); CGContextFlush(printingContext); CGImageRelease(image); CGDataProviderRelease(provider); } HUnlock((Handle)stPixMap); } else { } #if TARGET_API_MAC_CARBON if (printJob->allowPostscript && printJob->postscriptLength > 0) { CGDataProviderRef provider,providerFakeImage; CGImageRef image,imageFake; CGRect clip; static long dirt=0xBBBBBBBB; //PMPrinter currentPrinter = NULL; //CFArrayRef mimeTypes; //status = PMSessionGetCurrentPrinter(printJob->printSession,¤tPrinter); //status = PMPrinterGetMimeTypes(currentPrinter,printJob->printSettings,&mimeTypes); provider = CGDataProviderCreateDirectAccess((void*)printJob->postscript,printJob->postscriptLength, &gProviderCallbacks); providerFakeImage = CGDataProviderCreateDirectAccess((void*)&dirt,4, &gProviderCallbacks); //OK make fake image using tiny bit of data imageFake = CGImageCreate(1, 1, 8 /* bitsPerComponent */, 32 /* bitsPerPixel */, 4, colorspace, kCGImageAlphaNoneSkipFirst , providerFakeImage, NULL, 0, kCGRenderingIntentDefault); image = PMCGImageCreateWithEPSDataProvider(provider,imageFake); dstRect.top = 0; dstRect.left = 0; dstRect.bottom = CGImageGetHeight(image); dstRect.right = CGImageGetWidth(image); clip = CGRectMake(dstRect.left+(printJob->pageRect.left-printJob->paperRect.left), (printJob->paperRect.bottom-printJob->pageRect.bottom) + (printJob->pageRect.bottom - printJob->pageRect.top) - (dstRect.bottom-dstRect.top) - dstRect.top, dstRect.right-dstRect.left, dstRect.bottom-dstRect.top); //PMPrinterPrintWithProvider CGContextDrawImage(printingContext, clip, image); CGContextFlush(printingContext); CGImageRelease(image); CGImageRelease(imageFake); CGDataProviderRelease(provider); CGDataProviderRelease(providerFakeImage); } #else return PrError(); #endif return status; } // DrawPage
bool macvdCamera::initCamera(int width, int height, bool colour) { if (cameraID < 0) return false; this->width =width; this->height=height; this->colour=colour; this->bytes =(colour?3:1); OSErr err; if(err = vdgRequestSettings(pVdg)) { printf("camera setup cancelled\n"); //printf("vdgRequestSettings err=%d\n", err); return false; } long nameLength = 256; if (err = vdgGetDeviceNameAndFlags(pVdg, cameraName, &nameLength, NULL)) { sprintf(cameraName,"unknown camera"); } long milliSecPerFrame; Fixed framerate; long bytesPerSecond; if (err = vdgGetDataRate(pVdg, &milliSecPerFrame, &framerate, &bytesPerSecond)) { fps = 30; } else fps = (int)(framerate/65536); //fps = vdgGetFrameRate(pVdg); //printf("%d\n",fps); if(err = vdgPreflightGrabbing(pVdg)) { //printf("vdgPreflightGrabbing err=%d\n", err); return false; } vdImageDesc = (ImageDescriptionHandle)NewHandle(0); if (err = vdgGetImageDescription( pVdg, vdImageDesc)) { //printf("vdgGetImageDescription err=%d\n", err); return false; } this->width = (*vdImageDesc)->width; this->height = (*vdImageDesc)->height; //printf("%dx%d\n",this->width,this->height); dstPortBounds.left = 0; dstPortBounds.right = this->width; dstPortBounds.top = 0; dstPortBounds.bottom = this->height; if (err = createOffscreenGWorld( &dstPort, //k8IndexedGrayPixelFormat, //kYVYU422PixelFormat, //k24RGBPixelFormat, //kYUV420CodecType, k422YpCbCr8CodecType, &dstPortBounds)) { printf("createOffscreenGWorld err=%d\n", err); return false; } // Get buffer from GWorld pDstData = GetPixBaseAddr(GetGWorldPixMap(dstPort)); dstDataSize = GetPixRowBytes(GetGWorldPixMap(dstPort)) * (dstPortBounds.bottom - dstPortBounds.top); dstDisplayBounds = dstPortBounds; // Set the decompression destination to the offscreen GWorld if (err = vdgSetDestination( pVdg, dstPort )) { //printf("vdgSetDestination err=%d\n", err); return false; } buffer = new unsigned char[width*height*bytes]; return true; }
PicHandle GetScreenAsPicHandle(int width, int height, int destWidth, int destHeight) { PicHandle myPicture; Rect drawSize, scaleSize; GWorldPtr drawWorld, scaleWorld; Byte *graphicsIn, *graphicsOut; int row, graphicsRowBytes; SetRect(&drawSize, 0, 0, width, height); SetRect(&scaleSize, 0, 0, destWidth, destHeight); InitGWorld(&drawWorld, &drawSize, 16); InitGWorld(&scaleWorld, &scaleSize, 16); graphicsIn = (Byte *) GFX.Screen; graphicsOut = (Byte *) GetPixBaseAddr(GetGWorldPixMap(drawWorld)); graphicsRowBytes = GetPixRowBytes(GetGWorldPixMap(drawWorld)); for (row = 0; row < height; row++) { memcpy(graphicsOut, graphicsIn, width * 2); if (directDisplay) { if (drawingMethod != kDrawingOpenGL) graphicsIn += 512 * 2; else graphicsIn += width * 2; } else { if (lastDrawingMethod != kDrawingOpenGL) graphicsIn += 512 * 2; else graphicsIn += width * 2; } graphicsOut += graphicsRowBytes; } if ((scaleSize.right * scaleSize.bottom) < (drawSize.right * drawSize.bottom)) { PrepareForGDrawing(drawWorld); CopyBits(GetPortBitMapForCopyBits(drawWorld), GetPortBitMapForCopyBits(scaleWorld), &drawSize, &scaleSize, srcCopy | ditherCopy, nil); FinishGDrawing(drawWorld); PrepareForGDrawing(scaleWorld); myPicture = OpenPicture(&scaleSize); CopyBits(GetPortBitMapForCopyBits(scaleWorld), GetPortBitMapForCopyBits(scaleWorld), &scaleSize, &scaleSize, srcCopy, nil); ClosePicture(); FinishGDrawing(scaleWorld); } else { PrepareForGDrawing(scaleWorld); myPicture = OpenPicture(&scaleSize); CopyBits(GetPortBitMapForCopyBits(drawWorld), GetPortBitMapForCopyBits(scaleWorld), &drawSize, &scaleSize, srcCopy, nil); ClosePicture(); FinishGDrawing(scaleWorld); } DisposeGWorld(drawWorld); DisposeGWorld(scaleWorld); return myPicture; }
void QGLContext::updatePaintDevice() { Q_D(QGLContext); d->update = false; #if 0 //(MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) { if(d->paintDevice->devType() == QInternal::Widget) { QWidget *w = (QWidget *)d->paintDevice; aglSetHIViewRef((AGLContext)d->cx, (HIViewRef)w->winId()); } else if(d->paintDevice->devType() == QInternal::Pixmap) { QPixmap *pm = (QPixmap *)d->paintDevice; aglSetOffScreen((AGLContext)d->cx, pm->width(), pm->height(), qt_mac_pixmap_get_bytes_per_line(pm), qt_mac_pixmap_get_base(pm)); } else { qWarning("QGLContext::updatePaintDevice(): Not sure how to render OpenGL on this device!"); } } else #endif { #ifndef Q_OS_MAC64 if(d->paintDevice->devType() == QInternal::Widget) { //get control information QWidget *w = (QWidget *)d->paintDevice; HIViewRef hiview = (HIViewRef)w->winId(); WindowPtr window = qt_mac_window_for(hiview); #ifdef DEBUG_OPENGL_REGION_UPDATE static int serial_no_gl = 0; qDebug("[%d] %p setting on %s::%s %p/%p [%s]", ++serial_no_gl, w, w->metaObject()->className(), w->objectName().toLatin1().constData(), hiview, window, w->handle() ? "Inside" : "Outside"); #endif //update drawable if(0 && w->isWindow() && w->isFullScreen()) { aglSetDrawable((AGLContext)d->cx, 0); aglSetFullScreen((AGLContext)d->cx, w->width(), w->height(), 0, QApplication::desktop()->screenNumber(w)); w->hide(); } else { AGLDrawable old_draw = aglGetDrawable((AGLContext)d->cx), new_draw = GetWindowPort(window); if(old_draw != new_draw) aglSetDrawable((AGLContext)d->cx, new_draw); } if(!w->isWindow()) { QRegion clp = qt_mac_get_widget_rgn(w); //get drawable area #ifdef DEBUG_OPENGL_REGION_UPDATE if(clp.isEmpty()) { qDebug(" Empty area!"); } else { QVector<QRect> rs = clp.rects(); for(int i = 0; i < rs.count(); i++) qDebug(" %d %d %d %d", rs[i].x(), rs[i].y(), rs[i].width(), rs[i].height()); } #endif //update the clip if(!aglIsEnabled((AGLContext)d->cx, AGL_BUFFER_RECT)) aglEnable((AGLContext)d->cx, AGL_BUFFER_RECT); if(clp.isEmpty()) { GLint offs[4] = { 0, 0, 0, 0 }; aglSetInteger((AGLContext)d->cx, AGL_BUFFER_RECT, offs); if(aglIsEnabled((AGLContext)d->cx, AGL_CLIP_REGION)) aglDisable((AGLContext)d->cx, AGL_CLIP_REGION); } else { HIPoint origin = { 0., 0. }; HIViewConvertPoint(&origin, HIViewRef(w->winId()), 0); const GLint offs[4] = { qRound(origin.x), w->window()->frameGeometry().height() - (qRound(origin.y) + w->height()), w->width(), w->height() }; aglSetInteger((AGLContext)d->cx, AGL_BUFFER_RECT, offs); aglSetInteger((AGLContext)d->cx, AGL_CLIP_REGION, (const GLint *)clp.handle(true)); if(!aglIsEnabled((AGLContext)d->cx, AGL_CLIP_REGION)) aglEnable((AGLContext)d->cx, AGL_CLIP_REGION); } } } else if(d->paintDevice->devType() == QInternal::Pixmap) { QPixmap *pm = (QPixmap *)d->paintDevice; PixMapHandle mac_pm = GetGWorldPixMap((GWorldPtr)pm->macQDHandle()); aglSetOffScreen((AGLContext)d->cx, pm->width(), pm->height(), GetPixRowBytes(mac_pm), GetPixBaseAddr(mac_pm)); } else { qWarning("QGLContext::updatePaintDevice(): Not sure how to render OpenGL on this device!"); } #endif // Q_OS_MAC64 } aglUpdateContext((AGLContext)d->cx); }
static void draw_string(int x, int y, int width, int height, Str255 chars) { FontInfo info; GWorldPtr fromworld, toworld; CGrafPtr onport, saveport; GDHandle savedevice; int txFont, txFace, txSize; Rect fromrect, torect, onrect; PixMapHandle frompix, topix, onpix; int *from, *to, from_width, to_width; register int descent, w = 0, h = 0; register int i, j, ii = 0, jj = 0; if (p->path != 0) { GetFontInfo(&info); height += 8; descent = info.descent; switch (p->path) { case 1: x -= height-descent; y -= width; w = height; h = width; break; case 2: x -= width; y -= descent; w = width; h = height; break; case 3: x -= descent; w = height; h = width; break; } GetGWorld(&saveport, &savedevice); txFont = GetPortTextFont(saveport); txFace = GetPortTextFace(saveport); txSize = GetPortTextSize(saveport); onport = p->port; onpix = GetPortPixMap(onport); fromrect.left = 0; fromrect.right = width; fromrect.top = 0; fromrect.bottom = height; NewGWorld(&fromworld, 32, &fromrect, NULL, NULL, 0); SetGWorld(fromworld, NULL); frompix = GetGWorldPixMap(fromworld); from = (int *) GetPixBaseAddr(frompix); from_width = GetPixRowBytes(frompix) / 4; LockPixels(frompix); EraseRect(&fromrect); MoveTo(0, height - descent); TextFont(txFont); TextFace(txFace); TextSize(txSize); DrawString(chars); torect.left = 0; torect.right = w; torect.top = 0; torect.bottom = h; NewGWorld(&toworld, 32, &torect, NULL, NULL, 0); topix = GetGWorldPixMap(toworld); to = (int *) GetPixBaseAddr(topix); to_width = GetPixRowBytes(topix) / 4; LockPixels(topix); for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { switch (p->path) { case 1: ii = j; jj = h - i - 1; break; case 2: ii = w - i - 1; jj = h - j - 1; break; case 3: ii = w - j - 1; jj = i; break; } to[jj * to_width + ii] = from[j * from_width + i]; } } UnlockPixels(topix); UnlockPixels(frompix); onrect.left = x; onrect.right = x + w; onrect.top = y; onrect.bottom = y + h; SetGWorld(saveport, savedevice); SetPort(onport); LockPixels(topix); CopyBits( (BitMap *) *topix, (BitMap *) *onpix, &torect, &onrect, srcOr, NULL); UnlockPixels(topix); DisposeGWorld(toworld); DisposeGWorld(fromworld); } else { MoveTo(x, y); DrawString(chars); } }