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