Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/obj/jcmarker.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jcmarker.c
3
 *
4
 * Copyright (C) 1991-1998, Thomas G. Lane.
5
 * Modified 2003-2019 by Guido Vollbeding.
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_JPG8  = 0xf8,
77
  M_JPG13 = 0xfd,
78
  M_COM   = 0xfe,
79
80
  M_TEM   = 0x01,
81
82
  M_ERROR = 0x100
83
} JPEG_MARKER;
84
85
86
/* Private state */
87
88
typedef struct {
89
  struct jpeg_marker_writer pub; /* public fields */
90
91
  unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
92
} my_marker_writer;
93
94
typedef my_marker_writer * my_marker_ptr;
95
96
97
/*
98
 * Basic output routines.
99
 *
100
 * Note that we do not support suspension while writing a marker.
101
 * Therefore, an application using suspension must ensure that there is
102
 * enough buffer space for the initial markers (typ. 600-700 bytes) before
103
 * calling jpeg_start_compress, and enough space to write the trailing EOI
104
 * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
105
 * modes are not supported at all with suspension, so those two are the only
106
 * points where markers will be written.
107
 */
108
109
LOCAL(void)
110
emit_byte (j_compress_ptr cinfo, int val)
111
/* Emit a byte */
112
5.04M
{
113
5.04M
  struct jpeg_destination_mgr * dest = cinfo->dest;
114
115
5.04M
  *(dest->next_output_byte)++ = (JOCTET) val;
116
5.04M
  if (--dest->free_in_buffer == 0) {
117
0
    if (! (*dest->empty_output_buffer) (cinfo))
118
0
      ERREXIT(cinfo, JERR_CANT_SUSPEND);
119
0
  }
120
5.04M
}
121
122
123
LOCAL(void)
124
emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
125
/* Emit a marker code */
126
93.1k
{
127
93.1k
  emit_byte(cinfo, 0xFF);
128
93.1k
  emit_byte(cinfo, (int) mark);
129
93.1k
}
130
131
132
LOCAL(void)
133
emit_2bytes (j_compress_ptr cinfo, int value)
134
/* Emit a 2-byte integer; these are always MSB first in JPEG files */
135
92.5k
{
136
92.5k
  emit_byte(cinfo, (value >> 8) & 0xFF);
137
92.5k
  emit_byte(cinfo, value & 0xFF);
138
92.5k
}
139
140
141
/*
142
 * Routines to write specific marker types.
143
 */
