kusano 7d535a
/*
kusano 7d535a
 * jcmarker.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1998, Thomas G. Lane.
kusano 7d535a
 * Modified 2003-2012 by Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains routines to write JPEG datastream markers.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
typedef enum {			/* JPEG marker codes */
kusano 7d535a
  M_SOF0  = 0xc0,
kusano 7d535a
  M_SOF1  = 0xc1,
kusano 7d535a
  M_SOF2  = 0xc2,
kusano 7d535a
  M_SOF3  = 0xc3,
kusano 7d535a
kusano 7d535a
  M_SOF5  = 0xc5,
kusano 7d535a
  M_SOF6  = 0xc6,
kusano 7d535a
  M_SOF7  = 0xc7,
kusano 7d535a
kusano 7d535a
  M_JPG   = 0xc8,
kusano 7d535a
  M_SOF9  = 0xc9,
kusano 7d535a
  M_SOF10 = 0xca,
kusano 7d535a
  M_SOF11 = 0xcb,
kusano 7d535a
kusano 7d535a
  M_SOF13 = 0xcd,
kusano 7d535a
  M_SOF14 = 0xce,
kusano 7d535a
  M_SOF15 = 0xcf,
kusano 7d535a
kusano 7d535a
  M_DHT   = 0xc4,
kusano 7d535a
kusano 7d535a
  M_DAC   = 0xcc,
kusano 7d535a
kusano 7d535a
  M_RST0  = 0xd0,
kusano 7d535a
  M_RST1  = 0xd1,
kusano 7d535a
  M_RST2  = 0xd2,
kusano 7d535a
  M_RST3  = 0xd3,
kusano 7d535a
  M_RST4  = 0xd4,
kusano 7d535a
  M_RST5  = 0xd5,
kusano 7d535a
  M_RST6  = 0xd6,
kusano 7d535a
  M_RST7  = 0xd7,
kusano 7d535a
kusano 7d535a
  M_SOI   = 0xd8,
kusano 7d535a
  M_EOI   = 0xd9,
kusano 7d535a
  M_SOS   = 0xda,
kusano 7d535a
  M_DQT   = 0xdb,
kusano 7d535a
  M_DNL   = 0xdc,
kusano 7d535a
  M_DRI   = 0xdd,
kusano 7d535a
  M_DHP   = 0xde,
kusano 7d535a
  M_EXP   = 0xdf,
kusano 7d535a
kusano 7d535a
  M_APP0  = 0xe0,
kusano 7d535a
  M_APP1  = 0xe1,
kusano 7d535a
  M_APP2  = 0xe2,
kusano 7d535a
  M_APP3  = 0xe3,
kusano 7d535a
  M_APP4  = 0xe4,
kusano 7d535a
  M_APP5  = 0xe5,
kusano 7d535a
  M_APP6  = 0xe6,
kusano 7d535a
  M_APP7  = 0xe7,
kusano 7d535a
  M_APP8  = 0xe8,
kusano 7d535a
  M_APP9  = 0xe9,
kusano 7d535a
  M_APP10 = 0xea,
kusano 7d535a
  M_APP11 = 0xeb,
kusano 7d535a
  M_APP12 = 0xec,
kusano 7d535a
  M_APP13 = 0xed,
kusano 7d535a
  M_APP14 = 0xee,
kusano 7d535a
  M_APP15 = 0xef,
kusano 7d535a
kusano 7d535a
  M_JPG0  = 0xf0,
kusano 7d535a
  M_JPG8  = 0xf8,
kusano 7d535a
  M_JPG13 = 0xfd,
kusano 7d535a
  M_COM   = 0xfe,
kusano 7d535a
kusano 7d535a
  M_TEM   = 0x01,
kusano 7d535a
kusano 7d535a
  M_ERROR = 0x100
kusano 7d535a
} JPEG_MARKER;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private state */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_marker_writer pub; /* public fields */
kusano 7d535a
kusano 7d535a
  unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
kusano 7d535a
} my_marker_writer;
kusano 7d535a
kusano 7d535a
typedef my_marker_writer * my_marker_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Basic output routines.
kusano 7d535a
 *
kusano 7d535a
 * Note that we do not support suspension while writing a marker.
kusano 7d535a
 * Therefore, an application using suspension must ensure that there is
kusano 7d535a
 * enough buffer space for the initial markers (typ. 600-700 bytes) before
