OLD | NEW |
1 /* | 1 /* |
2 * wrgif.c | 2 * wrgif.c |
3 * | 3 * |
| 4 * This file was part of the Independent JPEG Group's software: |
4 * Copyright (C) 1991-1997, Thomas G. Lane. | 5 * Copyright (C) 1991-1997, Thomas G. Lane. |
5 * This file is part of the Independent JPEG Group's software. | 6 * libjpeg-turbo Modifications: |
6 * For conditions of distribution and use, see the accompanying README file. | 7 * Copyright (C) 2015, D. R. Commander. |
| 8 * For conditions of distribution and use, see the accompanying README.ijg |
| 9 * file. |
7 * | 10 * |
8 * This file contains routines to write output images in GIF format. | 11 * This file contains routines to write output images in GIF format. |
9 * | 12 * |
10 ************************************************************************** | 13 ************************************************************************** |
11 * NOTE: to avoid entanglements with Unisys' patent on LZW compression, * | 14 * NOTE: to avoid entanglements with Unisys' patent on LZW compression, * |
12 * this code has been modified to output "uncompressed GIF" files. * | 15 * this code has been modified to output "uncompressed GIF" files. * |
13 * There is no trace of the LZW algorithm in this file. * | 16 * There is no trace of the LZW algorithm in this file. * |
14 ************************************************************************** | 17 ************************************************************************** |
15 * | 18 * |
16 * These routines may need modification for non-Unix environments or | 19 * These routines may need modification for non-Unix environments or |
(...skipping 13 matching lines...) Expand all Loading... |
30 * copyright notice and this permission notice appear in supporting | 33 * copyright notice and this permission notice appear in supporting |
31 * documentation. This software is provided "as is" without express or | 34 * documentation. This software is provided "as is" without express or |
32 * implied warranty. | 35 * implied warranty. |
33 * | 36 * |
34 * We are also required to state that | 37 * We are also required to state that |
35 * "The Graphics Interchange Format(c) is the Copyright property of | 38 * "The Graphics Interchange Format(c) is the Copyright property of |
36 * CompuServe Incorporated. GIF(sm) is a Service Mark property of | 39 * CompuServe Incorporated. GIF(sm) is a Service Mark property of |
37 * CompuServe Incorporated." | 40 * CompuServe Incorporated." |
38 */ | 41 */ |
39 | 42 |
40 #include "cdjpeg.h"» » /* Common decls for cjpeg/djpeg applications */ | 43 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
41 | 44 |
42 #ifdef GIF_SUPPORTED | 45 #ifdef GIF_SUPPORTED |
43 | 46 |
44 | 47 |
45 /* Private version of data destination object */ | 48 /* Private version of data destination object */ |
46 | 49 |
47 typedef struct { | 50 typedef struct { |
48 struct djpeg_dest_struct pub;»/* public fields */ | 51 struct djpeg_dest_struct pub; /* public fields */ |
49 | 52 |
50 j_decompress_ptr cinfo;» /* back link saves passing separate parm */ | 53 j_decompress_ptr cinfo; /* back link saves passing separate parm */ |
51 | 54 |
52 /* State for packing variable-width codes into a bitstream */ | 55 /* State for packing variable-width codes into a bitstream */ |
53 int n_bits;» » » /* current number of bits/code */ | 56 int n_bits; /* current number of bits/code */ |
54 int maxcode;» » » /* maximum code, given n_bits */ | 57 int maxcode; /* maximum code, given n_bits */ |
55 INT32 cur_accum;» » /* holds bits not yet output */ | 58 long cur_accum; /* holds bits not yet output */ |
56 int cur_bits;»» » /* # of bits in cur_accum */ | 59 int cur_bits; /* # of bits in cur_accum */ |
57 | 60 |
58 /* State for GIF code assignment */ | 61 /* State for GIF code assignment */ |
59 int ClearCode;» » /* clear code (doesn't change) */ | 62 int ClearCode; /* clear code (doesn't change) */ |
60 int EOFCode;» » » /* EOF code (ditto) */ | 63 int EOFCode; /* EOF code (ditto) */ |
61 int code_counter;» » /* counts output symbols */ | 64 int code_counter; /* counts output symbols */ |
62 | 65 |
63 /* GIF data packet construction buffer */ | 66 /* GIF data packet construction buffer */ |
64 int bytesinpkt;» » /* # of bytes in current packet */ | 67 int bytesinpkt; /* # of bytes in current packet */ |
65 char packetbuf[256];» » /* workspace for accumulating packet */ | 68 char packetbuf[256]; /* workspace for accumulating packet */ |
66 | 69 |
67 } gif_dest_struct; | 70 } gif_dest_struct; |
68 | 71 |
69 typedef gif_dest_struct * gif_dest_ptr; | 72 typedef gif_dest_struct *gif_dest_ptr; |
70 | 73 |
71 /* Largest value that will fit in N bits */ | 74 /* Largest value that will fit in N bits */ |
72 #define MAXCODE(n_bits)»((1 << (n_bits)) - 1) | 75 #define MAXCODE(n_bits) ((1 << (n_bits)) - 1) |
73 | 76 |
74 | 77 |
75 /* | 78 /* |
76 * Routines to package finished data bytes into GIF data blocks. | 79 * Routines to package finished data bytes into GIF data blocks. |
77 * A data block consists of a count byte (1..255) and that many data bytes. | 80 * A data block consists of a count byte (1..255) and that many data bytes. |
78 */ | 81 */ |
79 | 82 |
80 LOCAL(void) | 83 LOCAL(void) |
81 flush_packet (gif_dest_ptr dinfo) | 84 flush_packet (gif_dest_ptr dinfo) |
82 /* flush any accumulated data */ | 85 /* flush any accumulated data */ |
83 { | 86 { |
84 if (dinfo->bytesinpkt > 0) {» /* never write zero-length packet */ | 87 if (dinfo->bytesinpkt > 0) { /* never write zero-length packet */ |
85 dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++; | 88 dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++; |
86 if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt) | 89 if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt) |
87 » != (size_t) dinfo->bytesinpkt) | 90 != (size_t) dinfo->bytesinpkt) |
88 ERREXIT(dinfo->cinfo, JERR_FILE_WRITE); | 91 ERREXIT(dinfo->cinfo, JERR_FILE_WRITE); |
89 dinfo->bytesinpkt = 0; | 92 dinfo->bytesinpkt = 0; |
90 } | 93 } |
91 } | 94 } |
92 | 95 |
93 | 96 |
94 /* Add a character to current packet; flush to disk if necessary */ | 97 /* Add a character to current packet; flush to disk if necessary */ |
95 #define CHAR_OUT(dinfo,c) \ | 98 #define CHAR_OUT(dinfo,c) \ |
96 » { (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c); \ | 99 { (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c); \ |
97 » if ((dinfo)->bytesinpkt >= 255) \ | 100 if ((dinfo)->bytesinpkt >= 255) \ |
98 » flush_packet(dinfo); \ | 101 flush_packet(dinfo); \ |
99 » } | 102 } |
100 | 103 |
101 | 104 |
102 /* Routine to convert variable-width codes into a byte stream */ | 105 /* Routine to convert variable-width codes into a byte stream */ |
103 | 106 |
104 LOCAL(void) | 107 LOCAL(void) |
105 output (gif_dest_ptr dinfo, int code) | 108 output (gif_dest_ptr dinfo, int code) |
106 /* Emit a code of n_bits bits */ | 109 /* Emit a code of n_bits bits */ |
107 /* Uses cur_accum and cur_bits to reblock into 8-bit bytes */ | 110 /* Uses cur_accum and cur_bits to reblock into 8-bit bytes */ |
108 { | 111 { |
109 dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits; | 112 dinfo->cur_accum |= ((long) code) << dinfo->cur_bits; |
110 dinfo->cur_bits += dinfo->n_bits; | 113 dinfo->cur_bits += dinfo->n_bits; |
111 | 114 |
112 while (dinfo->cur_bits >= 8) { | 115 while (dinfo->cur_bits >= 8) { |
113 CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF); | 116 CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF); |
114 dinfo->cur_accum >>= 8; | 117 dinfo->cur_accum >>= 8; |
115 dinfo->cur_bits -= 8; | 118 dinfo->cur_bits -= 8; |
116 } | 119 } |
117 } | 120 } |
118 | 121 |
119 | 122 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 { | 169 { |
167 /* Output the given pixel value as a symbol. */ | 170 /* Output the given pixel value as a symbol. */ |
168 output(dinfo, c); | 171 output(dinfo, c); |
169 /* Issue Clear codes often enough to keep the reader from ratcheting up | 172 /* Issue Clear codes often enough to keep the reader from ratcheting up |
170 * its symbol size. | 173 * its symbol size. |
171 */ | 174 */ |
172 if (dinfo->code_counter < dinfo->maxcode) { | 175 if (dinfo->code_counter < dinfo->maxcode) { |
173 dinfo->code_counter++; | 176 dinfo->code_counter++; |
174 } else { | 177 } else { |
175 output(dinfo, dinfo->ClearCode); | 178 output(dinfo, dinfo->ClearCode); |
176 dinfo->code_counter = dinfo->ClearCode + 2;»/* reset the counter */ | 179 dinfo->code_counter = dinfo->ClearCode + 2; /* reset the counter */ |
177 } | 180 } |
178 } | 181 } |
179 | 182 |
180 | 183 |
181 LOCAL(void) | 184 LOCAL(void) |
182 compress_term (gif_dest_ptr dinfo) | 185 compress_term (gif_dest_ptr dinfo) |
183 /* Clean up at end */ | 186 /* Clean up at end */ |
184 { | 187 { |
185 /* Send an EOF code */ | 188 /* Send an EOF code */ |
186 output(dinfo, dinfo->EOFCode); | 189 output(dinfo, dinfo->EOFCode); |
(...skipping 24 matching lines...) Expand all Loading... |
211 { | 214 { |
212 putc(val, dinfo->pub.output_file); | 215 putc(val, dinfo->pub.output_file); |
213 putc(val, dinfo->pub.output_file); | 216 putc(val, dinfo->pub.output_file); |
214 putc(val, dinfo->pub.output_file); | 217 putc(val, dinfo->pub.output_file); |
215 } | 218 } |
216 | 219 |
217 | 220 |
218 LOCAL(void) | 221 LOCAL(void) |
219 emit_header (gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap) | 222 emit_header (gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap) |
220 /* Output the GIF file header, including color map */ | 223 /* Output the GIF file header, including color map */ |
221 /* If colormap==NULL, synthesize a gray-scale colormap */ | 224 /* If colormap==NULL, synthesize a grayscale colormap */ |
222 { | 225 { |
223 int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte; | 226 int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte; |
224 int cshift = dinfo->cinfo->data_precision - 8; | 227 int cshift = dinfo->cinfo->data_precision - 8; |
225 int i; | 228 int i; |
226 | 229 |
227 if (num_colors > 256) | 230 if (num_colors > 256) |
228 ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors); | 231 ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors); |
229 /* Compute bits/pixel and related values */ | 232 /* Compute bits/pixel and related values */ |
230 BitsPerPixel = 1; | 233 BitsPerPixel = 1; |
231 while (num_colors > (1 << BitsPerPixel)) | 234 while (num_colors > (1 << BitsPerPixel)) |
232 BitsPerPixel++; | 235 BitsPerPixel++; |
233 ColorMapSize = 1 << BitsPerPixel; | 236 ColorMapSize = 1 << BitsPerPixel; |
234 if (BitsPerPixel <= 1) | 237 if (BitsPerPixel <= 1) |
235 InitCodeSize = 2; | 238 InitCodeSize = 2; |
236 else | 239 else |
237 InitCodeSize = BitsPerPixel; | 240 InitCodeSize = BitsPerPixel; |
238 /* | 241 /* |
239 * Write the GIF header. | 242 * Write the GIF header. |
240 * Note that we generate a plain GIF87 header for maximum compatibility. | 243 * Note that we generate a plain GIF87 header for maximum compatibility. |
241 */ | 244 */ |
242 putc('G', dinfo->pub.output_file); | 245 putc('G', dinfo->pub.output_file); |
243 putc('I', dinfo->pub.output_file); | 246 putc('I', dinfo->pub.output_file); |
244 putc('F', dinfo->pub.output_file); | 247 putc('F', dinfo->pub.output_file); |
245 putc('8', dinfo->pub.output_file); | 248 putc('8', dinfo->pub.output_file); |
246 putc('7', dinfo->pub.output_file); | 249 putc('7', dinfo->pub.output_file); |
247 putc('a', dinfo->pub.output_file); | 250 putc('a', dinfo->pub.output_file); |
248 /* Write the Logical Screen Descriptor */ | 251 /* Write the Logical Screen Descriptor */ |
249 put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); | 252 put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); |
250 put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); | 253 put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); |
251 FlagByte = 0x80;» » /* Yes, there is a global color table */ | 254 FlagByte = 0x80; /* Yes, there is a global color table */ |
252 FlagByte |= (BitsPerPixel-1) << 4; /* color resolution */ | 255 FlagByte |= (BitsPerPixel-1) << 4; /* color resolution */ |
253 FlagByte |= (BitsPerPixel-1);»/* size of global color table */ | 256 FlagByte |= (BitsPerPixel-1); /* size of global color table */ |
254 putc(FlagByte, dinfo->pub.output_file); | 257 putc(FlagByte, dinfo->pub.output_file); |
255 putc(0, dinfo->pub.output_file); /* Background color index */ | 258 putc(0, dinfo->pub.output_file); /* Background color index */ |
256 putc(0, dinfo->pub.output_file); /* Reserved (aspect ratio in GIF89) */ | 259 putc(0, dinfo->pub.output_file); /* Reserved (aspect ratio in GIF89) */ |
257 /* Write the Global Color Map */ | 260 /* Write the Global Color Map */ |
258 /* If the color map is more than 8 bits precision, */ | 261 /* If the color map is more than 8 bits precision, */ |
259 /* we reduce it to 8 bits by shifting */ | 262 /* we reduce it to 8 bits by shifting */ |
260 for (i=0; i < ColorMapSize; i++) { | 263 for (i=0; i < ColorMapSize; i++) { |
261 if (i < num_colors) { | 264 if (i < num_colors) { |
262 if (colormap != NULL) { | 265 if (colormap != NULL) { |
263 » if (dinfo->cinfo->out_color_space == JCS_RGB) { | 266 if (dinfo->cinfo->out_color_space == JCS_RGB) { |
264 » /* Normal case: RGB color map */ | 267 /* Normal case: RGB color map */ |
265 » putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file); | 268 putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file); |
266 » putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file); | 269 putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file); |
267 » putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file); | 270 putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file); |
268 » } else { | 271 } else { |
269 » /* Grayscale "color map": possible if quantizing grayscale image */ | 272 /* Grayscale "color map": possible if quantizing grayscale image */ |
270 » put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift); | 273 put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift); |
271 » } | 274 } |
272 } else { | 275 } else { |
273 » /* Create a gray-scale map of num_colors values, range 0..255 */ | 276 /* Create a grayscale map of num_colors values, range 0..255 */ |
274 » put_3bytes(dinfo, (i * 255 + (num_colors-1)/2) / (num_colors-1)); | 277 put_3bytes(dinfo, (i * 255 + (num_colors-1)/2) / (num_colors-1)); |
275 } | 278 } |
276 } else { | 279 } else { |
277 /* fill out the map to a power of 2 */ | 280 /* fill out the map to a power of 2 */ |
278 put_3bytes(dinfo, 0); | 281 put_3bytes(dinfo, 0); |
279 } | 282 } |
280 } | 283 } |
281 /* Write image separator and Image Descriptor */ | 284 /* Write image separator and Image Descriptor */ |
282 putc(',', dinfo->pub.output_file); /* separator */ | 285 putc(',', dinfo->pub.output_file); /* separator */ |
283 put_word(dinfo, 0);» » /* left/top offset */ | 286 put_word(dinfo, 0); /* left/top offset */ |
284 put_word(dinfo, 0); | 287 put_word(dinfo, 0); |
285 put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); /* image size */ | 288 put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); /* image size */ |
286 put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); | 289 put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); |
287 /* flag byte: not interlaced, no local color map */ | 290 /* flag byte: not interlaced, no local color map */ |
288 putc(0x00, dinfo->pub.output_file); | 291 putc(0x00, dinfo->pub.output_file); |
289 /* Write Initial Code Size byte */ | 292 /* Write Initial Code Size byte */ |
290 putc(InitCodeSize, dinfo->pub.output_file); | 293 putc(InitCodeSize, dinfo->pub.output_file); |
291 | 294 |
292 /* Initialize for "compression" of image data */ | 295 /* Initialize for "compression" of image data */ |
293 compress_init(dinfo, InitCodeSize+1); | 296 compress_init(dinfo, InitCodeSize+1); |
(...skipping 16 matching lines...) Expand all Loading... |
310 } | 313 } |
311 | 314 |
312 | 315 |
313 /* | 316 /* |
314 * Write some pixel data. | 317 * Write some pixel data. |
315 * In this module rows_supplied will always be 1. | 318 * In this module rows_supplied will always be 1. |
316 */ | 319 */ |
317 | 320 |
318 METHODDEF(void) | 321 METHODDEF(void) |
319 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, | 322 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
320 » » JDIMENSION rows_supplied) | 323 JDIMENSION rows_supplied) |
321 { | 324 { |
322 gif_dest_ptr dest = (gif_dest_ptr) dinfo; | 325 gif_dest_ptr dest = (gif_dest_ptr) dinfo; |
323 register JSAMPROW ptr; | 326 register JSAMPROW ptr; |
324 register JDIMENSION col; | 327 register JDIMENSION col; |
325 | 328 |
326 ptr = dest->pub.buffer[0]; | 329 ptr = dest->pub.buffer[0]; |
327 for (col = cinfo->output_width; col > 0; col--) { | 330 for (col = cinfo->output_width; col > 0; col--) { |
328 compress_pixel(dest, GETJSAMPLE(*ptr++)); | 331 compress_pixel(dest, GETJSAMPLE(*ptr++)); |
329 } | 332 } |
330 } | 333 } |
(...skipping 26 matching lines...) Expand all Loading... |
357 */ | 360 */ |
358 | 361 |
359 GLOBAL(djpeg_dest_ptr) | 362 GLOBAL(djpeg_dest_ptr) |
360 jinit_write_gif (j_decompress_ptr cinfo) | 363 jinit_write_gif (j_decompress_ptr cinfo) |
361 { | 364 { |
362 gif_dest_ptr dest; | 365 gif_dest_ptr dest; |
363 | 366 |
364 /* Create module interface object, fill in method pointers */ | 367 /* Create module interface object, fill in method pointers */ |
365 dest = (gif_dest_ptr) | 368 dest = (gif_dest_ptr) |
366 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 369 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
367 » » » » SIZEOF(gif_dest_struct)); | 370 sizeof(gif_dest_struct)); |
368 dest->cinfo = cinfo;» » /* make back link for subroutines */ | 371 dest->cinfo = cinfo; /* make back link for subroutines */ |
369 dest->pub.start_output = start_output_gif; | 372 dest->pub.start_output = start_output_gif; |
370 dest->pub.put_pixel_rows = put_pixel_rows; | 373 dest->pub.put_pixel_rows = put_pixel_rows; |
371 dest->pub.finish_output = finish_output_gif; | 374 dest->pub.finish_output = finish_output_gif; |
372 | 375 |
373 if (cinfo->out_color_space != JCS_GRAYSCALE && | 376 if (cinfo->out_color_space != JCS_GRAYSCALE && |
374 cinfo->out_color_space != JCS_RGB) | 377 cinfo->out_color_space != JCS_RGB) |
375 ERREXIT(cinfo, JERR_GIF_COLORSPACE); | 378 ERREXIT(cinfo, JERR_GIF_COLORSPACE); |
376 | 379 |
377 /* Force quantization if color or if > 8 bits input */ | 380 /* Force quantization if color or if > 8 bits input */ |
378 if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) { | 381 if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) { |
(...skipping 11 matching lines...) Expand all Loading... |
390 | 393 |
391 /* Create decompressor output buffer. */ | 394 /* Create decompressor output buffer. */ |
392 dest->pub.buffer = (*cinfo->mem->alloc_sarray) | 395 dest->pub.buffer = (*cinfo->mem->alloc_sarray) |
393 ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION) 1); | 396 ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION) 1); |
394 dest->pub.buffer_height = 1; | 397 dest->pub.buffer_height = 1; |
395 | 398 |
396 return (djpeg_dest_ptr) dest; | 399 return (djpeg_dest_ptr) dest; |
397 } | 400 } |
398 | 401 |
399 #endif /* GIF_SUPPORTED */ | 402 #endif /* GIF_SUPPORTED */ |
OLD | NEW |