Hm I think this will work for me. I mean, at this point it'll make me a bitmap 
and I can save it and when I open it in Photoshop it looks correct!


-(NSData *)oneBitData:(NSImage *)inputNSImage
{
//output a 1 bit image of NSData format from an NSImage given in gray mode
//get an image rep from the current qrImage
NSSize oldSize = [inputNSImage size];
NSBitmapImageRep *imgRep = [NSBitmapImageRep imageRepWithData:[inputNSImage 
TIFFRepresentation]];
int
row, column,
widthInPixels = [imgRep pixelsWide],
heightInPixels = [imgRep pixelsHigh];
NSUInteger lePixel;
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:nil
pixelsWide:widthInPixels
pixelsHigh:heightInPixels
bitsPerSample:1
samplesPerPixel:1
hasAlpha:NO
isPlanar:YES
colorSpaceName:NSDeviceWhiteColorSpace
bytesPerRow:0
bitsPerPixel:1];
[newRep setSize:oldSize];
for (row = 0; row < heightInPixels; row++)
for (column = 0; column <widthInPixels; column++)
{
//get pixel value from grey and put into 1bit
[imgRep getPixel:&lePixel atX:row y:column];
[newRep setPixel:&lePixel atX:row y:column];
}
//FYI this is not an all purpose solution, this will ONLY work with greyscale 
images
//that are already 1-bit in spirit
NSImage *tempImage = [[NSImage alloc] initWithSize:oldSize];
[tempImage addRepresentation:newRep];
return [NSData dataWithData:[tempImage TIFFRepresentation]];
}


Chris

________________________________
From: Graham Cox <graham....@bigpond.com>
To: Chris Paveglio <chris_paveg...@yahoo.com> 
Cc: Cocoa Dev List <Cocoa-dev@lists.apple.com> 
Sent: Thursday, February 9, 2012 5:43 PM
Subject: Re: drawing 1 bit images, and image resolution




On 10/02/2012, at 3:54 AM, Chris Paveglio wrote:

still looking for a way to convert to 1-bit.


So what have you tried?

There are a lot of different methods for deciding how to threshold an image - 
in other words how to decide which colours end up as 1s and which as 0s. You 
will probably find that a 50% cut based on brightness is too simplistic, and 
the results will be very disappointing. For good results you will usually have 
to quantize the colour image into a smaller set of colours using some 
statistical method, for example popular 555, or octree. This is a whole art in 
itself, and quite complex. You might get away with first converting to an 8-bit 
 greyscale image and thresholding that - at least you can do a greyscale 
conversion easily. The old Mac OS used to have API for doing colour 
quantization, but Cocoa does not.

But actually performing the thresholding isn't hard - just walk the bitmap, 
look at the (quantized) pixel value and if its above the threshold, set a 1, 
below set a 0. It's probably best to set the pixel in a second bitmap rather 
than do it in place. I recommend designing your code so that you can experiment 
with both the weighting of RGB values to determine brightness, and the 
threshold level itself. If you don't the result is certain to disappoint, and 
end up much blacker than you expected.

You can use NSBitmapImageRep's getPixel:atX:y: to read the source pixel and 
setPixel:atX:y: to set the destination pixel in a x,y double loop. Note that 
pixel values used by these methods are not colours.


--Graham

_______________________________________________

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

Reply via email to