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