[futurebasic] Re: [FB] Re Quartz BitMap Export?

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 2009 : Group Archive : Group : All Groups

From: Deep <Info@...>
Date: Tue, 20 Jan 2009 00:02:01 +0000
Hi Robert,

Many thanks for that, it was the "missing link" thats now in place!

Kind regards,

Deep


> From: Robert Purves <listrp@...>
> Reply-To: <futurebasic@...>
> Date: Mon, 19 Jan 2009 22:39:36 +1300
> To: <futurebasic@...>
> Subject: Re: [FB] Re Quartz BitMap Export?
> 
> 
> Deep wrote:
> 
>> Has anyone created a Quartz BitMap from a CGContext? Trying to take
>> a (PDF)
>> CGContext in Quartz and save as a PICT or TIFF file in 10.4 onwards?
>> 
>> There is no obvious way to convert a CGContext into CGImageRef as
>> far as the
>> docs show. Thinking of creating a CGImageDestinationRef which is
>> easily
>> saved as a PICT file but requires a CGImageRef as the source data.
>> But how
>> to "convert" the CGContext into a CGImageRef?
> 
> CGBitmapContextCreateImage() is one way. A CGBitmapContext corresponds
> to a GWorld in QuickDraw.
> 
> Robert P.
> 
> 
> '------------
> /*
> How to manage and draw into a CGBitmapContext,
> and save a CG drawing as an image file.
> 
>    rp 20090119
> */
> include "Tlbx CoreGraphics.incl"
> 
> // CGImageDestination.h  AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER
> #if ndef _DEFINEDINCARBON
> #define CGImageDestinationRef as pointer
> toolbox fn CGImageDestinationCreateWithURL( CFURLRef url, CFStringRef
> type, UInt32 count, CFDictionaryRef options ) = CGImageDestinationRef
> toolbox fn CGImageDestinationFinalize( CGImageDestinationRef idst ) =
> Boolean
> toolbox CGImageDestinationAddImage( CGImageDestinationRef idst,
> CGImageRef image, CFDictionaryRef properties )
> #endif
> 
> 
> local mode
> local fn MyCreateCGBitmapContext( w as UInt32, h as UInt32 ) as
> CGContextRef
> '~'1
> dim as CGColorSpaceRef  cs
> dim as CGContextRef     ctx : ctx = 0
> dim as long             rowBytes
> dim as pointer          imageBuf
> 
> ctx = 0
> rowBytes = w*4 // 4 == (1 byte for each of RGBA)
> rowBytes = (rowBytes + 15) and not( 15 ) // round up to multiple of 16
> for speed
> imageBuf = fn calloc( h, rowBytes ) // allocate, cleared to zeros
> long if ( imageBuf )
> cs = fn CGColorSpaceCreateDeviceRGB()
> ctx = fn CGBitmapContextCreate( imageBuf, w, h, 8, rowBytes, cs,
> _kCGImageAlphaPremultipliedFirst )
> CGColorSpaceRelease( cs )
> end if
> end fn = ctx
> 
> 
> local mode
> local fn MyDisposeCGBitmapContext( ctx as CGContextRef )
> '~'1
> dim as pointer  imageBuf
> 
> imageBuf = fn CGBitmapContextGetData( ctx )
> CGContextRelease( ctx )
> if ( imageBuf ) then fn free( imageBuf )
> end fn
> 
> 
> local mode
> local fn DrawSomething( ctx as CGContextRef )
> '~'1
> dim as CGRect  r
> 
> // draw a frame 2.5 units inside the bitmap context's bounds
> r = fn CGRectMake( 0, 0, fn CGBitmapContextGetWidth( ctx ), fn
> CGBitmapContextGetHeight( ctx ) )
> CGContextStrokeRect( ctx, fn CGRectInset( r, 2.5, 2.5 ) )
> end fn
> 
> local mode
> local fn SaveImageFile( ctx as CGContextRef, url as CFURLRef, type as
> CFStringRef )
> '~'1
> dim as CGImageDestinationRef imageDest
> dim as CGImageRef            image
> dim as Boolean               success : success = _false
> 
> // CGBitMapContext -> CGImages
> image = fn CGBitmapContextCreateImage( ctx )
> long if ( image )
> // we have a CGImage; attempt to save it to file
> imageDest = fn CGImageDestinationCreateWithURL( url, type, 1, 0 )
> long if ( imageDest )
> // CGImage + CGImageDestination -> file
> CGImageDestinationAddImage( imageDest, image, 0 )
> success = fn CGImageDestinationFinalize( imageDest )
> CGImageRelease( image )
> end if
> end if
> end fn = success
> 
> 
> // main program
> dim as CFURLRef      url
> dim as CGContextRef  ctx
> dim as CFStringRef   type
> dim as Boolean       savedImageOK
> 
> long if ( files$( _CFURLRefSave,, "untitled.png", url ) )
> ctx = fn MyCreateCGBitmapContext( 50, 100 )
> if ( ctx == 0 ) then stop "MyCreateCGBitmapContext failed"
> fn DrawSomething( ctx )
> // some Uniform Type Identifiers that control image file format
> type = fn CFSTR( "public.png" )
> //type = fn CFSTR( "public.jpeg" )
> //type = fn CFSTR( "public.tiff" )
> //type = fn CFSTR( "com.apple.pict" )
> savedImageOK = fn SaveImageFile( ctx, url, type )
> fn MyDisposeCGBitmapContext( ctx )
> CFRelease( url )
> if ( savedImageOK == _false ) then stop "SaveImageFile failed"
> end if
> '------------
> 
> --
> To unsubscribe, send ANY message to: futurebasic-unsubscribe@...