I need to read a JPEG file and output a HEIC image. How can this be done? I have MBS plugins, and can’t figure it out. Please help!
OS ?
Mojave and probably latest Windows 10 (+ Android and iPhone) needed
Mojave.
Are you processing the image, or simply translating the format?
Converting format only
You can use CGImageDestinationAddImageFromSource
to translate images.
I use the following function to translate image formats, it’s written against my big ass library, but most of the functions should exist in the MBS.
- You need to change
outFile.UTI
to"public.heic"
-
Dim token as OWSecurityScopedBodgemarks.SSBToken = inSourceFile.startAccessingSecurityScopedFolderItem( currentMethodName )
just makes sure that I have Sandbox access during the translation. -
CGImageSourceLargestImage( cgis )
loops through the CGImageSource meta data looking for the largest image. - This doesn’t allow you to set compression parameters, that can be done by manually editing the metadata NSDictionary.
[code]Function translateImageFile(inSourceFile as folderitem, outFile as folderItem, byref errorMessage as string) as boolean
// — Image file translation, October 2018 Sam Rowlands.
Dim token as OWSecurityScopedBodgemarks.SSBToken = inSourceFile.startAccessingSecurityScopedFolderItem( currentMethodName )
Dim rValue as boolean
Dim cgis as integer = CGImageSourceCreateWithURL( inSourceFile.NSURL, 0 )
if cgis = 0 then
errorMessage = “Failed to open the image file “”” + inSourceFile.displayName + “”" (nilCGIS)."
return false
end if
Dim index as integer = CGImageSourceLargestImage( cgis )
if index = -1 then
errorMessage = “Failed to open the image file “”” + inSourceFile.displayName + “”" (nilIDX)."
CGImageSourceRelease cgis
return false
end if
Dim metaData as integer = CGImageSourceCopyPropertiesAtIndex( cgis, index, 0 )
Dim cgid as integer = CGImageDestinationCreateWithURL( outFile.NSURL, outFile.UTI, 1, 0 )
if cgid = 0 then
errorMessage = “Failed to create the image file “”” + outFile.displayName + “”" (nilCGID)."
else
CGImageDestinationAddImageFromSource( CGID, CGIS, index, metaData )
rvalue = CGImageDestinationFinalize( cgid )
if rvalue = false then errorMessage = "Failed to create the image file """ + outFile.displayName + """ (CGIDFinalize:NO)."
end if
NSObjectRelease metaData
CGImageDestinationRelease cgid
CGImageSourceRelease cgis
return rvalue
End Function[/code]
Got it working
[code]Public Function translateImageFile(inSourceFile as folderitem, outFile as folderItem, byref errorMessage as string) as Boolean
// — Image file translation, October 2018 Sam Rowlands.
Call CFBookmarkMBS.StartAccessingSecurityScopedResource(New CFURLMBS(inSourceFile))
Dim xImageSource As New CGImageSourceMBS(inSourceFile)
If xImageSource = Nil Then
errorMessage = “Failed to open the image file “”” + inSourceFile.displayName + “”" (nilCGIS)."
Return False
End If
Dim index As Integer = CGImageSourceLargestImage( xImageSource )
If index = -1 Then
errorMessage = “Failed to open the image file “”” + inSourceFile.displayName + “”" (nilIDX)."
Return False
End If
Dim metaData As dictionary = xImageSource.PropertiesAtIndex(index)
Dim xDestination As CGImageDestinationMBS = CGImageDestinationMBS.CreateWithFile( outFile, “public.heic”, 1)
If xDestination = Nil Then
errorMessage = “Failed to create the image file “”” + outFile.displayName + “”" (nilCGID)."
Return False
Else
xDestination.AddImageFromSource(xImageSource,index,metaData)
If Not xDestination.Finalize Then
errorMessage = “Failed to create the image file “”” + outFile.displayName + “”" (CGIDFinalize:NO)."
Return False
End If
End If
Return True
End Function
[/code]