| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#include "cdjpeg.h" |
| 41 |
|
| 42 |
#ifdef GIF_SUPPORTED |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
typedef struct { |
| 48 |
struct djpeg_dest_struct pub; |
| 49 |
|
| 50 |
j_decompress_ptr cinfo; |
| 51 |
|
| 52 |
|
| 53 |
int n_bits; |
| 54 |
int maxcode; |
| 55 |
INT32 cur_accum; |
| 56 |
int cur_bits; |
| 57 |
|
| 58 |
|
| 59 |
int ClearCode; |
| 60 |
int EOFCode; |
| 61 |
int code_counter; |
| 62 |
|
| 63 |
|
| 64 |
int bytesinpkt; |
| 65 |
char packetbuf[256]; |
| 66 |
|
| 67 |
} gif_dest_struct; |
| 68 |
|
| 69 |
typedef gif_dest_struct * gif_dest_ptr; |
| 70 |
|
| 71 |
|
| 72 |
#define MAXCODE(n_bits) ((1 << (n_bits)) - 1) |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
LOCAL(void) |
| 81 |
flush_packet (gif_dest_ptr dinfo) |
| 82 |
|
| 83 |
{ |
| 84 |
if (dinfo->bytesinpkt > 0) { |
| 85 |
dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++; |
| 86 |
if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt) |
| 87 |
!= (size_t) dinfo->bytesinpkt) |
| 88 |
ERREXIT(dinfo->cinfo, JERR_FILE_WRITE); |
| 89 |
dinfo->bytesinpkt = 0; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
#define CHAR_OUT(dinfo,c) \ |
| 96 |
{ (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c); \ |
| 97 |
if ((dinfo)->bytesinpkt >= 255) \ |
| 98 |
flush_packet(dinfo); \ |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
LOCAL(void) |
| 105 |
output (gif_dest_ptr dinfo, int code) |
| 106 |
|
| 107 |
|
| 108 |
{ |
| 109 |
dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits; |
| 110 |
dinfo->cur_bits += dinfo->n_bits; |
| 111 |
|
| 112 |
while (dinfo->cur_bits >= 8) { |
| 113 |
CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF); |
| 114 |
dinfo->cur_accum >>= 8; |
| 115 |
dinfo->cur_bits -= 8; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
LOCAL(void) |
| 143 |
compress_init (gif_dest_ptr dinfo, int i_bits) |
| 144 |
|
| 145 |
{ |
| 146 |
|
| 147 |
dinfo->n_bits = i_bits; |
| 148 |
dinfo->maxcode = MAXCODE(dinfo->n_bits); |
| 149 |
dinfo->ClearCode = (1 << (i_bits - 1)); |
| 150 |
dinfo->EOFCode = dinfo->ClearCode + 1; |
| 151 |
dinfo->code_counter = dinfo->ClearCode + 2; |
| 152 |
|
| 153 |
dinfo->bytesinpkt = 0; |
| 154 |
dinfo->cur_accum = 0; |
| 155 |
dinfo->cur_bits = 0; |
| 156 |
|
| 157 |
output(dinfo, dinfo->ClearCode); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
LOCAL(void) |
| 162 |
compress_pixel (gif_dest_ptr dinfo, int c) |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
{ |
| 167 |
|
| 168 |
output(dinfo, c); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if (dinfo->code_counter < dinfo->maxcode) { |
| 173 |
dinfo->code_counter++; |
| 174 |
} else { |
| 175 |
output(dinfo, dinfo->ClearCode); |
| 176 |
dinfo->code_counter = dinfo->ClearCode + 2; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
LOCAL(void) |
| 182 |
compress_term (gif_dest_ptr dinfo) |
| 183 |
|
| 184 |
{ |
| 185 |
|
| 186 |
output(dinfo, dinfo->EOFCode); |
| 187 |
|
| 188 |
if (dinfo->cur_bits > 0) { |
| 189 |
CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF); |
| 190 |
} |
| 191 |
|
| 192 |
flush_packet(dinfo); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
LOCAL(void) |
| 200 |
put_word (gif_dest_ptr dinfo, unsigned int w) |
| 201 |
|
| 202 |
{ |
| 203 |
putc(w & 0xFF, dinfo->pub.output_file); |
| 204 |
putc((w >> 8) & 0xFF, dinfo->pub.output_file); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
LOCAL(void) |
| 209 |
put_3bytes (gif_dest_ptr dinfo, int val) |
| 210 |
|
| 211 |
{ |
| 212 |
putc(val, dinfo->pub.output_file); |
| 213 |
putc(val, dinfo->pub.output_file); |
| 214 |
putc(val, dinfo->pub.output_file); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
LOCAL(void) |
| 219 |
emit_header (gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap) |
| 220 |
|
| 221 |
|
| 222 |
{ |
| 223 |
int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte; |
| 224 |
int cshift = dinfo->cinfo->data_precision - 8; |
| 225 |
int i; |
| 226 |
|
| 227 |
if (num_colors > 256) |
| 228 |
ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors); |
| 229 |
|
| 230 |
BitsPerPixel = 1; |
| 231 |
while (num_colors > (1 << BitsPerPixel)) |
| 232 |
BitsPerPixel++; |
| 233 |
ColorMapSize = 1 << BitsPerPixel; |
| 234 |
if (BitsPerPixel <= 1) |
| 235 |
InitCodeSize = 2; |
| 236 |
else |
| 237 |
InitCodeSize = BitsPerPixel; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
putc('G', dinfo->pub.output_file); |
| 243 |
putc('I', dinfo->pub.output_file); |
| 244 |
putc('F', dinfo->pub.output_file); |
| 245 |
putc('8', dinfo->pub.output_file); |
| 246 |
putc('7', dinfo->pub.output_file); |
| 247 |
putc('a', dinfo->pub.output_file); |
| 248 |
|
| 249 |
put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); |
| 250 |
put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); |
| 251 |
FlagByte = 0x80; |
| 252 |
FlagByte |= (BitsPerPixel-1) << 4; |
| 253 |
FlagByte |= (BitsPerPixel-1); |
| 254 |
putc(FlagByte, dinfo->pub.output_file); |
| 255 |
putc(0, dinfo->pub.output_file); |
| 256 |
putc(0, dinfo->pub.output_file); |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
for (i=0; i < ColorMapSize; i++) { |
| 261 |
if (i < num_colors) { |
| 262 |
if (colormap != NULL) { |
| 263 |
if (dinfo->cinfo->out_color_space == JCS_RGB) { |
| 264 |
|
| 265 |
putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file); |
| 266 |
putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file); |
| 267 |
putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file); |
| 268 |
} else { |
| 269 |
|
| 270 |
put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift); |
| 271 |
} |
| 272 |
} else { |
| 273 |
|
| 274 |
put_3bytes(dinfo, (i * 255 + (num_colors-1)/2) / (num_colors-1)); |
| 275 |
} |
| 276 |
} else { |
| 277 |
|
| 278 |
put_3bytes(dinfo, 0); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
putc(',', dinfo->pub.output_file); |
| 283 |
put_word(dinfo, 0); |
| 284 |
put_word(dinfo, 0); |
| 285 |
put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); |
| 286 |
put_word(dinfo, (unsigned int) dinfo->cinfo->output_height); |
| 287 |
|
| 288 |
putc(0x00, dinfo->pub.output_file); |
| 289 |
|
| 290 |
putc(InitCodeSize, dinfo->pub.output_file); |
| 291 |
|
| 292 |
|
| 293 |
compress_init(dinfo, InitCodeSize+1); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
METHODDEF(void) |
| 302 |
start_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
| 303 |
{ |
| 304 |
gif_dest_ptr dest = (gif_dest_ptr) dinfo; |
| 305 |
|
| 306 |
if (cinfo->quantize_colors) |
| 307 |
emit_header(dest, cinfo->actual_number_of_colors, cinfo->colormap); |
| 308 |
else |
| 309 |
emit_header(dest, 256, (JSAMPARRAY) NULL); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
METHODDEF(void) |
| 319 |
put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
| 320 |
JDIMENSION rows_supplied) |
| 321 |
{ |
| 322 |
gif_dest_ptr dest = (gif_dest_ptr) dinfo; |
| 323 |
register JSAMPROW ptr; |
| 324 |
register JDIMENSION col; |
| 325 |
|
| 326 |
ptr = dest->pub.buffer[0]; |
| 327 |
for (col = cinfo->output_width; col > 0; col--) { |
| 328 |
compress_pixel(dest, GETJSAMPLE(*ptr++)); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
METHODDEF(void) |
| 338 |
finish_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
| 339 |
{ |
| 340 |
gif_dest_ptr dest = (gif_dest_ptr) dinfo; |
| 341 |
|
| 342 |
|
| 343 |
compress_term(dest); |
| 344 |
|
| 345 |
putc(0, dest->pub.output_file); |
| 346 |
|
| 347 |
putc(';', dest->pub.output_file); |
| 348 |
|
| 349 |
fflush(dest->pub.output_file); |
| 350 |
if (ferror(dest->pub.output_file)) |
| 351 |
ERREXIT(cinfo, JERR_FILE_WRITE); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
GLOBAL(djpeg_dest_ptr) |
| 360 |
jinit_write_gif (j_decompress_ptr cinfo) |
| 361 |
{ |
| 362 |
gif_dest_ptr dest; |
| 363 |
|
| 364 |
|
| 365 |
dest = (gif_dest_ptr) |
| 366 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 367 |
SIZEOF(gif_dest_struct)); |
| 368 |
dest->cinfo = cinfo; |
| 369 |
dest->pub.start_output = start_output_gif; |
| 370 |
dest->pub.put_pixel_rows = put_pixel_rows; |
| 371 |
dest->pub.finish_output = finish_output_gif; |
| 372 |
|
| 373 |
if (cinfo->out_color_space != JCS_GRAYSCALE && |
| 374 |
cinfo->out_color_space != JCS_RGB) |
| 375 |
ERREXIT(cinfo, JERR_GIF_COLORSPACE); |
| 376 |
|
| 377 |
|
| 378 |
if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) { |
| 379 |
|
| 380 |
cinfo->quantize_colors = TRUE; |
| 381 |
if (cinfo->desired_number_of_colors > 256) |
| 382 |
cinfo->desired_number_of_colors = 256; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
jpeg_calc_output_dimensions(cinfo); |
| 387 |
|
| 388 |
if (cinfo->output_components != 1) |
| 389 |
ERREXIT(cinfo, JERR_GIF_BUG); |
| 390 |
|
| 391 |
|
| 392 |
dest->pub.buffer = (*cinfo->mem->alloc_sarray) |
| 393 |
((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION) 1); |
| 394 |
dest->pub.buffer_height = 1; |
| 395 |
|
| 396 |
return (djpeg_dest_ptr) dest; |
| 397 |
} |
| 398 |
|
| 399 |
#endif |