Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Some problems with my NSImage

Some problems with my NSImage
Thread Tools
kupan787
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
May 23, 2006, 01:20 AM
 
Ok, so I don't know why but the following code used to work just fine. Last time I compiled it was when I was running Panther. I just tried compiling my code the other day (with NO modifications), and I am now getting garbled images. I am not sure what has changed that would cause this. I have tried setting GCC back to version 3.1, and told it to use the 10.3.9 headers, but I still get junk images. I have even grabbed old copies of my code (from when I was building back on Jaguar), and it produces the same results. I don't knwo what is wrong. At any rate, here is my code that is producing bad images:

Basically it takes in a NSImage (which was created from a PDF), and it applies the effect of converting it to black and white ( 8 bitsPerSample, 1 samplesPerPixel, no alpha). Perhaps there is better code I could be using (even 10.4 specific stuff, with core image or whatnot)

Code:
- (NSImage *)filterImage:(NSImage *)srcImage originalSize:(NSSize)originalSize { NSAutoreleasePool *imagePool = [[NSAutoreleasePool alloc] init]; NSBitmapImageRep *TIFFRep = [NSBitmapImageRep imageRepWithData:[srcImage TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:0.0]]; NSImage *grayImage; NSBitmapImageRep *grayRep; int samplesPerPixel = [TIFFRep samplesPerPixel]; int bytesPerSample = ([TIFFRep bitsPerPixel]/8)/samplesPerPixel; int totalBytes = [TIFFRep pixelsHigh]*[TIFFRep pixelsWide]*samplesPerPixel*bytesPerSample; unsigned char *TIFFBitmapPointer = [TIFFRep bitmapData]; unsigned char *grayBitmapPointer; grayRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:[TIFFRep pixelsWide] pixelsHigh:[TIFFRep pixelsHigh] bitsPerSample:8 samplesPerPixel:1 hasAlpha:NO isPlanar:NO colorSpaceName:NSCalibratedWhiteColorSpace bytesPerRow:0 bitsPerPixel:0]; [grayRep setSize:[TIFFRep size]]; grayBitmapPointer = [grayRep bitmapData]; if((bytesPerSample==1)&&(samplesPerPixel>2)) { int o = 0; int i = 0; int background = 255; for (i=0; i<totalBytes; i+=samplesPerPixel) { int R = (int)*(TIFFBitmapPointer+i); int G = (int)*(TIFFBitmapPointer+i+1); int B = (int)*(TIFFBitmapPointer+i+2); int grayValue = (int)((R+G+B)/3); if (samplesPerPixel==4) { int alpha = (int)*(TIFFBitmapPointer+i+3); // this is the alpha value grayValue = (int) ( (grayValue*alpha + background*(255-alpha)) / 255); } *(grayBitmapPointer+o) = 255-grayValue; o++; } } else NSLog(@"Bytes per sample != 1 or less than 3 samples per pixel");; [grayRep setSize:originalSize]; // [grayRep TIFFRepresentation]; grayImage = [[NSImage alloc] initWithData:[grayRep TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:0.0]]; [grayRep release]; [imagePool release]; return [grayImage autorelease]; }
     
arcticmac
Dedicated MacNNer
Join Date: Apr 2004
Status: Offline
Reply With Quote
Jun 9, 2006, 12:33 PM
 
You haven't by any chance switched from PPC to Intel have you? or from PPC only to a universal binary? I haven't really got much experience with any of that (or with the stuff your code does), but that might have something to do with it...
     
kupan787  (op)
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Jun 9, 2006, 03:22 PM
 
Originally Posted by arcticmac
You haven't by any chance switched from PPC to Intel have you? or from PPC only to a universal binary? I haven't really got much experience with any of that (or with the stuff your code does), but that might have something to do with it...
Nope. I was planning on trying to do that, but I wanted to fix this issue first. I reinstalled Panther on another hard drive, installed the latest version of Project Builder, and it compiles and runs just fine. So something must have changed in XCode. Not sure what...
     
arcticmac
Dedicated MacNNer
Join Date: Apr 2004
Status: Offline
Reply With Quote
Jun 9, 2006, 09:21 PM
 
More likely in the APIs. I've had numerous problems with apple making changes to AppKit (and others), that cause software compiled under 10.2 or 10.3 to work fine, and software compiled under a newer OS to have BUGS... I suggest you check out the AppKit Release Notes for the functions relevant to your problem. Also, if you could post an example of say, a starting image, what the code is supposed to do, and what you see under 10.4 compiles, that might be helpful as well, in case you don't find anything in the Release Notes.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 04:57 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,