faster saveimage 8bit on OS X

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

faster saveimage 8bit on OS X

Post by J. Baker »

On my MacBook Air with SSD drive, 32bit and 4bit save within a second. But 8bit takes a few seconds. I haven't done any testing on XP or Linux to see if this is also true.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: faster saveimage 8bit on OS X

Post by wilbert »

What file format are you using ?
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: faster saveimage 8bit on OS X

Post by J. Baker »

wilbert wrote:What file format are you using ?
PNG format. I also began to wonder if it was due to the fact that converting a 32bit png to a 8bit png might be slower then 4bits because there is 256 colors instead of 16 to convert to? Could this be possible?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: faster saveimage 8bit on OS X

Post by wilbert »

I'm not sure but I imagine it can be a reason.
It probably has to do with finding the best color palette when decreasing the color depth.
Any reason why you are using 8 bit ?
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: faster saveimage 8bit on OS X

Post by J. Baker »

wilbert wrote:I'm not sure but I imagine it can be a reason.
It probably has to do with finding the best color palette when decreasing the color depth.
Any reason why you are using 8 bit ?
I use it in my Sprite Monkey app. Because if you are going to make a retro style game and use less colors, it might as well be in a 8bit or 4bit image format. Much smaller file size. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: faster saveimage 8bit on OS X

Post by wilbert »

I don't know how PureBasic handles things.
Maybe you could use api calls to speed things up.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: faster saveimage 8bit on OS X

Post by J. Baker »

wilbert wrote:I don't know how PureBasic handles things.
Maybe you could use api calls to speed things up.
Yeah, I thought about that or just creating my own procedure using plot and point. Which I already use for removing the alpha from 32bit to convert to 8 and 4bit. That way I could have a true RGB(255, 0, 255) background for those formats and not have to worry about the alpha channel blending into the RGB(255, 0, 255) background.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: faster saveimage 8bit on OS X

Post by wilbert »

You could try using QuickTime.
I experimented a bit with it but haven't gotten everything right. Maybe someone else can look at it.
What's great about QuickTime is that it supports a lot of image formats.

The example below creates a png file named 'test.png' on the desktop but it only works if the file doesn't exist yet.
I haven't figured out an easy way to create a file that overwrites an existing one.

Code: Select all

#kFSCatInfoNone = 0
#kOnSystemDisk = $8000
#kDesktopFolderType = 'desk'

#GraphicsExporterComponentType = 'grex'
#kQTFileTypePNG = 'PNGf'
#k4IndexedPixelFormat = 4
#k8IndexedPixelFormat = 8

ImportC ""
  FSFindFolder(vRefNum, folderType, createFolder, *foundRef)
  FSCreateFileUnicode(*parentRef, nameLength, name.p-unicode, whichInfo, *catalogInfo, *newRef, newSpec)
EndImport

ImportC "/System/Library/Frameworks/QuickTime.framework/QuickTime"
  OpenADefaultComponent(componentType, componentSubType, *ci)
  GraphicsExportSetInputCGImage(ci, imageRef)
  GraphicsExportSetDepth(ci, depth)
  GraphicsExportSetOutputFile(ci, *theFile)
  GraphicsExportDoExport(ci, *actualSizeWritten)
  CloseComponent(ci)
EndImport

CreateImage(0, 200, 200)
ImgRef = ImageID(0)

