Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

monkeyman23555

macrumors newbie
Original poster
Feb 16, 2008
12
0
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

macrumors 6502
Jul 25, 2006
294
0
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

macrumors newbie
Original poster
Feb 16, 2008
12
0
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?
 

monkeyman23555

macrumors newbie
Original poster
Feb 16, 2008
12
0
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

macrumors regular
Jan 8, 2008
149
1
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

macrumors newbie
Original poster
Feb 16, 2008
12
0
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

macrumors newbie
Original poster
Feb 16, 2008
12
0
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:
Code:
	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."

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." );
	
	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

macrumors regular
Jan 8, 2008
149
1
"/Users/monkeyman/" is not an URL, it is a path...

You need to make it into an URL with CFURLCreateWithFileSystemPath

phjo
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
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

macrumors newbie
Original poster
Feb 16, 2008
12
0
Sweet finally worked with this code:
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:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.