• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Create an NSImage using pixel (RGB) data
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Create an NSImage using pixel (RGB) data


  • Subject: Re: Create an NSImage using pixel (RGB) data
  • From: Simon Raisin <email@hidden>
  • Date: Tue, 25 May 2010 12:03:01 -0400

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 <email@hidden> 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 (email@hidden)

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:

This email sent to email@hidden

References: 
 >Create an NSImage using pixel (RGB) data (From: Simon Raisin <email@hidden>)

  • Prev by Date: Re: Deallocation while key value observers still registered
  • Next by Date: Re: -[NSMutableSet randomObject]
  • Previous by thread: Create an NSImage using pixel (RGB) data
  • Next by thread: Creating a PDFSelection...
  • Index(es):
    • Date
    • Thread