Re: drawing 1 bit images, and image resolution
Re: drawing 1 bit images, and image resolution
- Subject: Re: drawing 1 bit images, and image resolution
- From: Chris Paveglio <email@hidden>
- Date: Fri, 10 Feb 2012 09:15:12 -0800 (PST)
What I'm working with is a library that makes a QR code. It was built for iOS but I need desktop OSX (which, on a side note, seems silly because when you MAKE a qr code, you'd usually want to print it right? Otherwise phones would just read them. Anyways...). It was easy enough to adjust the library to product an NSImage instead of UIImage. It's giving me an RGBa image (which is already BW, so I don't need to quantize... nor would I know how, that is way out of my league of skills I think). I don't want to fiddle around with the library so much because I'm afraid I will break it, and some of it's C++ and straight C. It would be great if it could output an greyscale image at least, instead of RBGa. But right now it doesn't so I will deal with the RGBa, and all channels are equal so I could use any.
My C skills aren't so great, and I feel like if I can't make it all work properly, I wouldn't know if I had -any- of it working properly.
After a while of studying and trying some things, here's what I've got so far. I know it's not correct and doesn't work yet.
-(NSData *)oneBitData:(NSImage *)inputNSImage
{
NSData *testOnly = [NSData data];
//this will be somewhat akin to un-reading the qrEncoder +renderDataMatrix method
//I need to get 1 channel of the RGBa only here
//or, change the library to return a greyscale image
//output a 1 bit image of NSData format from an NSImage given in RGB mode
//get an image rep from the current qrImage
NSBitmapImageRep*imgRep = [NSBitmapImageRepimageRepWithData:[inputNSImage TIFFRepresentation]];
int
row, column,
widthInPixels = [imgRep pixelsWide],
heightInPixels = [imgRep pixelsHigh];
//make a buffer of bits, like 50px x 50px = 2500 chars
const int rawDataSize = widthInPixels * heightInPixels;
unsigned char *rawData = (unsigned char*)malloc(rawDataSize);
NSUInteger lePixel;
for (row = 0; row < heightInPixels; row++)
for (column = 0; column <widthInPixels; column++)
{
[imgRep getPixel:&lePixel atX:row y:column];
//copy the value from first memory area to the same
//location in the raw data area, it'll be either B or W, no inbetween values
NSLog(@"bit %d = %d", (row * column) , lePixel);
rawData[row * column] = lePixel < 128 ? 0 : 255;
}
return testOnly;
}
________________________________
From: Graham Cox <email@hidden>
To: Chris Paveglio <email@hidden>
Cc: Cocoa Dev List <email@hidden>
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 (email@hidden)
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:
This email sent to email@hidden