1793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
2793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * jdapimin.c
3793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
4793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Copyright (C) 1994-1998, Thomas G. Lane.
5793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Modified 2009 by Guido Vollbeding.
6793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * This file is part of the Independent JPEG Group's software.
7793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * For conditions of distribution and use, see the accompanying README file.
8793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
9793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * This file contains application interface code for the decompression half
10793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * of the JPEG library.  These are the "minimum" API routines that may be
11793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * needed in either the normal full-decompression case or the
12793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * transcoding-only case.
13793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
14793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Most of the routines intended to be called directly by an application
15793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
16793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * shared by compression and decompression, and jdtrans.c for the transcoding
17793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * case.
18793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
19793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
20793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define JPEG_INTERNALS
21793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "jinclude.h"
22793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "jpeglib.h"
23793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
24793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
25793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
26793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Initialization of a JPEG decompression object.
27793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * The error manager must already be set up (in case memory manager fails).
28793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
29793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
30793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerGLOBAL(void)
31793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerjpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
32793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
33793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int i;
34793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
35793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Guard against version mismatches between library and caller. */
36793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
37793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  if (version != JPEG_LIB_VERSION)
38793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
39793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  if (structsize != SIZEOF(struct jpeg_decompress_struct))
40793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
41793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler             (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
42793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
43793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* For debugging purposes, we zero the whole master structure.
44793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler   * But the application has already set the err pointer, and may have set
45793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler   * client_data, so we have to save and restore those fields.
46793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler   * Note: if application hasn't set client_data, tools like Purify may
47793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler   * complain here.
48793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler   */
49793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  {
50793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    struct jpeg_error_mgr * err = cinfo->err;
51793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
52793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
53793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->err = err;
54793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->client_data = client_data;
55793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  }
56793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->is_decompressor = TRUE;
57793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
58793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Initialize a memory manager instance for this object */
59793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  jinit_memory_mgr((j_common_ptr) cinfo);
60793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
61793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Zero out pointers to permanent structures. */
62793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->progress = NULL;
63793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->src = NULL;
64793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
65793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  for (i = 0; i < NUM_QUANT_TBLS; i++)
66793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->quant_tbl_ptrs[i] = NULL;
67793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
68793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  for (i = 0; i < NUM_HUFF_TBLS; i++) {
69793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->dc_huff_tbl_ptrs[i] = NULL;
70793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->ac_huff_tbl_ptrs[i] = NULL;
71793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  }
72793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
73793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Initialize marker processor so application can override methods
74793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler   * for COM, APPn markers before calling jpeg_read_header.
75793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler   */
76793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->marker_list = NULL;
77793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  jinit_marker_reader(cinfo);
78793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
79793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* And initialize the overall input controller. */
80793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  jinit_input_controller(cinfo);
81793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
82793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* OK, I'm ready */
83793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->global_state = DSTATE_START;
84793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
85793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
86793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
87793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
88793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Destruction of a JPEG decompression object
89793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
90793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
91793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerGLOBAL(void)
92793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerjpeg_destroy_decompress (j_decompress_ptr cinfo)
93793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
94793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
95793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
96793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
97793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
98793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
99793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Abort processing of a JPEG decompression operation,
100793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * but don't destroy the object itself.
101793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
102793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
103793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerGLOBAL(void)
104793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerjpeg_abort_decompress (j_decompress_ptr cinfo)
105793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
106793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
107793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
108793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
109793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
110793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
111793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Set default decompression parameters.
112793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
113793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
114793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerLOCAL(void)
115793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerdefault_decompress_parms (j_decompress_ptr cinfo)
116793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
117793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Guess the input colorspace, and set output colorspace accordingly. */
118793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* (Wish JPEG committee had provided a real way to specify this...) */
119793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Note application may override our guesses. */
120793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  switch (cinfo->num_components) {
121793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case 1:
122793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->jpeg_color_space = JCS_GRAYSCALE;
123793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->out_color_space = JCS_GRAYSCALE;
124793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
125793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
126793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case 3:
127793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if (cinfo->saw_JFIF_marker) {
128793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
129793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } else if (cinfo->saw_Adobe_marker) {
130793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      switch (cinfo->Adobe_transform) {
131793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      case 0:
132793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cinfo->jpeg_color_space = JCS_RGB;
133793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        break;
134793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      case 1:
135793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cinfo->jpeg_color_space = JCS_YCbCr;
136793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        break;
137793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      default:
138793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
139793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
140793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        break;
141793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      }
142793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } else {
143793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      /* Saw no special markers, try to guess from the component IDs */
144793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      int cid0 = cinfo->comp_info[0].component_id;
145793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      int cid1 = cinfo->comp_info[1].component_id;
146793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      int cid2 = cinfo->comp_info[2].component_id;
147793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
148793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      if (cid0 == 1 && cid1 == 2 && cid2 == 3)
149793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
150793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
151793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
152793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      else {
153793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
154793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
155793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      }
156793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
157793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* Always guess RGB is proper output colorspace. */
158793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->out_color_space = JCS_RGB;
159793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
160793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
161793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case 4:
162793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if (cinfo->saw_Adobe_marker) {
163793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      switch (cinfo->Adobe_transform) {
164793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      case 0:
165793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cinfo->jpeg_color_space = JCS_CMYK;
166793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        break;
167793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      case 2:
168793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cinfo->jpeg_color_space = JCS_YCCK;
169793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        break;
170793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      default:
171793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
172793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
173793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        break;
174793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      }
175793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } else {
176793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      /* No special markers, assume straight CMYK. */
177793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      cinfo->jpeg_color_space = JCS_CMYK;
178793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
179793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->out_color_space = JCS_CMYK;
180793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
181793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
182793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  default:
183793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->jpeg_color_space = JCS_UNKNOWN;
184793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->out_color_space = JCS_UNKNOWN;
185793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
186793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  }
187793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
188793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Set defaults for other decompression parameters. */
189793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->scale_num = cinfo->block_size;		/* 1:1 scaling */
190793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->scale_denom = cinfo->block_size;
191793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->output_gamma = 1.0;
192793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->buffered_image = FALSE;
193793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->raw_data_out = FALSE;
194793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->dct_method = JDCT_DEFAULT;
195793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->do_fancy_upsampling = TRUE;
196793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->do_block_smoothing = TRUE;
197793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->quantize_colors = FALSE;
198793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* We set these in case application only sets quantize_colors. */
199793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->dither_mode = JDITHER_FS;
200793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#ifdef QUANT_2PASS_SUPPORTED
201793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->two_pass_quantize = TRUE;
202793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#else
203793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->two_pass_quantize = FALSE;
204793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif
205793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->desired_number_of_colors = 256;
206793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->colormap = NULL;
207793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Initialize for no mode change in buffered-image mode. */
208793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->enable_1pass_quant = FALSE;
209793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->enable_external_quant = FALSE;
210793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cinfo->enable_2pass_quant = FALSE;
211793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
212793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
213793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
214793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
215793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Decompression startup: read start of JPEG datastream to see what's there.
216793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Need only initialize JPEG object and supply a data source before calling.
217793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
218793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * This routine will read as far as the first SOS marker (ie, actual start of
219793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * compressed data), and will save all tables and parameters in the JPEG
220793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * object.  It will also initialize the decompression parameters to default
221793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * values, and finally return JPEG_HEADER_OK.  On return, the application may
222793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * adjust the decompression parameters and then call jpeg_start_decompress.
223793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * (Or, if the application only wanted to determine the image parameters,
224793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * the data need not be decompressed.  In that case, call jpeg_abort or
225793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * jpeg_destroy to release any temporary space.)
226793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * If an abbreviated (tables only) datastream is presented, the routine will
227793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
228793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * re-use the JPEG object to read the abbreviated image datastream(s).
229793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * It is unnecessary (but OK) to call jpeg_abort in this case.
230793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * The JPEG_SUSPENDED return code only occurs if the data source module
231793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * requests suspension of the decompressor.  In this case the application
232793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * should load more source data and then re-call jpeg_read_header to resume
233793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * processing.
234793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * If a non-suspending data source is used and require_image is TRUE, then the
235793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * return code need not be inspected since only JPEG_HEADER_OK is possible.
236793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
237793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * This routine is now just a front end to jpeg_consume_input, with some
238793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * extra error checking.
239793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
240793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
241793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerGLOBAL(int)
242793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerjpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
243793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
244793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int retcode;
245793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
246793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  if (cinfo->global_state != DSTATE_START &&
247793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      cinfo->global_state != DSTATE_INHEADER)
248793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
249793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
250793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  retcode = jpeg_consume_input(cinfo);
251793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
252793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  switch (retcode) {
253793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case JPEG_REACHED_SOS:
254793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    retcode = JPEG_HEADER_OK;
255793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
256793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case JPEG_REACHED_EOI:
257793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if (require_image)		/* Complain if application wanted an image */
258793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      ERREXIT(cinfo, JERR_NO_IMAGE);
259793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* Reset to start state; it would be safer to require the application to
260793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler     * call jpeg_abort, but we can't change it now for compatibility reasons.
261793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler     * A side effect is to free any temporary memory (there shouldn't be any).
262793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler     */
263793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
264793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    retcode = JPEG_HEADER_TABLES_ONLY;
265793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
266793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case JPEG_SUSPENDED:
267793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* no work */
268793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
269793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  }
270793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
271793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  return retcode;
272793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
273793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
274793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
275793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
276793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Consume data in advance of what the decompressor requires.
277793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * This can be called at any time once the decompressor object has
278793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * been created and a data source has been set up.
279793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
280793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * This routine is essentially a state machine that handles a couple
281793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * of critical state-transition actions, namely initial setup and
282793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * transition from header scanning to ready-for-start_decompress.
283793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * All the actual input is done via the input controller's consume_input
284793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * method.
285793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
286793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
287793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerGLOBAL(int)
288793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerjpeg_consume_input (j_decompress_ptr cinfo)
289793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
290793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int retcode = JPEG_SUSPENDED;
291793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
292793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* NB: every possible DSTATE value should be listed in this switch */
293793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  switch (cinfo->global_state) {
294793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_START:
295793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* Start-of-datastream actions: reset appropriate modules */
296793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    (*cinfo->inputctl->reset_input_controller) (cinfo);
297793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* Initialize application's data source module */
298793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    (*cinfo->src->init_source) (cinfo);
299793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->global_state = DSTATE_INHEADER;
300793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /*FALLTHROUGH*/
301793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_INHEADER:
302793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    retcode = (*cinfo->inputctl->consume_input) (cinfo);
303793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
304793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      /* Set up default parameters based on header data */
305793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      default_decompress_parms(cinfo);
306793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      /* Set global state: ready for start_decompress */
307793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      cinfo->global_state = DSTATE_READY;
308793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
309793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
310793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_READY:
311793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* Can't advance past first SOS until start_decompress is called */
312793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    retcode = JPEG_REACHED_SOS;
313793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
314793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_PRELOAD:
315793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_PRESCAN:
316793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_SCANNING:
317793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_RAW_OK:
318793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_BUFIMAGE:
319793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_BUFPOST:
320793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  case DSTATE_STOPPING:
321793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    retcode = (*cinfo->inputctl->consume_input) (cinfo);
322793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    break;
323793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  default:
324793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
325793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  }
326793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  return retcode;
327793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
328793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
329793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
330793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
331793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Have we finished reading the input file?
332793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
333793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
334793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerGLOBAL(boolean)
335793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerjpeg_input_complete (j_decompress_ptr cinfo)
336793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
337793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Check for valid jpeg object */
338793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  if (cinfo->global_state < DSTATE_START ||
339793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      cinfo->global_state > DSTATE_STOPPING)
340793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
341793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  return cinfo->inputctl->eoi_reached;
342793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
343793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
344793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
345793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
346793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Is there more than one scan?
347793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
348793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
349793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerGLOBAL(boolean)
350793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerjpeg_has_multiple_scans (j_decompress_ptr cinfo)
351793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
352793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Only valid after jpeg_read_header completes */
353793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  if (cinfo->global_state < DSTATE_READY ||
354793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      cinfo->global_state > DSTATE_STOPPING)
355793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
356793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  return cinfo->inputctl->has_multiple_scans;
357793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
358793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
359793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
360793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
361793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Finish JPEG decompression.
362793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
363793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * This will normally just verify the file trailer and release temp storage.
364793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
365793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Returns FALSE if suspended.  The return value need be inspected only if
366793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * a suspending data source is used.
367793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
368793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
369793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerGLOBAL(boolean)
370793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerjpeg_finish_decompress (j_decompress_ptr cinfo)
371793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
372793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  if ((cinfo->global_state == DSTATE_SCANNING ||
373793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler       cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
374793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* Terminate final pass of non-buffered mode */
375793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if (cinfo->output_scanline < cinfo->output_height)
376793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
377793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    (*cinfo->master->finish_output_pass) (cinfo);
378793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->global_state = DSTATE_STOPPING;
379793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
380793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* Finishing after a buffered-image operation */
381793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    cinfo->global_state = DSTATE_STOPPING;
382793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  } else if (cinfo->global_state != DSTATE_STOPPING) {
383793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* STOPPING = repeat call after a suspension, anything else is error */
384793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
385793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  }
386793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Read until EOI */
387793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  while (! cinfo->inputctl->eoi_reached) {
388793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
389793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      return FALSE;		/* Suspend, come back later */
390793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  }
391793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* Do final cleanup */
392793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  (*cinfo->src->term_source) (cinfo);
393793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  /* We can use jpeg_abort to release memory and reset global_state */
394793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  jpeg_abort((j_common_ptr) cinfo);
395793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  return TRUE;
396793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
397