1from PyObjCTools.TestSupport import * 2import objc 3import array 4import sys 5 6from objc import YES, NO 7from AppKit import * 8 9try: 10 unicode 11except NameError: 12 unicode = str 13 14 15try: 16 long 17except NameError: 18 long = int 19 20class TestNSBitmapImageRep(TestCase): 21 def testInstantiation(self): 22 # widthxheight RGB 24bpp image 23 width = 256 24 height = 256 25 dataPlanes = (None, None, None, None, None) 26 dataPlanes = None 27 i1 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0, 0) 28 self.assertTrue(i1) 29 30 i2 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(None, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0, 0) 31 self.assertTrue(i2) 32 33 def testPixelFormat(self): 34 width = 16 35 height = 16 36 37 i1 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(None, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, NSAlphaFirstBitmapFormat, 0, 0) 38 self.assertIsInstance(i1, NSBitmapImageRep) 39 40 singlePlane = objc.allocateBuffer(width*height*4) 41 for i in range(0, width*height): 42 si = i * 4 43 singlePlane[si] = 1 44 singlePlane[si+1] = 2 45 singlePlane[si+2] = 3 46 singlePlane[si+3] = 4 47 dataPlanes = (singlePlane, None, None, None, None) 48 # test non-planar, premade buffer 49 i2 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, NSAlphaFirstBitmapFormat, 0, 0) 50 self.assertIsInstance(i2, NSBitmapImageRep) 51 52 bitmapData = i2.bitmapData() 53 54 self.assertEqual(len(bitmapData), width * height * 4) 55 56 def testImageData(self): 57 width = 256 58 height = 256 59 60 rPlane = array.array('B') 61 rPlane.fromlist( [y%256 for y in range(0,height) for x in range(0,width)] ) 62 if sys.version_info[0] == 3: 63 buffer = memoryview 64 else: 65 from __builtin__ import buffer 66 rPlane = buffer(rPlane) 67 68 gPlane = array.array('B') 69 gPlane.fromlist( [y%256 for y in range(0,height) for x in range(width,0,-1)] ) 70 gPlane = buffer(gPlane) 71 72 bPlane = array.array('B') 73 bPlane.fromlist( [x%256 for y in range(0,height) for x in range(0,width)] ) 74 bPlane = buffer(bPlane) 75 76 dataPlanes = (rPlane, gPlane, bPlane, None, None) 77 78 # test planar, pre-made buffer 79 i1 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, YES, NSDeviceRGBColorSpace, 0, 0) 80 self.assertTrue(i1) 81 82 singlePlane = objc.allocateBuffer(width*height*3) 83 for i in range(0, width*height): 84 si = i * 3 85 if sys.version_info[0] == 2: 86 singlePlane[si] = rPlane[i] 87 singlePlane[si+1] = gPlane[i] 88 singlePlane[si+2] = bPlane[i] 89 else: 90 def as_byte(v): 91 if isinstance(v, int): 92 return v 93 else: 94 return ord(v) 95 singlePlane[si] = as_byte(rPlane[i]) 96 singlePlane[si+1] = as_byte(gPlane[i]) 97 singlePlane[si+2] = as_byte(bPlane[i]) 98 99 dataPlanes = (singlePlane, None, None, None, None) 100 # test non-planar, premade buffer 101 i2 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0, 0) 102 103 # test grey scale 104 greyPlane = array.array('B') 105 greyPlane.fromlist( [x%256 for x in range(0,height) for x in range(0,width)] ) 106 greyPlanes = (greyPlane, None, None, None, None) 107 greyImage = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(greyPlanes, width, height, 8, 1, NO, YES, NSCalibratedWhiteColorSpace, width, 8) 108 109 # test planar, NSBIR allocated buffer 110 i3 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(None, width, height, 8, 3, NO, YES, NSDeviceRGBColorSpace, 0, 0) 111 112 r,g,b,a,o = i3.getBitmapDataPlanes_() 113 self.assertTrue(r) 114 self.assertTrue(g) 115 self.assertTrue(b) 116 self.assertTrue(not a) 117 self.assertTrue(not o) 118 119 self.assertEqual(len(r), len(rPlane)) 120 self.assertEqual(len(g), len(gPlane)) 121 self.assertEqual(len(b), len(bPlane)) 122 123 r[0:len(r)] = rPlane[0:len(rPlane)] 124 g[0:len(g)] = gPlane[0:len(gPlane)] 125 b[0:len(b)] = bPlane[0:len(bPlane)] 126 127 bitmapData = i2.bitmapData() 128 129 self.assertEqual(len(bitmapData), len(singlePlane)) 130 try: 131 memoryview 132 except NameError: 133 self.assertEqual(bitmapData, singlePlane) 134 else: 135 self.assertEqual(bitmapData.tobytes(), 136 singlePlane) 137 138 a = array.array('L', [255]*4) 139 self.assertArgIsOut(NSBitmapImageRep.getPixel_atX_y_, 0) 140 d = i2.getPixel_atX_y_(a, 1, 1) 141 self.assertIs(a, d) 142 143class TestBadCreation(TestCase): 144 145 # Redirect stderr to /dev/null for the duration of this test, 146 # NSBitmapImageRep will write an error message to stderr. 147 148 def setUp(self): 149 import os 150 self.duppedStderr = os.dup(2) 151 fp = os.open('/dev/null', os.O_RDWR) 152 os.dup2(fp, 2) 153 os.close(fp) 154 155 def tearDown(self): 156 import os 157 os.dup2(self.duppedStderr, 2) 158 159 160 161 def test_AllocInit(self): 162 y = NSBitmapImageRep.alloc() 163 try: 164 self.assertRaises(ValueError, y.init) 165 finally: 166 width = 256 167 height = 256 168 dataPlanes = (None, None, None, None, None) 169 y = y.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0, 0) 170 171 def testConstants(self): 172 self.assertEqual(NSTIFFCompressionNone, 1) 173 self.assertEqual(NSTIFFCompressionCCITTFAX3, 3) 174 self.assertEqual(NSTIFFCompressionCCITTFAX4, 4) 175 self.assertEqual(NSTIFFCompressionLZW, 5) 176 self.assertEqual(NSTIFFCompressionJPEG, 6) 177 self.assertEqual(NSTIFFCompressionNEXT, 32766) 178 self.assertEqual(NSTIFFCompressionPackBits, 32773) 179 self.assertEqual(NSTIFFCompressionOldJPEG, 32865) 180 self.assertEqual(NSTIFFFileType, 0) 181 self.assertEqual(NSBMPFileType, 1) 182 self.assertEqual(NSGIFFileType, 2) 183 self.assertEqual(NSJPEGFileType, 3) 184 self.assertEqual(NSPNGFileType, 4) 185 self.assertEqual(NSJPEG2000FileType, 5) 186 self.assertEqual(NSImageRepLoadStatusUnknownType, -1) 187 self.assertEqual(NSImageRepLoadStatusReadingHeader, -2) 188 self.assertEqual(NSImageRepLoadStatusWillNeedAllData, -3) 189 self.assertEqual(NSImageRepLoadStatusInvalidData, -4) 190 self.assertEqual(NSImageRepLoadStatusUnexpectedEOF, -5) 191 self.assertEqual(NSImageRepLoadStatusCompleted, -6) 192 self.assertEqual(NSAlphaFirstBitmapFormat, 1 << 0) 193 self.assertEqual(NSAlphaNonpremultipliedBitmapFormat, 1 << 1) 194 self.assertEqual(NSFloatingPointSamplesBitmapFormat, 1 << 2) 195 196 self.assertIsInstance(NSImageCompressionMethod, unicode) 197 self.assertIsInstance(NSImageCompressionFactor, unicode) 198 self.assertIsInstance(NSImageDitherTransparency, unicode) 199 self.assertIsInstance(NSImageRGBColorTable, unicode) 200 self.assertIsInstance(NSImageInterlaced, unicode) 201 self.assertIsInstance(NSImageColorSyncProfileData, unicode) 202 self.assertIsInstance(NSImageFrameCount, unicode) 203 self.assertIsInstance(NSImageCurrentFrame, unicode) 204 self.assertIsInstance(NSImageCurrentFrameDuration, unicode) 205 self.assertIsInstance(NSImageLoopCount, unicode) 206 self.assertIsInstance(NSImageGamma, unicode) 207 self.assertIsInstance(NSImageProgressive, unicode) 208 self.assertIsInstance(NSImageEXIFData, unicode) 209 self.assertIsInstance(NSImageFallbackBackgroundColor, unicode) 210 211 def testTiffCompression(self): 212 lst, nr = NSBitmapImageRep.getTIFFCompressionTypes_count_(None, None) 213 self.assertIsInstance(lst, tuple) 214 self.assertIsInstance(nr, (int, long)) 215 self.assertEqual(len(lst), nr) 216 self.assertNotEqual(len(lst), 0) 217 self.assertIsInstance(lst[0], (int, long)) 218 219 def testMethods(self): 220 self.assertResultIsBOOL(NSBitmapImageRep.isPlanar) 221 self.assertResultIsBOOL(NSBitmapImageRep.canBeCompressedUsing_) 222 self.assertArgIsBOOL(NSBitmapImageRep.incrementalLoadFromData_complete_, 1) 223 224 self.assertArgIsOut(NSBitmapImageRep.getCompression_factor_, 0) 225 self.assertArgIsOut(NSBitmapImageRep.getCompression_factor_, 1) 226 227 228 229if __name__ == '__main__': 230 main( ) 231