Example #1
0
def createMaskFromAlphaOnlyContext(alphaContext):
    rasterData = _rasterDataForContext[alphaContext]
    # We own the data, hence remove from the mapping
    del _rasterDataForContext[alphaContext]

    imageDataSize = Quartz.CGBitmapContextGetBytesPerRow(
        alphaContext) * Quartz.CGBitmapContextGetHeight(alphaContext)
    invertDecode = [1.0, 0.0]

    # Create the data provider from the image data.
    dataProvider = Quartz.CGDataProviderCreateWithData(None, rasterData,
                                                       imageDataSize, None)

    if dataProvider is None:
        print("Couldn't create data provider!")
        return None

    mask = Quartz.CGImageMaskCreate(
        Quartz.CGBitmapContextGetWidth(alphaContext),
        Quartz.CGBitmapContextGetHeight(alphaContext),
        Quartz.CGBitmapContextGetBitsPerComponent(alphaContext),
        Quartz.CGBitmapContextGetBitsPerPixel(alphaContext),
        Quartz.CGBitmapContextGetBytesPerRow(alphaContext),
        dataProvider,
        # The decode is an inverted decode since a mask has the opposite
        # sense than alpha, i.e. 0 in a mask paints 100% and 1 in a mask
        # paints nothing.
        invertDecode,
        True)

    if mask is None:
        print("Couldn't create image mask!")
        return None

    return mask
Example #2
0
def createImageFromBitmapContext(c):
    rasterData = _rasterDataForContext[c]
    # We own the data, hence remove from the mapping
    del _rasterDataForContext[c]

    imageDataSize = Quartz.CGBitmapContextGetBytesPerRow(
        c) * Quartz.CGBitmapContextGetHeight(c)

    if rasterData is None:
        fprintf(stderr, "Context is not a bitmap context!")

    # Create the data provider from the image data
    dataProvider = Quartz.CGDataProviderCreateWithData(None, rasterData,
                                                       imageDataSize, None)
    if dataProvider is None:
        print("Couldn't create data provider!")
        return None

    # Now create the image. The parameters for the image closely match
    # the parameters of the bitmap context. This code uses a NULL
    # decode array and shouldInterpolate is true.
    image = Quartz.CGImageCreate(Quartz.CGBitmapContextGetWidth(c),
                                 Quartz.CGBitmapContextGetHeight(c),
                                 Quartz.CGBitmapContextGetBitsPerComponent(c),
                                 Quartz.CGBitmapContextGetBitsPerPixel(c),
                                 Quartz.CGBitmapContextGetBytesPerRow(c),
                                 Quartz.CGBitmapContextGetColorSpace(c),
                                 myCGContextGetBitmapInfo(c), dataProvider,
                                 None, True, Quartz.kCGRenderingIntentDefault)

    if image is None:
        print("Couldn't create image!")
        return None
    return image
    def testFunctions(self):
        bytes_val = array.array("B", (0 for i in range(100 * 80 * 4)))
        self.assertIsInstance(bytes_val, array.array)
        self.assertEqual(len(bytes_val), 100 * 80 * 4)
        ctx = Quartz.CGBitmapContextCreate(
            bytes_val,
            100,
            80,
            8,
            400,
            Quartz.CGColorSpaceCreateDeviceRGB(),
            Quartz.kCGImageAlphaPremultipliedLast,
        )
        self.assertIsInstance(ctx, Quartz.CGContextRef)

        buf = Quartz.CGBitmapContextGetData(ctx)
        self.assertIsInstance(buf, objc.varlist)
        self.assertIsInstance(buf[0], bytes)

        self.assertEqual(Quartz.CGBitmapContextGetWidth(ctx), 100)
        self.assertEqual(Quartz.CGBitmapContextGetHeight(ctx), 80)
        self.assertEqual(Quartz.CGBitmapContextGetBitsPerComponent(ctx), 8)
        self.assertEqual(Quartz.CGBitmapContextGetBitsPerPixel(ctx), 32)
        self.assertEqual(Quartz.CGBitmapContextGetBytesPerRow(ctx), 400)

        v = Quartz.CGBitmapContextGetColorSpace(ctx)
        self.assertIsInstance(v, Quartz.CGColorSpaceRef)

        v = Quartz.CGBitmapContextGetAlphaInfo(ctx)
        self.assertIsInstance(v, int)

        v = Quartz.CGBitmapContextGetBitmapInfo(ctx)
        self.assertIsInstance(v, int)

        img = Quartz.CGBitmapContextCreateImage(ctx)
        self.assertIsInstance(img, Quartz.CGImageRef)