1991-10-07 02:00:00 +02:00
|
|
|
/*
|
|
|
|
* jcmaster.c
|
|
|
|
*
|
1998-03-27 03:00:00 +03:00
|
|
|
* Copyright (C) 1991-1997, Thomas G. Lane.
|
2011-01-16 03:00:00 +03:00
|
|
|
* Modified 2003-2011 by Guido Vollbeding.
|
1991-10-07 02:00:00 +02:00
|
|
|
* This file is part of the Independent JPEG Group's software.
|
|
|
|
* For conditions of distribution and use, see the accompanying README file.
|
|
|
|
*
|
1994-09-24 04:00:00 +04:00
|
|
|
* This file contains master control logic for the JPEG compressor.
|
1995-08-02 04:00:00 +04:00
|
|
|
* These routines are concerned with parameter validation, initial setup,
|
|
|
|
* and inter-pass control (determining the number of passes and the work
|
|
|
|
* to be done in each pass).
|
1991-10-07 02:00:00 +02:00
|
|
|
*/
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
#define JPEG_INTERNALS
|
1991-10-07 02:00:00 +02:00
|
|
|
#include "jinclude.h"
|
1994-09-24 04:00:00 +04:00
|
|
|
#include "jpeglib.h"
|
1991-10-07 02:00:00 +02:00
|
|
|
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Private state */
|
1991-10-07 02:00:00 +02:00
|
|
|
|
1995-08-02 04:00:00 +04:00
|
|
|
typedef enum {
|
|
|
|
main_pass, /* input data, also do first output step */
|
|
|
|
huff_opt_pass, /* Huffman code optimization pass */
|
|
|
|
output_pass /* data output pass */
|
|
|
|
} c_pass_type;
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
typedef struct {
|
|
|
|
struct jpeg_comp_master pub; /* public fields */
|
1991-10-07 02:00:00 +02:00
|
|
|
|
1995-08-02 04:00:00 +04:00
|
|
|
c_pass_type pass_type; /* the type of the current pass */
|
|
|
|
|
|
|
|
int pass_number; /* # of passes completed */
|
|
|
|
int total_passes; /* total # of passes needed */
|
|
|
|
|
|
|
|
int scan_number; /* current index in scan_info[] */
|
1994-09-24 04:00:00 +04:00
|
|
|
} my_comp_master;
|
|
|
|
|
|
|
|
typedef my_comp_master * my_master_ptr;
|
1991-10-07 02:00:00 +02:00
|
|
|
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/*
|
|
|
|
* Support routines that do various essential calculations.
|
|
|
|
*/
|
1991-10-07 02:00:00 +02:00
|
|
|
|
2009-06-27 04:00:00 +04:00
|
|
|
/*
|
|
|
|
* Compute JPEG image dimensions and related values.
|
|
|
|
* NOTE: this is exported for possible use by application.
|
|
|
|
* Hence it mustn't do anything that can't be done twice.
|
|
|
|
*/
|
|
|
|
|
|
|
|
GLOBAL(void)
|
|
|
|
jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
|
|
|
|
/* Do computations that are needed before master selection phase */
|
|
|
|
{
|
|
|
|
#ifdef DCT_SCALING_SUPPORTED
|
|
|
|
|
2011-01-16 03:00:00 +03:00
|
|
|
/* Sanity check on input image dimensions to prevent overflow in
|
|
|
|
* following calculation.
|
|
|
|
* We do check jpeg_width and jpeg_height in initial_setup below,
|
|
|
|
* but image_width and image_height can come from arbitrary data,
|
|
|
|
* and we need some space for multiplication by block_size.
|
|
|
|
*/
|
|
|
|
if (((long) cinfo->image_width >> 24) || ((long) cinfo->image_height >> 24))
|
|
|
|
ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
|
|
|
|
|
2009-06-27 04:00:00 +04:00
|
|
|
/* Compute actual JPEG image dimensions and DCT scaling choices. */
|
2011-01-16 03:00:00 +03:00
|
|
|
if (cinfo->scale_num >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/1 scaling */
|
|
|
|
cinfo->jpeg_width = cinfo->image_width * cinfo->block_size;
|
|
|
|
cinfo->jpeg_height = cinfo->image_height * cinfo->block_size;
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 1;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 1;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 2 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/2 scaling */
|
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 2L);
|
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 2L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 2;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 2;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 3 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/3 scaling */
|
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 3L);
|
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 3L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 3;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 3;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 4 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/4 scaling */
|
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 4L);
|
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 4L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 4;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 4;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 5 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/5 scaling */
|
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 5L);
|
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 5L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 5;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 5;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 6 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/6 scaling */
|
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 6L);
|
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 6L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 6;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 6;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 7 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/7 scaling */
|
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 7L);
|
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 7L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 7;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 7;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 8 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/8 scaling */
|
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 8L);
|
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 8L);
|
2010-01-10 03:00:00 +03:00
|
|
|
cinfo->min_DCT_h_scaled_size = 8;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 8;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 9 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/9 scaling */
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 9L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 9L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 9;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 9;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 10 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/10 scaling */
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 10L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 10L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 10;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 10;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 11 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/11 scaling */
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 11L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 11L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 11;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 11;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 12 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/12 scaling */
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 12L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 12L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 12;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 12;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 13 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/13 scaling */
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 13L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 13L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 13;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 13;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 14 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/14 scaling */
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 14L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 14L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 14;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 14;
|
2011-01-16 03:00:00 +03:00
|
|
|
} else if (cinfo->scale_num * 15 >= cinfo->scale_denom * cinfo->block_size) {
|
|
|
|
/* Provide block_size/15 scaling */
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 15L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 15L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 15;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 15;
|
|
|
|
} else {
|
2011-01-16 03:00:00 +03:00
|
|
|
/* Provide block_size/16 scaling */
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_width = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 16L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->jpeg_height = (JDIMENSION)
|
2011-01-16 03:00:00 +03:00
|
|
|
jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 16L);
|
2009-06-27 04:00:00 +04:00
|
|
|
cinfo->min_DCT_h_scaled_size = 16;
|
|
|
|
cinfo->min_DCT_v_scaled_size = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !DCT_SCALING_SUPPORTED */
|
|
|
|
|
|
|
|
/* Hardwire it to "no scaling" */
|
|
|
|
cinfo->jpeg_width = cinfo->image_width;
|
|
|
|
cinfo->jpeg_height = cinfo->image_height;
|
|
|
|
cinfo->min_DCT_h_scaled_size = DCTSIZE;
|
|
|
|
cinfo->min_DCT_v_scaled_size = DCTSIZE;
|
|
|
|
|
|
|
|
#endif /* DCT_SCALING_SUPPORTED */
|
2010-01-10 03:00:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL(void)
|
|
|
|
jpeg_calc_trans_dimensions (j_compress_ptr cinfo)
|
|
|
|
{
|
2011-01-16 03:00:00 +03:00
|
|
|
if (cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size)
|
2010-01-10 03:00:00 +03:00
|
|
|
ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
|
|
|
|
cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size);
|
|
|
|
|
|
|
|
cinfo->block_size = cinfo->min_DCT_h_scaled_size;
|
2009-06-27 04:00:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-02-07 03:00:00 +03:00
|
|
|
LOCAL(void)
|
2010-01-10 03:00:00 +03:00
|
|
|
initial_setup (j_compress_ptr cinfo, boolean transcode_only)
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Do computations that are needed before master selection phase */
|
1991-10-07 02:00:00 +02:00
|
|
|
{
|
2009-06-27 04:00:00 +04:00
|
|
|
int ci, ssize;
|
1991-10-07 02:00:00 +02:00
|
|
|
jpeg_component_info *compptr;
|
1994-09-24 04:00:00 +04:00
|
|
|
long samplesperrow;
|
|
|
|
JDIMENSION jd_samplesperrow;
|
|
|
|
|
2010-01-10 03:00:00 +03:00
|
|
|
if (transcode_only)
|
|
|
|
jpeg_calc_trans_dimensions(cinfo);
|
|
|
|
else
|
|
|
|
jpeg_calc_jpeg_dimensions(cinfo);
|
2009-06-27 04:00:00 +04:00
|
|
|
|
2011-01-16 03:00:00 +03:00
|
|
|
/* Sanity check on block_size */
|
|
|
|
if (cinfo->block_size < 1 || cinfo->block_size > 16)
|
|
|
|
ERREXIT2(cinfo, JERR_BAD_DCTSIZE, cinfo->block_size, cinfo->block_size);
|
|
|
|
|
|
|
|
/* Derive natural_order from block_size */
|
|
|
|
switch (cinfo->block_size) {
|
|
|
|
case 2: cinfo->natural_order = jpeg_natural_order2; break;
|
|
|
|
case 3: cinfo->natural_order = jpeg_natural_order3; break;
|
|
|
|
case 4: cinfo->natural_order = jpeg_natural_order4; break;
|
|
|
|
case 5: cinfo->natural_order = jpeg_natural_order5; break;
|
|
|
|
case 6: cinfo->natural_order = jpeg_natural_order6; break;
|
|
|
|
case 7: cinfo->natural_order = jpeg_natural_order7; break;
|
|
|
|
default: cinfo->natural_order = jpeg_natural_order; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Derive lim_Se from block_size */
|
|
|
|
cinfo->lim_Se = cinfo->block_size < DCTSIZE ?
|
|
|
|
cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1;
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Sanity check on image dimensions */
|
2010-01-10 03:00:00 +03:00
|
|
|
if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 ||
|
|
|
|
cinfo->num_components <= 0 || cinfo->input_components <= 0)
|
1994-09-24 04:00:00 +04:00
|
|
|
ERREXIT(cinfo, JERR_EMPTY_IMAGE);
|
|
|
|
|
|
|
|
/* Make sure image isn't bigger than I can handle */
|
2009-06-27 04:00:00 +04:00
|
|
|
if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
|
|
|
|
(long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
|
1994-09-24 04:00:00 +04:00
|
|
|
ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
|
|
|
|
|
|
|
|
/* Width of an input scanline must be representable as JDIMENSION. */
|
|
|
|
samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
|
|
|
|
jd_samplesperrow = (JDIMENSION) samplesperrow;
|
|
|
|
if ((long) jd_samplesperrow != samplesperrow)
|
|
|
|
ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
|
|
|
|
|
|
|
|
/* For now, precision must match compiled-in value... */
|
|
|
|
if (cinfo->data_precision != BITS_IN_JSAMPLE)
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
|
|
|
|
|
|
|
|
/* Check that number of components won't exceed internal array sizes */
|
|
|
|
if (cinfo->num_components > MAX_COMPONENTS)
|
|
|
|
ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
|
|
|
|
MAX_COMPONENTS);
|
1991-10-07 02:00:00 +02:00
|
|
|
|
|
|
|
/* Compute maximum sampling factors; check factor validity */
|
|
|
|
cinfo->max_h_samp_factor = 1;
|
|
|
|
cinfo->max_v_samp_factor = 1;
|
1994-09-24 04:00:00 +04:00
|
|
|
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
|
|
|
ci++, compptr++) {
|
1991-10-07 02:00:00 +02:00
|
|
|
if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
|
|
|
|
compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
|
1994-09-24 04:00:00 +04:00
|
|
|
ERREXIT(cinfo, JERR_BAD_SAMPLING);
|
1991-10-07 02:00:00 +02:00
|
|
|
cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
|
|
|
|
compptr->h_samp_factor);
|
|
|
|
cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
|
|
|
|
compptr->v_samp_factor);
|
1994-09-24 04:00:00 +04:00
|
|
|
}
|
1991-10-07 02:00:00 +02:00
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Compute dimensions of components */
|
|
|
|
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
|
|
|
ci++, compptr++) {
|
1995-08-02 04:00:00 +04:00
|
|
|
/* Fill in the correct component_index value; don't rely on application */
|
|
|
|
compptr->component_index = ci;
|
2009-06-27 04:00:00 +04:00
|
|
|
/* In selecting the actual DCT scaling for each component, we try to
|
|
|
|
* scale down the chroma components via DCT scaling rather than downsampling.
|
|
|
|
* This saves time if the downsampler gets to use 1:1 scaling.
|
|
|
|
* Note this code adapts subsampling ratios which are powers of 2.
|
|
|
|
*/
|
|
|
|
ssize = 1;
|
|
|
|
#ifdef DCT_SCALING_SUPPORTED
|
|
|
|
while (cinfo->min_DCT_h_scaled_size * ssize <=
|
|
|
|
(cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
|
|
|
|
(cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
|
|
|
|
ssize = ssize * 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
|
|
|
|
ssize = 1;
|
|
|
|
#ifdef DCT_SCALING_SUPPORTED
|
|
|
|
while (cinfo->min_DCT_v_scaled_size * ssize <=
|
|
|
|
(cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
|
|
|
|
(cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
|
|
|
|
ssize = ssize * 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
|
|
|
|
|
|
|
|
/* We don't support DCT ratios larger than 2. */
|
|
|
|
if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
|
|
|
|
compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
|
|
|
|
else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
|
|
|
|
compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Size in DCT blocks */
|
|
|
|
compptr->width_in_blocks = (JDIMENSION)
|
2009-06-27 04:00:00 +04:00
|
|
|
jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
|
2010-01-10 03:00:00 +03:00
|
|
|
(long) (cinfo->max_h_samp_factor * cinfo->block_size));
|
1994-09-24 04:00:00 +04:00
|
|
|
compptr->height_in_blocks = (JDIMENSION)
|
2009-06-27 04:00:00 +04:00
|
|
|
jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
|
2010-01-10 03:00:00 +03:00
|
|
|
(long) (cinfo->max_v_samp_factor * cinfo->block_size));
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Size in samples */
|
|
|
|
compptr->downsampled_width = (JDIMENSION)
|
2009-06-27 04:00:00 +04:00
|
|
|
jdiv_round_up((long) cinfo->jpeg_width *
|
|
|
|
(long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
|
2010-01-10 03:00:00 +03:00
|
|
|
(long) (cinfo->max_h_samp_factor * cinfo->block_size));
|
1994-09-24 04:00:00 +04:00
|
|
|
compptr->downsampled_height = (JDIMENSION)
|
2009-06-27 04:00:00 +04:00
|
|
|
jdiv_round_up((long) cinfo->jpeg_height *
|
|
|
|
(long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
|
2010-01-10 03:00:00 +03:00
|
|
|
(long) (cinfo->max_v_samp_factor * cinfo->block_size));
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Mark component needed (this flag isn't actually used for compression) */
|
|
|
|
compptr->component_needed = TRUE;
|
1991-10-07 02:00:00 +02:00
|
|
|
}
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Compute number of fully interleaved MCU rows (number of times that
|
|
|
|
* main controller will call coefficient controller).
|
|
|
|
*/
|
|
|
|
cinfo->total_iMCU_rows = (JDIMENSION)
|
2009-06-27 04:00:00 +04:00
|
|
|
jdiv_round_up((long) cinfo->jpeg_height,
|
2010-01-10 03:00:00 +03:00
|
|
|
(long) (cinfo->max_v_samp_factor * cinfo->block_size));
|
1994-09-24 04:00:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-08-02 04:00:00 +04:00
|
|
|
#ifdef C_MULTISCAN_FILES_SUPPORTED
|
|
|
|
|
1996-02-07 03:00:00 +03:00
|
|
|
LOCAL(void)
|
1995-08-02 04:00:00 +04:00
|
|
|
validate_script (j_compress_ptr cinfo)
|
|
|
|
/* Verify that the scan script in cinfo->scan_info[] is valid; also
|
|
|
|
* determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
const jpeg_scan_info * scanptr;
|
|
|
|
int scanno, ncomps, ci, coefi, thisi;
|
|
|
|
int Ss, Se, Ah, Al;
|
|
|
|
boolean component_sent[MAX_COMPONENTS];
|
|
|
|
#ifdef C_PROGRESSIVE_SUPPORTED
|
|
|
|
int * last_bitpos_ptr;
|
|
|
|
int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
|
|
|
|
/* -1 until that coefficient has been seen; then last Al for it */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (cinfo->num_scans <= 0)
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
|
|
|
|
|
|
|
|
/* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
|
|
|
|
* for progressive JPEG, no scan can have this.
|
|
|
|
*/
|
|
|
|
scanptr = cinfo->scan_info;
|
|
|
|
if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
|
|
|
|
#ifdef C_PROGRESSIVE_SUPPORTED
|
|
|
|
cinfo->progressive_mode = TRUE;
|
|
|
|
last_bitpos_ptr = & last_bitpos[0][0];
|
|
|
|
for (ci = 0; ci < cinfo->num_components; ci++)
|
|
|
|
for (coefi = 0; coefi < DCTSIZE2; coefi++)
|
|
|
|
*last_bitpos_ptr++ = -1;
|
|
|
|
#else
|
|
|
|
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
cinfo->progressive_mode = FALSE;
|
|
|
|
for (ci = 0; ci < cinfo->num_components; ci++)
|
|
|
|
component_sent[ci] = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
|
|
|
|
/* Validate component indexes */
|
|
|
|
ncomps = scanptr->comps_in_scan;
|
|
|
|
if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
|
|
|
|
ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
|
|
|
|
for (ci = 0; ci < ncomps; ci++) {
|
|
|
|
thisi = scanptr->component_index[ci];
|
|
|
|
if (thisi < 0 || thisi >= cinfo->num_components)
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
|
|
|
|
/* Components must appear in SOF order within each scan */
|
|
|
|
if (ci > 0 && thisi <= scanptr->component_index[ci-1])
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
|
|
|
|
}
|
|
|
|
/* Validate progression parameters */
|
|
|
|
Ss = scanptr->Ss;
|
|
|
|
Se = scanptr->Se;
|
|
|
|
Ah = scanptr->Ah;
|
|
|
|
Al = scanptr->Al;
|
|
|
|
if (cinfo->progressive_mode) {
|
|
|
|
#ifdef C_PROGRESSIVE_SUPPORTED
|
1998-03-27 03:00:00 +03:00
|
|
|
/* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
|
|
|
|
* seems wrong: the upper bound ought to depend on data precision.
|
|
|
|
* Perhaps they really meant 0..N+1 for N-bit precision.
|
|
|
|
* Here we allow 0..10 for 8-bit data; Al larger than 10 results in
|
|
|
|
* out-of-range reconstructed DC values during the first DC scan,
|
|
|
|
* which might cause problems for some decoders.
|
|
|
|
*/
|
|
|
|
#if BITS_IN_JSAMPLE == 8
|
|
|
|
#define MAX_AH_AL 10
|
|
|
|
#else
|
|
|
|
#define MAX_AH_AL 13
|
|
|
|
#endif
|
1995-08-02 04:00:00 +04:00
|
|
|
if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
|
1998-03-27 03:00:00 +03:00
|
|
|
Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
|
1995-08-02 04:00:00 +04:00
|
|
|
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
|
|
|
|
if (Ss == 0) {
|
|
|
|
if (Se != 0) /* DC and AC together not OK */
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
|
|
|
|
} else {
|
|
|
|
if (ncomps != 1) /* AC scans must be for only one component */
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
|
|
|
|
}
|
|
|
|
for (ci = 0; ci < ncomps; ci++) {
|
|
|
|
last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
|
|
|
|
if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
|
|
|
|
for (coefi = Ss; coefi <= Se; coefi++) {
|
|
|
|
if (last_bitpos_ptr[coefi] < 0) {
|
|
|
|
/* first scan of this coefficient */
|
|
|
|
if (Ah != 0)
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
|
|
|
|
} else {
|
|
|
|
/* not first scan */
|
|
|
|
if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
|
|
|
|
}
|
|
|
|
last_bitpos_ptr[coefi] = Al;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
/* For sequential JPEG, all progression parameters must be these: */
|
|
|
|
if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
|
|
|
|
/* Make sure components are not sent twice */
|
|
|
|
for (ci = 0; ci < ncomps; ci++) {
|
|
|
|
thisi = scanptr->component_index[ci];
|
|
|
|
if (component_sent[thisi])
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
|
|
|
|
component_sent[thisi] = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now verify that everything got sent. */
|
|
|
|
if (cinfo->progressive_mode) {
|
|
|
|
#ifdef C_PROGRESSIVE_SUPPORTED
|
|
|
|
/* For progressive mode, we only check that at least some DC data
|
|
|
|
* got sent for each component; the spec does not require that all bits
|
|
|
|
* of all coefficients be transmitted. Would it be wiser to enforce
|
|
|
|
* transmission of all coefficient bits??
|
|
|
|
*/
|
|
|
|
for (ci = 0; ci < cinfo->num_components; ci++) {
|
|
|
|
if (last_bitpos[ci][0] < 0)
|
|
|
|
ERREXIT(cinfo, JERR_MISSING_DATA);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
for (ci = 0; ci < cinfo->num_components; ci++) {
|
|
|
|
if (! component_sent[ci])
|
|
|
|
ERREXIT(cinfo, JERR_MISSING_DATA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-10 03:00:00 +03:00
|
|
|
|
|
|
|
LOCAL(void)
|
|
|
|
reduce_script (j_compress_ptr cinfo)
|
|
|
|
/* Adapt scan script for use with reduced block size;
|
|
|
|
* assume that script has been validated before.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
jpeg_scan_info * scanptr;
|
|
|
|
int idxout, idxin;
|
|
|
|
|
|
|
|
/* Circumvent const declaration for this function */
|
|
|
|
scanptr = (jpeg_scan_info *) cinfo->scan_info;
|
|
|
|
idxout = 0;
|
|
|
|
|
|
|
|
for (idxin = 0; idxin < cinfo->num_scans; idxin++) {
|
|
|
|
/* After skipping, idxout becomes smaller than idxin */
|
|
|
|
if (idxin != idxout)
|
|
|
|
/* Copy rest of data;
|
|
|
|
* note we stay in given chunk of allocated memory.
|
|
|
|
*/
|
|
|
|
scanptr[idxout] = scanptr[idxin];
|
|
|
|
if (scanptr[idxout].Ss > cinfo->lim_Se)
|
|
|
|
/* Entire scan out of range - skip this entry */
|
|
|
|
continue;
|
|
|
|
if (scanptr[idxout].Se > cinfo->lim_Se)
|
|
|
|
/* Limit scan to end of block */
|
|
|
|
scanptr[idxout].Se = cinfo->lim_Se;
|
|
|
|
idxout++;
|
|
|
|
}
|
|
|
|
|
|
|
|
cinfo->num_scans = idxout;
|
|
|
|
}
|
|
|
|
|
1995-08-02 04:00:00 +04:00
|
|
|
#endif /* C_MULTISCAN_FILES_SUPPORTED */
|
|
|
|
|
|
|
|
|
1996-02-07 03:00:00 +03:00
|
|
|
LOCAL(void)
|
1995-08-02 04:00:00 +04:00
|
|
|
select_scan_parameters (j_compress_ptr cinfo)
|
|
|
|
/* Set up the scan parameters for the current scan */
|
|
|
|
{
|
|
|
|
int ci;
|
|
|
|
|
|
|
|
#ifdef C_MULTISCAN_FILES_SUPPORTED
|
|
|
|
if (cinfo->scan_info != NULL) {
|
|
|
|
/* Prepare for current scan --- the script is already validated */
|
|
|
|
my_master_ptr master = (my_master_ptr) cinfo->master;
|
|
|
|
const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
|
|
|
|
|
|
|
|
cinfo->comps_in_scan = scanptr->comps_in_scan;
|
|
|
|
for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
|
|
|
|
cinfo->cur_comp_info[ci] =
|
|
|
|
&cinfo->comp_info[scanptr->component_index[ci]];
|
|
|
|
}
|
2010-01-10 03:00:00 +03:00
|
|
|
if (cinfo->progressive_mode) {
|
|
|
|
cinfo->Ss = scanptr->Ss;
|
|
|
|
cinfo->Se = scanptr->Se;
|
|
|
|
cinfo->Ah = scanptr->Ah;
|
|
|
|
cinfo->Al = scanptr->Al;
|
|
|
|
return;
|
|
|
|
}
|
1995-08-02 04:00:00 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Prepare for single sequential-JPEG scan containing all components */
|
|
|
|
if (cinfo->num_components > MAX_COMPS_IN_SCAN)
|
|
|
|
ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
|
|
|
|
MAX_COMPS_IN_SCAN);
|
|
|
|
cinfo->comps_in_scan = cinfo->num_components;
|
|
|
|
for (ci = 0; ci < cinfo->num_components; ci++) {
|
|
|
|
cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
|
|
|
|
}
|
|
|
|
}
|
2010-01-10 03:00:00 +03:00
|
|
|
cinfo->Ss = 0;
|
|
|
|
cinfo->Se = cinfo->block_size * cinfo->block_size - 1;
|
|
|
|
cinfo->Ah = 0;
|
|
|
|
cinfo->Al = 0;
|
1995-08-02 04:00:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-02-07 03:00:00 +03:00
|
|
|
LOCAL(void)
|
1994-09-24 04:00:00 +04:00
|
|
|
per_scan_setup (j_compress_ptr cinfo)
|
|
|
|
/* Do computations that are needed before processing a JPEG scan */
|
|
|
|
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
|
|
|
|
{
|
|
|
|
int ci, mcublks, tmp;
|
|
|
|
jpeg_component_info *compptr;
|
|
|
|
|
|
|
|
if (cinfo->comps_in_scan == 1) {
|
|
|
|
|
|
|
|
/* Noninterleaved (single-component) scan */
|
|
|
|
compptr = cinfo->cur_comp_info[0];
|
|
|
|
|
|
|
|
/* Overall image size in MCUs */
|
|
|
|
cinfo->MCUs_per_row = compptr->width_in_blocks;
|
|
|
|
cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
|
|
|
|
|
|
|
|
/* For noninterleaved scan, always one block per MCU */
|
|
|
|
compptr->MCU_width = 1;
|
|
|
|
compptr->MCU_height = 1;
|
|
|
|
compptr->MCU_blocks = 1;
|
2009-06-27 04:00:00 +04:00
|
|
|
compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
|
1994-09-24 04:00:00 +04:00
|
|
|
compptr->last_col_width = 1;
|
1995-03-15 03:00:00 +03:00
|
|
|
/* For noninterleaved scans, it is convenient to define last_row_height
|
|
|
|
* as the number of block rows present in the last iMCU row.
|
|
|
|
*/
|
|
|
|
tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
|
|
|
|
if (tmp == 0) tmp = compptr->v_samp_factor;
|
|
|
|
compptr->last_row_height = tmp;
|
1994-09-24 04:00:00 +04:00
|
|
|
|
|
|
|
/* Prepare array describing MCU composition */
|
|
|
|
cinfo->blocks_in_MCU = 1;
|
|
|
|
cinfo->MCU_membership[0] = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Interleaved (multi-component) scan */
|
|
|
|
if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
|
|
|
|
ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
|
|
|
|
MAX_COMPS_IN_SCAN);
|
|
|
|
|
|
|
|
/* Overall image size in MCUs */
|
|
|
|
cinfo->MCUs_per_row = (JDIMENSION)
|
2009-06-27 04:00:00 +04:00
|
|
|
jdiv_round_up((long) cinfo->jpeg_width,
|
2010-01-10 03:00:00 +03:00
|
|
|
(long) (cinfo->max_h_samp_factor * cinfo->block_size));
|
1994-09-24 04:00:00 +04:00
|
|
|
cinfo->MCU_rows_in_scan = (JDIMENSION)
|
2009-06-27 04:00:00 +04:00
|
|
|
jdiv_round_up((long) cinfo->jpeg_height,
|
2010-01-10 03:00:00 +03:00
|
|
|
(long) (cinfo->max_v_samp_factor * cinfo->block_size));
|
1994-09-24 04:00:00 +04:00
|
|
|
|
|
|
|
cinfo->blocks_in_MCU = 0;
|
|
|
|
|
|
|
|
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
|
|
|
compptr = cinfo->cur_comp_info[ci];
|
|
|
|
/* Sampling factors give # of blocks of component in each MCU */
|
|
|
|
compptr->MCU_width = compptr->h_samp_factor;
|
|
|
|
compptr->MCU_height = compptr->v_samp_factor;
|
|
|
|
compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
|
2009-06-27 04:00:00 +04:00
|
|
|
compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Figure number of non-dummy blocks in last MCU column & row */
|
|
|
|
tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
|
|
|
|
if (tmp == 0) tmp = compptr->MCU_width;
|
|
|
|
compptr->last_col_width = tmp;
|
|
|
|
tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
|
|
|
|
if (tmp == 0) tmp = compptr->MCU_height;
|
|
|
|
compptr->last_row_height = tmp;
|
|
|
|
/* Prepare array describing MCU composition */
|
|
|
|
mcublks = compptr->MCU_blocks;
|
1995-08-02 04:00:00 +04:00
|
|
|
if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
|
1994-09-24 04:00:00 +04:00
|
|
|
ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
|
|
|
|
while (mcublks-- > 0) {
|
|
|
|
cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert restart specified in rows to actual MCU count. */
|
|
|
|
/* Note that count must fit in 16 bits, so we provide limiting. */
|
|
|
|
if (cinfo->restart_in_rows > 0) {
|
|
|
|
long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
|
|
|
|
cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
|
1991-10-07 02:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/*
|
|
|
|
* Per-pass setup.
|
|
|
|
* This is called at the beginning of each pass. We determine which modules
|
|
|
|
* will be active during this pass and give them appropriate start_pass calls.
|
|
|
|
* We also set is_last_pass to indicate whether any more passes will be
|
|
|
|
* required.
|
|
|
|
*/
|
|
|
|
|
1996-02-07 03:00:00 +03:00
|
|
|
METHODDEF(void)
|
1994-09-24 04:00:00 +04:00
|
|
|
prepare_for_pass (j_compress_ptr cinfo)
|
|
|
|
{
|
|
|
|
my_master_ptr master = (my_master_ptr) cinfo->master;
|
|
|
|
|
1995-08-02 04:00:00 +04:00
|
|
|
switch (master->pass_type) {
|
|
|
|
case main_pass:
|
|
|
|
/* Initial pass: will collect input data, and do either Huffman
|
|
|
|
* optimization or data output for the first scan.
|
|
|
|
*/
|
|
|
|
select_scan_parameters(cinfo);
|
|
|
|
per_scan_setup(cinfo);
|
1994-09-24 04:00:00 +04:00
|
|
|
if (! cinfo->raw_data_in) {
|
|
|
|
(*cinfo->cconvert->start_pass) (cinfo);
|
|
|
|
(*cinfo->downsample->start_pass) (cinfo);
|
|
|
|
(*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
|
|
|
|
}
|
|
|
|
(*cinfo->fdct->start_pass) (cinfo);
|
1995-08-02 04:00:00 +04:00
|
|
|
(*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
|
|
|
|
(*cinfo->coef->start_pass) (cinfo,
|
|
|
|
(master->total_passes > 1 ?
|
|
|
|
JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
|
1994-09-24 04:00:00 +04:00
|
|
|
(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
|
1995-08-02 04:00:00 +04:00
|
|
|
if (cinfo->optimize_coding) {
|
|
|
|
/* No immediate data output; postpone writing frame/scan headers */
|
1994-09-24 04:00:00 +04:00
|
|
|
master->pub.call_pass_startup = FALSE;
|
1995-08-02 04:00:00 +04:00
|
|
|
} else {
|
|
|
|
/* Will write frame/scan headers at first jpeg_write_scanlines call */
|
|
|
|
master->pub.call_pass_startup = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef ENTROPY_OPT_SUPPORTED
|
|
|
|
case huff_opt_pass:
|
|
|
|
/* Do Huffman optimization for a scan after the first one. */
|
|
|
|
select_scan_parameters(cinfo);
|
|
|
|
per_scan_setup(cinfo);
|
1998-03-28 03:00:00 +03:00
|
|
|
if (cinfo->Ss != 0 || cinfo->Ah == 0) {
|
1994-09-24 04:00:00 +04:00
|
|
|
(*cinfo->entropy->start_pass) (cinfo, TRUE);
|
|
|
|
(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
|
1995-08-02 04:00:00 +04:00
|
|
|
master->pub.call_pass_startup = FALSE;
|
1994-09-24 04:00:00 +04:00
|
|
|
break;
|
|
|
|
}
|
1995-08-02 04:00:00 +04:00
|
|
|
/* Special case: Huffman DC refinement scans need no Huffman table
|
|
|
|
* and therefore we can skip the optimization pass for them.
|
|
|
|
*/
|
|
|
|
master->pass_type = output_pass;
|
|
|
|
master->pass_number++;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
#endif
|
|
|
|
case output_pass:
|
|
|
|
/* Do a data-output pass. */
|
|
|
|
/* We need not repeat per-scan setup if prior optimization pass did it. */
|
|
|
|
if (! cinfo->optimize_coding) {
|
|
|
|
select_scan_parameters(cinfo);
|
|
|
|
per_scan_setup(cinfo);
|
|
|
|
}
|
|
|
|
(*cinfo->entropy->start_pass) (cinfo, FALSE);
|
|
|
|
(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
|
|
|
|
/* We emit frame/scan headers now */
|
|
|
|
if (master->scan_number == 0)
|
|
|
|
(*cinfo->marker->write_frame_header) (cinfo);
|
|
|
|
(*cinfo->marker->write_scan_header) (cinfo);
|
|
|
|
master->pub.call_pass_startup = FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
1994-09-24 04:00:00 +04:00
|
|
|
}
|
|
|
|
|
1995-08-02 04:00:00 +04:00
|
|
|
master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/* Set up progress monitor's pass info if present */
|
|
|
|
if (cinfo->progress != NULL) {
|
|
|
|
cinfo->progress->completed_passes = master->pass_number;
|
1995-08-02 04:00:00 +04:00
|
|
|
cinfo->progress->total_passes = master->total_passes;
|
1994-09-24 04:00:00 +04:00
|
|
|
}
|
|
|
|
}
|
1991-10-07 02:00:00 +02:00
|
|
|
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/*
|
|
|
|
* Special start-of-pass hook.
|
|
|
|
* This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
|
|
|
|
* In single-pass processing, we need this hook because we don't want to
|
|
|
|
* write frame/scan headers during jpeg_start_compress; we want to let the
|
|
|
|
* application write COM markers etc. between jpeg_start_compress and the
|
|
|
|
* jpeg_write_scanlines loop.
|
|
|
|
* In multi-pass processing, this routine is not used.
|
|
|
|
*/
|
|
|
|
|
1996-02-07 03:00:00 +03:00
|
|
|
METHODDEF(void)
|
1994-09-24 04:00:00 +04:00
|
|
|
pass_startup (j_compress_ptr cinfo)
|
|
|
|
{
|
|
|
|
cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
|
|
|
|
|
|
|
|
(*cinfo->marker->write_frame_header) (cinfo);
|
|
|
|
(*cinfo->marker->write_scan_header) (cinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finish up at end of pass.
|
|
|
|
*/
|
|
|
|
|
1996-02-07 03:00:00 +03:00
|
|
|
METHODDEF(void)
|
1994-09-24 04:00:00 +04:00
|
|
|
finish_pass_master (j_compress_ptr cinfo)
|
|
|
|
{
|
1995-08-02 04:00:00 +04:00
|
|
|
my_master_ptr master = (my_master_ptr) cinfo->master;
|
1994-09-24 04:00:00 +04:00
|
|
|
|
1995-08-02 04:00:00 +04:00
|
|
|
/* The entropy coder always needs an end-of-pass call,
|
|
|
|
* either to analyze statistics or to flush its output buffer.
|
1994-09-24 04:00:00 +04:00
|
|
|
*/
|
|
|
|
(*cinfo->entropy->finish_pass) (cinfo);
|
1995-08-02 04:00:00 +04:00
|
|
|
|
|
|
|
/* Update state for next pass */
|
|
|
|
switch (master->pass_type) {
|
|
|
|
case main_pass:
|
|
|
|
/* next pass is either output of scan 0 (after optimization)
|
|
|
|
* or output of scan 1 (if no optimization).
|
|
|
|
*/
|
|
|
|
master->pass_type = output_pass;
|
|
|
|
if (! cinfo->optimize_coding)
|
|
|
|
master->scan_number++;
|
|
|
|
break;
|
|
|
|
case huff_opt_pass:
|
|
|
|
/* next pass is always output of current scan */
|
|
|
|
master->pass_type = output_pass;
|
|
|
|
break;
|
|
|
|
case output_pass:
|
|
|
|
/* next pass is either optimization or output of next scan */
|
|
|
|
if (cinfo->optimize_coding)
|
|
|
|
master->pass_type = huff_opt_pass;
|
|
|
|
master->scan_number++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
master->pass_number++;
|
1994-09-24 04:00:00 +04:00
|
|
|
}
|
1991-10-07 02:00:00 +02:00
|
|
|
|
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
/*
|
|
|
|
* Initialize master compression control.
|
|
|
|
*/
|
|
|
|
|
1996-02-07 03:00:00 +03:00
|
|
|
GLOBAL(void)
|
1995-08-02 04:00:00 +04:00
|
|
|
jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
|
1994-09-24 04:00:00 +04:00
|
|
|
{
|
|
|
|
my_master_ptr master;
|
1991-10-07 02:00:00 +02:00
|
|
|
|
1994-09-24 04:00:00 +04:00
|
|
|
master = (my_master_ptr)
|
|
|
|
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
|
|
|
SIZEOF(my_comp_master));
|
|
|
|
cinfo->master = (struct jpeg_comp_master *) master;
|
|
|
|
master->pub.prepare_for_pass = prepare_for_pass;
|
|
|
|
master->pub.pass_startup = pass_startup;
|
|
|
|
master->pub.finish_pass = finish_pass_master;
|
1995-08-02 04:00:00 +04:00
|
|
|
master->pub.is_last_pass = FALSE;
|
|
|
|
|
|
|
|
/* Validate parameters, determine derived values */
|
2010-01-10 03:00:00 +03:00
|
|
|
initial_setup(cinfo, transcode_only);
|
1992-03-17 03:00:00 +03:00
|
|
|
|
1995-08-02 04:00:00 +04:00
|
|
|
if (cinfo->scan_info != NULL) {
|
|
|
|
#ifdef C_MULTISCAN_FILES_SUPPORTED
|
|
|
|
validate_script(cinfo);
|
2010-01-10 03:00:00 +03:00
|
|
|
if (cinfo->block_size < DCTSIZE)
|
|
|
|
reduce_script(cinfo);
|
1995-08-02 04:00:00 +04:00
|
|
|
#else
|
|
|
|
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
cinfo->progressive_mode = FALSE;
|
|
|
|
cinfo->num_scans = 1;
|
|
|
|
}
|
|
|
|
|
2010-01-10 03:00:00 +03:00
|
|
|
if ((cinfo->progressive_mode || cinfo->block_size < DCTSIZE) &&
|
|
|
|
!cinfo->arith_code) /* TEMPORARY HACK ??? */
|
|
|
|
/* assume default tables no good for progressive or downscale mode */
|
|
|
|
cinfo->optimize_coding = TRUE;
|
1995-08-02 04:00:00 +04:00
|
|
|
|
|
|
|
/* Initialize my private state */
|
|
|
|
if (transcode_only) {
|
|
|
|
/* no main pass in transcoding */
|
|
|
|
if (cinfo->optimize_coding)
|
|
|
|
master->pass_type = huff_opt_pass;
|
|
|
|
else
|
|
|
|
master->pass_type = output_pass;
|
|
|
|
} else {
|
|
|
|
/* for normal compression, first pass is always this type: */
|
|
|
|
master->pass_type = main_pass;
|
|
|
|
}
|
|
|
|
master->scan_number = 0;
|
|
|
|
master->pass_number = 0;
|
|
|
|
if (cinfo->optimize_coding)
|
|
|
|
master->total_passes = cinfo->num_scans * 2;
|
|
|
|
else
|
|
|
|
master->total_passes = cinfo->num_scans;
|
1991-10-07 02:00:00 +02:00
|
|
|
}
|