144
145
LOCAL(int)
146
emit_dqt (j_compress_ptr cinfo, int index)
147
/* Emit a DQT marker */
148
/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
149
22.1k
{
150
22.1k
  JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
151
22.1k
  int prec;
152
22.1k
  int i;
153
154
22.1k
  if (qtbl == NULL)
155
0
    ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
156
157
22.1k
  prec = 0;
158
1.43M
  for (i = 0; i <= cinfo->lim_Se; i++) {
159
1.41M
    if (qtbl->quantval[cinfo->natural_order[i]] > 255)
160
0
      prec = 1;
161
1.41M
  }
162
163
22.1k
  if (! qtbl->sent_table) {
164
16.4k
    emit_marker(cinfo, M_DQT);
165
166
16.4k
    emit_2bytes(cinfo,
167
16.4k
      prec ? cinfo->lim_Se * 2 + 2 + 1 + 2 : cinfo->lim_Se + 1 + 1 + 2);
168
169
16.4k
    emit_byte(cinfo, index + (prec<<4));
170
171
1.07M
    for (i = 0; i <= cinfo->lim_Se; i++) {
172
      /* The table entries must be emitted in zigzag order. */
173
1.05M
      unsigned int qval = qtbl->quantval[cinfo->natural_order[i]];
174
1.05M
      if (prec)
175
0
  emit_byte(cinfo, (int) (qval >> 8));
176
1.05M
      emit_byte(cinfo, (int) (qval & 0xFF));
177
1.05M
    }
178
179
16.4k
    qtbl->sent_table = TRUE;
180
16.4k
  }
181
182
22.1k
  return prec;
183
22.1k
}
184
185
186
LOCAL(void)
187
emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
188
/* Emit a DHT marker */
189
44.2k
{
190
44.2k
  JHUFF_TBL * htbl;
191
44.2k
  int length, i;
192
  
193
44.2k
  if (is_ac) {
194
22.1k
    htbl = cinfo->ac_huff_tbl_ptrs[index];
195
22.1k
    index += 0x10;    /* output index has AC bit set */
196
22.1k
  } else {
197
22.1k
    htbl = cinfo->dc_huff_tbl_ptrs[index];
198
22.1k
  }
199
200
44.2k
  if (htbl == NULL)
201
0
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
202
  
203
44.2k
  if (! htbl->sent_table) {
204
32.9k
    emit_marker(cinfo, M_DHT);
205
    
206
32.9k
    length = 0;
207
559k
    for (i = 1; i <= 16; i++)
208
527k
      length += htbl->bits[i];
209
    
210
32.9k
    emit_2bytes(cinfo, length + 2 + 1 + 16);
211
32.9k
    emit_byte(cinfo, index);
212
    
213
559k
    for (i = 1; i <= 16; i++)
214
527k
      emit_byte(cinfo, htbl->bits[i]);
215
    
216
2.89M
    for (i = 0; i < length; i++)
217
2.86M
      emit_byte(cinfo, htbl->huffval[i]);
218
    
219
32.9k
    htbl->sent_table = TRUE;
220
32.9k
  }
221
44.2k
}
222
223
224
LOCAL(void)
225
emit_dac (j_compress_ptr cinfo)
226
/* Emit a DAC marker */
227
/* Since the useful info is so small, we want to emit all the tables in */
228
/* one DAC marker.  Therefore this routine does its own scan of the table. */
229
0
{
230
0
#ifdef C_ARITH_CODING_SUPPORTED
231
0
  char dc_in_use[NUM_ARITH_TBLS];
232
0
  char ac_in_use[NUM_ARITH_TBLS];
233
0
  int length, i;
234
0
  jpeg_component_info *compptr;
235
236
0
  for (i = 0; i < NUM_ARITH_TBLS; i++)
237
0
    dc_in_use[i] = ac_in_use[i] = 0;
238
239
0
  for (i = 0; i < cinfo->comps_in_scan; i++) {
240
0
    compptr = cinfo->cur_comp_info[i];
241
    /* DC needs no table for refinement scan */
242
0
    if (cinfo->Ss == 0 && cinfo->Ah == 0)
243
0
      dc_in_use[compptr->dc_tbl_no] = 1;
244
    /* AC needs no table when not present */
245
0
    if (cinfo->Se)
246
0
      ac_in_use[compptr->ac_tbl_no] = 1;
247
0
  }
248
249
0
  length = 0;
250
0
  for (i = 0; i < NUM_ARITH_TBLS; i++)
251
0
    length += dc_in_use[i] + ac_in_use[i];
252
253
0
  if (length) {
254
0
    emit_marker(cinfo, M_DAC);
255
256
0
    emit_2bytes(cinfo, length*2 + 2);
257
258
0
    for (i = 0; i < NUM_ARITH_TBLS; i++) {
259
0
      if (dc_in_use[i]) {
260
0
  emit_byte(cinfo, i);
261
0
  emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
262
0
      }
263
0
      if (ac_in_use[i]) {
264
0
  emit_byte(cinfo, i + 0x10);
265
0
  emit_byte(cinfo, cinfo->arith_ac_K[i]);
266
0
      }
267
0
    }
268
0
  }
269
0
#endif /* C_ARITH_CODING_SUPPORTED */
270
0
}
271
272
273
LOCAL(void)
274
emit_dri (j_compress_ptr cinfo)
275
/* Emit a DRI marker */
276
0
{
277
0
  emit_marker(cinfo, M_DRI);
278
  
279
0
  emit_2bytes(cinfo, 4);  /* fixed length */
280
281
0
  emit_2bytes(cinfo, (int) cinfo->restart_interval);
282
0
}
283
284
285
LOCAL(void)
286
emit_lse_ict (j_compress_ptr cinfo)
287
/* Emit an LSE inverse color transform specification marker */
288
0
{
289
  /* Support only 1 transform */
290
0
  if (cinfo->color_transform != JCT_SUBTRACT_GREEN ||
291
0
      cinfo->num_components < 3)
292
0
    ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
293
294
0
  emit_marker(cinfo, M_JPG8);
295
  
296
0
  emit_2bytes(cinfo, 24); /* fixed length */
297
298
0
  emit_byte(cinfo, 0x0D); /* ID inverse transform specification */
299
0
  emit_2bytes(cinfo, MAXJSAMPLE);  /* MAXTRANS */
300
0
  emit_byte(cinfo, 3);    /* Nt=3 */
301
0
  emit_byte(cinfo, cinfo->comp_info[1].component_id);
302
0
  emit_byte(cinfo, cinfo->comp_info[0].component_id);
303
0
  emit_byte(cinfo, cinfo->comp_info[2].component_id);
304
0
  emit_byte(cinfo, 0x80); /* F1: CENTER1=1, NORM1=0 */
305
0
  emit_2bytes(cinfo, 0);  /* A(1,1)=0 */
306
0
  emit_2bytes(cinfo, 0);  /* A(1,2)=0 */
307
0
  emit_byte(cinfo, 0);    /* F2: CENTER2=0, NORM2=0 */
308
0
  emit_2bytes(cinfo, 1);  /* A(2,1)=1 */
309
0
  emit_2bytes(cinfo, 0);  /* A(2,2)=0 */
310
0
  emit_byte(cinfo, 0);    /* F3: CENTER3=0, NORM3=0 */
311
0
  emit_2bytes(cinfo, 1);  /* A(3,1)=1 */
312
0
  emit_2bytes(cinfo, 0);  /* A(3,2)=0 */
313
0
}
314
315
316
LOCAL(void)
317
emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
318
/* Emit a SOF marker */
319
10.7k
{
320
10.7k
  int ci;
321
10.7k
  jpeg_component_info *compptr;
322
  
323
10.7k
  emit_marker(cinfo, code);
324
  
325
10.7k
  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
326
327
  /* Make sure image isn't bigger than SOF field can handle */
328
10.7k
  if ((long) cinfo->jpeg_height > 65535L ||
329
10.7k
      (long) cinfo->jpeg_width > 65535L)
330
0
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
331
332
10.7k
  emit_byte(cinfo, cinfo->data_precision);
333
10.7k
  emit_2bytes(cinfo, (int) cinfo->jpeg_height);
334
10.7k
  emit_2bytes(cinfo, (int) cinfo->jpeg_width);
335
336
10.7k
  emit_byte(cinfo, cinfo->num_components);
337
338
32.9k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
339
22.1k
       ci++, compptr++) {
340
22.1k
    emit_byte(cinfo, compptr->component_id);
341
22.1k
    emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
342
22.1k
    emit_byte(cinfo, compptr->quant_tbl_no);
343
22.1k
  }
