Saving 1-bit TIFF images
Saving 1-bit TIFF images
- Subject: Saving 1-bit TIFF images
- From: "Ivan Kraljic" <email@hidden>
- Date: Mon, 21 Nov 2005 07:57:17 -0500
- Thread-topic: Saving 1-bit TIFF images
Hi all,
I'm trying to save 1-bit images as CCITT4 TIFF files. Here's the code
below. Is this the correct way of doing it? Also, I get an error
"CGImageDestinationFinalize failed for output type 'public.tiff'", when
executing "TIFFDataRes = [destImageRep
representationUsingType:NSTIFFFileType properties:bwDict];", so
obviously there's something wrong. Can anyone help?
Thanks in advance,
Ivan
--------------------
// The 1-bit image is in res, 1 monochrome pixel per byte.
Image width is w pixels.
// We pack 8 monochrome pixels into one byte, in im.
short k0, k1, k2, k3, k4, k5, k6, k7;
int w_1_bit = floor(w/8); // crop to nearest multiple of 8;
worst case we lose 7 pixels
// Pack 8 pixels into a byte
for (y=0; y<h; y++) {
for (x=0; x<w_1_bit; x++) {
k7 = res[y][x*8]>1?1:0; // bit 7
k6 = res[y][x*8+1]>1?1:0; // bit 6
k5 = res[y][x*8+2]>1?1:0; // bit 5
k4 = res[y][x*8+3]>1?1:0; // bit 4
k3 = res[y][x*8+4]>1?1:0; // bit 3
k2 = res[y][x*8+5]>1?1:0; // bit 2
k1 = res[y][x*8+6]>1?1:0; // bit 1
k0 = res[y][x*8+7]>1?1:0; // bit 0
im[y][x] = (k7<<7) | (k6<<6) |
(k5<<5) | (k4<<4) | (k3<<3) | (k2<<2) | (k1<<1) | (k0<<0);
}
}
NSImage *destImage = [[NSImage alloc]
initWithSize:NSMakeSize(w, h)];
NSBitmapImageRep *destImageRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:nil
pixelsWide:w
pixelsHigh:h
bitsPerSample:1
samplesPerPixel:1
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSCalibratedBlackColorSpace
bytesPerRow:w_1_bit
bitsPerPixel:1];
unsigned char *destData = [destImageRep bitmapData];
for (y=0; y<h; y++) {
for (x=0; x<w_1_bit; x++) {
p2 = destData + (y * w_1_bit + x);
k = im[y][x];
p2[0] = k;
}
}
NSData *TIFFDataRes;
NSDictionary *bwDict = [NSDictionary
dictionaryWithObject:@"NSTIFFCompressionCCITTFAX4"
forKey:@"NSImageCompressionMethod"];
TIFFDataRes = [destImageRep
representationUsingType:NSTIFFFileType properties:bwDict]; // ***ERROR:
CGImageDestinationFinalize failed for output type 'public.tiff'
[TIFFDataRes writeToFile:s atomically:YES];
---------------------------
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden