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