예제 #1
0
    def testFunctions(self):
        url = CoreFoundation.CFURLCreateWithFileSystemPath(
            None, __file__, CoreFoundation.kCFURLPOSIXPathStyle, False
        )

        self.assertArgIsOut(CoreFoundation.CFURLCreatePropertyFromResource, 3)
        val, errorCode = CoreFoundation.CFURLCreatePropertyFromResource(
            None, url, CoreFoundation.kCFURLFileExists, None
        )
        self.assertIsInstance(errorCode, int)
        self.assertIs(val, True)
        self.assertResultIsBOOL(CoreFoundation.CFURLCreateDataAndPropertiesFromResource)
        self.assertArgIsOut(CoreFoundation.CFURLCreateDataAndPropertiesFromResource, 2)
        self.assertArgIsOut(CoreFoundation.CFURLCreateDataAndPropertiesFromResource, 3)
        self.assertArgIsOut(CoreFoundation.CFURLCreateDataAndPropertiesFromResource, 5)
        (
            ok,
            data,
            properties,
            errorCode,
        ) = CoreFoundation.CFURLCreateDataAndPropertiesFromResource(  # noqa: B950
            None, url, None, None, None, None
        )
        self.assertTrue(ok)
        self.assertIsInstance(data, CoreFoundation.CFDataRef)
        self.assertIsInstance(properties, CoreFoundation.CFDictionaryRef)
        self.assertIsInstance(errorCode, int)
        self.assertTrue(properties[CoreFoundation.kCFURLFileExists])

        self.assertResultIsBOOL(CoreFoundation.CFURLWriteDataAndPropertiesToResource)
        self.assertArgIsOut(CoreFoundation.CFURLWriteDataAndPropertiesToResource, 3)
        url = CoreFoundation.CFURLCreateWithFileSystemPath(
            None, __file__ + "TEST", CoreFoundation.kCFURLPOSIXPathStyle, False
        )

        if sys.version_info[0] == 3:

            def buffer(value):
                return value

        else:
            from __builtin__ import buffer

        ok, errorCode = CoreFoundation.CFURLWriteDataAndPropertiesToResource(
            url, buffer(b"foobar"), None, None
        )
        self.assertTrue(ok)
        self.assertIsInstance(errorCode, int)
        self.assertTrue(os.path.exists(__file__ + "TEST"))
        with open(__file__ + "TEST", "r") as fp:
            data = fp.read()
        self.assertEqual(data, "foobar")
        self.assertResultIsBOOL(CoreFoundation.CFURLDestroyResource)
        self.assertArgIsOut(CoreFoundation.CFURLDestroyResource, 1)
        ok, errorCode = CoreFoundation.CFURLDestroyResource(url, None)
        self.assertTrue(ok)
        self.assertIsInstance(errorCode, int)
        self.assertFalse(os.path.exists(__file__ + "TEST"))
예제 #2
0
    def testFunctions(self):
        url = CFURLCreateWithFileSystemPath(None, __file__, kCFURLPOSIXPathStyle, False)

        self.assertArgIsOut(CFURLCreatePropertyFromResource, 3)
        val, errorCode = CFURLCreatePropertyFromResource(None, url, kCFURLFileExists, None)
        self.assertIsInstance(errorCode, (int, long))
        self.assertIs(val, True)
        self.assertResultIsBOOL(CFURLCreateDataAndPropertiesFromResource)
        self.assertArgIsOut(CFURLCreateDataAndPropertiesFromResource, 2)
        self.assertArgIsOut(CFURLCreateDataAndPropertiesFromResource, 3)
        self.assertArgIsOut(CFURLCreateDataAndPropertiesFromResource, 5)
        ok, data, properties, errorCode = CFURLCreateDataAndPropertiesFromResource(
                None, url, None, None, None, None)
        self.assertTrue(ok)
        self.assertIsInstance(data, CFDataRef)
        self.assertIsInstance(properties, CFDictionaryRef)
        self.assertIsInstance(errorCode, (int, long))
        self.assertTrue(properties[kCFURLFileExists])

        self.assertResultIsBOOL(CFURLWriteDataAndPropertiesToResource)
        self.assertArgIsOut(CFURLWriteDataAndPropertiesToResource, 3)
        url = CFURLCreateWithFileSystemPath(None, __file__ + "TEST", kCFURLPOSIXPathStyle, False)

        if sys.version_info[0] == 3:
            def buffer(value):
                return value
        else:
            from __builtin__ import buffer

        ok, errorCode = CFURLWriteDataAndPropertiesToResource(
                url, buffer(b"foobar"), None, None)
        self.assertTrue(ok)
        self.assertIsInstance(errorCode, (int, long))
        self.assertTrue(os.path.exists(__file__ + "TEST"))
        with open(__file__ + "TEST", 'r') as fp:
            data = fp.read()
        self.assertEqual(data , 'foobar')
        self.assertResultIsBOOL(CFURLDestroyResource)
        self.assertArgIsOut(CFURLDestroyResource, 1)
        ok, errorCode = CFURLDestroyResource(url, None)
        self.assertTrue(ok)
        self.assertIsInstance(errorCode, (int, long))
        self.assertFalse(os.path.exists(__file__ + "TEST"))
