136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * jcparam.c
336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
4a73e870ad02de20c2b34cb3a5382c2846c2afbe3DRC * This file was part of the Independent JPEG Group's software:
55ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * Copyright (C) 1991-1998, Thomas G. Lane.
65996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding * Modified 2003-2008 by Guido Vollbeding.
7a6ef282a49f2d7d1b4d19cc89f63e81fd66b35b7DRC * libjpeg-turbo Modifications:
867ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC * Copyright (C) 2009-2011, D. R. Commander.
936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * For conditions of distribution and use, see the accompanying README file.
1036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
1136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This file contains optional default-setting code for the JPEG compressor.
1236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Applications do not have to use this file, but those that don't use it
1336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * must know a lot more about the innards of the JPEG code.
1436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
1536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
1636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define JPEG_INTERNALS
1736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jinclude.h"
1836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jpeglib.h"
19a113506d175d03ae0e40965c3d3d21a5d561e119DRC#include "jstdhuff.c"
2036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
2336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Quantization table setup routines
2436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
2536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
26489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
2736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
28e5eaf37440b8e337ab150c017df7c03faf846c51DRC                      const unsigned int *basic_table,
29e5eaf37440b8e337ab150c017df7c03faf846c51DRC                      int scale_factor, boolean force_baseline)
3036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Define a quantization table equal to the basic_table times
3136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * a scale factor (given as a percentage).
3236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * If force_baseline is TRUE, the computed quantization table entries
3336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * are limited to 1..255 for JPEG baseline compatibility.
3436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
3536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
365ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  JQUANT_TBL ** qtblptr;
3736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int i;
3836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  long temp;
3936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
4036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Safety check to ensure start_compress not called yet. */
4136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->global_state != CSTATE_START)
4236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
4336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
445ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
455ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane    ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
465ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
475ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
485ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
4936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (*qtblptr == NULL)
5036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
5136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
5236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (i = 0; i < DCTSIZE2; i++) {
5336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
5436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* limit the values to the valid range */
5536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (temp <= 0L) temp = 1L;
5636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
5736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (force_baseline && temp > 255L)
58e5eaf37440b8e337ab150c017df7c03faf846c51DRC      temp = 255L;              /* limit to baseline range if requested */
5936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*qtblptr)->quantval[i] = (UINT16) temp;
6036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
6136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
6236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Initialize sent_table FALSE so table will be written to JPEG file. */
6336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  (*qtblptr)->sent_table = FALSE;
6436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
6536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
6636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
675996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding/* These are the sample quantization tables given in JPEG spec section K.1.
685996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding * The spec says that the values given produce "good" quality, and
695996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding * when divided by 2, "very good" quality.
705996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding */
715996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbedingstatic const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
725996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  16,  11,  10,  16,  24,  40,  51,  61,
735996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  12,  12,  14,  19,  26,  58,  60,  55,
745996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  14,  13,  16,  24,  40,  57,  69,  56,
755996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  14,  17,  22,  29,  51,  87,  80,  62,
765996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  18,  22,  37,  56,  68, 109, 103,  77,
775996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  24,  35,  55,  64,  81, 104, 113,  92,
785996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  49,  64,  78,  87, 103, 121, 120, 101,
795996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  72,  92,  95,  98, 112, 100, 103,  99
805996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding};
815996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbedingstatic const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
825996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  17,  18,  24,  47,  99,  99,  99,  99,
835996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  18,  21,  26,  66,  99,  99,  99,  99,
845996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  24,  26,  56,  99,  99,  99,  99,  99,
855996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  47,  66,  99,  99,  99,  99,  99,  99,
865996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  99,  99,  99,  99,  99,  99,  99,  99,
875996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  99,  99,  99,  99,  99,  99,  99,  99,
885996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  99,  99,  99,  99,  99,  99,  99,  99,
895996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  99,  99,  99,  99,  99,  99,  99,  99
905996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding};
915996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding
925996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding
9336a6eec93250e390d1028b3372078810b4428eafDRC#if JPEG_LIB_VERSION >= 70
945996a25e2f50d20d6a8f09830724035b49c3927bGuido VollbedingGLOBAL(void)
955996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbedingjpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline)
965996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding/* Set or change the 'quality' (quantization) setting, using default tables
975996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding * and straight percentage-scaling quality scales.
985996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding * This entry point allows different scalings for luminance and chrominance.
995996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding */
1005996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding{
1015996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  /* Set up two quantization tables using the specified scaling */
1025996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
103e5eaf37440b8e337ab150c017df7c03faf846c51DRC                       cinfo->q_scale_factor[0], force_baseline);
1045996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
105e5eaf37440b8e337ab150c017df7c03faf846c51DRC                       cinfo->q_scale_factor[1], force_baseline);
1065996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding}
10736a6eec93250e390d1028b3372078810b4428eafDRC#endif
1085996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding
1095996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding
110489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
11136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
112e5eaf37440b8e337ab150c017df7c03faf846c51DRC                         boolean force_baseline)
11336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Set or change the 'quality' (quantization) setting, using default tables
11436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * and a straight percentage-scaling quality scale.  In most cases it's better
11536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * to use jpeg_set_quality (below); this entry point is provided for
11636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * applications that insist on a linear percentage scaling.
11736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
11836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
11936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Set up two quantization tables using the specified scaling */
12036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
121e5eaf37440b8e337ab150c017df7c03faf846c51DRC                       scale_factor, force_baseline);
12236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
123e5eaf37440b8e337ab150c017df7c03faf846c51DRC                       scale_factor, force_baseline);
12436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
12536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
12636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
127489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(int)
12836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejpeg_quality_scaling (int quality)
12936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Convert a user-specified quality rating to a percentage scaling factor
13036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * for an underlying quantization table, using our recommended scaling curve.
13136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * The input 'quality' factor should be 0 (terrible) to 100 (very good).
13236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
13336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
13436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Safety limit on quality factor.  Convert 0 to 1 to avoid zero divide. */
13536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (quality <= 0) quality = 1;
13636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (quality > 100) quality = 100;
13736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
13836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* The basic table is used as-is (scaling 100) for a quality of 50.
13936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
140489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane   * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
141489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane   * to make all the table entries 1 (hence, minimum quantization loss).
14236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * Qualities 1..50 are converted to scaling percentage 5000/Q.
14336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
14436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (quality < 50)
14536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    quality = 5000 / quality;
14636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  else
14736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    quality = 200 - quality*2;
14836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
14936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  return quality;
15036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
15136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
15236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
153489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
15436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
15536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Set or change the 'quality' (quantization) setting, using default tables.
15636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This is the standard quality-adjusting entry point for typical user
15736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * interfaces; only those who want detailed control over quantization tables
15836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * would use the preceding three routines directly.
15936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
16036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
16136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Convert user 0-100 rating to percentage scaling */
16236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  quality = jpeg_quality_scaling(quality);
16336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
16436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Set up standard quality tables */
16536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_set_linear_quality(cinfo, quality, force_baseline);
16636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
16736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
16836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
16936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
17036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Default parameter setup for compression.
17136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
17236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Applications that don't choose to use this routine must do their
17336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * own setup of all these parameters.  Alternately, you can call this
17436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * to establish defaults and then alter parameters selectively.  This
17536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * is the recommended approach since, if we add any new parameters,
17636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * your code will still work (they'll be set to reasonable defaults).
17736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
17836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
179489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
18036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejpeg_set_defaults (j_compress_ptr cinfo)
18136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
18236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int i;
18336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
18436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Safety check to ensure start_compress not called yet. */
18536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->global_state != CSTATE_START)
18636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
18736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
18836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Allocate comp_info array large enough for maximum component count.
18936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * Array is made permanent in case application wants to compress
19036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * multiple images at same param settings.
19136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
19236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->comp_info == NULL)
19336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->comp_info = (jpeg_component_info *)
19436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
1955de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                                  MAX_COMPONENTS * sizeof(jpeg_component_info));
19636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
19736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Initialize everything not dependent on the color space */
19836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
19936a6eec93250e390d1028b3372078810b4428eafDRC#if JPEG_LIB_VERSION >= 70
200e5eaf37440b8e337ab150c017df7c03faf846c51DRC  cinfo->scale_num = 1;         /* 1:1 scaling */
2015996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  cinfo->scale_denom = 1;
20236a6eec93250e390d1028b3372078810b4428eafDRC#endif
20336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->data_precision = BITS_IN_JSAMPLE;
20436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Set up two quantization tables using default quality of 75 */
20536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_set_quality(cinfo, 75, TRUE);
20636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Set up two Huffman tables */
207a113506d175d03ae0e40965c3d3d21a5d561e119DRC  std_huff_tables((j_common_ptr) cinfo);
20836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
20936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Initialize default arithmetic coding conditioning */
21036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (i = 0; i < NUM_ARITH_TBLS; i++) {
21136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->arith_dc_L[i] = 0;
21236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->arith_dc_U[i] = 1;
21336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->arith_ac_K[i] = 5;
21436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
21536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
216bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Default is no multiple-scan output */
217bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cinfo->scan_info = NULL;
218bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cinfo->num_scans = 0;
219bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
22036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Expect normal source image, not raw downsampled data */
22136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->raw_data_in = FALSE;
22236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
22336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Use Huffman coding, not arithmetic coding, by default */
22436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->arith_code = FALSE;
22536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
22636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* By default, don't do extra passes to optimize entropy coding */
22736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->optimize_coding = FALSE;
22836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* The standard Huffman tables are only valid for 8-bit data precision.
22936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * If the precision is higher, force optimization on so that usable
23036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * tables will be computed.  This test can be removed if default tables
23136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * are supplied that are valid for the desired precision.
23236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
23336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->data_precision > 8)
23436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->optimize_coding = TRUE;
23536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
23636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* By default, use the simpler non-cosited sampling alignment */
23736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->CCIR601_sampling = FALSE;
23836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
23936a6eec93250e390d1028b3372078810b4428eafDRC#if JPEG_LIB_VERSION >= 70
2405996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  /* By default, apply fancy downsampling */
2415996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  cinfo->do_fancy_downsampling = TRUE;
24236a6eec93250e390d1028b3372078810b4428eafDRC#endif
2435996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding
24436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* No input smoothing */
24536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->smoothing_factor = 0;
24636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
24736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* DCT algorithm preference */
24836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->dct_method = JDCT_DEFAULT;
24936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
25036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* No restart markers */
25136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->restart_interval = 0;
25236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->restart_in_rows = 0;
25336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
25436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Fill in default JFIF marker parameters.  Note that whether the marker
25536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * will actually be written is determined by jpeg_set_colorspace.
2565ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   *
2575ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * By default, the library emits JFIF version code 1.01.
2585ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * An application that wants to emit JFIF 1.02 extension markers should set
2595ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * JFIF_minor_version to 2.  We could probably get away with just defaulting
2605ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * to 1.02, but there may still be some decoders in use that will complain
2615ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * about that; saying 1.01 should minimize compatibility problems.
26236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
2635ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
2645ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  cinfo->JFIF_minor_version = 1;
265e5eaf37440b8e337ab150c017df7c03faf846c51DRC  cinfo->density_unit = 0;      /* Pixel size is unknown by default */
266e5eaf37440b8e337ab150c017df7c03faf846c51DRC  cinfo->X_density = 1;         /* Pixel aspect ratio is square by default */
26736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->Y_density = 1;
26836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
26936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Choose JPEG colorspace based on input space, set defaults accordingly */
27036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
27136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_default_colorspace(cinfo);
27236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
27336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
27436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
27536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
27636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Select an appropriate JPEG colorspace for in_color_space.
27736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
27836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
279489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
28036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejpeg_default_colorspace (j_compress_ptr cinfo)
28136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
28236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  switch (cinfo->in_color_space) {
28336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_GRAYSCALE:
28436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
28536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
28636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_RGB:
287f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_RGB:
288f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_RGBX:
289f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_BGR:
290f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_BGRX:
291f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_XBGR:
292f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_XRGB:
29367ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_RGBA:
29467ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_BGRA:
29567ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_ABGR:
29667ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_ARGB:
29736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    jpeg_set_colorspace(cinfo, JCS_YCbCr);
29836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
29936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_YCbCr:
30036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    jpeg_set_colorspace(cinfo, JCS_YCbCr);
30136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
30236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_CMYK:
30336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
30436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
30536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_YCCK:
30636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    jpeg_set_colorspace(cinfo, JCS_YCCK);
30736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
30836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_UNKNOWN:
30936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
31036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
31136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  default:
31236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
31336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
31436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
31536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
31636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
31736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
31836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Set the JPEG colorspace, and choose colorspace-dependent default values.
31936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
32036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
321489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
32236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
32336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
32436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_component_info * compptr;
32536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int ci;
32636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
32736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
32836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  (compptr = &cinfo->comp_info[index], \
32936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   compptr->component_id = (id), \
33036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   compptr->h_samp_factor = (hsamp), \
33136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   compptr->v_samp_factor = (vsamp), \
33236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   compptr->quant_tbl_no = (quant), \
33336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   compptr->dc_tbl_no = (dctbl), \
33436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   compptr->ac_tbl_no = (actbl) )
33536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
33636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Safety check to ensure start_compress not called yet. */
33736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->global_state != CSTATE_START)
33836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
33936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
34036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
34136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * tables 1 for chrominance components.
34236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
34336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
34436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->jpeg_color_space = colorspace;
34536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
34636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
34736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
34836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
34936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  switch (colorspace) {
35036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_GRAYSCALE:
35136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
35236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->num_components = 1;
35336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* JFIF specifies component ID 1 */
35436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    SET_COMP(0, 1, 1,1, 0, 0,0);
35536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
35636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_RGB:
35736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
35836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->num_components = 3;
359bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
360bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
361bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
36236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
36336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_YCbCr:
36436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
36536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->num_components = 3;
36636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* JFIF specifies component IDs 1,2,3 */
36736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* We default to 2x2 subsamples of chrominance */
36836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    SET_COMP(0, 1, 2,2, 0, 0,0);
36936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    SET_COMP(1, 2, 1,1, 1, 1,1);
37036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    SET_COMP(2, 3, 1,1, 1, 1,1);
37136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
37236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_CMYK:
37336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
37436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->num_components = 4;
375bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
376bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
377bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
378bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
37936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
38036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_YCCK:
38136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
38236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->num_components = 4;
38336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    SET_COMP(0, 1, 2,2, 0, 0,0);
38436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    SET_COMP(1, 2, 1,1, 1, 1,1);
38536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    SET_COMP(2, 3, 1,1, 1, 1,1);
38636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    SET_COMP(3, 4, 2,2, 0, 0,0);
38736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
38836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_UNKNOWN:
38936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->num_components = cinfo->input_components;
39036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
39136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
392e5eaf37440b8e337ab150c017df7c03faf846c51DRC               MAX_COMPONENTS);
39336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    for (ci = 0; ci < cinfo->num_components; ci++) {
39436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      SET_COMP(ci, ci, 1,1, 0, 0,0);
39536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
39636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
39736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  default:
39836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
39936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
40036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
401bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
402bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
403bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef C_PROGRESSIVE_SUPPORTED
404bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
405489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(jpeg_scan_info *)
406bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanefill_a_scan (jpeg_scan_info * scanptr, int ci,
407e5eaf37440b8e337ab150c017df7c03faf846c51DRC             int Ss, int Se, int Ah, int Al)
408bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* Support routine: generate one scan for specified component */
409bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
410bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  scanptr->comps_in_scan = 1;
411bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  scanptr->component_index[0] = ci;
412bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  scanptr->Ss = Ss;
413bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  scanptr->Se = Se;
414bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  scanptr->Ah = Ah;
415bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  scanptr->Al = Al;
416bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  scanptr++;
417bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  return scanptr;
418bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
419bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
420489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(jpeg_scan_info *)
421bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanefill_scans (jpeg_scan_info * scanptr, int ncomps,
422e5eaf37440b8e337ab150c017df7c03faf846c51DRC            int Ss, int Se, int Ah, int Al)
423bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* Support routine: generate one scan for each component */
424bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
425bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int ci;
426bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
427bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  for (ci = 0; ci < ncomps; ci++) {
428bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->comps_in_scan = 1;
429bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->component_index[0] = ci;
430bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->Ss = Ss;
431bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->Se = Se;
432bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->Ah = Ah;
433bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->Al = Al;
434bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr++;
435bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
436bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  return scanptr;
437bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
438bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
439489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(jpeg_scan_info *)
440bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanefill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
441bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* Support routine: generate interleaved DC scan if possible, else N scans */
442bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
443bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int ci;
444bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
445bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (ncomps <= MAX_COMPS_IN_SCAN) {
446bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Single interleaved DC scan */
447bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->comps_in_scan = ncomps;
448bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    for (ci = 0; ci < ncomps; ci++)
449bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      scanptr->component_index[ci] = ci;
450bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->Ss = scanptr->Se = 0;
451bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->Ah = Ah;
452bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr->Al = Al;
453bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr++;
454bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else {
455bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Noninterleaved DC scan for each component */
456bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
457bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
458bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  return scanptr;
459bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
460bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
461bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
462bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
463bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Create a recommended progressive-JPEG script.
464bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * cinfo->num_components and cinfo->jpeg_color_space must be correct.
465bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
466bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
467489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
468bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejpeg_simple_progression (j_compress_ptr cinfo)
469bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
470bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int ncomps = cinfo->num_components;
471bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int nscans;
472bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  jpeg_scan_info * scanptr;
473bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
474bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Safety check to ensure start_compress not called yet. */
475bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->global_state != CSTATE_START)
476bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
477bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
478bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Figure space needed for script.  Calculation must match code below! */
479bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
480bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Custom script for YCbCr color images. */
481bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    nscans = 10;
482bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else {
483bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* All-purpose script for other color spaces. */
484bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (ncomps > MAX_COMPS_IN_SCAN)
485e5eaf37440b8e337ab150c017df7c03faf846c51DRC      nscans = 6 * ncomps;      /* 2 DC + 4 AC scans per component */
486bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    else
487e5eaf37440b8e337ab150c017df7c03faf846c51DRC      nscans = 2 + 4 * ncomps;  /* 2 DC scans; 4 AC scans per component */
488bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
489bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
4905ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  /* Allocate space for script.
4915ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * We need to put it in the permanent pool in case the application performs
4925ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * multiple compressions without changing the settings.  To avoid a memory
4935ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * leak if jpeg_simple_progression is called repeatedly for the same JPEG
4945ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * object, we try to re-use previously allocated space, and we allocate
4955ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * enough space to handle YCbCr even if initially asked for grayscale.
4965ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   */
4975ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
4985ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane    cinfo->script_space_size = MAX(nscans, 10);
4995ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane    cinfo->script_space = (jpeg_scan_info *)
5005ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
5015de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                        cinfo->script_space_size * sizeof(jpeg_scan_info));
5025ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  }
5035ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  scanptr = cinfo->script_space;
504bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cinfo->scan_info = scanptr;
505bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cinfo->num_scans = nscans;
506bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
507bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
508bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Custom script for YCbCr color images. */
509bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Initial DC scan */
510bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
511bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Initial AC scan: get some luma data out in a hurry */
512bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
513bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Chroma data is too small to be worth expending many scans on */
514bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
515bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
516bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Complete spectral selection for luma AC */
517bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
518bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Refine next bit of luma AC */
519bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
520bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Finish DC successive approximation */
521bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
522bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Finish AC successive approximation */
523bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
524bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
525bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Luma bottom bit comes last since it's usually largest scan */
526bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
527bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else {
528bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* All-purpose script for other color spaces. */
529bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Successive approximation first pass */
530bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
531bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
532bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
533bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Successive approximation second pass */
534bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
535bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Successive approximation final pass */
536bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
537bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
538bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
539bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
540bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
541bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif /* C_PROGRESSIVE_SUPPORTED */
542