344
10.7k
}
345
346
347
LOCAL(void)
348
emit_sos (j_compress_ptr cinfo)
349
/* Emit a SOS marker */
350
10.7k
{
351
10.7k
  int i, td, ta;
352
10.7k
  jpeg_component_info *compptr;
353
  
354
10.7k
  emit_marker(cinfo, M_SOS);
355
  
356
10.7k
  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
357
  
358
10.7k
  emit_byte(cinfo, cinfo->comps_in_scan);
359
  
360
32.9k
  for (i = 0; i < cinfo->comps_in_scan; i++) {
361
22.1k
    compptr = cinfo->cur_comp_info[i];
362
22.1k
    emit_byte(cinfo, compptr->component_id);
363
364
    /* We emit 0 for unused field(s); this is recommended by the P&M text
365
     * but does not seem to be specified in the standard.
366
     */
367
368
    /* DC needs no table for refinement scan */
369
22.1k
    td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
370
    /* AC needs no table when not present */
371
22.1k
    ta = cinfo->Se ? compptr->ac_tbl_no : 0;
372
373
22.1k
    emit_byte(cinfo, (td << 4) + ta);
374
22.1k
  }
375
376
10.7k
  emit_byte(cinfo, cinfo->Ss);
377
10.7k
  emit_byte(cinfo, cinfo->Se);
378
10.7k
  emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
379
10.7k
}
380
381
382
LOCAL(void)
383
emit_pseudo_sos (j_compress_ptr cinfo)
384
/* Emit a pseudo SOS marker */
385
0
{
386
0
  emit_marker(cinfo, M_SOS);
387
  
388
0
  emit_2bytes(cinfo, 2 + 1 + 3); /* length */
389
  
390
0
  emit_byte(cinfo, 0); /* Ns */
391
392
0
  emit_byte(cinfo, 0); /* Ss */
393
0
  emit_byte(cinfo, cinfo->block_size * cinfo->block_size - 1); /* Se */
394
0
  emit_byte(cinfo, 0); /* Ah/Al */
395
0
}
396
397
398
LOCAL(void)
399
emit_jfif_app0 (j_compress_ptr cinfo)
400
/* Emit a JFIF-compliant APP0 marker */
401
0
{
402
  /*
403
   * Length of APP0 block (2 bytes)
404
   * Block ID     (4 bytes - ASCII "JFIF")
405
   * Zero byte      (1 byte to terminate the ID string)
406
   * Version Major, Minor (2 bytes - major first)
407
   * Units      (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
408
   * Xdpu     (2 bytes - dots per unit horizontal)
409
   * Ydpu     (2 bytes - dots per unit vertical)
410
   * Thumbnail X size   (1 byte)
411
   * Thumbnail Y size   (1 byte)
412
   */
413
  
414
0
  emit_marker(cinfo, M_APP0);
415
  
416
0
  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
417
418
0
  emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
419
0
  emit_byte(cinfo, 0x46);
420
0
  emit_byte(cinfo, 0x49);
421
0
  emit_byte(cinfo, 0x46);
422
0
  emit_byte(cinfo, 0);
423
0
  emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
424
0
  emit_byte(cinfo, cinfo->JFIF_minor_version);
425
0
  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
426
0
  emit_2bytes(cinfo, (int) cinfo->X_density);
427
0
  emit_2bytes(cinfo, (int) cinfo->Y_density);
428
0
  emit_byte(cinfo, 0);    /* No thumbnail image */
429
0
  emit_byte(cinfo, 0);
430
0
}
431
432
433
LOCAL(void)
434
emit_adobe_app14 (j_compress_ptr cinfo)
435
/* Emit an Adobe APP14 marker */
436
0
{
437
  /*
438
   * Length of APP14 block  (2 bytes)
439
   * Block ID     (5 bytes - ASCII "Adobe")
440
   * Version Number   (2 bytes - currently 100)
441
   * Flags0     (2 bytes - currently 0)
442
   * Flags1     (2 bytes - currently 0)
443
   * Color transform    (1 byte)
444
   *
445
   * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
446
   * now in circulation seem to use Version = 100, so that's what we write.
447
   *
448
   * We write the color transform byte as 1 if the JPEG color space is
449
   * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
450
   * whether the encoder performed a transformation, which is pretty useless.
451
   */
452
  
453
0
  emit_marker(cinfo, M_APP14);
454
  
455
0
  emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
456
457
0
  emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
458
0
  emit_byte(cinfo, 0x64);
459
0
  emit_byte(cinfo, 0x6F);
460
0
  emit_byte(cinfo, 0x62);
461
0
  emit_byte(cinfo, 0x65);
462
0
  emit_2bytes(cinfo, 100);  /* Version */
463
0
  emit_2bytes(cinfo, 0);  /* Flags0 */
464
0
  emit_2bytes(cinfo, 0);  /* Flags1 */
465
0
  switch (cinfo->jpeg_color_space) {
466
0
  case JCS_YCbCr:
467
0
    emit_byte(cinfo, 1);  /* Color transform = 1 */
468
0
    break;
469
0
  case JCS_YCCK:
470
0
    emit_byte(cinfo, 2);  /* Color transform = 2 */
471
0
    break;
472
0
  default:
473
0
    emit_byte(cinfo, 0); /* Color transform = 0 */
474
0
  }
475
0
}
476
477
478
/*
479
 * These routines allow writing an arbitrary marker with parameters.
480
 * The only intended use is to emit COM or APPn markers after calling
481
 * write_file_header and before calling write_frame_header.
482
 * Other uses are not guaranteed to produce desirable results.
483
 * Counting the parameter bytes properly is the caller's responsibility.
484
 */
