PDA

View Full Version : Image In/Output Framework?




monkeyman23555
Apr 3, 2008, 10:28 AM
Hello

I am trying to get some kind of native support for image (not X11, libpng.dylib) output and input on mac os x.

I was wondering if there is a framework that allows to access raw data of an image ( pixel values ), I need a to be able to write pixels and read pixels.

Does anyone maybe know a framework that I can use?



kpua
Apr 3, 2008, 10:42 AM
How about ImageIO.framework (/System/Library/Frameworks/ApplicationServices.framework/Frameworks/ImageIO.framework/)?

But NSBitmapImageRep will let you simply access pixel values too, if you're using Cocoa.

monkeyman23555
Apr 3, 2008, 10:49 AM
I am actually considering switch to cocoa and obj-c, however that means I need to learn another language before I can start programming...

Is there a tutorial anywhere on using ImageIO.framework?

Sayer
Apr 3, 2008, 12:14 PM
Go to http://developer.apple.com/ and look for sample code for Imaging. Look at QuickTime, which handles dozens and dozens of media formats in a procedural C api and now Objective-C/Cocoa with QTKit.

phjo
Apr 3, 2008, 12:14 PM
I would advice to do it with Quartz by reading this for a start :
http://developer.apple.com/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_intro/chapter_1_section_1.html

You can use it from C as well, and the investment won't be lost going the objective-c/cocoa route later on...

phjo

jeremy.king
Apr 3, 2008, 12:57 PM
What about ImageMagick (http://www.imagemagick.org/script/index.php)?

monkeyman23555
Apr 3, 2008, 10:06 PM
Can all these packages return channel values for the pixels.

I need to able to select a pixel lets say ( 200, 150 ) in the 400, 300 image. Then I need to be able to access the values of the red, green and blue channels.

I looked at ImageIO, however it does not seem to do what I need.

phjo
Apr 4, 2008, 12:33 AM
Can all these packages return channel values for the pixels.

I need to able to select a pixel lets say ( 200, 150 ) in the 400, 300 image. Then I need to be able to access the values of the red, green and blue channels.

and alpha channel... which shouldn't be a problem.

You were proposed to have look at NSBitmapImageRep class (just paste it into google and read the apple documentation about it...). Did you ?

Now if you don't want to go the cocoa route right now and had rather stick with C, Quartz it is, read everything containing bitmap in the Quartz 2d documentation I pasted the adress.

It is a bit more involved than the cocoa way, as you would need to compute yourself the adress(es) of the pixel you want with something like myBitmap[row*number_of_bytes_per_row+column*number_of_bytes_per_pixel] for the first byte of information (could be Red in the RGBA 32 bits with alpha at the end), followed with Green, Blue and Alpha...


I looked at ImageIO, however it does not seem to do what I need.

It might come handy at one point though, if you wish to import images from your favorite camera saved in RAW mode for example... (and remember the title you gave to the topic...)

phjo

monkeyman23555
Apr 4, 2008, 07:40 AM
Ah okay thanks guys :)

I will go the quartz way, I don't wanna tackle a new language right before exams :D

EDIT:
I have been searching and it seems that I need to use ImageIO to write my images to the harddrive, I guess that (CGImageDestinationCreateWithURL) is what I am looking for... I need to write it for example to "/Users/user/Documents/" is that a URL?

monkeyman23555
Apr 4, 2008, 12:25 PM
Well now I had a shot at this whole thing and thats the code that came out... Its not perfect yet obviously, but from the things I have read it was supposed to work ( most of it is taken right from the texts ).

The problem is when I do this:
CGImageDestinationRef imageDest = CGImageDestinationCreateWithURL( url, type, 1, NULL );


It hangs I believe. I am not sure what I am doing wrong it just prints "Type Created." but not the "Image Destination Created."

#include <ApplicationServices/ApplicationServices.h>

#include <stdio.h>

int main( int argc, char * const argv[] )
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
printf( "Colorspace made." );

