PackBitsYou can use the
PackBitsprocedure to compress a data buffer stored in RAM.
PROCEDURE PackBits (VAR srcPtr, dstPtr: Ptr; srcBytes: Integer);
- On entry, a pointer to the first byte of a buffer of data to be compressed. On exit, a pointer to the first byte following the bytes compressed.
- On entry, a pointer to the first byte in which to store compressed data. On exit, a pointer to the first byte following the compressed data.
- The number of bytes of uncompressed data to be compressed. In versions of software prior to version 6.0.2, this number must be 127 or less.
srcBytesbytes of data beginning at the location specified by the
srcPtrparameter and stores it at the location specified by the
dstPtrparameter. It then modifies the
dstPtrvariables to point to the first bytes after the uncompressed and compressed data, respectively.
Your application must allocate memory for the destination buffer itself. In general, you should allocate enough memory for a worst-case scenario. In the worst case, the destination buffer is 128 bytes long for each block of source data up to 127 bytes. Thus, you can use the following formula to determine how much space to allocate for the destination buffer:
maxDstBytes := srcBytes + (srcBytes+126) DIV 127;where
maxDstBytesstands for the maximum number of destination bytes.
PackBitsalgorithm is most effective on data buffers in which there are likely to be series of bytes containing the same value. For example, resources of many formats often contain many consecutive zeros. If you have a data buffer in which there are only likely to be series of words or long words containing the same value,
PackBitsis unlikely to be effective.
Because your application must allocate memory for the source and destination buffers,
PackBitsdoes not move relocatable blocks. Thus, you can call it at interrupt time.
PackBitschanges the values of the
dstPtrparameters, you should pass to
PackBitsonly copies of pointers to the source and destination buffers. This allows you to access the beginning of the source and destination buffers after
PackBitsreturns. Also, if the source or destination buffer is stored in an unlocked, relocatable block, this technique prevents
PackBitsfrom changing the value of a master pointer, which would make the original handle invalid.
SEE ALSOFor an example of the use of the
PackBitsprocedure, see Listing 3-3 on page 3-20.