Programmer Reference : Common Graphics : Image and icon file formats : Unloading images and icons into ByteArrays
Unloading images and icons into ByteArrays
In addition to using files, Common Graphics allows images and icons to be loaded and unloaded using ByteArrays. The totalSizeBeforeUnload: method determines the number of bytes required to unload the image. The unload:intoByteObjects:offsetsIntoByteObjects: method, which does the actual unloading, takes an array of ByteArrays and an array of Integer offsets. The offsets specify how many bytes to skip at the beginning of each ByteArray. The offsets can be used, for example, to leave room for database record headers. As with unload:intoFile:, true is answered if the unload was successful, false if an error occurred.
The following code illustrates how to unload an image into an array of ByteArrays using the PCX format. Each ByteArray is limited to a maximum size of 64K. To illustrate how space is reserved for a database record header, 32 bytes are skipped at the beginning of each ByteArray.
| image format headerSize maxSize imageSize numByteArrays byteArrays offsets |
image := self imageToUnload.
format := CgPCXFileFormat new.
headerSize := 32.
maxSize := 65536. "64K"
imageSize := format totalSizeBeforeUnload: image.
"Determine total size."
numByteArrays := imageSize // (maxSize - headerSize) + 1.
byteArrays := OrderedCollection new.
1 to: numByteArrays do: [:i|
byteArrays add: (ByteArray new: maxSize)].
byteArrays := byteArrays asArray.
offsets := Array new: byteArrays size.
offsets atAllPut: headerSize.
(format
unload: image
intoByteObjects: byteArrays
offsetsIntoByteObjects: offsets)
ifFalse: [self error: format currentErrorString]
Icons are unloaded into ByteArrays in a similar fashion. The only difference is that the totalSizeBeforeUnload: and unload:intoByteObjects:offsetsIntoByteObjects: methods for the icon file formats take an array of CgIcons rather than an image.
Last modified date: 01/29/2015