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