The CK_Compression class is a mixin class. It defines an interface for compressing and decompressing buffers in memory. CK_Image objects can use CK_Compression objects for serializing and deserializing image data. The CK_Image::setCompression() method is used to register a compression object to be used in serialization.
The library provides one example of an implementation of CK_Compression. The CK_RLE class provides a simple run-length encoded compression algorithm. It is useful for compressing images with transparency since there are typically many 'runs' of pixels in the transparent color.
The library provides this compression architecture for a number of reasons. One reason is to allow developers to incorporate their own proprietary compression algorithms if desired. Another reason is because some popular compression algorithms, such as LZW, are patented and must be licensed if used. The library does not provide implementations for these compression algorithms. For more information, see Compression.
Serialize Example
CK_StaticImage *image = CK_StaticImage::createFromFile("gliderTrans.bmp");
// Create a compression object and plug it into the image. Now the image
// will use compression when it is serialized.
CK_RLE *compression = new CK_RLE;
image->setCompression(compression);
FILE *fp = fopen("gliderTrans.cim","wb");
image->serialize(fp);
fclose(fp);
Deserialize Example
CK_RLE *compression = new CK_RLE;
FILE *fp = fopen("gliderTrans.cim","rb");
CK_StaticImage *image = CK_StaticImage::create(fp,compression);
fclose(fp);
Compresses a memory buffer and returns a new buffer containing the compressed data.
Return Value
- Returns a new buffer holding the compressed data. The size of the new buffer is returned in compressedNumBytes. The returned buffer was allocated with new char[] and therefore should be deleted using delete [].
Parameters
- inputBuffer
- The memory buffer to be compressed.
- inputNumBytes
- The number of bytes in the inputBuffer.
- compressedNumBytes
- The size of the allocated compressed buffer returned.
Decompresses a memory buffer. An output buffer that is large enough to hold the decompressed data must be provided.
Return Value
- Returns 0 if successful. Returns -1 if the outputBuffer is not large enough. The number of decompressed bytes in the outputBuffer is stored in outputNumBytes.
Parameters
- inputBuffer
- The memory buffer to be decompressed.
- inputNumBytes
- The number of bytes in the inputBuffer.
- outputBuffer
- The memory buffer to hold the decompressed data.
- outputNumBytes
- The number of bytes in the outputBuffer is passed in. The number of bytes actually used is stored in this parameter.
Returns a pointer to an instance of a compression object that is of the class represented by the ID.
Return Value
- Returns a pointer to a compression object, or 0 if no compression objects have been created that match the ID.
Parameters
- id
- Looks for a compression object that was created with this ID.
Returns the compression ID for the class. As an example, CK_RLE compression object will return a compression ID of 1.