Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shoaib_ahmed 0:791a779d6220 1 /*
shoaib_ahmed 0:791a779d6220 2 * jcmaster.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1991-1997, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * Modified 2003-2013 by Guido Vollbeding.
shoaib_ahmed 0:791a779d6220 6 * This file is part of the Independent JPEG Group's software.
shoaib_ahmed 0:791a779d6220 7 * For conditions of distribution and use, see the accompanying README file.
shoaib_ahmed 0:791a779d6220 8 *
shoaib_ahmed 0:791a779d6220 9 * This file contains master control logic for the JPEG compressor.
shoaib_ahmed 0:791a779d6220 10 * These routines are concerned with parameter validation, initial setup,
shoaib_ahmed 0:791a779d6220 11 * and inter-pass control (determining the number of passes and the work
shoaib_ahmed 0:791a779d6220 12 * to be done in each pass).
shoaib_ahmed 0:791a779d6220 13 */
shoaib_ahmed 0:791a779d6220 14
shoaib_ahmed 0:791a779d6220 15 #define JPEG_INTERNALS
shoaib_ahmed 0:791a779d6220 16 #include "jinclude.h"
shoaib_ahmed 0:791a779d6220 17 #include "jpeglib.h"
shoaib_ahmed 0:791a779d6220 18
shoaib_ahmed 0:791a779d6220 19
shoaib_ahmed 0:791a779d6220 20 /* Private state */
shoaib_ahmed 0:791a779d6220 21
shoaib_ahmed 0:791a779d6220 22 typedef enum {
shoaib_ahmed 0:791a779d6220 23 main_pass, /* input data, also do first output step */
shoaib_ahmed 0:791a779d6220 24 huff_opt_pass, /* Huffman code optimization pass */
shoaib_ahmed 0:791a779d6220 25 output_pass /* data output pass */
shoaib_ahmed 0:791a779d6220 26 } c_pass_type;
shoaib_ahmed 0:791a779d6220 27
shoaib_ahmed 0:791a779d6220 28 typedef struct {
shoaib_ahmed 0:791a779d6220 29 struct jpeg_comp_master pub; /* public fields */
shoaib_ahmed 0:791a779d6220 30
shoaib_ahmed 0:791a779d6220 31 c_pass_type pass_type; /* the type of the current pass */
shoaib_ahmed 0:791a779d6220 32
shoaib_ahmed 0:791a779d6220 33 int pass_number; /* # of passes completed */
shoaib_ahmed 0:791a779d6220 34 int total_passes; /* total # of passes needed */
shoaib_ahmed 0:791a779d6220 35
shoaib_ahmed 0:791a779d6220 36 int scan_number; /* current index in scan_info[] */
shoaib_ahmed 0:791a779d6220 37 } my_comp_master;
shoaib_ahmed 0:791a779d6220 38
shoaib_ahmed 0:791a779d6220 39 typedef my_comp_master * my_master_ptr;
shoaib_ahmed 0:791a779d6220 40
shoaib_ahmed 0:791a779d6220 41
shoaib_ahmed 0:791a779d6220 42 /*
shoaib_ahmed 0:791a779d6220 43 * Support routines that do various essential calculations.
shoaib_ahmed 0:791a779d6220 44 */
shoaib_ahmed 0:791a779d6220 45
shoaib_ahmed 0:791a779d6220 46 /*
shoaib_ahmed 0:791a779d6220 47 * Compute JPEG image dimensions and related values.
shoaib_ahmed 0:791a779d6220 48 * NOTE: this is exported for possible use by application.
shoaib_ahmed 0:791a779d6220 49 * Hence it mustn't do anything that can't be done twice.
shoaib_ahmed 0:791a779d6220 50 */
shoaib_ahmed 0:791a779d6220 51
shoaib_ahmed 0:791a779d6220 52 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 53 jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 54 /* Do computations that are needed before master selection phase */
shoaib_ahmed 0:791a779d6220 55 {
shoaib_ahmed 0:791a779d6220 56 #ifdef DCT_SCALING_SUPPORTED
shoaib_ahmed 0:791a779d6220 57
shoaib_ahmed 0:791a779d6220 58 /* Sanity check on input image dimensions to prevent overflow in
shoaib_ahmed 0:791a779d6220 59 * following calculation.
shoaib_ahmed 0:791a779d6220 60 * We do check jpeg_width and jpeg_height in initial_setup below,
shoaib_ahmed 0:791a779d6220 61 * but image_width and image_height can come from arbitrary data,
shoaib_ahmed 0:791a779d6220 62 * and we need some space for multiplication by block_size.
shoaib_ahmed 0:791a779d6220 63 */
shoaib_ahmed 0:791a779d6220 64 if (((long) cinfo->image_width >> 24) || ((long) cinfo->image_height >> 24))
shoaib_ahmed 0:791a779d6220 65 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
shoaib_ahmed 0:791a779d6220 66
shoaib_ahmed 0:791a779d6220 67 /* Compute actual JPEG image dimensions and DCT scaling choices. */
shoaib_ahmed 0:791a779d6220 68 if (cinfo->scale_num >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 69 /* Provide block_size/1 scaling */
shoaib_ahmed 0:791a779d6220 70 cinfo->jpeg_width = cinfo->image_width * cinfo->block_size;
shoaib_ahmed 0:791a779d6220 71 cinfo->jpeg_height = cinfo->image_height * cinfo->block_size;
shoaib_ahmed 0:791a779d6220 72 cinfo->min_DCT_h_scaled_size = 1;
shoaib_ahmed 0:791a779d6220 73 cinfo->min_DCT_v_scaled_size = 1;
shoaib_ahmed 0:791a779d6220 74 } else if (cinfo->scale_num * 2 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 75 /* Provide block_size/2 scaling */
shoaib_ahmed 0:791a779d6220 76 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 77 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 2L);
shoaib_ahmed 0:791a779d6220 78 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 79 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 2L);
shoaib_ahmed 0:791a779d6220 80 cinfo->min_DCT_h_scaled_size = 2;
shoaib_ahmed 0:791a779d6220 81 cinfo->min_DCT_v_scaled_size = 2;
shoaib_ahmed 0:791a779d6220 82 } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 83 /* Provide block_size/3 scaling */
shoaib_ahmed 0:791a779d6220 84 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 85 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 3L);
shoaib_ahmed 0:791a779d6220 86 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 87 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 3L);
shoaib_ahmed 0:791a779d6220 88 cinfo->min_DCT_h_scaled_size = 3;
shoaib_ahmed 0:791a779d6220 89 cinfo->min_DCT_v_scaled_size = 3;
shoaib_ahmed 0:791a779d6220 90 } else if (cinfo->scale_num * 4 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 91 /* Provide block_size/4 scaling */
shoaib_ahmed 0:791a779d6220 92 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 93 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 4L);
shoaib_ahmed 0:791a779d6220 94 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 95 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 4L);
shoaib_ahmed 0:791a779d6220 96 cinfo->min_DCT_h_scaled_size = 4;
shoaib_ahmed 0:791a779d6220 97 cinfo->min_DCT_v_scaled_size = 4;
shoaib_ahmed 0:791a779d6220 98 } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 99 /* Provide block_size/5 scaling */
shoaib_ahmed 0:791a779d6220 100 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 101 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 5L);
shoaib_ahmed 0:791a779d6220 102 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 103 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 5L);
shoaib_ahmed 0:791a779d6220 104 cinfo->min_DCT_h_scaled_size = 5;
shoaib_ahmed 0:791a779d6220 105 cinfo->min_DCT_v_scaled_size = 5;
shoaib_ahmed 0:791a779d6220 106 } else if (cinfo->scale_num * 6 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 107 /* Provide block_size/6 scaling */
shoaib_ahmed 0:791a779d6220 108 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 109 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 6L);
shoaib_ahmed 0:791a779d6220 110 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 111 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 6L);
shoaib_ahmed 0:791a779d6220 112 cinfo->min_DCT_h_scaled_size = 6;
shoaib_ahmed 0:791a779d6220 113 cinfo->min_DCT_v_scaled_size = 6;
shoaib_ahmed 0:791a779d6220 114 } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 115 /* Provide block_size/7 scaling */
shoaib_ahmed 0:791a779d6220 116 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 117 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 7L);
shoaib_ahmed 0:791a779d6220 118 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 119 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 7L);
shoaib_ahmed 0:791a779d6220 120 cinfo->min_DCT_h_scaled_size = 7;
shoaib_ahmed 0:791a779d6220 121 cinfo->min_DCT_v_scaled_size = 7;
shoaib_ahmed 0:791a779d6220 122 } else if (cinfo->scale_num * 8 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 123 /* Provide block_size/8 scaling */
shoaib_ahmed 0:791a779d6220 124 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 125 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 8L);
shoaib_ahmed 0:791a779d6220 126 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 127 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 8L);
shoaib_ahmed 0:791a779d6220 128 cinfo->min_DCT_h_scaled_size = 8;
shoaib_ahmed 0:791a779d6220 129 cinfo->min_DCT_v_scaled_size = 8;
shoaib_ahmed 0:791a779d6220 130 } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 131 /* Provide block_size/9 scaling */
shoaib_ahmed 0:791a779d6220 132 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 133 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 9L);
shoaib_ahmed 0:791a779d6220 134 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 135 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 9L);
shoaib_ahmed 0:791a779d6220 136 cinfo->min_DCT_h_scaled_size = 9;
shoaib_ahmed 0:791a779d6220 137 cinfo->min_DCT_v_scaled_size = 9;
shoaib_ahmed 0:791a779d6220 138 } else if (cinfo->scale_num * 10 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 139 /* Provide block_size/10 scaling */
shoaib_ahmed 0:791a779d6220 140 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 141 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 10L);
shoaib_ahmed 0:791a779d6220 142 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 143 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 10L);
shoaib_ahmed 0:791a779d6220 144 cinfo->min_DCT_h_scaled_size = 10;
shoaib_ahmed 0:791a779d6220 145 cinfo->min_DCT_v_scaled_size = 10;
shoaib_ahmed 0:791a779d6220 146 } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 147 /* Provide block_size/11 scaling */
shoaib_ahmed 0:791a779d6220 148 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 149 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 11L);
shoaib_ahmed 0:791a779d6220 150 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 151 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 11L);
shoaib_ahmed 0:791a779d6220 152 cinfo->min_DCT_h_scaled_size = 11;
shoaib_ahmed 0:791a779d6220 153 cinfo->min_DCT_v_scaled_size = 11;
shoaib_ahmed 0:791a779d6220 154 } else if (cinfo->scale_num * 12 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 155 /* Provide block_size/12 scaling */
shoaib_ahmed 0:791a779d6220 156 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 157 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 12L);
shoaib_ahmed 0:791a779d6220 158 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 159 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 12L);
shoaib_ahmed 0:791a779d6220 160 cinfo->min_DCT_h_scaled_size = 12;
shoaib_ahmed 0:791a779d6220 161 cinfo->min_DCT_v_scaled_size = 12;
shoaib_ahmed 0:791a779d6220 162 } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 163 /* Provide block_size/13 scaling */
shoaib_ahmed 0:791a779d6220 164 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 165 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 13L);
shoaib_ahmed 0:791a779d6220 166 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 167 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 13L);
shoaib_ahmed 0:791a779d6220 168 cinfo->min_DCT_h_scaled_size = 13;
shoaib_ahmed 0:791a779d6220 169 cinfo->min_DCT_v_scaled_size = 13;
shoaib_ahmed 0:791a779d6220 170 } else if (cinfo->scale_num * 14 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 171 /* Provide block_size/14 scaling */
shoaib_ahmed 0:791a779d6220 172 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 173 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 14L);
shoaib_ahmed 0:791a779d6220 174 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 175 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 14L);
shoaib_ahmed 0:791a779d6220 176 cinfo->min_DCT_h_scaled_size = 14;
shoaib_ahmed 0:791a779d6220 177 cinfo->min_DCT_v_scaled_size = 14;
shoaib_ahmed 0:791a779d6220 178 } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 179 /* Provide block_size/15 scaling */
shoaib_ahmed 0:791a779d6220 180 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 181 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 15L);
shoaib_ahmed 0:791a779d6220 182 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 183 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 15L);
shoaib_ahmed 0:791a779d6220 184 cinfo->min_DCT_h_scaled_size = 15;
shoaib_ahmed 0:791a779d6220 185 cinfo->min_DCT_v_scaled_size = 15;
shoaib_ahmed 0:791a779d6220 186 } else {
shoaib_ahmed 0:791a779d6220 187 /* Provide block_size/16 scaling */
shoaib_ahmed 0:791a779d6220 188 cinfo->jpeg_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 189 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 16L);
shoaib_ahmed 0:791a779d6220 190 cinfo->jpeg_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 191 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 16L);
shoaib_ahmed 0:791a779d6220 192 cinfo->min_DCT_h_scaled_size = 16;
shoaib_ahmed 0:791a779d6220 193 cinfo->min_DCT_v_scaled_size = 16;
shoaib_ahmed 0:791a779d6220 194 }
shoaib_ahmed 0:791a779d6220 195
shoaib_ahmed 0:791a779d6220 196 #else /* !DCT_SCALING_SUPPORTED */
shoaib_ahmed 0:791a779d6220 197
shoaib_ahmed 0:791a779d6220 198 /* Hardwire it to "no scaling" */
shoaib_ahmed 0:791a779d6220 199 cinfo->jpeg_width = cinfo->image_width;
shoaib_ahmed 0:791a779d6220 200 cinfo->jpeg_height = cinfo->image_height;
shoaib_ahmed 0:791a779d6220 201 cinfo->min_DCT_h_scaled_size = DCTSIZE;
shoaib_ahmed 0:791a779d6220 202 cinfo->min_DCT_v_scaled_size = DCTSIZE;
shoaib_ahmed 0:791a779d6220 203
shoaib_ahmed 0:791a779d6220 204 #endif /* DCT_SCALING_SUPPORTED */
shoaib_ahmed 0:791a779d6220 205 }
shoaib_ahmed 0:791a779d6220 206
shoaib_ahmed 0:791a779d6220 207
shoaib_ahmed 0:791a779d6220 208 LOCAL(void)
shoaib_ahmed 0:791a779d6220 209 jpeg_calc_trans_dimensions (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 210 {
shoaib_ahmed 0:791a779d6220 211 if (cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size)
shoaib_ahmed 0:791a779d6220 212 ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
shoaib_ahmed 0:791a779d6220 213 cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size);
shoaib_ahmed 0:791a779d6220 214
shoaib_ahmed 0:791a779d6220 215 cinfo->block_size = cinfo->min_DCT_h_scaled_size;
shoaib_ahmed 0:791a779d6220 216 }
shoaib_ahmed 0:791a779d6220 217
shoaib_ahmed 0:791a779d6220 218
shoaib_ahmed 0:791a779d6220 219 LOCAL(void)
shoaib_ahmed 0:791a779d6220 220 initial_setup (j_compress_ptr cinfo, boolean transcode_only)
shoaib_ahmed 0:791a779d6220 221 /* Do computations that are needed before master selection phase */
shoaib_ahmed 0:791a779d6220 222 {
shoaib_ahmed 0:791a779d6220 223 int ci, ssize;
shoaib_ahmed 0:791a779d6220 224 jpeg_component_info *compptr;
shoaib_ahmed 0:791a779d6220 225
shoaib_ahmed 0:791a779d6220 226 if (transcode_only)
shoaib_ahmed 0:791a779d6220 227 jpeg_calc_trans_dimensions(cinfo);
shoaib_ahmed 0:791a779d6220 228 else
shoaib_ahmed 0:791a779d6220 229 jpeg_calc_jpeg_dimensions(cinfo);
shoaib_ahmed 0:791a779d6220 230
shoaib_ahmed 0:791a779d6220 231 /* Sanity check on block_size */
shoaib_ahmed 0:791a779d6220 232 if (cinfo->block_size < 1 || cinfo->block_size > 16)
shoaib_ahmed 0:791a779d6220 233 ERREXIT2(cinfo, JERR_BAD_DCTSIZE, cinfo->block_size, cinfo->block_size);
shoaib_ahmed 0:791a779d6220 234
shoaib_ahmed 0:791a779d6220 235 /* Derive natural_order from block_size */
shoaib_ahmed 0:791a779d6220 236 switch (cinfo->block_size) {
shoaib_ahmed 0:791a779d6220 237 case 2: cinfo->natural_order = jpeg_natural_order2; break;
shoaib_ahmed 0:791a779d6220 238 case 3: cinfo->natural_order = jpeg_natural_order3; break;
shoaib_ahmed 0:791a779d6220 239 case 4: cinfo->natural_order = jpeg_natural_order4; break;
shoaib_ahmed 0:791a779d6220 240 case 5: cinfo->natural_order = jpeg_natural_order5; break;
shoaib_ahmed 0:791a779d6220 241 case 6: cinfo->natural_order = jpeg_natural_order6; break;
shoaib_ahmed 0:791a779d6220 242 case 7: cinfo->natural_order = jpeg_natural_order7; break;
shoaib_ahmed 0:791a779d6220 243 default: cinfo->natural_order = jpeg_natural_order; break;
shoaib_ahmed 0:791a779d6220 244 }
shoaib_ahmed 0:791a779d6220 245
shoaib_ahmed 0:791a779d6220 246 /* Derive lim_Se from block_size */
shoaib_ahmed 0:791a779d6220 247 cinfo->lim_Se = cinfo->block_size < DCTSIZE ?
shoaib_ahmed 0:791a779d6220 248 cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1;
shoaib_ahmed 0:791a779d6220 249
shoaib_ahmed 0:791a779d6220 250 /* Sanity check on image dimensions */
shoaib_ahmed 0:791a779d6220 251 if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 ||
shoaib_ahmed 0:791a779d6220 252 cinfo->num_components <= 0)
shoaib_ahmed 0:791a779d6220 253 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
shoaib_ahmed 0:791a779d6220 254
shoaib_ahmed 0:791a779d6220 255 /* Make sure image isn't bigger than I can handle */
shoaib_ahmed 0:791a779d6220 256 if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
shoaib_ahmed 0:791a779d6220 257 (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
shoaib_ahmed 0:791a779d6220 258 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
shoaib_ahmed 0:791a779d6220 259
shoaib_ahmed 0:791a779d6220 260 /* Only 8 to 12 bits data precision are supported for DCT based JPEG */
shoaib_ahmed 0:791a779d6220 261 if (cinfo->data_precision < 8 || cinfo->data_precision > 12)
shoaib_ahmed 0:791a779d6220 262 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
shoaib_ahmed 0:791a779d6220 263
shoaib_ahmed 0:791a779d6220 264 /* Check that number of components won't exceed internal array sizes */
shoaib_ahmed 0:791a779d6220 265 if (cinfo->num_components > MAX_COMPONENTS)
shoaib_ahmed 0:791a779d6220 266 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
shoaib_ahmed 0:791a779d6220 267 MAX_COMPONENTS);
shoaib_ahmed 0:791a779d6220 268
shoaib_ahmed 0:791a779d6220 269 /* Compute maximum sampling factors; check factor validity */
shoaib_ahmed 0:791a779d6220 270 cinfo->max_h_samp_factor = 1;
shoaib_ahmed 0:791a779d6220 271 cinfo->max_v_samp_factor = 1;
shoaib_ahmed 0:791a779d6220 272 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
shoaib_ahmed 0:791a779d6220 273 ci++, compptr++) {
shoaib_ahmed 0:791a779d6220 274 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
shoaib_ahmed 0:791a779d6220 275 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
shoaib_ahmed 0:791a779d6220 276 ERREXIT(cinfo, JERR_BAD_SAMPLING);
shoaib_ahmed 0:791a779d6220 277 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
shoaib_ahmed 0:791a779d6220 278 compptr->h_samp_factor);
shoaib_ahmed 0:791a779d6220 279 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
shoaib_ahmed 0:791a779d6220 280 compptr->v_samp_factor);
shoaib_ahmed 0:791a779d6220 281 }
shoaib_ahmed 0:791a779d6220 282
shoaib_ahmed 0:791a779d6220 283 /* Compute dimensions of components */
shoaib_ahmed 0:791a779d6220 284 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
shoaib_ahmed 0:791a779d6220 285 ci++, compptr++) {
shoaib_ahmed 0:791a779d6220 286 /* Fill in the correct component_index value; don't rely on application */
shoaib_ahmed 0:791a779d6220 287 compptr->component_index = ci;
shoaib_ahmed 0:791a779d6220 288 /* In selecting the actual DCT scaling for each component, we try to
shoaib_ahmed 0:791a779d6220 289 * scale down the chroma components via DCT scaling rather than downsampling.
shoaib_ahmed 0:791a779d6220 290 * This saves time if the downsampler gets to use 1:1 scaling.
shoaib_ahmed 0:791a779d6220 291 * Note this code adapts subsampling ratios which are powers of 2.
shoaib_ahmed 0:791a779d6220 292 */
shoaib_ahmed 0:791a779d6220 293 ssize = 1;
shoaib_ahmed 0:791a779d6220 294 #ifdef DCT_SCALING_SUPPORTED
shoaib_ahmed 0:791a779d6220 295 while (cinfo->min_DCT_h_scaled_size * ssize <=
shoaib_ahmed 0:791a779d6220 296 (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
shoaib_ahmed 0:791a779d6220 297 (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
shoaib_ahmed 0:791a779d6220 298 ssize = ssize * 2;
shoaib_ahmed 0:791a779d6220 299 }
shoaib_ahmed 0:791a779d6220 300 #endif
shoaib_ahmed 0:791a779d6220 301 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
shoaib_ahmed 0:791a779d6220 302 ssize = 1;
shoaib_ahmed 0:791a779d6220 303 #ifdef DCT_SCALING_SUPPORTED
shoaib_ahmed 0:791a779d6220 304 while (cinfo->min_DCT_v_scaled_size * ssize <=
shoaib_ahmed 0:791a779d6220 305 (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
shoaib_ahmed 0:791a779d6220 306 (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
shoaib_ahmed 0:791a779d6220 307 ssize = ssize * 2;
shoaib_ahmed 0:791a779d6220 308 }
shoaib_ahmed 0:791a779d6220 309 #endif
shoaib_ahmed 0:791a779d6220 310 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
shoaib_ahmed 0:791a779d6220 311
shoaib_ahmed 0:791a779d6220 312 /* We don't support DCT ratios larger than 2. */
shoaib_ahmed 0:791a779d6220 313 if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
shoaib_ahmed 0:791a779d6220 314 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
shoaib_ahmed 0:791a779d6220 315 else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
shoaib_ahmed 0:791a779d6220 316 compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
shoaib_ahmed 0:791a779d6220 317
shoaib_ahmed 0:791a779d6220 318 /* Size in DCT blocks */
shoaib_ahmed 0:791a779d6220 319 compptr->width_in_blocks = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 320 jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
shoaib_ahmed 0:791a779d6220 321 (long) (cinfo->max_h_samp_factor * cinfo->block_size));
shoaib_ahmed 0:791a779d6220 322 compptr->height_in_blocks = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 323 jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
shoaib_ahmed 0:791a779d6220 324 (long) (cinfo->max_v_samp_factor * cinfo->block_size));
shoaib_ahmed 0:791a779d6220 325 /* Size in samples */
shoaib_ahmed 0:791a779d6220 326 compptr->downsampled_width = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 327 jdiv_round_up((long) cinfo->jpeg_width *
shoaib_ahmed 0:791a779d6220 328 (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
shoaib_ahmed 0:791a779d6220 329 (long) (cinfo->max_h_samp_factor * cinfo->block_size));
shoaib_ahmed 0:791a779d6220 330 compptr->downsampled_height = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 331 jdiv_round_up((long) cinfo->jpeg_height *
shoaib_ahmed 0:791a779d6220 332 (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
shoaib_ahmed 0:791a779d6220 333 (long) (cinfo->max_v_samp_factor * cinfo->block_size));
shoaib_ahmed 0:791a779d6220 334 /* Don't need quantization scale after DCT,
shoaib_ahmed 0:791a779d6220 335 * until color conversion says otherwise.
shoaib_ahmed 0:791a779d6220 336 */
shoaib_ahmed 0:791a779d6220 337 compptr->component_needed = FALSE;
shoaib_ahmed 0:791a779d6220 338 }
shoaib_ahmed 0:791a779d6220 339
shoaib_ahmed 0:791a779d6220 340 /* Compute number of fully interleaved MCU rows (number of times that
shoaib_ahmed 0:791a779d6220 341 * main controller will call coefficient controller).
shoaib_ahmed 0:791a779d6220 342 */
shoaib_ahmed 0:791a779d6220 343 cinfo->total_iMCU_rows = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 344 jdiv_round_up((long) cinfo->jpeg_height,
shoaib_ahmed 0:791a779d6220 345 (long) (cinfo->max_v_samp_factor * cinfo->block_size));
shoaib_ahmed 0:791a779d6220 346 }
shoaib_ahmed 0:791a779d6220 347
shoaib_ahmed 0:791a779d6220 348
shoaib_ahmed 0:791a779d6220 349 #ifdef C_MULTISCAN_FILES_SUPPORTED
shoaib_ahmed 0:791a779d6220 350
shoaib_ahmed 0:791a779d6220 351 LOCAL(void)
shoaib_ahmed 0:791a779d6220 352 validate_script (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 353 /* Verify that the scan script in cinfo->scan_info[] is valid; also
shoaib_ahmed 0:791a779d6220 354 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
shoaib_ahmed 0:791a779d6220 355 */
shoaib_ahmed 0:791a779d6220 356 {
shoaib_ahmed 0:791a779d6220 357 const jpeg_scan_info * scanptr;
shoaib_ahmed 0:791a779d6220 358 int scanno, ncomps, ci, coefi, thisi;
shoaib_ahmed 0:791a779d6220 359 int Ss, Se, Ah, Al;
shoaib_ahmed 0:791a779d6220 360 boolean component_sent[MAX_COMPONENTS];
shoaib_ahmed 0:791a779d6220 361 #ifdef C_PROGRESSIVE_SUPPORTED
shoaib_ahmed 0:791a779d6220 362 int * last_bitpos_ptr;
shoaib_ahmed 0:791a779d6220 363 int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
shoaib_ahmed 0:791a779d6220 364 /* -1 until that coefficient has been seen; then last Al for it */
shoaib_ahmed 0:791a779d6220 365 #endif
shoaib_ahmed 0:791a779d6220 366
shoaib_ahmed 0:791a779d6220 367 if (cinfo->num_scans <= 0)
shoaib_ahmed 0:791a779d6220 368 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
shoaib_ahmed 0:791a779d6220 369
shoaib_ahmed 0:791a779d6220 370 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
shoaib_ahmed 0:791a779d6220 371 * for progressive JPEG, no scan can have this.
shoaib_ahmed 0:791a779d6220 372 */
shoaib_ahmed 0:791a779d6220 373 scanptr = cinfo->scan_info;
shoaib_ahmed 0:791a779d6220 374 if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
shoaib_ahmed 0:791a779d6220 375 #ifdef C_PROGRESSIVE_SUPPORTED
shoaib_ahmed 0:791a779d6220 376 cinfo->progressive_mode = TRUE;
shoaib_ahmed 0:791a779d6220 377 last_bitpos_ptr = & last_bitpos[0][0];
shoaib_ahmed 0:791a779d6220 378 for (ci = 0; ci < cinfo->num_components; ci++)
shoaib_ahmed 0:791a779d6220 379 for (coefi = 0; coefi < DCTSIZE2; coefi++)
shoaib_ahmed 0:791a779d6220 380 *last_bitpos_ptr++ = -1;
shoaib_ahmed 0:791a779d6220 381 #else
shoaib_ahmed 0:791a779d6220 382 ERREXIT(cinfo, JERR_NOT_COMPILED);
shoaib_ahmed 0:791a779d6220 383 #endif
shoaib_ahmed 0:791a779d6220 384 } else {
shoaib_ahmed 0:791a779d6220 385 cinfo->progressive_mode = FALSE;
shoaib_ahmed 0:791a779d6220 386 for (ci = 0; ci < cinfo->num_components; ci++)
shoaib_ahmed 0:791a779d6220 387 component_sent[ci] = FALSE;
shoaib_ahmed 0:791a779d6220 388 }
shoaib_ahmed 0:791a779d6220 389
shoaib_ahmed 0:791a779d6220 390 for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
shoaib_ahmed 0:791a779d6220 391 /* Validate component indexes */
shoaib_ahmed 0:791a779d6220 392 ncomps = scanptr->comps_in_scan;
shoaib_ahmed 0:791a779d6220 393 if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
shoaib_ahmed 0:791a779d6220 394 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
shoaib_ahmed 0:791a779d6220 395 for (ci = 0; ci < ncomps; ci++) {
shoaib_ahmed 0:791a779d6220 396 thisi = scanptr->component_index[ci];
shoaib_ahmed 0:791a779d6220 397 if (thisi < 0 || thisi >= cinfo->num_components)
shoaib_ahmed 0:791a779d6220 398 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 399 /* Components must appear in SOF order within each scan */
shoaib_ahmed 0:791a779d6220 400 if (ci > 0 && thisi <= scanptr->component_index[ci-1])
shoaib_ahmed 0:791a779d6220 401 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 402 }
shoaib_ahmed 0:791a779d6220 403 /* Validate progression parameters */
shoaib_ahmed 0:791a779d6220 404 Ss = scanptr->Ss;
shoaib_ahmed 0:791a779d6220 405 Se = scanptr->Se;
shoaib_ahmed 0:791a779d6220 406 Ah = scanptr->Ah;
shoaib_ahmed 0:791a779d6220 407 Al = scanptr->Al;
shoaib_ahmed 0:791a779d6220 408 if (cinfo->progressive_mode) {
shoaib_ahmed 0:791a779d6220 409 #ifdef C_PROGRESSIVE_SUPPORTED
shoaib_ahmed 0:791a779d6220 410 /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
shoaib_ahmed 0:791a779d6220 411 * seems wrong: the upper bound ought to depend on data precision.
shoaib_ahmed 0:791a779d6220 412 * Perhaps they really meant 0..N+1 for N-bit precision.
shoaib_ahmed 0:791a779d6220 413 * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
shoaib_ahmed 0:791a779d6220 414 * out-of-range reconstructed DC values during the first DC scan,
shoaib_ahmed 0:791a779d6220 415 * which might cause problems for some decoders.
shoaib_ahmed 0:791a779d6220 416 */
shoaib_ahmed 0:791a779d6220 417 #if BITS_IN_JSAMPLE == 8
shoaib_ahmed 0:791a779d6220 418 #define MAX_AH_AL 10
shoaib_ahmed 0:791a779d6220 419 #else
shoaib_ahmed 0:791a779d6220 420 #define MAX_AH_AL 13
shoaib_ahmed 0:791a779d6220 421 #endif
shoaib_ahmed 0:791a779d6220 422 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
shoaib_ahmed 0:791a779d6220 423 Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
shoaib_ahmed 0:791a779d6220 424 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 425 if (Ss == 0) {
shoaib_ahmed 0:791a779d6220 426 if (Se != 0) /* DC and AC together not OK */
shoaib_ahmed 0:791a779d6220 427 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 428 } else {
shoaib_ahmed 0:791a779d6220 429 if (ncomps != 1) /* AC scans must be for only one component */
shoaib_ahmed 0:791a779d6220 430 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 431 }
shoaib_ahmed 0:791a779d6220 432 for (ci = 0; ci < ncomps; ci++) {
shoaib_ahmed 0:791a779d6220 433 last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
shoaib_ahmed 0:791a779d6220 434 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
shoaib_ahmed 0:791a779d6220 435 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 436 for (coefi = Ss; coefi <= Se; coefi++) {
shoaib_ahmed 0:791a779d6220 437 if (last_bitpos_ptr[coefi] < 0) {
shoaib_ahmed 0:791a779d6220 438 /* first scan of this coefficient */
shoaib_ahmed 0:791a779d6220 439 if (Ah != 0)
shoaib_ahmed 0:791a779d6220 440 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 441 } else {
shoaib_ahmed 0:791a779d6220 442 /* not first scan */
shoaib_ahmed 0:791a779d6220 443 if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
shoaib_ahmed 0:791a779d6220 444 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 445 }
shoaib_ahmed 0:791a779d6220 446 last_bitpos_ptr[coefi] = Al;
shoaib_ahmed 0:791a779d6220 447 }
shoaib_ahmed 0:791a779d6220 448 }
shoaib_ahmed 0:791a779d6220 449 #endif
shoaib_ahmed 0:791a779d6220 450 } else {
shoaib_ahmed 0:791a779d6220 451 /* For sequential JPEG, all progression parameters must be these: */
shoaib_ahmed 0:791a779d6220 452 if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
shoaib_ahmed 0:791a779d6220 453 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 454 /* Make sure components are not sent twice */
shoaib_ahmed 0:791a779d6220 455 for (ci = 0; ci < ncomps; ci++) {
shoaib_ahmed 0:791a779d6220 456 thisi = scanptr->component_index[ci];
shoaib_ahmed 0:791a779d6220 457 if (component_sent[thisi])
shoaib_ahmed 0:791a779d6220 458 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
shoaib_ahmed 0:791a779d6220 459 component_sent[thisi] = TRUE;
shoaib_ahmed 0:791a779d6220 460 }
shoaib_ahmed 0:791a779d6220 461 }
shoaib_ahmed 0:791a779d6220 462 }
shoaib_ahmed 0:791a779d6220 463
shoaib_ahmed 0:791a779d6220 464 /* Now verify that everything got sent. */
shoaib_ahmed 0:791a779d6220 465 if (cinfo->progressive_mode) {
shoaib_ahmed 0:791a779d6220 466 #ifdef C_PROGRESSIVE_SUPPORTED
shoaib_ahmed 0:791a779d6220 467 /* For progressive mode, we only check that at least some DC data
shoaib_ahmed 0:791a779d6220 468 * got sent for each component; the spec does not require that all bits
shoaib_ahmed 0:791a779d6220 469 * of all coefficients be transmitted. Would it be wiser to enforce
shoaib_ahmed 0:791a779d6220 470 * transmission of all coefficient bits??
shoaib_ahmed 0:791a779d6220 471 */
shoaib_ahmed 0:791a779d6220 472 for (ci = 0; ci < cinfo->num_components; ci++) {
shoaib_ahmed 0:791a779d6220 473 if (last_bitpos[ci][0] < 0)
shoaib_ahmed 0:791a779d6220 474 ERREXIT(cinfo, JERR_MISSING_DATA);
shoaib_ahmed 0:791a779d6220 475 }
shoaib_ahmed 0:791a779d6220 476 #endif
shoaib_ahmed 0:791a779d6220 477 } else {
shoaib_ahmed 0:791a779d6220 478 for (ci = 0; ci < cinfo->num_components; ci++) {
shoaib_ahmed 0:791a779d6220 479 if (! component_sent[ci])
shoaib_ahmed 0:791a779d6220 480 ERREXIT(cinfo, JERR_MISSING_DATA);
shoaib_ahmed 0:791a779d6220 481 }
shoaib_ahmed 0:791a779d6220 482 }
shoaib_ahmed 0:791a779d6220 483 }
shoaib_ahmed 0:791a779d6220 484
shoaib_ahmed 0:791a779d6220 485
shoaib_ahmed 0:791a779d6220 486 LOCAL(void)
shoaib_ahmed 0:791a779d6220 487 reduce_script (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 488 /* Adapt scan script for use with reduced block size;
shoaib_ahmed 0:791a779d6220 489 * assume that script has been validated before.
shoaib_ahmed 0:791a779d6220 490 */
shoaib_ahmed 0:791a779d6220 491 {
shoaib_ahmed 0:791a779d6220 492 jpeg_scan_info * scanptr;
shoaib_ahmed 0:791a779d6220 493 int idxout, idxin;
shoaib_ahmed 0:791a779d6220 494
shoaib_ahmed 0:791a779d6220 495 /* Circumvent const declaration for this function */
shoaib_ahmed 0:791a779d6220 496 scanptr = (jpeg_scan_info *) cinfo->scan_info;
shoaib_ahmed 0:791a779d6220 497 idxout = 0;
shoaib_ahmed 0:791a779d6220 498
shoaib_ahmed 0:791a779d6220 499 for (idxin = 0; idxin < cinfo->num_scans; idxin++) {
shoaib_ahmed 0:791a779d6220 500 /* After skipping, idxout becomes smaller than idxin */
shoaib_ahmed 0:791a779d6220 501 if (idxin != idxout)
shoaib_ahmed 0:791a779d6220 502 /* Copy rest of data;
shoaib_ahmed 0:791a779d6220 503 * note we stay in given chunk of allocated memory.
shoaib_ahmed 0:791a779d6220 504 */
shoaib_ahmed 0:791a779d6220 505 scanptr[idxout] = scanptr[idxin];
shoaib_ahmed 0:791a779d6220 506 if (scanptr[idxout].Ss > cinfo->lim_Se)
shoaib_ahmed 0:791a779d6220 507 /* Entire scan out of range - skip this entry */
shoaib_ahmed 0:791a779d6220 508 continue;
shoaib_ahmed 0:791a779d6220 509 if (scanptr[idxout].Se > cinfo->lim_Se)
shoaib_ahmed 0:791a779d6220 510 /* Limit scan to end of block */
shoaib_ahmed 0:791a779d6220 511 scanptr[idxout].Se = cinfo->lim_Se;
shoaib_ahmed 0:791a779d6220 512 idxout++;
shoaib_ahmed 0:791a779d6220 513 }
shoaib_ahmed 0:791a779d6220 514
shoaib_ahmed 0:791a779d6220 515 cinfo->num_scans = idxout;
shoaib_ahmed 0:791a779d6220 516 }
shoaib_ahmed 0:791a779d6220 517
shoaib_ahmed 0:791a779d6220 518 #endif /* C_MULTISCAN_FILES_SUPPORTED */
shoaib_ahmed 0:791a779d6220 519
shoaib_ahmed 0:791a779d6220 520
shoaib_ahmed 0:791a779d6220 521 LOCAL(void)
shoaib_ahmed 0:791a779d6220 522 select_scan_parameters (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 523 /* Set up the scan parameters for the current scan */
shoaib_ahmed 0:791a779d6220 524 {
shoaib_ahmed 0:791a779d6220 525 int ci;
shoaib_ahmed 0:791a779d6220 526
shoaib_ahmed 0:791a779d6220 527 #ifdef C_MULTISCAN_FILES_SUPPORTED
shoaib_ahmed 0:791a779d6220 528 if (cinfo->scan_info != NULL) {
shoaib_ahmed 0:791a779d6220 529 /* Prepare for current scan --- the script is already validated */
shoaib_ahmed 0:791a779d6220 530 my_master_ptr master = (my_master_ptr) cinfo->master;
shoaib_ahmed 0:791a779d6220 531 const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
shoaib_ahmed 0:791a779d6220 532
shoaib_ahmed 0:791a779d6220 533 cinfo->comps_in_scan = scanptr->comps_in_scan;
shoaib_ahmed 0:791a779d6220 534 for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
shoaib_ahmed 0:791a779d6220 535 cinfo->cur_comp_info[ci] =
shoaib_ahmed 0:791a779d6220 536 &cinfo->comp_info[scanptr->component_index[ci]];
shoaib_ahmed 0:791a779d6220 537 }
shoaib_ahmed 0:791a779d6220 538 if (cinfo->progressive_mode) {
shoaib_ahmed 0:791a779d6220 539 cinfo->Ss = scanptr->Ss;
shoaib_ahmed 0:791a779d6220 540 cinfo->Se = scanptr->Se;
shoaib_ahmed 0:791a779d6220 541 cinfo->Ah = scanptr->Ah;
shoaib_ahmed 0:791a779d6220 542 cinfo->Al = scanptr->Al;
shoaib_ahmed 0:791a779d6220 543 return;
shoaib_ahmed 0:791a779d6220 544 }
shoaib_ahmed 0:791a779d6220 545 }
shoaib_ahmed 0:791a779d6220 546 else
shoaib_ahmed 0:791a779d6220 547 #endif
shoaib_ahmed 0:791a779d6220 548 {
shoaib_ahmed 0:791a779d6220 549 /* Prepare for single sequential-JPEG scan containing all components */
shoaib_ahmed 0:791a779d6220 550 if (cinfo->num_components > MAX_COMPS_IN_SCAN)
shoaib_ahmed 0:791a779d6220 551 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
shoaib_ahmed 0:791a779d6220 552 MAX_COMPS_IN_SCAN);
shoaib_ahmed 0:791a779d6220 553 cinfo->comps_in_scan = cinfo->num_components;
shoaib_ahmed 0:791a779d6220 554 for (ci = 0; ci < cinfo->num_components; ci++) {
shoaib_ahmed 0:791a779d6220 555 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
shoaib_ahmed 0:791a779d6220 556 }
shoaib_ahmed 0:791a779d6220 557 }
shoaib_ahmed 0:791a779d6220 558 cinfo->Ss = 0;
shoaib_ahmed 0:791a779d6220 559 cinfo->Se = cinfo->block_size * cinfo->block_size - 1;
shoaib_ahmed 0:791a779d6220 560 cinfo->Ah = 0;
shoaib_ahmed 0:791a779d6220 561 cinfo->Al = 0;
shoaib_ahmed 0:791a779d6220 562 }
shoaib_ahmed 0:791a779d6220 563
shoaib_ahmed 0:791a779d6220 564
shoaib_ahmed 0:791a779d6220 565 LOCAL(void)
shoaib_ahmed 0:791a779d6220 566 per_scan_setup (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 567 /* Do computations that are needed before processing a JPEG scan */
shoaib_ahmed 0:791a779d6220 568 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
shoaib_ahmed 0:791a779d6220 569 {
shoaib_ahmed 0:791a779d6220 570 int ci, mcublks, tmp;
shoaib_ahmed 0:791a779d6220 571 jpeg_component_info *compptr;
shoaib_ahmed 0:791a779d6220 572
shoaib_ahmed 0:791a779d6220 573 if (cinfo->comps_in_scan == 1) {
shoaib_ahmed 0:791a779d6220 574
shoaib_ahmed 0:791a779d6220 575 /* Noninterleaved (single-component) scan */
shoaib_ahmed 0:791a779d6220 576 compptr = cinfo->cur_comp_info[0];
shoaib_ahmed 0:791a779d6220 577
shoaib_ahmed 0:791a779d6220 578 /* Overall image size in MCUs */
shoaib_ahmed 0:791a779d6220 579 cinfo->MCUs_per_row = compptr->width_in_blocks;
shoaib_ahmed 0:791a779d6220 580 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
shoaib_ahmed 0:791a779d6220 581
shoaib_ahmed 0:791a779d6220 582 /* For noninterleaved scan, always one block per MCU */
shoaib_ahmed 0:791a779d6220 583 compptr->MCU_width = 1;
shoaib_ahmed 0:791a779d6220 584 compptr->MCU_height = 1;
shoaib_ahmed 0:791a779d6220 585 compptr->MCU_blocks = 1;
shoaib_ahmed 0:791a779d6220 586 compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
shoaib_ahmed 0:791a779d6220 587 compptr->last_col_width = 1;
shoaib_ahmed 0:791a779d6220 588 /* For noninterleaved scans, it is convenient to define last_row_height
shoaib_ahmed 0:791a779d6220 589 * as the number of block rows present in the last iMCU row.
shoaib_ahmed 0:791a779d6220 590 */
shoaib_ahmed 0:791a779d6220 591 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
shoaib_ahmed 0:791a779d6220 592 if (tmp == 0) tmp = compptr->v_samp_factor;
shoaib_ahmed 0:791a779d6220 593 compptr->last_row_height = tmp;
shoaib_ahmed 0:791a779d6220 594
shoaib_ahmed 0:791a779d6220 595 /* Prepare array describing MCU composition */
shoaib_ahmed 0:791a779d6220 596 cinfo->blocks_in_MCU = 1;
shoaib_ahmed 0:791a779d6220 597 cinfo->MCU_membership[0] = 0;
shoaib_ahmed 0:791a779d6220 598
shoaib_ahmed 0:791a779d6220 599 } else {
shoaib_ahmed 0:791a779d6220 600
shoaib_ahmed 0:791a779d6220 601 /* Interleaved (multi-component) scan */
shoaib_ahmed 0:791a779d6220 602 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
shoaib_ahmed 0:791a779d6220 603 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
shoaib_ahmed 0:791a779d6220 604 MAX_COMPS_IN_SCAN);
shoaib_ahmed 0:791a779d6220 605
shoaib_ahmed 0:791a779d6220 606 /* Overall image size in MCUs */
shoaib_ahmed 0:791a779d6220 607 cinfo->MCUs_per_row = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 608 jdiv_round_up((long) cinfo->jpeg_width,
shoaib_ahmed 0:791a779d6220 609 (long) (cinfo->max_h_samp_factor * cinfo->block_size));
shoaib_ahmed 0:791a779d6220 610 cinfo->MCU_rows_in_scan = (JDIMENSION)
shoaib_ahmed 0:791a779d6220 611 jdiv_round_up((long) cinfo->jpeg_height,
shoaib_ahmed 0:791a779d6220 612 (long) (cinfo->max_v_samp_factor * cinfo->block_size));
shoaib_ahmed 0:791a779d6220 613
shoaib_ahmed 0:791a779d6220 614 cinfo->blocks_in_MCU = 0;
shoaib_ahmed 0:791a779d6220 615
shoaib_ahmed 0:791a779d6220 616 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shoaib_ahmed 0:791a779d6220 617 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 618 /* Sampling factors give # of blocks of component in each MCU */
shoaib_ahmed 0:791a779d6220 619 compptr->MCU_width = compptr->h_samp_factor;
shoaib_ahmed 0:791a779d6220 620 compptr->MCU_height = compptr->v_samp_factor;
shoaib_ahmed 0:791a779d6220 621 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
shoaib_ahmed 0:791a779d6220 622 compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
shoaib_ahmed 0:791a779d6220 623 /* Figure number of non-dummy blocks in last MCU column & row */
shoaib_ahmed 0:791a779d6220 624 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
shoaib_ahmed 0:791a779d6220 625 if (tmp == 0) tmp = compptr->MCU_width;
shoaib_ahmed 0:791a779d6220 626 compptr->last_col_width = tmp;
shoaib_ahmed 0:791a779d6220 627 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
shoaib_ahmed 0:791a779d6220 628 if (tmp == 0) tmp = compptr->MCU_height;
shoaib_ahmed 0:791a779d6220 629 compptr->last_row_height = tmp;
shoaib_ahmed 0:791a779d6220 630 /* Prepare array describing MCU composition */
shoaib_ahmed 0:791a779d6220 631 mcublks = compptr->MCU_blocks;
shoaib_ahmed 0:791a779d6220 632 if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
shoaib_ahmed 0:791a779d6220 633 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
shoaib_ahmed 0:791a779d6220 634 while (mcublks-- > 0) {
shoaib_ahmed 0:791a779d6220 635 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
shoaib_ahmed 0:791a779d6220 636 }
shoaib_ahmed 0:791a779d6220 637 }
shoaib_ahmed 0:791a779d6220 638
shoaib_ahmed 0:791a779d6220 639 }
shoaib_ahmed 0:791a779d6220 640
shoaib_ahmed 0:791a779d6220 641 /* Convert restart specified in rows to actual MCU count. */
shoaib_ahmed 0:791a779d6220 642 /* Note that count must fit in 16 bits, so we provide limiting. */
shoaib_ahmed 0:791a779d6220 643 if (cinfo->restart_in_rows > 0) {
shoaib_ahmed 0:791a779d6220 644 long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
shoaib_ahmed 0:791a779d6220 645 cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
shoaib_ahmed 0:791a779d6220 646 }
shoaib_ahmed 0:791a779d6220 647 }
shoaib_ahmed 0:791a779d6220 648
shoaib_ahmed 0:791a779d6220 649
shoaib_ahmed 0:791a779d6220 650 /*
shoaib_ahmed 0:791a779d6220 651 * Per-pass setup.
shoaib_ahmed 0:791a779d6220 652 * This is called at the beginning of each pass. We determine which modules
shoaib_ahmed 0:791a779d6220 653 * will be active during this pass and give them appropriate start_pass calls.
shoaib_ahmed 0:791a779d6220 654 * We also set is_last_pass to indicate whether any more passes will be
shoaib_ahmed 0:791a779d6220 655 * required.
shoaib_ahmed 0:791a779d6220 656 */
shoaib_ahmed 0:791a779d6220 657
shoaib_ahmed 0:791a779d6220 658 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 659 prepare_for_pass (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 660 {
shoaib_ahmed 0:791a779d6220 661 my_master_ptr master = (my_master_ptr) cinfo->master;
shoaib_ahmed 0:791a779d6220 662
shoaib_ahmed 0:791a779d6220 663 switch (master->pass_type) {
shoaib_ahmed 0:791a779d6220 664 case main_pass:
shoaib_ahmed 0:791a779d6220 665 /* Initial pass: will collect input data, and do either Huffman
shoaib_ahmed 0:791a779d6220 666 * optimization or data output for the first scan.
shoaib_ahmed 0:791a779d6220 667 */
shoaib_ahmed 0:791a779d6220 668 select_scan_parameters(cinfo);
shoaib_ahmed 0:791a779d6220 669 per_scan_setup(cinfo);
shoaib_ahmed 0:791a779d6220 670 if (! cinfo->raw_data_in) {
shoaib_ahmed 0:791a779d6220 671 (*cinfo->cconvert->start_pass) (cinfo);
shoaib_ahmed 0:791a779d6220 672 (*cinfo->downsample->start_pass) (cinfo);
shoaib_ahmed 0:791a779d6220 673 (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
shoaib_ahmed 0:791a779d6220 674 }
shoaib_ahmed 0:791a779d6220 675 (*cinfo->fdct->start_pass) (cinfo);
shoaib_ahmed 0:791a779d6220 676 (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
shoaib_ahmed 0:791a779d6220 677 (*cinfo->coef->start_pass) (cinfo,
shoaib_ahmed 0:791a779d6220 678 (master->total_passes > 1 ?
shoaib_ahmed 0:791a779d6220 679 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
shoaib_ahmed 0:791a779d6220 680 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
shoaib_ahmed 0:791a779d6220 681 if (cinfo->optimize_coding) {
shoaib_ahmed 0:791a779d6220 682 /* No immediate data output; postpone writing frame/scan headers */
shoaib_ahmed 0:791a779d6220 683 master->pub.call_pass_startup = FALSE;
shoaib_ahmed 0:791a779d6220 684 } else {
shoaib_ahmed 0:791a779d6220 685 /* Will write frame/scan headers at first jpeg_write_scanlines call */
shoaib_ahmed 0:791a779d6220 686 master->pub.call_pass_startup = TRUE;
shoaib_ahmed 0:791a779d6220 687 }
shoaib_ahmed 0:791a779d6220 688 break;
shoaib_ahmed 0:791a779d6220 689 #ifdef ENTROPY_OPT_SUPPORTED
shoaib_ahmed 0:791a779d6220 690 case huff_opt_pass:
shoaib_ahmed 0:791a779d6220 691 /* Do Huffman optimization for a scan after the first one. */
shoaib_ahmed 0:791a779d6220 692 select_scan_parameters(cinfo);
shoaib_ahmed 0:791a779d6220 693 per_scan_setup(cinfo);
shoaib_ahmed 0:791a779d6220 694 if (cinfo->Ss != 0 || cinfo->Ah == 0) {
shoaib_ahmed 0:791a779d6220 695 (*cinfo->entropy->start_pass) (cinfo, TRUE);
shoaib_ahmed 0:791a779d6220 696 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
shoaib_ahmed 0:791a779d6220 697 master->pub.call_pass_startup = FALSE;
shoaib_ahmed 0:791a779d6220 698 break;
shoaib_ahmed 0:791a779d6220 699 }
shoaib_ahmed 0:791a779d6220 700 /* Special case: Huffman DC refinement scans need no Huffman table
shoaib_ahmed 0:791a779d6220 701 * and therefore we can skip the optimization pass for them.
shoaib_ahmed 0:791a779d6220 702 */
shoaib_ahmed 0:791a779d6220 703 master->pass_type = output_pass;
shoaib_ahmed 0:791a779d6220 704 master->pass_number++;
shoaib_ahmed 0:791a779d6220 705 /*FALLTHROUGH*/
shoaib_ahmed 0:791a779d6220 706 #endif
shoaib_ahmed 0:791a779d6220 707 case output_pass:
shoaib_ahmed 0:791a779d6220 708 /* Do a data-output pass. */
shoaib_ahmed 0:791a779d6220 709 /* We need not repeat per-scan setup if prior optimization pass did it. */
shoaib_ahmed 0:791a779d6220 710 if (! cinfo->optimize_coding) {
shoaib_ahmed 0:791a779d6220 711 select_scan_parameters(cinfo);
shoaib_ahmed 0:791a779d6220 712 per_scan_setup(cinfo);
shoaib_ahmed 0:791a779d6220 713 }
shoaib_ahmed 0:791a779d6220 714 (*cinfo->entropy->start_pass) (cinfo, FALSE);
shoaib_ahmed 0:791a779d6220 715 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
shoaib_ahmed 0:791a779d6220 716 /* We emit frame/scan headers now */
shoaib_ahmed 0:791a779d6220 717 if (master->scan_number == 0)
shoaib_ahmed 0:791a779d6220 718 (*cinfo->marker->write_frame_header) (cinfo);
shoaib_ahmed 0:791a779d6220 719 (*cinfo->marker->write_scan_header) (cinfo);
shoaib_ahmed 0:791a779d6220 720 master->pub.call_pass_startup = FALSE;
shoaib_ahmed 0:791a779d6220 721 break;
shoaib_ahmed 0:791a779d6220 722 default:
shoaib_ahmed 0:791a779d6220 723 ERREXIT(cinfo, JERR_NOT_COMPILED);
shoaib_ahmed 0:791a779d6220 724 }
shoaib_ahmed 0:791a779d6220 725
shoaib_ahmed 0:791a779d6220 726 master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
shoaib_ahmed 0:791a779d6220 727
shoaib_ahmed 0:791a779d6220 728 /* Set up progress monitor's pass info if present */
shoaib_ahmed 0:791a779d6220 729 if (cinfo->progress != NULL) {
shoaib_ahmed 0:791a779d6220 730 cinfo->progress->completed_passes = master->pass_number;
shoaib_ahmed 0:791a779d6220 731 cinfo->progress->total_passes = master->total_passes;
shoaib_ahmed 0:791a779d6220 732 }
shoaib_ahmed 0:791a779d6220 733 }
shoaib_ahmed 0:791a779d6220 734
shoaib_ahmed 0:791a779d6220 735
shoaib_ahmed 0:791a779d6220 736 /*
shoaib_ahmed 0:791a779d6220 737 * Special start-of-pass hook.
shoaib_ahmed 0:791a779d6220 738 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
shoaib_ahmed 0:791a779d6220 739 * In single-pass processing, we need this hook because we don't want to
shoaib_ahmed 0:791a779d6220 740 * write frame/scan headers during jpeg_start_compress; we want to let the
shoaib_ahmed 0:791a779d6220 741 * application write COM markers etc. between jpeg_start_compress and the
shoaib_ahmed 0:791a779d6220 742 * jpeg_write_scanlines loop.
shoaib_ahmed 0:791a779d6220 743 * In multi-pass processing, this routine is not used.
shoaib_ahmed 0:791a779d6220 744 */
shoaib_ahmed 0:791a779d6220 745
shoaib_ahmed 0:791a779d6220 746 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 747 pass_startup (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 748 {
shoaib_ahmed 0:791a779d6220 749 cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
shoaib_ahmed 0:791a779d6220 750
shoaib_ahmed 0:791a779d6220 751 (*cinfo->marker->write_frame_header) (cinfo);
shoaib_ahmed 0:791a779d6220 752 (*cinfo->marker->write_scan_header) (cinfo);
shoaib_ahmed 0:791a779d6220 753 }
shoaib_ahmed 0:791a779d6220 754
shoaib_ahmed 0:791a779d6220 755
shoaib_ahmed 0:791a779d6220 756 /*
shoaib_ahmed 0:791a779d6220 757 * Finish up at end of pass.
shoaib_ahmed 0:791a779d6220 758 */
shoaib_ahmed 0:791a779d6220 759
shoaib_ahmed 0:791a779d6220 760 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 761 finish_pass_master (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 762 {
shoaib_ahmed 0:791a779d6220 763 my_master_ptr master = (my_master_ptr) cinfo->master;
shoaib_ahmed 0:791a779d6220 764
shoaib_ahmed 0:791a779d6220 765 /* The entropy coder always needs an end-of-pass call,
shoaib_ahmed 0:791a779d6220 766 * either to analyze statistics or to flush its output buffer.
shoaib_ahmed 0:791a779d6220 767 */
shoaib_ahmed 0:791a779d6220 768 (*cinfo->entropy->finish_pass) (cinfo);
shoaib_ahmed 0:791a779d6220 769
shoaib_ahmed 0:791a779d6220 770 /* Update state for next pass */
shoaib_ahmed 0:791a779d6220 771 switch (master->pass_type) {
shoaib_ahmed 0:791a779d6220 772 case main_pass:
shoaib_ahmed 0:791a779d6220 773 /* next pass is either output of scan 0 (after optimization)
shoaib_ahmed 0:791a779d6220 774 * or output of scan 1 (if no optimization).
shoaib_ahmed 0:791a779d6220 775 */
shoaib_ahmed 0:791a779d6220 776 master->pass_type = output_pass;
shoaib_ahmed 0:791a779d6220 777 if (! cinfo->optimize_coding)
shoaib_ahmed 0:791a779d6220 778 master->scan_number++;
shoaib_ahmed 0:791a779d6220 779 break;
shoaib_ahmed 0:791a779d6220 780 case huff_opt_pass:
shoaib_ahmed 0:791a779d6220 781 /* next pass is always output of current scan */
shoaib_ahmed 0:791a779d6220 782 master->pass_type = output_pass;
shoaib_ahmed 0:791a779d6220 783 break;
shoaib_ahmed 0:791a779d6220 784 case output_pass:
shoaib_ahmed 0:791a779d6220 785 /* next pass is either optimization or output of next scan */
shoaib_ahmed 0:791a779d6220 786 if (cinfo->optimize_coding)
shoaib_ahmed 0:791a779d6220 787 master->pass_type = huff_opt_pass;
shoaib_ahmed 0:791a779d6220 788 master->scan_number++;
shoaib_ahmed 0:791a779d6220 789 break;
shoaib_ahmed 0:791a779d6220 790 }
shoaib_ahmed 0:791a779d6220 791
shoaib_ahmed 0:791a779d6220 792 master->pass_number++;
shoaib_ahmed 0:791a779d6220 793 }
shoaib_ahmed 0:791a779d6220 794
shoaib_ahmed 0:791a779d6220 795
shoaib_ahmed 0:791a779d6220 796 /*
shoaib_ahmed 0:791a779d6220 797 * Initialize master compression control.
shoaib_ahmed 0:791a779d6220 798 */
shoaib_ahmed 0:791a779d6220 799
shoaib_ahmed 0:791a779d6220 800 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 801 jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
shoaib_ahmed 0:791a779d6220 802 {
shoaib_ahmed 0:791a779d6220 803 my_master_ptr master;
shoaib_ahmed 0:791a779d6220 804
shoaib_ahmed 0:791a779d6220 805 master = (my_master_ptr)
shoaib_ahmed 0:791a779d6220 806 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 807 SIZEOF(my_comp_master));
shoaib_ahmed 0:791a779d6220 808 cinfo->master = &master->pub;
shoaib_ahmed 0:791a779d6220 809 master->pub.prepare_for_pass = prepare_for_pass;
shoaib_ahmed 0:791a779d6220 810 master->pub.pass_startup = pass_startup;
shoaib_ahmed 0:791a779d6220 811 master->pub.finish_pass = finish_pass_master;
shoaib_ahmed 0:791a779d6220 812 master->pub.is_last_pass = FALSE;
shoaib_ahmed 0:791a779d6220 813
shoaib_ahmed 0:791a779d6220 814 /* Validate parameters, determine derived values */
shoaib_ahmed 0:791a779d6220 815 initial_setup(cinfo, transcode_only);
shoaib_ahmed 0:791a779d6220 816
shoaib_ahmed 0:791a779d6220 817 if (cinfo->scan_info != NULL) {
shoaib_ahmed 0:791a779d6220 818 #ifdef C_MULTISCAN_FILES_SUPPORTED
shoaib_ahmed 0:791a779d6220 819 validate_script(cinfo);
shoaib_ahmed 0:791a779d6220 820 if (cinfo->block_size < DCTSIZE)
shoaib_ahmed 0:791a779d6220 821 reduce_script(cinfo);
shoaib_ahmed 0:791a779d6220 822 #else
shoaib_ahmed 0:791a779d6220 823 ERREXIT(cinfo, JERR_NOT_COMPILED);
shoaib_ahmed 0:791a779d6220 824 #endif
shoaib_ahmed 0:791a779d6220 825 } else {
shoaib_ahmed 0:791a779d6220 826 cinfo->progressive_mode = FALSE;
shoaib_ahmed 0:791a779d6220 827 cinfo->num_scans = 1;
shoaib_ahmed 0:791a779d6220 828 }
shoaib_ahmed 0:791a779d6220 829
shoaib_ahmed 0:791a779d6220 830 if (cinfo->optimize_coding)
shoaib_ahmed 0:791a779d6220 831 cinfo->arith_code = FALSE; /* disable arithmetic coding */
shoaib_ahmed 0:791a779d6220 832 else if (! cinfo->arith_code &&
shoaib_ahmed 0:791a779d6220 833 (cinfo->progressive_mode ||
shoaib_ahmed 0:791a779d6220 834 (cinfo->block_size > 1 && cinfo->block_size < DCTSIZE)))
shoaib_ahmed 0:791a779d6220 835 /* TEMPORARY HACK ??? */
shoaib_ahmed 0:791a779d6220 836 /* assume default tables no good for progressive or reduced AC mode */
shoaib_ahmed 0:791a779d6220 837 cinfo->optimize_coding = TRUE; /* force Huffman optimization */
shoaib_ahmed 0:791a779d6220 838
shoaib_ahmed 0:791a779d6220 839 /* Initialize my private state */
shoaib_ahmed 0:791a779d6220 840 if (transcode_only) {
shoaib_ahmed 0:791a779d6220 841 /* no main pass in transcoding */
shoaib_ahmed 0:791a779d6220 842 if (cinfo->optimize_coding)
shoaib_ahmed 0:791a779d6220 843 master->pass_type = huff_opt_pass;
shoaib_ahmed 0:791a779d6220 844 else
shoaib_ahmed 0:791a779d6220 845 master->pass_type = output_pass;
shoaib_ahmed 0:791a779d6220 846 } else {
shoaib_ahmed 0:791a779d6220 847 /* for normal compression, first pass is always this type: */
shoaib_ahmed 0:791a779d6220 848 master->pass_type = main_pass;
shoaib_ahmed 0:791a779d6220 849 }
shoaib_ahmed 0:791a779d6220 850 master->scan_number = 0;
shoaib_ahmed 0:791a779d6220 851 master->pass_number = 0;
shoaib_ahmed 0:791a779d6220 852 if (cinfo->optimize_coding)
shoaib_ahmed 0:791a779d6220 853 master->total_passes = cinfo->num_scans * 2;
shoaib_ahmed 0:791a779d6220 854 else
shoaib_ahmed 0:791a779d6220 855 master->total_passes = cinfo->num_scans;
shoaib_ahmed 0:791a779d6220 856 }