[futurebasic] Re: [FB] Saving various image formats

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2011 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Fri, 22 Apr 2011 04:57:38 -0400
And here's a quick and dirty way to save between file formats. Note 
that if you don't pass a dictionary of meta data and compression 
info, the converted file will be 72 DPI by default as in this example.

Ken


include "CoreGraphics:CGImageSource.incl"
include "Tlbx UTCoreTypes.incl"

// Declared in CGImageDestination.h

#if ndef _DEFINEDINCARBON
#define CGImageDestinationRef as pointer // to struct CGImageDestination
#endif

// system CFStringRef kCGImageDestinationLossyCompressionQuality

toolbox fn CGImageDestinationCreateWithURL ( CFURLRef url, 
CFStringRef type, Long, CFDictionaryRef options ) = 
CGImageDestinationRef
toolbox CGImageDestinationAddImageFromSource ( CGImageDestinationRef 
idst, CGImageSourceRef imageSource, Long, CFDictionaryRef properties )
toolbox fn CGImageDestinationFinalize ( CGImageDestinationRef idst ) = Boolean


begin globals
dim as Str255  gOriginalFileName
end globals


local fn ConvertImageType( imageSourceRef as CGImageSourceRef, 
outputkUTTypeRef as CFStringRef, destinationURL as CFURLRef ) as 
Boolean
'~'1
dim as CGImageDestinationRef   destinationRef
dim as Boolean  result : result = 1

// Create a file location and type for the converted image
destinationRef = fn CGImageDestinationCreateWithURL( destinationURL, 
outputkUTTypeRef, 1, #0 )
long if ( destinationRef )
/*
Add converted image to its destination.
Since we have not provided a dictionary of meta data
from the original image the converted image will be 72 DPI)
*/
CGImageDestinationAddImageFromSource( destinationRef, imageSourceRef, 0, #0 )
// Finalize creation of new image file
result = fn CGImageDestinationFinalize( destinationRef )
CFRelease( destinationRef )
end if
end fn = result


local fn SelectImageDestination( imageSourceRef as CGImageSourceRef, 
newFormatType as CFStringRef ) as CFURLRef
'~'1
dim as CFURLRef            destinationURL : destinationURL = 0
dim as Str255              s, extension : extension = ""
dim as Boolean             result

// Determine new image format type...
result = fn CFStringGetPascalString( newFormatType, @s, 256, 
_kCFStringEncodingASCII )
if s = "com.microsoft.bmp"  then extension = ".bmp"
if s = "com.compuserve.gif" then extension = ".gif"
if s = "public.tiff"        then extension = ".tiff"
if s = "public.jpeg"        then extension = ".jpg"
if s = "com.apple.pict"     then extension = ".pict"
if s = "public.png"         then extension = ".png"
if s = "public.tiff"        then extension = ".tiff"

// ...and add proper extension to new file name
s = gOriginalFileName
s = mid$( s, 1, instr( 1, s, "." ) -1 )
s = s + extension
// Select destination for converted image file
long if ( files$( _CFURLRefSave, "Select destination for converted 
image", s, destinationURL ) )
xelse
stop "User canceled
end if
end fn = destinationURL


local fn SelectImageSource as CGImageSourceRef
'~'1
dim as CGImageSourceRef    imageSourceRef : imageSourceRef = 0
dim as CFURLRef            url

// Open image file
gOriginalFileName = files$( _CFURLRefOpen, , "Select an image to 
convert", url )
long if ( gOriginalFileName[0] )
long if ( url )
// Create CGImageSourceRef from original image
imageSourceRef = fn CGImageSourceCreateWithURL( url, #0 )
CFRelease( url )
end if
xelse
stop "User canceled"
end if
end fn = imageSourceRef     // User must release


local fn ConvertAnImage( newFormatType as CFStringRef )
'~'1
dim as CGImageSourceRef  imageSourceRef
dim as CFURLRef          destinationURL
dim as Boolean           result

// Create CGImageSourceRef from original image
imageSourceRef = fn SelectImageSource
// Select location and format type for converted image file
destinationURL = fn SelectImageDestination( imageSourceRef, newFormatType )
// Convert file to new format and save
result = fn ConvertImageType( imageSourceRef, newFormatType, destinationURL )
// Empty garbage
CFRelease( imageSourceRef )
CFRelease( destinationURL )
long if (result)
stop "Image was successfully converted"
xelse
stop "Conversion failed"
end if
end fn

/*
Available output format types include:

    kUTTypeBMP
    kUTTypeGIF
    kUTTypeJPEG
    kUTTypePICT
    kUTTypePNG
    kUTTypeTIFF

We will convert the input image file to a PNG
*/
fn ConvertAnImage( kUTTypePNG )

do
HandleEvents
until gFBQuit