NSimage Problem
NSimage Problem
- Subject: NSimage Problem
- From: Linea Tessile srl <email@hidden>
- Date: Mon, 7 Aug 2006 22:25:37 +0200
Hi people!
I need to transform a tiff image in a new image with different colors.
To do this I use the following code:
NSImage *immagine = [[NSImage alloc] initWithContentsOfFile: path];
float altezza = [immagine size].height;
float larghezza = [immagine size].width;
NSBitmapImageRep *bitmap1 = [NSBitmapImageRep
imageRepWithData:[immagine TIFFRepresentation]];
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:nil
pixelsWide:larghezza
pixelsHigh:altezza
bitsPerSample: [bitmap1 bitsPerSample]
samplesPerPixel: [bitmap1 samplesPerPixel]
hasAlpha: [bitmap1 hasAlpha]
isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bytesPerRow: [bitmap1 bytesPerRow]
unsigned char *imageBytes = [bitmap bitmapData];
int x,y;
float r,g,b;
NSPoint where;
for (y=altezza-1; y>=0; y--)
{
for (x=larghezza-1; x>=0; x--)
{
where = NSMakePoint(x,y);
where = [[finestra contentView] convertPoint: where
fromView: sfondo];
[self lockFocus];
NSColor *pixelColor = NSReadPixel(where);
[self unlockFocus]; // always balance -lockFocus with an
-unlockFocus.
int pixIndex;
NSColor *nuovoCol;
int indice1;
pixIndex = 3 * ((y*larghezza)+x);
// coloriOr is an array of possible color
nuovoCol = [coloriOr indexOfObject:pixelColor];
r = [nuovoCol redComponent];
g = [nuovoCol greenComponent];
b = [nuovoCol blueComponent];
imageBytes[pixIndex] = (int)r;
imageBytes[pixIndex+1] = (int)g;
imageBytes[pixIndex+2] = (int)b;
}
}
NSSize s;
s.width=larghezza;
s.height=altezza;
NSImage* newImage=[[[NSImage alloc] initWithSize:s]autorelease];
[newImage addRepresentation:bitmap];
[bitmap release];
[sfondo setImage:newImage];
But the image is drawed all black!!!
If instead I use direct value for the color It works fine!!!
For example:
imageBytes[pixIndex] = 153;
imageBytes[pixIndex+1] = 153;
imageBytes[pixIndex+2] = 153;
Where is the error?
Thanks,
Alessandra
_______________________________________________
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