Bitmap Image Filters
Subject:   problem modifying filter
Date:   2003-05-08 13:23:14
From:   anonymous2

I hope it's ok to post this here. I have been playing around
with this filter. I used it as a template for making my own
filter, which is supposed to simply flip an image vertically.
Now, my filter works just fine, but if I try to apply it
twice to the same image it crashes. The crash occurs in
the loop which does the actual filtering. Now, I noticed that
the original grayscale filter provided in this lesson
doesn't crash, no matter how often I apply it. As I'm not
very good at pointer magic, I don't really see what the
reason could be. Maybe someone cares to look at this and
can suggest a solution. Note that I have changed the method
to a class method, but I don't see where that could be a
problem (I did the same to the grayscale filter).
Here is my code:
+ (NSImage *)filterImage: (NSImage *)srcImage
NSBitmapImageRep *srcImageRep = [NSBitmapImageRep
imageRepWithData: [srcImage TIFFRepresentation]];
int w = [srcImageRep pixelsWide];
int h = [srcImageRep pixelsHigh];
int x, y;

NSImage *destImage = [[NSImage alloc] initWithSize: NSMakeSize(w, h)];

NSBitmapImageRep *destImageRep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL // not planar but interlaced
pixelsWide: w
pixelsHigh: h
bitsPerSample: 8
samplesPerPixel: [srcImageRep samplesPerPixel]
hasAlpha: NO
isPlanar: NO
colorSpaceName: [srcImageRep colorSpaceName]
bytesPerRow: NULL
bitsPerPixel: NULL] autorelease];

// problem: this only works for rbg-images. unsigned char is 8-bit.
// how do I dynamically get a pointer type of the size
// (bitsPerSample * samplesPerPixel)?
unsigned char *srcData = [srcImageRep bitmapData];
unsigned char *destData = [destImageRep bitmapData];
unsigned char *p1, *p2;
int n = [srcImageRep bitsPerPixel] / 8;

for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
p1 = srcData + n * (y * w + x); // pointer to pixel in row of src
p2 = destData + n * (y * w + (w - x)); // pointer to pixel in row of dest
*p2 = *p1; // the red bit
*(p2 + 1) = *(p1 + 1); // the green bit
*(p2 + 2) = *(p1 + 2); // the blue bit

[destImage addRepresentation: destImageRep];
return destImage;
Thanks for help,

1 to 1 of 1
  1. problem modifying filter
    2003-05-27 23:22:42  anonymous2 [View]

1 to 1 of 1