-- Image export behavior -- Written for Tri-Media Productions ©2002 -- Neil Madsen -- Freely distributable as is -- Using Sharp Software's Image export xtra (www.sharp-software.com) -- Also requires BuddyAPI (www.mods.com.au/budapi) -- Drop this behavior onto a button that starts the image export -- Possible error codes for the Export Xtra -- 0 success -- 1 destination file can not be written -- 2 out of memory -- 3 wrong number of args -- 4 bad parameter -- 5 castmember not found -- 6 castmember media not found -- 7 castmember is not a bitmap -- 8 unsupported bitdepth (JPEG support is 8 bits or higher) -- 9 JPEG compression failed (internal error with JPEG library) -- 10 PNG compression failed (internal error with PNG library) -- Property Declarations property pMySprite -- sprite this button behavior is attached to property pImgMem -- member that gets used for the image export property pExpPath -- path to image export location property pExpComp -- Compression to be used if the image format is JPG property pExpFormat -- Format of image export (PNG,JPG,BMP[win only],PCT[mac only]) property pErrCodes -- list of errors and messages for the Export Xtra -------------------------------- -------------------------------- on beginSprite me pMySprite = sprite(me.spriteNum) pErrCodes = ["destination file can not be written",\ "out of memory","wrong number of args","bad parameter",\ "castmember not found","castmember media not found",\ "castmember is not a bitmap","unsupported bitdepth (JPEG support is 8 bits or higher)",\ "JPEG compression failed (internal error with JPEG library)",\ "PNG compression failed (internal error with PNG library)"] -- Make sure the author hasn't selected the worng image format for that platform. if the environment.platform contains "win" AND pExpFormat = ".PCT" then alert ".PCT files can only be exported on a Macintosh." & RETURN & "Please make the appropriate changes." & RETURN & RETURN & \ "The sprite to be fixed is " & pMySprite & "." halt end if if the environment.platform contains "mac" AND pExpFormat = ".BMP" then alert ".BMP files can only be exported on a PC." & RETURN & "Please make the appropriate changes." & RETURN & RETURN & \ "The sprite to be fixed is " & pMySprite & "." halt end if end beginSprite ---------------- on mouseUp me -- open a file dialog for the user to select where to save their images pExpPath = baGetFilename( "Save", "", "My Image", "Image files|*." & pExpFormat, 2621440, "Save image as...", false, 100, 100 ) -- As an alternative to buddyAPI, you can use the fileIO xtra to put up a dialog -- myFile = new (xtra "fileIO") -- pExpPath = displaySave( myFile, "Save image as...", "MyImage" & pExpFormat) -- see if the user pressed cancel if pExpPath = "" then exit end if -- Make sure there is an extension for the filename ext = pExpPath.char[pExpPath.length-3..pExpPath.length] if ext <> pExpFormat then -- add the file extension pExpPath = pExpPath & pExpFormat end if -- Begin image export -- instantiate the export xtra expImg = new (xtra "SharpExport") -- parameter 1 - bitmap member reference -- parameter 2 - pathname to save to -- parameter 3 - jpeg compression level (higher is better quality) case pExpFormat of ".JPG": errCode = expImg.exportJPG(pImgMem, pExpPath, pExpComp) ".PNG": errCode = expImg.exportPNG(pImgMem, pExpPath) ".BMP": -- win only if the environment.platform contains "win" then errCode = expImg.exportBMP(pImgMem, pExpPath) else alert ".BMP files can only be exported on a PC" exit end if ".PCT": -- mac only if the environment.platform contains "mac" then errCode = expImg.exportPICT(pImgMem, pExpPath) else alert ".PCT files can only be exported on a Macintosh" exit end if end case -- make sure no errors occurred while exporting if errCode <> 0 then -- an error occurred -- display error and message to user me.showError(errCode) end if -- void the export xtra instance expImg = VOID end mouseUp ---------------- on showError me, errCode if the runMode <> "Author" then alert "An error has occurred while exporting the image." & RETURN & \ "The error code is: " & errCode & RETURN & RETURN & "Image export failed." else alert "An error has occurred while exporting the image." & RETURN & RETURN & \ "The error code is: " & errCode & RETURN & pErrCodes[abs(errCode)] & RETURN & RETURN & "Image export failed." end if end showError ---------------- on getPropertyDescriptionList me GPDL = [:] GPDL[#pImgMem] = [#comment: "Cast member to use for image export:", #format:#bitmap, #default: ""] GPDL[#pExpFormat] = [#comment: "Format of exported images:", #format:#string, #default:"", #range:[".JPG",".PNG",".BMP",".PCT"]] GPDL[#pExpComp] = [#comment: "Compression to use for JPG:", #format:#integer, #default:80, #range:[#min:0, #max:100]] return GPDL end getPropertyDescriptionList