diff options
Diffstat (limited to 'macosx/tkMacOSXXStubs.c')
-rw-r--r-- | macosx/tkMacOSXXStubs.c | 90 |
1 files changed, 46 insertions, 44 deletions
diff --git a/macosx/tkMacOSXXStubs.c b/macosx/tkMacOSXXStubs.c index 29233d9..9594cd3 100644 --- a/macosx/tkMacOSXXStubs.c +++ b/macosx/tkMacOSXXStubs.c @@ -869,7 +869,7 @@ XCreateImage( * This function copies data from a pixmap or window into an XImage. * * Results: - * Returns a newly allocated XImage containing the data from the given + * Returns a newly allocated image containing the data from the given * rectangle of the given drawable. * * Side effects: @@ -890,57 +890,59 @@ XGetImage( int format) { MacDrawable *macDraw = (MacDrawable *) d; - NSBitmapImageRep *bitmap_rep; - XImage * imagePtr = NULL; - char * bitmap = NULL; - char * image_data=NULL; - int depth = 32; - int offset = 0; - int bitmap_pad = 0; - int bytes_per_row = 4*width; - int size; + XImage * imagePtr = NULL; + Pixmap pixmap = (Pixmap) NULL; + Tk_Window win = (Tk_Window) macDraw->winPtr; + GC gc; + char * data = NULL; + int depth = 32; + int offset = 0; + int bitmap_pad = 0; + int bytes_per_line = 0; + if (format == ZPixmap) { - if (width == 0 || height == 0) { - return NULL; + if (width > 0 && height > 0) { + /* + * Tk_GetPixmap fails for zero width or height. + */ + + pixmap = Tk_GetPixmap(display, d, width, height, depth); } + if (win) { + XGCValues values; - bitmap_rep = BitmapRepFromDrawableRect(macDraw, x, y,width, height); - NSSize image_size = NSMakeSize(width, height); - NSImage* ns_image = [[NSImage alloc]initWithSize:image_size]; - [ns_image addRepresentation:bitmap_rep]; - - /* Assume premultiplied nonplanar data with 4 bytes per pixel and alpha last.*/ - if ( [bitmap_rep bitmapFormat] == 0 && - [bitmap_rep isPlanar ] == 0 && - [bitmap_rep samplesPerPixel] == 4 ) { - bytes_per_row = [bitmap_rep bytesPerRow]; - size = bytes_per_row*height; - image_data = (char*)[bitmap_rep bitmapData]; - if ( image_data ) { - int row, n, m; - bitmap = ckalloc(size); - /* - Oddly enough, the bitmap has the top row at the beginning, - and the pixels are in BGRA format. - */ - for (row=0, n=0; row<height; row++, n+=bytes_per_row) { - for (m=n; m<n+bytes_per_row; m+=4) { - *(bitmap+m) = *(image_data+m+2); - *(bitmap+m+1) = *(image_data+m+1); - *(bitmap+m+2) = *(image_data+m); - *(bitmap+m+3) = *(image_data+m+3); - } - } + gc = Tk_GetGC(win, 0, &values); + } else { + gc = XCreateGC(display, pixmap, 0, NULL); + } + if (pixmap) { + CGContextRef context; + + XCopyArea(display, d, pixmap, gc, x, y, width, height, 0, 0); + context = ((MacDrawable *) pixmap)->context; + if (context) { + data = CGBitmapContextGetData(context); + bytes_per_line = CGBitmapContextGetBytesPerRow(context); } } - if (bitmap) { + if (data) { imagePtr = XCreateImage(display, NULL, depth, format, offset, - (char*)bitmap, width, height, bitmap_pad, bytes_per_row); - [ns_image removeRepresentation:bitmap_rep]; /*releases the rep*/ - [ns_image release]; + data, width, height, bitmap_pad, bytes_per_line); + + /* + * Track Pixmap underlying the XImage in the unused obdata field + * so that we can treat XImages coming from XGetImage specially. + */ + + imagePtr->obdata = (XPointer) pixmap; + } else if (pixmap) { + Tk_FreePixmap(display, pixmap); + } + if (!win) { + XFreeGC(display, gc); } } else { - TkMacOSXDbgMsg("Could not extract image from drawable."); + TkMacOSXDbgMsg("Invalid image format"); } return imagePtr; } |