Index: tkMacOSXDraw.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/macosx/tkMacOSXDraw.c,v
retrieving revision 1.2.2.5
diff -r1.2.2.5 tkMacOSXDraw.c
117a118
> #if 0
147c148
<     
---
>  
191c192
<         int xOffset, yOffset;
---
>         int xOffset = 0, yOffset = 0;
215a217,344
> #else
> 	CGRect	srcCGRect, destRect;
>   CGRect * srcPtr, * dstPtr;
>   MacDrawable *srcDraw = (MacDrawable *) src;
>   MacDrawable *dstDraw = (MacDrawable *) dst;
>   CGrafPtr srcPort, destPort;
>   CGrafPtr saveWorld;
>   GDHandle saveDevice;
>   Rect clpRect;
> 	CGImageRef srcImage = NULL;
> 	CGAffineTransform coordsTransform;
> 	CGContextRef destContext;
> 	float sx, sy, dx, dy;
> 
>   destPort = TkMacOSXGetDrawablePort(dst);
>   srcPort = TkMacOSXGetDrawablePort(src);
> 	
> 	display->request++;
>   GetGWorld(&saveWorld, &saveDevice);
>   SetGWorld(destPort, NULL);
> 	if (tmpRgn2 == NULL) {
>         tmpRgn2 = NewRgn();
>   }
> 	srcPtr = &srcCGRect;
> 
> 	srcCGRect = CGRectMake(	(float)(srcDraw->xOff-src_x),
> 				(float)(srcDraw->yOff-src_y),
> 				(float)width,
> 				(float)height );
>   if (tkPictureIsOpen ) {
> 		dstPtr = &srcCGRect;
>   } else {
>       dstPtr = &destRect;
> 		destRect = CGRectMake(	(float)(dstDraw->xOff + dest_x),
> 				(float)(dstDraw->yOff + dest_y),
> 				(float)width,
> 				(float)height );
>   }
>   TkMacOSXSetUpClippingRgn(dst);
>     /*
>      *  We will change the clip rgn in this routine, so we need to 
>      *  be able to restore it when we exit.
>      */
>  
>     GetClip(tmpRgn2);
>     if (tkPictureIsOpen) {
>         /*
>          * When rendering into a picture, after a call to "OpenCPicture"
>          * the clipping is seriously WRONG and also INCONSISTENT with the
>          * clipping for single plane bitmaps.
>          * To circumvent this problem,  we clip to the whole window 
>          * In this case, would have also clipped to the srcRect
>          * ClipRect(&srcRect);
>          */
>         GetPortBounds(destPort,&clpRect);
>         dstPtr = &srcCGRect;
>         ClipRect(&clpRect);
>     }
>     if (!gc->clip_mask ) { 
>     } else if (((TkpClipMask*)gc->clip_mask)->type == TKP_CLIP_REGION) {
>         RgnHandle clipRgn = (RgnHandle)
>             ((TkpClipMask*)gc->clip_mask)->value.region;
>  
>         int xOffset = 0, yOffset = 0;
>         if (tmpRgn == NULL) {
>             tmpRgn = NewRgn();
>         }
>         if (!tkPictureIsOpen) {
>             xOffset = dstDraw->xOff + gc->clip_x_origin;
>             yOffset = dstDraw->yOff + gc->clip_y_origin;
>             OffsetRgn(clipRgn, xOffset, yOffset);
>         }
>         GetClip(tmpRgn);
>         SectRgn(tmpRgn, clipRgn, tmpRgn);
>         SetClip(tmpRgn);
>         if (!tkPictureIsOpen) {
>              OffsetRgn(clipRgn, -xOffset, -yOffset);
>         }
>     }
> 
> 	PixMapHandle pm = GetPortPixMap( srcPort );
>         
> 	TkMacOSXSetUpCGContext(dstDraw, destPort, gc, &destContext);
> 			
> 	size_t srcW = (size_t)width;
> 	size_t srcH = (size_t)height;
> 	size_t bytesPerRow = (size_t)GetPixRowBytes( pm );
> 	size_t totalBytes = srcH * bytesPerRow;
> 	unsigned char* bitmap = GetPixBaseAddr( pm );
> 	
> 	CGDataProviderRef provider = CGDataProviderCreateWithData( NULL, bitmap, totalBytes, NULL );
> 	CGColorSpaceRef colorSpaceUserRGB = CGColorSpaceCreateDeviceRGB();
> 		
> 	srcImage = CGImageCreate( width, height,
> 						8,//bitsPerComponent
> 						32,//bitsPerPixel
> 						bytesPerRow,
> 						colorSpaceUserRGB,//colorspace
> 						kCGImageAlphaFirst, //alphaInfo
> 						provider, // dataProvider
> 						NULL, //decode array
> 						0, //shouldInterpolate?
> 						kCGRenderingIntentDefault); // CGColorRenderingIntent
> 	CGDataProviderRelease( provider );
> 	CGRect drawRect = CGRectMake ( (float)(dstDraw->xOff + dest_x),
> 								   (float)(dstDraw->yOff + dest_y),
> 								   width, height);
> 	if(!CGRectEqualToRect( srcCGRect, destRect ))
> 	{
> 		sx = CGRectGetWidth(destRect) / CGRectGetWidth(srcCGRect);
>       sy = CGRectGetHeight(destRect) / CGRectGetHeight(srcCGRect);
>       dx = CGRectGetMinX(destRect) - (CGRectGetMinX(srcCGRect) * sx);
> 		dy = CGRectGetMinY(destRect) - (CGRectGetMinY(srcCGRect) * sy);
> 
>       drawRect = CGRectMake (dx, dy, width*sx, height*sy);
>   }
> 	CGContextSaveGState( destContext );
> 	
> 	CGContextClipToRect( destContext, destRect );
> 	CGContextDrawImage( destContext, drawRect, srcImage );
> 	CGImageRelease( srcImage );
> 	CGContextRestoreGState( destContext );
> 	CGColorSpaceRelease( colorSpaceUserRGB );
> 	
> 	TkMacOSXReleaseCGContext(dstDraw, destPort, &destContext);
> 	SetClip(tmpRgn2);
> 	SetGWorld(saveWorld, saveDevice);
> #endif
250a380
> #if 0
348a479,588
> #else
> 	Rect srcRect, dstRect;
>     Rect * srcPtr, * dstPtr;
>     MacDrawable *srcDraw = (MacDrawable *) src;
>     MacDrawable *dstDraw = (MacDrawable *) dst;
>     CGrafPtr srcPort, destPort;
>     CGrafPtr saveWorld;
>     GDHandle saveDevice;
>     Rect clpRect;
> 	Rect boundsRect;
> 	CGImageRef srcImage = NULL;
> 	CGAffineTransform coordsTransform;
> 
>     destPort = TkMacOSXGetDrawablePort(dst);
>     srcPort = TkMacOSXGetDrawablePort(src);
> 	
> 	display->request++;
>     GetGWorld(&saveWorld, &saveDevice);
>     SetGWorld(destPort, NULL);
> 
> 	if (tmpRgn2 == NULL) {
>         tmpRgn2 = NewRgn();
>     }
>     TkMacOSXSetUpClippingRgn(dst);
>     /*
>      *  We will change the clip rgn in this routine, so we need to 
>      *  be able to restore it when we exit.
>      */
>  
>     GetClip(tmpRgn2);
>     if (tkPictureIsOpen) {
>         /*
>          * When rendering into a picture, after a call to "OpenCPicture"
>          * the clipping is seriously WRONG and also INCONSISTENT with the
>          * clipping for single plane bitmaps.
>          * To circumvent this problem,  we clip to the whole window 
>          * In this case, would have also clipped to the srcRect
>          * ClipRect(&srcRect);
>          */
>         GetPortBounds(destPort,&clpRect);
>         dstPtr = &srcRect;
>         ClipRect(&clpRect);
>     }
>     if (!gc->clip_mask ) { 
>     } else if (((TkpClipMask*)gc->clip_mask)->type == TKP_CLIP_REGION) {
>         RgnHandle clipRgn = (RgnHandle)
>             ((TkpClipMask*)gc->clip_mask)->value.region;
>  
>         int xOffset = 0, yOffset = 0;
>         if (tmpRgn == NULL) {
>             tmpRgn = NewRgn();
>         }
>         if (!tkPictureIsOpen) {
>             xOffset = dstDraw->xOff + gc->clip_x_origin;
>             yOffset = dstDraw->yOff + gc->clip_y_origin;
>             OffsetRgn(clipRgn, xOffset, yOffset);
>         }
>         GetClip(tmpRgn);
>         SectRgn(tmpRgn, clipRgn, tmpRgn);
>         SetClip(tmpRgn);
>         if (!tkPictureIsOpen) {
>              OffsetRgn(clipRgn, -xOffset, -yOffset);
>         }
>     }
> 
>         CGContextRef destContext;
> 		CGRect	srcCGRect, destRect;
> 		RgnHandle clipRgn;
> 		PixMapHandle pm = GetPortPixMap( srcPort );
> 		BitMap* srcBit = GetPortBitMapForCopyBits( srcPort );
>         
>         TkMacOSXSetUpCGContext(dstDraw, destPort, gc, &destContext);
> 
> 		srcCGRect = CGRectMake(	(float)(srcDraw->xOff + src_x),
> 				(float)(srcDraw->yOff + src_y),
> 				(float)width,
> 				(float)height );
> 		destRect = CGRectMake(	(float)(dstDraw->xOff + dest_x),
> 				(float)(dstDraw->yOff + dest_y),
> 				(float)width,
> 				(float)height );
> 				
> 		size_t srcW = (size_t)width;
> 		size_t srcH = (size_t)height;
> 		size_t bytesPerRow = (size_t)GetPixRowBytes( pm );
> 		size_t totalBytes = srcH * bytesPerRow;
> 		unsigned char* bitmap = GetPixBaseAddr( pm );
> 		CGDataProviderRef provider = CGDataProviderCreateWithData( NULL, bitmap, totalBytes, NULL );
> 		CGColorSpaceRef colorSpaceUserRGB = CGColorSpaceCreateDeviceRGB();
> 		
> 		srcImage = CGImageCreate( width, height,
> 						8,//bitsPerComponent
> 						32,//bitsPerPixel
> 						bytesPerRow,
> 						colorSpaceUserRGB,//colorspace
> 						kCGImageAlphaNoneSkipFirst, //alphaInfo
> 						provider, // dataProvider
> 						NULL, //decode array
> 						0, //shouldInterpolate?
> 						kCGRenderingIntentDefault); // CGColorRenderingIntent
> 		CGDataProviderRelease( provider );
> 				
> 		CGContextSaveGState( destContext );
> 		CGContextDrawImage( destContext, destRect, srcImage );
> 		CGImageRelease( srcImage );
> 		CGContextRestoreGState( destContext );
> 		
>       TkMacOSXReleaseCGContext(dstDraw, destPort, &destContext);
> 		CGColorSpaceRelease( colorSpaceUserRGB );
> #endif
382a623
> #if 0
480a722,884
> #else
> 	Rect srcRect, dstRect;
>     Rect * srcPtr, * dstPtr;
>     CGrafPtr srcPort, destPort;
>     CGrafPtr saveWorld;
>     GDHandle saveDevice;
> 	MacDrawable *dstDraw = (MacDrawable *) d;
> 	BitMap bitmap;
> 	unsigned char *newData = NULL;
> 	int i, j;
>     Rect clpRect;
> 	Rect boundsRect;
> 	CGImageRef srcImage = NULL;
> 	CGAffineTransform coordsTransform;
> 	CGDataProviderRef provider;
> 	CGContextRef destContext;
> 	CGRect	srcCGRect, destRect;
> 	RgnHandle clipRgn;
> 
> 	destPort = TkMacOSXGetDrawablePort(d);
> 	display->request++;
> 	GetGWorld(&saveWorld, &saveDevice);
> 	SetGWorld(destPort, NULL);
> 	TkMacOSXSetUpClippingRgn(d);
> 		
> 	GetPortBounds(destPort, &boundsRect);
>         
> 	TkMacOSXSetUpCGContext(d, destPort, gc, &destContext);
> 
> 	srcCGRect = CGRectMake(	(float)src_x, (float)src_y,
> 							(float)width, (float)height );
> 								
> 	destRect = CGRectMake(	(float)(dstDraw->xOff + dest_x), (float)(dstDraw->yOff + dest_y),
> 							(float)width, (float)height );
> 	if (image->obdata) {
>         /* Image from XGetImage, copy from containing GWorld directly */
>         GWorldPtr srcPort = TkMacOSXGetDrawablePort((Drawable)image->obdata);
> 		PixMapHandle pm = GetPortPixMap( srcPort );
> 		size_t srcW = (size_t)width;
> 		size_t srcH = (size_t)height;
> 		size_t bytesPerRow = (size_t)GetPixRowBytes( pm );
> 		size_t totalBytes = srcH * bytesPerRow;
> 		unsigned char* bitmap = GetPixBaseAddr( pm );
> 		CGDataProviderRef provider = CGDataProviderCreateWithData( NULL, bitmap, totalBytes, NULL );
> 		CGColorSpaceRef colorSpaceUserRGB = CGColorSpaceCreateDeviceRGB();
> 		
> 		srcImage = CGImageCreate( width, height,
> 						8,//bitsPerComponent
> 						32,//bitsPerPixel
> 						bytesPerRow,
> 						colorSpaceUserRGB,//colorspace
> 						kCGImageAlphaNoneSkipFirst, //alphaInfo
> 						provider, // dataProvider
> 						NULL, //decode array
> 						0, //shouldInterpolate?
> 						kCGRenderingIntentDefault); // CGColorRenderingIntent
> 		CGDataProviderRelease( provider );
> 				
> 		CGContextSaveGState( destContext );
> 		
> 		CGContextClipToRect( destContext, destRect );
> 		CGContextClearRect( destContext, destRect );
> 		CGContextSetShouldAntialias( destContext, 0 );
> 		CGContextDrawImage( destContext, srcCGRect, srcImage );
> 		CGImageRelease( srcImage );
> 		CGContextRestoreGState( destContext );
> 		
>       TkMacOSXReleaseCGContext(d, destPort, &destContext);
> 		CGColorSpaceRelease( colorSpaceUserRGB );
> 				
>     } else if (image->depth == 1) {
>         /* 
>          * This code assumes a pixel depth of 1 
>          */
> 		char *newData = NULL;
> 		int bytesPerLine = 0;
> 		
>         if ((image->bytes_per_line % 2) == 1) {
>             unsigned char *newPtr, *oldPtr;
> 			bytesPerLine = image->bytes_per_line + 1;
>             newData = (char *) malloc(image->height *
>                     bytesPerLine);
>             newPtr = newData;
>             oldPtr = image->data;
>             for (i = 0; i < image->height; i++) {
>                 for (j = 0; j < image->bytes_per_line; j++) {
>                     *newPtr = ~(InvertByte((unsigned char) *oldPtr));
>                     newPtr++, oldPtr++;
>                 }
>             *newPtr = 0;
>             newPtr++;
>             }
>         } else {
> 			bytesPerLine = image->bytes_per_line;
> 			newData = (unsigned char *) malloc(image->height * image->bytes_per_line);
>             for (i = 0; i < image->height * image->bytes_per_line; i++) {
>                 newData[i] = ~(InvertByte((unsigned char) image->data[i]));
>             }
>         }
> 		size_t totalBytes = (size_t)image->height * bytesPerLine;
> 		CGDataProviderRef provider = CGDataProviderCreateWithData( NULL, newData, totalBytes, NULL );
> 		CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
> 		
> 		srcImage = CGImageCreate( (size_t)image->width, (size_t)image->height,
> 						1,//bitsPerComponent
> 						1,//bitsPerPixel
> 						bytesPerLine,
> 						grayColorSpace,//colorspace
> 						kCGImageAlphaNone, //alphaInfo
> 						provider, // dataProvider
> 						NULL, //decode array
> 						0, //shouldInterpolate?
> 						kCGRenderingIntentDefault); // CGColorRenderingIntent
> 				
> 		CGContextSaveGState( destContext );
> 		
> 		CGContextClipToRect( destContext, destRect );
> 		CGContextClearRect( destContext, destRect );
> 		CGContextSetShouldAntialias( destContext, 0 );
> 		CGContextDrawImage( destContext, srcCGRect, srcImage );
> 		CGImageRelease( srcImage );
> 		CGContextRestoreGState( destContext );
> 		
> 		CGDataProviderRelease( provider );
> 		CGColorSpaceRelease( grayColorSpace );
>       TkMacOSXReleaseCGContext(d, destPort, &destContext);
> 	}else{
> 	/* Color image */
> 		size_t srcW = (size_t)width;
> 		size_t srcH = (size_t)height;
> 		size_t bytesPerRow = (size_t)image->bytes_per_line;
> 		size_t totalBytes = srcH * bytesPerRow;
> 		CGDataProviderRef provider = CGDataProviderCreateWithData( NULL, (unsigned char*)image->data, 
> 																	totalBytes, NULL );
> 		CGColorSpaceRef colorSpaceUserRGB = CGColorSpaceCreateDeviceRGB();
> 		
> 		srcImage = CGImageCreate( srcW, srcH,
> 						8,//bitsPerComponent
> 						32,//bitsPerPixel
> 						bytesPerRow,
> 						colorSpaceUserRGB,//colorspace
> 						kCGImageAlphaNoneSkipFirst, //alphaInfo
> 						provider, // dataProvider
> 						NULL, //decode array
> 						0, //shouldInterpolate?
> 						kCGRenderingIntentDefault); // CGColorRenderingIntent
> 		CGDataProviderRelease( provider );
> 				
> 		CGContextSaveGState( destContext );
> 		
> 		CGContextClipToRect( destContext, destRect );
> 		CGContextDrawImage( destContext, srcCGRect, srcImage );
> 		CGImageRelease( srcImage );
> 		CGContextRestoreGState( destContext );
> 		
> 		CGColorSpaceRelease( colorSpaceUserRGB );
>       TkMacOSXReleaseCGContext(d, destPort, &destContext);
> 	}
> 	if (newData != NULL){
> 		free(newData);
> 	}
> 	SetGWorld(saveWorld, saveDevice);
> #endif
834a1239
> // needed for cgcontext or n
841c1246
< 	CGRect	rect;
---
> 		CGRect	rect;
845,849c1250,1254
< 	rect = CGRectMake((float) ((float) macWin->xOff + (float) x),
< 	        (float) ((float) macWin->yOff + (float) y),
< 	        (float) width,
< 		(float) height);
< 	CGContextStrokeRect(outContext, rect);
---
> 		rect = CGRectMake(	(float)((float)macWin->xOff + (float)x),
> 				(float)((float)macWin->yOff + (float)y),
> 				(float)width,
> 				(float)height );
> 		CGContextStrokeRect( outContext, rect );
916d1320
< 
919c1323
< 	CGRect	rect;
---
> 		CGRect	rect;
923,930c1327,1333
<         for (i = 0, rectPtr = rectArr; i < nRects; i++, rectPtr++) {
< 	    rect = CGRectMake((float) ((float) macWin->xOff 
<                     + (float) rectPtr->x),
< 		    (float) ((float) macWin->yOff + (float) rectPtr->y),
< 		    (float) rectPtr->width,
< 		    (float) rectPtr->height);
< 	    CGContextStrokeRect(outContext, rect);
< 	}
---
>         for (i=0, rectPtr = rectArr; i < nRects; i++, rectPtr++ ){
> 			rect = CGRectMake((float)((float)macWin->xOff + (float)rectPtr->x),
> 				    (float)((float)macWin->yOff + (float)rectPtr->y),
> 				    (float)rectPtr->width,
> 				    (float)rectPtr->height );
> 			CGContextStrokeRect( outContext, rect );
> 		}
933,934c1336
< 	TkMacOSXSetUpGraphicsPort(gc, destPort);
< 
---
> 		TkMacOSXSetUpGraphicsPort(gc, destPort);
1043d1444
< 
1701c2102
<  * TkMacOSXSetUpGraphicsPort --
---
>  * TkMacOSXSetUpCGContext --
1703c2104
<  *        Set up the graphics port from the given GC.
---
>  *        Set up the CGContext from the given GC.