485
486
METHODDEF(void)
487
write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
488
/* Emit an arbitrary marker header */
489
0
{
490
0
  if (datalen > (unsigned int) 65533)   /* safety check */
491
0
    ERREXIT(cinfo, JERR_BAD_LENGTH);
492
493
0
  emit_marker(cinfo, (JPEG_MARKER) marker);
494
495
0
  emit_2bytes(cinfo, (int) (datalen + 2));  /* total length */
496
0
}
497
498
METHODDEF(void)
499
write_marker_byte (j_compress_ptr cinfo, int val)
500
/* Emit one byte of marker parameters following write_marker_header */
501
0
{
502
0
  emit_byte(cinfo, val);
503
0
}
504
505
506
/*
507
 * Write datastream header.
508
 * This consists of an SOI and optional APPn markers.
509
 * We recommend use of the JFIF marker, but not the Adobe marker,
510
 * when using YCbCr or grayscale data.  The JFIF marker is also used
511
 * for other standard JPEG colorspaces.  The Adobe marker is helpful
512
 * to distinguish RGB, CMYK, and YCCK colorspaces.
513
 * Note that an application can write additional header markers after
514
 * jpeg_start_compress returns.
515
 */
516
517
METHODDEF(void)
518
write_file_header (j_compress_ptr cinfo)
519
11.3k
{
520
11.3k
  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
521
522
11.3k
  emit_marker(cinfo, M_SOI);  /* first the SOI */
523
524
  /* SOI is defined to reset restart interval to 0 */
525
11.3k
  marker->last_restart_interval = 0;
526
527
11.3k
  if (cinfo->write_JFIF_header)  /* next an optional JFIF APP0 */
528
0
    emit_jfif_app0(cinfo);
529
11.3k
  if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
530
0
    emit_adobe_app14(cinfo);
531
11.3k
}
532
533
534
/*
535
 * Write frame header.
536
 * This consists of DQT and SOFn markers,
537
 * a conditional LSE marker and a conditional pseudo SOS marker.
538
 * Note that we do not emit the SOF until we have emitted the DQT(s).
539
 * This avoids compatibility problems with incorrect implementations that
540
 * try to error-check the quant table numbers as soon as they see the SOF.
541
 */
