blob: 1804e0bb39c486f0efbd47404fbe157530b5a0ed [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * wrgif.c
3 *
DRC5de454b2014-05-18 19:04:03 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
Alex Naidis6eb7d372016-10-16 23:10:08 +02006 * libjpeg-turbo Modifications:
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05007 * Copyright (C) 2015, 2017, D. R. Commander.
Alex Naidis6eb7d372016-10-16 23:10:08 +02008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 * This file contains routines to write output images in GIF format.
12 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000013 **************************************************************************
14 * NOTE: to avoid entanglements with Unisys' patent on LZW compression, *
15 * this code has been modified to output "uncompressed GIF" files. *
16 * There is no trace of the LZW algorithm in this file. *
17 **************************************************************************
18 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000019 * These routines may need modification for non-Unix environments or
20 * specialized applications. As they stand, they assume output to
21 * an ordinary stdio stream.
22 */
23
24/*
25 * This code is loosely based on ppmtogif from the PBMPLUS distribution
26 * of Feb. 1991. That file contains the following copyright notice:
27 * Based on GIFENCODE by David Rowley <mgardi@watdscu.waterloo.edu>.
28 * Lempel-Ziv compression based on "compress" by Spencer W. Thomas et al.
29 * Copyright (C) 1989 by Jef Poskanzer.
30 * Permission to use, copy, modify, and distribute this software and its
31 * documentation for any purpose and without fee is hereby granted, provided
32 * that the above copyright notice appear in all copies and that both that
33 * copyright notice and this permission notice appear in supporting
34 * documentation. This software is provided "as is" without express or
35 * implied warranty.
36 *
37 * We are also required to state that
38 * "The Graphics Interchange Format(c) is the Copyright property of
39 * CompuServe Incorporated. GIF(sm) is a Service Mark property of
40 * CompuServe Incorporated."
41 */
42
DRCb7753512014-05-11 09:36:25 +000043#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000044
45#ifdef GIF_SUPPORTED
46
47
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000048/* Private version of data destination object */
49
50typedef struct {
DRCb7753512014-05-11 09:36:25 +000051 struct djpeg_dest_struct pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000052
DRCb7753512014-05-11 09:36:25 +000053 j_decompress_ptr cinfo; /* back link saves passing separate parm */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000054
55 /* State for packing variable-width codes into a bitstream */
DRCb7753512014-05-11 09:36:25 +000056 int n_bits; /* current number of bits/code */
57 int maxcode; /* maximum code, given n_bits */
Alex Naidis6eb7d372016-10-16 23:10:08 +020058 long cur_accum; /* holds bits not yet output */
DRCb7753512014-05-11 09:36:25 +000059 int cur_bits; /* # of bits in cur_accum */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000060
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000061 /* State for GIF code assignment */
DRCb7753512014-05-11 09:36:25 +000062 int ClearCode; /* clear code (doesn't change) */
63 int EOFCode; /* EOF code (ditto) */
64 int code_counter; /* counts output symbols */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000065
66 /* GIF data packet construction buffer */
DRCb7753512014-05-11 09:36:25 +000067 int bytesinpkt; /* # of bytes in current packet */
68 char packetbuf[256]; /* workspace for accumulating packet */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069
70} gif_dest_struct;
71
Alex Naidis6eb7d372016-10-16 23:10:08 +020072typedef gif_dest_struct *gif_dest_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000074/* Largest value that will fit in N bits */
Leon Scroggins III3993b372018-07-16 10:43:45 -040075#define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000076
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000077
78/*
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000079 * Routines to package finished data bytes into GIF data blocks.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080 * A data block consists of a count byte (1..255) and that many data bytes.
81 */
82
Thomas G. Lane489583f1996-02-07 00:00:00 +000083LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -040084flush_packet(gif_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000085/* flush any accumulated data */
86{
DRCb7753512014-05-11 09:36:25 +000087 if (dinfo->bytesinpkt > 0) { /* never write zero-length packet */
Leon Scroggins III3993b372018-07-16 10:43:45 -040088 dinfo->packetbuf[0] = (char)dinfo->bytesinpkt++;
89 if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt) !=
90 (size_t)dinfo->bytesinpkt)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091 ERREXIT(dinfo->cinfo, JERR_FILE_WRITE);
92 dinfo->bytesinpkt = 0;
93 }
94}
95
96
97/* Add a character to current packet; flush to disk if necessary */
Leon Scroggins III3993b372018-07-16 10:43:45 -040098#define CHAR_OUT(dinfo, c) { \
99 (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char)(c); \
100 if ((dinfo)->bytesinpkt >= 255) \
101 flush_packet(dinfo); \
102}
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000103
104
105/* Routine to convert variable-width codes into a byte stream */
106
Thomas G. Lane489583f1996-02-07 00:00:00 +0000107LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400108output(gif_dest_ptr dinfo, int code)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000109/* Emit a code of n_bits bits */
110/* Uses cur_accum and cur_bits to reblock into 8-bit bytes */
111{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400112 dinfo->cur_accum |= ((long)code) << dinfo->cur_bits;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000113 dinfo->cur_bits += dinfo->n_bits;
114
115 while (dinfo->cur_bits >= 8) {
116 CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
117 dinfo->cur_accum >>= 8;
118 dinfo->cur_bits -= 8;
119 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120}
121
122
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000123/* The pseudo-compression algorithm.
124 *
125 * In this module we simply output each pixel value as a separate symbol;
126 * thus, no compression occurs. In fact, there is expansion of one bit per
127 * pixel, because we use a symbol width one bit wider than the pixel width.
128 *
129 * GIF ordinarily uses variable-width symbols, and the decoder will expect
130 * to ratchet up the symbol width after a fixed number of symbols.
131 * To simplify the logic and keep the expansion penalty down, we emit a
132 * GIF Clear code to reset the decoder just before the width would ratchet up.
133 * Thus, all the symbols in the output file will have the same bit width.
134 * Note that emitting the Clear codes at the right times is a mere matter of
135 * counting output symbols and is in no way dependent on the LZW patent.
136 *
137 * With a small basic pixel width (low color count), Clear codes will be
138 * needed very frequently, causing the file to expand even more. So this
139 * simplistic approach wouldn't work too well on bilevel images, for example.
140 * But for output of JPEG conversions the pixel width will usually be 8 bits
141 * (129 to 256 colors), so the overhead added by Clear symbols is only about
142 * one symbol in every 256.
143 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144
Thomas G. Lane489583f1996-02-07 00:00:00 +0000145LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400146compress_init(gif_dest_ptr dinfo, int i_bits)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000147/* Initialize pseudo-compressor */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000148{
149 /* init all the state variables */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000150 dinfo->n_bits = i_bits;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000151 dinfo->maxcode = MAXCODE(dinfo->n_bits);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000152 dinfo->ClearCode = (1 << (i_bits - 1));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000153 dinfo->EOFCode = dinfo->ClearCode + 1;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000154 dinfo->code_counter = dinfo->ClearCode + 2;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000155 /* init output buffering vars */
156 dinfo->bytesinpkt = 0;
157 dinfo->cur_accum = 0;
158 dinfo->cur_bits = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000159 /* GIF specifies an initial Clear code */
160 output(dinfo, dinfo->ClearCode);
161}
162
163
Thomas G. Lane489583f1996-02-07 00:00:00 +0000164LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400165compress_pixel(gif_dest_ptr dinfo, int c)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000166/* Accept and "compress" one pixel value.
167 * The given value must be less than n_bits wide.
168 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000169{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000170 /* Output the given pixel value as a symbol. */
171 output(dinfo, c);
172 /* Issue Clear codes often enough to keep the reader from ratcheting up
173 * its symbol size.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000175 if (dinfo->code_counter < dinfo->maxcode) {
176 dinfo->code_counter++;
177 } else {
178 output(dinfo, dinfo->ClearCode);
DRCb7753512014-05-11 09:36:25 +0000179 dinfo->code_counter = dinfo->ClearCode + 2; /* reset the counter */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181}
182
183
Thomas G. Lane489583f1996-02-07 00:00:00 +0000184LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400185compress_term(gif_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186/* Clean up at end */
187{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000188 /* Send an EOF code */
189 output(dinfo, dinfo->EOFCode);
190 /* Flush the bit-packing buffer */
191 if (dinfo->cur_bits > 0) {
192 CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
193 }
194 /* Flush the packet buffer */
195 flush_packet(dinfo);
196}
197
198
199/* GIF header construction */
200
201
Thomas G. Lane489583f1996-02-07 00:00:00 +0000202LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400203put_word(gif_dest_ptr dinfo, unsigned int w)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204/* Emit a 16-bit word, LSB first */
205{
206 putc(w & 0xFF, dinfo->pub.output_file);
207 putc((w >> 8) & 0xFF, dinfo->pub.output_file);
208}
209
210
Thomas G. Lane489583f1996-02-07 00:00:00 +0000211LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400212put_3bytes(gif_dest_ptr dinfo, int val)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000213/* Emit 3 copies of same byte value --- handy subr for colormap construction */
214{
215 putc(val, dinfo->pub.output_file);
216 putc(val, dinfo->pub.output_file);
217 putc(val, dinfo->pub.output_file);
218}
219
220
Thomas G. Lane489583f1996-02-07 00:00:00 +0000221LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400222emit_header(gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223/* Output the GIF file header, including color map */
DRC90d6c382014-05-12 09:08:39 +0000224/* If colormap==NULL, synthesize a grayscale colormap */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000225{
226 int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte;
227 int cshift = dinfo->cinfo->data_precision - 8;
228 int i;
229
230 if (num_colors > 256)
231 ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors);
232 /* Compute bits/pixel and related values */
233 BitsPerPixel = 1;
234 while (num_colors > (1 << BitsPerPixel))
235 BitsPerPixel++;
236 ColorMapSize = 1 << BitsPerPixel;
237 if (BitsPerPixel <= 1)
238 InitCodeSize = 2;
239 else
240 InitCodeSize = BitsPerPixel;
241 /*
242 * Write the GIF header.
243 * Note that we generate a plain GIF87 header for maximum compatibility.
244 */
245 putc('G', dinfo->pub.output_file);
246 putc('I', dinfo->pub.output_file);
247 putc('F', dinfo->pub.output_file);
248 putc('8', dinfo->pub.output_file);
249 putc('7', dinfo->pub.output_file);
250 putc('a', dinfo->pub.output_file);
251 /* Write the Logical Screen Descriptor */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400252 put_word(dinfo, (unsigned int)dinfo->cinfo->output_width);
253 put_word(dinfo, (unsigned int)dinfo->cinfo->output_height);
DRCb7753512014-05-11 09:36:25 +0000254 FlagByte = 0x80; /* Yes, there is a global color table */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400255 FlagByte |= (BitsPerPixel - 1) << 4; /* color resolution */
256 FlagByte |= (BitsPerPixel - 1); /* size of global color table */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257 putc(FlagByte, dinfo->pub.output_file);
258 putc(0, dinfo->pub.output_file); /* Background color index */
259 putc(0, dinfo->pub.output_file); /* Reserved (aspect ratio in GIF89) */
260 /* Write the Global Color Map */
261 /* If the color map is more than 8 bits precision, */
262 /* we reduce it to 8 bits by shifting */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400263 for (i = 0; i < ColorMapSize; i++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000264 if (i < num_colors) {
265 if (colormap != NULL) {
DRCb7753512014-05-11 09:36:25 +0000266 if (dinfo->cinfo->out_color_space == JCS_RGB) {
267 /* Normal case: RGB color map */
268 putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file);
269 putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file);
270 putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file);
271 } else {
272 /* Grayscale "color map": possible if quantizing grayscale image */
273 put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift);
274 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000275 } else {
DRC90d6c382014-05-12 09:08:39 +0000276 /* Create a grayscale map of num_colors values, range 0..255 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400277 put_3bytes(dinfo, (i * 255 + (num_colors - 1) / 2) / (num_colors - 1));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000278 }
279 } else {
280 /* fill out the map to a power of 2 */
281 put_3bytes(dinfo, 0);
282 }
283 }
284 /* Write image separator and Image Descriptor */
285 putc(',', dinfo->pub.output_file); /* separator */
DRCb7753512014-05-11 09:36:25 +0000286 put_word(dinfo, 0); /* left/top offset */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000287 put_word(dinfo, 0);
Leon Scroggins III3993b372018-07-16 10:43:45 -0400288 put_word(dinfo, (unsigned int)dinfo->cinfo->output_width); /* image size */
289 put_word(dinfo, (unsigned int)dinfo->cinfo->output_height);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 /* flag byte: not interlaced, no local color map */
291 putc(0x00, dinfo->pub.output_file);
292 /* Write Initial Code Size byte */
293 putc(InitCodeSize, dinfo->pub.output_file);
294
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000295 /* Initialize for "compression" of image data */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400296 compress_init(dinfo, InitCodeSize + 1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000297}
298
299
300/*
301 * Startup: write the file header.
302 */
303
Thomas G. Lane489583f1996-02-07 00:00:00 +0000304METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400305start_output_gif(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000306{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400307 gif_dest_ptr dest = (gif_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000308
309 if (cinfo->quantize_colors)
310 emit_header(dest, cinfo->actual_number_of_colors, cinfo->colormap);
311 else
Leon Scroggins III3993b372018-07-16 10:43:45 -0400312 emit_header(dest, 256, (JSAMPARRAY)NULL);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000313}
314
315
316/*
317 * Write some pixel data.
318 * In this module rows_supplied will always be 1.
319 */
320
Thomas G. Lane489583f1996-02-07 00:00:00 +0000321METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400322put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
323 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000324{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400325 gif_dest_ptr dest = (gif_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000326 register JSAMPROW ptr;
327 register JDIMENSION col;
328
329 ptr = dest->pub.buffer[0];
330 for (col = cinfo->output_width; col > 0; col--) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000331 compress_pixel(dest, GETJSAMPLE(*ptr++));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000332 }
333}
334
335
336/*
337 * Finish up at the end of the file.
338 */
339
Thomas G. Lane489583f1996-02-07 00:00:00 +0000340METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400341finish_output_gif(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000342{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400343 gif_dest_ptr dest = (gif_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000344
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000345 /* Flush "compression" mechanism */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000346 compress_term(dest);
347 /* Write a zero-length data block to end the series */
348 putc(0, dest->pub.output_file);
349 /* Write the GIF terminator mark */
350 putc(';', dest->pub.output_file);
351 /* Make sure we wrote the output file OK */
352 fflush(dest->pub.output_file);
353 if (ferror(dest->pub.output_file))
354 ERREXIT(cinfo, JERR_FILE_WRITE);
355}
356
357
358/*
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500359 * Re-calculate buffer dimensions based on output dimensions.
360 */
361
362METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400363calc_buffer_dimensions_gif(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500364{
365}
366
367
368/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000369 * The module selection routine for GIF format output.
370 */
371
Thomas G. Lane489583f1996-02-07 00:00:00 +0000372GLOBAL(djpeg_dest_ptr)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400373jinit_write_gif(j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000374{
375 gif_dest_ptr dest;
376
377 /* Create module interface object, fill in method pointers */
378 dest = (gif_dest_ptr)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400379 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
380 sizeof(gif_dest_struct));
DRCb7753512014-05-11 09:36:25 +0000381 dest->cinfo = cinfo; /* make back link for subroutines */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000382 dest->pub.start_output = start_output_gif;
383 dest->pub.put_pixel_rows = put_pixel_rows;
384 dest->pub.finish_output = finish_output_gif;
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500385 dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_gif;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000386
387 if (cinfo->out_color_space != JCS_GRAYSCALE &&
388 cinfo->out_color_space != JCS_RGB)
389 ERREXIT(cinfo, JERR_GIF_COLORSPACE);
390
391 /* Force quantization if color or if > 8 bits input */
392 if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) {
393 /* Force quantization to at most 256 colors */
394 cinfo->quantize_colors = TRUE;
395 if (cinfo->desired_number_of_colors > 256)
396 cinfo->desired_number_of_colors = 256;
397 }
398
399 /* Calculate output image dimensions so we can allocate space */
400 jpeg_calc_output_dimensions(cinfo);
401
402 if (cinfo->output_components != 1) /* safety check: just one component? */
403 ERREXIT(cinfo, JERR_GIF_BUG);
404
405 /* Create decompressor output buffer. */
406 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400407 ((j_common_ptr)cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION)1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000408 dest->pub.buffer_height = 1;
409
Leon Scroggins III3993b372018-07-16 10:43:45 -0400410 return (djpeg_dest_ptr)dest;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000411}
412
413#endif /* GIF_SUPPORTED */