def exportColorRampImageWithQT(context): width = 256 height = 256 bitsPerComponent = 8 bitsPerPixel = 24 bytesPerRow = width * 3 shouldInterpolate = True imageDataProvider = DataProvidersAndConsumers.createRGBRampDataProvider() if imageDataProvider is None: print >>sys.stderr, "Couldn't create Image Data provider!" return colorspace = Utilities.getTheCalibratedRGBColorSpace() image = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, kCGImageAlphaNone, imageDataProvider, None, shouldInterpolate, kCGRenderingIntentDefault) del imageDataProvider if image is None: print >>sys.stderr, "Couldn't create CGImageRef for this data!" return rect = CGRectMake(0.0, 0.0, width, height) CGContextDrawImage(context, rect, image) # Of course this is a total hack. outPath = "/tmp/imageout.jpg" exportURL = CFURLCreateFromFileSystemRepresentation(None, outPath, len(outPath), False) if exportURL: exportCGImageToJPEGFile(image, exportURL)
def doGrayRamp(context): width = 256 height = 1 bitsPerComponent = 8 bitsPerPixel = 8 bytesPerRow = width shouldInterpolate = True dataProvider = DataProvidersAndConsumers.createGrayRampDirectAccessDP() if dataProvider is None: print >>sys.stderr, "Couldn't create Gray Ramp provider!" return colorspace = Utilities.getTheCalibratedGrayColorSpace() image = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, kCGImageAlphaNone, dataProvider, None, shouldInterpolate, kCGRenderingIntentDefault) del dataProvider if image is None: print >>sys.stderr, "Couldn't create CGImageRef for image data!" return imageRect = CGRectMake(0.0, 0.0, 256, 256) # Drawing the image that is 256 samples wide and # 1 scanline high into a rectangle that is 256 x 256 units # on a side causes Quartz to stretch the image to fill # the destination rectangle. CGContextDrawImage(context, imageRect, image)
def doColorRampImage(context): width = 256 height = 256 bitsPerComponent = 8 bitsPerPixel = 24 bytesPerRow = width * 3 shouldInterpolate = True imageDataProvider = DataProvidersAndConsumers.createRGBRampDataProvider() if imageDataProvider is None: print >>sys.stderr, "Couldn't create Image Data provider!" return colorspace = Utilities.getTheCalibratedRGBColorSpace() image = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, kCGImageAlphaNone, imageDataProvider, None, shouldInterpolate, kCGRenderingIntentDefault) # No longer need the data provider. del imageDataProvider if image is None: print >>sys.stderr, "Couldn't create CGImageRef for this data!" return imageRect = CGRectMake(0.0, 0.0, width, height) # Draw the image. CGContextDrawImage(context, imageRect, image)
def doImageWithCallbacksCreatedFromURL(context, url, width, height, bitsPerComponent, isRGB): if isRGB: bitsPerPixel = bitsPerComponent * 3 else: bitsPerPixel = bitsPerComponent bytesPerRow = ((width * bitsPerPixel) + 7) / 8 shouldInterpolate = True dataProvider = DataProvidersAndConsumers.createSequentialAccessDPForURL( url) if dataProvider is None: print("Couldn't create Image Data provider!") return # Create a Quartz color space object appropriate for the image type. # These user written functions create the color space object # and that reference must be released by this code. if isRGB: colorspace = Utilities.getTheCalibratedRGBColorSpace() else: colorspace = Utilities.getTheCalibratedGrayColorSpace() image = Quartz.CGImageCreate( width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, Quartz.kCGImageAlphaNone, dataProvider, None, shouldInterpolate, Quartz.kCGRenderingIntentDefault, ) del dataProvider if image is None: print("Couldn't create CGImageRef for this data!") return imageRect = Quartz.CGRectMake(0.0, 0.0, width, height) # Draw the image into the rectangle. Quartz.CGContextDrawImage(context, imageRect, image)
def cfDataCreatePDFDocumentFromCommand(command): # Media rect for the drawing. In a real application this # should be the bounding rectangle of the graphics # that will be the PDF content. mediaRect = CGRectMake(0, 0, 612, 792) # Create a dictionary to hold the optional information describing the PDF data. dict = {} # Add the creator and title information to the PDF content. dict[kCGPDFContextTitle] = "Pasted From Sample Quartz Application" dict[kCGPDFContextCreator] = "Sample Quartz Application" # Create a mutable CFData object with unlimited capacity. data = CFDataCreateMutable(None, 0) if data is None: print >> sys.stderr, "Couldn't make CFData!" return None # Create the data consumer to capture the PDF data. consumer = DataProvidersAndConsumers.myCGDataConsumerCreateWithCFData(data) if consumer is None: print >> sys.stderr, "Couldn't create data consumer!" return None pdfContext, mediaRect = CGPDFContextCreate(consumer, None, dict) del consumer del dict if pdfContext is None: print >> sys.stderr, "Couldn't create pdf context!" return None mediaRect = CGContextBeginPage(pdfContext) if 1: CGContextSaveGState(pdfContext) if 1: CGContextClipToRect(pdfContext, mediaRect) AppDrawing.DispatchDrawing(pdfContext, command) CGContextRestoreGState(pdfContext) CGContextEndPage(pdfContext) return data
def cfDataCreatePDFDocumentFromCommand( command): # Media rect for the drawing. In a real application this # should be the bounding rectangle of the graphics # that will be the PDF content. mediaRect = CGRectMake(0, 0, 612, 792) # Create a dictionary to hold the optional information describing the PDF data. dict = {} # Add the creator and title information to the PDF content. dict[kCGPDFContextTitle] = "Pasted From Sample Quartz Application" dict[kCGPDFContextCreator] = "Sample Quartz Application" # Create a mutable CFData object with unlimited capacity. data = CFDataCreateMutable(None, 0) if data is None: print >>sys.stderr, "Couldn't make CFData!" return None # Create the data consumer to capture the PDF data. consumer = DataProvidersAndConsumers.myCGDataConsumerCreateWithCFData(data) if consumer is None: print >>sys.stderr, "Couldn't create data consumer!" return None pdfContext, mediaRect = CGPDFContextCreate(consumer, None, dict) del consumer del dict if pdfContext is None: print >>sys.stderr, "Couldn't create pdf context!" return None mediaRect = CGContextBeginPage(pdfContext) if 1: CGContextSaveGState(pdfContext) if 1: CGContextClipToRect(pdfContext, mediaRect) AppDrawing.DispatchDrawing(pdfContext, command) CGContextRestoreGState(pdfContext) CGContextEndPage(pdfContext) return data
def doImageWithCallbacksCreatedFromURL(context, url, width, height, bitsPerComponent, isRGB): if isRGB: bitsPerPixel = bitsPerComponent * 3 else: bitsPerPixel = bitsPerComponent bytesPerRow = ((width * bitsPerPixel) + 7)/8 shouldInterpolate = True dataProvider = DataProvidersAndConsumers.createSequentialAccessDPForURL(url) if dataProvider is None: print >>sys.stderr, "Couldn't create Image Data provider!" return # Create a Quartz color space object appropriate for the image type. # These user written functions create the color space object # and that reference must be released by this code. if isRGB: colorspace = Utilities.getTheCalibratedRGBColorSpace() else: colorspace = Utilities.getTheCalibratedGrayColorSpace() image = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, kCGImageAlphaNone, dataProvider, None, shouldInterpolate, kCGRenderingIntentDefault) del dataProvider if image is None: print >>sys.stder, "Couldn't create CGImageRef for this data!" return imageRect = CGRectMake(0.0, 0.0, width, height) # Draw the image into the rectangle. CGContextDrawImage(context, imageRect, image)
def doColorRampSubImage(context): # Start 4 scanlines from the top and 16 pixels from the left edge, # skip the last 40 scanlines of the image and the right # most 64 pixels. insetLeft = 16 insetTop = 4 insetRight = 64 insetBottom = 40 fullImageWidth = 256 fullImageHeight = 256 subImageWidth = fullImageWidth-insetLeft-insetRight subImageHeight = fullImageHeight-insetTop-insetBottom bitsPerComponent = 8 bitsPerPixel = 24 bytesPerRow = fullImageWidth * 3 shouldInterpolate = True imageSubRect = CGRectMake( insetLeft, insetTop, subImageWidth, subImageHeight) colorspace = Utilities.getTheCalibratedRGBColorSpace() if hasattr(Quartz, 'CGImageCreateWithImageInRect'): imageDataProvider = DataProvidersAndConsumers.createRGBRampDataProvider() if imageDataProvider is None: print >>sys.stderr, "Couldn't create Image Data provider!" return fullImage = CGImageCreate(fullImageWidth, fullImageHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, kCGImageAlphaNone, imageDataProvider, None, shouldInterpolate, kCGRenderingIntentDefault) if fullImage is not None: image = CGImageCreateWithImageInRect(fullImage, imageSubRect) # release the full image since it is no longer required. del fullImage # If the image hasn't been created yet, this code uses the # customized data provider to do so. if image is None: imageDataProvider = createRGBRampSubDataProvider(imageSubRect) if imageDataProvider is None: print >>sys.stderr, "Couldn't create Image Data provider!" return # By supplying bytesPerRow, the extra data at the end of # each scanline and the beginning of the next is properly skipped. image = CGImageCreate(subImageWidth, subImageHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, kCGImageAlphaNone, imageDataProvider, None, shouldInterpolate, kCGRenderingIntentDefault) # This code no longer needs the data provider. del imageDataProvider if image is None: print >>sys.stderr, "Couldn't create CGImageRef for this data!" return # Draw the subimage. rect = CGRectMake(0, 0, subImageWidth, subImageHeight) CGContextDrawImage(context, rect, image)