1from PyObjCTools.TestSupport import * 2import objc 3import array 4 5from objc import YES, NO 6from AppKit import * 7 8class TestNSBitmapImageRep(TestCase): 9 def testInstantiation(self): 10 # widthxheight RGB 24bpp image 11 width = 256 12 height = 256 13 dataPlanes = (None, None, None, None, None) 14 i1 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0, 0) 15 self.assert_(i1) 16 17 i2 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(None, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0, 0) 18 self.assert_(i2) 19 20 def testImageData(self): 21 width = 256 22 height = 256 23 24 rPlane = array.array('B') 25 rPlane.fromlist( [y%256 for y in range(0,height) for x in range(0,width)] ) 26 rPlane = buffer(rPlane) 27 28 gPlane = array.array('B') 29 gPlane.fromlist( [y%256 for x in range(0,height) for x in range(width,0,-1)] ) 30 gPlane = buffer(gPlane) 31 32 bPlane = array.array('B') 33 bPlane.fromlist( [x%256 for x in range(0,height) for x in range(0,width)] ) 34 bPlane = buffer(bPlane) 35 36 dataPlanes = (rPlane, gPlane, bPlane, None, None) 37 38 # test planar, pre-made buffer 39 i1 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, YES, NSDeviceRGBColorSpace, 0, 0) 40 self.assert_(i1) 41 42 singlePlane = objc.allocateBuffer(width*height*3) 43 for i in range(0, 256*256): 44 si = i * 3 45 singlePlane[si] = rPlane[i] 46 singlePlane[si+1] = gPlane[i] 47 singlePlane[si+2] = bPlane[i] 48 49 dataPlanes = (singlePlane, None, None, None, None) 50 # test non-planar, premade buffer 51 i2 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0, 0) 52 53 # test grey scale 54 greyPlane = array.array('B') 55 greyPlane.fromlist( [x%256 for x in range(0,height) for x in range(0,width)] ) 56 greyPlanes = (greyPlane, None, None, None, None) 57 greyImage = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(greyPlanes, width, height, 8, 1, NO, YES, NSCalibratedWhiteColorSpace, width, 8) 58 59 # test planar, NSBIR allocated buffer 60 i3 = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(None, width, height, 8, 3, NO, YES, NSDeviceRGBColorSpace, 0, 0) 61 62 r,g,b,a,o = i3.getBitmapDataPlanes_() 63 self.assert_(r) 64 self.assert_(g) 65 self.assert_(b) 66 self.assert_(not a) 67 self.assert_(not o) 68 69 self.assertEquals(len(r), len(rPlane)) 70 self.assertEquals(len(g), len(gPlane)) 71 self.assertEquals(len(b), len(bPlane)) 72 73 r[0:len(r)] = rPlane[0:len(rPlane)] 74 g[0:len(g)] = gPlane[0:len(gPlane)] 75 b[0:len(b)] = bPlane[0:len(bPlane)] 76 77 bitmapData = i2.bitmapData() 78 self.assertEquals(len(bitmapData), len(singlePlane)) 79 self.assertEquals(bitmapData, singlePlane) 80 81 a = array.array('I', [255]*4) 82 self.failUnlessArgIsOut(NSBitmapImageRep.getPixel_atX_y_, 0) 83 d = i2.getPixel_atX_y_(a, 1, 1) 84 self.failUnless(a is d) 85 86 87class TestBadCreation(TestCase): 88 89 # Redirect stderr to /dev/null for the duration of this test, 90 # NSBitmapImageRep will write an error message to stderr. 91 92 def setUp(self): 93 import os 94 self.duppedStderr = os.dup(2) 95 fp = os.open('/dev/null', os.O_RDWR) 96 os.dup2(fp, 2) 97 os.close(fp) 98 99 def tearDown(self): 100 import os 101 os.dup2(self.duppedStderr, 2) 102 103 104 105 def test_AllocInit(self): 106 y = NSBitmapImageRep.alloc() 107 try: 108 self.assertRaises(ValueError, y.init) 109 finally: 110 width = 256 111 height = 256 112 dataPlanes = (None, None, None, None, None) 113 y = y.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(dataPlanes, width, height, 8, 3, NO, NO, NSDeviceRGBColorSpace, 0, 0) 114 115 def testConstants(self): 116 self.failUnlessEqual(NSTIFFCompressionNone, 1) 117 self.failUnlessEqual(NSTIFFCompressionCCITTFAX3, 3) 118 self.failUnlessEqual(NSTIFFCompressionCCITTFAX4, 4) 119 self.failUnlessEqual(NSTIFFCompressionLZW, 5) 120 self.failUnlessEqual(NSTIFFCompressionJPEG, 6) 121 self.failUnlessEqual(NSTIFFCompressionNEXT, 32766) 122 self.failUnlessEqual(NSTIFFCompressionPackBits, 32773) 123 self.failUnlessEqual(NSTIFFCompressionOldJPEG, 32865) 124 self.failUnlessEqual(NSTIFFFileType, 0) 125 self.failUnlessEqual(NSBMPFileType, 1) 126 self.failUnlessEqual(NSGIFFileType, 2) 127 self.failUnlessEqual(NSJPEGFileType, 3) 128 self.failUnlessEqual(NSPNGFileType, 4) 129 self.failUnlessEqual(NSJPEG2000FileType, 5) 130 self.failUnlessEqual(NSImageRepLoadStatusUnknownType, -1) 131 self.failUnlessEqual(NSImageRepLoadStatusReadingHeader, -2) 132 self.failUnlessEqual(NSImageRepLoadStatusWillNeedAllData, -3) 133 self.failUnlessEqual(NSImageRepLoadStatusInvalidData, -4) 134 self.failUnlessEqual(NSImageRepLoadStatusUnexpectedEOF, -5) 135 self.failUnlessEqual(NSImageRepLoadStatusCompleted, -6) 136 self.failUnlessEqual(NSAlphaFirstBitmapFormat, 1 << 0) 137 self.failUnlessEqual(NSAlphaNonpremultipliedBitmapFormat, 1 << 1) 138 self.failUnlessEqual(NSFloatingPointSamplesBitmapFormat, 1 << 2) 139 140 self.failUnlessIsInstance(NSImageCompressionMethod, unicode) 141 self.failUnlessIsInstance(NSImageCompressionFactor, unicode) 142 self.failUnlessIsInstance(NSImageDitherTransparency, unicode) 143 self.failUnlessIsInstance(NSImageRGBColorTable, unicode) 144 self.failUnlessIsInstance(NSImageInterlaced, unicode) 145 self.failUnlessIsInstance(NSImageColorSyncProfileData, unicode) 146 self.failUnlessIsInstance(NSImageFrameCount, unicode) 147 self.failUnlessIsInstance(NSImageCurrentFrame, unicode) 148 self.failUnlessIsInstance(NSImageCurrentFrameDuration, unicode) 149 self.failUnlessIsInstance(NSImageLoopCount, unicode) 150 self.failUnlessIsInstance(NSImageGamma, unicode) 151 self.failUnlessIsInstance(NSImageProgressive, unicode) 152 self.failUnlessIsInstance(NSImageEXIFData, unicode) 153 self.failUnlessIsInstance(NSImageFallbackBackgroundColor, unicode) 154 155 def testTiffCompression(self): 156 lst, nr = NSBitmapImageRep.getTIFFCompressionTypes_count_(None, None) 157 self.failUnlessIsInstance(lst, tuple) 158 self.failUnlessIsInstance(nr, (int, long)) 159 self.failUnlessEqual(len(lst), nr) 160 self.failIfEqual(len(lst), 0) 161 self.failUnlessIsInstance(lst[0], (int, long)) 162 163 def testMethods(self): 164 self.failUnlessResultIsBOOL(NSBitmapImageRep.isPlanar) 165 self.failUnlessResultIsBOOL(NSBitmapImageRep.canBeCompressedUsing_) 166 self.failUnlessArgIsBOOL(NSBitmapImageRep.incrementalLoadFromData_complete_, 1) 167 168 self.failUnlessArgIsOut(NSBitmapImageRep.getCompression_factor_, 0) 169 self.failUnlessArgIsOut(NSBitmapImageRep.getCompression_factor_, 1) 170 171 self.fail("- (id)initWithBitmapDataPlanes:(unsigned char **)planes pixelsWide:(NSInteger)width pixelsHigh:(NSInteger)height bitsPerSample:(NSInteger)bps samplesPerPixel:(NSInteger)spp hasAlpha:(BOOL)alpha isPlanar:(BOOL)isPlanar colorSpaceName:(NSString *)colorSpaceName bitmapFormat:(NSBitmapFormat)bitmapFormat bytesPerRow:(NSInteger)rBytes bitsPerPixel:(NSInteger)pBits;") 172 173 174 175if __name__ == '__main__': 176 main( ) 177