1from PyObjCTools.TestSupport import *
2from Quartz import *
3
4from CoreFoundation import CFArrayRef
5from Foundation import NSMutableData
6
7try:
8    unicode
9except NameError:
10    unicode = str
11
12try:
13    long
14except NameError:
15    long = int
16
17import sys, os
18
19if sys.version_info[0] != 2:
20    def buffer(value):
21        if isinstance(value, bytes):
22            return value
23        return value.encode('latin1')
24
25
26
27class TestCGImageDestination (TestCase):
28    def testTypes(self):
29        self.assertIsCFType(CGImageDestinationRef)
30
31    def testConstants(self):
32        self.assertIsInstance(kCGImageDestinationLossyCompressionQuality, unicode)
33        self.assertIsInstance(kCGImageDestinationBackgroundColor, unicode)
34
35    def testFunctions(self):
36        self.assertIsInstance(CGImageDestinationGetTypeID(), (int, long))
37
38        self.assertResultIsCFRetained(CGImageDestinationCopyTypeIdentifiers)
39        v = CGImageDestinationCopyTypeIdentifiers()
40        self.assertIsInstance(v, CFArrayRef)
41        if v:
42            self.assertIsInstance(v[0], unicode)
43
44        data = NSMutableData.data()
45        self.assertResultIsCFRetained(CGImageDestinationCreateWithData)
46        dest = CGImageDestinationCreateWithData(data, v[0], 1, None)
47        self.assertIsInstance(dest, CGImageDestinationRef)
48
49        url = CFURLCreateWithFileSystemPath(None,
50                "/tmp/pyobjc.test.pdf", kCFURLPOSIXPathStyle, False)
51        self.assertResultIsCFRetained(CGImageDestinationCreateWithURL)
52        dest = CGImageDestinationCreateWithURL(url, "public.tiff", 2, None)
53        self.assertIsInstance(dest, CGImageDestinationRef)
54
55        CGImageDestinationSetProperties(dest, {b'key'.decode('latin1'): b'value'.decode('latin1')})
56
57        provider = CGDataProviderCreateWithCFData(buffer("1" * 4 * 100 * 80))
58        img = CGImageCreate(100, 80, 8, 32, 400, CGColorSpaceCreateDeviceRGB(),
59                kCGImageAlphaPremultipliedLast, provider, None, False, kCGRenderingIntentDefault)
60        self.assertIsInstance(img, CGImageRef)
61
62        CGImageDestinationAddImage(dest, img, None)
63
64        image_path = "/System/Library//ColorSync/Calibrators/Display Calibrator.app/Contents/Resources/bullet.tif"
65        if not os.path.exists(image_path):
66            image_path = "/System/Library//ColorSync/Calibrators/Display Calibrator.app/Contents/Resources/brightness.png"
67        if not os.path.exists(image_path):
68            image_path = "/System/Library//ColorSync/Calibrators/Display Calibrator.app/Contents/Resources/brightness.tiff"
69        url = CFURLCreateWithFileSystemPath(None,
70            image_path,
71            kCFURLPOSIXPathStyle, False)
72
73        isrc = CGImageSourceCreateWithURL(url, None)
74        CGImageDestinationAddImageFromSource(dest,  isrc, 0, None)
75
76        self.assertResultHasType(CGImageDestinationFinalize, objc._C_BOOL)
77        v = CGImageDestinationFinalize(dest)
78        self.assertIsInstance(v, bool)
79        self.assertIs(v, True)
80
81        dta = NSMutableData.alloc().init()
82        cons = CGDataConsumerCreateWithCFData(dta)
83
84        self.assertResultIsCFRetained(CGImageDestinationCreateWithDataConsumer)
85        c = CGImageDestinationCreateWithDataConsumer(cons, 'public.tiff', 1, None)
86        self.assertIsInstance(c, CGImageDestinationRef)
87
88    @min_os_level('10.8')
89    def testConstants10_8(self):
90        self.assertIsInstance(kCGImageDestinationMetadata, unicode)
91        self.assertIsInstance(kCGImageDestinationMergeMetadata, unicode)
92        self.assertIsInstance(kCGImageMetadataShouldExcludeXMP, unicode)
93        self.assertIsInstance(kCGImageDestinationDateTime, unicode)
94        self.assertIsInstance(kCGImageDestinationOrientation, unicode)
95
96    @min_os_level('10.8')
97    def testFunctions10_8(self):
98        CGImageDestinationAddImageAndMetadata
99        self.assertResultHasType(CGImageDestinationCopyImageSource, objc._C_BOOL)
100        self.assertArgIsOut(CGImageDestinationCopyImageSource, 3)
101
102
103
104
105if __name__ == "__main__":
106    main()
107