OpenADefaultComponent(#GraphicsExporterComponentType, #kQTFileTypePNG, @ci); export to png
GraphicsExportSetInputCGImage(ci, ImgRef); set input image
GraphicsExportSetDepth(ci, #k8IndexedPixelFormat); 8 bit

FSFindFolder(#kOnSystemDisk, #kDesktopFolderType, 0, @parentDir); find desktop folder
FSCreateFileUnicode(@parentDir, 8, "test.png", #kFSCatInfoNone, #Null, #Null, @fileSpec); create 'test.png' file on desktop

GraphicsExportSetOutputFile(ci, @fileSpec); set the file to export the image to
GraphicsExportDoExport(ci, #Null); export the image
CloseComponent(ci)
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: faster saveimage 8bit on OS X

Post by J. Baker »

WOW, thanks wilbert! With PB I could easily check if the file exist and if it does, delete before writing the new one. I'll check this out more after I get some sleep. Thanks again! ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: faster saveimage 8bit on OS X

Post by wilbert »

I hope it works for you.
GIF or TIFF export is also possible using the QuickTime functions.

Here's some information about the QuickTime functions for Import / Export.
http://developer.apple.com/library/mac/ ... rence.html

I found out how to delete the file if it already exists.
Here's a more complete example

Code: Select all

#kFSCatInfoNone = 0
#kOnSystemDisk = $8000
#kDesktopFolderType = 'desk'
#kTextEncodingUnknown = $ffff

#GraphicsExporterComponentType = 'grex'
#kQTFileTypePNG = 'PNGf'
#kQTFileTypeTIFF = 'TIFF'
#kQTFileTypeBMP = 'BMPf'
#kQTFileTypeJPEG = 'JPEG'
#kQTFileTypeTargaImage = 'TPIC'
#k4IndexedPixelFormat = 4
#k8IndexedPixelFormat = 8

ImportC ""
  FSFindFolder(vRefNum, folderType, createFolder, *foundRef)
  FSMakeFSRefUnicode(*parentRef, nameLength, name.p-unicode, textEncodingHint, *newRef)
  FSDeleteObject(*ref)
  FSCreateFileUnicode(*parentRef, nameLength, name.p-unicode, whichInfo, *catalogInfo, *newRef, newSpec)
EndImport

ImportC "/System/Library/Frameworks/QuickTime.framework/QuickTime"
  OpenADefaultComponent(componentType, componentSubType, *ci)
  GraphicsExportSetInputCGImage(ci, imageRef)
  GraphicsExportSetDepth(ci, depth)
  GraphicsExportSetOutputFile(ci, *theFile)
  GraphicsExportSetOutputHandle(ci, handle)
  GraphicsExportDoExport(ci, *actualSizeWritten)
  CloseComponent(ci)
EndImport

Procedure QTSaveImage(ImageID, FileName.s)
  
  Global parentDir, fileSpec; don't know why but function crashes when there are not global
  
  OpenADefaultComponent(#GraphicsExporterComponentType, #kQTFileTypePNG, @ci); export to png
  GraphicsExportSetInputCGImage(ci, ImageID); set input image
  GraphicsExportSetDepth(ci, #k8IndexedPixelFormat); 8 bit
  
  FSFindFolder(#kOnSystemDisk, #kDesktopFolderType, 0, @parentDir); find desktop folder
  FSMakeFSRefUnicode(@parentDir, Len(Filename), Filename, #kTextEncodingUnknown, @fileSpec); find existing file
  If fileSpec : FSDeleteObject(@fileSpec) : EndIf; delete file if it exists
  
  FSFindFolder(#kOnSystemDisk, #kDesktopFolderType, 0, @parentDir); find desktop folder
  FSCreateFileUnicode(@parentDir, Len(FileName), Filename, #kFSCatInfoNone, #Null, #Null, @fileSpec); create new 'test.png' file on desktop
  
  GraphicsExportSetOutputFile(ci, @fileSpec); set the file to export the image to
  GraphicsExportDoExport(ci, #Null); export the image
  CloseComponent(ci)
  
EndProcedure

If OpenWindow(0, 0, 0, 200, 200, "QT 2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If CreateImage(0, 200, 200) And StartDrawing(ImageOutput(0))
    y = 0
    For x = 0 To 95 Step 10
      Box(x, y, 200-2*x, 200-2*y, RGB(Random(255), Random(255), Random(255)))
      y + 10
    Next x
    StopDrawing() 
    ImageGadget(0, 0, 0, 200, 200, ImageID(0))
  EndIf
  
  QTSaveImage(ImageID(0), "test.png"); save test.png to desktop  
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
Post Reply