After some searching the net, I came up with/to this code. It reads an img file, goes through it pixel by pixel and makes a new image pixel by pixel and saves that.
White pixels are made transparant and non-whites are made black.
But the problem is that the image gets destorted. The only thing I need it for is to make a few images transparant, so the code doesn't have to be that good.
Any idea?
White pixels are made transparant and non-whites are made black.
But the problem is that the image gets destorted. The only thing I need it for is to make a few images transparant, so the code doesn't have to be that good.
Any idea?
Code:
int i, j, cell_width, cell_height;
//old image
NSImage *image =[[NSImage alloc] initWithContentsOfFile:@"testIN.pdf"];
NSBitmapImageRep *bitmap = [[[NSBitmapImageRep alloc] initWithData: [image TIFFRepresentation]] autorelease];
unsigned char *bytes = [bitmap bitmapData];
//new image
[view1 setImage:image];
cell_width = [bitmap pixelsWide];
cell_height = [bitmap pixelsHigh];
NSImage *newImage = [[NSImage alloc] initWithSize:NSMakeSize(cell_width,cell_height)];
NSBitmapImageRep *new_bitmap = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
pixelsWide: cell_width
pixelsHigh: cell_height
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bytesPerRow: 0
bitsPerPixel: 0] autorelease];
unsigned char *newbytes = [new_bitmap bitmapData];
j = 0;
for (i = 0; i < cell_width * cell_height * 3; i += 3) {
unsigned char r, g, b;
r = *(bytes + i);
g = *(bytes + i + 1);
b = *(bytes + i + 2);
if(r == 255 && g == 255 && b == 255){
*(newbytes + j + 0) = 0;
*(newbytes + j + 1) = 0;
*(newbytes + j + 2) = 0;
*(newbytes + j + 3) = 0;
} else {
*(newbytes + j + 0) = 0;
*(newbytes + j + 1) = 0;
*(newbytes + j + 2) = 0;
*(newbytes + j + 3) = 255;
}
j += 4;
}
[newImage lockFocus];
[new_bitmap draw];
[newImage unlockFocus];
NSData *data = [newImage TIFFRepresentation];
[data writeToFile: @"testOut.tiff" atomically: NO]