Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

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


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:
[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;
}
[/code]
Thanks for help,
Knud

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • problem modifying filter
    2003-05-27 23:22:42  anonymous2 [View]

    A small bug in your code is you should write:
    p2 = destData + n*(y*w + w -x -1);
    to calculate the p2 pointer.
    This bug might result in the destData array out of bound durning the last transform operation. And if you were luck enough that there were any critical data, you application will crash.

    However, I tested your code in my project. I was not so lucky to make it crash.

    Maybe the real problem of yours is a bug in Mikebeam's framework. He said the object returned by filterImage: was autoreleased, and he did send a retain message to the returned object. But within the implemetation of filterImage:, the destImage was not sent such a message. A memory leakage!