542
543
METHODDEF(void)
544
write_frame_header (j_compress_ptr cinfo)
545
10.7k
{
546
10.7k
  int ci, prec;
547
10.7k
  boolean is_baseline;
548
10.7k
  jpeg_component_info *compptr;
549
  
550
  /* Emit DQT for each quantization table.
551
   * Note that emit_dqt() suppresses any duplicate tables.
552
   */
553
10.7k
  prec = 0;
554
32.9k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
555
22.1k
       ci++, compptr++) {
556
22.1k
    prec += emit_dqt(cinfo, compptr->quant_tbl_no);
557
22.1k
  }
558
  /* now prec is nonzero iff there are any 16-bit quant tables. */
559
560
  /* Check for a non-baseline specification.
561
   * Note we assume that Huffman table numbers won't be changed later.
562
   */
563
10.7k
  if (cinfo->arith_code || cinfo->progressive_mode ||
564
10.7k
      cinfo->data_precision != 8 || cinfo->block_size != DCTSIZE) {
565
0
    is_baseline = FALSE;
566
10.7k
  } else {
567
10.7k
    is_baseline = TRUE;
568
32.9k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
569
22.1k
   ci++, compptr++) {
570
22.1k
      if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
571
0
  is_baseline = FALSE;
572
22.1k
    }
573
10.7k
    if (prec && is_baseline) {
574
0
      is_baseline = FALSE;
575
      /* If it's baseline except for quantizer size, warn the user */
576
0
      TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
577
0
    }
578
10.7k
  }
579
580
  /* Emit the proper SOF marker */
581
10.7k
  if (cinfo->arith_code) {
582
0
    if (cinfo->progressive_mode)
583
0
      emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
584
0
    else
585
0
      emit_sof(cinfo, M_SOF9);  /* SOF code for sequential arithmetic */
586
10.7k
  } else {
587
10.7k
    if (cinfo->progressive_mode)
588
0
      emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
589
10.7k
    else if (is_baseline)
590
10.7k
      emit_sof(cinfo, M_SOF0);  /* SOF code for baseline implementation */
591
0
    else
592
0
      emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
593
10.7k
  }
594
595
  /* Check to emit LSE inverse color transform specification marker */
596
10.7k
  if (cinfo->color_transform)
597
0
    emit_lse_ict(cinfo);
598
599
  /* Check to emit pseudo SOS marker */
600
10.7k
  if (cinfo->progressive_mode && cinfo->block_size != DCTSIZE)
601
0
    emit_pseudo_sos(cinfo);
