Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/LibJPEG/wrgif.c view on Meta::CPAN
/*
* Routines to package finished data bytes into GIF data blocks.
* A data block consists of a count byte (1..255) and that many data bytes.
*/
LOCAL(void)
flush_packet (gif_dest_ptr dinfo)
/* flush any accumulated data */
{
if (dinfo->bytesinpkt > 0) { /* never write zero-length packet */
dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++;
if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt)
!= (size_t) dinfo->bytesinpkt)
ERREXIT(dinfo->cinfo, JERR_FILE_WRITE);
dinfo->bytesinpkt = 0;
}
}
/* Add a character to current packet; flush to disk if necessary */
#define CHAR_OUT(dinfo,c) \
{ (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c); \
if ((dinfo)->bytesinpkt >= 255) \
flush_packet(dinfo); \
}
/* Routine to convert variable-width codes into a byte stream */
LOCAL(void)
output (gif_dest_ptr dinfo, int code)
/* Emit a code of n_bits bits */
/* Uses cur_accum and cur_bits to reblock into 8-bit bytes */
{
dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits;
dinfo->cur_bits += dinfo->n_bits;
while (dinfo->cur_bits >= 8) {
CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
dinfo->cur_accum >>= 8;
dinfo->cur_bits -= 8;
}
}
/* The pseudo-compression algorithm.
*
* In this module we simply output each pixel value as a separate symbol;
* thus, no compression occurs. In fact, there is expansion of one bit per
* pixel, because we use a symbol width one bit wider than the pixel width.
*
* GIF ordinarily uses variable-width symbols, and the decoder will expect
* to ratchet up the symbol width after a fixed number of symbols.
* To simplify the logic and keep the expansion penalty down, we emit a
* GIF Clear code to reset the decoder just before the width would ratchet up.
* Thus, all the symbols in the output file will have the same bit width.
* Note that emitting the Clear codes at the right times is a mere matter of
* counting output symbols and is in no way dependent on the LZW patent.
*
* With a small basic pixel width (low color count), Clear codes will be
* needed very frequently, causing the file to expand even more. So this
* simplistic approach wouldn't work too well on bilevel images, for example.
* But for output of JPEG conversions the pixel width will usually be 8 bits
* (129 to 256 colors), so the overhead added by Clear symbols is only about
* one symbol in every 256.
*/
LOCAL(void)
compress_init (gif_dest_ptr dinfo, int i_bits)
/* Initialize pseudo-compressor */
{
/* init all the state variables */
dinfo->n_bits = i_bits;
dinfo->maxcode = MAXCODE(dinfo->n_bits);
dinfo->ClearCode = (1 << (i_bits - 1));
dinfo->EOFCode = dinfo->ClearCode + 1;
dinfo->code_counter = dinfo->ClearCode + 2;
/* init output buffering vars */
dinfo->bytesinpkt = 0;
dinfo->cur_accum = 0;
dinfo->cur_bits = 0;
/* GIF specifies an initial Clear code */
output(dinfo, dinfo->ClearCode);
}
LOCAL(void)
compress_pixel (gif_dest_ptr dinfo, int c)
/* Accept and "compress" one pixel value.
* The given value must be less than n_bits wide.
*/
{
/* Output the given pixel value as a symbol. */
output(dinfo, c);
/* Issue Clear codes often enough to keep the reader from ratcheting up
* its symbol size.
*/
if (dinfo->code_counter < dinfo->maxcode) {
dinfo->code_counter++;
} else {
output(dinfo, dinfo->ClearCode);
dinfo->code_counter = dinfo->ClearCode + 2; /* reset the counter */
}
}
LOCAL(void)
compress_term (gif_dest_ptr dinfo)
/* Clean up at end */
{
/* Send an EOF code */
output(dinfo, dinfo->EOFCode);
/* Flush the bit-packing buffer */
if (dinfo->cur_bits > 0) {
CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
}
/* Flush the packet buffer */
flush_packet(dinfo);
}
( run in 0.698 second using v1.01-cache-2.11-cpan-5623c5533a1 )