예제 #3
0
    def testImageData(self):
        width = 256
        height = 256

        rPlane = array.array("B")
        rPlane.fromlist(
            [y % 256 for y in range(0, height) for x in range(0, width)])
        if sys.version_info[0] == 3:
            buffer = memoryview
        else:
            from __builtin__ import buffer
        rPlane = buffer(rPlane)

        gPlane = array.array("B")
        gPlane.fromlist(
            [y % 256 for y in range(0, height) for x in range(width, 0, -1)])
        gPlane = buffer(gPlane)

        bPlane = array.array("B")
        bPlane.fromlist(
            [x % 256 for y in range(0, height) for x in range(0, width)])
        bPlane = buffer(bPlane)

        dataPlanes = (rPlane, gPlane, bPlane, None, None)

        # test planar, pre-made buffer
        i1 = NSBitmapImageRep.alloc(
        ).initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
            dataPlanes, width, height, 8, 3, NO, YES, NSDeviceRGBColorSpace, 0,
            0)
        self.assertTrue(i1)

        singlePlane = objc.allocateBuffer(width * height * 3)
        for i in range(0, width * height):
            si = i * 3
            if sys.version_info[0] == 2:
                singlePlane[si] = rPlane[i]
                singlePlane[si + 1] = gPlane[i]
                singlePlane[si + 2] = bPlane[i]
            else:

                def as_byte(v):
                    if isinstance(v, int):
                        return v
                    else:
                        return ord(v)

                singlePlane[si] = as_byte(rPlane[i])
                singlePlane[si + 1] = as_byte(gPlane[i])
                singlePlane[si + 2] = as_byte(bPlane[i])

        dataPlanes = (singlePlane, None, None, None, None)
        # test non-planar, premade buffer
        i2 = NSBitmapImageRep.alloc(
        ).initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
            dataPlanes, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0,
            0)

        # test grey scale
        greyPlane = array.array("B")
        greyPlane.fromlist(
            [x % 256 for x in range(0, height) for x in range(0, width)])
        greyPlanes = (greyPlane, None, None, None, None)
        greyImage = NSBitmapImageRep.alloc(
        ).initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
            greyPlanes,
            width,
            height,
            8,
            1,
            NO,
            YES,
            NSCalibratedWhiteColorSpace,
            width,
            8,
        )

        # test planar, NSBIR allocated buffer
        i3 = NSBitmapImageRep.alloc(
        ).initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
            None, width, height, 8, 3, NO, YES, NSDeviceRGBColorSpace, 0, 0)

        r, g, b, a, o = i3.getBitmapDataPlanes_()
        self.assertTrue(r)
        self.assertTrue(g)
        self.assertTrue(b)
        self.assertTrue(not a)
        self.assertTrue(not o)

        self.assertEqual(len(r), len(rPlane))
        self.assertEqual(len(g), len(gPlane))
        self.assertEqual(len(b), len(bPlane))

        r[0:len(r)] = rPlane[0:len(rPlane)]
        g[0:len(g)] = gPlane[0:len(gPlane)]
        b[0:len(b)] = bPlane[0:len(bPlane)]

        bitmapData = i2.bitmapData()

        self.assertEqual(len(bitmapData), len(singlePlane))
        try:
            memoryview
        except NameError:
            self.assertEqual(bitmapData, singlePlane)
        else:
            self.assertEqual(bitmapData.tobytes(), singlePlane)

        a = array.array("L", [255] * 4)
        self.assertArgIsOut(NSBitmapImageRep.getPixel_atX_y_, 0)
        d = i2.getPixel_atX_y_(a, 1, 1)
        self.assertIs(a, d)
