This is what I came up with - I hope it helps someone.

#import <Foundation/Foundation.h>


typedef struct

{

    unsigned char red;

    unsigned char green;

    unsigned char blue;

} HL_RGB;


int main (int argc, const char * argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


    NSSize imageSize = { 100, 100 };

    NSBitmapImageRep* bitmapImageRep = [[NSBitmapImageRep alloc]

                                        initWithBitmapDataPlanes:(unsigned
char **)NULL

                                        pixelsWide:(int)imageSize.width

                                        pixelsHigh:(int)imageSize.height

                                        bitsPerSample:(int)8

                                        samplesPerPixel:(int)3

                                        hasAlpha:(BOOL)NO

                                        isPlanar:(BOOL)NO

                                        colorSpaceName:(NSString *)
NSDeviceRGBColorSpace

                                        bytesPerRow:(int)(imageSize.width *
sizeof(HL_RGB))

                                        bitsPerPixel:(int)0];



    HL_RGB* pOutputPixel = (HL_RGB*)[bitmapImageRep bitmapData];



    // Draw a red diagonal line

    for (NSUInteger row = 0; row < imageSize.width; row++)

    {

        for (NSUInteger col = 0; col < imageSize.height; col++)

        {

            if (row == col)

            {

                pOutputPixel[row * (int)imageSize.width + col].red = 0xFF;

            }

        }

    }


    // Write the data out to disk - no compression

    NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber
numberWithFloat:1.0]

                                                           forKey:
NSImageCompressionFactor];

    NSData* outputImageData = [bitmapImageRep representationUsingType:
NSJPEGFileType properties:imageProps];

    [outputImageData writeToFile:@"output.jpg" atomically:NO];



    [bitmapImageRep release];



    [pool drain];

    return 0;

}


On Tue, May 25, 2010 at 8:24 AM, Simon Raisin <catx...@gmail.com> wrote:

> Hi,
>
> I would like to create an new NSImage (of a specified size) by modifying
> its pixel data directly.  I'm assuming that I have to create/provide a
> representation then call [rep bitmapData], but I've been unable to come up
> with a working solution thus far.
>
> Does anyone know of an example I might look at?
>
> Thanks in advance,
> Cat
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to