1
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
120
121
122
123
124
125
129
130
131
132
133
134
138
139
140
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
161
162
163
164
165
166
167
168
169
170
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
268
269
270
271
272
273
274
275
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
444
445
446
447
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
510
511
512
513
514
515
516
517
518
521
522
526
527
528
531
547
548
549
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
574
575
576
577
578
579
580
581
582
589
592
593
594
595
596
597
598
599
600
601
602
603
606
610
611
612
613
614
615
618
619
620
624
625
626
632
633
634
635
636
637
638
639
640
644
645
653
654
655
656
657
658
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/* ... */
#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
typedef enum {
M_SOF0 = 0xc0,
M_SOF1 = 0xc1,
M_SOF2 = 0xc2,
M_SOF3 = 0xc3,
M_SOF5 = 0xc5,
M_SOF6 = 0xc6,
M_SOF7 = 0xc7,
M_JPG = 0xc8,
M_SOF9 = 0xc9,
M_SOF10 = 0xca,
M_SOF11 = 0xcb,
M_SOF13 = 0xcd,
M_SOF14 = 0xce,
M_SOF15 = 0xcf,
M_DHT = 0xc4,
M_DAC = 0xcc,
M_RST0 = 0xd0,
M_RST1 = 0xd1,
M_RST2 = 0xd2,
M_RST3 = 0xd3,
M_RST4 = 0xd4,
M_RST5 = 0xd5,
M_RST6 = 0xd6,
M_RST7 = 0xd7,
M_SOI = 0xd8,
M_EOI = 0xd9,
M_SOS = 0xda,
M_DQT = 0xdb,
M_DNL = 0xdc,
M_DRI = 0xdd,
M_DHP = 0xde,
M_EXP = 0xdf,
M_APP0 = 0xe0,
M_APP1 = 0xe1,
M_APP2 = 0xe2,
M_APP3 = 0xe3,
M_APP4 = 0xe4,
M_APP5 = 0xe5,
M_APP6 = 0xe6,
M_APP7 = 0xe7,
M_APP8 = 0xe8,
M_APP9 = 0xe9,
M_APP10 = 0xea,
M_APP11 = 0xeb,
M_APP12 = 0xec,
M_APP13 = 0xed,
M_APP14 = 0xee,
M_APP15 = 0xef,
M_JPG0 = 0xf0,
M_JPG13 = 0xfd,
M_COM = 0xfe,
M_TEM = 0x01,
M_ERROR = 0x100
...} JPEG_MARKER;
typedef struct {
struct jpeg_marker_writer pub;
unsigned int last_restart_interval;
...} my_marker_writer;
typedef my_marker_writer * my_marker_ptr;
/* ... */
LOCAL(void)
emit_byte (j_compress_ptr cinfo, int val)
{
struct jpeg_destination_mgr * dest = cinfo->dest;
*(dest->next_output_byte)++ = (JOCTET) val;
if (--dest->free_in_buffer == 0) {
if (! (*dest->empty_output_buffer) (cinfo))
ERREXIT(cinfo, JERR_CANT_SUSPEND);
}if (--dest->free_in_buffer == 0) { ... }
...}
LOCAL(void)
emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
{
emit_byte(cinfo, 0xFF);
emit_byte(cinfo, (int) mark);
...}
LOCAL(void)
emit_2bytes (j_compress_ptr cinfo, int value)
{
emit_byte(cinfo, (value >> 8) & 0xFF);
emit_byte(cinfo, value & 0xFF);
...}
/* ... */
LOCAL(int)
emit_dqt (j_compress_ptr cinfo, int index)
{
JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
int prec;
int i;
if (qtbl == NULL)
ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
prec = 0;
for (i = 0; i <= cinfo->lim_Se; i++) {
if (qtbl->quantval[cinfo->natural_order[i]] > 255)
prec = 1;
}for (i = 0; i <= cinfo->lim_Se; i++) { ... }
if (! qtbl->sent_table) {
emit_marker(cinfo, M_DQT);
emit_2bytes(cinfo,
prec ? cinfo->lim_Se * 2 + 2 + 1 + 2 : cinfo->lim_Se + 1 + 1 + 2);
emit_byte(cinfo, index + (prec<<4));
for (i = 0; i <= cinfo->lim_Se; i++) {
unsigned int qval = qtbl->quantval[cinfo->natural_order[i]];
if (prec)
emit_byte(cinfo, (int) (qval >> 8));
emit_byte(cinfo, (int) (qval & 0xFF));
}for (i = 0; i <= cinfo->lim_Se; i++) { ... }
qtbl->sent_table = TRUE;
}if (! qtbl->sent_table) { ... }
return prec;
...}
LOCAL(void)
emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
{
JHUFF_TBL * htbl;
int length, i;
if (is_ac) {
htbl = cinfo->ac_huff_tbl_ptrs[index];
index += 0x10;
}if (is_ac) { ... } else {
htbl = cinfo->dc_huff_tbl_ptrs[index];
}else { ... }
if (htbl == NULL)
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
if (! htbl->sent_table) {
emit_marker(cinfo, M_DHT);
length = 0;
for (i = 1; i <= 16; i++)
length += htbl->bits[i];
emit_2bytes(cinfo, length + 2 + 1 + 16);
emit_byte(cinfo, index);
for (i = 1; i <= 16; i++)
emit_byte(cinfo, htbl->bits[i]);
for (i = 0; i < length; i++)
emit_byte(cinfo, htbl->huffval[i]);
htbl->sent_table = TRUE;
}if (! htbl->sent_table) { ... }
...}
LOCAL(void)
emit_dac (j_compress_ptr cinfo)
{
#ifdef C_ARITH_CODING_SUPPORTED
char dc_in_use[NUM_ARITH_TBLS];
char ac_in_use[NUM_ARITH_TBLS];
int length, i;
jpeg_component_info *compptr;
for (i = 0; i < NUM_ARITH_TBLS; i++)
dc_in_use[i] = ac_in_use[i] = 0;
for (i = 0; i < cinfo->comps_in_scan; i++) {
compptr = cinfo->cur_comp_info[i];
if (cinfo->Ss == 0 && cinfo->Ah == 0)
dc_in_use[compptr->dc_tbl_no] = 1;
if (cinfo->Se)
ac_in_use[compptr->ac_tbl_no] = 1;
}for (i = 0; i < cinfo->comps_in_scan; i++) { ... }
length = 0;
for (i = 0; i < NUM_ARITH_TBLS; i++)
length += dc_in_use[i] + ac_in_use[i];
if (length) {
emit_marker(cinfo, M_DAC);
emit_2bytes(cinfo, length*2 + 2);
for (i = 0; i < NUM_ARITH_TBLS; i++) {
if (dc_in_use[i]) {
emit_byte(cinfo, i);
emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
}if (dc_in_use[i]) { ... }
if (ac_in_use[i]) {
emit_byte(cinfo, i + 0x10);
emit_byte(cinfo, cinfo->arith_ac_K[i]);
}if (ac_in_use[i]) { ... }
}for (i = 0; i < NUM_ARITH_TBLS; i++) { ... }
}if (length) { ... }
/* ... */#endif
...}
LOCAL(void)
emit_dri (j_compress_ptr cinfo)
{
emit_marker(cinfo, M_DRI);
emit_2bytes(cinfo, 4);
emit_2bytes(cinfo, (int) cinfo->restart_interval);
...}
LOCAL(void)
emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
{
int ci;
jpeg_component_info *compptr;
emit_marker(cinfo, code);
emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1);
if ((long) cinfo->jpeg_height > 65535L ||
(long) cinfo->jpeg_width > 65535L)
ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
emit_byte(cinfo, cinfo->data_precision);
emit_2bytes(cinfo, (int) cinfo->jpeg_height);
emit_2bytes(cinfo, (int) cinfo->jpeg_width);
emit_byte(cinfo, cinfo->num_components);
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
emit_byte(cinfo, compptr->component_id);
emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
emit_byte(cinfo, compptr->quant_tbl_no);
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
...}
LOCAL(void)
emit_sos (j_compress_ptr cinfo)
{
int i, td, ta;
jpeg_component_info *compptr;
emit_marker(cinfo, M_SOS);
emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3);
emit_byte(cinfo, cinfo->comps_in_scan);
for (i = 0; i < cinfo->comps_in_scan; i++) {
compptr = cinfo->cur_comp_info[i];
emit_byte(cinfo, compptr->component_id);
/* ... */
td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
ta = cinfo->Se ? compptr->ac_tbl_no : 0;
emit_byte(cinfo, (td << 4) + ta);
}for (i = 0; i < cinfo->comps_in_scan; i++) { ... }
emit_byte(cinfo, cinfo->Ss);
emit_byte(cinfo, cinfo->Se);
emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
...}
LOCAL(void)
emit_pseudo_sos (j_compress_ptr cinfo)
{
emit_marker(cinfo, M_SOS);
emit_2bytes(cinfo, 2 + 1 + 3);
emit_byte(cinfo, 0);
emit_byte(cinfo, 0);
emit_byte(cinfo, cinfo->block_size * cinfo->block_size - 1);
emit_byte(cinfo, 0);
...}
LOCAL(void)
emit_jfif_app0 (j_compress_ptr cinfo)
{
/* ... */
emit_marker(cinfo, M_APP0);
emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1);
emit_byte(cinfo, 0x4A);
emit_byte(cinfo, 0x46);
emit_byte(cinfo, 0x49);
emit_byte(cinfo, 0x46);
emit_byte(cinfo, 0);
emit_byte(cinfo, cinfo->JFIF_major_version);
emit_byte(cinfo, cinfo->JFIF_minor_version);
emit_byte(cinfo, cinfo->density_unit);
emit_2bytes(cinfo, (int) cinfo->X_density);
emit_2bytes(cinfo, (int) cinfo->Y_density);
emit_byte(cinfo, 0);
emit_byte(cinfo, 0);
...}
LOCAL(void)
emit_adobe_app14 (j_compress_ptr cinfo)
{
/* ... */
emit_marker(cinfo, M_APP14);
emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1);
emit_byte(cinfo, 0x41);
emit_byte(cinfo, 0x64);
emit_byte(cinfo, 0x6F);
emit_byte(cinfo, 0x62);
emit_byte(cinfo, 0x65);
emit_2bytes(cinfo, 100);
emit_2bytes(cinfo, 0);
emit_2bytes(cinfo, 0);
switch (cinfo->jpeg_color_space) {
case JCS_YCbCr:
emit_byte(cinfo, 1);
break;case JCS_YCbCr:
case JCS_YCCK:
emit_byte(cinfo, 2);
break;case JCS_YCCK:
default:
emit_byte(cinfo, 0);
break;default
}switch (cinfo->jpeg_color_space) { ... }
...}
/* ... */
METHODDEF(void)
write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
{
if (datalen > (unsigned int) 65533)
ERREXIT(cinfo, JERR_BAD_LENGTH);
emit_marker(cinfo, (JPEG_MARKER) marker);
emit_2bytes(cinfo, (int) (datalen + 2));
...}
METHODDEF(void)
write_marker_byte (j_compress_ptr cinfo, int val)
{
emit_byte(cinfo, val);
...}
/* ... */
METHODDEF(void)
write_file_header (j_compress_ptr cinfo)
{
my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
emit_marker(cinfo, M_SOI);
marker->last_restart_interval = 0;
if (cinfo->write_JFIF_header)
emit_jfif_app0(cinfo);
if (cinfo->write_Adobe_marker)
emit_adobe_app14(cinfo);
}{ ... }
/* ... */
METHODDEF(void)
write_frame_header (j_compress_ptr cinfo)
{
int ci, prec;
boolean is_baseline;
jpeg_component_info *compptr;
/* ... */
prec = 0;
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
prec += emit_dqt(cinfo, compptr->quant_tbl_no);
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
/* ... */
if (cinfo->arith_code || cinfo->progressive_mode ||
cinfo->data_precision != 8 || cinfo->block_size != DCTSIZE) {
is_baseline = FALSE;
}if (cinfo->arith_code || cinfo->progressive_mode || cinfo->data_precision != 8 || cinfo->block_size != DCTSIZE) { ... } else {
is_baseline = TRUE;
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
is_baseline = FALSE;
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
if (prec && is_baseline) {
is_baseline = FALSE;
TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
}if (prec && is_baseline) { ... }
}else { ... }
if (cinfo->arith_code) {
if (cinfo->progressive_mode)
emit_sof(cinfo, M_SOF10);
else
emit_sof(cinfo, M_SOF9);
}if (cinfo->arith_code) { ... } else {
if (cinfo->progressive_mode)
emit_sof(cinfo, M_SOF2);
else if (is_baseline)
emit_sof(cinfo, M_SOF0);
else
emit_sof(cinfo, M_SOF1);
}else { ... }
if (cinfo->progressive_mode && cinfo->block_size != DCTSIZE)
emit_pseudo_sos(cinfo);
}{ ... }
/* ... */
METHODDEF(void)
write_scan_header (j_compress_ptr cinfo)
{
my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
int i;
jpeg_component_info *compptr;
if (cinfo->arith_code) {
/* ... */
emit_dac(cinfo);
}if (cinfo->arith_code) { ... } else {
/* ... */
for (i = 0; i < cinfo->comps_in_scan; i++) {
compptr = cinfo->cur_comp_info[i];
if (cinfo->Ss == 0 && cinfo->Ah == 0)
emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
if (cinfo->Se)
emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
}for (i = 0; i < cinfo->comps_in_scan; i++) { ... }
}else { ... }
/* ... */
if (cinfo->restart_interval != marker->last_restart_interval) {
emit_dri(cinfo);
marker->last_restart_interval = cinfo->restart_interval;
}if (cinfo->restart_interval != marker->last_restart_interval) { ... }
emit_sos(cinfo);
}{ ... }
/* ... */
METHODDEF(void)
write_file_trailer (j_compress_ptr cinfo)
{
emit_marker(cinfo, M_EOI);
}{ ... }
/* ... */
METHODDEF(void)
write_tables_only (j_compress_ptr cinfo)
{
int i;
emit_marker(cinfo, M_SOI);
for (i = 0; i < NUM_QUANT_TBLS; i++) {
if (cinfo->quant_tbl_ptrs[i] != NULL)
(void) emit_dqt(cinfo, i);
}for (i = 0; i < NUM_QUANT_TBLS; i++) { ... }
if (! cinfo->arith_code) {
for (i = 0; i < NUM_HUFF_TBLS; i++) {
if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
emit_dht(cinfo, i, FALSE);
if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
emit_dht(cinfo, i, TRUE);
}for (i = 0; i < NUM_HUFF_TBLS; i++) { ... }
}if (! cinfo->arith_code) { ... }
emit_marker(cinfo, M_EOI);
}{ ... }
/* ... */
GLOBAL(void)
jinit_marker_writer (j_compress_ptr cinfo)
{
my_marker_ptr marker;
marker = (my_marker_ptr)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
SIZEOF(my_marker_writer));
cinfo->marker = (struct jpeg_marker_writer *) marker;
marker->pub.write_file_header = write_file_header;
marker->pub.write_frame_header = write_frame_header;
marker->pub.write_scan_header = write_scan_header;
marker->pub.write_file_trailer = write_file_trailer;
marker->pub.write_tables_only = write_tables_only;
marker->pub.write_marker_header = write_marker_header;
marker->pub.write_marker_byte = write_marker_byte;
marker->last_restart_interval = 0;
}{ ... }