media/libjpeg/jcmarker.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libjpeg/jcmarker.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,664 @@
     1.4 +/*
     1.5 + * jcmarker.c
     1.6 + *
     1.7 + * This file was part of the Independent JPEG Group's software:
     1.8 + * Copyright (C) 1991-1998, Thomas G. Lane.
     1.9 + * Modified 2003-2010 by Guido Vollbeding.
    1.10 + * libjpeg-turbo Modifications:
    1.11 + * Copyright (C) 2010, D. R. Commander.
    1.12 + * For conditions of distribution and use, see the accompanying README file.
    1.13 + *
    1.14 + * This file contains routines to write JPEG datastream markers.
    1.15 + */
    1.16 +
    1.17 +#define JPEG_INTERNALS
    1.18 +#include "jinclude.h"
    1.19 +#include "jpeglib.h"
    1.20 +#include "jpegcomp.h"
    1.21 +
    1.22 +
    1.23 +typedef enum {			/* JPEG marker codes */
    1.24 +  M_SOF0  = 0xc0,
    1.25 +  M_SOF1  = 0xc1,
    1.26 +  M_SOF2  = 0xc2,
    1.27 +  M_SOF3  = 0xc3,
    1.28 +
    1.29 +  M_SOF5  = 0xc5,
    1.30 +  M_SOF6  = 0xc6,
    1.31 +  M_SOF7  = 0xc7,
    1.32 +
    1.33 +  M_JPG   = 0xc8,
    1.34 +  M_SOF9  = 0xc9,
    1.35 +  M_SOF10 = 0xca,
    1.36 +  M_SOF11 = 0xcb,
    1.37 +
    1.38 +  M_SOF13 = 0xcd,
    1.39 +  M_SOF14 = 0xce,
    1.40 +  M_SOF15 = 0xcf,
    1.41 +
    1.42 +  M_DHT   = 0xc4,
    1.43 +
    1.44 +  M_DAC   = 0xcc,
    1.45 +
    1.46 +  M_RST0  = 0xd0,
    1.47 +  M_RST1  = 0xd1,
    1.48 +  M_RST2  = 0xd2,
    1.49 +  M_RST3  = 0xd3,
    1.50 +  M_RST4  = 0xd4,
    1.51 +  M_RST5  = 0xd5,
    1.52 +  M_RST6  = 0xd6,
    1.53 +  M_RST7  = 0xd7,
    1.54 +
    1.55 +  M_SOI   = 0xd8,
    1.56 +  M_EOI   = 0xd9,
    1.57 +  M_SOS   = 0xda,
    1.58 +  M_DQT   = 0xdb,
    1.59 +  M_DNL   = 0xdc,
    1.60 +  M_DRI   = 0xdd,
    1.61 +  M_DHP   = 0xde,
    1.62 +  M_EXP   = 0xdf,
    1.63 +
    1.64 +  M_APP0  = 0xe0,
    1.65 +  M_APP1  = 0xe1,
    1.66 +  M_APP2  = 0xe2,
    1.67 +  M_APP3  = 0xe3,
    1.68 +  M_APP4  = 0xe4,
    1.69 +  M_APP5  = 0xe5,
    1.70 +  M_APP6  = 0xe6,
    1.71 +  M_APP7  = 0xe7,
    1.72 +  M_APP8  = 0xe8,
    1.73 +  M_APP9  = 0xe9,
    1.74 +  M_APP10 = 0xea,
    1.75 +  M_APP11 = 0xeb,
    1.76 +  M_APP12 = 0xec,
    1.77 +  M_APP13 = 0xed,
    1.78 +  M_APP14 = 0xee,
    1.79 +  M_APP15 = 0xef,
    1.80 +
    1.81 +  M_JPG0  = 0xf0,
    1.82 +  M_JPG13 = 0xfd,
    1.83 +  M_COM   = 0xfe,
    1.84 +
    1.85 +  M_TEM   = 0x01,
    1.86 +
    1.87 +  M_ERROR = 0x100
    1.88 +} JPEG_MARKER;
    1.89 +
    1.90 +
    1.91 +/* Private state */
    1.92 +
    1.93 +typedef struct {
    1.94 +  struct jpeg_marker_writer pub; /* public fields */
    1.95 +
    1.96 +  unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
    1.97 +} my_marker_writer;
    1.98 +
    1.99 +typedef my_marker_writer * my_marker_ptr;
   1.100 +
   1.101 +
   1.102 +/*
   1.103 + * Basic output routines.
   1.104 + *
   1.105 + * Note that we do not support suspension while writing a marker.
   1.106 + * Therefore, an application using suspension must ensure that there is
   1.107 + * enough buffer space for the initial markers (typ. 600-700 bytes) before
   1.108 + * calling jpeg_start_compress, and enough space to write the trailing EOI
   1.109 + * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
   1.110 + * modes are not supported at all with suspension, so those two are the only
   1.111 + * points where markers will be written.
   1.112 + */
   1.113 +
   1.114 +LOCAL(void)
   1.115 +emit_byte (j_compress_ptr cinfo, int val)
   1.116 +/* Emit a byte */
   1.117 +{
   1.118 +  struct jpeg_destination_mgr * dest = cinfo->dest;
   1.119 +
   1.120 +  *(dest->next_output_byte)++ = (JOCTET) val;
   1.121 +  if (--dest->free_in_buffer == 0) {
   1.122 +    if (! (*dest->empty_output_buffer) (cinfo))
   1.123 +      ERREXIT(cinfo, JERR_CANT_SUSPEND);
   1.124 +  }
   1.125 +}
   1.126 +
   1.127 +
   1.128 +LOCAL(void)
   1.129 +emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
   1.130 +/* Emit a marker code */
   1.131 +{
   1.132 +  emit_byte(cinfo, 0xFF);
   1.133 +  emit_byte(cinfo, (int) mark);
   1.134 +}
   1.135 +
   1.136 +
   1.137 +LOCAL(void)
   1.138 +emit_2bytes (j_compress_ptr cinfo, int value)
   1.139 +/* Emit a 2-byte integer; these are always MSB first in JPEG files */
   1.140 +{
   1.141 +  emit_byte(cinfo, (value >> 8) & 0xFF);
   1.142 +  emit_byte(cinfo, value & 0xFF);
   1.143 +}
   1.144 +
   1.145 +
   1.146 +/*
   1.147 + * Routines to write specific marker types.
   1.148 + */
   1.149 +
   1.150 +LOCAL(int)
   1.151 +emit_dqt (j_compress_ptr cinfo, int index)
   1.152 +/* Emit a DQT marker */
   1.153 +/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
   1.154 +{
   1.155 +  JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
   1.156 +  int prec;
   1.157 +  int i;
   1.158 +
   1.159 +  if (qtbl == NULL)
   1.160 +    ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
   1.161 +
   1.162 +  prec = 0;
   1.163 +  for (i = 0; i < DCTSIZE2; i++) {
   1.164 +    if (qtbl->quantval[i] > 255)
   1.165 +      prec = 1;
   1.166 +  }
   1.167 +
   1.168 +  if (! qtbl->sent_table) {
   1.169 +    emit_marker(cinfo, M_DQT);
   1.170 +
   1.171 +    emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
   1.172 +
   1.173 +    emit_byte(cinfo, index + (prec<<4));
   1.174 +
   1.175 +    for (i = 0; i < DCTSIZE2; i++) {
   1.176 +      /* The table entries must be emitted in zigzag order. */
   1.177 +      unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
   1.178 +      if (prec)
   1.179 +	emit_byte(cinfo, (int) (qval >> 8));
   1.180 +      emit_byte(cinfo, (int) (qval & 0xFF));
   1.181 +    }
   1.182 +
   1.183 +    qtbl->sent_table = TRUE;
   1.184 +  }
   1.185 +
   1.186 +  return prec;
   1.187 +}
   1.188 +
   1.189 +
   1.190 +LOCAL(void)
   1.191 +emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
   1.192 +/* Emit a DHT marker */
   1.193 +{
   1.194 +  JHUFF_TBL * htbl;
   1.195 +  int length, i;
   1.196 +  
   1.197 +  if (is_ac) {
   1.198 +    htbl = cinfo->ac_huff_tbl_ptrs[index];
   1.199 +    index += 0x10;		/* output index has AC bit set */
   1.200 +  } else {
   1.201 +    htbl = cinfo->dc_huff_tbl_ptrs[index];
   1.202 +  }
   1.203 +
   1.204 +  if (htbl == NULL)
   1.205 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
   1.206 +  
   1.207 +  if (! htbl->sent_table) {
   1.208 +    emit_marker(cinfo, M_DHT);
   1.209 +    
   1.210 +    length = 0;
   1.211 +    for (i = 1; i <= 16; i++)
   1.212 +      length += htbl->bits[i];
   1.213 +    
   1.214 +    emit_2bytes(cinfo, length + 2 + 1 + 16);
   1.215 +    emit_byte(cinfo, index);
   1.216 +    
   1.217 +    for (i = 1; i <= 16; i++)
   1.218 +      emit_byte(cinfo, htbl->bits[i]);
   1.219 +    
   1.220 +    for (i = 0; i < length; i++)
   1.221 +      emit_byte(cinfo, htbl->huffval[i]);
   1.222 +    
   1.223 +    htbl->sent_table = TRUE;
   1.224 +  }
   1.225 +}
   1.226 +
   1.227 +
   1.228 +LOCAL(void)
   1.229 +emit_dac (j_compress_ptr cinfo)
   1.230 +/* Emit a DAC marker */
   1.231 +/* Since the useful info is so small, we want to emit all the tables in */
   1.232 +/* one DAC marker.  Therefore this routine does its own scan of the table. */
   1.233 +{
   1.234 +#ifdef C_ARITH_CODING_SUPPORTED
   1.235 +  char dc_in_use[NUM_ARITH_TBLS];
   1.236 +  char ac_in_use[NUM_ARITH_TBLS];
   1.237 +  int length, i;
   1.238 +  jpeg_component_info *compptr;
   1.239 +
   1.240 +  for (i = 0; i < NUM_ARITH_TBLS; i++)
   1.241 +    dc_in_use[i] = ac_in_use[i] = 0;
   1.242 +
   1.243 +  for (i = 0; i < cinfo->comps_in_scan; i++) {
   1.244 +    compptr = cinfo->cur_comp_info[i];
   1.245 +    /* DC needs no table for refinement scan */
   1.246 +    if (cinfo->Ss == 0 && cinfo->Ah == 0)
   1.247 +      dc_in_use[compptr->dc_tbl_no] = 1;
   1.248 +    /* AC needs no table when not present */
   1.249 +    if (cinfo->Se)
   1.250 +      ac_in_use[compptr->ac_tbl_no] = 1;
   1.251 +  }
   1.252 +
   1.253 +  length = 0;
   1.254 +  for (i = 0; i < NUM_ARITH_TBLS; i++)
   1.255 +    length += dc_in_use[i] + ac_in_use[i];
   1.256 +
   1.257 +  if (length) {
   1.258 +    emit_marker(cinfo, M_DAC);
   1.259 +
   1.260 +    emit_2bytes(cinfo, length*2 + 2);
   1.261 +
   1.262 +    for (i = 0; i < NUM_ARITH_TBLS; i++) {
   1.263 +      if (dc_in_use[i]) {
   1.264 +	emit_byte(cinfo, i);
   1.265 +	emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
   1.266 +      }
   1.267 +      if (ac_in_use[i]) {
   1.268 +	emit_byte(cinfo, i + 0x10);
   1.269 +	emit_byte(cinfo, cinfo->arith_ac_K[i]);
   1.270 +      }
   1.271 +    }
   1.272 +  }
   1.273 +#endif /* C_ARITH_CODING_SUPPORTED */
   1.274 +}
   1.275 +
   1.276 +
   1.277 +LOCAL(void)
   1.278 +emit_dri (j_compress_ptr cinfo)
   1.279 +/* Emit a DRI marker */
   1.280 +{
   1.281 +  emit_marker(cinfo, M_DRI);
   1.282 +  
   1.283 +  emit_2bytes(cinfo, 4);	/* fixed length */
   1.284 +
   1.285 +  emit_2bytes(cinfo, (int) cinfo->restart_interval);
   1.286 +}
   1.287 +
   1.288 +
   1.289 +LOCAL(void)
   1.290 +emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
   1.291 +/* Emit a SOF marker */
   1.292 +{
   1.293 +  int ci;
   1.294 +  jpeg_component_info *compptr;
   1.295 +  
   1.296 +  emit_marker(cinfo, code);
   1.297 +  
   1.298 +  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
   1.299 +
   1.300 +  /* Make sure image isn't bigger than SOF field can handle */
   1.301 +  if ((long) cinfo->_jpeg_height > 65535L ||
   1.302 +      (long) cinfo->_jpeg_width > 65535L)
   1.303 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
   1.304 +
   1.305 +  emit_byte(cinfo, cinfo->data_precision);
   1.306 +  emit_2bytes(cinfo, (int) cinfo->_jpeg_height);
   1.307 +  emit_2bytes(cinfo, (int) cinfo->_jpeg_width);
   1.308 +
   1.309 +  emit_byte(cinfo, cinfo->num_components);
   1.310 +
   1.311 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.312 +       ci++, compptr++) {
   1.313 +    emit_byte(cinfo, compptr->component_id);
   1.314 +    emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
   1.315 +    emit_byte(cinfo, compptr->quant_tbl_no);
   1.316 +  }
   1.317 +}
   1.318 +
   1.319 +
   1.320 +LOCAL(void)
   1.321 +emit_sos (j_compress_ptr cinfo)
   1.322 +/* Emit a SOS marker */
   1.323 +{
   1.324 +  int i, td, ta;
   1.325 +  jpeg_component_info *compptr;
   1.326 +  
   1.327 +  emit_marker(cinfo, M_SOS);
   1.328 +  
   1.329 +  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
   1.330 +  
   1.331 +  emit_byte(cinfo, cinfo->comps_in_scan);
   1.332 +  
   1.333 +  for (i = 0; i < cinfo->comps_in_scan; i++) {
   1.334 +    compptr = cinfo->cur_comp_info[i];
   1.335 +    emit_byte(cinfo, compptr->component_id);
   1.336 +
   1.337 +    /* We emit 0 for unused field(s); this is recommended by the P&M text
   1.338 +     * but does not seem to be specified in the standard.
   1.339 +     */
   1.340 +
   1.341 +    /* DC needs no table for refinement scan */
   1.342 +    td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
   1.343 +    /* AC needs no table when not present */
   1.344 +    ta = cinfo->Se ? compptr->ac_tbl_no : 0;
   1.345 +
   1.346 +    emit_byte(cinfo, (td << 4) + ta);
   1.347 +  }
   1.348 +
   1.349 +  emit_byte(cinfo, cinfo->Ss);
   1.350 +  emit_byte(cinfo, cinfo->Se);
   1.351 +  emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
   1.352 +}
   1.353 +
   1.354 +
   1.355 +LOCAL(void)
   1.356 +emit_jfif_app0 (j_compress_ptr cinfo)
   1.357 +/* Emit a JFIF-compliant APP0 marker */
   1.358 +{
   1.359 +  /*
   1.360 +   * Length of APP0 block	(2 bytes)
   1.361 +   * Block ID			(4 bytes - ASCII "JFIF")
   1.362 +   * Zero byte			(1 byte to terminate the ID string)
   1.363 +   * Version Major, Minor	(2 bytes - major first)
   1.364 +   * Units			(1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
   1.365 +   * Xdpu			(2 bytes - dots per unit horizontal)
   1.366 +   * Ydpu			(2 bytes - dots per unit vertical)
   1.367 +   * Thumbnail X size		(1 byte)
   1.368 +   * Thumbnail Y size		(1 byte)
   1.369 +   */
   1.370 +  
   1.371 +  emit_marker(cinfo, M_APP0);
   1.372 +  
   1.373 +  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
   1.374 +
   1.375 +  emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
   1.376 +  emit_byte(cinfo, 0x46);
   1.377 +  emit_byte(cinfo, 0x49);
   1.378 +  emit_byte(cinfo, 0x46);
   1.379 +  emit_byte(cinfo, 0);
   1.380 +  emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
   1.381 +  emit_byte(cinfo, cinfo->JFIF_minor_version);
   1.382 +  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
   1.383 +  emit_2bytes(cinfo, (int) cinfo->X_density);
   1.384 +  emit_2bytes(cinfo, (int) cinfo->Y_density);
   1.385 +  emit_byte(cinfo, 0);		/* No thumbnail image */
   1.386 +  emit_byte(cinfo, 0);
   1.387 +}
   1.388 +
   1.389 +
   1.390 +LOCAL(void)
   1.391 +emit_adobe_app14 (j_compress_ptr cinfo)
   1.392 +/* Emit an Adobe APP14 marker */
   1.393 +{
   1.394 +  /*
   1.395 +   * Length of APP14 block	(2 bytes)
   1.396 +   * Block ID			(5 bytes - ASCII "Adobe")
   1.397 +   * Version Number		(2 bytes - currently 100)
   1.398 +   * Flags0			(2 bytes - currently 0)
   1.399 +   * Flags1			(2 bytes - currently 0)
   1.400 +   * Color transform		(1 byte)
   1.401 +   *
   1.402 +   * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
   1.403 +   * now in circulation seem to use Version = 100, so that's what we write.
   1.404 +   *
   1.405 +   * We write the color transform byte as 1 if the JPEG color space is
   1.406 +   * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
   1.407 +   * whether the encoder performed a transformation, which is pretty useless.
   1.408 +   */
   1.409 +  
   1.410 +  emit_marker(cinfo, M_APP14);
   1.411 +  
   1.412 +  emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
   1.413 +
   1.414 +  emit_byte(cinfo, 0x41);	/* Identifier: ASCII "Adobe" */
   1.415 +  emit_byte(cinfo, 0x64);
   1.416 +  emit_byte(cinfo, 0x6F);
   1.417 +  emit_byte(cinfo, 0x62);
   1.418 +  emit_byte(cinfo, 0x65);
   1.419 +  emit_2bytes(cinfo, 100);	/* Version */
   1.420 +  emit_2bytes(cinfo, 0);	/* Flags0 */
   1.421 +  emit_2bytes(cinfo, 0);	/* Flags1 */
   1.422 +  switch (cinfo->jpeg_color_space) {
   1.423 +  case JCS_YCbCr:
   1.424 +    emit_byte(cinfo, 1);	/* Color transform = 1 */
   1.425 +    break;
   1.426 +  case JCS_YCCK:
   1.427 +    emit_byte(cinfo, 2);	/* Color transform = 2 */
   1.428 +    break;
   1.429 +  default:
   1.430 +    emit_byte(cinfo, 0);	/* Color transform = 0 */
   1.431 +    break;
   1.432 +  }
   1.433 +}
   1.434 +
   1.435 +
   1.436 +/*
   1.437 + * These routines allow writing an arbitrary marker with parameters.
   1.438 + * The only intended use is to emit COM or APPn markers after calling
   1.439 + * write_file_header and before calling write_frame_header.
   1.440 + * Other uses are not guaranteed to produce desirable results.
   1.441 + * Counting the parameter bytes properly is the caller's responsibility.
   1.442 + */
   1.443 +
   1.444 +METHODDEF(void)
   1.445 +write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
   1.446 +/* Emit an arbitrary marker header */
   1.447 +{
   1.448 +  if (datalen > (unsigned int) 65533)		/* safety check */
   1.449 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
   1.450 +
   1.451 +  emit_marker(cinfo, (JPEG_MARKER) marker);
   1.452 +
   1.453 +  emit_2bytes(cinfo, (int) (datalen + 2));	/* total length */
   1.454 +}
   1.455 +
   1.456 +METHODDEF(void)
   1.457 +write_marker_byte (j_compress_ptr cinfo, int val)
   1.458 +/* Emit one byte of marker parameters following write_marker_header */
   1.459 +{
   1.460 +  emit_byte(cinfo, val);
   1.461 +}
   1.462 +
   1.463 +
   1.464 +/*
   1.465 + * Write datastream header.
   1.466 + * This consists of an SOI and optional APPn markers.
   1.467 + * We recommend use of the JFIF marker, but not the Adobe marker,
   1.468 + * when using YCbCr or grayscale data.  The JFIF marker should NOT
   1.469 + * be used for any other JPEG colorspace.  The Adobe marker is helpful
   1.470 + * to distinguish RGB, CMYK, and YCCK colorspaces.
   1.471 + * Note that an application can write additional header markers after
   1.472 + * jpeg_start_compress returns.
   1.473 + */
   1.474 +
   1.475 +METHODDEF(void)
   1.476 +write_file_header (j_compress_ptr cinfo)
   1.477 +{
   1.478 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
   1.479 +
   1.480 +  emit_marker(cinfo, M_SOI);	/* first the SOI */
   1.481 +
   1.482 +  /* SOI is defined to reset restart interval to 0 */
   1.483 +  marker->last_restart_interval = 0;
   1.484 +
   1.485 +  if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
   1.486 +    emit_jfif_app0(cinfo);
   1.487 +  if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
   1.488 +    emit_adobe_app14(cinfo);
   1.489 +}
   1.490 +
   1.491 +
   1.492 +/*
   1.493 + * Write frame header.
   1.494 + * This consists of DQT and SOFn markers.
   1.495 + * Note that we do not emit the SOF until we have emitted the DQT(s).
   1.496 + * This avoids compatibility problems with incorrect implementations that
   1.497 + * try to error-check the quant table numbers as soon as they see the SOF.
   1.498 + */
   1.499 +
   1.500 +METHODDEF(void)
   1.501 +write_frame_header (j_compress_ptr cinfo)
   1.502 +{
   1.503 +  int ci, prec;
   1.504 +  boolean is_baseline;
   1.505 +  jpeg_component_info *compptr;
   1.506 +  
   1.507 +  /* Emit DQT for each quantization table.
   1.508 +   * Note that emit_dqt() suppresses any duplicate tables.
   1.509 +   */
   1.510 +  prec = 0;
   1.511 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.512 +       ci++, compptr++) {
   1.513 +    prec += emit_dqt(cinfo, compptr->quant_tbl_no);
   1.514 +  }
   1.515 +  /* now prec is nonzero iff there are any 16-bit quant tables. */
   1.516 +
   1.517 +  /* Check for a non-baseline specification.
   1.518 +   * Note we assume that Huffman table numbers won't be changed later.
   1.519 +   */
   1.520 +  if (cinfo->arith_code || cinfo->progressive_mode ||
   1.521 +      cinfo->data_precision != 8) {
   1.522 +    is_baseline = FALSE;
   1.523 +  } else {
   1.524 +    is_baseline = TRUE;
   1.525 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.526 +	 ci++, compptr++) {
   1.527 +      if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
   1.528 +	is_baseline = FALSE;
   1.529 +    }
   1.530 +    if (prec && is_baseline) {
   1.531 +      is_baseline = FALSE;
   1.532 +      /* If it's baseline except for quantizer size, warn the user */
   1.533 +      TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
   1.534 +    }
   1.535 +  }
   1.536 +
   1.537 +  /* Emit the proper SOF marker */
   1.538 +  if (cinfo->arith_code) {
   1.539 +    if (cinfo->progressive_mode)
   1.540 +      emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
   1.541 +    else
   1.542 +      emit_sof(cinfo, M_SOF9);  /* SOF code for sequential arithmetic */
   1.543 +  } else {
   1.544 +    if (cinfo->progressive_mode)
   1.545 +      emit_sof(cinfo, M_SOF2);	/* SOF code for progressive Huffman */
   1.546 +    else if (is_baseline)
   1.547 +      emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
   1.548 +    else
   1.549 +      emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
   1.550 +  }
   1.551 +}
   1.552 +
   1.553 +
   1.554 +/*
   1.555 + * Write scan header.
   1.556 + * This consists of DHT or DAC markers, optional DRI, and SOS.
   1.557 + * Compressed data will be written following the SOS.
   1.558 + */
   1.559 +
   1.560 +METHODDEF(void)
   1.561 +write_scan_header (j_compress_ptr cinfo)
   1.562 +{
   1.563 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
   1.564 +  int i;
   1.565 +  jpeg_component_info *compptr;
   1.566 +
   1.567 +  if (cinfo->arith_code) {
   1.568 +    /* Emit arith conditioning info.  We may have some duplication
   1.569 +     * if the file has multiple scans, but it's so small it's hardly
   1.570 +     * worth worrying about.
   1.571 +     */
   1.572 +    emit_dac(cinfo);
   1.573 +  } else {
   1.574 +    /* Emit Huffman tables.
   1.575 +     * Note that emit_dht() suppresses any duplicate tables.
   1.576 +     */
   1.577 +    for (i = 0; i < cinfo->comps_in_scan; i++) {
   1.578 +      compptr = cinfo->cur_comp_info[i];
   1.579 +      /* DC needs no table for refinement scan */
   1.580 +      if (cinfo->Ss == 0 && cinfo->Ah == 0)
   1.581 +	emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
   1.582 +      /* AC needs no table when not present */
   1.583 +      if (cinfo->Se)
   1.584 +	emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
   1.585 +    }
   1.586 +  }
   1.587 +
   1.588 +  /* Emit DRI if required --- note that DRI value could change for each scan.
   1.589 +   * We avoid wasting space with unnecessary DRIs, however.
   1.590 +   */
   1.591 +  if (cinfo->restart_interval != marker->last_restart_interval) {
   1.592 +    emit_dri(cinfo);
   1.593 +    marker->last_restart_interval = cinfo->restart_interval;
   1.594 +  }
   1.595 +
   1.596 +  emit_sos(cinfo);
   1.597 +}
   1.598 +
   1.599 +
   1.600 +/*
   1.601 + * Write datastream trailer.
   1.602 + */
   1.603 +
   1.604 +METHODDEF(void)
   1.605 +write_file_trailer (j_compress_ptr cinfo)
   1.606 +{
   1.607 +  emit_marker(cinfo, M_EOI);
   1.608 +}
   1.609 +
   1.610 +
   1.611 +/*
   1.612 + * Write an abbreviated table-specification datastream.
   1.613 + * This consists of SOI, DQT and DHT tables, and EOI.
   1.614 + * Any table that is defined and not marked sent_table = TRUE will be
   1.615 + * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
   1.616 + */
   1.617 +
   1.618 +METHODDEF(void)
   1.619 +write_tables_only (j_compress_ptr cinfo)
   1.620 +{
   1.621 +  int i;
   1.622 +
   1.623 +  emit_marker(cinfo, M_SOI);
   1.624 +
   1.625 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
   1.626 +    if (cinfo->quant_tbl_ptrs[i] != NULL)
   1.627 +      (void) emit_dqt(cinfo, i);
   1.628 +  }
   1.629 +
   1.630 +  if (! cinfo->arith_code) {
   1.631 +    for (i = 0; i < NUM_HUFF_TBLS; i++) {
   1.632 +      if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
   1.633 +	emit_dht(cinfo, i, FALSE);
   1.634 +      if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
   1.635 +	emit_dht(cinfo, i, TRUE);
   1.636 +    }
   1.637 +  }
   1.638 +
   1.639 +  emit_marker(cinfo, M_EOI);
   1.640 +}
   1.641 +
   1.642 +
   1.643 +/*
   1.644 + * Initialize the marker writer module.
   1.645 + */
   1.646 +
   1.647 +GLOBAL(void)
   1.648 +jinit_marker_writer (j_compress_ptr cinfo)
   1.649 +{
   1.650 +  my_marker_ptr marker;
   1.651 +
   1.652 +  /* Create the subobject */
   1.653 +  marker = (my_marker_ptr)
   1.654 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.655 +				SIZEOF(my_marker_writer));
   1.656 +  cinfo->marker = (struct jpeg_marker_writer *) marker;
   1.657 +  /* Initialize method pointers */
   1.658 +  marker->pub.write_file_header = write_file_header;
   1.659 +  marker->pub.write_frame_header = write_frame_header;
   1.660 +  marker->pub.write_scan_header = write_scan_header;
   1.661 +  marker->pub.write_file_trailer = write_file_trailer;
   1.662 +  marker->pub.write_tables_only = write_tables_only;
   1.663 +  marker->pub.write_marker_header = write_marker_header;
   1.664 +  marker->pub.write_marker_byte = write_marker_byte;
   1.665 +  /* Initialize private state */
   1.666 +  marker->last_restart_interval = 0;
   1.667 +}

mercurial