12cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
22cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * jdmaster.c
32cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
4a73e870ad02de20c2b34cb3a5382c2846c2afbe3DRC * This file was part of the Independent JPEG Group's software:
55ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * Copyright (C) 1991-1997, Thomas G. Lane.
627fb3fc5895baccff74d7286785d97039682e45cDRC * Modified 2002-2009 by Guido Vollbeding.
7a6ef282a49f2d7d1b4d19cc89f63e81fd66b35b7DRC * libjpeg-turbo Modifications:
80ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC * Copyright (C) 2009-2011, 2016, D. R. Commander.
978df2e6115b0e579432d01cb034132cd4402a1baDRC * Copyright (C) 2013, Linaro Limited.
100ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC * Copyright (C) 2015, Google, Inc.
110ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC * For conditions of distribution and use, see the accompanying README.ijg
120ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC * file.
132cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
1436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This file contains master control logic for the JPEG decompressor.
1536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * These routines are concerned with selecting the modules to be executed
1636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * and with determining the number of passes and the work to be done in each
1736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * pass.
182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
192cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define JPEG_INTERNALS
212cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#include "jinclude.h"
2236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jpeglib.h"
2336a6eec93250e390d1028b3372078810b4428eafDRC#include "jpegcomp.h"
240ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC#include "jdmaster.h"
256eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis#include "jsimd.h"
2636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
2936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Determine whether merged upsample/color conversion should be used.
3036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * CRUCIAL: this must match the actual capabilities of jdmerge.c!
3136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
3236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
33489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(boolean)
3436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneuse_merged_upsample (j_decompress_ptr cinfo)
3536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
3636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef UPSAMPLE_MERGING_SUPPORTED
3736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Merging is the equivalent of plain box-filter upsampling */
3836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
3936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    return FALSE;
4078df2e6115b0e579432d01cb034132cd4402a1baDRC  /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
4136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
42720e161083758559109133cbb1d1133bd924759aDRC      (cinfo->out_color_space != JCS_RGB &&
4378df2e6115b0e579432d01cb034132cd4402a1baDRC      cinfo->out_color_space != JCS_RGB565 &&
44720e161083758559109133cbb1d1133bd924759aDRC      cinfo->out_color_space != JCS_EXT_RGB &&
45720e161083758559109133cbb1d1133bd924759aDRC      cinfo->out_color_space != JCS_EXT_RGBX &&
46720e161083758559109133cbb1d1133bd924759aDRC      cinfo->out_color_space != JCS_EXT_BGR &&
47720e161083758559109133cbb1d1133bd924759aDRC      cinfo->out_color_space != JCS_EXT_BGRX &&
48720e161083758559109133cbb1d1133bd924759aDRC      cinfo->out_color_space != JCS_EXT_XBGR &&
4967ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC      cinfo->out_color_space != JCS_EXT_XRGB &&
5067ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC      cinfo->out_color_space != JCS_EXT_RGBA &&
5167ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC      cinfo->out_color_space != JCS_EXT_BGRA &&
5267ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC      cinfo->out_color_space != JCS_EXT_ABGR &&
5378df2e6115b0e579432d01cb034132cd4402a1baDRC      cinfo->out_color_space != JCS_EXT_ARGB))
5478df2e6115b0e579432d01cb034132cd4402a1baDRC    return FALSE;
5578df2e6115b0e579432d01cb034132cd4402a1baDRC  if ((cinfo->out_color_space == JCS_RGB565 &&
5678df2e6115b0e579432d01cb034132cd4402a1baDRC      cinfo->out_color_components != 3) ||
5778df2e6115b0e579432d01cb034132cd4402a1baDRC      (cinfo->out_color_space != JCS_RGB565 &&
5878df2e6115b0e579432d01cb034132cd4402a1baDRC      cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))
5936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    return FALSE;
6036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* and it only handles 2h1v or 2h2v sampling ratios */
6136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->comp_info[0].h_samp_factor != 2 ||
6236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cinfo->comp_info[1].h_samp_factor != 1 ||
6336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cinfo->comp_info[2].h_samp_factor != 1 ||
6436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cinfo->comp_info[0].v_samp_factor >  2 ||
6536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cinfo->comp_info[1].v_samp_factor != 1 ||
6636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cinfo->comp_info[2].v_samp_factor != 1)
6736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    return FALSE;
6836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
6949967cdb30edd0479a1719eedc1dace5ba078d3fDRC  if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
7049967cdb30edd0479a1719eedc1dace5ba078d3fDRC      cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
7149967cdb30edd0479a1719eedc1dace5ba078d3fDRC      cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
7236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    return FALSE;
736eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis#ifdef WITH_SIMD
746eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  /* If YCbCr-to-RGB color conversion is SIMD-accelerated but merged upsampling
756eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis     isn't, then disabling merged upsampling is likely to be faster when
766eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis     decompressing YCbCr JPEG images. */
776eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  if (!jsimd_can_h2v2_merged_upsample() && !jsimd_can_h2v1_merged_upsample() &&
786eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis      jsimd_can_ycc_rgb() && cinfo->jpeg_color_space == JCS_YCbCr &&
796eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis      (cinfo->out_color_space == JCS_RGB ||
806eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis       (cinfo->out_color_space >= JCS_EXT_RGB &&
816eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis        cinfo->out_color_space <= JCS_EXT_ARGB)))
826eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis    return FALSE;
836eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis#endif
8436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* ??? also need to test for upsample-time rescaling, when & if supported */
85e5eaf37440b8e337ab150c017df7c03faf846c51DRC  return TRUE;                  /* by golly, it'll work... */
86cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane#else
8736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  return FALSE;
88cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane#endif
892cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
902cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
912cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
9236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
93bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Compute output image dimensions and related values.
94bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * NOTE: this is exported for possible use by application.
9536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Hence it mustn't do anything that can't be done twice.
9636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
9736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
9827fb3fc5895baccff74d7286785d97039682e45cDRC#if JPEG_LIB_VERSION >= 80
99489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
10027fb3fc5895baccff74d7286785d97039682e45cDRC#else
10127fb3fc5895baccff74d7286785d97039682e45cDRCLOCAL(void)
10227fb3fc5895baccff74d7286785d97039682e45cDRC#endif
10327fb3fc5895baccff74d7286785d97039682e45cDRCjpeg_core_output_dimensions (j_decompress_ptr cinfo)
104989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding/* Do computations that are needed before master selection phase.
10527fb3fc5895baccff74d7286785d97039682e45cDRC * This function is used for transcoding and full decompression.
106989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding */
1072cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
1085ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane#ifdef IDCT_SCALING_SUPPORTED
10936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int ci;
1102cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  jpeg_component_info *compptr;
11127fb3fc5895baccff74d7286785d97039682e45cDRC
11227fb3fc5895baccff74d7286785d97039682e45cDRC  /* Compute actual output image dimensions and DCT scaling choices. */
11327fb3fc5895baccff74d7286785d97039682e45cDRC  if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
11427fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 1/block_size scaling */
11527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
11627fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width, (long) DCTSIZE);
11727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
11827fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height, (long) DCTSIZE);
11927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 1;
12027fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 1;
12127fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
12227fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 2/block_size scaling */
12327fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
12427fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 2L, (long) DCTSIZE);
12527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
12627fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 2L, (long) DCTSIZE);
12727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 2;
12827fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 2;
12927fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
13027fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 3/block_size scaling */
13127fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
13227fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 3L, (long) DCTSIZE);
13327fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
13427fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 3L, (long) DCTSIZE);
13527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 3;
13627fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 3;
13727fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
13827fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 4/block_size scaling */
13927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
14027fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 4L, (long) DCTSIZE);
14127fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
14227fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 4L, (long) DCTSIZE);
14327fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 4;
14427fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 4;
14527fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
14627fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 5/block_size scaling */
14727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
14827fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 5L, (long) DCTSIZE);
14927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
15027fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 5L, (long) DCTSIZE);
15127fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 5;
15227fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 5;
15327fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
15427fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 6/block_size scaling */
15527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
15627fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 6L, (long) DCTSIZE);
15727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
15827fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 6L, (long) DCTSIZE);
15927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 6;
16027fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 6;
16127fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
16227fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 7/block_size scaling */
16327fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
16427fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 7L, (long) DCTSIZE);
16527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
16627fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 7L, (long) DCTSIZE);
16727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 7;
16827fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 7;
16927fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
17027fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 8/block_size scaling */
17127fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
17227fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 8L, (long) DCTSIZE);
17327fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
17427fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 8L, (long) DCTSIZE);
17527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 8;
17627fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 8;
17727fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
17827fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 9/block_size scaling */
17927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
18027fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 9L, (long) DCTSIZE);
18127fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
18227fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 9L, (long) DCTSIZE);
18327fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 9;
18427fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 9;
18527fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
18627fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 10/block_size scaling */
18727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
18827fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 10L, (long) DCTSIZE);
18927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
19027fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 10L, (long) DCTSIZE);
19127fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 10;
19227fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 10;
19327fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
19427fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 11/block_size scaling */
19527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
19627fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 11L, (long) DCTSIZE);
19727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
19827fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 11L, (long) DCTSIZE);
19927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 11;
20027fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 11;
20127fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
20227fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 12/block_size scaling */
20327fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
20427fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 12L, (long) DCTSIZE);
20527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
20627fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 12L, (long) DCTSIZE);
20727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 12;
20827fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 12;
20927fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
21027fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 13/block_size scaling */
21127fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
21227fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 13L, (long) DCTSIZE);
21327fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
21427fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 13L, (long) DCTSIZE);
21527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 13;
21627fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 13;
21727fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
21827fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 14/block_size scaling */
21927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
22027fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 14L, (long) DCTSIZE);
22127fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
22227fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 14L, (long) DCTSIZE);
22327fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 14;
22427fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 14;
22527fb3fc5895baccff74d7286785d97039682e45cDRC  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
22627fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 15/block_size scaling */
22727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
22827fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 15L, (long) DCTSIZE);
22927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
23027fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 15L, (long) DCTSIZE);
23127fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 15;
23227fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 15;
23327fb3fc5895baccff74d7286785d97039682e45cDRC  } else {
23427fb3fc5895baccff74d7286785d97039682e45cDRC    /* Provide 16/block_size scaling */
23527fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_width = (JDIMENSION)
23627fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_width * 16L, (long) DCTSIZE);
23727fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->output_height = (JDIMENSION)
23827fb3fc5895baccff74d7286785d97039682e45cDRC      jdiv_round_up((long) cinfo->image_height * 16L, (long) DCTSIZE);
23927fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_h_scaled_size = 16;
24027fb3fc5895baccff74d7286785d97039682e45cDRC    cinfo->_min_DCT_v_scaled_size = 16;
24127fb3fc5895baccff74d7286785d97039682e45cDRC  }
24227fb3fc5895baccff74d7286785d97039682e45cDRC
24327fb3fc5895baccff74d7286785d97039682e45cDRC  /* Recompute dimensions of components */
24427fb3fc5895baccff74d7286785d97039682e45cDRC  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
24527fb3fc5895baccff74d7286785d97039682e45cDRC       ci++, compptr++) {
24627fb3fc5895baccff74d7286785d97039682e45cDRC    compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
24727fb3fc5895baccff74d7286785d97039682e45cDRC    compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
24827fb3fc5895baccff74d7286785d97039682e45cDRC  }
24927fb3fc5895baccff74d7286785d97039682e45cDRC
25027fb3fc5895baccff74d7286785d97039682e45cDRC#else /* !IDCT_SCALING_SUPPORTED */
25127fb3fc5895baccff74d7286785d97039682e45cDRC
25227fb3fc5895baccff74d7286785d97039682e45cDRC  /* Hardwire it to "no scaling" */
25327fb3fc5895baccff74d7286785d97039682e45cDRC  cinfo->output_width = cinfo->image_width;
25427fb3fc5895baccff74d7286785d97039682e45cDRC  cinfo->output_height = cinfo->image_height;
25527fb3fc5895baccff74d7286785d97039682e45cDRC  /* jdinput.c has already initialized DCT_scaled_size,
25627fb3fc5895baccff74d7286785d97039682e45cDRC   * and has computed unscaled downsampled_width and downsampled_height.
25727fb3fc5895baccff74d7286785d97039682e45cDRC   */
25827fb3fc5895baccff74d7286785d97039682e45cDRC
25927fb3fc5895baccff74d7286785d97039682e45cDRC#endif /* IDCT_SCALING_SUPPORTED */
26027fb3fc5895baccff74d7286785d97039682e45cDRC}
26127fb3fc5895baccff74d7286785d97039682e45cDRC
26227fb3fc5895baccff74d7286785d97039682e45cDRC
26327fb3fc5895baccff74d7286785d97039682e45cDRC/*
26427fb3fc5895baccff74d7286785d97039682e45cDRC * Compute output image dimensions and related values.
26527fb3fc5895baccff74d7286785d97039682e45cDRC * NOTE: this is exported for possible use by application.
26627fb3fc5895baccff74d7286785d97039682e45cDRC * Hence it mustn't do anything that can't be done twice.
267bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Also note that it may be called before the master module is initialized!
26836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
26936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
270489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
27136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejpeg_calc_output_dimensions (j_decompress_ptr cinfo)
27236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Do computations that are needed before master selection phase */
2732cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
2745ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane#ifdef IDCT_SCALING_SUPPORTED
27536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int ci;
2762cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  jpeg_component_info *compptr;
2775ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane#endif
2782cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
279bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Prevent application from calling me at wrong times */
280bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->global_state != DSTATE_READY)
281bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
28236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
283989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding  /* Compute core output image dimensions and DCT scaling choices. */
284989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding  jpeg_core_output_dimensions(cinfo);
285989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding
28636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef IDCT_SCALING_SUPPORTED
287bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
28836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* In selecting the actual DCT scaling for each component, we try to
28936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * scale up the chroma components via IDCT scaling rather than upsampling.
29036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * This saves time if the upsampler gets to use 1:1 scaling.
291c8fb758bc2b48f5a963cf93e9e44fc1ae9147ac0DRC   * Note this code adapts subsampling ratios which are powers of 2.
29236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
29336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
29436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane       ci++, compptr++) {
29549967cdb30edd0479a1719eedc1dace5ba078d3fDRC    int ssize = cinfo->_min_DCT_scaled_size;
29636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    while (ssize < DCTSIZE &&
297e5eaf37440b8e337ab150c017df7c03faf846c51DRC           ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
298e5eaf37440b8e337ab150c017df7c03faf846c51DRC            (compptr->h_samp_factor * ssize * 2) == 0) &&
299e5eaf37440b8e337ab150c017df7c03faf846c51DRC           ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
300e5eaf37440b8e337ab150c017df7c03faf846c51DRC            (compptr->v_samp_factor * ssize * 2) == 0)) {
30136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ssize = ssize * 2;
30236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
30336a6eec93250e390d1028b3372078810b4428eafDRC#if JPEG_LIB_VERSION >= 70
30436a6eec93250e390d1028b3372078810b4428eafDRC    compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
30536a6eec93250e390d1028b3372078810b4428eafDRC#else
30636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    compptr->DCT_scaled_size = ssize;
30736a6eec93250e390d1028b3372078810b4428eafDRC#endif
30836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
309bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
310bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Recompute downsampled dimensions of components;
311bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   * application needs to know these if using raw downsampled data.
312bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   */
313bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
314bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane       ci++, compptr++) {
315bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Size in samples, after IDCT scaling */
316bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    compptr->downsampled_width = (JDIMENSION)
317bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      jdiv_round_up((long) cinfo->image_width *
318e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
319e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) (cinfo->max_h_samp_factor * DCTSIZE));
320bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    compptr->downsampled_height = (JDIMENSION)
321bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      jdiv_round_up((long) cinfo->image_height *
322e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
323e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    (long) (cinfo->max_v_samp_factor * DCTSIZE));
324bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
325bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
32636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#else /* !IDCT_SCALING_SUPPORTED */
327bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
32836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Hardwire it to "no scaling" */
32936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->output_width = cinfo->image_width;
33036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->output_height = cinfo->image_height;
331bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
332bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   * and has computed unscaled downsampled_width and downsampled_height.
333bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   */
334bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
33536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif /* IDCT_SCALING_SUPPORTED */
3362cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
33736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Report number of components in selected colorspace. */
33836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Probably this should be in the color conversion module... */
33936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  switch (cinfo->out_color_space) {
34036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_GRAYSCALE:
34136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->out_color_components = 1;
34236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
34336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_RGB:
344720e161083758559109133cbb1d1133bd924759aDRC  case JCS_EXT_RGB:
345720e161083758559109133cbb1d1133bd924759aDRC  case JCS_EXT_RGBX:
346720e161083758559109133cbb1d1133bd924759aDRC  case JCS_EXT_BGR:
347720e161083758559109133cbb1d1133bd924759aDRC  case JCS_EXT_BGRX:
348720e161083758559109133cbb1d1133bd924759aDRC  case JCS_EXT_XBGR:
349720e161083758559109133cbb1d1133bd924759aDRC  case JCS_EXT_XRGB:
35067ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_RGBA:
35167ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_BGRA:
35267ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_ABGR:
35367ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_ARGB:
354720e161083758559109133cbb1d1133bd924759aDRC    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
35536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
35636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_YCbCr:
35778df2e6115b0e579432d01cb034132cd4402a1baDRC  case JCS_RGB565:
35836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->out_color_components = 3;
35936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
36036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_CMYK:
36136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_YCCK:
36236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->out_color_components = 4;
36336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
364e5eaf37440b8e337ab150c017df7c03faf846c51DRC  default:                      /* else must be same colorspace as in file */
36536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->out_color_components = cinfo->num_components;
36636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
3672cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
36836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->output_components = (cinfo->quantize_colors ? 1 :
369e5eaf37440b8e337ab150c017df7c03faf846c51DRC                              cinfo->out_color_components);
37036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
37136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* See if upsampler will want to emit more than one row at a time */
37236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (use_merged_upsample(cinfo))
37336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
37436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  else
37536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->rec_outbuf_height = 1;
3762cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
3772cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3782cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
3792cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
38036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Several decompression processes need to range-limit values to the range
38136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
38236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * due to noise introduced by quantization, roundoff error, etc.  These
38336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * processes are inner loops and need to be as fast as possible.  On most
38436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * machines, particularly CPUs with pipelines or instruction prefetch,
38536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * a (subscript-check-less) C table lookup
386e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              x = sample_range_limit[x];
38736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * is faster than explicit tests
388e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              if (x < 0)  x = 0;
389e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
39036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * These processes all use a common table prepared by the routine below.
39136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
39236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * For most steps we can mathematically guarantee that the initial value
39336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
39436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
395e5eaf37440b8e337ab150c017df7c03faf846c51DRC * limiting step (just after the IDCT), a wildly out-of-range value is
39636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * possible if the input data is corrupt.  To avoid any chance of indexing
39736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * off the end of memory and getting a bad-pointer trap, we perform the
39836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * post-IDCT limiting thus:
399e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              x = range_limit[x & MASK];
40036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
40136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * samples.  Under normal circumstances this is more than enough range and
40236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * a correct output will be generated; with bogus input data the mask will
40336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * cause wraparound, and we will safely generate a bogus-but-in-range output.
40436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * For the post-IDCT step, we want to convert the data from signed to unsigned
40536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * representation by adding CENTERJSAMPLE at the same time that we limit it.
40636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * So the post-IDCT limiting table ends up looking like this:
40736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
40836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
40936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
41036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *   0,1,...,CENTERJSAMPLE-1
41136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Negative inputs select values from the upper half of the table after
41236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * masking.
41336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
41436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * We can save some space by overlapping the start of the post-IDCT table
41536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * with the simpler range limiting table.  The post-IDCT table begins at
41636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * sample_range_limit + CENTERJSAMPLE.
4172cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
4182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
419489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void)
42036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneprepare_range_limit_table (j_decompress_ptr cinfo)
42136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Allocate and fill in the sample_range_limit table */
42236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
4236eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  JSAMPLE *table;
42436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int i;
4252cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
42636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  table = (JSAMPLE *)
42736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4285de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
429e5eaf37440b8e337ab150c017df7c03faf846c51DRC  table += (MAXJSAMPLE+1);      /* allow negative subscripts of simple table */
43036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->sample_range_limit = table;
43136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
4325de454b291f48382648a5d1dc2aa0fca8b5786d4DRC  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * sizeof(JSAMPLE));
43336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Main part of "simple" table: limit[x] = x */
43436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (i = 0; i <= MAXJSAMPLE; i++)
43536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    table[i] = (JSAMPLE) i;
436e5eaf37440b8e337ab150c017df7c03faf846c51DRC  table += CENTERJSAMPLE;       /* Point to where post-IDCT table starts */
43736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* End of simple table, rest of first half of post-IDCT table */
43836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
43936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    table[i] = MAXJSAMPLE;
44036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Second half of post-IDCT table */
44136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  MEMZERO(table + (2 * (MAXJSAMPLE+1)),
4425de454b291f48382648a5d1dc2aa0fca8b5786d4DRC          (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
44336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
4445de454b291f48382648a5d1dc2aa0fca8b5786d4DRC          cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
44536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
44636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
44736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
44836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
44936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Master selection of decompression modules.
450bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This is done once at jpeg_start_decompress time.  We determine
45136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * which modules will be used and give them appropriate initialization calls.
452bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * We also initialize the decompressor input side to begin consuming data.
45336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
454bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Since jpeg_read_header has finished, we know what is in the SOF
455bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * and (first) SOS markers.  We also have all the application parameter
456bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * settings.
45736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
45836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
459489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void)
46036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanemaster_selection (j_decompress_ptr cinfo)
4612cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
46236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_master_ptr master = (my_master_ptr) cinfo->master;
463bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  boolean use_c_buffer;
46436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  long samplesperrow;
46536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  JDIMENSION jd_samplesperrow;
46636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
46736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Initialize dimensions and other stuff */
46836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_calc_output_dimensions(cinfo);
46936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  prepare_range_limit_table(cinfo);
47036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
47136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Width of an output scanline must be representable as JDIMENSION. */
47236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
47336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jd_samplesperrow = (JDIMENSION) samplesperrow;
47436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if ((long) jd_samplesperrow != samplesperrow)
47536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
47636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
47736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Initialize my private state */
47836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  master->pass_number = 0;
47936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  master->using_merged_upsample = use_merged_upsample(cinfo);
48036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
48136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Color quantizer selection */
482bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->quantizer_1pass = NULL;
483bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->quantizer_2pass = NULL;
484bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* No mode changes if not using buffered-image mode. */
485bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
486bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->enable_1pass_quant = FALSE;
487bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->enable_external_quant = FALSE;
488bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->enable_2pass_quant = FALSE;
489bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
49036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->quantize_colors) {
49136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (cinfo->raw_data_out)
49236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_NOTIMPL);
493bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* 2-pass quantizer only works in 3-component color space. */
494bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->out_color_components != 3) {
495bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->enable_1pass_quant = TRUE;
496bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->enable_external_quant = FALSE;
497bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->enable_2pass_quant = FALSE;
498bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->colormap = NULL;
499bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    } else if (cinfo->colormap != NULL) {
500bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->enable_external_quant = TRUE;
501bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    } else if (cinfo->two_pass_quantize) {
502bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->enable_2pass_quant = TRUE;
503bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    } else {
504bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->enable_1pass_quant = TRUE;
505bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
506bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
507bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->enable_1pass_quant) {
508bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef QUANT_1PASS_SUPPORTED
509bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      jinit_1pass_quantizer(cinfo);
510bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      master->quantizer_1pass = cinfo->cquantize;
51136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#else
512bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      ERREXIT(cinfo, JERR_NOT_COMPILED);
51336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
514bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
51536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
516bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* We use the 2-pass code to map to external colormaps. */
517bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
51836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef QUANT_2PASS_SUPPORTED
51936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      jinit_2pass_quantizer(cinfo);
520bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      master->quantizer_2pass = cinfo->cquantize;
52136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#else
52236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_NOT_COMPILED);
52336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
52436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
525bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* If both quantizers are initialized, the 2-pass one is left active;
526bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * this is necessary for starting with quantization to an external map.
527bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
52836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
52936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
53036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Post-processing: in particular, color conversion first */
53136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (! cinfo->raw_data_out) {
53236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (master->using_merged_upsample) {
53336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef UPSAMPLE_MERGING_SUPPORTED
53436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      jinit_merged_upsampler(cinfo); /* does color conversion too */
53536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#else
53636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_NOT_COMPILED);
53736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
53836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    } else {
53936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      jinit_color_deconverter(cinfo);
54036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      jinit_upsampler(cinfo);
54136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
542bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
54336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
54436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Inverse DCT */
54536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jinit_inverse_dct(cinfo);
54636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Entropy decoding: either Huffman or arithmetic coding. */
54736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->arith_code) {
54866f97e6820e2cc9ef7429ea36285c80ffda87c8fDRC#ifdef D_ARITH_CODING_SUPPORTED
5491e247ac854f8e33682bcfea475f6bccc42377208Guido Vollbeding    jinit_arith_decoder(cinfo);
55066f97e6820e2cc9ef7429ea36285c80ffda87c8fDRC#else
55136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
55266f97e6820e2cc9ef7429ea36285c80ffda87c8fDRC#endif
553bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else {
554bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->progressive_mode) {
555bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef D_PROGRESSIVE_SUPPORTED
556bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      jinit_phuff_decoder(cinfo);
557bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#else
558bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      ERREXIT(cinfo, JERR_NOT_COMPILED);
55936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
560bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    } else
561bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      jinit_huff_decoder(cinfo);
562bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
56336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
564bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Initialize principal buffer controllers. */
565bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
566bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  jinit_d_coef_controller(cinfo, use_c_buffer);
567bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
568bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (! cinfo->raw_data_out)
569bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
57036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
57136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* We can now tell the memory manager to allocate virtual arrays. */
57236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
573bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
574bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Initialize input side of decompressor to consume first scan. */
575bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  (*cinfo->inputctl->start_input_pass) (cinfo);
576bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
5770ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC  /* Set the first and last iMCU columns to decompress from single-scan images.
5780ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC   * By default, decompress all of the iMCU columns.
5790ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC   */
5800ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC  cinfo->master->first_iMCU_col = 0;
5810ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC  cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;
5820ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC
583bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef D_MULTISCAN_FILES_SUPPORTED
584bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* If jpeg_start_decompress will read the whole file, initialize
585bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   * progress monitoring appropriately.  The input step is counted
586bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   * as one pass.
587bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   */
588bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->progress != NULL && ! cinfo->buffered_image &&
589bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->inputctl->has_multiple_scans) {
590bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    int nscans;
591bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Estimate number of scans to set pass_limit. */
592bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->progressive_mode) {
593bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
594bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      nscans = 2 + 3 * cinfo->num_components;
595bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    } else {
596bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
597bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      nscans = cinfo->num_components;
598bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
599bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->progress->pass_counter = 0L;
600bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
601bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->progress->completed_passes = 0;
602bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
603bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Count the input pass as done */
604bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->pass_number++;
605bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
606bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif /* D_MULTISCAN_FILES_SUPPORTED */
60736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
60836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
60936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
61036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
61136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Per-pass setup.
612bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This is called at the beginning of each output pass.  We determine which
613bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * modules will be active during this pass and give them appropriate
614bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * start_pass calls.  We also set is_dummy_pass to indicate whether this
615bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * is a "real" output pass or a dummy pass for color quantization.
6165ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * (In the latter case, jdapistd.c will crank the pass to completion.)
61736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
6182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
619489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
620bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneprepare_for_output_pass (j_decompress_ptr cinfo)
62136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
62236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_master_ptr master = (my_master_ptr) cinfo->master;
6232cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
624bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (master->pub.is_dummy_pass) {
625bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef QUANT_2PASS_SUPPORTED
626bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Final pass of 2-pass quantization */
627bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->pub.is_dummy_pass = FALSE;
628bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
629bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
630bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
631bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#else
632bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT(cinfo, JERR_NOT_COMPILED);
633bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif /* QUANT_2PASS_SUPPORTED */
634bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else {
635bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
636bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      /* Select new quantization method */
637bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
638e5eaf37440b8e337ab150c017df7c03faf846c51DRC        cinfo->cquantize = master->quantizer_2pass;
639e5eaf37440b8e337ab150c017df7c03faf846c51DRC        master->pub.is_dummy_pass = TRUE;
640bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      } else if (cinfo->enable_1pass_quant) {
641e5eaf37440b8e337ab150c017df7c03faf846c51DRC        cinfo->cquantize = master->quantizer_1pass;
642bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      } else {
643e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT(cinfo, JERR_MODE_CHANGE);
644bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      }
64536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
646bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->idct->start_pass) (cinfo);
647bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->coef->start_output_pass) (cinfo);
64836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (! cinfo->raw_data_out) {
64936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      if (! master->using_merged_upsample)
650e5eaf37440b8e337ab150c017df7c03faf846c51DRC        (*cinfo->cconvert->start_pass) (cinfo);
65136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->upsample->start_pass) (cinfo);
65236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      if (cinfo->quantize_colors)
653e5eaf37440b8e337ab150c017df7c03faf846c51DRC        (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
65436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->post->start_pass) (cinfo,
655e5eaf37440b8e337ab150c017df7c03faf846c51DRC            (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
656bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
65736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
65836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
6592cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
66036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Set up progress monitor's pass info if present */
66136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->progress != NULL) {
66236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->progress->completed_passes = master->pass_number;
663bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->progress->total_passes = master->pass_number +
664e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                    (master->pub.is_dummy_pass ? 2 : 1);
665bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* In buffered-image mode, we assume one more output pass if EOI not
666bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * yet reached, but no more passes if EOI has been reached.
667bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
668bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
669bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
670bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
67136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
67236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
6732cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
6742cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
67536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
676bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Finish up at end of an output pass.
67736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
67836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
679489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
680bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanefinish_output_pass (j_decompress_ptr cinfo)
68136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
68236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_master_ptr master = (my_master_ptr) cinfo->master;
68336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
684bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->quantize_colors)
68536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->cquantize->finish_pass) (cinfo);
686bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->pass_number++;
687bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
688bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
689bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
690bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef D_MULTISCAN_FILES_SUPPORTED
691bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
692bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
693bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Switch to a new external colormap between output passes.
694bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
695bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
696489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
697bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejpeg_new_colormap (j_decompress_ptr cinfo)
698bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
699bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  my_master_ptr master = (my_master_ptr) cinfo->master;
700bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
701bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Prevent application from calling me at wrong times */
702bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->global_state != DSTATE_BUFIMAGE)
703bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
704bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
705bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
706bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->colormap != NULL) {
707bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Select 2-pass quantizer for external colormap use */
708bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->cquantize = master->quantizer_2pass;
709bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Notify quantizer of colormap change */
710bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->cquantize->new_color_map) (cinfo);
711bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    master->pub.is_dummy_pass = FALSE; /* just in case */
712bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else
713bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT(cinfo, JERR_MODE_CHANGE);
71436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
71536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
716bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif /* D_MULTISCAN_FILES_SUPPORTED */
717bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
71836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
71936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
720bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Initialize master decompression control and select active modules.
721bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This is performed at the start of jpeg_start_decompress.
72236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
72336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
724489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
72536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejinit_master_decompress (j_decompress_ptr cinfo)
72636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
7270ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC  my_master_ptr master = (my_master_ptr) cinfo->master;
7282cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
729bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->pub.prepare_for_output_pass = prepare_for_output_pass;
730bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->pub.finish_output_pass = finish_output_pass;
731bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
732bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  master->pub.is_dummy_pass = FALSE;
7330ef076fb7b326dc201b4ab3bd30fefd4e35ad1c4DRC  master->pub.jinit_upsampler_no_alloc = FALSE;
7342cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
73536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  master_selection(cinfo);
7362cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
737