3

I'm creating an NSImage from scratch in my code. To start I allocate an array of bytes:

pixels = malloc( (int)( size.width * size.height) * 4 * 8 * sizeof(unsigned char));

I then attempt to create a test image, that should contain a gradient going from pure black on the left to pure red on the right:

-(void)generateTestImage{
    int idx =0;
    for( int i=0; i<(int)(size.height); i++){// Rows
        for (int j=0; j<(int) size.width; j++){// Columns            
            pixels[ idx + 0 ] = 255 * ((float) j / size.width); // r
            pixels[ idx + 1 ] = 0; //  g
            pixels[ idx + 2 ] = 0; //  b
            pixels[ idx + 3 ] = 255;// a

            idx += 4;
        }
    }
}

Finally I convert the array of bytes into a NSImage using:

-(void) convertPixelsToImage{

    if (renderImage == NULL)
        renderImage = [[NSImage alloc] initWithSize:size];
    else{

        NSArray *prevReps = [renderImage representations];
        for (id r in prevReps)
            [renderImage removeRepresentation:r];
    }

    NSLog(@"Creating bitmapImageReprsentation with size:%@", NSStringFromSize(size));
    NSBitmapImageRep *imgRep =
    [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&pixels
                                            pixelsWide:size.width
                                            pixelsHigh:size.height
                                         bitsPerSample:8
                                       samplesPerPixel:sampPerPix
                                              hasAlpha:true
                                              isPlanar:false
                                        colorSpaceName:NSDeviceRGBColorSpace
                                          bitmapFormat:0
                                           bytesPerRow:0
                                          bitsPerPixel:0];

    [renderImage addRepresentation:imgRep];
}

However, the resulting image isn't what I expect.

enter image description here

Here is a zoomed in view of the indexing offset enter image description here

It appears as though my I'm indexing is off, as the gradient pattern shifts by 9 pixels to the left on each row.

Additionally I'm confused by the white stripe at the bottom of the image...

I've spent the last few hours trying various indexing schemes non of which fix the problem. I've verified that I'm using the same size throughout my code. Additionally I've verified that the resulting image has the right dimensions, but its the content that is off.

Is there an obvious problem with this code? Any ideas on how to fix this?

slayton
  • 20,123
  • 10
  • 60
  • 89
  • It's a nice effect, none-the-less. – Hot Licks Nov 03 '12 at 21:04
  • Remember, rows are rounded up to some boundary -- I'm thinking 16 pixels. – Hot Licks Nov 03 '12 at 21:05
  • @HotLicks, that actually fixes the problem. The original number of rows was 839, when I raise it to 848 the problem is rectified. I'd love to give you credit for helping me with this. If you post this as an answer I'll accept it. Also I'd love to see a link to some documentation too! – slayton Nov 04 '12 at 00:24
  • I don't recall where I saw it documented. I got into this stuff trying to [resize an image](http://stackoverflow.com/questions/6052188/high-quality-scaling-of-uiimage). From that it looks like the rounding is to a 4-pixel boundary, but I'd have to study it to be sure. – Hot Licks Nov 04 '12 at 01:24

1 Answers1

1

Remember, rows are rounded up to some boundary -- I'm thinking 16 pixels.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151