예제 #4
0
 def _concatenate(bObj, offset, bArray):
     # Avoid one extra string copy by using a buffer to limit what
     # we include in the result.
     return buffer(bObj, offset) + b"".join(bArray)
예제 #5
0
    def testImageData(self):
        width = 256
        height = 256

        rPlane = array.array('B')
        rPlane.fromlist( [y%256 for y in range(0,height) for x in range(0,width)] )
        if sys.version_info[0] == 3:
            buffer = memoryview
        else:
            from __builtin__ import buffer
        rPlane = buffer(rPlane)

        gPlane = array.array('B')
        gPlane.fromlist( [y%256 for y in range(0,height) for x in range(width,0,-1)] )
        gPlane = buffer(gPlane)

        bPlane = array.array('B')
        bPlane.fromlist( [x%256 for y in range(0,height) for x in range(0,width)] )
        bPlane = buffer(bPlane)

        dataPlanes = (rPlane, gPlane, bPlane, None, None)

        # test planar, pre-made buffer
        i1 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, YES, NSDeviceRGBColorSpace, 0, 0)
        self.assertTrue(i1)

        singlePlane = objc.allocateBuffer(width*height*3)
        for i in range(0, width*height):
            si = i * 3
            if sys.version_info[0] == 2:
                singlePlane[si] = rPlane[i]
                singlePlane[si+1] = gPlane[i]
                singlePlane[si+2] = bPlane[i]
            else:
                def as_byte(v):
                    if isinstance(v, int):
                        return v
                    else:
                        return ord(v)
                singlePlane[si] = as_byte(rPlane[i])
                singlePlane[si+1] = as_byte(gPlane[i])
                singlePlane[si+2] = as_byte(bPlane[i])

        dataPlanes = (singlePlane, None, None, None, None)
        # test non-planar, premade buffer
        i2 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0, 0)

        # test grey scale
        greyPlane = array.array('B')
        greyPlane.fromlist( [x%256 for x in range(0,height) for x in range(0,width)] )
        greyPlanes = (greyPlane, None, None, None, None)
        greyImage = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(greyPlanes, width, height, 8, 1, NO, YES, NSCalibratedWhiteColorSpace, width, 8)

        # test planar, NSBIR allocated buffer
        i3 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(None, width, height, 8, 3, NO, YES, NSDeviceRGBColorSpace, 0, 0)

        r,g,b,a,o = i3.getBitmapDataPlanes_()
        self.assertTrue(r)
        self.assertTrue(g)
        self.assertTrue(b)
        self.assertTrue(not a)
        self.assertTrue(not o)

        self.assertEqual(len(r), len(rPlane))
        self.assertEqual(len(g), len(gPlane))
        self.assertEqual(len(b), len(bPlane))

        r[0:len(r)] = rPlane[0:len(rPlane)]
        g[0:len(g)] = gPlane[0:len(gPlane)]
        b[0:len(b)] = bPlane[0:len(bPlane)]

        bitmapData = i2.bitmapData()

        self.assertEqual(len(bitmapData), len(singlePlane))
        try:
            memoryview
        except NameError:
            self.assertEqual(bitmapData, singlePlane)
        else:
            self.assertEqual(bitmapData.tobytes(),
                singlePlane)

        a = array.array('L', [255]*4)
        self.assertArgIsOut(NSBitmapImageRep.getPixel_atX_y_, 0)
        d = i2.getPixel_atX_y_(a, 1, 1)
        self.assertIs(a, d)