602
10.7k
}
603
604
605
/*
606
 * Write scan header.
607
 * This consists of DHT or DAC markers, optional DRI, and SOS.
608
 * Compressed data will be written following the SOS.
609
 */
610
611
METHODDEF(void)
612
write_scan_header (j_compress_ptr cinfo)
613
10.7k
{
614
10.7k
  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
615
10.7k
  int i;
616
10.7k
  jpeg_component_info *compptr;
617
618
10.7k
  if (cinfo->arith_code) {
619
    /* Emit arith conditioning info.  We may have some duplication
620
     * if the file has multiple scans, but it's so small it's hardly
621
     * worth worrying about.
622
     */
623
0
    emit_dac(cinfo);
624
10.7k
  } else {
625
    /* Emit Huffman tables.
626
     * Note that emit_dht() suppresses any duplicate tables.
627
     */
628
32.9k
    for (i = 0; i < cinfo->comps_in_scan; i++) {
629
22.1k
      compptr = cinfo->cur_comp_info[i];
630
      /* DC needs no table for refinement scan */
631
22.1k
      if (cinfo->Ss == 0 && cinfo->Ah == 0)
632
22.1k
  emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
633
      /* AC needs no table when not present */
634
22.1k
      if (cinfo->Se)
635
22.1k
  emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
636
22.1k
    }
637
10.7k
  }
638
639
  /* Emit DRI if required --- note that DRI value could change for each scan.
640
   * We avoid wasting space with unnecessary DRIs, however.
641
   */
642
10.7k
  if (cinfo->restart_interval != marker->last_restart_interval) {
643
0
    emit_dri(cinfo);
644
0
    marker->last_restart_interval = cinfo->restart_interval;
645
0
  }
646
647
10.7k
  emit_sos(cinfo);
648
10.7k
}
649
650
651
/*
652
 * Write datastream trailer.
653
 */
654
655
METHODDEF(void)
656
write_file_trailer (j_compress_ptr cinfo)
657
10.7k
{
658
10.7k
  emit_marker(cinfo, M_EOI);
659
10.7k
}
660
661
662
/*
663
 * Write an abbreviated table-specification datastream.
664
 * This consists of SOI, DQT and DHT tables, and EOI.
665
 * Any table that is defined and not marked sent_table = TRUE will be
666
 * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
667
 */
668
669
METHODDEF(void)
670
write_tables_only (j_compress_ptr cinfo)
671
0
{
672
0
  int i;
673
674
0
  emit_marker(cinfo, M_SOI);
675
676
0
  for (i = 0; i < NUM_QUANT_TBLS; i++) {
677
0
    if (cinfo->quant_tbl_ptrs[i] != NULL)
678
0
      (void) emit_dqt(cinfo, i);
679
0
  }
680
681
0
  if (! cinfo->arith_code) {
682
0
    for (i = 0; i < NUM_HUFF_TBLS; i++) {
683
0
      if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
684
0
  emit_dht(cinfo, i, FALSE);
685
0
      if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
686
0
  emit_dht(cinfo, i, TRUE);
687
0
    }
688
0
  }
689
690
0
  emit_marker(cinfo, M_EOI);
691
0
}
692
693
694
/*
695
 * Initialize the marker writer module.
696
 */
697
698
GLOBAL(void)
699
jinit_marker_writer (j_compress_ptr cinfo)
700
11.3k
{
701
11.3k
  my_marker_ptr marker;
702
703
  /* Create the subobject */
704
11.3k
  marker = (my_marker_ptr) (*cinfo->mem->alloc_small)
705
11.3k
    ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_marker_writer));
706
11.3k
  cinfo->marker = &marker->pub;
707
  /* Initialize method pointers */
708
11.3k
  marker->pub.write_file_header = write_file_header;
709
11.3k
  marker->pub.write_frame_header = write_frame_header;
710
11.3k
  marker->pub.write_scan_header = write_scan_header;
711
11.3k
  marker->pub.write_file_trailer = write_file_trailer;
712
11.3k
  marker->pub.write_tables_only = write_tables_only;
713
11.3k
  marker->pub.write_marker_header = write_marker_header;
714
11.3k
  marker->pub.write_marker_byte = write_marker_byte;
715
  /* Initialize private state */
716
11.3k
  marker->last_restart_interval = 0;
717
11.3k
}