> On 25 Jun 2017, at 09:29, Graham Cox <graham....@bigpond.com> wrote: > > You could get the colorSpaceName from the source image and use that. If > there’s no conversion needed between the images it’ll probably be faster > (though seems you are giving yourself the option of creating a grayscale > version here).
The images are really b&w, but the source image claims to be NSCalibratedRGBColorSpace. So I prefer to control the colour here and not rely on the source image. Which of these two lines is preferable: NSString *colorSpaceName = useColour ? NSDeviceRGBColorSpace : NSDeviceWhiteColorSpace; NSString *colorSpaceName = useColour ? NSCalibratedRGBColorSpace : NSCalibratedWhiteColorSpace; ? >> NSInteger bitsPerPixel = samplesPerPixel * bitsPerSample; >> NSInteger bytesPerRow = pixelsWide * bitsPerPixel / 8; > > In fact you can pass 0 for these last two and the function will calculate > them for you. It’s better to do that because if there’s the slightest thing > wrong with the parameters it will give up with an exception. > The internal calculation can also round up bytesPerRow to something that will > optimise copying performance. I changed my code following your suggestion. >> [ NSGraphicsContext setCurrentContext: context ]; > > You may want to save and restore the current context around this, just to be > sure your method isn’t going to have the unwanted side effect of changing the > current context. It’s likely OK, but better safe than sorry. I just did. Turns out that the oldContext seems to be nil. But as you rightly said: “better safe than sorry”. >> NSRect destRect = NSZeroRect; destRect.size = subRect.size; > > Just use NSZeroRect for destRect. This is interpreted to mean “the size of > the destination image”, which is already <subRect>, rounded up. If you set > the size here and it’s not a whole number for some reason, there is the > potential for the very edge pixels not to get set, leaving a visible edge at > the right and bottom of the result. I tried this. But got a rather small image (just 88 bytes - usually 337 bytes) and the resulting image looks rather empty. It has the right size, but not a single pixel is set - all are transparent. But heading your warning about non-whole numbers, I added at the very start: - (NSData *)subImagePngDataWithRect: (NSRect)suxRect useColour: (BOOL)useColour { NSRect subRect = NSIntegralRect(suxRect); … } > Otherwise, looks good. Does it do the job? It did. And now it does even better. Thanks a lot for your help. Very much appreciated! Kind regards, Gerriet. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com