00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "cdjpeg.h"
00041
00042 #ifdef GIF_SUPPORTED
00043
00044
00045
00046
00047 typedef struct {
00048 struct djpeg_dest_struct pub;
00049
00050 j_decompress_ptr cinfo;
00051
00052
00053 int n_bits;
00054 int maxcode;
00055 INT32 cur_accum;
00056 int cur_bits;
00057
00058
00059 int ClearCode;
00060 int EOFCode;
00061 int code_counter;
00062
00063
00064 int bytesinpkt;
00065 char packetbuf[256];
00066
00067 } gif_dest_struct;
00068
00069 typedef gif_dest_struct * gif_dest_ptr;
00070
00071
00072 #define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
00073
00074
00075
00076
00077
00078
00079
00080 LOCAL(void)
00081 flush_packet (gif_dest_ptr dinfo)
00082
00083 {
00084 if (dinfo->bytesinpkt > 0) {
00085 dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++;
00086 if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt)
00087 != (size_t) dinfo->bytesinpkt)
00088 ERREXIT(dinfo->cinfo, JERR_FILE_WRITE);
00089 dinfo->bytesinpkt = 0;
00090 }
00091 }
00092
00093
00094
00095 #define CHAR_OUT(dinfo,c) \
00096 { (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c); \
00097 if ((dinfo)->bytesinpkt >= 255) \
00098 flush_packet(dinfo); \
00099 }
00100
00101
00102
00103
00104 LOCAL(void)
00105 output (gif_dest_ptr dinfo, int code)
00106
00107
00108 {
00109 dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits;
00110 dinfo->cur_bits += dinfo->n_bits;
00111
00112 while (dinfo->cur_bits >= 8) {
00113 CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
00114 dinfo->cur_accum >>= 8;
00115 dinfo->cur_bits -= 8;
00116 }
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 LOCAL(void)
00143 compress_init (gif_dest_ptr dinfo, int i_bits)
00144
00145 {
00146
00147 dinfo->n_bits = i_bits;
00148 dinfo->maxcode = MAXCODE(dinfo->n_bits);
00149 dinfo->ClearCode = (1 << (i_bits - 1));
00150 dinfo->EOFCode = dinfo->ClearCode + 1;
00151 dinfo->code_counter = dinfo->ClearCode + 2;
00152
00153 dinfo->bytesinpkt = 0;
00154 dinfo->cur_accum = 0;
00155 dinfo->cur_bits = 0;
00156
00157 output(dinfo, dinfo->ClearCode);
00158 }
00159
00160
00161 LOCAL(void)
00162 compress_pixel (gif_dest_ptr dinfo, int c)
00163
00164
00165
00166 {
00167
00168 output(dinfo, c);
00169
00170
00171
00172 if (dinfo->code_counter < dinfo->maxcode) {
00173 dinfo->code_counter++;
00174 } else {
00175 output(dinfo, dinfo->ClearCode);
00176 dinfo->code_counter = dinfo->ClearCode + 2;
00177 }
00178 }
00179
00180
00181 LOCAL(void)
00182 compress_term (gif_dest_ptr dinfo)
00183
00184 {
00185
00186 output(dinfo, dinfo->EOFCode);
00187
00188 if (dinfo->cur_bits > 0) {
00189 CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
00190 }
00191
00192 flush_packet(dinfo);
00193 }
00194
00195
00196
00197
00198
00199 LOCAL(void)
00200 put_word (gif_dest_ptr dinfo, unsigned int w)
00201
00202 {
00203 putc(w & 0xFF, dinfo->pub.output_file);
00204 putc((w >> 8) & 0xFF, dinfo->pub.output_file);
00205 }
00206
00207
00208 LOCAL(void)
00209 put_3bytes (gif_dest_ptr dinfo, int val)
00210
00211 {
00212 putc(val, dinfo->pub.output_file);
00213 putc(val, dinfo->pub.output_file);
00214 putc(val, dinfo->pub.output_file);
00215 }
00216
00217
00218 LOCAL(void)
00219 emit_header (gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap)
00220
00221
00222 {
00223 int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte;
00224 int cshift = dinfo->cinfo->data_precision - 8;
00225 int i;
00226
00227 if (num_colors > 256)
00228 ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors);
00229
00230 BitsPerPixel = 1;
00231 while (num_colors > (1 << BitsPerPixel))
00232 BitsPerPixel++;
00233 ColorMapSize = 1 << BitsPerPixel;
00234 if (BitsPerPixel <= 1)
00235 InitCodeSize = 2;
00236 else
00237 InitCodeSize = BitsPerPixel;
00238
00239
00240
00241
00242 putc('G', dinfo->pub.output_file);
00243 putc('I', dinfo->pub.output_file);
00244 putc('F', dinfo->pub.output_file);
00245 putc('8', dinfo->pub.output_file);
00246 putc('7', dinfo->pub.output_file);
00247 putc('a', dinfo->pub.output_file);
00248
00249 put_word(dinfo, (unsigned int) dinfo->cinfo->output_width);
00250 put_word(dinfo, (unsigned int) dinfo->cinfo->output_height);
00251 FlagByte = 0x80;
00252 FlagByte |= (BitsPerPixel-1) << 4;
00253 FlagByte |= (BitsPerPixel-1);
00254 putc(FlagByte, dinfo->pub.output_file);
00255 putc(0, dinfo->pub.output_file);
00256 putc(0, dinfo->pub.output_file);
00257
00258
00259
00260 for (i=0; i < ColorMapSize; i++) {
00261 if (i < num_colors) {
00262 if (colormap != NULL) {
00263 if (dinfo->cinfo->out_color_space == JCS_RGB) {
00264
00265 putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file);
00266 putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file);
00267 putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file);
00268 } else {
00269
00270 put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift);
00271 }
00272 } else {
00273
00274 put_3bytes(dinfo, (i * 255 + (num_colors-1)/2) / (num_colors-1));
00275 }
00276 } else {
00277
00278 put_3bytes(dinfo, 0);
00279 }
00280 }
00281
00282 putc(',', dinfo->pub.output_file);
00283 put_word(dinfo, 0);
00284 put_word(dinfo, 0);
00285 put_word(dinfo, (unsigned int) dinfo->cinfo->output_width);
00286 put_word(dinfo, (unsigned int) dinfo->cinfo->output_height);
00287
00288 putc(0x00, dinfo->pub.output_file);
00289
00290 putc(InitCodeSize, dinfo->pub.output_file);
00291
00292
00293 compress_init(dinfo, InitCodeSize+1);
00294 }
00295
00296
00297
00298
00299
00300
00301 METHODDEF(void)
00302 start_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
00303 {
00304 gif_dest_ptr dest = (gif_dest_ptr) dinfo;
00305
00306 if (cinfo->quantize_colors)
00307 emit_header(dest, cinfo->actual_number_of_colors, cinfo->colormap);
00308 else
00309 emit_header(dest, 256, (JSAMPARRAY) NULL);
00310 }
00311
00312
00313
00314
00315
00316
00317
00318 METHODDEF(void)
00319 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
00320 JDIMENSION rows_supplied)
00321 {
00322 gif_dest_ptr dest = (gif_dest_ptr) dinfo;
00323 register JSAMPROW ptr;
00324 register JDIMENSION col;
00325
00326 ptr = dest->pub.buffer[0];
00327 for (col = cinfo->output_width; col > 0; col--) {
00328 compress_pixel(dest, GETJSAMPLE(*ptr++));
00329 }
00330 }
00331
00332
00333
00334
00335
00336
00337 METHODDEF(void)
00338 finish_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
00339 {
00340 gif_dest_ptr dest = (gif_dest_ptr) dinfo;
00341
00342
00343 compress_term(dest);
00344
00345 putc(0, dest->pub.output_file);
00346
00347 putc(';', dest->pub.output_file);
00348
00349 fflush(dest->pub.output_file);
00350 if (ferror(dest->pub.output_file))
00351 ERREXIT(cinfo, JERR_FILE_WRITE);
00352 }
00353
00354
00355
00356
00357
00358
00359 GLOBAL(djpeg_dest_ptr)
00360 jinit_write_gif (j_decompress_ptr cinfo)
00361 {
00362 gif_dest_ptr dest;
00363
00364
00365 dest = (gif_dest_ptr)
00366 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00367 SIZEOF(gif_dest_struct));
00368 dest->cinfo = cinfo;
00369 dest->pub.start_output = start_output_gif;
00370 dest->pub.put_pixel_rows = put_pixel_rows;
00371 dest->pub.finish_output = finish_output_gif;
00372
00373 if (cinfo->out_color_space != JCS_GRAYSCALE &&
00374 cinfo->out_color_space != JCS_RGB)
00375 ERREXIT(cinfo, JERR_GIF_COLORSPACE);
00376
00377
00378 if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) {
00379
00380 cinfo->quantize_colors = TRUE;
00381 if (cinfo->desired_number_of_colors > 256)
00382 cinfo->desired_number_of_colors = 256;
00383 }
00384
00385
00386 jpeg_calc_output_dimensions(cinfo);
00387
00388 if (cinfo->output_components != 1)
00389 ERREXIT(cinfo, JERR_GIF_BUG);
00390
00391
00392 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
00393 ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION) 1);
00394 dest->pub.buffer_height = 1;
00395
00396 return (djpeg_dest_ptr) dest;
00397 }
00398
00399 #endif