kusano 7d535a
 * calling jpeg_start_compress, and enough space to write the trailing EOI
kusano 7d535a
 * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
kusano 7d535a
 * modes are not supported at all with suspension, so those two are the only
kusano 7d535a
 * points where markers will be written.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_byte (j_compress_ptr cinfo, int val)
kusano 7d535a
/* Emit a byte */
kusano 7d535a
{
kusano 7d535a
  struct jpeg_destination_mgr * dest = cinfo->dest;
kusano 7d535a
kusano 7d535a
  *(dest->next_output_byte)++ = (JOCTET) val;
kusano 7d535a
  if (--dest->free_in_buffer == 0) {
kusano 7d535a
    if (! (*dest->empty_output_buffer) (cinfo))
kusano 7d535a
      ERREXIT(cinfo, JERR_CANT_SUSPEND);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
kusano 7d535a
/* Emit a marker code */
kusano 7d535a
{
kusano 7d535a
  emit_byte(cinfo, 0xFF);
kusano 7d535a
  emit_byte(cinfo, (int) mark);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_2bytes (j_compress_ptr cinfo, int value)
kusano 7d535a
/* Emit a 2-byte integer; these are always MSB first in JPEG files */
kusano 7d535a
{
kusano 7d535a
  emit_byte(cinfo, (value >> 8) & 0xFF);
kusano 7d535a
  emit_byte(cinfo, value & 0xFF);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Routines to write specific marker types.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(int)
kusano 7d535a
emit_dqt (j_compress_ptr cinfo, int index)
kusano 7d535a
/* Emit a DQT marker */
kusano 7d535a
/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
kusano 7d535a
{
kusano 7d535a
  JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
kusano 7d535a
  int prec;
kusano 7d535a
  int i;
kusano 7d535a
kusano 7d535a
  if (qtbl == NULL)
kusano 7d535a
    ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
kusano 7d535a
kusano 7d535a
  prec = 0;
kusano 7d535a
  for (i = 0; i <= cinfo->lim_Se; i++) {
kusano 7d535a
    if (qtbl->quantval[cinfo->natural_order[i]] > 255)
kusano 7d535a
      prec = 1;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (! qtbl->sent_table) {
kusano 7d535a
    emit_marker(cinfo, M_DQT);
kusano 7d535a
kusano 7d535a
    emit_2bytes(cinfo,
kusano 7d535a
      prec ? cinfo->lim_Se * 2 + 2 + 1 + 2 : cinfo->lim_Se + 1 + 1 + 2);
kusano 7d535a
kusano 7d535a
    emit_byte(cinfo, index + (prec<<4));
kusano 7d535a
kusano 7d535a
    for (i = 0; i <= cinfo->lim_Se; i++) {
kusano 7d535a
      /* The table entries must be emitted in zigzag order. */
kusano 7d535a
      unsigned int qval = qtbl->quantval[cinfo->natural_order[i]];
kusano 7d535a
      if (prec)
kusano 7d535a
	emit_byte(cinfo, (int) (qval >> 8));
kusano 7d535a
      emit_byte(cinfo, (int) (qval & 0xFF));
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    qtbl->sent_table = TRUE;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return prec;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
kusano 7d535a
/* Emit a DHT marker */
kusano 7d535a
{
kusano 7d535a
  JHUFF_TBL * htbl;
kusano 7d535a
  int length, i;
kusano 7d535a
  
kusano 7d535a
  if (is_ac) {
kusano 7d535a
    htbl = cinfo->ac_huff_tbl_ptrs[index];
kusano 7d535a
    index += 0x10;		/* output index has AC bit set */
kusano 7d535a
  } else {
kusano 7d535a
    htbl = cinfo->dc_huff_tbl_ptrs[index];
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (htbl == NULL)
kusano 7d535a
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
kusano 7d535a
  
kusano 7d535a
  if (! htbl->sent_table) {
kusano 7d535a
    emit_marker(cinfo, M_DHT);
kusano 7d535a
    
kusano 7d535a
    length = 0;
kusano 7d535a
    for (i = 1; i <= 16; i++)
kusano 7d535a
      length += htbl->bits[i];
kusano 7d535a
    
kusano 7d535a
    emit_2bytes(cinfo, length + 2 + 1 + 16);
kusano 7d535a
    emit_byte(cinfo, index);
kusano 7d535a
    
kusano 7d535a
    for (i = 1; i <= 16; i++)
kusano 7d535a
      emit_byte(cinfo, htbl->bits[i]);
kusano 7d535a
    
kusano 7d535a
    for (i = 0; i < length; i++)
kusano 7d535a
      emit_byte(cinfo, htbl->huffval[i]);
kusano 7d535a
    
kusano 7d535a
    htbl->sent_table = TRUE;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_dac (j_compress_ptr cinfo)
kusano 7d535a
/* Emit a DAC marker */
kusano 7d535a
/* Since the useful info is so small, we want to emit all the tables in */
kusano 7d535a
/* one DAC marker.  Therefore this routine does its own scan of the table. */
kusano 7d535a
{
kusano 7d535a
#ifdef C_ARITH_CODING_SUPPORTED
kusano 7d535a
  char dc_in_use[NUM_ARITH_TBLS];
kusano 7d535a
  char ac_in_use[NUM_ARITH_TBLS];
kusano 7d535a
  int length, i;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  for (i = 0; i < NUM_ARITH_TBLS; i++)
kusano 7d535a
    dc_in_use[i] = ac_in_use[i] = 0;
kusano 7d535a
kusano 7d535a
  for (i = 0; i < cinfo->comps_in_scan; i++) {
kusano 7d535a
    compptr = cinfo->cur_comp_info[i];
kusano 7d535a
    /* DC needs no table for refinement scan */
kusano 7d535a
    if (cinfo->Ss == 0 && cinfo->Ah == 0)
kusano 7d535a
      dc_in_use[compptr->dc_tbl_no] = 1;
kusano 7d535a
    /* AC needs no table when not present */
kusano 7d535a
    if (cinfo->Se)
kusano 7d535a
      ac_in_use[compptr->ac_tbl_no] = 1;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  length = 0;
kusano 7d535a
  for (i = 0; i < NUM_ARITH_TBLS; i++)
kusano 7d535a
    length += dc_in_use[i] + ac_in_use[i];
kusano 7d535a
kusano 7d535a
  if (length) {
kusano 7d535a
    emit_marker(cinfo, M_DAC);
kusano 7d535a
kusano 7d535a
    emit_2bytes(cinfo, length*2 + 2);
kusano 7d535a
kusano 7d535a
    for (i = 0; i < NUM_ARITH_TBLS; i++) {
kusano 7d535a
      if (dc_in_use[i]) {
kusano 7d535a
	emit_byte(cinfo, i);
kusano 7d535a
	emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
kusano 7d535a
      }
kusano 7d535a
      if (ac_in_use[i]) {
kusano 7d535a
	emit_byte(cinfo, i + 0x10);
kusano 7d535a
	emit_byte(cinfo, cinfo->arith_ac_K[i]);
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
#endif /* C_ARITH_CODING_SUPPORTED */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_dri (j_compress_ptr cinfo)
kusano 7d535a
/* Emit a DRI marker */
kusano 7d535a
{
kusano 7d535a
  emit_marker(cinfo, M_DRI);
kusano 7d535a
  
kusano 7d535a
  emit_2bytes(cinfo, 4);	/* fixed length */
kusano 7d535a
kusano 7d535a
  emit_2bytes(cinfo, (int) cinfo->restart_interval);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_lse_ict (j_compress_ptr cinfo)
kusano 7d535a
/* Emit an LSE inverse color transform specification marker */
kusano 7d535a
{
kusano 7d535a
  /* Support only 1 transform */
kusano 7d535a
  if (cinfo->color_transform != JCT_SUBTRACT_GREEN ||
kusano 7d535a
      cinfo->num_components < 3)
kusano 7d535a
    ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
kusano 7d535a
kusano 7d535a
  emit_marker(cinfo, M_JPG8);
kusano 7d535a
  
kusano 7d535a
  emit_2bytes(cinfo, 24);	/* fixed length */
kusano 7d535a
kusano 7d535a
  emit_byte(cinfo, 0x0D);	/* ID inverse transform specification */
kusano 7d535a
  emit_2bytes(cinfo, MAXJSAMPLE);	/* MAXTRANS */
kusano 7d535a
  emit_byte(cinfo, 3);		/* Nt=3 */
kusano 7d535a
  emit_byte(cinfo, cinfo->comp_info[1].component_id);
kusano 7d535a
  emit_byte(cinfo, cinfo->comp_info[0].component_id);
kusano 7d535a
  emit_byte(cinfo, cinfo->comp_info[2].component_id);
kusano 7d535a
  emit_byte(cinfo, 0x80);	/* F1: CENTER1=1, NORM1=0 */
kusano 7d535a
  emit_2bytes(cinfo, 0);	/* A(1,1)=0 */
kusano 7d535a
  emit_2bytes(cinfo, 0);	/* A(1,2)=0 */
kusano 7d535a
  emit_byte(cinfo, 0);		/* F2: CENTER2=0, NORM2=0 */
kusano 7d535a
  emit_2bytes(cinfo, 1);	/* A(2,1)=1 */
kusano 7d535a
  emit_2bytes(cinfo, 0);	/* A(2,2)=0 */
kusano 7d535a
  emit_byte(cinfo, 0);		/* F3: CENTER3=0, NORM3=0 */
kusano 7d535a
  emit_2bytes(cinfo, 1);	/* A(3,1)=1 */
kusano 7d535a
  emit_2bytes(cinfo, 0);	/* A(3,2)=0 */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
kusano 7d535a
/* Emit a SOF marker */
kusano 7d535a
{
kusano 7d535a
  int ci;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  
kusano 7d535a
  emit_marker(cinfo, code);
kusano 7d535a
  
kusano 7d535a
  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
kusano 7d535a
kusano 7d535a
  /* Make sure image isn't bigger than SOF field can handle */
kusano 7d535a
  if ((long) cinfo->jpeg_height > 65535L ||
kusano 7d535a
      (long) cinfo->jpeg_width > 65535L)
kusano 7d535a
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
kusano 7d535a
kusano 7d535a
  emit_byte(cinfo, cinfo->data_precision);
kusano 7d535a
  emit_2bytes(cinfo, (int) cinfo->jpeg_height);
kusano 7d535a
  emit_2bytes(cinfo, (int) cinfo->jpeg_width);
kusano 7d535a
kusano 7d535a
  emit_byte(cinfo, cinfo->num_components);
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    emit_byte(cinfo, compptr->component_id);
kusano 7d535a
    emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
kusano 7d535a
    emit_byte(cinfo, compptr->quant_tbl_no);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_sos (j_compress_ptr cinfo)
kusano 7d535a
/* Emit a SOS marker */
kusano 7d535a
{
kusano 7d535a
  int i, td, ta;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  
kusano 7d535a
  emit_marker(cinfo, M_SOS);
kusano 7d535a
  
kusano 7d535a
  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
kusano 7d535a
  
kusano 7d535a
  emit_byte(cinfo, cinfo->comps_in_scan);
kusano 7d535a
  
kusano 7d535a
  for (i = 0; i < cinfo->comps_in_scan; i++) {
kusano 7d535a
    compptr = cinfo->cur_comp_info[i];
kusano 7d535a
    emit_byte(cinfo, compptr->component_id);
kusano 7d535a
kusano 7d535a
    /* We emit 0 for unused field(s); this is recommended by the P&M text
kusano 7d535a
     * but does not seem to be specified in the standard.
kusano 7d535a
     */
kusano 7d535a
kusano 7d535a
    /* DC needs no table for refinement scan */
kusano 7d535a
    td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
kusano 7d535a
    /* AC needs no table when not present */
kusano 7d535a
    ta = cinfo->Se ? compptr->ac_tbl_no : 0;
kusano 7d535a
kusano 7d535a
    emit_byte(cinfo, (td << 4) + ta);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  emit_byte(cinfo, cinfo->Ss);
kusano 7d535a
  emit_byte(cinfo, cinfo->Se);
kusano 7d535a
  emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_pseudo_sos (j_compress_ptr cinfo)
kusano 7d535a
/* Emit a pseudo SOS marker */
kusano 7d535a
{
kusano 7d535a
  emit_marker(cinfo, M_SOS);
kusano 7d535a
  
kusano 7d535a
  emit_2bytes(cinfo, 2 + 1 + 3); /* length */
kusano 7d535a
  
kusano 7d535a
  emit_byte(cinfo, 0); /* Ns */
kusano 7d535a
kusano 7d535a
  emit_byte(cinfo, 0); /* Ss */
kusano 7d535a
  emit_byte(cinfo, cinfo->block_size * cinfo->block_size - 1); /* Se */
kusano 7d535a
  emit_byte(cinfo, 0); /* Ah/Al */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_jfif_app0 (j_compress_ptr cinfo)
kusano 7d535a
/* Emit a JFIF-compliant APP0 marker */
kusano 7d535a
{
kusano 7d535a
  /*
kusano 7d535a
   * Length of APP0 block	(2 bytes)
kusano 7d535a
   * Block ID			(4 bytes - ASCII "JFIF")
kusano 7d535a
   * Zero byte			(1 byte to terminate the ID string)
kusano 7d535a
   * Version Major, Minor	(2 bytes - major first)
kusano 7d535a
   * Units			(1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
kusano 7d535a
   * Xdpu			(2 bytes - dots per unit horizontal)
kusano 7d535a
   * Ydpu			(2 bytes - dots per unit vertical)
kusano 7d535a
   * Thumbnail X size		(1 byte)
kusano 7d535a
   * Thumbnail Y size		(1 byte)
kusano 7d535a
   */
kusano 7d535a
  
kusano 7d535a
  emit_marker(cinfo, M_APP0);
kusano 7d535a
  
kusano 7d535a
  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
kusano 7d535a
kusano 7d535a
  emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
kusano 7d535a
  emit_byte(cinfo, 0x46);
kusano 7d535a
  emit_byte(cinfo, 0x49);
kusano 7d535a
  emit_byte(cinfo, 0x46);
kusano 7d535a
  emit_byte(cinfo, 0);
kusano 7d535a
  emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
kusano 7d535a
  emit_byte(cinfo, cinfo->JFIF_minor_version);
kusano 7d535a
  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
kusano 7d535a
  emit_2bytes(cinfo, (int) cinfo->X_density);
kusano 7d535a
  emit_2bytes(cinfo, (int) cinfo->Y_density);
kusano 7d535a
  emit_byte(cinfo, 0);		/* No thumbnail image */
kusano 7d535a
  emit_byte(cinfo, 0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
emit_adobe_app14 (j_compress_ptr cinfo)
kusano 7d535a
/* Emit an Adobe APP14 marker */
kusano 7d535a
{
kusano 7d535a
  /*
kusano 7d535a
   * Length of APP14 block	(2 bytes)
kusano 7d535a
   * Block ID			(5 bytes - ASCII "Adobe")
kusano 7d535a
   * Version Number		(2 bytes - currently 100)
kusano 7d535a
   * Flags0			(2 bytes - currently 0)
kusano 7d535a
   * Flags1			(2 bytes - currently 0)
kusano 7d535a
   * Color transform		(1 byte)
kusano 7d535a
   *
kusano 7d535a
   * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
kusano 7d535a
   * now in circulation seem to use Version = 100, so that's what we write.
kusano 7d535a
   *
kusano 7d535a
   * We write the color transform byte as 1 if the JPEG color space is
kusano 7d535a
   * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
kusano 7d535a
   * whether the encoder performed a transformation, which is pretty useless.
kusano 7d535a
   */
kusano 7d535a
  
kusano 7d535a
  emit_marker(cinfo, M_APP14);
kusano 7d535a
  
kusano 7d535a
  emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
kusano 7d535a
kusano 7d535a
  emit_byte(cinfo, 0x41);	/* Identifier: ASCII "Adobe" */
kusano 7d535a
  emit_byte(cinfo, 0x64);
kusano 7d535a
  emit_byte(cinfo, 0x6F);
kusano 7d535a
  emit_byte(cinfo, 0x62);
kusano 7d535a
  emit_byte(cinfo, 0x65);
kusano 7d535a
  emit_2bytes(cinfo, 100);	/* Version */
kusano 7d535a
  emit_2bytes(cinfo, 0);	/* Flags0 */
kusano 7d535a
  emit_2bytes(cinfo, 0);	/* Flags1 */
kusano 7d535a
  switch (cinfo->jpeg_color_space) {
kusano 7d535a
  case JCS_YCbCr:
kusano 7d535a
    emit_byte(cinfo, 1);	/* Color transform = 1 */
kusano 7d535a
    break;
kusano 7d535a
  case JCS_YCCK:
kusano 7d535a
    emit_byte(cinfo, 2);	/* Color transform = 2 */
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    emit_byte(cinfo, 0);	/* Color transform = 0 */
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * These routines allow writing an arbitrary marker with parameters.
kusano 7d535a
 * The only intended use is to emit COM or APPn markers after calling
kusano 7d535a
 * write_file_header and before calling write_frame_header.
kusano 7d535a
 * Other uses are not guaranteed to produce desirable results.
kusano 7d535a
 * Counting the parameter bytes properly is the caller's responsibility.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
kusano 7d535a
/* Emit an arbitrary marker header */
kusano 7d535a
{
kusano 7d535a
  if (datalen > (unsigned int) 65533)		/* safety check */
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_LENGTH);
kusano 7d535a
kusano 7d535a
  emit_marker(cinfo, (JPEG_MARKER) marker);
kusano 7d535a
kusano 7d535a
  emit_2bytes(cinfo, (int) (datalen + 2));	/* total length */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
write_marker_byte (j_compress_ptr cinfo, int val)
kusano 7d535a
/* Emit one byte of marker parameters following write_marker_header */
kusano 7d535a
{
kusano 7d535a
  emit_byte(cinfo, val);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write datastream header.
kusano 7d535a
 * This consists of an SOI and optional APPn markers.
kusano 7d535a
 * We recommend use of the JFIF marker, but not the Adobe marker,
kusano 7d535a
 * when using YCbCr or grayscale data.  The JFIF marker should NOT
kusano 7d535a
 * be used for any other JPEG colorspace.  The Adobe marker is helpful
kusano 7d535a
 * to distinguish RGB, CMYK, and YCCK colorspaces.
kusano 7d535a
 * Note that an application can write additional header markers after
kusano 7d535a
 * jpeg_start_compress returns.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
write_file_header (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
kusano 7d535a
kusano 7d535a
  emit_marker(cinfo, M_SOI);	/* first the SOI */
kusano 7d535a
kusano 7d535a
  /* SOI is defined to reset restart interval to 0 */
kusano 7d535a
  marker->last_restart_interval = 0;
kusano 7d535a
kusano 7d535a
  if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
kusano 7d535a
    emit_jfif_app0(cinfo);
kusano 7d535a
  if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
kusano 7d535a
    emit_adobe_app14(cinfo);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write frame header.
kusano 7d535a
 * This consists of DQT and SOFn markers,
kusano 7d535a
 * a conditional LSE marker and a conditional pseudo SOS marker.
kusano 7d535a
 * Note that we do not emit the SOF until we have emitted the DQT(s).
kusano 7d535a
 * This avoids compatibility problems with incorrect implementations that
kusano 7d535a
 * try to error-check the quant table numbers as soon as they see the SOF.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
write_frame_header (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  int ci, prec;
kusano 7d535a
  boolean is_baseline;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  
kusano 7d535a
  /* Emit DQT for each quantization table.
kusano 7d535a
   * Note that emit_dqt() suppresses any duplicate tables.
kusano 7d535a
   */
kusano 7d535a
  prec = 0;
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    prec += emit_dqt(cinfo, compptr->quant_tbl_no);
kusano 7d535a
  }
kusano 7d535a
  /* now prec is nonzero iff there are any 16-bit quant tables. */
kusano 7d535a
kusano 7d535a
  /* Check for a non-baseline specification.
kusano 7d535a
   * Note we assume that Huffman table numbers won't be changed later.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->arith_code || cinfo->progressive_mode ||
kusano 7d535a
      cinfo->data_precision != 8 || cinfo->block_size != DCTSIZE) {
kusano 7d535a
    is_baseline = FALSE;
kusano 7d535a
  } else {
kusano 7d535a
    is_baseline = TRUE;
kusano 7d535a
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
	 ci++, compptr++) {
kusano 7d535a
      if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
kusano 7d535a
	is_baseline = FALSE;
kusano 7d535a
    }
kusano 7d535a
    if (prec && is_baseline) {
kusano 7d535a
      is_baseline = FALSE;
kusano 7d535a
      /* If it's baseline except for quantizer size, warn the user */
kusano 7d535a
      TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Emit the proper SOF marker */
kusano 7d535a
  if (cinfo->arith_code) {
kusano 7d535a
    if (cinfo->progressive_mode)
kusano 7d535a
      emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
kusano 7d535a
    else
kusano 7d535a
      emit_sof(cinfo, M_SOF9);  /* SOF code for sequential arithmetic */
kusano 7d535a
  } else {
kusano 7d535a
    if (cinfo->progressive_mode)
kusano 7d535a
      emit_sof(cinfo, M_SOF2);	/* SOF code for progressive Huffman */
kusano 7d535a
    else if (is_baseline)
kusano 7d535a
      emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
kusano 7d535a
    else
kusano 7d535a
      emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Check to emit LSE inverse color transform specification marker */
kusano 7d535a
  if (cinfo->color_transform)
kusano 7d535a
    emit_lse_ict(cinfo);
kusano 7d535a
kusano 7d535a
  /* Check to emit pseudo SOS marker */
kusano 7d535a
  if (cinfo->progressive_mode && cinfo->block_size != DCTSIZE)
kusano 7d535a
    emit_pseudo_sos(cinfo);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write scan header.
kusano 7d535a
 * This consists of DHT or DAC markers, optional DRI, and SOS.
kusano 7d535a
 * Compressed data will be written following the SOS.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
write_scan_header (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
kusano 7d535a
  int i;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  if (cinfo->arith_code) {
kusano 7d535a
    /* Emit arith conditioning info.  We may have some duplication
kusano 7d535a
     * if the file has multiple scans, but it's so small it's hardly
kusano 7d535a
     * worth worrying about.
kusano 7d535a
     */
kusano 7d535a
    emit_dac(cinfo);
kusano 7d535a
  } else {
kusano 7d535a
    /* Emit Huffman tables.
kusano 7d535a
     * Note that emit_dht() suppresses any duplicate tables.
kusano 7d535a
     */
kusano 7d535a
    for (i = 0; i < cinfo->comps_in_scan; i++) {
kusano 7d535a
      compptr = cinfo->cur_comp_info[i];
kusano 7d535a
      /* DC needs no table for refinement scan */
kusano 7d535a
      if (cinfo->Ss == 0 && cinfo->Ah == 0)
kusano 7d535a
	emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
kusano 7d535a
      /* AC needs no table when not present */
kusano 7d535a
      if (cinfo->Se)
kusano 7d535a
	emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Emit DRI if required --- note that DRI value could change for each scan.
kusano 7d535a
   * We avoid wasting space with unnecessary DRIs, however.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->restart_interval != marker->last_restart_interval) {
kusano 7d535a
    emit_dri(cinfo);
kusano 7d535a
    marker->last_restart_interval = cinfo->restart_interval;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  emit_sos(cinfo);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write datastream trailer.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
write_file_trailer (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  emit_marker(cinfo, M_EOI);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write an abbreviated table-specification datastream.
kusano 7d535a
 * This consists of SOI, DQT and DHT tables, and EOI.
kusano 7d535a
 * Any table that is defined and not marked sent_table = TRUE will be
kusano 7d535a
 * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
write_tables_only (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  int i;
kusano 7d535a
kusano 7d535a
  emit_marker(cinfo, M_SOI);
kusano 7d535a
kusano 7d535a
  for (i = 0; i < NUM_QUANT_TBLS; i++) {
kusano 7d535a
    if (cinfo->quant_tbl_ptrs[i] != NULL)
kusano 7d535a
      (void) emit_dqt(cinfo, i);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (! cinfo->arith_code) {
kusano 7d535a
    for (i = 0; i < NUM_HUFF_TBLS; i++) {
kusano 7d535a
      if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
kusano 7d535a
	emit_dht(cinfo, i, FALSE);
kusano 7d535a
      if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
kusano 7d535a
	emit_dht(cinfo, i, TRUE);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  emit_marker(cinfo, M_EOI);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize the marker writer module.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_marker_writer (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_marker_ptr marker;
kusano 7d535a
kusano 7d535a
  /* Create the subobject */
kusano 7d535a
  marker = (my_marker_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(my_marker_writer));
kusano 7d535a
  cinfo->marker = &marker->pub;
kusano 7d535a
  /* Initialize method pointers */
kusano 7d535a
  marker->pub.write_file_header = write_file_header;
kusano 7d535a
  marker->pub.write_frame_header = write_frame_header;
kusano 7d535a
  marker->pub.write_scan_header = write_scan_header;
kusano 7d535a
  marker->pub.write_file_trailer = write_file_trailer;
kusano 7d535a
  marker->pub.write_tables_only = write_tables_only;
kusano 7d535a
  marker->pub.write_marker_header = write_marker_header;
kusano 7d535a
  marker->pub.write_marker_byte = write_marker_byte;
kusano 7d535a
  /* Initialize private state */
kusano 7d535a
  marker->last_restart_interval = 0;
kusano 7d535a
}