media/libjpeg/jcmarker.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * jcmarker.c
michael@0 3 *
michael@0 4 * This file was part of the Independent JPEG Group's software:
michael@0 5 * Copyright (C) 1991-1998, Thomas G. Lane.
michael@0 6 * Modified 2003-2010 by Guido Vollbeding.
michael@0 7 * libjpeg-turbo Modifications:
michael@0 8 * Copyright (C) 2010, D. R. Commander.
michael@0 9 * For conditions of distribution and use, see the accompanying README file.
michael@0 10 *
michael@0 11 * This file contains routines to write JPEG datastream markers.
michael@0 12 */
michael@0 13
michael@0 14 #define JPEG_INTERNALS
michael@0 15 #include "jinclude.h"
michael@0 16 #include "jpeglib.h"
michael@0 17 #include "jpegcomp.h"
michael@0 18
michael@0 19
michael@0 20 typedef enum { /* JPEG marker codes */
michael@0 21 M_SOF0 = 0xc0,
michael@0 22 M_SOF1 = 0xc1,
michael@0 23 M_SOF2 = 0xc2,
michael@0 24 M_SOF3 = 0xc3,
michael@0 25
michael@0 26 M_SOF5 = 0xc5,
michael@0 27 M_SOF6 = 0xc6,
michael@0 28 M_SOF7 = 0xc7,
michael@0 29
michael@0 30 M_JPG = 0xc8,
michael@0 31 M_SOF9 = 0xc9,
michael@0 32 M_SOF10 = 0xca,
michael@0 33 M_SOF11 = 0xcb,
michael@0 34
michael@0 35 M_SOF13 = 0xcd,
michael@0 36 M_SOF14 = 0xce,
michael@0 37 M_SOF15 = 0xcf,
michael@0 38
michael@0 39 M_DHT = 0xc4,
michael@0 40
michael@0 41 M_DAC = 0xcc,
michael@0 42
michael@0 43 M_RST0 = 0xd0,
michael@0 44 M_RST1 = 0xd1,
michael@0 45 M_RST2 = 0xd2,
michael@0 46 M_RST3 = 0xd3,
michael@0 47 M_RST4 = 0xd4,
michael@0 48 M_RST5 = 0xd5,
michael@0 49 M_RST6 = 0xd6,
michael@0 50 M_RST7 = 0xd7,
michael@0 51
michael@0 52 M_SOI = 0xd8,
michael@0 53 M_EOI = 0xd9,
michael@0 54 M_SOS = 0xda,
michael@0 55 M_DQT = 0xdb,
michael@0 56 M_DNL = 0xdc,
michael@0 57 M_DRI = 0xdd,
michael@0 58 M_DHP = 0xde,
michael@0 59 M_EXP = 0xdf,
michael@0 60
michael@0 61 M_APP0 = 0xe0,
michael@0 62 M_APP1 = 0xe1,
michael@0 63 M_APP2 = 0xe2,
michael@0 64 M_APP3 = 0xe3,
michael@0 65 M_APP4 = 0xe4,
michael@0 66 M_APP5 = 0xe5,
michael@0 67 M_APP6 = 0xe6,
michael@0 68 M_APP7 = 0xe7,
michael@0 69 M_APP8 = 0xe8,
michael@0 70 M_APP9 = 0xe9,
michael@0 71 M_APP10 = 0xea,
michael@0 72 M_APP11 = 0xeb,
michael@0 73 M_APP12 = 0xec,
michael@0 74 M_APP13 = 0xed,
michael@0 75 M_APP14 = 0xee,
michael@0 76 M_APP15 = 0xef,
michael@0 77
michael@0 78 M_JPG0 = 0xf0,
michael@0 79 M_JPG13 = 0xfd,
michael@0 80 M_COM = 0xfe,
michael@0 81
michael@0 82 M_TEM = 0x01,
michael@0 83
michael@0 84 M_ERROR = 0x100
michael@0 85 } JPEG_MARKER;
michael@0 86
michael@0 87
michael@0 88 /* Private state */
michael@0 89
michael@0 90 typedef struct {
michael@0 91 struct jpeg_marker_writer pub; /* public fields */
michael@0 92
michael@0 93 unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
michael@0 94 } my_marker_writer;
michael@0 95
michael@0 96 typedef my_marker_writer * my_marker_ptr;
michael@0 97
michael@0 98
michael@0 99 /*
michael@0 100 * Basic output routines.
michael@0 101 *
michael@0 102 * Note that we do not support suspension while writing a marker.
michael@0 103 * Therefore, an application using suspension must ensure that there is
michael@0 104 * enough buffer space for the initial markers (typ. 600-700 bytes) before
michael@0 105 * calling jpeg_start_compress, and enough space to write the trailing EOI
michael@0 106 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
michael@0 107 * modes are not supported at all with suspension, so those two are the only
michael@0 108 * points where markers will be written.
michael@0 109 */
michael@0 110
michael@0 111 LOCAL(void)
michael@0 112 emit_byte (j_compress_ptr cinfo, int val)
michael@0 113 /* Emit a byte */
michael@0 114 {
michael@0 115 struct jpeg_destination_mgr * dest = cinfo->dest;
michael@0 116
michael@0 117 *(dest->next_output_byte)++ = (JOCTET) val;
michael@0 118 if (--dest->free_in_buffer == 0) {
michael@0 119 if (! (*dest->empty_output_buffer) (cinfo))
michael@0 120 ERREXIT(cinfo, JERR_CANT_SUSPEND);
michael@0 121 }
michael@0 122 }
michael@0 123
michael@0 124
michael@0 125 LOCAL(void)
michael@0 126 emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
michael@0 127 /* Emit a marker code */
michael@0 128 {
michael@0 129 emit_byte(cinfo, 0xFF);
michael@0 130 emit_byte(cinfo, (int) mark);
michael@0 131 }
michael@0 132
michael@0 133
michael@0 134 LOCAL(void)
michael@0 135 emit_2bytes (j_compress_ptr cinfo, int value)
michael@0 136 /* Emit a 2-byte integer; these are always MSB first in JPEG files */
michael@0 137 {
michael@0 138 emit_byte(cinfo, (value >> 8) & 0xFF);
michael@0 139 emit_byte(cinfo, value & 0xFF);
michael@0 140 }
michael@0 141
michael@0 142
michael@0 143 /*
michael@0 144 * Routines to write specific marker types.
michael@0 145 */
michael@0 146
michael@0 147 LOCAL(int)
michael@0 148 emit_dqt (j_compress_ptr cinfo, int index)
michael@0 149 /* Emit a DQT marker */
michael@0 150 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
michael@0 151 {
michael@0 152 JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
michael@0 153 int prec;
michael@0 154 int i;
michael@0 155
michael@0 156 if (qtbl == NULL)
michael@0 157 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
michael@0 158
michael@0 159 prec = 0;
michael@0 160 for (i = 0; i < DCTSIZE2; i++) {
michael@0 161 if (qtbl->quantval[i] > 255)
michael@0 162 prec = 1;
michael@0 163 }
michael@0 164
michael@0 165 if (! qtbl->sent_table) {
michael@0 166 emit_marker(cinfo, M_DQT);
michael@0 167
michael@0 168 emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
michael@0 169
michael@0 170 emit_byte(cinfo, index + (prec<<4));
michael@0 171
michael@0 172 for (i = 0; i < DCTSIZE2; i++) {
michael@0 173 /* The table entries must be emitted in zigzag order. */
michael@0 174 unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
michael@0 175 if (prec)
michael@0 176 emit_byte(cinfo, (int) (qval >> 8));
michael@0 177 emit_byte(cinfo, (int) (qval & 0xFF));
michael@0 178 }
michael@0 179
michael@0 180 qtbl->sent_table = TRUE;
michael@0 181 }
michael@0 182
michael@0 183 return prec;
michael@0 184 }
michael@0 185
michael@0 186
michael@0 187 LOCAL(void)
michael@0 188 emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
michael@0 189 /* Emit a DHT marker */
michael@0 190 {
michael@0 191 JHUFF_TBL * htbl;
michael@0 192 int length, i;
michael@0 193
michael@0 194 if (is_ac) {
michael@0 195 htbl = cinfo->ac_huff_tbl_ptrs[index];
michael@0 196 index += 0x10; /* output index has AC bit set */
michael@0 197 } else {
michael@0 198 htbl = cinfo->dc_huff_tbl_ptrs[index];
michael@0 199 }
michael@0 200
michael@0 201 if (htbl == NULL)
michael@0 202 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
michael@0 203
michael@0 204 if (! htbl->sent_table) {
michael@0 205 emit_marker(cinfo, M_DHT);
michael@0 206
michael@0 207 length = 0;
michael@0 208 for (i = 1; i <= 16; i++)
michael@0 209 length += htbl->bits[i];
michael@0 210
michael@0 211 emit_2bytes(cinfo, length + 2 + 1 + 16);
michael@0 212 emit_byte(cinfo, index);
michael@0 213
michael@0 214 for (i = 1; i <= 16; i++)
michael@0 215 emit_byte(cinfo, htbl->bits[i]);
michael@0 216
michael@0 217 for (i = 0; i < length; i++)
michael@0 218 emit_byte(cinfo, htbl->huffval[i]);
michael@0 219
michael@0 220 htbl->sent_table = TRUE;
michael@0 221 }
michael@0 222 }
michael@0 223
michael@0 224
michael@0 225 LOCAL(void)
michael@0 226 emit_dac (j_compress_ptr cinfo)
michael@0 227 /* Emit a DAC marker */
michael@0 228 /* Since the useful info is so small, we want to emit all the tables in */
michael@0 229 /* one DAC marker. Therefore this routine does its own scan of the table. */
michael@0 230 {
michael@0 231 #ifdef C_ARITH_CODING_SUPPORTED
michael@0 232 char dc_in_use[NUM_ARITH_TBLS];
michael@0 233 char ac_in_use[NUM_ARITH_TBLS];
michael@0 234 int length, i;
michael@0 235 jpeg_component_info *compptr;
michael@0 236
michael@0 237 for (i = 0; i < NUM_ARITH_TBLS; i++)
michael@0 238 dc_in_use[i] = ac_in_use[i] = 0;
michael@0 239
michael@0 240 for (i = 0; i < cinfo->comps_in_scan; i++) {
michael@0 241 compptr = cinfo->cur_comp_info[i];
michael@0 242 /* DC needs no table for refinement scan */
michael@0 243 if (cinfo->Ss == 0 && cinfo->Ah == 0)
michael@0 244 dc_in_use[compptr->dc_tbl_no] = 1;
michael@0 245 /* AC needs no table when not present */
michael@0 246 if (cinfo->Se)
michael@0 247 ac_in_use[compptr->ac_tbl_no] = 1;
michael@0 248 }
michael@0 249
michael@0 250 length = 0;
michael@0 251 for (i = 0; i < NUM_ARITH_TBLS; i++)
michael@0 252 length += dc_in_use[i] + ac_in_use[i];
michael@0 253
michael@0 254 if (length) {
michael@0 255 emit_marker(cinfo, M_DAC);
michael@0 256
michael@0 257 emit_2bytes(cinfo, length*2 + 2);
michael@0 258
michael@0 259 for (i = 0; i < NUM_ARITH_TBLS; i++) {
michael@0 260 if (dc_in_use[i]) {
michael@0 261 emit_byte(cinfo, i);
michael@0 262 emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
michael@0 263 }
michael@0 264 if (ac_in_use[i]) {
michael@0 265 emit_byte(cinfo, i + 0x10);
michael@0 266 emit_byte(cinfo, cinfo->arith_ac_K[i]);
michael@0 267 }
michael@0 268 }
michael@0 269 }
michael@0 270 #endif /* C_ARITH_CODING_SUPPORTED */
michael@0 271 }
michael@0 272
michael@0 273
michael@0 274 LOCAL(void)
michael@0 275 emit_dri (j_compress_ptr cinfo)
michael@0 276 /* Emit a DRI marker */
michael@0 277 {
michael@0 278 emit_marker(cinfo, M_DRI);
michael@0 279
michael@0 280 emit_2bytes(cinfo, 4); /* fixed length */
michael@0 281
michael@0 282 emit_2bytes(cinfo, (int) cinfo->restart_interval);
michael@0 283 }
michael@0 284
michael@0 285
michael@0 286 LOCAL(void)
michael@0 287 emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
michael@0 288 /* Emit a SOF marker */
michael@0 289 {
michael@0 290 int ci;
michael@0 291 jpeg_component_info *compptr;
michael@0 292
michael@0 293 emit_marker(cinfo, code);
michael@0 294
michael@0 295 emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
michael@0 296
michael@0 297 /* Make sure image isn't bigger than SOF field can handle */
michael@0 298 if ((long) cinfo->_jpeg_height > 65535L ||
michael@0 299 (long) cinfo->_jpeg_width > 65535L)
michael@0 300 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
michael@0 301
michael@0 302 emit_byte(cinfo, cinfo->data_precision);
michael@0 303 emit_2bytes(cinfo, (int) cinfo->_jpeg_height);
michael@0 304 emit_2bytes(cinfo, (int) cinfo->_jpeg_width);
michael@0 305
michael@0 306 emit_byte(cinfo, cinfo->num_components);
michael@0 307
michael@0 308 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
michael@0 309 ci++, compptr++) {
michael@0 310 emit_byte(cinfo, compptr->component_id);
michael@0 311 emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
michael@0 312 emit_byte(cinfo, compptr->quant_tbl_no);
michael@0 313 }
michael@0 314 }
michael@0 315
michael@0 316
michael@0 317 LOCAL(void)
michael@0 318 emit_sos (j_compress_ptr cinfo)
michael@0 319 /* Emit a SOS marker */
michael@0 320 {
michael@0 321 int i, td, ta;
michael@0 322 jpeg_component_info *compptr;
michael@0 323
michael@0 324 emit_marker(cinfo, M_SOS);
michael@0 325
michael@0 326 emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
michael@0 327
michael@0 328 emit_byte(cinfo, cinfo->comps_in_scan);
michael@0 329
michael@0 330 for (i = 0; i < cinfo->comps_in_scan; i++) {
michael@0 331 compptr = cinfo->cur_comp_info[i];
michael@0 332 emit_byte(cinfo, compptr->component_id);
michael@0 333
michael@0 334 /* We emit 0 for unused field(s); this is recommended by the P&M text
michael@0 335 * but does not seem to be specified in the standard.
michael@0 336 */
michael@0 337
michael@0 338 /* DC needs no table for refinement scan */
michael@0 339 td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
michael@0 340 /* AC needs no table when not present */
michael@0 341 ta = cinfo->Se ? compptr->ac_tbl_no : 0;
michael@0 342
michael@0 343 emit_byte(cinfo, (td << 4) + ta);
michael@0 344 }
michael@0 345
michael@0 346 emit_byte(cinfo, cinfo->Ss);
michael@0 347 emit_byte(cinfo, cinfo->Se);
michael@0 348 emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
michael@0 349 }
michael@0 350
michael@0 351
michael@0 352 LOCAL(void)
michael@0 353 emit_jfif_app0 (j_compress_ptr cinfo)
michael@0 354 /* Emit a JFIF-compliant APP0 marker */
michael@0 355 {
michael@0 356 /*
michael@0 357 * Length of APP0 block (2 bytes)
michael@0 358 * Block ID (4 bytes - ASCII "JFIF")
michael@0 359 * Zero byte (1 byte to terminate the ID string)
michael@0 360 * Version Major, Minor (2 bytes - major first)
michael@0 361 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
michael@0 362 * Xdpu (2 bytes - dots per unit horizontal)
michael@0 363 * Ydpu (2 bytes - dots per unit vertical)
michael@0 364 * Thumbnail X size (1 byte)
michael@0 365 * Thumbnail Y size (1 byte)
michael@0 366 */
michael@0 367
michael@0 368 emit_marker(cinfo, M_APP0);
michael@0 369
michael@0 370 emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
michael@0 371
michael@0 372 emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
michael@0 373 emit_byte(cinfo, 0x46);
michael@0 374 emit_byte(cinfo, 0x49);
michael@0 375 emit_byte(cinfo, 0x46);
michael@0 376 emit_byte(cinfo, 0);
michael@0 377 emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
michael@0 378 emit_byte(cinfo, cinfo->JFIF_minor_version);
michael@0 379 emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
michael@0 380 emit_2bytes(cinfo, (int) cinfo->X_density);
michael@0 381 emit_2bytes(cinfo, (int) cinfo->Y_density);
michael@0 382 emit_byte(cinfo, 0); /* No thumbnail image */
michael@0 383 emit_byte(cinfo, 0);
michael@0 384 }
michael@0 385
michael@0 386
michael@0 387 LOCAL(void)
michael@0 388 emit_adobe_app14 (j_compress_ptr cinfo)
michael@0 389 /* Emit an Adobe APP14 marker */
michael@0 390 {
michael@0 391 /*
michael@0 392 * Length of APP14 block (2 bytes)
michael@0 393 * Block ID (5 bytes - ASCII "Adobe")
michael@0 394 * Version Number (2 bytes - currently 100)
michael@0 395 * Flags0 (2 bytes - currently 0)
michael@0 396 * Flags1 (2 bytes - currently 0)
michael@0 397 * Color transform (1 byte)
michael@0 398 *
michael@0 399 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
michael@0 400 * now in circulation seem to use Version = 100, so that's what we write.
michael@0 401 *
michael@0 402 * We write the color transform byte as 1 if the JPEG color space is
michael@0 403 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
michael@0 404 * whether the encoder performed a transformation, which is pretty useless.
michael@0 405 */
michael@0 406
michael@0 407 emit_marker(cinfo, M_APP14);
michael@0 408
michael@0 409 emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
michael@0 410
michael@0 411 emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
michael@0 412 emit_byte(cinfo, 0x64);
michael@0 413 emit_byte(cinfo, 0x6F);
michael@0 414 emit_byte(cinfo, 0x62);
michael@0 415 emit_byte(cinfo, 0x65);
michael@0 416 emit_2bytes(cinfo, 100); /* Version */
michael@0 417 emit_2bytes(cinfo, 0); /* Flags0 */
michael@0 418 emit_2bytes(cinfo, 0); /* Flags1 */
michael@0 419 switch (cinfo->jpeg_color_space) {
michael@0 420 case JCS_YCbCr:
michael@0 421 emit_byte(cinfo, 1); /* Color transform = 1 */
michael@0 422 break;
michael@0 423 case JCS_YCCK:
michael@0 424 emit_byte(cinfo, 2); /* Color transform = 2 */
michael@0 425 break;
michael@0 426 default:
michael@0 427 emit_byte(cinfo, 0); /* Color transform = 0 */
michael@0 428 break;
michael@0 429 }
michael@0 430 }
michael@0 431
michael@0 432
michael@0 433 /*
michael@0 434 * These routines allow writing an arbitrary marker with parameters.
michael@0 435 * The only intended use is to emit COM or APPn markers after calling
michael@0 436 * write_file_header and before calling write_frame_header.
michael@0 437 * Other uses are not guaranteed to produce desirable results.
michael@0 438 * Counting the parameter bytes properly is the caller's responsibility.
michael@0 439 */
michael@0 440
michael@0 441 METHODDEF(void)
michael@0 442 write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
michael@0 443 /* Emit an arbitrary marker header */
michael@0 444 {
michael@0 445 if (datalen > (unsigned int) 65533) /* safety check */
michael@0 446 ERREXIT(cinfo, JERR_BAD_LENGTH);
michael@0 447
michael@0 448 emit_marker(cinfo, (JPEG_MARKER) marker);
michael@0 449
michael@0 450 emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
michael@0 451 }
michael@0 452
michael@0 453 METHODDEF(void)
michael@0 454 write_marker_byte (j_compress_ptr cinfo, int val)
michael@0 455 /* Emit one byte of marker parameters following write_marker_header */
michael@0 456 {
michael@0 457 emit_byte(cinfo, val);
michael@0 458 }
michael@0 459
michael@0 460
michael@0 461 /*
michael@0 462 * Write datastream header.
michael@0 463 * This consists of an SOI and optional APPn markers.
michael@0 464 * We recommend use of the JFIF marker, but not the Adobe marker,
michael@0 465 * when using YCbCr or grayscale data. The JFIF marker should NOT
michael@0 466 * be used for any other JPEG colorspace. The Adobe marker is helpful
michael@0 467 * to distinguish RGB, CMYK, and YCCK colorspaces.
michael@0 468 * Note that an application can write additional header markers after
michael@0 469 * jpeg_start_compress returns.
michael@0 470 */
michael@0 471
michael@0 472 METHODDEF(void)
michael@0 473 write_file_header (j_compress_ptr cinfo)
michael@0 474 {
michael@0 475 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
michael@0 476
michael@0 477 emit_marker(cinfo, M_SOI); /* first the SOI */
michael@0 478
michael@0 479 /* SOI is defined to reset restart interval to 0 */
michael@0 480 marker->last_restart_interval = 0;
michael@0 481
michael@0 482 if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
michael@0 483 emit_jfif_app0(cinfo);
michael@0 484 if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
michael@0 485 emit_adobe_app14(cinfo);
michael@0 486 }
michael@0 487
michael@0 488
michael@0 489 /*
michael@0 490 * Write frame header.
michael@0 491 * This consists of DQT and SOFn markers.
michael@0 492 * Note that we do not emit the SOF until we have emitted the DQT(s).
michael@0 493 * This avoids compatibility problems with incorrect implementations that
michael@0 494 * try to error-check the quant table numbers as soon as they see the SOF.
michael@0 495 */
michael@0 496
michael@0 497 METHODDEF(void)
michael@0 498 write_frame_header (j_compress_ptr cinfo)
michael@0 499 {
michael@0 500 int ci, prec;
michael@0 501 boolean is_baseline;
michael@0 502 jpeg_component_info *compptr;
michael@0 503
michael@0 504 /* Emit DQT for each quantization table.
michael@0 505 * Note that emit_dqt() suppresses any duplicate tables.
michael@0 506 */
michael@0 507 prec = 0;
michael@0 508 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
michael@0 509 ci++, compptr++) {
michael@0 510 prec += emit_dqt(cinfo, compptr->quant_tbl_no);
michael@0 511 }
michael@0 512 /* now prec is nonzero iff there are any 16-bit quant tables. */
michael@0 513
michael@0 514 /* Check for a non-baseline specification.
michael@0 515 * Note we assume that Huffman table numbers won't be changed later.
michael@0 516 */
michael@0 517 if (cinfo->arith_code || cinfo->progressive_mode ||
michael@0 518 cinfo->data_precision != 8) {
michael@0 519 is_baseline = FALSE;
michael@0 520 } else {
michael@0 521 is_baseline = TRUE;
michael@0 522 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
michael@0 523 ci++, compptr++) {
michael@0 524 if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
michael@0 525 is_baseline = FALSE;
michael@0 526 }
michael@0 527 if (prec && is_baseline) {
michael@0 528 is_baseline = FALSE;
michael@0 529 /* If it's baseline except for quantizer size, warn the user */
michael@0 530 TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
michael@0 531 }
michael@0 532 }
michael@0 533
michael@0 534 /* Emit the proper SOF marker */
michael@0 535 if (cinfo->arith_code) {
michael@0 536 if (cinfo->progressive_mode)
michael@0 537 emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
michael@0 538 else
michael@0 539 emit_sof(cinfo, M_SOF9); /* SOF code for sequential arithmetic */
michael@0 540 } else {
michael@0 541 if (cinfo->progressive_mode)
michael@0 542 emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
michael@0 543 else if (is_baseline)
michael@0 544 emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
michael@0 545 else
michael@0 546 emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
michael@0 547 }
michael@0 548 }
michael@0 549
michael@0 550
michael@0 551 /*
michael@0 552 * Write scan header.
michael@0 553 * This consists of DHT or DAC markers, optional DRI, and SOS.
michael@0 554 * Compressed data will be written following the SOS.
michael@0 555 */
michael@0 556
michael@0 557 METHODDEF(void)
michael@0 558 write_scan_header (j_compress_ptr cinfo)
michael@0 559 {
michael@0 560 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
michael@0 561 int i;
michael@0 562 jpeg_component_info *compptr;
michael@0 563
michael@0 564 if (cinfo->arith_code) {
michael@0 565 /* Emit arith conditioning info. We may have some duplication
michael@0 566 * if the file has multiple scans, but it's so small it's hardly
michael@0 567 * worth worrying about.
michael@0 568 */
michael@0 569 emit_dac(cinfo);
michael@0 570 } else {
michael@0 571 /* Emit Huffman tables.
michael@0 572 * Note that emit_dht() suppresses any duplicate tables.
michael@0 573 */
michael@0 574 for (i = 0; i < cinfo->comps_in_scan; i++) {
michael@0 575 compptr = cinfo->cur_comp_info[i];
michael@0 576 /* DC needs no table for refinement scan */
michael@0 577 if (cinfo->Ss == 0 && cinfo->Ah == 0)
michael@0 578 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
michael@0 579 /* AC needs no table when not present */
michael@0 580 if (cinfo->Se)
michael@0 581 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
michael@0 582 }
michael@0 583 }
michael@0 584
michael@0 585 /* Emit DRI if required --- note that DRI value could change for each scan.
michael@0 586 * We avoid wasting space with unnecessary DRIs, however.
michael@0 587 */
michael@0 588 if (cinfo->restart_interval != marker->last_restart_interval) {
michael@0 589 emit_dri(cinfo);
michael@0 590 marker->last_restart_interval = cinfo->restart_interval;
michael@0 591 }
michael@0 592
michael@0 593 emit_sos(cinfo);
michael@0 594 }
michael@0 595
michael@0 596
michael@0 597 /*
michael@0 598 * Write datastream trailer.
michael@0 599 */
michael@0 600
michael@0 601 METHODDEF(void)
michael@0 602 write_file_trailer (j_compress_ptr cinfo)
michael@0 603 {
michael@0 604 emit_marker(cinfo, M_EOI);
michael@0 605 }
michael@0 606
michael@0 607
michael@0 608 /*
michael@0 609 * Write an abbreviated table-specification datastream.
michael@0 610 * This consists of SOI, DQT and DHT tables, and EOI.
michael@0 611 * Any table that is defined and not marked sent_table = TRUE will be
michael@0 612 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
michael@0 613 */
michael@0 614
michael@0 615 METHODDEF(void)
michael@0 616 write_tables_only (j_compress_ptr cinfo)
michael@0 617 {
michael@0 618 int i;
michael@0 619
michael@0 620 emit_marker(cinfo, M_SOI);
michael@0 621
michael@0 622 for (i = 0; i < NUM_QUANT_TBLS; i++) {
michael@0 623 if (cinfo->quant_tbl_ptrs[i] != NULL)
michael@0 624 (void) emit_dqt(cinfo, i);
michael@0 625 }
michael@0 626
michael@0 627 if (! cinfo->arith_code) {
michael@0 628 for (i = 0; i < NUM_HUFF_TBLS; i++) {
michael@0 629 if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
michael@0 630 emit_dht(cinfo, i, FALSE);
michael@0 631 if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
michael@0 632 emit_dht(cinfo, i, TRUE);
michael@0 633 }
michael@0 634 }
michael@0 635
michael@0 636 emit_marker(cinfo, M_EOI);
michael@0 637 }
michael@0 638
michael@0 639
michael@0 640 /*
michael@0 641 * Initialize the marker writer module.
michael@0 642 */
michael@0 643
michael@0 644 GLOBAL(void)
michael@0 645 jinit_marker_writer (j_compress_ptr cinfo)
michael@0 646 {
michael@0 647 my_marker_ptr marker;
michael@0 648
michael@0 649 /* Create the subobject */
michael@0 650 marker = (my_marker_ptr)
michael@0 651 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 652 SIZEOF(my_marker_writer));
michael@0 653 cinfo->marker = (struct jpeg_marker_writer *) marker;
michael@0 654 /* Initialize method pointers */
michael@0 655 marker->pub.write_file_header = write_file_header;
michael@0 656 marker->pub.write_frame_header = write_frame_header;
michael@0 657 marker->pub.write_scan_header = write_scan_header;
michael@0 658 marker->pub.write_file_trailer = write_file_trailer;
michael@0 659 marker->pub.write_tables_only = write_tables_only;
michael@0 660 marker->pub.write_marker_header = write_marker_header;
michael@0 661 marker->pub.write_marker_byte = write_marker_byte;
michael@0 662 /* Initialize private state */
michael@0 663 marker->last_restart_interval = 0;
michael@0 664 }

mercurial