Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * wrgif.c |
| 3 | * |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1997, Thomas G. Lane. |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 6 | * It was modified by The libjpeg-turbo Project to include only code relevant |
| 7 | * to libjpeg-turbo. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 8 | * For conditions of distribution and use, see the accompanying README file. |
| 9 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 10 | * This file contains routines to write output images in GIF format. |
| 11 | * |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 12 | ************************************************************************** |
| 13 | * NOTE: to avoid entanglements with Unisys' patent on LZW compression, * |
| 14 | * this code has been modified to output "uncompressed GIF" files. * |
| 15 | * There is no trace of the LZW algorithm in this file. * |
| 16 | ************************************************************************** |
| 17 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 18 | * These routines may need modification for non-Unix environments or |
| 19 | * specialized applications. As they stand, they assume output to |
| 20 | * an ordinary stdio stream. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * This code is loosely based on ppmtogif from the PBMPLUS distribution |
| 25 | * of Feb. 1991. That file contains the following copyright notice: |
| 26 | * Based on GIFENCODE by David Rowley <mgardi@watdscu.waterloo.edu>. |
| 27 | * Lempel-Ziv compression based on "compress" by Spencer W. Thomas et al. |
| 28 | * Copyright (C) 1989 by Jef Poskanzer. |
| 29 | * Permission to use, copy, modify, and distribute this software and its |
| 30 | * documentation for any purpose and without fee is hereby granted, provided |
| 31 | * that the above copyright notice appear in all copies and that both that |
| 32 | * copyright notice and this permission notice appear in supporting |
| 33 | * documentation. This software is provided "as is" without express or |
| 34 | * implied warranty. |
| 35 | * |
| 36 | * We are also required to state that |
| 37 | * "The Graphics Interchange Format(c) is the Copyright property of |
| 38 | * CompuServe Incorporated. GIF(sm) is a Service Mark property of |
| 39 | * CompuServe Incorporated." |
| 40 | */ |
| 41 | |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 42 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 43 | |
| 44 | #ifdef GIF_SUPPORTED |
| 45 | |
| 46 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 47 | /* Private version of data destination object */ |
| 48 | |
| 49 | typedef struct { |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 50 | struct djpeg_dest_struct pub; /* public fields */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 51 | |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 52 | j_decompress_ptr cinfo; /* back link saves passing separate parm */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 53 | |
| 54 | /* State for packing variable-width codes into a bitstream */ |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 55 | int n_bits; /* current number of bits/code */ |
| 56 | int maxcode; /* maximum code, given n_bits */ |
| 57 | INT32 cur_accum; /* holds bits not yet output */ |
| 58 | int cur_bits; /* # of bits in cur_accum */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 59 | |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 60 | /* State for GIF code assignment */ |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 61 | int ClearCode; /* clear code (doesn't change) */ |
| 62 | int EOFCode; /* EOF code (ditto) */ |
| 63 | int code_counter; /* counts output symbols */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 64 | |
| 65 | /* GIF data packet construction buffer */ |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 66 | int bytesinpkt; /* # of bytes in current packet */ |
| 67 | char packetbuf[256]; /* workspace for accumulating packet */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 68 | |
| 69 | } gif_dest_struct; |
| 70 | |
| 71 | typedef gif_dest_struct * gif_dest_ptr; |
| 72 | |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 73 | /* Largest value that will fit in N bits */ |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 74 | #define MAXCODE(n_bits) ((1 << (n_bits)) - 1) |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 75 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 76 | |
| 77 | /* |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 78 | * Routines to package finished data bytes into GIF data blocks. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 79 | * A data block consists of a count byte (1..255) and that many data bytes. |
| 80 | */ |
| 81 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 82 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 83 | flush_packet (gif_dest_ptr dinfo) |
| 84 | /* flush any accumulated data */ |
| 85 | { |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 86 | if (dinfo->bytesinpkt > 0) { /* never write zero-length packet */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 87 | dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++; |
| 88 | if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt) |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 89 | != (size_t) dinfo->bytesinpkt) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 90 | ERREXIT(dinfo->cinfo, JERR_FILE_WRITE); |
| 91 | dinfo->bytesinpkt = 0; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* Add a character to current packet; flush to disk if necessary */ |
| 97 | #define CHAR_OUT(dinfo,c) \ |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 98 | { (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c); \ |
| 99 | if ((dinfo)->bytesinpkt >= 255) \ |
| 100 | flush_packet(dinfo); \ |
| 101 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | /* Routine to convert variable-width codes into a byte stream */ |
| 105 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 106 | LOCAL(void) |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 107 | output (gif_dest_ptr dinfo, int code) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 108 | /* Emit a code of n_bits bits */ |
| 109 | /* Uses cur_accum and cur_bits to reblock into 8-bit bytes */ |
| 110 | { |
| 111 | dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits; |
| 112 | dinfo->cur_bits += dinfo->n_bits; |
| 113 | |
| 114 | while (dinfo->cur_bits >= 8) { |
| 115 | CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF); |
| 116 | dinfo->cur_accum >>= 8; |
| 117 | dinfo->cur_bits -= 8; |
| 118 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 122 | /* The pseudo-compression algorithm. |
| 123 | * |
| 124 | * In this module we simply output each pixel value as a separate symbol; |
| 125 | * thus, no compression occurs. In fact, there is expansion of one bit per |
| 126 | * pixel, because we use a symbol width one bit wider than the pixel width. |
| 127 | * |
| 128 | * GIF ordinarily uses variable-width symbols, and the decoder will expect |
| 129 | * to ratchet up the symbol width after a fixed number of symbols. |
| 130 | * To simplify the logic and keep the expansion penalty down, we emit a |
| 131 | * GIF Clear code to reset the decoder just before the width would ratchet up. |
| 132 | * Thus, all the symbols in the output file will have the same bit width. |
| 133 | * Note that emitting the Clear codes at the right times is a mere matter of |
| 134 | * counting output symbols and is in no way dependent on the LZW patent. |
| 135 | * |
| 136 | * With a small basic pixel width (low color count), Clear codes will be |
| 137 | * needed very frequently, causing the file to expand even more. So this |
| 138 | * simplistic approach wouldn't work too well on bilevel images, for example. |
| 139 | * But for output of JPEG conversions the pixel width will usually be 8 bits |
| 140 | * (129 to 256 colors), so the overhead added by Clear symbols is only about |
| 141 | * one symbol in every 256. |
| 142 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 143 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 144 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 145 | compress_init (gif_dest_ptr dinfo, int i_bits) |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 146 | /* Initialize pseudo-compressor */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 147 | { |
| 148 | /* init all the state variables */ |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 149 | dinfo->n_bits = i_bits; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 150 | dinfo->maxcode = MAXCODE(dinfo->n_bits); |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 151 | dinfo->ClearCode = (1 << (i_bits - 1)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 152 | dinfo->EOFCode = dinfo->ClearCode + 1; |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 153 | dinfo->code_counter = dinfo->ClearCode + 2; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 154 | /* init output buffering vars */ |
| 155 | dinfo->bytesinpkt = 0; |
| 156 | dinfo->cur_accum = 0; |
| 157 | dinfo->cur_bits = 0; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 158 | /* GIF specifies an initial Clear code */ |
| 159 | output(dinfo, dinfo->ClearCode); |
| 160 | } |
| 161 | |
| 162 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 163 | LOCAL(void) |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 164 | compress_pixel (gif_dest_ptr dinfo, int c) |
| 165 | /* Accept and "compress" one pixel value. |
| 166 | * The given value must be less than n_bits wide. |
| 167 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 168 | { |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 169 | /* Output the given pixel value as a symbol. */ |
| 170 | output(dinfo, c); |
| 171 | /* Issue Clear codes often enough to keep the reader from ratcheting up |
| 172 | * its symbol size. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 173 | */ |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 174 | if (dinfo->code_counter < dinfo->maxcode) { |
| 175 | dinfo->code_counter++; |
| 176 | } else { |
| 177 | output(dinfo, dinfo->ClearCode); |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 178 | dinfo->code_counter = dinfo->ClearCode + 2; /* reset the counter */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 179 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 183 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 184 | compress_term (gif_dest_ptr dinfo) |
| 185 | /* Clean up at end */ |
| 186 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 187 | /* Send an EOF code */ |
| 188 | output(dinfo, dinfo->EOFCode); |
| 189 | /* Flush the bit-packing buffer */ |
| 190 | if (dinfo->cur_bits > 0) { |
| 191 | CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF); |
| 192 | } |
| 193 | /* Flush the packet buffer */ |
| 194 | flush_packet(dinfo); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /* GIF header construction */ |
| 199 | |
| 200 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 201 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 202 | put_word (gif_dest_ptr dinfo, unsigned int w) |
| 203 | /* Emit a 16-bit word, LSB first */ |
| 204 | { |
| 205 | putc(w & 0xFF, dinfo->pub.output_file); |
| 206 | putc((w >> 8) & 0xFF, dinfo->pub.output_file); |
| 207 | } |
| 208 | |
| 209 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 210 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 211 | put_3bytes (gif_dest_ptr dinfo, int val) |
| 212 | /* Emit 3 copies of same byte value --- handy subr for colormap construction */ |
| 213 | { |
| 214 | putc(val, dinfo->pub.output_file); |
| 215 | putc(val, dinfo->pub.output_file); |
| 216 | putc(val, dinfo->pub.output_file); |
| 217 | } |
| 218 | |
| 219 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 220 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 221 | emit_header (gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap) |
| 222 | /* Output the GIF file header, including color map */ |
DRC | 90d6c38 | 2014-05-12 09:08:39 +0000 | [diff] [blame] | 223 | /* If colormap==NULL, synthesize a grayscale colormap */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 224 | { |
| 225 | int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte; |
| 226 | int cshift = dinfo->cinfo->data_precision - 8; |
| 227 | int i; |
| 228 | |
| 229 | if (num_colors > 256) |
| 230 | ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors); |
| 231 | /* Compute bits/pixel and related values */ |
| 232 | BitsPerPixel = 1; |
| 233 | while (num_colors > (1 << BitsPerPixel)) |
| 234 | BitsPerPixel++; |
| 235 | ColorMapSize = 1 << BitsPerPixel; |
| 236 | if (BitsPerPixel <= 1) |
| 237 | InitCodeSize = 2; |
| 238 | else |
| 239 | InitCodeSize = BitsPerPixel; |
| 240 | /* |
| 241 | * Write the GIF header. |
| 242 | * Note that we generate a plain GIF87 header for maximum compatibility. |
| 243 | */ |
| 244 | putc('G', dinfo->pub.output_file); |
| 245 | putc('I', dinfo->pub.output_file); |
| 246 | putc('F', dinfo->pub.output_file); |
| 247 | putc('8', dinfo->pub.output_file); |
| 248 | putc('7', dinfo->pub.output_file); |
| 249 | putc('a', dinfo->pub.output_file); |
| 250 | /* Write the Logical Screen Descriptor */ |
| 251 | put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); |
| 252 | put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 253 | FlagByte = 0x80; /* Yes, there is a global color table */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 254 | FlagByte |= (BitsPerPixel-1) << 4; /* color resolution */ |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 255 | FlagByte |= (BitsPerPixel-1); /* size of global color table */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 256 | putc(FlagByte, dinfo->pub.output_file); |
| 257 | putc(0, dinfo->pub.output_file); /* Background color index */ |
| 258 | putc(0, dinfo->pub.output_file); /* Reserved (aspect ratio in GIF89) */ |
| 259 | /* Write the Global Color Map */ |
| 260 | /* If the color map is more than 8 bits precision, */ |
| 261 | /* we reduce it to 8 bits by shifting */ |
| 262 | for (i=0; i < ColorMapSize; i++) { |
| 263 | if (i < num_colors) { |
| 264 | if (colormap != NULL) { |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 265 | if (dinfo->cinfo->out_color_space == JCS_RGB) { |
| 266 | /* Normal case: RGB color map */ |
| 267 | putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file); |
| 268 | putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file); |
| 269 | putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file); |
| 270 | } else { |
| 271 | /* Grayscale "color map": possible if quantizing grayscale image */ |
| 272 | put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift); |
| 273 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 274 | } else { |
DRC | 90d6c38 | 2014-05-12 09:08:39 +0000 | [diff] [blame] | 275 | /* Create a grayscale map of num_colors values, range 0..255 */ |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 276 | put_3bytes(dinfo, (i * 255 + (num_colors-1)/2) / (num_colors-1)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 277 | } |
| 278 | } else { |
| 279 | /* fill out the map to a power of 2 */ |
| 280 | put_3bytes(dinfo, 0); |
| 281 | } |
| 282 | } |
| 283 | /* Write image separator and Image Descriptor */ |
| 284 | putc(',', dinfo->pub.output_file); /* separator */ |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 285 | put_word(dinfo, 0); /* left/top offset */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 286 | put_word(dinfo, 0); |
| 287 | put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); /* image size */ |
| 288 | put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); |
| 289 | /* flag byte: not interlaced, no local color map */ |
| 290 | putc(0x00, dinfo->pub.output_file); |
| 291 | /* Write Initial Code Size byte */ |
| 292 | putc(InitCodeSize, dinfo->pub.output_file); |
| 293 | |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 294 | /* Initialize for "compression" of image data */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 295 | compress_init(dinfo, InitCodeSize+1); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | /* |
| 300 | * Startup: write the file header. |
| 301 | */ |
| 302 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 303 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 304 | start_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
| 305 | { |
| 306 | gif_dest_ptr dest = (gif_dest_ptr) dinfo; |
| 307 | |
| 308 | if (cinfo->quantize_colors) |
| 309 | emit_header(dest, cinfo->actual_number_of_colors, cinfo->colormap); |
| 310 | else |
| 311 | emit_header(dest, 256, (JSAMPARRAY) NULL); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /* |
| 316 | * Write some pixel data. |
| 317 | * In this module rows_supplied will always be 1. |
| 318 | */ |
| 319 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 320 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 321 | put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 322 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 323 | { |
| 324 | gif_dest_ptr dest = (gif_dest_ptr) dinfo; |
| 325 | register JSAMPROW ptr; |
| 326 | register JDIMENSION col; |
| 327 | |
| 328 | ptr = dest->pub.buffer[0]; |
| 329 | for (col = cinfo->output_width; col > 0; col--) { |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 330 | compress_pixel(dest, GETJSAMPLE(*ptr++)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /* |
| 336 | * Finish up at the end of the file. |
| 337 | */ |
| 338 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 339 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 340 | finish_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
| 341 | { |
| 342 | gif_dest_ptr dest = (gif_dest_ptr) dinfo; |
| 343 | |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 344 | /* Flush "compression" mechanism */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 345 | compress_term(dest); |
| 346 | /* Write a zero-length data block to end the series */ |
| 347 | putc(0, dest->pub.output_file); |
| 348 | /* Write the GIF terminator mark */ |
| 349 | putc(';', dest->pub.output_file); |
| 350 | /* Make sure we wrote the output file OK */ |
| 351 | fflush(dest->pub.output_file); |
| 352 | if (ferror(dest->pub.output_file)) |
| 353 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* |
| 358 | * The module selection routine for GIF format output. |
| 359 | */ |
| 360 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 361 | GLOBAL(djpeg_dest_ptr) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 362 | jinit_write_gif (j_decompress_ptr cinfo) |
| 363 | { |
| 364 | gif_dest_ptr dest; |
| 365 | |
| 366 | /* Create module interface object, fill in method pointers */ |
| 367 | dest = (gif_dest_ptr) |
| 368 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 369 | sizeof(gif_dest_struct)); |
DRC | b775351 | 2014-05-11 09:36:25 +0000 | [diff] [blame] | 370 | dest->cinfo = cinfo; /* make back link for subroutines */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 371 | dest->pub.start_output = start_output_gif; |
| 372 | dest->pub.put_pixel_rows = put_pixel_rows; |
| 373 | dest->pub.finish_output = finish_output_gif; |
| 374 | |
| 375 | if (cinfo->out_color_space != JCS_GRAYSCALE && |
| 376 | cinfo->out_color_space != JCS_RGB) |
| 377 | ERREXIT(cinfo, JERR_GIF_COLORSPACE); |
| 378 | |
| 379 | /* Force quantization if color or if > 8 bits input */ |
| 380 | if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) { |
| 381 | /* Force quantization to at most 256 colors */ |
| 382 | cinfo->quantize_colors = TRUE; |
| 383 | if (cinfo->desired_number_of_colors > 256) |
| 384 | cinfo->desired_number_of_colors = 256; |
| 385 | } |
| 386 | |
| 387 | /* Calculate output image dimensions so we can allocate space */ |
| 388 | jpeg_calc_output_dimensions(cinfo); |
| 389 | |
| 390 | if (cinfo->output_components != 1) /* safety check: just one component? */ |
| 391 | ERREXIT(cinfo, JERR_GIF_BUG); |
| 392 | |
| 393 | /* Create decompressor output buffer. */ |
| 394 | dest->pub.buffer = (*cinfo->mem->alloc_sarray) |
| 395 | ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION) 1); |
| 396 | dest->pub.buffer_height = 1; |
| 397 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 398 | return (djpeg_dest_ptr) dest; |
| 399 | } |
| 400 | |
| 401 | #endif /* GIF_SUPPORTED */ |