CGContextRef context = CGBitmapContextCreate( 0,
400,
300,
8,
1600,
colorSpace,
kCGImageAlphaPremultipliedLast
);

printf( "BitmapContext made." );

CGColorSpaceRelease( colorSpace );
printf( "Colorspace released." );

//Drawing
CGContextSetRGBFillColor( context, 1, 0, 0, 1 );
CGContextFillRect( context, CGRectMake ( 0, 0, 199, 149 ) );

CGContextSetRGBFillColor( context, 0, 0, 1, 1 );
CGContextFillRect( context, CGRectMake ( 200, 150, 399, 299 ) );
printf( "Drawn Rectangles." );

//Image
CGImageRef image = CGBitmapContextCreateImage( context );
printf( "Image created." );

CFURLRef url = ( CFURLRef )"/Users/monkeyman/";
printf( "URL Created." );

CFStringRef type = kUTTypePNG;
printf( "Type Created." );

CGImageDestinationRef imageDest = CGImageDestinationCreateWithURL( url, type, 1, NULL );
printf( "Image Destination Created." );

CGImageDestinationAddImage( imageDest, image, 0 );
printf( "Image added." );

bool status = CGImageDestinationFinalize( imageDest );
printf( "Finalized." );

if( status )
printf( "status = true" );

CGImageRelease( image );
CFRelease( imageDest );
printf( "Released." );

return 0;
}

phjo
Apr 4, 2008, 12:47 PM
"/Users/monkeyman/" is not an URL, it is a path...

You need to make it into an URL with CFURLCreateWithFileSystemPath

phjo

Krevnik
Apr 4, 2008, 01:00 PM
Not to mention CFURL and CFString are object types, and a C-String isn't. You can't just typecast a C-String into a CF object like that.

Plus the URL it expects is a full file URL, not a directory URL. It will fail if you pass it a directory path, because it can't overwrite a directory with a file.

monkeyman23555
Apr 4, 2008, 01:19 PM
Sweet finally worked with this code:
#include <ApplicationServices/ApplicationServices.h>

#include <stdio.h>

int main( int argc, char * const argv[] )
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
printf( "Colorspace made." );

CGContextRef context = CGBitmapContextCreate( 0,
400,
300,
8,
1600,
colorSpace,
kCGImageAlphaPremultipliedLast
);

printf( "BitmapContext made." );

CGColorSpaceRelease( colorSpace );
printf( "Colorspace released." );

//Drawing
CGContextSetRGBFillColor( context, 1, 0, 0, 1 );
CGContextFillRect( context, CGRectMake ( 0, 0, 199, 149 ) );

CGContextSetRGBFillColor( context, 0, 0, 1, 1 );
CGContextFillRect( context, CGRectMake ( 200, 150, 399, 299 ) );
printf( "Drawn Rectangles." );

//Image
CGImageRef image = CGBitmapContextCreateImage( context );
printf( "Image created." );

char *pathstr = "/Users/monkeyman/test.png";
CFStringRef path = CFStringCreateWithCString( kCFAllocatorDefault, pathstr, kCFStringEncodingUTF8 );
printf( "Path created." );

CFURLRef url = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, path, kCFURLPOSIXPathStyle, false );
printf( "URL Created." );

CFStringRef type = kUTTypePNG;
printf( "Type Created." );

CGImageDestinationRef imageDest = CGImageDestinationCreateWithURL( url, type, 1, NULL );
printf( "Image Destination Created." );

if( imageDest == NULL )
{
printf( "imageDest was NULL." );
return 0;
}

CGImageDestinationAddImage( imageDest, image, 0 );
printf( "Image added." );

bool status = CGImageDestinationFinalize( imageDest );
printf( "Finalized." );

if( status )
printf( "status = true" );

CGImageRelease( image );
CFRelease( imageDest );
printf( "Released." );

return 0;
}

Now I just need to figure out how to write individual pixels...


Thanks guys :) here are a few apples: :apple::apple::apple: