Image In/Output Framework?

Discussion in 'Mac Programming' started by monkeyman23555, Apr 3, 2008.

  1. macrumors newbie

    Joined:
    Feb 16, 2008
    #1
    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?
     
  2. macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    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.
     
  3. thread starter macrumors newbie

    Joined:
    Feb 16, 2008
    #3
    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?
     
  4. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #4
    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.
     
  5. macrumors regular

    Joined:
    Jan 8, 2008
    #5
  6. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #6
  7. thread starter macrumors newbie

    Joined:
    Feb 16, 2008
    #7
    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.
     
  8. macrumors regular

    Joined:
    Jan 8, 2008
    #8
    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...

    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
     
  9. thread starter macrumors newbie

    Joined:
    Feb 16, 2008
    #9
    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?
     
  10. thread starter macrumors newbie

    Joined:
    Feb 16, 2008
    #10
    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;
    }
     
  11. macrumors regular

    Joined:
    Jan 8, 2008
    #11
    "/Users/monkeyman/" is not an URL, it is a path...

    You need to make it into an URL with CFURLCreateWithFileSystemPath

    phjo
     
  12. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #12
    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.
     
  13. thread starter macrumors newbie

    Joined:
    Feb 16, 2008
    #13
    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:
     

Share This Page