0001: /*******************************************************************************
0002: * Copyright (c) 2000, 2006 IBM Corporation and others.
0003: * All rights reserved. This program and the accompanying materials
0004: * are made available under the terms of the Eclipse Public License v1.0
0005: * which accompanies this distribution, and is available at
0006: * http://www.eclipse.org/legal/epl-v10.html
0007: *
0008: * Contributors:
0009: * IBM Corporation - initial API and implementation
0010: *******************************************************************************/package org.eclipse.swt.internal.image;
0011:
0012: import java.io.*;
0013:
0014: import org.eclipse.swt.*;
0015: import org.eclipse.swt.graphics.*;
0016:
0017: public class JPEGDecoder {
0018:
0019: static final int DCTSIZE = 8;
0020: static final int DCTSIZE2 = 64;
0021: static final int NUM_QUANT_TBLS = 4;
0022: static final int NUM_HUFF_TBLS = 4;
0023: static final int NUM_ARITH_TBLS = 16;
0024: static final int MAX_COMPS_IN_SCAN = 4;
0025: static final int MAX_COMPONENTS = 10;
0026: static final int MAX_SAMP_FACTOR = 4;
0027: static final int D_MAX_BLOCKS_IN_MCU = 10;
0028: static final int HUFF_LOOKAHEAD = 8;
0029: static final int MAX_Q_COMPS = 4;
0030: static final int IFAST_SCALE_BITS = 2;
0031: static final int MAXJSAMPLE = 255;
0032: static final int CENTERJSAMPLE = 128;
0033: static final int MIN_GET_BITS = 32 - 7;
0034: static final int INPUT_BUFFER_SIZE = 4096;
0035:
0036: static final int SCALEBITS = 16; /* speediest right-shift on some machines */
0037: static final int ONE_HALF = 1 << (SCALEBITS - 1);
0038:
0039: static final int RGB_RED = 2; /* Offset of Red in an RGB scanline element */
0040: static final int RGB_GREEN = 1; /* Offset of Green */
0041: static final int RGB_BLUE = 0; /* Offset of Blue */
0042: static final int RGB_PIXELSIZE = 3;
0043:
0044: static final int JBUF_PASS_THRU = 0;
0045: static final int JBUF_SAVE_SOURCE = 1; /* Run source subobject only, save output */
0046: static final int JBUF_CRANK_DEST = 2; /* Run dest subobject only, using saved data */
0047: static final int JBUF_SAVE_AND_PASS = 3;
0048:
0049: static final int JPEG_MAX_DIMENSION = 65500;
0050: static final int BITS_IN_JSAMPLE = 8;
0051:
0052: static final int JDITHER_NONE = 0; /* no dithering */
0053: static final int JDITHER_ORDERED = 1; /* simple ordered dither */
0054: static final int JDITHER_FS = 2;
0055:
0056: static final int JDCT_ISLOW = 0; /* slow but accurate integer algorithm */
0057: static final int JDCT_IFAST = 1; /* faster, less accurate integer method */
0058: static final int JDCT_FLOAT = 2; /* floating-point: accurate, fast on fast HW */
0059: static final int JDCT_DEFAULT = JDCT_ISLOW;
0060:
0061: static final int JCS_UNKNOWN = 0; /* error/unspecified */
0062: static final int JCS_GRAYSCALE = 1; /* monochrome */
0063: static final int JCS_RGB = 2; /* red/green/blue */
0064: static final int JCS_YCbCr = 3; /* Y/Cb/Cr (also known as YUV) */
0065: static final int JCS_CMYK = 4; /* C/M/Y/K */
0066: static final int JCS_YCCK = 5; /* Y/Cb/Cr/K */
0067:
0068: static final int SAVED_COEFS = 6;
0069: static final int Q01_POS = 1;
0070: static final int Q10_POS = 8;
0071: static final int Q20_POS = 16;
0072: static final int Q11_POS = 9;
0073: static final int Q02_POS = 2;
0074:
0075: static final int CTX_PREPARE_FOR_IMCU = 0; /* need to prepare for MCU row */
0076: static final int CTX_PROCESS_IMCU = 1; /* feeding iMCU to postprocessor */
0077: static final int CTX_POSTPONED_ROW = 2; /* feeding postponed row group */
0078:
0079: static final int APP0_DATA_LEN = 14; /* Length of interesting data in APP0 */
0080: static final int APP14_DATA_LEN = 12; /* Length of interesting data in APP14 */
0081: static final int APPN_DATA_LEN = 14; /* Must be the largest of the above!! */
0082:
0083: /* markers */
0084: static final int M_SOF0 = 0xc0;
0085: static final int M_SOF1 = 0xc1;
0086: static final int M_SOF2 = 0xc2;
0087: static final int M_SOF3 = 0xc3;
0088: static final int M_SOF5 = 0xc5;
0089: static final int M_SOF6 = 0xc6;
0090: static final int M_SOF7 = 0xc7;
0091: static final int M_JPG = 0xc8;
0092: static final int M_SOF9 = 0xc9;
0093: static final int M_SOF10 = 0xca;
0094: static final int M_SOF11 = 0xcb;
0095: static final int M_SOF13 = 0xcd;
0096: static final int M_SOF14 = 0xce;
0097: static final int M_SOF15 = 0xcf;
0098: static final int M_DHT = 0xc4;
0099: static final int M_DAC = 0xcc;
0100: static final int M_RST0 = 0xd0;
0101: static final int M_RST1 = 0xd1;
0102: static final int M_RST2 = 0xd2;
0103: static final int M_RST3 = 0xd3;
0104: static final int M_RST4 = 0xd4;
0105: static final int M_RST5 = 0xd5;
0106: static final int M_RST6 = 0xd6;
0107: static final int M_RST7 = 0xd7;
0108: static final int M_SOI = 0xd8;
0109: static final int M_EOI = 0xd9;
0110: static final int M_SOS = 0xda;
0111: static final int M_DQT = 0xdb;
0112: static final int M_DNL = 0xdc;
0113: static final int M_DRI = 0xdd;
0114: static final int M_DHP = 0xde;
0115: static final int M_EXP = 0xdf;
0116: static final int M_APP0 = 0xe0;
0117: static final int M_APP1 = 0xe1;
0118: static final int M_APP2 = 0xe2;
0119: static final int M_APP3 = 0xe3;
0120: static final int M_APP4 = 0xe4;
0121: static final int M_APP5 = 0xe5;
0122: static final int M_APP6 = 0xe6;
0123: static final int M_APP7 = 0xe7;
0124: static final int M_APP8 = 0xe8;
0125: static final int M_APP9 = 0xe9;
0126: static final int M_APP10 = 0xea;
0127: static final int M_APP11 = 0xeb;
0128: static final int M_APP12 = 0xec;
0129: static final int M_APP13 = 0xed;
0130: static final int M_APP14 = 0xee;
0131: static final int M_APP15 = 0xef;
0132: static final int M_JPG0 = 0xf0;
0133: static final int M_JPG13 = 0xfd;
0134: static final int M_COM = 0xfe;
0135: static final int M_TEM = 0x01;
0136: static final int M_ERROR = 0x100;
0137:
0138: /* Values of global_state field (jdapi.c has some dependencies on ordering!) */
0139: static final int CSTATE_START = 100; /* after create_compress */
0140: static final int CSTATE_SCANNING = 101; /* start_compress done, write_scanlines OK */
0141: static final int CSTATE_RAW_OK = 102; /* start_compress done, write_raw_data OK */
0142: static final int CSTATE_WRCOEFS = 103; /* jpeg_write_coefficients done */
0143: static final int DSTATE_START = 200; /* after create_decompress */
0144: static final int DSTATE_INHEADER = 201; /* reading header markers, no SOS yet */
0145: static final int DSTATE_READY = 202; /* found SOS, ready for start_decompress */
0146: static final int DSTATE_PRELOAD = 203; /* reading multiscan file in start_decompress*/
0147: static final int DSTATE_PRESCAN = 204; /* performing dummy pass for 2-pass quant */
0148: static final int DSTATE_SCANNING = 205; /* start_decompress done, read_scanlines OK */
0149: static final int DSTATE_RAW_OK = 206; /* start_decompress done, read_raw_data OK */
0150: static final int DSTATE_BUFIMAGE = 207; /* expecting jpeg_start_output */
0151: static final int DSTATE_BUFPOST = 208; /* looking for SOS/EOI in jpeg_finish_output */
0152: static final int DSTATE_RDCOEFS = 209; /* reading file in jpeg_read_coefficients */
0153: static final int DSTATE_STOPPING = 210; /* looking for EOI in jpeg_finish_decompress */
0154:
0155: static final int JPEG_REACHED_SOS = 1; /* Reached start of new scan */
0156: static final int JPEG_REACHED_EOI = 2; /* Reached end of image */
0157: static final int JPEG_ROW_COMPLETED = 3; /* Completed one iMCU row */
0158: static final int JPEG_SCAN_COMPLETED = 4; /* Completed last iMCU row of a scan */
0159:
0160: static final int JPEG_SUSPENDED = 0; /* Suspended due to lack of input data */
0161: static final int JPEG_HEADER_OK = 1; /* Found valid image datastream */
0162: static final int JPEG_HEADER_TABLES_ONLY = 2; /* Found valid table-specs-only datastream */
0163:
0164: /* Function pointers */
0165: static final int DECOMPRESS_DATA = 0;
0166: static final int DECOMPRESS_SMOOTH_DATA = 1;
0167: static final int DECOMPRESS_ONEPASS = 2;
0168:
0169: static final int CONSUME_DATA = 0;
0170: static final int DUMMY_CONSUME_DATA = 1;
0171:
0172: static final int PROCESS_DATA_SIMPLE_MAIN = 0;
0173: static final int PROCESS_DATA_CONTEXT_MAIN = 1;
0174: static final int PROCESS_DATA_CRANK_POST = 2;
0175:
0176: static final int POST_PROCESS_1PASS = 0;
0177: static final int POST_PROCESS_DATA_UPSAMPLE = 1;
0178:
0179: static final int NULL_CONVERT = 0;
0180: static final int GRAYSCALE_CONVERT = 1;
0181: static final int YCC_RGB_CONVERT = 2;
0182: static final int GRAY_RGB_CONVERT = 3;
0183: static final int YCCK_CMYK_CONVERT = 4;
0184:
0185: static final int NOOP_UPSAMPLE = 0;
0186: static final int FULLSIZE_UPSAMPLE = 1;
0187: static final int H2V1_FANCY_UPSAMPLE = 2;
0188: static final int H2V1_UPSAMPLE = 3;
0189: static final int H2V2_FANCY_UPSAMPLE = 4;
0190: static final int H2V2_UPSAMPLE = 5;
0191: static final int INT_UPSAMPLE = 6;
0192:
0193: static final int INPUT_CONSUME_INPUT = 0;
0194: static final int COEF_CONSUME_INPUT = 1;
0195:
0196: static int extend_test[] = /* entry n is 2**(n-1) */
0197: { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040,
0198: 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000,
0199: 0x4000 };
0200:
0201: static int extend_offset[] = /* entry n is (-1 << n) + 1 */
0202: { 0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1,
0203: ((-1) << 4) + 1, ((-1) << 5) + 1, ((-1) << 6) + 1,
0204: ((-1) << 7) + 1, ((-1) << 8) + 1, ((-1) << 9) + 1,
0205: ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1,
0206: ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1 };
0207:
0208: static int jpeg_natural_order[] = { 0, 1, 8, 16, 9, 2, 3, 10, 17,
0209: 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34,
0210: 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43,
0211: 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31,
0212: 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, 63, 63, 63, 63, 63,
0213: 63, 63, 63, /* extra entries for safety in decoder */
0214: 63, 63, 63, 63, 63, 63, 63, 63 };
0215:
0216: static final class JQUANT_TBL {
0217: /* This array gives the coefficient quantizers in natural array order
0218: * (not the zigzag order in which they are stored in a JPEG DQT marker).
0219: * CAUTION: IJG versions prior to v6a kept this array in zigzag order.
0220: */
0221: short[] quantval = new short[DCTSIZE2]; /* quantization step for each coefficient */
0222: /* This field is used only during compression. It's initialized false when
0223: * the table is created, and set true when it's been output to the file.
0224: * You could suppress output of a table by setting this to true.
0225: * (See jpeg_suppress_tables for an example.)
0226: */
0227: boolean sent_table; /* true when table has been output */
0228: }
0229:
0230: static final class JHUFF_TBL {
0231: /* These two fields directly represent the contents of a JPEG DHT marker */
0232: byte[] bits = new byte[17]; /* bits[k] = # of symbols with codes of */
0233: /* length k bits; bits[0] is unused */
0234: byte[] huffval = new byte[256]; /* The symbols, in order of incr code length */
0235: /* This field is used only during compression. It's initialized false when
0236: * the table is created, and set true when it's been output to the file.
0237: * You could suppress output of a table by setting this to true.
0238: * (See jpeg_suppress_tables for an example.)
0239: */
0240: boolean sent_table; /* true when table has been output */
0241: }
0242:
0243: static final class bitread_perm_state { /* Bitreading state saved across MCUs */
0244: int get_buffer; /* current bit-extraction buffer */
0245: int bits_left; /* # of unused bits in it */
0246: }
0247:
0248: static final class bitread_working_state { /* Bitreading working state within an MCU */
0249: /* Current data source location */
0250: /* We need a copy, rather than munging the original, in case of suspension */
0251: byte[] buffer; /* => next byte to read from source */
0252: int bytes_offset;
0253: int bytes_in_buffer; /* # of bytes remaining in source buffer */
0254: /* Bit input buffer --- note these values are kept in register variables,
0255: * not in this struct, inside the inner loops.
0256: */
0257: int get_buffer; /* current bit-extraction buffer */
0258: int bits_left; /* # of unused bits in it */
0259: /* Pointer needed by jpeg_fill_bit_buffer. */
0260: jpeg_decompress_struct cinfo; /* back link to decompress master record */
0261: }
0262:
0263: static final class savable_state {
0264: int EOBRUN; //Note that this is only used in the progressive case
0265: int[] last_dc_val = new int[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
0266: }
0267:
0268: static final class d_derived_tbl {
0269: /* Basic tables: (element [0] of each array is unused) */
0270: int[] maxcode = new int[18]; /* largest code of length k (-1 if none) */
0271: /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
0272: int[] valoffset = new int[17]; /* huffval[] offset for codes of length k */
0273: /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
0274: * the smallest code of length k; so given a code of length k, the
0275: * corresponding symbol is huffval[code + valoffset[k]]
0276: */
0277:
0278: /* Link to public Huffman table (needed only in jpeg_huff_decode) */
0279: JHUFF_TBL pub;
0280:
0281: /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
0282: * the input data stream. If the next Huffman code is no more
0283: * than HUFF_LOOKAHEAD bits long, we can obtain its length and
0284: * the corresponding symbol directly from these tables.
0285: */
0286: int[] look_nbits = new int[1 << HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
0287: byte[] look_sym = new byte[1 << HUFF_LOOKAHEAD]; /* symbol, or unused */
0288: }
0289:
0290: static final class jpeg_d_coef_controller {
0291: int consume_data;
0292: int decompress_data;
0293:
0294: /* Pointer to array of coefficient virtual arrays, or null if none */
0295: short[][][] coef_arrays;
0296:
0297: /* These variables keep track of the current location of the input side. */
0298: /* cinfo.input_iMCU_row is also used for this. */
0299: int MCU_ctr; /* counts MCUs processed in current row */
0300: int MCU_vert_offset; /* counts MCU rows within iMCU row */
0301: int MCU_rows_per_iMCU_row; /* number of such rows needed */
0302:
0303: /* The output side's location is represented by cinfo.output_iMCU_row. */
0304:
0305: /* In single-pass modes, it's sufficient to buffer just one MCU.
0306: * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
0307: * and let the entropy decoder write into that workspace each time.
0308: * (On 80x86, the workspace is FAR even though it's not really very big;
0309: * this is to keep the module interfaces unchanged when a large coefficient
0310: * buffer is necessary.)
0311: * In multi-pass modes, this array points to the current MCU's blocks
0312: * within the virtual arrays; it is used only by the input side.
0313: */
0314: short[][] MCU_buffer = new short[D_MAX_BLOCKS_IN_MCU][];
0315:
0316: /* In multi-pass modes, we need a virtual block array for each component. */
0317: short[][][][] whole_image = new short[MAX_COMPONENTS][][][];
0318:
0319: /* When doing block smoothing, we latch coefficient Al values here */
0320: int[] coef_bits_latch;
0321:
0322: short[] workspace;
0323:
0324: void start_input_pass(jpeg_decompress_struct cinfo) {
0325: cinfo.input_iMCU_row = 0;
0326: start_iMCU_row(cinfo);
0327: }
0328:
0329: /* Reset within-iMCU-row counters for a new row (input side) */
0330: void start_iMCU_row(jpeg_decompress_struct cinfo) {
0331: jpeg_d_coef_controller coef = cinfo.coef;
0332:
0333: /* In an interleaved scan, an MCU row is the same as an iMCU row.
0334: * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
0335: * But at the bottom of the image, process only what's left.
0336: */
0337: if (cinfo.comps_in_scan > 1) {
0338: coef.MCU_rows_per_iMCU_row = 1;
0339: } else {
0340: if (cinfo.input_iMCU_row < (cinfo.total_iMCU_rows - 1))
0341: coef.MCU_rows_per_iMCU_row = cinfo.cur_comp_info[0].v_samp_factor;
0342: else
0343: coef.MCU_rows_per_iMCU_row = cinfo.cur_comp_info[0].last_row_height;
0344: }
0345:
0346: coef.MCU_ctr = 0;
0347: coef.MCU_vert_offset = 0;
0348: }
0349:
0350: }
0351:
0352: static abstract class jpeg_entropy_decoder {
0353: abstract void start_pass(jpeg_decompress_struct cinfo);
0354:
0355: abstract boolean decode_mcu(jpeg_decompress_struct cinfo,
0356: short[][] MCU_data);
0357:
0358: /* This is here to share code between baseline and progressive decoders; */
0359: /* other modules probably should not use it */
0360: boolean insufficient_data; /* set true after emitting warning */
0361:
0362: bitread_working_state br_state_local = new bitread_working_state();
0363: savable_state state_local = new savable_state();
0364: }
0365:
0366: static final class huff_entropy_decoder extends
0367: jpeg_entropy_decoder {
0368: bitread_perm_state bitstate = new bitread_perm_state(); /* Bit buffer at start of MCU */
0369: savable_state saved = new savable_state(); /* Other state at start of MCU */
0370:
0371: /* These fields are NOT loaded into local working state. */
0372: int restarts_to_go; /* MCUs left in this restart interval */
0373:
0374: /* Pointers to derived tables (these workspaces have image lifespan) */
0375: d_derived_tbl[] dc_derived_tbls = new d_derived_tbl[NUM_HUFF_TBLS];
0376: d_derived_tbl[] ac_derived_tbls = new d_derived_tbl[NUM_HUFF_TBLS];
0377:
0378: /* Precalculated info set up by start_pass for use in decode_mcu: */
0379:
0380: /* Pointers to derived tables to be used for each block within an MCU */
0381: d_derived_tbl[] dc_cur_tbls = new d_derived_tbl[D_MAX_BLOCKS_IN_MCU];
0382: d_derived_tbl[] ac_cur_tbls = new d_derived_tbl[D_MAX_BLOCKS_IN_MCU];
0383: /* Whether we care about the DC and AC coefficient values for each block */
0384: boolean[] dc_needed = new boolean[D_MAX_BLOCKS_IN_MCU];
0385: boolean[] ac_needed = new boolean[D_MAX_BLOCKS_IN_MCU];
0386:
0387: void start_pass(jpeg_decompress_struct cinfo) {
0388: start_pass_huff_decoder(cinfo);
0389: }
0390:
0391: boolean decode_mcu(jpeg_decompress_struct cinfo,
0392: short[][] MCU_data) {
0393: huff_entropy_decoder entropy = this ;
0394: int blkn;
0395: // BITREAD_STATE_VARS;
0396: int get_buffer;
0397: int bits_left;
0398: // bitread_working_state br_state = new bitread_working_state();
0399: // savable_state state = new savable_state();
0400: bitread_working_state br_state = br_state_local;
0401: savable_state state = state_local;
0402:
0403: /* Process restart marker if needed; may have to suspend */
0404: if (cinfo.restart_interval != 0) {
0405: if (entropy.restarts_to_go == 0)
0406: if (!process_restart(cinfo))
0407: return false;
0408: }
0409:
0410: /* If we've run out of data, just leave the MCU set to zeroes.
0411: * This way, we return uniform gray for the remainder of the segment.
0412: */
0413: if (!entropy.insufficient_data) {
0414:
0415: /* Load up working state */
0416: // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
0417: br_state.cinfo = cinfo;
0418: br_state.buffer = cinfo.buffer;
0419: br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
0420: br_state.bytes_offset = cinfo.bytes_offset;
0421: get_buffer = entropy.bitstate.get_buffer;
0422: bits_left = entropy.bitstate.bits_left;
0423:
0424: // ASSIGN_STATE(state, entropy.saved);
0425: state.last_dc_val[0] = entropy.saved.last_dc_val[0];
0426: state.last_dc_val[1] = entropy.saved.last_dc_val[1];
0427: state.last_dc_val[2] = entropy.saved.last_dc_val[2];
0428: state.last_dc_val[3] = entropy.saved.last_dc_val[3];
0429:
0430: /* Outer loop handles each block in the MCU */
0431:
0432: for (blkn = 0; blkn < cinfo.blocks_in_MCU; blkn++) {
0433: short[] block = MCU_data[blkn];
0434: d_derived_tbl dctbl = entropy.dc_cur_tbls[blkn];
0435: d_derived_tbl actbl = entropy.ac_cur_tbls[blkn];
0436: int s = 0, k, r;
0437:
0438: /* Decode a single block's worth of coefficients */
0439:
0440: /* Section F.2.2.1: decode the DC coefficient difference */
0441: // HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
0442: {
0443: int nb = 0, look;
0444: if (bits_left < HUFF_LOOKAHEAD) {
0445: if (!jpeg_fill_bit_buffer(br_state,
0446: get_buffer, bits_left, 0)) {
0447: return false;
0448: }
0449: get_buffer = br_state.get_buffer;
0450: bits_left = br_state.bits_left;
0451: if (bits_left < HUFF_LOOKAHEAD) {
0452: nb = 1;
0453: // goto slowlabel;
0454: if ((s = jpeg_huff_decode(br_state,
0455: get_buffer, bits_left, dctbl,
0456: nb)) < 0) {
0457: return false;
0458: }
0459: get_buffer = br_state.get_buffer;
0460: bits_left = br_state.bits_left;
0461: }
0462: }
0463: // look = PEEK_BITS(HUFF_LOOKAHEAD);
0464: if (nb != 1) {
0465: look = (((get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1 << (HUFF_LOOKAHEAD)) - 1));
0466: if ((nb = dctbl.look_nbits[look]) != 0) {
0467: // DROP_BITS(nb);
0468: bits_left -= nb;
0469: s = dctbl.look_sym[look] & 0xFF;
0470: } else {
0471: nb = HUFF_LOOKAHEAD + 1;
0472: // slowlabel:
0473: if ((s = jpeg_huff_decode(br_state,
0474: get_buffer, bits_left, dctbl,
0475: nb)) < 0) {
0476: return false;
0477: }
0478: get_buffer = br_state.get_buffer;
0479: bits_left = br_state.bits_left;
0480: }
0481: }
0482: }
0483:
0484: if (s != 0) {
0485: // CHECK_BIT_BUFFER(br_state, s, return FALSE);
0486: {
0487: if (bits_left < (s)) {
0488: if (!jpeg_fill_bit_buffer(br_state,
0489: get_buffer, bits_left, s)) {
0490: return false;
0491: }
0492: get_buffer = (br_state).get_buffer;
0493: bits_left = (br_state).bits_left;
0494: }
0495: }
0496: // r = GET_BITS(s);
0497: r = (((get_buffer >> (bits_left -= (s)))) & ((1 << (s)) - 1));
0498: // s = HUFF_EXTEND(r, s);
0499: s = ((r) < extend_test[s] ? (r)
0500: + extend_offset[s] : (r));
0501: }
0502:
0503: if (entropy.dc_needed[blkn]) {
0504: /* Convert DC difference to actual value, update last_dc_val */
0505: int ci = cinfo.MCU_membership[blkn];
0506: s += state.last_dc_val[ci];
0507: state.last_dc_val[ci] = s;
0508: /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
0509: block[0] = (short) s;
0510: }
0511:
0512: if (entropy.ac_needed[blkn]) {
0513:
0514: /* Section F.2.2.2: decode the AC coefficients */
0515: /* Since zeroes are skipped, output area must be cleared beforehand */
0516: for (k = 1; k < DCTSIZE2; k++) {
0517: // HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
0518: {
0519: int nb = 0, look;
0520: if (bits_left < HUFF_LOOKAHEAD) {
0521: if (!jpeg_fill_bit_buffer(br_state,
0522: get_buffer, bits_left, 0)) {
0523: return false;
0524: }
0525: get_buffer = br_state.get_buffer;
0526: bits_left = br_state.bits_left;
0527: if (bits_left < HUFF_LOOKAHEAD) {
0528: nb = 1;
0529: // goto slowlabel;
0530: if ((s = jpeg_huff_decode(
0531: br_state, get_buffer,
0532: bits_left, actbl, nb)) < 0) {
0533: return false;
0534: }
0535: get_buffer = br_state.get_buffer;
0536: bits_left = br_state.bits_left;
0537: }
0538: }
0539: if (nb != 1) {
0540: // look = PEEK_BITS(HUFF_LOOKAHEAD);
0541: look = (((get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1 << (HUFF_LOOKAHEAD)) - 1));
0542: if ((nb = actbl.look_nbits[look]) != 0) {
0543: // DROP_BITS(nb);
0544: bits_left -= (nb);
0545: s = actbl.look_sym[look] & 0xFF;
0546: } else {
0547: nb = HUFF_LOOKAHEAD + 1;
0548: // slowlabel:
0549: if ((s = jpeg_huff_decode(
0550: br_state, get_buffer,
0551: bits_left, actbl, nb)) < 0) {
0552: return false;
0553: }
0554: get_buffer = br_state.get_buffer;
0555: bits_left = br_state.bits_left;
0556: }
0557: }
0558: }
0559: r = s >> 4;
0560: s &= 15;
0561:
0562: if (s != 0) {
0563: k += r;
0564: // CHECK_BIT_BUFFER(br_state, s, return FALSE);
0565: {
0566: if (bits_left < (s)) {
0567: if (!jpeg_fill_bit_buffer(
0568: br_state, get_buffer,
0569: bits_left, s)) {
0570: return false;
0571: }
0572: get_buffer = (br_state).get_buffer;
0573: bits_left = (br_state).bits_left;
0574: }
0575: }
0576: // r = GET_BITS(s);
0577: r = (((get_buffer >> (bits_left -= (s)))) & ((1 << (s)) - 1));
0578: // s = HUFF_EXTEND(r, s);
0579: s = ((r) < extend_test[s] ? (r)
0580: + extend_offset[s] : (r));
0581: /*
0582: * Output coefficient in natural (dezigzagged)
0583: * order. Note: the extra entries in
0584: * jpeg_natural_order[] will save us if k >=
0585: * DCTSIZE2, which could happen if the data is
0586: * corrupted.
0587: */
0588: block[jpeg_natural_order[k]] = (short) s;
0589: } else {
0590: if (r != 15)
0591: break;
0592: k += 15;
0593: }
0594: }
0595:
0596: } else {
0597:
0598: /* Section F.2.2.2: decode the AC coefficients */
0599: /* In this path we just discard the values */
0600: for (k = 1; k < DCTSIZE2; k++) {
0601: // HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
0602: {
0603: int nb = 0, look;
0604: if (bits_left < HUFF_LOOKAHEAD) {
0605: if (!jpeg_fill_bit_buffer(br_state,
0606: get_buffer, bits_left, 0)) {
0607: return false;
0608: }
0609: get_buffer = br_state.get_buffer;
0610: bits_left = br_state.bits_left;
0611: if (bits_left < HUFF_LOOKAHEAD) {
0612: nb = 1;
0613: // goto slowlabel;
0614: if ((s = jpeg_huff_decode(
0615: br_state, get_buffer,
0616: bits_left, actbl, nb)) < 0) {
0617: return false;
0618: }
0619: get_buffer = br_state.get_buffer;
0620: bits_left = br_state.bits_left;
0621: }
0622: }
0623: if (nb != 1) {
0624: // look = PEEK_BITS(HUFF_LOOKAHEAD);
0625: look = (((get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1 << (HUFF_LOOKAHEAD)) - 1));
0626: if ((nb = actbl.look_nbits[look]) != 0) {
0627: // DROP_BITS(nb);
0628: bits_left -= (nb);
0629: s = actbl.look_sym[look] & 0xFF;
0630: } else {
0631: nb = HUFF_LOOKAHEAD + 1;
0632: // slowlabel:
0633: if ((s = jpeg_huff_decode(
0634: br_state, get_buffer,
0635: bits_left, actbl, nb)) < 0) {
0636: return false;
0637: }
0638: get_buffer = br_state.get_buffer;
0639: bits_left = br_state.bits_left;
0640: }
0641: }
0642: }
0643: r = s >> 4;
0644: s &= 15;
0645:
0646: if (s != 0) {
0647: k += r;
0648: // CHECK_BIT_BUFFER(br_state, s, return FALSE);
0649: {
0650: if (bits_left < (s)) {
0651: if (!jpeg_fill_bit_buffer(
0652: (br_state), get_buffer,
0653: bits_left, s)) {
0654: return false;
0655: }
0656: get_buffer = (br_state).get_buffer;
0657: bits_left = (br_state).bits_left;
0658: }
0659: }
0660: // DROP_BITS(s);
0661: bits_left -= s;
0662: } else {
0663: if (r != 15)
0664: break;
0665: k += 15;
0666: }
0667: }
0668:
0669: }
0670: }
0671:
0672: /* Completed MCU, so update state */
0673: // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
0674: cinfo.buffer = br_state.buffer;
0675: cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
0676: cinfo.bytes_offset = br_state.bytes_offset;
0677: entropy.bitstate.get_buffer = get_buffer;
0678: entropy.bitstate.bits_left = bits_left;
0679: // ASSIGN_STATE(entropy.saved, state);
0680: entropy.saved.last_dc_val[0] = state.last_dc_val[0];
0681: entropy.saved.last_dc_val[1] = state.last_dc_val[1];
0682: entropy.saved.last_dc_val[2] = state.last_dc_val[2];
0683: entropy.saved.last_dc_val[3] = state.last_dc_val[3];
0684: }
0685:
0686: /* Account for restart interval (no-op if not using restarts) */
0687: entropy.restarts_to_go--;
0688:
0689: return true;
0690: }
0691:
0692: void start_pass_huff_decoder(jpeg_decompress_struct cinfo) {
0693: huff_entropy_decoder entropy = this ;
0694: int ci, blkn, dctbl, actbl;
0695: jpeg_component_info compptr;
0696:
0697: /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
0698: * This ought to be an error condition, but we make it a warning because
0699: * there are some baseline files out there with all zeroes in these bytes.
0700: */
0701: if (cinfo.Ss != 0 || cinfo.Se != DCTSIZE2 - 1
0702: || cinfo.Ah != 0 || cinfo.Al != 0) {
0703: // WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
0704: }
0705:
0706: for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
0707: compptr = cinfo.cur_comp_info[ci];
0708: dctbl = compptr.dc_tbl_no;
0709: actbl = compptr.ac_tbl_no;
0710: /* Compute derived values for Huffman tables */
0711: /* We may do this more than once for a table, but it's not expensive */
0712: jpeg_make_d_derived_tbl(
0713: cinfo,
0714: true,
0715: dctbl,
0716: entropy.dc_derived_tbls[dctbl] = new d_derived_tbl());
0717: jpeg_make_d_derived_tbl(
0718: cinfo,
0719: false,
0720: actbl,
0721: entropy.ac_derived_tbls[actbl] = new d_derived_tbl());
0722: /* Initialize DC predictions to 0 */
0723: entropy.saved.last_dc_val[ci] = 0;
0724: }
0725:
0726: /* Precalculate decoding info for each block in an MCU of this scan */
0727: for (blkn = 0; blkn < cinfo.blocks_in_MCU; blkn++) {
0728: ci = cinfo.MCU_membership[blkn];
0729: compptr = cinfo.cur_comp_info[ci];
0730: /* Precalculate which table to use for each block */
0731: entropy.dc_cur_tbls[blkn] = entropy.dc_derived_tbls[compptr.dc_tbl_no];
0732: entropy.ac_cur_tbls[blkn] = entropy.ac_derived_tbls[compptr.ac_tbl_no];
0733: /* Decide whether we really care about the coefficient values */
0734: if (compptr.component_needed) {
0735: entropy.dc_needed[blkn] = true;
0736: /* we don't need the ACs if producing a 1/8th-size image */
0737: entropy.ac_needed[blkn] = (compptr.DCT_scaled_size > 1);
0738: } else {
0739: entropy.dc_needed[blkn] = entropy.ac_needed[blkn] = false;
0740: }
0741: }
0742:
0743: /* Initialize bitread state variables */
0744: entropy.bitstate.bits_left = 0;
0745: entropy.bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
0746: entropy.insufficient_data = false;
0747:
0748: /* Initialize restart counter */
0749: entropy.restarts_to_go = cinfo.restart_interval;
0750: }
0751:
0752: boolean process_restart(jpeg_decompress_struct cinfo) {
0753: huff_entropy_decoder entropy = this ;
0754: int ci;
0755:
0756: /* Throw away any unused bits remaining in bit buffer; */
0757: /* include any full bytes in next_marker's count of discarded bytes */
0758: cinfo.marker.discarded_bytes += entropy.bitstate.bits_left / 8;
0759: entropy.bitstate.bits_left = 0;
0760:
0761: /* Advance past the RSTn marker */
0762: if (!read_restart_marker(cinfo))
0763: return false;
0764:
0765: /* Re-initialize DC predictions to 0 */
0766: for (ci = 0; ci < cinfo.comps_in_scan; ci++)
0767: entropy.saved.last_dc_val[ci] = 0;
0768:
0769: /* Reset restart counter */
0770: entropy.restarts_to_go = cinfo.restart_interval;
0771:
0772: /* Reset out-of-data flag, unless read_restart_marker left us smack up
0773: * against a marker. In that case we will end up treating the next data
0774: * segment as empty, and we can avoid producing bogus output pixels by
0775: * leaving the flag set.
0776: */
0777: if (cinfo.unread_marker == 0)
0778: entropy.insufficient_data = false;
0779:
0780: return true;
0781: }
0782: }
0783:
0784: static final class phuff_entropy_decoder extends
0785: jpeg_entropy_decoder {
0786:
0787: /* These fields are loaded into local variables at start of each MCU.
0788: * In case of suspension, we exit WITHOUT updating them.
0789: */
0790: bitread_perm_state bitstate = new bitread_perm_state(); /* Bit buffer at start of MCU */
0791: savable_state saved = new savable_state(); /* Other state at start of MCU */
0792:
0793: /* These fields are NOT loaded into local working state. */
0794: int restarts_to_go; /* MCUs left in this restart interval */
0795:
0796: /* Pointers to derived tables (these workspaces have image lifespan) */
0797: d_derived_tbl[] derived_tbls = new d_derived_tbl[NUM_HUFF_TBLS];
0798:
0799: d_derived_tbl ac_derived_tbl; /* active table during an AC scan */
0800:
0801: int[] newnz_pos = new int[DCTSIZE2];
0802:
0803: void start_pass(jpeg_decompress_struct cinfo) {
0804: start_pass_phuff_decoder(cinfo);
0805: }
0806:
0807: boolean decode_mcu(jpeg_decompress_struct cinfo,
0808: short[][] MCU_data) {
0809: boolean is_DC_band = (cinfo.Ss == 0);
0810: if (cinfo.Ah == 0) {
0811: if (is_DC_band)
0812: return decode_mcu_DC_first(cinfo, MCU_data);
0813: else
0814: return decode_mcu_AC_first(cinfo, MCU_data);
0815: } else {
0816: if (is_DC_band)
0817: return decode_mcu_DC_refine(cinfo, MCU_data);
0818: else
0819: return decode_mcu_AC_refine(cinfo, MCU_data);
0820: }
0821: }
0822:
0823: boolean decode_mcu_DC_refine(jpeg_decompress_struct cinfo,
0824: short[][] MCU_data) {
0825: phuff_entropy_decoder entropy = this ;
0826: int p1 = 1 << cinfo.Al; /* 1 in the bit position being coded */
0827: int blkn;
0828: short[] block;
0829: // BITREAD_STATE_VARS;
0830: int get_buffer;
0831: int bits_left;
0832: // bitread_working_state br_state = new bitread_working_state();
0833: bitread_working_state br_state = br_state_local;
0834:
0835: /* Process restart marker if needed; may have to suspend */
0836: if (cinfo.restart_interval != 0) {
0837: if (entropy.restarts_to_go == 0)
0838: if (!process_restart(cinfo))
0839: return false;
0840: }
0841:
0842: /* Not worth the cycles to check insufficient_data here,
0843: * since we will not change the data anyway if we read zeroes.
0844: */
0845:
0846: /* Load up working state */
0847: // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
0848: br_state.cinfo = cinfo;
0849: br_state.buffer = cinfo.buffer;
0850: br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
0851: br_state.bytes_offset = cinfo.bytes_offset;
0852: get_buffer = entropy.bitstate.get_buffer;
0853: bits_left = entropy.bitstate.bits_left;
0854:
0855: /* Outer loop handles each block in the MCU */
0856:
0857: for (blkn = 0; blkn < cinfo.blocks_in_MCU; blkn++) {
0858: block = MCU_data[blkn];
0859:
0860: /* Encoded data is simply the next bit of the two's-complement DC value */
0861: // CHECK_BIT_BUFFER(br_state, 1, return FALSE);
0862: {
0863: if (bits_left < (1)) {
0864: if (!jpeg_fill_bit_buffer(br_state, get_buffer,
0865: bits_left, 1)) {
0866: return false;
0867: }
0868: get_buffer = (br_state).get_buffer;
0869: bits_left = (br_state).bits_left;
0870: }
0871: }
0872: // if (GET_BITS(1))
0873: if ((((get_buffer >> (bits_left -= (1)))) & ((1 << (1)) - 1)) != 0)
0874: block[0] |= p1;
0875: /* Note: since we use |=, repeating the assignment later is safe */
0876: }
0877:
0878: /* Completed MCU, so update state */
0879: // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
0880: cinfo.buffer = br_state.buffer;
0881: cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
0882: cinfo.bytes_offset = br_state.bytes_offset;
0883: entropy.bitstate.get_buffer = get_buffer;
0884: entropy.bitstate.bits_left = bits_left;
0885:
0886: /* Account for restart interval (no-op if not using restarts) */
0887: entropy.restarts_to_go--;
0888:
0889: return true;
0890:
0891: }
0892:
0893: boolean decode_mcu_AC_refine(jpeg_decompress_struct cinfo,
0894: short[][] MCU_data) {
0895: phuff_entropy_decoder entropy = this ;
0896: int Se = cinfo.Se;
0897: int p1 = 1 << cinfo.Al; /* 1 in the bit position being coded */
0898: int m1 = (-1) << cinfo.Al; /* -1 in the bit position being coded */
0899: int s = 0, k, r;
0900: int EOBRUN;
0901: short[] block;
0902: short[] this coef;
0903: // BITREAD_STATE_VARS;
0904: int get_buffer;
0905: int bits_left;
0906: // bitread_working_state br_state = new bitread_working_state();
0907: bitread_working_state br_state = br_state_local;
0908:
0909: d_derived_tbl tbl;
0910: int num_newnz;
0911: int[] newnz_pos = entropy.newnz_pos;
0912:
0913: /* Process restart marker if needed; may have to suspend */
0914: if (cinfo.restart_interval != 0) {
0915: if (entropy.restarts_to_go == 0)
0916: if (!process_restart(cinfo))
0917: return false;
0918: }
0919:
0920: /* If we've run out of data, don't modify the MCU.
0921: */
0922: if (!entropy.insufficient_data) {
0923:
0924: /* Load up working state */
0925: // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
0926: br_state.cinfo = cinfo;
0927: br_state.buffer = cinfo.buffer;
0928: br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
0929: br_state.bytes_offset = cinfo.bytes_offset;
0930: get_buffer = entropy.bitstate.get_buffer;
0931: bits_left = entropy.bitstate.bits_left;
0932:
0933: EOBRUN = entropy.saved.EOBRUN; /* only part of saved state we need */
0934:
0935: /* There is always only one block per MCU */
0936: block = MCU_data[0];
0937: tbl = entropy.ac_derived_tbl;
0938:
0939: /* If we are forced to suspend, we must undo the assignments to any newly
0940: * nonzero coefficients in the block, because otherwise we'd get confused
0941: * next time about which coefficients were already nonzero.
0942: * But we need not undo addition of bits to already-nonzero coefficients;
0943: * instead, we can test the current bit to see if we already did it.
0944: */
0945: num_newnz = 0;
0946:
0947: /* initialize coefficient loop counter to start of band */
0948: k = cinfo.Ss;
0949:
0950: if (EOBRUN == 0) {
0951: for (; k <= Se; k++) {
0952: // HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
0953: {
0954: int nb = 0, look;
0955: if (bits_left < HUFF_LOOKAHEAD) {
0956: if (!jpeg_fill_bit_buffer(br_state,
0957: get_buffer, bits_left, 0)) {
0958: // failaction;
0959: while (num_newnz > 0)
0960: block[newnz_pos[--num_newnz]] = 0;
0961:
0962: return false;
0963: }
0964: get_buffer = br_state.get_buffer;
0965: bits_left = br_state.bits_left;
0966: if (bits_left < HUFF_LOOKAHEAD) {
0967: nb = 1;
0968: // goto slowlabel;
0969: if ((s = jpeg_huff_decode(br_state,
0970: get_buffer, bits_left, tbl,
0971: nb)) < 0) {
0972: // failaction;
0973: while (num_newnz > 0)
0974: block[newnz_pos[--num_newnz]] = 0;
0975:
0976: return false;
0977: }
0978: get_buffer = br_state.get_buffer;
0979: bits_left = br_state.bits_left;
0980: }
0981: }
0982: if (nb != 1) {
0983: // look = PEEK_BITS(HUFF_LOOKAHEAD);
0984: look = (((get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1 << (HUFF_LOOKAHEAD)) - 1));
0985: if ((nb = tbl.look_nbits[look]) != 0) {
0986: // DROP_BITS(nb);
0987: bits_left -= nb;
0988: s = tbl.look_sym[look] & 0xFF;
0989: } else {
0990: nb = HUFF_LOOKAHEAD + 1;
0991: // slowlabel:
0992: if ((s = jpeg_huff_decode(br_state,
0993: get_buffer, bits_left, tbl,
0994: nb)) < 0) {
0995: // failaction;
0996: while (num_newnz > 0)
0997: block[newnz_pos[--num_newnz]] = 0;
0998:
0999: return false;
1000: }
1001: get_buffer = br_state.get_buffer;
1002: bits_left = br_state.bits_left;
1003: }
1004: }
1005: }
1006: r = s >> 4;
1007: s &= 15;
1008: if (s != 0) {
1009: if (s != 1) { /* size of new coef should always be 1 */
1010: // WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
1011: }
1012: // CHECK_BIT_BUFFER(br_state, 1, goto undoit);
1013: {
1014: if (bits_left < (1)) {
1015: if (!jpeg_fill_bit_buffer(br_state,
1016: get_buffer, bits_left, 1)) {
1017: // failaction;
1018: while (num_newnz > 0)
1019: block[newnz_pos[--num_newnz]] = 0;
1020:
1021: return false;
1022: }
1023: get_buffer = (br_state).get_buffer;
1024: bits_left = (br_state).bits_left;
1025: }
1026: }
1027: // if (GET_BITS(1))
1028: if ((((get_buffer >> (bits_left -= (1)))) & ((1 << (1)) - 1)) != 0)
1029: s = p1; /* newly nonzero coef is positive */
1030: else
1031: s = m1; /* newly nonzero coef is negative */
1032: } else {
1033: if (r != 15) {
1034: EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
1035: if (r != 0) {
1036: // CHECK_BIT_BUFFER(br_state, r, goto undoit);
1037: {
1038: if (bits_left < (r)) {
1039: if (!jpeg_fill_bit_buffer(
1040: br_state,
1041: get_buffer,
1042: bits_left, r)) {
1043: // failaction;
1044: while (num_newnz > 0)
1045: block[newnz_pos[--num_newnz]] = 0;
1046:
1047: return false;
1048: }
1049: get_buffer = (br_state).get_buffer;
1050: bits_left = (br_state).bits_left;
1051: }
1052: }
1053: // r = GET_BITS(r);
1054: r = (((get_buffer >> (bits_left -= (r)))) & ((1 << (r)) - 1));
1055: EOBRUN += r;
1056: }
1057: break; /* rest of block is handled by EOB logic */
1058: }
1059: /* note s = 0 for processing ZRL */
1060: }
1061: /* Advance over already-nonzero coefs and r still-zero coefs,
1062: * appending correction bits to the nonzeroes. A correction bit is 1
1063: * if the absolute value of the coefficient must be increased.
1064: */
1065: do {
1066: this coef = block;
1067: int this coef_offset = jpeg_natural_order[k];
1068: if (this coef[this coef_offset] != 0) {
1069: // CHECK_BIT_BUFFER(br_state, 1, goto undoit);
1070: {
1071: if (bits_left < (1)) {
1072: if (!jpeg_fill_bit_buffer(
1073: br_state, get_buffer,
1074: bits_left, 1)) {
1075: // failaction;
1076: while (num_newnz > 0)
1077: block[newnz_pos[--num_newnz]] = 0;
1078:
1079: return false;
1080: }
1081: get_buffer = (br_state).get_buffer;
1082: bits_left = (br_state).bits_left;
1083: }
1084: }
1085: // if (GET_BITS(1)) {
1086: if ((((get_buffer >> (bits_left -= (1)))) & ((1 << (1)) - 1)) != 0) {
1087: if ((this coef[this coef_offset] & p1) == 0) { /* do nothing if already set it */
1088: if (this coef[this coef_offset] >= 0)
1089: this coef[this coef_offset] += p1;
1090: else
1091: this coef[this coef_offset] += m1;
1092: }
1093: }
1094: } else {
1095: if (--r < 0)
1096: break; /* reached target zero coefficient */
1097: }
1098: k++;
1099: } while (k <= Se);
1100: if (s != 0) {
1101: int pos = jpeg_natural_order[k];
1102: /* Output newly nonzero coefficient */
1103: block[pos] = (short) s;
1104: /* Remember its position in case we have to suspend */
1105: newnz_pos[num_newnz++] = pos;
1106: }
1107: }
1108: }
1109:
1110: if (EOBRUN > 0) {
1111: /* Scan any remaining coefficient positions after the end-of-band
1112: * (the last newly nonzero coefficient, if any). Append a correction
1113: * bit to each already-nonzero coefficient. A correction bit is 1
1114: * if the absolute value of the coefficient must be increased.
1115: */
1116: for (; k <= Se; k++) {
1117: this coef = block;
1118: int this coef_offset = jpeg_natural_order[k];
1119: if (this coef[this coef_offset] != 0) {
1120: // CHECK_BIT_BUFFER(br_state, 1, goto undoit);
1121: {
1122: if (bits_left < (1)) {
1123: if (!jpeg_fill_bit_buffer(br_state,
1124: get_buffer, bits_left, 1)) {
1125: // failaction;
1126: while (num_newnz > 0)
1127: block[newnz_pos[--num_newnz]] = 0;
1128:
1129: return false;
1130: }
1131: get_buffer = (br_state).get_buffer;
1132: bits_left = (br_state).bits_left;
1133: }
1134: }
1135: // if (GET_BITS(1)) {
1136: if ((((get_buffer >> (bits_left -= (1)))) & ((1 << (1)) - 1)) != 0) {
1137: if ((this coef[this coef_offset] & p1) == 0) { /* do nothing if already changed it */
1138: if (this coef[this coef_offset] >= 0)
1139: this coef[this coef_offset] += p1;
1140: else
1141: this coef[this coef_offset] += m1;
1142: }
1143: }
1144: }
1145: }
1146: /* Count one block completed in EOB run */
1147: EOBRUN--;
1148: }
1149:
1150: /* Completed MCU, so update state */
1151: // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
1152: cinfo.buffer = br_state.buffer;
1153: cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
1154: cinfo.bytes_offset = br_state.bytes_offset;
1155: entropy.bitstate.get_buffer = get_buffer;
1156: entropy.bitstate.bits_left = bits_left;
1157:
1158: entropy.saved.EOBRUN = EOBRUN; /* only part of saved state we need */
1159: }
1160:
1161: /* Account for restart interval (no-op if not using restarts) */
1162: entropy.restarts_to_go--;
1163:
1164: return true;
1165:
1166: // undoit:
1167: // /* Re-zero any output coefficients that we made newly nonzero */
1168: // while (num_newnz > 0)
1169: // (*block)[newnz_pos[--num_newnz]] = 0;
1170: //
1171: // return false;
1172:
1173: }
1174:
1175: boolean decode_mcu_AC_first(jpeg_decompress_struct cinfo,
1176: short[][] MCU_data) {
1177: phuff_entropy_decoder entropy = this ;
1178: int Se = cinfo.Se;
1179: int Al = cinfo.Al;
1180: int s = 0, k, r;
1181: int EOBRUN;
1182: short[] block;
1183: // BITREAD_STATE_VARS;
1184: int get_buffer;
1185: int bits_left;
1186: // bitread_working_state br_state = new bitread_working_state();
1187: bitread_working_state br_state = br_state_local;
1188:
1189: d_derived_tbl tbl;
1190:
1191: /* Process restart marker if needed; may have to suspend */
1192: if (cinfo.restart_interval != 0) {
1193: if (entropy.restarts_to_go == 0)
1194: if (!process_restart(cinfo))
1195: return false;
1196: }
1197:
1198: /* If we've run out of data, just leave the MCU set to zeroes.
1199: * This way, we return uniform gray for the remainder of the segment.
1200: */
1201: if (!entropy.insufficient_data) {
1202:
1203: /* Load up working state.
1204: * We can avoid loading/saving bitread state if in an EOB run.
1205: */
1206: EOBRUN = entropy.saved.EOBRUN; /* only part of saved state we need */
1207:
1208: /* There is always only one block per MCU */
1209:
1210: if (EOBRUN > 0) /* if it's a band of zeroes... */
1211: EOBRUN--; /* ...process it now (we do nothing) */
1212: else {
1213: // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
1214: br_state.cinfo = cinfo;
1215: br_state.buffer = cinfo.buffer;
1216: br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
1217: br_state.bytes_offset = cinfo.bytes_offset;
1218: get_buffer = entropy.bitstate.get_buffer;
1219: bits_left = entropy.bitstate.bits_left;
1220:
1221: block = MCU_data[0];
1222: tbl = entropy.ac_derived_tbl;
1223:
1224: for (k = cinfo.Ss; k <= Se; k++) {
1225: // HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
1226: {
1227: int nb = 0, look;
1228: if (bits_left < HUFF_LOOKAHEAD) {
1229: if (!jpeg_fill_bit_buffer(br_state,
1230: get_buffer, bits_left, 0)) {
1231: return false;
1232: }
1233: get_buffer = br_state.get_buffer;
1234: bits_left = br_state.bits_left;
1235: if (bits_left < HUFF_LOOKAHEAD) {
1236: nb = 1;
1237: // goto slowlabel;
1238: if ((s = jpeg_huff_decode(br_state,
1239: get_buffer, bits_left, tbl,
1240: nb)) < 0) {
1241: return false;
1242: }
1243: get_buffer = br_state.get_buffer;
1244: bits_left = br_state.bits_left;
1245: }
1246: }
1247: if (nb != 1) {
1248: // look = PEEK_BITS(HUFF_LOOKAHEAD);
1249: look = (((get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1 << (HUFF_LOOKAHEAD)) - 1));
1250:
1251: if ((nb = tbl.look_nbits[look]) != 0) {
1252: // DROP_BITS(nb);
1253: bits_left -= nb;
1254: s = tbl.look_sym[look] & 0xFF;
1255: } else {
1256: nb = HUFF_LOOKAHEAD + 1;
1257: // slowlabel:
1258: if ((s = jpeg_huff_decode(br_state,
1259: get_buffer, bits_left, tbl,
1260: nb)) < 0) {
1261: return false;
1262: }
1263: get_buffer = br_state.get_buffer;
1264: bits_left = br_state.bits_left;
1265: }
1266: }
1267: }
1268: r = s >> 4;
1269: s &= 15;
1270: if (s != 0) {
1271: k += r;
1272: // CHECK_BIT_BUFFER(br_state, s, return FALSE);
1273: {
1274: if (bits_left < (s)) {
1275: if (!jpeg_fill_bit_buffer(br_state,
1276: get_buffer, bits_left, s)) {
1277: return false;
1278: }
1279: get_buffer = (br_state).get_buffer;
1280: bits_left = (br_state).bits_left;
1281: }
1282: }
1283: // r = GET_BITS(s);
1284: r = (((get_buffer >> (bits_left -= (s)))) & ((1 << (s)) - 1));
1285: // s = HUFF_EXTEND(r, s);
1286: s = ((r) < extend_test[s] ? (r)
1287: + extend_offset[s] : (r));
1288: /* Scale and output coefficient in natural (dezigzagged) order */
1289: block[jpeg_natural_order[k]] = (short) (s << Al);
1290: } else {
1291: if (r == 15) { /* ZRL */
1292: k += 15; /* skip 15 zeroes in band */
1293: } else { /* EOBr, run length is 2^r + appended bits */
1294: EOBRUN = 1 << r;
1295: if (r != 0) { /* EOBr, r > 0 */
1296: // CHECK_BIT_BUFFER(br_state, r, return FALSE);
1297: {
1298: if (bits_left < (r)) {
1299: if (!jpeg_fill_bit_buffer(
1300: br_state,
1301: get_buffer,
1302: bits_left, r)) {
1303: return false;
1304: }
1305: get_buffer = (br_state).get_buffer;
1306: bits_left = (br_state).bits_left;
1307: }
1308: }
1309: // r = GET_BITS(r);
1310: r = (((get_buffer >> (bits_left -= (r)))) & ((1 << (r)) - 1));
1311: EOBRUN += r;
1312: }
1313: EOBRUN--; /* this band is processed at this moment */
1314: break; /* force end-of-band */
1315: }
1316: }
1317: }
1318:
1319: // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
1320: cinfo.buffer = br_state.buffer;
1321: cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
1322: cinfo.bytes_offset = br_state.bytes_offset;
1323: entropy.bitstate.get_buffer = get_buffer;
1324: entropy.bitstate.bits_left = bits_left;
1325: }
1326:
1327: /* Completed MCU, so update state */
1328: entropy.saved.EOBRUN = EOBRUN; /* only part of saved state we need */
1329: }
1330:
1331: /* Account for restart interval (no-op if not using restarts) */
1332: entropy.restarts_to_go--;
1333:
1334: return true;
1335: }
1336:
1337: boolean decode_mcu_DC_first(jpeg_decompress_struct cinfo,
1338: short[][] MCU_data) {
1339: phuff_entropy_decoder entropy = this ;
1340: int Al = cinfo.Al;
1341: int s = 0, r;
1342: int blkn, ci;
1343: short[] block;
1344: // BITREAD_STATE_VARS;
1345: int get_buffer;
1346: int bits_left;
1347: // bitread_working_state br_state = new bitread_working_state();
1348: bitread_working_state br_state = br_state_local;
1349:
1350: // savable_state state = new savable_state();
1351: savable_state state = state_local;
1352: d_derived_tbl tbl;
1353: jpeg_component_info compptr;
1354:
1355: /* Process restart marker if needed; may have to suspend */
1356: if (cinfo.restart_interval != 0) {
1357: if (entropy.restarts_to_go == 0)
1358: if (!process_restart(cinfo))
1359: return false;
1360: }
1361:
1362: /* If we've run out of data, just leave the MCU set to zeroes.
1363: * This way, we return uniform gray for the remainder of the segment.
1364: */
1365: if (!entropy.insufficient_data) {
1366:
1367: /* Load up working state */
1368: // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
1369: br_state.cinfo = cinfo;
1370: br_state.buffer = cinfo.buffer;
1371: br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
1372: br_state.bytes_offset = cinfo.bytes_offset;
1373: get_buffer = entropy.bitstate.get_buffer;
1374: bits_left = entropy.bitstate.bits_left;
1375:
1376: // ASSIGN_STATE(state, entropy.saved);
1377: state.EOBRUN = entropy.saved.EOBRUN;
1378: state.last_dc_val[0] = entropy.saved.last_dc_val[0];
1379: state.last_dc_val[1] = entropy.saved.last_dc_val[1];
1380: state.last_dc_val[2] = entropy.saved.last_dc_val[2];
1381: state.last_dc_val[3] = entropy.saved.last_dc_val[3];
1382:
1383: /* Outer loop handles each block in the MCU */
1384:
1385: for (blkn = 0; blkn < cinfo.blocks_in_MCU; blkn++) {
1386: block = MCU_data[blkn];
1387: ci = cinfo.MCU_membership[blkn];
1388: compptr = cinfo.cur_comp_info[ci];
1389: tbl = entropy.derived_tbls[compptr.dc_tbl_no];
1390:
1391: /* Decode a single block's worth of coefficients */
1392:
1393: /* Section F.2.2.1: decode the DC coefficient difference */
1394: // HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
1395: {
1396: int nb = 0, look;
1397: if (bits_left < HUFF_LOOKAHEAD) {
1398: if (!jpeg_fill_bit_buffer(br_state,
1399: get_buffer, bits_left, 0)) {
1400: return false;
1401: }
1402: get_buffer = br_state.get_buffer;
1403: bits_left = br_state.bits_left;
1404: if (bits_left < HUFF_LOOKAHEAD) {
1405: nb = 1;
1406: // goto slowlabel;
1407: if ((s = jpeg_huff_decode(br_state,
1408: get_buffer, bits_left, tbl, nb)) < 0) {
1409: return false;
1410: }
1411: get_buffer = br_state.get_buffer;
1412: bits_left = br_state.bits_left;
1413: }
1414: }
1415: if (nb != 1) {
1416: // look = PEEK_BITS(HUFF_LOOKAHEAD);
1417: look = (((get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1 << (HUFF_LOOKAHEAD)) - 1));
1418:
1419: if ((nb = tbl.look_nbits[look]) != 0) {
1420: // DROP_BITS(nb);
1421: bits_left -= nb;
1422: s = tbl.look_sym[look] & 0xFF;
1423: } else {
1424: nb = HUFF_LOOKAHEAD + 1;
1425: // slowlabel:
1426: if ((s = jpeg_huff_decode(br_state,
1427: get_buffer, bits_left, tbl, nb)) < 0) {
1428: return false;
1429: }
1430: get_buffer = br_state.get_buffer;
1431: bits_left = br_state.bits_left;
1432: }
1433: }
1434: }
1435: if (s != 0) {
1436: // CHECK_BIT_BUFFER(br_state, s, return FALSE);
1437: {
1438: if (bits_left < (s)) {
1439: if (!jpeg_fill_bit_buffer(br_state,
1440: get_buffer, bits_left, s)) {
1441: return false;
1442: }
1443: get_buffer = (br_state).get_buffer;
1444: bits_left = (br_state).bits_left;
1445: }
1446: }
1447: // r = GET_BITS(s);
1448: r = (((get_buffer >> (bits_left -= (s)))) & ((1 << (s)) - 1));
1449: // s = HUFF_EXTEND(r, s);
1450: s = ((r) < extend_test[s] ? (r)
1451: + extend_offset[s] : (r));
1452: }
1453:
1454: /* Convert DC difference to actual value, update last_dc_val */
1455: s += state.last_dc_val[ci];
1456: state.last_dc_val[ci] = s;
1457: /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
1458: block[0] = (short) (s << Al);
1459: }
1460:
1461: /* Completed MCU, so update state */
1462: // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
1463: cinfo.buffer = br_state.buffer;
1464: cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
1465: cinfo.bytes_offset = br_state.bytes_offset;
1466: entropy.bitstate.get_buffer = get_buffer;
1467: entropy.bitstate.bits_left = bits_left;
1468: // ASSIGN_STATE(entropy.saved, state);
1469: entropy.saved.EOBRUN = state.EOBRUN;
1470: entropy.saved.last_dc_val[0] = state.last_dc_val[0];
1471: entropy.saved.last_dc_val[1] = state.last_dc_val[1];
1472: entropy.saved.last_dc_val[2] = state.last_dc_val[2];
1473: entropy.saved.last_dc_val[3] = state.last_dc_val[3];
1474: }
1475:
1476: /* Account for restart interval (no-op if not using restarts) */
1477: entropy.restarts_to_go--;
1478:
1479: return true;
1480: }
1481:
1482: boolean process_restart(jpeg_decompress_struct cinfo) {
1483: phuff_entropy_decoder entropy = this ;
1484: int ci;
1485:
1486: /* Throw away any unused bits remaining in bit buffer; */
1487: /* include any full bytes in next_marker's count of discarded bytes */
1488: cinfo.marker.discarded_bytes += entropy.bitstate.bits_left / 8;
1489: entropy.bitstate.bits_left = 0;
1490:
1491: /* Advance past the RSTn marker */
1492: if (!read_restart_marker(cinfo))
1493: return false;
1494:
1495: /* Re-initialize DC predictions to 0 */
1496: for (ci = 0; ci < cinfo.comps_in_scan; ci++)
1497: entropy.saved.last_dc_val[ci] = 0;
1498: /* Re-init EOB run count, too */
1499: entropy.saved.EOBRUN = 0;
1500:
1501: /* Reset restart counter */
1502: entropy.restarts_to_go = cinfo.restart_interval;
1503:
1504: /* Reset out-of-data flag, unless read_restart_marker left us smack up
1505: * against a marker. In that case we will end up treating the next data
1506: * segment as empty, and we can avoid producing bogus output pixels by
1507: * leaving the flag set.
1508: */
1509: if (cinfo.unread_marker == 0)
1510: entropy.insufficient_data = false;
1511:
1512: return true;
1513: }
1514:
1515: void start_pass_phuff_decoder(jpeg_decompress_struct cinfo) {
1516: phuff_entropy_decoder entropy = this ;
1517: boolean is_DC_band, bad;
1518: int ci, coefi, tbl;
1519: int[] coef_bit_ptr;
1520: jpeg_component_info compptr;
1521:
1522: is_DC_band = (cinfo.Ss == 0);
1523:
1524: /* Validate scan parameters */
1525: bad = false;
1526: if (is_DC_band) {
1527: if (cinfo.Se != 0)
1528: bad = true;
1529: } else {
1530: /* need not check Ss/Se < 0 since they came from unsigned bytes */
1531: if (cinfo.Ss > cinfo.Se || cinfo.Se >= DCTSIZE2)
1532: bad = true;
1533: /* AC scans may have only one component */
1534: if (cinfo.comps_in_scan != 1)
1535: bad = true;
1536: }
1537: if (cinfo.Ah != 0) {
1538: /* Successive approximation refinement scan: must have Al = Ah-1. */
1539: if (cinfo.Al != cinfo.Ah - 1)
1540: bad = true;
1541: }
1542: if (cinfo.Al > 13) /* need not check for < 0 */
1543: bad = true;
1544: /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
1545: * but the spec doesn't say so, and we try to be liberal about what we
1546: * accept. Note: large Al values could result in out-of-range DC
1547: * coefficients during early scans, leading to bizarre displays due to
1548: * overflows in the IDCT math. But we won't crash.
1549: */
1550: if (bad)
1551: error();
1552: // ERREXIT4(cinfo, JERR_BAD_PROGRESSION, cinfo.Ss, cinfo.Se, cinfo.Ah, cinfo.Al);
1553: /* Update progression status, and verify that scan order is legal.
1554: * Note that inter-scan inconsistencies are treated as warnings
1555: * not fatal errors ... not clear if this is right way to behave.
1556: */
1557: for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
1558: int cindex = cinfo.cur_comp_info[ci].component_index;
1559: coef_bit_ptr = cinfo.coef_bits[cindex];
1560: if (!is_DC_band && coef_bit_ptr[0] < 0) {/* AC without prior DC scan */
1561: // WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
1562: }
1563: for (coefi = cinfo.Ss; coefi <= cinfo.Se; coefi++) {
1564: int expected = (coef_bit_ptr[coefi] < 0) ? 0
1565: : coef_bit_ptr[coefi];
1566: if (cinfo.Ah != expected) {
1567: // WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
1568: }
1569: coef_bit_ptr[coefi] = cinfo.Al;
1570: }
1571: }
1572:
1573: /* Select MCU decoding routine */
1574: // if (cinfo.Ah == 0) {
1575: // if (is_DC_band)
1576: // entropy.pub.decode_mcu = decode_mcu_DC_first;
1577: // else
1578: // entropy.pub.decode_mcu = decode_mcu_AC_first;
1579: // } else {
1580: // if (is_DC_band)
1581: // entropy.pub.decode_mcu = decode_mcu_DC_refine;
1582: // else
1583: // entropy.pub.decode_mcu = decode_mcu_AC_refine;
1584: // }
1585: for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
1586: compptr = cinfo.cur_comp_info[ci];
1587: /* Make sure requested tables are present, and compute derived tables.
1588: * We may build same derived table more than once, but it's not expensive.
1589: */
1590: if (is_DC_band) {
1591: if (cinfo.Ah == 0) { /* DC refinement needs no table */
1592: tbl = compptr.dc_tbl_no;
1593: jpeg_make_d_derived_tbl(
1594: cinfo,
1595: true,
1596: tbl,
1597: entropy.derived_tbls[tbl] = new d_derived_tbl());
1598: }
1599: } else {
1600: tbl = compptr.ac_tbl_no;
1601: jpeg_make_d_derived_tbl(
1602: cinfo,
1603: false,
1604: tbl,
1605: entropy.derived_tbls[tbl] = new d_derived_tbl());
1606: /* remember the single active table */
1607: entropy.ac_derived_tbl = entropy.derived_tbls[tbl];
1608: }
1609: /* Initialize DC predictions to 0 */
1610: entropy.saved.last_dc_val[ci] = 0;
1611: }
1612:
1613: /* Initialize bitread state variables */
1614: entropy.bitstate.bits_left = 0;
1615: entropy.bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
1616: entropy.insufficient_data = false;
1617:
1618: /* Initialize private state variables */
1619: entropy.saved.EOBRUN = 0;
1620:
1621: /* Initialize restart counter */
1622: entropy.restarts_to_go = cinfo.restart_interval;
1623: }
1624:
1625: }
1626:
1627: static final class jpeg_component_info {
1628: /* These values are fixed over the whole image. */
1629: /* For compression, they must be supplied by parameter setup; */
1630: /* for decompression, they are read from the SOF marker. */
1631: int component_id; /* identifier for this component (0..255) */
1632: int component_index; /* its index in SOF or cinfo.comp_info[] */
1633: int h_samp_factor; /* horizontal sampling factor (1..4) */
1634: int v_samp_factor; /* vertical sampling factor (1..4) */
1635: int quant_tbl_no; /* quantization table selector (0..3) */
1636: /* These values may vary between scans. */
1637: /* For compression, they must be supplied by parameter setup; */
1638: /* for decompression, they are read from the SOS marker. */
1639: /* The decompressor output side may not use these variables. */
1640: int dc_tbl_no; /* DC entropy table selector (0..3) */
1641: int ac_tbl_no; /* AC entropy table selector (0..3) */
1642:
1643: /* Remaining fields should be treated as private by applications. */
1644:
1645: /* These values are computed during compression or decompression startup: */
1646: /* Component's size in DCT blocks.
1647: * Any dummy blocks added to complete an MCU are not counted; therefore
1648: * these values do not depend on whether a scan is interleaved or not.
1649: */
1650: int width_in_blocks;
1651: int height_in_blocks;
1652: /* Size of a DCT block in samples. Always DCTSIZE for compression.
1653: * For decompression this is the size of the output from one DCT block,
1654: * reflecting any scaling we choose to apply during the IDCT step.
1655: * Values of 1,2,4,8 are likely to be supported. Note that different
1656: * components may receive different IDCT scalings.
1657: */
1658: int DCT_scaled_size;
1659: /* The downsampled dimensions are the component's actual, unpadded number
1660: * of samples at the main buffer (preprocessing/compression interface), thus
1661: * downsampled_width = ceil(image_width * Hi/Hmax)
1662: * and similarly for height. For decompression, IDCT scaling is included, so
1663: * downsampled_width = ceil(image_width * Hi/Hmax * DCT_scaled_size/DCTSIZE)
1664: */
1665: int downsampled_width; /* actual width in samples */
1666: int downsampled_height; /* actual height in samples */
1667: /* This flag is used only for decompression. In cases where some of the
1668: * components will be ignored (eg grayscale output from YCbCr image),
1669: * we can skip most computations for the unused components.
1670: */
1671: boolean component_needed; /* do we need the value of this component? */
1672:
1673: /* These values are computed before starting a scan of the component. */
1674: /* The decompressor output side may not use these variables. */
1675: int MCU_width; /* number of blocks per MCU, horizontally */
1676: int MCU_height; /* number of blocks per MCU, vertically */
1677: int MCU_blocks; /* MCU_width * MCU_height */
1678: int MCU_sample_width; /* MCU width in samples, MCU_width*DCT_scaled_size */
1679: int last_col_width; /* # of non-dummy blocks across in last MCU */
1680: int last_row_height; /* # of non-dummy blocks down in last MCU */
1681:
1682: /* Saved quantization table for component; null if none yet saved.
1683: * See jdinput.c comments about the need for this information.
1684: * This field is currently used only for decompression.
1685: */
1686: JQUANT_TBL quant_table;
1687:
1688: /* Private per-component storage for DCT or IDCT subsystem. */
1689: int[] dct_table;
1690: }
1691:
1692: static final class jpeg_color_quantizer {
1693: // JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
1694: // JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
1695: // JSAMPARRAY input_buf, JSAMPARRAY output_buf,
1696: // int num_rows));
1697: // JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
1698: // JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
1699:
1700: /* Initially allocated colormap is saved here */
1701: int[][] sv_colormap; /* The color map as a 2-D pixel array */
1702: int sv_actual; /* number of entries in use */
1703:
1704: int[][] colorindex; /* Precomputed mapping for speed */
1705: /* colorindex[i][j] = index of color closest to pixel value j in component i,
1706: * premultiplied as described above. Since colormap indexes must fit into
1707: * JSAMPLEs, the entries of this array will too.
1708: */
1709: boolean is_padded; /* is the colorindex padded for odither? */
1710:
1711: int[] Ncolors = new int[MAX_Q_COMPS]; /* # of values alloced to each component */
1712:
1713: /* Variables for ordered dithering */
1714: int row_index; /* cur row's vertical index in dither matrix */
1715: // ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */
1716:
1717: /* Variables for Floyd-Steinberg dithering */
1718: // FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
1719: boolean on_odd_row;
1720:
1721: void start_pass(jpeg_decompress_struct cinfo,
1722: boolean is_pre_scan) {
1723: error();
1724: }
1725: }
1726:
1727: static final class jpeg_upsampler {
1728: // JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
1729: // JMETHOD(void, upsample, (j_decompress_ptr cinfo,
1730: // JSAMPIMAGE input_buf,
1731: // JDIMENSION *in_row_group_ctr,
1732: // JDIMENSION in_row_groups_avail,
1733: // JSAMPARRAY output_buf,
1734: // JDIMENSION *out_row_ctr,
1735: // JDIMENSION out_rows_avail));
1736:
1737: boolean need_context_rows; /* TRUE if need rows above & below */
1738:
1739: /* Color conversion buffer. When using separate upsampling and color
1740: * conversion steps, this buffer holds one upsampled row group until it
1741: * has been color converted and output.
1742: * Note: we do not allocate any storage for component(s) which are full-size,
1743: * ie do not need rescaling. The corresponding entry of color_buf[] is
1744: * simply set to point to the input data array, thereby avoiding copying.
1745: */
1746: byte[][][] color_buf = new byte[MAX_COMPONENTS][][];
1747: int[] color_buf_offset = new int[MAX_COMPONENTS];
1748:
1749: /* Per-component upsampling method pointers */
1750: int[] methods = new int[MAX_COMPONENTS];
1751:
1752: int next_row_out; /* counts rows emitted from color_buf */
1753: int rows_to_go; /* counts rows remaining in image */
1754:
1755: /* Height of an input row group for each component. */
1756: int[] rowgroup_height = new int[MAX_COMPONENTS];
1757:
1758: /* These arrays save pixel expansion factors so that int_expand need not
1759: * recompute them each time. They are unused for other upsampling methods.
1760: */
1761: byte[] h_expand = new byte[MAX_COMPONENTS];
1762: byte[] v_expand = new byte[MAX_COMPONENTS];
1763:
1764: void start_pass(jpeg_decompress_struct cinfo) {
1765: jpeg_upsampler upsample = cinfo.upsample;
1766:
1767: /* Mark the conversion buffer empty */
1768: upsample.next_row_out = cinfo.max_v_samp_factor;
1769: /* Initialize total-height counter for detecting bottom of image */
1770: upsample.rows_to_go = cinfo.output_height;
1771: }
1772:
1773: }
1774:
1775: static final class jpeg_marker_reader {
1776: /* Read a restart marker --- exported for use by entropy decoder only */
1777: // jpeg_marker_parser_method read_restart_marker;
1778: /* State of marker reader --- nominally internal, but applications
1779: * supplying COM or APPn handlers might like to know the state.
1780: */
1781: boolean saw_SOI; /* found SOI? */
1782: boolean saw_SOF; /* found SOF? */
1783: int next_restart_num; /* next restart number expected (0-7) */
1784: int discarded_bytes; /* # of bytes skipped looking for a marker */
1785:
1786: /* Application-overridable marker processing methods */
1787: // jpeg_marker_parser_method process_COM;
1788: // jpeg_marker_parser_method process_APPn[16];
1789: /* Limit on marker data length to save for each marker type */
1790: int length_limit_COM;
1791: int[] length_limit_APPn = new int[16];
1792:
1793: /* Status of COM/APPn marker saving */
1794: // jpeg_marker_reader cur_marker; /* null if not processing a marker */
1795: // int bytes_read; /* data bytes read so far in marker */
1796: /* Note: cur_marker is not linked into marker_list until it's all read. */
1797: }
1798:
1799: static final class jpeg_d_main_controller {
1800: // JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
1801: int process_data;
1802:
1803: /* Pointer to allocated workspace (M or M+2 row groups). */
1804: byte[][][] buffer = new byte[MAX_COMPONENTS][][];
1805: int[] buffer_offset = new int[MAX_COMPONENTS];
1806:
1807: boolean buffer_full; /* Have we gotten an iMCU row from decoder? */
1808: int[] rowgroup_ctr = new int[1]; /* counts row groups output to postprocessor */
1809:
1810: /* Remaining fields are only used in the context case. */
1811:
1812: /* These are the master pointers to the funny-order pointer lists. */
1813: byte[][][][] xbuffer = new byte[2][][][]; /* pointers to weird pointer lists */
1814: int[][] xbuffer_offset = new int[2][];
1815:
1816: int whichptr; /* indicates which pointer set is now in use */
1817: int context_state; /* process_data state machine status */
1818: int rowgroups_avail; /* row groups available to postprocessor */
1819: int iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */
1820:
1821: void start_pass(jpeg_decompress_struct cinfo, int pass_mode) {
1822: jpeg_d_main_controller main = cinfo.main;
1823:
1824: switch (pass_mode) {
1825: case JBUF_PASS_THRU:
1826: if (cinfo.upsample.need_context_rows) {
1827: main.process_data = PROCESS_DATA_CONTEXT_MAIN;
1828: make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
1829: main.whichptr = 0; /* Read first iMCU row into xbuffer[0] */
1830: main.context_state = CTX_PREPARE_FOR_IMCU;
1831: main.iMCU_row_ctr = 0;
1832: } else {
1833: /* Simple case with no context needed */
1834: main.process_data = PROCESS_DATA_SIMPLE_MAIN;
1835: }
1836: main.buffer_full = false; /* Mark buffer empty */
1837: main.rowgroup_ctr[0] = 0;
1838: break;
1839: // #ifdef QUANT_2PASS_SUPPORTED
1840: // case JBUF_CRANK_DEST:
1841: // /* For last pass of 2-pass quantization, just crank the postprocessor */
1842: // main.process_data = PROCESS_DATA_CRANK_POST;
1843: // break;
1844: // #endif
1845: default:
1846: error();
1847: // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
1848: break;
1849: }
1850: }
1851:
1852: }
1853:
1854: static final class jpeg_decomp_master {
1855: // JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
1856: // JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
1857:
1858: /* State variables made visible to other modules */
1859: boolean is_dummy_pass;
1860:
1861: int pass_number; /* # of passes completed */
1862:
1863: boolean using_merged_upsample; /* true if using merged upsample/cconvert */
1864:
1865: /* Saved references to initialized quantizer modules,
1866: * in case we need to switch modes.
1867: */
1868: jpeg_color_quantizer quantizer_1pass;
1869: jpeg_color_quantizer quantizer_2pass;
1870: }
1871:
1872: static final class jpeg_inverse_dct {
1873: // JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
1874: // /* It is useful to allow each component to have a separate IDCT method. */
1875: // inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
1876: int[] cur_method = new int[MAX_COMPONENTS];
1877:
1878: void start_pass(jpeg_decompress_struct cinfo) {
1879: jpeg_inverse_dct idct = cinfo.idct;
1880: int ci, i;
1881: jpeg_component_info compptr;
1882: int method = 0;
1883: // inverse_DCT_method_ptr method_ptr = NULL;
1884: JQUANT_TBL qtbl;
1885:
1886: for (ci = 0; ci < cinfo.num_components; ci++) {
1887: compptr = cinfo.comp_info[ci];
1888: /* Select the proper IDCT routine for this component's scaling */
1889: switch (compptr.DCT_scaled_size) {
1890: // #ifdef IDCT_SCALING_SUPPORTED
1891: // case 1:
1892: // method_ptr = jpeg_idct_1x1;
1893: // method = JDCT_ISLOW; /* jidctred uses islow-style table */
1894: // break;
1895: // case 2:
1896: // method_ptr = jpeg_idct_2x2;
1897: // method = JDCT_ISLOW; /* jidctred uses islow-style table */
1898: // break;
1899: // case 4:
1900: // method_ptr = jpeg_idct_4x4;
1901: // method = JDCT_ISLOW; /* jidctred uses islow-style table */
1902: // break;
1903: // #endif
1904: case DCTSIZE:
1905: switch (cinfo.dct_method) {
1906: // #ifdef DCT_ISLOW_SUPPORTED
1907: case JDCT_ISLOW:
1908: // method_ptr = jpeg_idct_islow;
1909: method = JDCT_ISLOW;
1910: break;
1911: // #endif
1912: // #ifdef DCT_IFAST_SUPPORTED
1913: // case JDCT_IFAST:
1914: // method_ptr = jpeg_idct_ifast;
1915: // method = JDCT_IFAST;
1916: // break;
1917: // #endif
1918: // #ifdef DCT_FLOAT_SUPPORTED
1919: // case JDCT_FLOAT:
1920: // method_ptr = jpeg_idct_float;
1921: // method = JDCT_FLOAT;
1922: // break;
1923: // #endif
1924: default:
1925: error();
1926: // ERREXIT(cinfo, JERR_NOT_COMPILED);
1927: break;
1928: }
1929: break;
1930: default:
1931: error();
1932: // ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr.DCT_scaled_size);
1933: break;
1934: }
1935: // idct.inverse_DCT[ci] = method_ptr;
1936: /* Create multiplier table from quant table.
1937: * However, we can skip this if the component is uninteresting
1938: * or if we already built the table. Also, if no quant table
1939: * has yet been saved for the component, we leave the
1940: * multiplier table all-zero; we'll be reading zeroes from the
1941: * coefficient controller's buffer anyway.
1942: */
1943: if (!compptr.component_needed
1944: || idct.cur_method[ci] == method)
1945: continue;
1946: qtbl = compptr.quant_table;
1947: if (qtbl == null) /* happens if no data yet for component */
1948: continue;
1949: idct.cur_method[ci] = method;
1950: switch (method) {
1951: // #ifdef PROVIDE_ISLOW_TABLES
1952: case JDCT_ISLOW: {
1953: /* For LL&M IDCT method, multipliers are equal to raw quantization
1954: * coefficients, but are stored as ints to ensure access efficiency.
1955: */
1956: int[] ismtbl = compptr.dct_table;
1957: for (i = 0; i < DCTSIZE2; i++) {
1958: ismtbl[i] = qtbl.quantval[i];
1959: }
1960: }
1961: break;
1962: // #endif
1963: // #ifdef DCT_IFAST_SUPPORTED
1964: // case JDCT_IFAST:
1965: // {
1966: // /* For AA&N IDCT method, multipliers are equal to quantization
1967: // * coefficients scaled by scalefactor[row]*scalefactor[col], where
1968: // * scalefactor[0] = 1
1969: // * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
1970: // * For integer operation, the multiplier table is to be scaled by
1971: // * IFAST_SCALE_BITS.
1972: // */
1973: // int[] ifmtbl = compptr.dct_table;
1974: // short aanscales[] = {
1975: // /* precomputed values scaled up by 14 bits */
1976: // 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
1977: // 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
1978: // 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
1979: // 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
1980: // 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
1981: // 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
1982: // 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
1983: // 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
1984: // };
1985: // SHIFT_TEMPS
1986: //
1987: // for (i = 0; i < DCTSIZE2; i++) {
1988: // ifmtbl[i] = DESCALE(MULTIPLY16V16( qtbl.quantval[i], aanscales[i]), CONST_BITS-IFAST_SCALE_BITS);
1989: // }
1990: // }
1991: // break;
1992: // #endif
1993: // #ifdef DCT_FLOAT_SUPPORTED
1994: // case JDCT_FLOAT:
1995: // {
1996: // /* For float AA&N IDCT method, multipliers are equal to quantization
1997: // * coefficients scaled by scalefactor[row]*scalefactor[col], where
1998: // * scalefactor[0] = 1
1999: // * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
2000: // */
2001: // FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr.dct_table;
2002: // int row, col;
2003: // static const double aanscalefactor[DCTSIZE] = {
2004: // 1.0, 1.387039845, 1.306562965, 1.175875602,
2005: // 1.0, 0.785694958, 0.541196100, 0.275899379
2006: // };
2007: //
2008: // i = 0;
2009: // for (row = 0; row < DCTSIZE; row++) {
2010: // for (col = 0; col < DCTSIZE; col++) {
2011: // fmtbl[i] = (FLOAT_MULT_TYPE)
2012: // ((double) qtbl.quantval[i] *
2013: // aanscalefactor[row] * aanscalefactor[col]);
2014: // i++;
2015: // }
2016: // }
2017: // }
2018: // break;
2019: // #endif
2020: default:
2021: error();
2022: // ERREXIT(cinfo, JERR_NOT_COMPILED);
2023: break;
2024: }
2025: }
2026: }
2027: }
2028:
2029: static final class jpeg_input_controller {
2030: int consume_input;
2031: boolean has_multiple_scans; /* True if file has multiple scans */
2032: boolean eoi_reached;
2033:
2034: boolean inheaders; /* true until first SOS is reached */
2035: }
2036:
2037: static final class jpeg_color_deconverter {
2038: // JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
2039: int color_convert;
2040:
2041: /* Private state for YCC.RGB conversion */
2042: int[] Cr_r_tab; /* => table for Cr to R conversion */
2043: int[] Cb_b_tab; /* => table for Cb to B conversion */
2044: int[] Cr_g_tab; /* => table for Cr to G conversion */
2045: int[] Cb_g_tab; /* => table for Cb to G conversion */
2046:
2047: void start_pass(jpeg_decompress_struct cinfo) {
2048: /* no work needed */
2049: }
2050:
2051: }
2052:
2053: static final class jpeg_d_post_controller {
2054: // JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
2055: int post_process_data;
2056:
2057: /* Color quantization source buffer: this holds output data from
2058: * the upsample/color conversion step to be passed to the quantizer.
2059: * For two-pass color quantization, we need a full-image buffer;
2060: * for one-pass operation, a strip buffer is sufficient.
2061: */
2062: int[] whole_image; /* virtual array, or NULL if one-pass */
2063: int[][] buffer; /* strip buffer, or current strip of virtual */
2064: int strip_height; /* buffer size in rows */
2065: /* for two-pass mode only: */
2066: int starting_row; /* row # of first row in current strip */
2067: int next_row; /* index of next row to fill/empty in strip */
2068:
2069: void start_pass(jpeg_decompress_struct cinfo, int pass_mode) {
2070: jpeg_d_post_controller post = cinfo.post;
2071:
2072: switch (pass_mode) {
2073: case JBUF_PASS_THRU:
2074: if (cinfo.quantize_colors) {
2075: error(SWT.ERROR_NOT_IMPLEMENTED);
2076: // /* Single-pass processing with color quantization. */
2077: // post.post_process_data = POST_PROCESS_1PASS;
2078: // /* We could be doing buffered-image output before starting a 2-pass
2079: // * color quantization; in that case, jinit_d_post_controller did not
2080: // * allocate a strip buffer. Use the virtual-array buffer as workspace.
2081: // */
2082: // if (post.buffer == null) {
2083: // post.buffer = (*cinfo.mem.access_virt_sarray)
2084: // ((j_common_ptr) cinfo, post.whole_image,
2085: // (JDIMENSION) 0, post.strip_height, TRUE);
2086: // }
2087: } else {
2088: /* For single-pass processing without color quantization,
2089: * I have no work to do; just call the upsampler directly.
2090: */
2091: post.post_process_data = POST_PROCESS_DATA_UPSAMPLE;
2092: }
2093: break;
2094: // #ifdef QUANT_2PASS_SUPPORTED
2095: // case JBUF_SAVE_AND_PASS:
2096: // /* First pass of 2-pass quantization */
2097: // if (post.whole_image == NULL)
2098: // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
2099: // post.pub.post_process_data = post_process_prepass;
2100: // break;
2101: // case JBUF_CRANK_DEST:
2102: // /* Second pass of 2-pass quantization */
2103: // if (post.whole_image == NULL)
2104: // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
2105: // post.pub.post_process_data = post_process_2pass;
2106: // break;
2107: // #endif /* QUANT_2PASS_SUPPORTED */
2108: default:
2109: error();
2110: // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
2111: break;
2112: }
2113: post.starting_row = post.next_row = 0;
2114: }
2115:
2116: }
2117:
2118: static final class jpeg_decompress_struct {
2119: // jpeg_error_mgr * err; /* Error handler module */\
2120: // struct jpeg_memory_mgr * mem; /* Memory manager module */\
2121: // struct jpeg_progress_mgr * progress; /* Progress monitor, or null if none */\
2122: // void * client_data; /* Available for use by application */\
2123: boolean is_decompressor; /* So common code can tell which is which */
2124: int global_state; /* For checking call sequence validity */
2125:
2126: // /* Source of compressed data */
2127: // struct jpeg_source_mgr * src;
2128: InputStream inputStream;
2129: byte[] buffer;
2130: int bytes_in_buffer;
2131: int bytes_offset;
2132: boolean start_of_file;
2133:
2134: /* Basic description of image --- filled in by jpeg_read_header(). */
2135: /* Application may inspect these values to decide how to process image. */
2136:
2137: int image_width; /* nominal image width (from SOF marker) */
2138: int image_height; /* nominal image height */
2139: int num_components; /* # of color components in JPEG image */
2140: int jpeg_color_space; /* colorspace of JPEG image */
2141:
2142: /* Decompression processing parameters --- these fields must be set before
2143: * calling jpeg_start_decompress(). Note that jpeg_read_header() initializes
2144: * them to default values.
2145: */
2146:
2147: int out_color_space; /* colorspace for output */
2148:
2149: int scale_num, scale_denom; /* fraction by which to scale image */
2150:
2151: double output_gamma; /* image gamma wanted in output */
2152:
2153: boolean buffered_image; /* true=multiple output passes */
2154: boolean raw_data_out; /* true=downsampled data wanted */
2155:
2156: int dct_method; /* IDCT algorithm selector */
2157: boolean do_fancy_upsampling; /* true=apply fancy upsampling */
2158: boolean do_block_smoothing; /* true=apply interblock smoothing */
2159:
2160: boolean quantize_colors; /* true=colormapped output wanted */
2161: /* the following are ignored if not quantize_colors: */
2162: int dither_mode; /* type of color dithering to use */
2163: boolean two_pass_quantize; /* true=use two-pass color quantization */
2164: int desired_number_of_colors; /* max # colors to use in created colormap */
2165: /* these are significant only in buffered-image mode: */
2166: boolean enable_1pass_quant; /* enable future use of 1-pass quantizer */
2167: boolean enable_external_quant;/* enable future use of external colormap */
2168: boolean enable_2pass_quant; /* enable future use of 2-pass quantizer */
2169:
2170: /* Description of actual output image that will be returned to application.
2171: * These fields are computed by jpeg_start_decompress().
2172: * You can also use jpeg_calc_output_dimensions() to determine these values
2173: * in advance of calling jpeg_start_decompress().
2174: */
2175:
2176: int output_width; /* scaled image width */
2177: int output_height; /* scaled image height */
2178: int out_color_components; /* # of color components in out_color_space */
2179: int output_components; /* # of color components returned */
2180: /* output_components is 1 (a colormap index) when quantizing colors;
2181: * otherwise it equals out_color_components.
2182: */
2183: int rec_outbuf_height; /* min recommended height of scanline buffer */
2184: /* If the buffer passed to jpeg_read_scanlines() is less than this many rows
2185: * high, space and time will be wasted due to unnecessary data copying.
2186: * Usually rec_outbuf_height will be 1 or 2, at most 4.
2187: */
2188:
2189: /* When quantizing colors, the output colormap is described by these fields.
2190: * The application can supply a colormap by setting colormap non-null before
2191: * calling jpeg_start_decompress; otherwise a colormap is created during
2192: * jpeg_start_decompress or jpeg_start_output.
2193: * The map has out_color_components rows and actual_number_of_colors columns.
2194: */
2195: int actual_number_of_colors; /* number of entries in use */
2196: int[] colormap; /* The color map as a 2-D pixel array */
2197:
2198: /* State variables: these variables indicate the progress of decompression.
2199: * The application may examine these but must not modify them.
2200: */
2201:
2202: /* Row index of next scanline to be read from jpeg_read_scanlines().
2203: * Application may use this to control its processing loop, e.g.,
2204: * "while (output_scanline < output_height)".
2205: */
2206: int output_scanline; /* 0 .. output_height-1 */
2207:
2208: /* Current input scan number and number of iMCU rows completed in scan.
2209: * These indicate the progress of the decompressor input side.
2210: */
2211: int input_scan_number; /* Number of SOS markers seen so far */
2212: int input_iMCU_row; /* Number of iMCU rows completed */
2213:
2214: /* The "output scan number" is the notional scan being displayed by the
2215: * output side. The decompressor will not allow output scan/row number
2216: * to get ahead of input scan/row, but it can fall arbitrarily far behind.
2217: */
2218: int output_scan_number; /* Nominal scan number being displayed */
2219: int output_iMCU_row; /* Number of iMCU rows read */
2220:
2221: /* Current progression status. coef_bits[c][i] indicates the precision
2222: * with which component c's DCT coefficient i (in zigzag order) is known.
2223: * It is -1 when no data has yet been received, otherwise it is the point
2224: * transform (shift) value for the most recent scan of the coefficient
2225: * (thus, 0 at completion of the progression).
2226: * This pointer is null when reading a non-progressive file.
2227: */
2228: int[][] coef_bits; /* -1 or current Al value for each coef */
2229:
2230: /* Internal JPEG parameters --- the application usually need not look at
2231: * these fields. Note that the decompressor output side may not use
2232: * any parameters that can change between scans.
2233: */
2234:
2235: /* Quantization and Huffman tables are carried forward across input
2236: * datastreams when processing abbreviated JPEG datastreams.
2237: */
2238:
2239: JQUANT_TBL[] quant_tbl_ptrs = new JQUANT_TBL[NUM_QUANT_TBLS];
2240: /* ptrs to coefficient quantization tables, or null if not defined */
2241:
2242: JHUFF_TBL[] dc_huff_tbl_ptrs = new JHUFF_TBL[NUM_HUFF_TBLS];
2243: JHUFF_TBL[] ac_huff_tbl_ptrs = new JHUFF_TBL[NUM_HUFF_TBLS];
2244: /* ptrs to Huffman coding tables, or null if not defined */
2245:
2246: /* These parameters are never carried across datastreams, since they
2247: * are given in SOF/SOS markers or defined to be reset by SOI.
2248: */
2249:
2250: int data_precision; /* bits of precision in image data */
2251:
2252: jpeg_component_info[] comp_info;
2253: /* comp_info[i] describes component that appears i'th in SOF */
2254:
2255: boolean progressive_mode; /* true if SOFn specifies progressive mode */
2256: boolean arith_code; /* true=arithmetic coding, false=Huffman */
2257:
2258: byte[] arith_dc_L = new byte[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
2259: byte[] arith_dc_U = new byte[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
2260: byte[] arith_ac_K = new byte[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
2261:
2262: int restart_interval; /* MCUs per restart interval, or 0 for no restart */
2263:
2264: /* These fields record data obtained from optional markers recognized by
2265: * the JPEG library.
2266: */
2267: boolean saw_JFIF_marker; /* true iff a JFIF APP0 marker was found */
2268: /* Data copied from JFIF marker; only valid if saw_JFIF_marker is true: */
2269: byte JFIF_major_version; /* JFIF version number */
2270: byte JFIF_minor_version;
2271: byte density_unit; /* JFIF code for pixel size units */
2272: short X_density; /* Horizontal pixel density */
2273: short Y_density; /* Vertical pixel density */
2274: boolean saw_Adobe_marker; /* true iff an Adobe APP14 marker was found */
2275: byte Adobe_transform; /* Color transform code from Adobe marker */
2276:
2277: boolean CCIR601_sampling; /* true=first samples are cosited */
2278:
2279: /* Aside from the specific data retained from APPn markers known to the
2280: * library, the uninterpreted contents of any or all APPn and COM markers
2281: * can be saved in a list for examination by the application.
2282: */
2283: jpeg_marker_reader marker_list; /* Head of list of saved markers */
2284:
2285: /* Remaining fields are known throughout decompressor, but generally
2286: * should not be touched by a surrounding application.
2287: */
2288:
2289: /*
2290: * These fields are computed during decompression startup
2291: */
2292: int max_h_samp_factor; /* largest h_samp_factor */
2293: int max_v_samp_factor; /* largest v_samp_factor */
2294:
2295: int min_DCT_scaled_size; /* smallest DCT_scaled_size of any component */
2296:
2297: int total_iMCU_rows; /* # of iMCU rows in image */
2298: /* The coefficient controller's input and output progress is measured in
2299: * units of "iMCU" (interleaved MCU) rows. These are the same as MCU rows
2300: * in fully interleaved JPEG scans, but are used whether the scan is
2301: * interleaved or not. We define an iMCU row as v_samp_factor DCT block
2302: * rows of each component. Therefore, the IDCT output contains
2303: * v_samp_factor*DCT_scaled_size sample rows of a component per iMCU row.
2304: */
2305:
2306: byte[] sample_range_limit; /* table for fast range-limiting */
2307: int sample_range_limit_offset;
2308:
2309: /*
2310: * These fields are valid during any one scan.
2311: * They describe the components and MCUs actually appearing in the scan.
2312: * Note that the decompressor output side must not use these fields.
2313: */
2314: int comps_in_scan; /* # of JPEG components in this scan */
2315: jpeg_component_info[] cur_comp_info = new jpeg_component_info[MAX_COMPS_IN_SCAN];
2316: /* *cur_comp_info[i] describes component that appears i'th in SOS */
2317:
2318: int MCUs_per_row; /* # of MCUs across the image */
2319: int MCU_rows_in_scan; /* # of MCU rows in the image */
2320:
2321: int blocks_in_MCU; /* # of DCT blocks per MCU */
2322: int[] MCU_membership = new int[D_MAX_BLOCKS_IN_MCU];
2323: /* MCU_membership[i] is index in cur_comp_info of component owning */
2324: /* i'th block in an MCU */
2325:
2326: int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */
2327:
2328: /* This field is shared between entropy decoder and marker parser.
2329: * It is either zero or the code of a JPEG marker that has been
2330: * read from the data source, but has not yet been processed.
2331: */
2332: int unread_marker;
2333:
2334: int[] workspace = new int[DCTSIZE2];
2335: int[] row_ctr = new int[1];
2336:
2337: /*
2338: * Links to decompression subobjects (methods, private variables of modules)
2339: */
2340: jpeg_decomp_master master;
2341: jpeg_d_main_controller main;
2342: jpeg_d_coef_controller coef;
2343: jpeg_d_post_controller post;
2344: jpeg_input_controller inputctl;
2345: jpeg_marker_reader marker;
2346: jpeg_entropy_decoder entropy;
2347: jpeg_inverse_dct idct;
2348: jpeg_upsampler upsample;
2349: jpeg_color_deconverter cconvert;
2350: jpeg_color_quantizer cquantize;
2351: }
2352:
2353: static void error() {
2354: SWT.error(SWT.ERROR_INVALID_IMAGE);
2355: }
2356:
2357: static void error(int code) {
2358: SWT.error(code);
2359: }
2360:
2361: static void error(String msg) {
2362: SWT.error(SWT.ERROR_INVALID_IMAGE, null, msg);
2363: }
2364:
2365: static void jinit_marker_reader(jpeg_decompress_struct cinfo) {
2366: jpeg_marker_reader marker = cinfo.marker = new jpeg_marker_reader();
2367: // int i;
2368:
2369: /* Initialize COM/APPn processing.
2370: * By default, we examine and then discard APP0 and APP14,
2371: * but simply discard COM and all other APPn.
2372: */
2373: // marker.process_COM = skip_variable;
2374: marker.length_limit_COM = 0;
2375: // for (i = 0; i < 16; i++) {
2376: // marker.process_APPn[i] = skip_variable;
2377: // marker.length_limit_APPn[i] = 0;
2378: // }
2379: // marker.process_APPn[0] = get_interesting_appn;
2380: // marker.process_APPn[14] = get_interesting_appn;
2381: /* Reset marker processing state */
2382: reset_marker_reader(cinfo);
2383: }
2384:
2385: static void jinit_d_coef_controller(jpeg_decompress_struct cinfo,
2386: boolean need_full_buffer) {
2387: jpeg_d_coef_controller coef = new jpeg_d_coef_controller();
2388: cinfo.coef = coef;
2389: // coef.pub.start_input_pass = start_input_pass;
2390: // coef.pub.start_output_pass = start_output_pass;
2391: coef.coef_bits_latch = null;
2392:
2393: /* Create the coefficient buffer. */
2394: if (need_full_buffer) {
2395: //#ifdef D_MULTISCAN_FILES_SUPPORTED
2396: /* Allocate a full-image virtual array for each component, */
2397: /* padded to a multiple of samp_factor DCT blocks in each direction. */
2398: /* Note we ask for a pre-zeroed array. */
2399: int ci, access_rows;
2400: jpeg_component_info compptr;
2401:
2402: for (ci = 0; ci < cinfo.num_components; ci++) {
2403: compptr = cinfo.comp_info[ci];
2404: access_rows = compptr.v_samp_factor;
2405: //#ifdef BLOCK_SMOOTHING_SUPPORTED
2406: /* If block smoothing could be used, need a bigger window */
2407: if (cinfo.progressive_mode)
2408: access_rows *= 3;
2409: //#endif
2410: coef.whole_image[ci] = new short[(int) jround_up(
2411: compptr.height_in_blocks, compptr.v_samp_factor)][(int) jround_up(
2412: compptr.width_in_blocks, compptr.h_samp_factor)][DCTSIZE2];
2413: }
2414: // coef.consume_data = consume_data;
2415: coef.decompress_data = DECOMPRESS_DATA;
2416: coef.coef_arrays = coef.whole_image[0]; /* link to virtual arrays */
2417: // #else
2418: // ERREXIT(cinfo, JERR_NOT_COMPILED);
2419: // #endif
2420: } else {
2421: /* We only need a single-MCU buffer. */
2422: coef.MCU_buffer = new short[D_MAX_BLOCKS_IN_MCU][DCTSIZE2];
2423: // coef.consume_data = dummy_consume_data;
2424: coef.decompress_data = DECOMPRESS_ONEPASS;
2425: coef.coef_arrays = null; /* flag for no virtual arrays */
2426: }
2427: }
2428:
2429: static void start_output_pass(jpeg_decompress_struct cinfo) {
2430: //#ifdef BLOCK_SMOOTHING_SUPPORTED
2431: jpeg_d_coef_controller coef = cinfo.coef;
2432:
2433: /* If multipass, check to see whether to use block smoothing on this pass */
2434: if (coef.coef_arrays != null) {
2435: if (cinfo.do_block_smoothing && smoothing_ok(cinfo))
2436: coef.decompress_data = DECOMPRESS_SMOOTH_DATA;
2437: else
2438: coef.decompress_data = DECOMPRESS_DATA;
2439: }
2440: //#endif
2441: cinfo.output_iMCU_row = 0;
2442: }
2443:
2444: static void jpeg_create_decompress(jpeg_decompress_struct cinfo) {
2445: cinfo.is_decompressor = true;
2446:
2447: /* Initialize marker processor so application can override methods
2448: * for COM, APPn markers before calling jpeg_read_header.
2449: */
2450: cinfo.marker_list = null;
2451: jinit_marker_reader(cinfo);
2452:
2453: /* And initialize the overall input controller. */
2454: jinit_input_controller(cinfo);
2455:
2456: /* OK, I'm ready */
2457: cinfo.global_state = DSTATE_START;
2458: }
2459:
2460: static void jpeg_calc_output_dimensions(jpeg_decompress_struct cinfo)
2461: /* Do computations that are needed before master selection phase */
2462: {
2463: //#ifdef IDCT_SCALING_SUPPORTED
2464: // int ci;
2465: // jpeg_component_info compptr;
2466: //#endif
2467:
2468: /* Prevent application from calling me at wrong times */
2469: if (cinfo.global_state != DSTATE_READY)
2470: error();
2471: // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
2472:
2473: //#ifdef IDCT_SCALING_SUPPORTED
2474: //
2475: // /* Compute actual output image dimensions and DCT scaling choices. */
2476: // if (cinfo.scale_num * 8 <= cinfo.scale_denom) {
2477: // /* Provide 1/8 scaling */
2478: // cinfo.output_width = (int)
2479: // jdiv_round_up(cinfo.image_width, 8L);
2480: // cinfo.output_height = (int)
2481: // jdiv_round_up(cinfo.image_height, 8L);
2482: // cinfo.min_DCT_scaled_size = 1;
2483: // } else if (cinfo.scale_num * 4 <= cinfo.scale_denom) {
2484: // /* Provide 1/4 scaling */
2485: // cinfo.output_width = (int)
2486: // jdiv_round_up(cinfo.image_width, 4L);
2487: // cinfo.output_height = (int)
2488: // jdiv_round_up(cinfo.image_height, 4L);
2489: // cinfo.min_DCT_scaled_size = 2;
2490: // } else if (cinfo.scale_num * 2 <= cinfo.scale_denom) {
2491: // /* Provide 1/2 scaling */
2492: // cinfo.output_width = (int)
2493: // jdiv_round_up(cinfo.image_width, 2L);
2494: // cinfo.output_height = (int)
2495: // jdiv_round_up(cinfo.image_height, 2L);
2496: // cinfo.min_DCT_scaled_size = 4;
2497: // } else {
2498: // /* Provide 1/1 scaling */
2499: // cinfo.output_width = cinfo.image_width;
2500: // cinfo.output_height = cinfo.image_height;
2501: // cinfo.min_DCT_scaled_size = DCTSIZE;
2502: // }
2503: // /* In selecting the actual DCT scaling for each component, we try to
2504: // * scale up the chroma components via IDCT scaling rather than upsampling.
2505: // * This saves time if the upsampler gets to use 1:1 scaling.
2506: // * Note this code assumes that the supported DCT scalings are powers of 2.
2507: // */
2508: // for (ci = 0; ci < cinfo.num_components; ci++) {
2509: // compptr = cinfo.comp_info[ci];
2510: // int ssize = cinfo.min_DCT_scaled_size;
2511: // while (ssize < DCTSIZE &&
2512: // (compptr.h_samp_factor * ssize * 2 <= cinfo.max_h_samp_factor * cinfo.min_DCT_scaled_size) &&
2513: // (compptr.v_samp_factor * ssize * 2 <= cinfo.max_v_samp_factor * cinfo.min_DCT_scaled_size))
2514: // {
2515: // ssize = ssize * 2;
2516: // }
2517: // compptr.DCT_scaled_size = ssize;
2518: // }
2519: //
2520: // /* Recompute downsampled dimensions of components;
2521: // * application needs to know these if using raw downsampled data.
2522: // */
2523: // for (ci = 0; ci < cinfo.num_components; ci++) {
2524: // compptr = cinfo.comp_info[ci];
2525: // /* Size in samples, after IDCT scaling */
2526: // compptr.downsampled_width = (int)
2527: // jdiv_round_up((long) cinfo.image_width * (long) (compptr.h_samp_factor * compptr.DCT_scaled_size),
2528: // (cinfo.max_h_samp_factor * DCTSIZE));
2529: // compptr.downsampled_height = (int)
2530: // jdiv_round_up((long) cinfo.image_height * (long) (compptr.v_samp_factor * compptr.DCT_scaled_size),
2531: // (cinfo.max_v_samp_factor * DCTSIZE));
2532: // }
2533: //
2534: //#else /* !IDCT_SCALING_SUPPORTED */
2535:
2536: /* Hardwire it to "no scaling" */
2537: cinfo.output_width = cinfo.image_width;
2538: cinfo.output_height = cinfo.image_height;
2539: /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
2540: * and has computed unscaled downsampled_width and downsampled_height.
2541: */
2542:
2543: //#endif /* IDCT_SCALING_SUPPORTED */
2544: /* Report number of components in selected colorspace. */
2545: /* Probably this should be in the color conversion module... */
2546: switch (cinfo.out_color_space) {
2547: case JCS_GRAYSCALE:
2548: cinfo.out_color_components = 1;
2549: break;
2550: case JCS_RGB:
2551: if (RGB_PIXELSIZE != 3) {
2552: cinfo.out_color_components = RGB_PIXELSIZE;
2553: break;
2554: }
2555: //FALLTHROUGH
2556: case JCS_YCbCr:
2557: cinfo.out_color_components = 3;
2558: break;
2559: case JCS_CMYK:
2560: case JCS_YCCK:
2561: cinfo.out_color_components = 4;
2562: break;
2563: default: /* else must be same colorspace as in file */
2564: cinfo.out_color_components = cinfo.num_components;
2565: break;
2566: }
2567: cinfo.output_components = (cinfo.quantize_colors ? 1
2568: : cinfo.out_color_components);
2569:
2570: /* See if upsampler will want to emit more than one row at a time */
2571: if (use_merged_upsample(cinfo))
2572: cinfo.rec_outbuf_height = cinfo.max_v_samp_factor;
2573: else
2574: cinfo.rec_outbuf_height = 1;
2575: }
2576:
2577: static boolean use_merged_upsample(jpeg_decompress_struct cinfo) {
2578: //#ifdef UPSAMPLE_MERGING_SUPPORTED
2579: /* Merging is the equivalent of plain box-filter upsampling */
2580: if (cinfo.do_fancy_upsampling || cinfo.CCIR601_sampling)
2581: return false;
2582: /* jdmerge.c only supports YCC=>RGB color conversion */
2583: if (cinfo.jpeg_color_space != JCS_YCbCr
2584: || cinfo.num_components != 3
2585: || cinfo.out_color_space != JCS_RGB
2586: || cinfo.out_color_components != RGB_PIXELSIZE)
2587: return false;
2588: /* and it only handles 2h1v or 2h2v sampling ratios */
2589: if (cinfo.comp_info[0].h_samp_factor != 2
2590: || cinfo.comp_info[1].h_samp_factor != 1
2591: || cinfo.comp_info[2].h_samp_factor != 1
2592: || cinfo.comp_info[0].v_samp_factor > 2
2593: || cinfo.comp_info[1].v_samp_factor != 1
2594: || cinfo.comp_info[2].v_samp_factor != 1)
2595: return false;
2596: /* furthermore, it doesn't work if we've scaled the IDCTs differently */
2597: if (cinfo.comp_info[0].DCT_scaled_size != cinfo.min_DCT_scaled_size
2598: || cinfo.comp_info[1].DCT_scaled_size != cinfo.min_DCT_scaled_size
2599: || cinfo.comp_info[2].DCT_scaled_size != cinfo.min_DCT_scaled_size)
2600: return false;
2601: /* ??? also need to test for upsample-time rescaling, when & if supported */
2602: return true; /* by golly, it'll work... */
2603: //#else
2604: // return false;
2605: //#endif
2606: }
2607:
2608: static void prepare_range_limit_table(jpeg_decompress_struct cinfo)
2609: /* Allocate and fill in the sample_range_limit table */
2610: {
2611: byte[] table;
2612: int i;
2613:
2614: table = new byte[5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE];
2615: int offset = (MAXJSAMPLE + 1); /* allow negative subscripts of simple table */
2616: cinfo.sample_range_limit_offset = offset;
2617: cinfo.sample_range_limit = table;
2618: /* First segment of "simple" table: limit[x] = 0 for x < 0 */
2619: /* Main part of "simple" table: limit[x] = x */
2620: for (i = 0; i <= MAXJSAMPLE; i++)
2621: table[i + offset] = (byte) i;
2622: offset += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
2623: /* End of simple table, rest of first half of post-IDCT table */
2624: for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++)
2625: table[i + offset] = (byte) MAXJSAMPLE;
2626: /* Second half of post-IDCT table */
2627: System.arraycopy(cinfo.sample_range_limit,
2628: cinfo.sample_range_limit_offset, table, offset
2629: + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),
2630: CENTERJSAMPLE);
2631: }
2632:
2633: static void build_ycc_rgb_table(jpeg_decompress_struct cinfo) {
2634: jpeg_color_deconverter cconvert = cinfo.cconvert;
2635: int i;
2636: int x;
2637: // SHIFT_TEMPS
2638:
2639: cconvert.Cr_r_tab = new int[MAXJSAMPLE + 1];
2640: cconvert.Cb_b_tab = new int[MAXJSAMPLE + 1];
2641: cconvert.Cr_g_tab = new int[MAXJSAMPLE + 1];
2642: cconvert.Cb_g_tab = new int[MAXJSAMPLE + 1];
2643:
2644: for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
2645: /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
2646: /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
2647: /* Cr=>R value is nearest int to 1.40200 * x */
2648: cconvert.Cr_r_tab[i] = ((int) (1.40200f * (1 << SCALEBITS) + 0.5f)
2649: * x + ONE_HALF) >> SCALEBITS;
2650: /* Cb=>B value is nearest int to 1.77200 * x */
2651: cconvert.Cb_b_tab[i] = ((int) (1.77200f * (1 << SCALEBITS) + 0.5f)
2652: * x + ONE_HALF) >> SCALEBITS;
2653: /* Cr=>G value is scaled-up -0.71414 * x */
2654: cconvert.Cr_g_tab[i] = ((int) (-(0.71414f * (1 << SCALEBITS) + 0.5f)) * x);
2655: /* Cb=>G value is scaled-up -0.34414 * x */
2656: /* We also add in ONE_HALF so that need not do it in inner loop */
2657: cconvert.Cb_g_tab[i] = ((int) (-(0.34414f * (1 << SCALEBITS) + 0.5f))
2658: * x + ONE_HALF);
2659: }
2660: }
2661:
2662: static void jinit_color_deconverter(jpeg_decompress_struct cinfo) {
2663: jpeg_color_deconverter cconvert = cinfo.cconvert = new jpeg_color_deconverter();
2664: // cconvert.start_pass = start_pass_dcolor;
2665:
2666: /* Make sure num_components agrees with jpeg_color_space */
2667: switch (cinfo.jpeg_color_space) {
2668: case JCS_GRAYSCALE:
2669: if (cinfo.num_components != 1)
2670: error();
2671: // ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
2672: break;
2673:
2674: case JCS_RGB:
2675: case JCS_YCbCr:
2676: if (cinfo.num_components != 3)
2677: error();
2678: // ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
2679: break;
2680:
2681: case JCS_CMYK:
2682: case JCS_YCCK:
2683: if (cinfo.num_components != 4)
2684: error();
2685: // ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
2686: break;
2687:
2688: default: /* JCS_UNKNOWN can be anything */
2689: if (cinfo.num_components < 1)
2690: error();
2691: // ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
2692: break;
2693: }
2694:
2695: /* Set out_color_components and conversion method based on requested space.
2696: * Also clear the component_needed flags for any unused components,
2697: * so that earlier pipeline stages can avoid useless computation.
2698: */
2699:
2700: int ci;
2701: switch (cinfo.out_color_space) {
2702: case JCS_GRAYSCALE:
2703: cinfo.out_color_components = 1;
2704: if (cinfo.jpeg_color_space == JCS_GRAYSCALE
2705: || cinfo.jpeg_color_space == JCS_YCbCr) {
2706: cconvert.color_convert = GRAYSCALE_CONVERT;
2707: /* For color.grayscale conversion, only the Y (0) component is needed */
2708: for (ci = 1; ci < cinfo.num_components; ci++)
2709: cinfo.comp_info[ci].component_needed = false;
2710: } else
2711: error();
2712: // ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
2713: break;
2714:
2715: case JCS_RGB:
2716: cinfo.out_color_components = RGB_PIXELSIZE;
2717: if (cinfo.jpeg_color_space == JCS_YCbCr) {
2718: cconvert.color_convert = YCC_RGB_CONVERT;
2719: build_ycc_rgb_table(cinfo);
2720: } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
2721: cconvert.color_convert = GRAY_RGB_CONVERT;
2722: } else if (cinfo.jpeg_color_space == JCS_RGB
2723: && RGB_PIXELSIZE == 3) {
2724: cconvert.color_convert = NULL_CONVERT;
2725: } else
2726: error();
2727: // ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
2728: break;
2729:
2730: case JCS_CMYK:
2731: cinfo.out_color_components = 4;
2732: if (cinfo.jpeg_color_space == JCS_YCCK) {
2733: cconvert.color_convert = YCCK_CMYK_CONVERT;
2734: build_ycc_rgb_table(cinfo);
2735: } else if (cinfo.jpeg_color_space == JCS_CMYK) {
2736: cconvert.color_convert = NULL_CONVERT;
2737: } else
2738: error();
2739: // ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
2740: break;
2741:
2742: default:
2743: /* Permit null conversion to same output space */
2744: if (cinfo.out_color_space == cinfo.jpeg_color_space) {
2745: cinfo.out_color_components = cinfo.num_components;
2746: cconvert.color_convert = NULL_CONVERT;
2747: } else
2748: /* unsupported non-null conversion */
2749: error();
2750: // ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
2751: break;
2752: }
2753:
2754: if (cinfo.quantize_colors)
2755: cinfo.output_components = 1; /* single colormapped output component */
2756: else
2757: cinfo.output_components = cinfo.out_color_components;
2758: }
2759:
2760: static void jinit_d_post_controller(jpeg_decompress_struct cinfo,
2761: boolean need_full_buffer) {
2762: jpeg_d_post_controller post = cinfo.post = new jpeg_d_post_controller();
2763: // post.pub.start_pass = start_pass_dpost;
2764: post.whole_image = null; /* flag for no virtual arrays */
2765: post.buffer = null; /* flag for no strip buffer */
2766:
2767: /* Create the quantization buffer, if needed */
2768: if (cinfo.quantize_colors) {
2769: error(SWT.ERROR_NOT_IMPLEMENTED);
2770: // /* The buffer strip height is max_v_samp_factor, which is typically
2771: // * an efficient number of rows for upsampling to return.
2772: // * (In the presence of output rescaling, we might want to be smarter?)
2773: // */
2774: // post.strip_height = cinfo.max_v_samp_factor;
2775: // if (need_full_buffer) {
2776: // /* Two-pass color quantization: need full-image storage. */
2777: // /* We round up the number of rows to a multiple of the strip height. */
2778: //#ifdef QUANT_2PASS_SUPPORTED
2779: // post.whole_image = (*cinfo.mem.request_virt_sarray)
2780: // ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
2781: // cinfo.output_width * cinfo.out_color_components,
2782: // (JDIMENSION) jround_up((long) cinfo.output_height,
2783: // (long) post.strip_height),
2784: // post.strip_height);
2785: //#else
2786: // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
2787: //#endif /* QUANT_2PASS_SUPPORTED */
2788: // } else {
2789: // /* One-pass color quantization: just make a strip buffer. */
2790: // post.buffer = (*cinfo.mem.alloc_sarray)
2791: // ((j_common_ptr) cinfo, JPOOL_IMAGE,
2792: // cinfo.output_width * cinfo.out_color_components,
2793: // post.strip_height);
2794: // }
2795: }
2796: }
2797:
2798: static void make_funny_pointers(jpeg_decompress_struct cinfo)
2799: /* Create the funny pointer lists discussed in the comments above.
2800: * The actual workspace is already allocated (in main.buffer),
2801: * and the space for the pointer lists is allocated too.
2802: * This routine just fills in the curiously ordered lists.
2803: * This will be repeated at the beginning of each pass.
2804: */
2805: {
2806: jpeg_d_main_controller main = cinfo.main;
2807: int ci, i, rgroup;
2808: int M = cinfo.min_DCT_scaled_size;
2809: jpeg_component_info compptr;
2810: byte[][] buf, xbuf0, xbuf1;
2811:
2812: for (ci = 0; ci < cinfo.num_components; ci++) {
2813: compptr = cinfo.comp_info[ci];
2814: rgroup = (compptr.v_samp_factor * compptr.DCT_scaled_size)
2815: / cinfo.min_DCT_scaled_size; /* height of a row group of component */
2816: xbuf0 = main.xbuffer[0][ci];
2817: int xbuf0_offset = main.xbuffer_offset[0][ci];
2818: xbuf1 = main.xbuffer[1][ci];
2819: int xbuf1_offset = main.xbuffer_offset[1][ci];
2820: /* First copy the workspace pointers as-is */
2821: buf = main.buffer[ci];
2822: for (i = 0; i < rgroup * (M + 2); i++) {
2823: xbuf0[i + xbuf0_offset] = xbuf1[i + xbuf1_offset] = buf[i];
2824: }
2825: /* In the second list, put the last four row groups in swapped order */
2826: for (i = 0; i < rgroup * 2; i++) {
2827: xbuf1[rgroup * (M - 2) + i + xbuf1_offset] = buf[rgroup
2828: * M + i];
2829: xbuf1[rgroup * M + i + xbuf1_offset] = buf[rgroup
2830: * (M - 2) + i];
2831: }
2832: /* The wraparound pointers at top and bottom will be filled later
2833: * (see set_wraparound_pointers, below). Initially we want the "above"
2834: * pointers to duplicate the first actual data line. This only needs
2835: * to happen in xbuffer[0].
2836: */
2837: for (i = 0; i < rgroup; i++) {
2838: xbuf0[i - rgroup + xbuf0_offset] = xbuf0[0 + xbuf0_offset];
2839: }
2840: }
2841: }
2842:
2843: static void alloc_funny_pointers(jpeg_decompress_struct cinfo)
2844: /* Allocate space for the funny pointer lists.
2845: * This is done only once, not once per pass.
2846: */
2847: {
2848: jpeg_d_main_controller main = cinfo.main;
2849: int ci, rgroup;
2850: int M = cinfo.min_DCT_scaled_size;
2851: jpeg_component_info compptr;
2852: byte[][] xbuf;
2853:
2854: /* Get top-level space for component array pointers.
2855: * We alloc both arrays with one call to save a few cycles.
2856: */
2857: main.xbuffer[0] = new byte[cinfo.num_components][][];
2858: main.xbuffer[1] = new byte[cinfo.num_components][][];
2859: main.xbuffer_offset[0] = new int[cinfo.num_components];
2860: main.xbuffer_offset[1] = new int[cinfo.num_components];
2861:
2862: for (ci = 0; ci < cinfo.num_components; ci++) {
2863: compptr = cinfo.comp_info[ci];
2864: rgroup = (compptr.v_samp_factor * compptr.DCT_scaled_size)
2865: / cinfo.min_DCT_scaled_size; /* height of a row group of component */
2866: /* Get space for pointer lists --- M+4 row groups in each list.
2867: * We alloc both pointer lists with one call to save a few cycles.
2868: */
2869: xbuf = new byte[2 * (rgroup * (M + 4))][];
2870: int offset = rgroup;
2871: main.xbuffer_offset[0][ci] = offset;
2872: main.xbuffer[0][ci] = xbuf;
2873: offset += rgroup * (M + 4);
2874: main.xbuffer_offset[1][ci] = offset;
2875: main.xbuffer[1][ci] = xbuf;
2876: }
2877: }
2878:
2879: static void jinit_d_main_controller(jpeg_decompress_struct cinfo,
2880: boolean need_full_buffer) {
2881: int ci, rgroup, ngroups;
2882: jpeg_component_info compptr;
2883:
2884: jpeg_d_main_controller main = cinfo.main = new jpeg_d_main_controller();
2885: // main.pub.start_pass = start_pass_main;
2886:
2887: if (need_full_buffer) /* shouldn't happen */
2888: error();
2889: // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
2890:
2891: /* Allocate the workspace.
2892: * ngroups is the number of row groups we need.
2893: */
2894: if (cinfo.upsample.need_context_rows) {
2895: if (cinfo.min_DCT_scaled_size < 2) /* unsupported, see comments above */
2896: error();
2897: // ERREXIT(cinfo, JERR_NOTIMPL);
2898: alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
2899: ngroups = cinfo.min_DCT_scaled_size + 2;
2900: } else {
2901: ngroups = cinfo.min_DCT_scaled_size;
2902: }
2903:
2904: for (ci = 0; ci < cinfo.num_components; ci++) {
2905: compptr = cinfo.comp_info[ci];
2906: rgroup = (compptr.v_samp_factor * compptr.DCT_scaled_size)
2907: / cinfo.min_DCT_scaled_size; /* height of a row group of component */
2908: main.buffer[ci] = new byte[rgroup * ngroups][compptr.width_in_blocks
2909: * compptr.DCT_scaled_size];
2910: }
2911: }
2912:
2913: static long jround_up(long a, long b)
2914: /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
2915: /* Assumes a >= 0, b > 0 */
2916: {
2917: a += b - 1L;
2918: return a - (a % b);
2919: }
2920:
2921: static void jinit_upsampler(jpeg_decompress_struct cinfo) {
2922: int ci;
2923: jpeg_component_info compptr;
2924: boolean need_buffer, do_fancy;
2925: int h_in_group, v_in_group, h_out_group, v_out_group;
2926:
2927: jpeg_upsampler upsample = new jpeg_upsampler();
2928: cinfo.upsample = upsample;
2929: // upsample.start_pass = start_pass_upsample;
2930: // upsample.upsample = sep_upsample;
2931: upsample.need_context_rows = false; /* until we find out differently */
2932:
2933: if (cinfo.CCIR601_sampling) /* this isn't supported */
2934: error();
2935: // ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
2936:
2937: /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
2938: * so don't ask for it.
2939: */
2940: do_fancy = cinfo.do_fancy_upsampling
2941: && cinfo.min_DCT_scaled_size > 1;
2942:
2943: /* Verify we can handle the sampling factors, select per-component methods,
2944: * and create storage as needed.
2945: */
2946: for (ci = 0; ci < cinfo.num_components; ci++) {
2947: compptr = cinfo.comp_info[ci];
2948: /* Compute size of an "input group" after IDCT scaling. This many samples
2949: * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
2950: */
2951: h_in_group = (compptr.h_samp_factor * compptr.DCT_scaled_size)
2952: / cinfo.min_DCT_scaled_size;
2953: v_in_group = (compptr.v_samp_factor * compptr.DCT_scaled_size)
2954: / cinfo.min_DCT_scaled_size;
2955: h_out_group = cinfo.max_h_samp_factor;
2956: v_out_group = cinfo.max_v_samp_factor;
2957: upsample.rowgroup_height[ci] = v_in_group; /* save for use later */
2958: need_buffer = true;
2959: if (!compptr.component_needed) {
2960: /* Don't bother to upsample an uninteresting component. */
2961: upsample.methods[ci] = NOOP_UPSAMPLE;
2962: need_buffer = false;
2963: } else if (h_in_group == h_out_group
2964: && v_in_group == v_out_group) {
2965: /* Fullsize components can be processed without any work. */
2966: upsample.methods[ci] = FULLSIZE_UPSAMPLE;
2967: need_buffer = false;
2968: } else if (h_in_group * 2 == h_out_group
2969: && v_in_group == v_out_group) {
2970: /* Special cases for 2h1v upsampling */
2971: if (do_fancy && compptr.downsampled_width > 2)
2972: upsample.methods[ci] = H2V1_FANCY_UPSAMPLE;
2973: else
2974: upsample.methods[ci] = H2V1_UPSAMPLE;
2975: } else if (h_in_group * 2 == h_out_group
2976: && v_in_group * 2 == v_out_group) {
2977: /* Special cases for 2h2v upsampling */
2978: if (do_fancy && compptr.downsampled_width > 2) {
2979: upsample.methods[ci] = H2V2_FANCY_UPSAMPLE;
2980: upsample.need_context_rows = true;
2981: } else
2982: upsample.methods[ci] = H2V2_UPSAMPLE;
2983: } else if ((h_out_group % h_in_group) == 0
2984: && (v_out_group % v_in_group) == 0) {
2985: /* Generic integral-factors upsampling method */
2986: upsample.methods[ci] = INT_UPSAMPLE;
2987: upsample.h_expand[ci] = (byte) (h_out_group / h_in_group);
2988: upsample.v_expand[ci] = (byte) (v_out_group / v_in_group);
2989: } else
2990: error();
2991: // ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
2992: if (need_buffer) {
2993: upsample.color_buf[ci] = new byte[cinfo.max_v_samp_factor][(int) jround_up(
2994: cinfo.output_width, cinfo.max_h_samp_factor)];
2995: }
2996: }
2997: }
2998:
2999: static void jinit_phuff_decoder(jpeg_decompress_struct cinfo) {
3000: int[][] coef_bit_ptr;
3001: int ci, i;
3002:
3003: cinfo.entropy = new phuff_entropy_decoder();
3004: // entropy.pub.start_pass = start_pass_phuff_decoder;
3005:
3006: /* Create progression status table */
3007: cinfo.coef_bits = new int[cinfo.num_components][DCTSIZE2];
3008: coef_bit_ptr = cinfo.coef_bits;
3009: for (ci = 0; ci < cinfo.num_components; ci++)
3010: for (i = 0; i < DCTSIZE2; i++)
3011: coef_bit_ptr[ci][i] = -1;
3012: }
3013:
3014: static void jinit_huff_decoder(jpeg_decompress_struct cinfo) {
3015:
3016: cinfo.entropy = new huff_entropy_decoder();
3017: // entropy.pub.start_pass = start_pass_huff_decoder;
3018: // entropy.pub.decode_mcu = decode_mcu;
3019:
3020: }
3021:
3022: static void jinit_inverse_dct(jpeg_decompress_struct cinfo) {
3023: int ci;
3024: jpeg_component_info compptr;
3025:
3026: jpeg_inverse_dct idct = cinfo.idct = new jpeg_inverse_dct();
3027: // idct.pub.start_pass = start_pass;
3028:
3029: for (ci = 0; ci < cinfo.num_components; ci++) {
3030: compptr = cinfo.comp_info[ci];
3031: /* Allocate and pre-zero a multiplier table for each component */
3032: compptr.dct_table = new int[DCTSIZE2];
3033: /* Mark multiplier table not yet set up for any method */
3034: idct.cur_method[ci] = -1;
3035: }
3036: }
3037:
3038: static final int CONST_BITS = 13;
3039: static final int PASS1_BITS = 2;
3040: static final int RANGE_MASK = (MAXJSAMPLE * 4 + 3);
3041:
3042: static void jpeg_idct_islow(jpeg_decompress_struct cinfo,
3043: jpeg_component_info compptr, short[] coef_block,
3044: byte[][] output_buf, int output_buf_offset, int output_col) {
3045: int tmp0, tmp1, tmp2, tmp3;
3046: int tmp10, tmp11, tmp12, tmp13;
3047: int z1, z2, z3, z4, z5;
3048: short[] inptr;
3049: int[] quantptr;
3050: int[] wsptr;
3051: byte[] outptr;
3052: byte[] range_limit = cinfo.sample_range_limit;
3053: int range_limit_offset = cinfo.sample_range_limit_offset
3054: + CENTERJSAMPLE;
3055: int ctr;
3056: int[] workspace = cinfo.workspace; /* buffers data between passes */
3057: // SHIFT_TEMPS
3058:
3059: /* Pass 1: process columns from input, store into work array. */
3060: /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
3061: /* furthermore, we scale the results by 2**PASS1_BITS. */
3062:
3063: inptr = coef_block;
3064: quantptr = compptr.dct_table;
3065: wsptr = workspace;
3066: int inptr_offset = 0, quantptr_offset = 0, wsptr_offset = 0;
3067: for (ctr = DCTSIZE; ctr > 0; ctr--) {
3068: /* Due to quantization, we will usually find that many of the input
3069: * coefficients are zero, especially the AC terms. We can exploit this
3070: * by short-circuiting the IDCT calculation for any column in which all
3071: * the AC terms are zero. In that case each output is equal to the
3072: * DC coefficient (with scale factor as needed).
3073: * With typical images and quantization tables, half or more of the
3074: * column DCT calculations can be simplified this way.
3075: */
3076:
3077: if (inptr[DCTSIZE * 1 + inptr_offset] == 0
3078: && inptr[DCTSIZE * 2 + inptr_offset] == 0
3079: && inptr[DCTSIZE * 3 + inptr_offset] == 0
3080: && inptr[DCTSIZE * 4 + inptr_offset] == 0
3081: && inptr[DCTSIZE * 5 + inptr_offset] == 0
3082: && inptr[DCTSIZE * 6 + inptr_offset] == 0
3083: && inptr[DCTSIZE * 7 + inptr_offset] == 0) {
3084: /* AC terms all zero */
3085: int dcval = ((inptr[DCTSIZE * 0 + inptr_offset]) * quantptr[DCTSIZE
3086: * 0 + quantptr_offset]) << PASS1_BITS;
3087:
3088: wsptr[DCTSIZE * 0 + wsptr_offset] = dcval;
3089: wsptr[DCTSIZE * 1 + wsptr_offset] = dcval;
3090: wsptr[DCTSIZE * 2 + wsptr_offset] = dcval;
3091: wsptr[DCTSIZE * 3 + wsptr_offset] = dcval;
3092: wsptr[DCTSIZE * 4 + wsptr_offset] = dcval;
3093: wsptr[DCTSIZE * 5 + wsptr_offset] = dcval;
3094: wsptr[DCTSIZE * 6 + wsptr_offset] = dcval;
3095: wsptr[DCTSIZE * 7 + wsptr_offset] = dcval;
3096:
3097: inptr_offset++; /* advance pointers to next column */
3098: quantptr_offset++;
3099: wsptr_offset++;
3100: continue;
3101: }
3102:
3103: /* Even part: reverse the even part of the forward DCT. */
3104: /* The rotator is sqrt(2)*c(-6). */
3105:
3106: z2 = ((inptr[DCTSIZE * 2 + inptr_offset]) * quantptr[DCTSIZE
3107: * 2 + quantptr_offset]);
3108: z3 = ((inptr[DCTSIZE * 6 + inptr_offset]) * quantptr[DCTSIZE
3109: * 6 + quantptr_offset]);
3110:
3111: z1 = ((z2 + z3) * 4433/*FIX_0_541196100*/);
3112: tmp2 = z1 + (z3 * -15137/*FIX_1_847759065*/);
3113: tmp3 = z1 + (z2 * 6270/*FIX_0_765366865*/);
3114:
3115: z2 = ((inptr[DCTSIZE * 0 + inptr_offset]) * quantptr[DCTSIZE
3116: * 0 + quantptr_offset]);
3117: z3 = ((inptr[DCTSIZE * 4 + inptr_offset]) * quantptr[DCTSIZE
3118: * 4 + quantptr_offset]);
3119:
3120: tmp0 = (z2 + z3) << CONST_BITS;
3121: tmp1 = (z2 - z3) << CONST_BITS;
3122:
3123: tmp10 = tmp0 + tmp3;
3124: tmp13 = tmp0 - tmp3;
3125: tmp11 = tmp1 + tmp2;
3126: tmp12 = tmp1 - tmp2;
3127:
3128: /* Odd part per figure 8; the matrix is unitary and hence its
3129: * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
3130: */
3131:
3132: tmp0 = ((inptr[DCTSIZE * 7 + inptr_offset]) * quantptr[DCTSIZE
3133: * 7 + quantptr_offset]);
3134: tmp1 = ((inptr[DCTSIZE * 5 + inptr_offset]) * quantptr[DCTSIZE
3135: * 5 + quantptr_offset]);
3136: tmp2 = ((inptr[DCTSIZE * 3 + inptr_offset]) * quantptr[DCTSIZE
3137: * 3 + quantptr_offset]);
3138: tmp3 = ((inptr[DCTSIZE * 1 + inptr_offset]) * quantptr[DCTSIZE
3139: * 1 + quantptr_offset]);
3140:
3141: z1 = tmp0 + tmp3;
3142: z2 = tmp1 + tmp2;
3143: z3 = tmp0 + tmp2;
3144: z4 = tmp1 + tmp3;
3145: z5 = ((z3 + z4) * 9633/*FIX_1_175875602*/); /* sqrt(2) * c3 */
3146:
3147: tmp0 = (tmp0 * 2446/*FIX_0_298631336*/); /* sqrt(2) * (-c1+c3+c5-c7) */
3148: tmp1 = (tmp1 * 16819/*FIX_2_053119869*/); /* sqrt(2) * ( c1+c3-c5+c7) */
3149: tmp2 = (tmp2 * 25172/*FIX_3_072711026*/); /* sqrt(2) * ( c1+c3+c5-c7) */
3150: tmp3 = (tmp3 * 12299/*FIX_1_501321110*/); /* sqrt(2) * ( c1+c3-c5-c7) */
3151: z1 = (z1 * -7373/*FIX_0_899976223*/); /* sqrt(2) * (c7-c3) */
3152: z2 = (z2 * -20995/*FIX_2_562915447*/); /* sqrt(2) * (-c1-c3) */
3153: z3 = (z3 * -16069/*FIX_1_961570560*/); /* sqrt(2) * (-c3-c5) */
3154: z4 = (z4 * -3196/*FIX_0_390180644*/); /* sqrt(2) * (c5-c3) */
3155:
3156: z3 += z5;
3157: z4 += z5;
3158:
3159: tmp0 += z1 + z3;
3160: tmp1 += z2 + z4;
3161: tmp2 += z2 + z3;
3162: tmp3 += z1 + z4;
3163:
3164: /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
3165:
3166: // #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
3167: wsptr[DCTSIZE * 0 + wsptr_offset] = (((tmp10 + tmp3) + (1 << ((CONST_BITS - PASS1_BITS) - 1))) >> (CONST_BITS - PASS1_BITS));
3168: wsptr[DCTSIZE * 7 + wsptr_offset] = (((tmp10 - tmp3) + (1 << ((CONST_BITS - PASS1_BITS) - 1))) >> (CONST_BITS - PASS1_BITS));
3169: wsptr[DCTSIZE * 1 + wsptr_offset] = (((tmp11 + tmp2) + (1 << ((CONST_BITS - PASS1_BITS) - 1))) >> (CONST_BITS - PASS1_BITS));
3170: wsptr[DCTSIZE * 6 + wsptr_offset] = (((tmp11 - tmp2) + (1 << ((CONST_BITS - PASS1_BITS) - 1))) >> (CONST_BITS - PASS1_BITS));
3171: wsptr[DCTSIZE * 2 + wsptr_offset] = (((tmp12 + tmp1) + (1 << ((CONST_BITS - PASS1_BITS) - 1))) >> (CONST_BITS - PASS1_BITS));
3172: wsptr[DCTSIZE * 5 + wsptr_offset] = (((tmp12 - tmp1) + (1 << ((CONST_BITS - PASS1_BITS) - 1))) >> (CONST_BITS - PASS1_BITS));
3173: wsptr[DCTSIZE * 3 + wsptr_offset] = (((tmp13 + tmp0) + (1 << ((CONST_BITS - PASS1_BITS) - 1))) >> (CONST_BITS - PASS1_BITS));
3174: wsptr[DCTSIZE * 4 + wsptr_offset] = (((tmp13 - tmp0) + (1 << ((CONST_BITS - PASS1_BITS) - 1))) >> (CONST_BITS - PASS1_BITS));
3175:
3176: inptr_offset++; /* advance pointers to next column */
3177: quantptr_offset++;
3178: wsptr_offset++;
3179: }
3180:
3181: /* Pass 2: process rows from work array, store into output array. */
3182: /* Note that we must descale the results by a factor of 8 == 2**3, */
3183: /* and also undo the PASS1_BITS scaling. */
3184:
3185: int outptr_offset = 0;
3186: wsptr = workspace;
3187: wsptr_offset = 0;
3188: for (ctr = 0; ctr < DCTSIZE; ctr++) {
3189: outptr = output_buf[ctr + output_buf_offset];
3190: outptr_offset = output_col;
3191: /* Rows of zeroes can be exploited in the same way as we did with columns.
3192: * However, the column calculation has created many nonzero AC terms, so
3193: * the simplification applies less often (typically 5% to 10% of the time).
3194: * On machines with very fast multiplication, it's possible that the
3195: * test takes more time than it's worth. In that case this section
3196: * may be commented out.
3197: */
3198:
3199: //#ifndef NO_ZERO_ROW_TEST
3200: if (wsptr[1 + wsptr_offset] == 0
3201: && wsptr[2 + wsptr_offset] == 0
3202: && wsptr[3 + wsptr_offset] == 0
3203: && wsptr[4 + wsptr_offset] == 0
3204: && wsptr[5 + wsptr_offset] == 0
3205: && wsptr[6 + wsptr_offset] == 0
3206: && wsptr[7 + wsptr_offset] == 0) {
3207: /* AC terms all zero */
3208: // #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
3209: byte dcval = range_limit[range_limit_offset
3210: + ((((wsptr[0 + wsptr_offset]) + (1 << ((PASS1_BITS + 3) - 1))) >> PASS1_BITS + 3) & RANGE_MASK)];
3211:
3212: outptr[0 + outptr_offset] = dcval;
3213: outptr[1 + outptr_offset] = dcval;
3214: outptr[2 + outptr_offset] = dcval;
3215: outptr[3 + outptr_offset] = dcval;
3216: outptr[4 + outptr_offset] = dcval;
3217: outptr[5 + outptr_offset] = dcval;
3218: outptr[6 + outptr_offset] = dcval;
3219: outptr[7 + outptr_offset] = dcval;
3220:
3221: wsptr_offset += DCTSIZE; /* advance pointer to next row */
3222: continue;
3223: }
3224: //#endif
3225:
3226: /* Even part: reverse the even part of the forward DCT. */
3227: /* The rotator is sqrt(2)*c(-6). */
3228:
3229: z2 = wsptr[2 + wsptr_offset];
3230: z3 = wsptr[6 + wsptr_offset];
3231:
3232: z1 = ((z2 + z3) * 4433/*FIX_0_541196100*/);
3233: tmp2 = z1 + (z3 * -15137/*FIX_1_847759065*/);
3234: tmp3 = z1 + (z2 * 6270/*FIX_0_765366865*/);
3235:
3236: tmp0 = (wsptr[0 + wsptr_offset] + wsptr[4 + wsptr_offset]) << CONST_BITS;
3237: tmp1 = (wsptr[0 + wsptr_offset] - wsptr[4 + wsptr_offset]) << CONST_BITS;
3238:
3239: tmp10 = tmp0 + tmp3;
3240: tmp13 = tmp0 - tmp3;
3241: tmp11 = tmp1 + tmp2;
3242: tmp12 = tmp1 - tmp2;
3243:
3244: /* Odd part per figure 8; the matrix is unitary and hence its
3245: * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
3246: */
3247:
3248: tmp0 = wsptr[7 + wsptr_offset];
3249: tmp1 = wsptr[5 + wsptr_offset];
3250: tmp2 = wsptr[3 + wsptr_offset];
3251: tmp3 = wsptr[1 + wsptr_offset];
3252:
3253: z1 = tmp0 + tmp3;
3254: z2 = tmp1 + tmp2;
3255: z3 = tmp0 + tmp2;
3256: z4 = tmp1 + tmp3;
3257: z5 = ((z3 + z4) * 9633/*FIX_1_175875602*/); /* sqrt(2) * c3 */
3258:
3259: tmp0 = (tmp0 * 2446/*FIX_0_298631336*/); /* sqrt(2) * (-c1+c3+c5-c7) */
3260: tmp1 = (tmp1 * 16819/*FIX_2_053119869*/); /* sqrt(2) * ( c1+c3-c5+c7) */
3261: tmp2 = (tmp2 * 25172/*FIX_3_072711026*/); /* sqrt(2) * ( c1+c3+c5-c7) */
3262: tmp3 = (tmp3 * 12299/*FIX_1_501321110*/); /* sqrt(2) * ( c1+c3-c5-c7) */
3263: z1 = (z1 * -7373/*FIX_0_899976223*/); /* sqrt(2) * (c7-c3) */
3264: z2 = (z2 * -20995/*FIX_2_562915447*/); /* sqrt(2) * (-c1-c3) */
3265: z3 = (z3 * -16069/*FIX_1_961570560*/); /* sqrt(2) * (-c3-c5) */
3266: z4 = (z4 * -3196/*FIX_0_390180644*/); /* sqrt(2) * (c5-c3) */
3267:
3268: z3 += z5;
3269: z4 += z5;
3270:
3271: tmp0 += z1 + z3;
3272: tmp1 += z2 + z4;
3273: tmp2 += z2 + z3;
3274: tmp3 += z1 + z4;
3275:
3276: /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
3277:
3278: // #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
3279: outptr[0 + outptr_offset] = range_limit[range_limit_offset
3280: + ((((tmp10 + tmp3) + (1 << ((CONST_BITS
3281: + PASS1_BITS + 3) - 1))) >> CONST_BITS
3282: + PASS1_BITS + 3) & RANGE_MASK)];
3283: outptr[7 + outptr_offset] = range_limit[range_limit_offset
3284: + ((((tmp10 - tmp3) + (1 << ((CONST_BITS
3285: + PASS1_BITS + 3) - 1))) >> CONST_BITS
3286: + PASS1_BITS + 3) & RANGE_MASK)];
3287: outptr[1 + outptr_offset] = range_limit[range_limit_offset
3288: + ((((tmp11 + tmp2) + (1 << ((CONST_BITS
3289: + PASS1_BITS + 3) - 1))) >> CONST_BITS
3290: + PASS1_BITS + 3) & RANGE_MASK)];
3291: outptr[6 + outptr_offset] = range_limit[range_limit_offset
3292: + ((((tmp11 - tmp2) + (1 << ((CONST_BITS
3293: + PASS1_BITS + 3) - 1))) >> CONST_BITS
3294: + PASS1_BITS + 3) & RANGE_MASK)];
3295: outptr[2 + outptr_offset] = range_limit[range_limit_offset
3296: + ((((tmp12 + tmp1) + (1 << ((CONST_BITS
3297: + PASS1_BITS + 3) - 1))) >> CONST_BITS
3298: + PASS1_BITS + 3) & RANGE_MASK)];
3299: outptr[5 + outptr_offset] = range_limit[range_limit_offset
3300: + ((((tmp12 - tmp1) + (1 << ((CONST_BITS
3301: + PASS1_BITS + 3) - 1))) >> CONST_BITS
3302: + PASS1_BITS + 3) & RANGE_MASK)];
3303: outptr[3 + outptr_offset] = range_limit[range_limit_offset
3304: + ((((tmp13 + tmp0) + (1 << ((CONST_BITS
3305: + PASS1_BITS + 3) - 1))) >> CONST_BITS
3306: + PASS1_BITS + 3) & RANGE_MASK)];
3307: outptr[4 + outptr_offset] = range_limit[range_limit_offset
3308: + ((((tmp13 - tmp0) + (1 << ((CONST_BITS
3309: + PASS1_BITS + 3) - 1))) >> CONST_BITS
3310: + PASS1_BITS + 3) & RANGE_MASK)];
3311:
3312: wsptr_offset += DCTSIZE; /* advance pointer to next row */
3313: }
3314: }
3315:
3316: static void upsample(jpeg_decompress_struct cinfo,
3317: byte[][][] input_buf, int[] input_buf_offset,
3318: int[] in_row_group_ctr, int in_row_groups_avail,
3319: byte[][] output_buf, int[] out_row_ctr, int out_rows_avail) {
3320: sep_upsample(cinfo, input_buf, input_buf_offset,
3321: in_row_group_ctr, in_row_groups_avail, output_buf,
3322: out_row_ctr, out_rows_avail);
3323: }
3324:
3325: static boolean smoothing_ok(jpeg_decompress_struct cinfo) {
3326: jpeg_d_coef_controller coef = cinfo.coef;
3327: boolean smoothing_useful = false;
3328: int ci, coefi;
3329: jpeg_component_info compptr;
3330: JQUANT_TBL qtable;
3331: int[] coef_bits;
3332: int[] coef_bits_latch;
3333:
3334: if (!cinfo.progressive_mode || cinfo.coef_bits == null)
3335: return false;
3336:
3337: /* Allocate latch area if not already done */
3338: if (coef.coef_bits_latch == null)
3339: coef.coef_bits_latch = new int[cinfo.num_components
3340: * SAVED_COEFS];
3341: coef_bits_latch = coef.coef_bits_latch;
3342: int coef_bits_latch_offset = 0;
3343:
3344: for (ci = 0; ci < cinfo.num_components; ci++) {
3345: compptr = cinfo.comp_info[ci];
3346: /* All components' quantization values must already be latched. */
3347: if ((qtable = compptr.quant_table) == null)
3348: return false;
3349: /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
3350: if (qtable.quantval[0] == 0
3351: || qtable.quantval[Q01_POS] == 0
3352: || qtable.quantval[Q10_POS] == 0
3353: || qtable.quantval[Q20_POS] == 0
3354: || qtable.quantval[Q11_POS] == 0
3355: || qtable.quantval[Q02_POS] == 0)
3356: return false;
3357: /* DC values must be at least partly known for all components. */
3358: coef_bits = cinfo.coef_bits[ci];
3359: if (coef_bits[0] < 0)
3360: return false;
3361: /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
3362: for (coefi = 1; coefi <= 5; coefi++) {
3363: coef_bits_latch[coefi + coef_bits_latch_offset] = coef_bits[coefi];
3364: if (coef_bits[coefi] != 0)
3365: smoothing_useful = true;
3366: }
3367: coef_bits_latch_offset += SAVED_COEFS;
3368: }
3369:
3370: return smoothing_useful;
3371: }
3372:
3373: static void master_selection(jpeg_decompress_struct cinfo) {
3374: jpeg_decomp_master master = cinfo.master;
3375: boolean use_c_buffer;
3376: long samplesperrow;
3377: int jd_samplesperrow;
3378:
3379: /* Initialize dimensions and other stuff */
3380: jpeg_calc_output_dimensions(cinfo);
3381: prepare_range_limit_table(cinfo);
3382:
3383: /* Width of an output scanline must be representable as JDIMENSION. */
3384: samplesperrow = (long) cinfo.output_width
3385: * (long) cinfo.out_color_components;
3386: jd_samplesperrow = (int) samplesperrow;
3387: if (jd_samplesperrow != samplesperrow)
3388: error();
3389: // ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
3390:
3391: /* Initialize my private state */
3392: master.pass_number = 0;
3393: master.using_merged_upsample = use_merged_upsample(cinfo);
3394:
3395: /* Color quantizer selection */
3396: master.quantizer_1pass = null;
3397: master.quantizer_2pass = null;
3398: /* No mode changes if not using buffered-image mode. */
3399: if (!cinfo.quantize_colors || !cinfo.buffered_image) {
3400: cinfo.enable_1pass_quant = false;
3401: cinfo.enable_external_quant = false;
3402: cinfo.enable_2pass_quant = false;
3403: }
3404: if (cinfo.quantize_colors) {
3405: error(SWT.ERROR_NOT_IMPLEMENTED);
3406: // if (cinfo.raw_data_out)
3407: // ERREXIT(cinfo, JERR_NOTIMPL);
3408: // /* 2-pass quantizer only works in 3-component color space. */
3409: // if (cinfo.out_color_components != 3) {
3410: // cinfo.enable_1pass_quant = true;
3411: // cinfo.enable_external_quant = false;
3412: // cinfo.enable_2pass_quant = false;
3413: // cinfo.colormap = null;
3414: // } else if (cinfo.colormap != null) {
3415: // cinfo.enable_external_quant = true;
3416: // } else if (cinfo.two_pass_quantize) {
3417: // cinfo.enable_2pass_quant = true;
3418: // } else {
3419: // cinfo.enable_1pass_quant = true;
3420: // }
3421: //
3422: // if (cinfo.enable_1pass_quant) {
3423: //#ifdef QUANT_1PASS_SUPPORTED
3424: // jinit_1pass_quantizer(cinfo);
3425: // master.quantizer_1pass = cinfo.cquantize;
3426: //#else
3427: // ERREXIT(cinfo, JERR_NOT_COMPILED);
3428: //#endif
3429: // }
3430: //
3431: // /* We use the 2-pass code to map to external colormaps. */
3432: // if (cinfo.enable_2pass_quant || cinfo.enable_external_quant) {
3433: //#ifdef QUANT_2PASS_SUPPORTED
3434: // jinit_2pass_quantizer(cinfo);
3435: // master.quantizer_2pass = cinfo.cquantize;
3436: //#else
3437: // ERREXIT(cinfo, JERR_NOT_COMPILED);
3438: //#endif
3439: // }
3440: // /* If both quantizers are initialized, the 2-pass one is left active;
3441: // * this is necessary for starting with quantization to an external map.
3442: // */
3443: }
3444:
3445: /* Post-processing: in particular, color conversion first */
3446: if (!cinfo.raw_data_out) {
3447: if (master.using_merged_upsample) {
3448: //#ifdef UPSAMPLE_MERGING_SUPPORTED
3449: // jinit_merged_upsampler(cinfo); /* does color conversion too */
3450: //#else
3451: error();
3452: // ERREXIT(cinfo, JERR_NOT_COMPILED);
3453: //#endif
3454: } else {
3455: jinit_color_deconverter(cinfo);
3456: jinit_upsampler(cinfo);
3457: }
3458: jinit_d_post_controller(cinfo, cinfo.enable_2pass_quant);
3459: }
3460: /* Inverse DCT */
3461: jinit_inverse_dct(cinfo);
3462: /* Entropy decoding: either Huffman or arithmetic coding. */
3463: if (cinfo.arith_code) {
3464: error();
3465: // ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
3466: } else {
3467: if (cinfo.progressive_mode) {
3468: //#ifdef D_PROGRESSIVE_SUPPORTED
3469: jinit_phuff_decoder(cinfo);
3470: //#else
3471: // ERREXIT(cinfo, JERR_NOT_COMPILED);
3472: //#endif
3473: } else
3474: jinit_huff_decoder(cinfo);
3475: }
3476:
3477: /* Initialize principal buffer controllers. */
3478: use_c_buffer = cinfo.inputctl.has_multiple_scans
3479: || cinfo.buffered_image;
3480: jinit_d_coef_controller(cinfo, use_c_buffer);
3481:
3482: if (!cinfo.raw_data_out)
3483: jinit_d_main_controller(cinfo, false /* never need full buffer here */);
3484:
3485: /* Initialize input side of decompressor to consume first scan. */
3486: start_input_pass(cinfo);
3487:
3488: //#ifdef D_MULTISCAN_FILES_SUPPORTED
3489: /* If jpeg_start_decompress will read the whole file, initialize
3490: * progress monitoring appropriately. The input step is counted
3491: * as one pass.
3492: */
3493: // if (cinfo.progress != null && ! cinfo.buffered_image &&
3494: // cinfo.inputctl.has_multiple_scans) {
3495: // int nscans;
3496: // /* Estimate number of scans to set pass_limit. */
3497: // if (cinfo.progressive_mode) {
3498: // /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
3499: // nscans = 2 + 3 * cinfo.num_components;
3500: // } else {
3501: // /* For a nonprogressive multiscan file, estimate 1 scan per component. */
3502: // nscans = cinfo.num_components;
3503: // }
3504: // cinfo.progress.pass_counter = 0L;
3505: // cinfo.progress.pass_limit = (long) cinfo.total_iMCU_rows * nscans;
3506: // cinfo.progress.completed_passes = 0;
3507: // cinfo.progress.total_passes = (cinfo.enable_2pass_quant ? 3 : 2);
3508: // /* Count the input pass as done */
3509: // master.pass_number++;
3510: // }
3511: //#endif /* D_MULTISCAN_FILES_SUPPORTED */
3512: }
3513:
3514: static void jinit_master_decompress(jpeg_decompress_struct cinfo) {
3515: jpeg_decomp_master master = new jpeg_decomp_master();
3516: cinfo.master = master;
3517: // master.prepare_for_output_pass = prepare_for_output_pass;
3518: // master.finish_output_pass = finish_output_pass;
3519:
3520: master.is_dummy_pass = false;
3521:
3522: master_selection(cinfo);
3523: }
3524:
3525: static void jcopy_sample_rows(byte[][] input_array, int source_row,
3526: byte[][] output_array, int dest_row, int num_rows,
3527: int num_cols)
3528: /* Copy some rows of samples from one place to another.
3529: * num_rows rows are copied from input_array[source_row++]
3530: * to output_array[dest_row++]; these areas may overlap for duplication.
3531: * The source and destination arrays must be at least as wide as num_cols.
3532: */
3533: {
3534: byte[] inptr, outptr;
3535: int count = num_cols;
3536: int row;
3537:
3538: int input_array_offset = source_row;
3539: int output_array_offset = dest_row;
3540:
3541: for (row = num_rows; row > 0; row--) {
3542: inptr = input_array[input_array_offset++];
3543: outptr = output_array[output_array_offset++];
3544: System.arraycopy(inptr, 0, outptr, 0, count);
3545: }
3546: }
3547:
3548: static boolean jpeg_start_decompress(jpeg_decompress_struct cinfo) {
3549: if (cinfo.global_state == DSTATE_READY) {
3550: /* First call: initialize master control, select active modules */
3551: jinit_master_decompress(cinfo);
3552: if (cinfo.buffered_image) {
3553: /* No more work here; expecting jpeg_start_output next */
3554: cinfo.global_state = DSTATE_BUFIMAGE;
3555: return true;
3556: }
3557: cinfo.global_state = DSTATE_PRELOAD;
3558: }
3559: if (cinfo.global_state == DSTATE_PRELOAD) {
3560: /* If file has multiple scans, absorb them all into the coef buffer */
3561: if (cinfo.inputctl.has_multiple_scans) {
3562: //#ifdef D_MULTISCAN_FILES_SUPPORTED
3563: for (;;) {
3564: int retcode;
3565: /* Call progress monitor hook if present */
3566: // if (cinfo.progress != null)
3567: // (*cinfo.progress.progress_monitor) ((j_common_ptr) cinfo);
3568: /* Absorb some more input */
3569: retcode = consume_input(cinfo);
3570: if (retcode == JPEG_SUSPENDED)
3571: return false;
3572: if (retcode == JPEG_REACHED_EOI)
3573: break;
3574: /* Advance progress counter if appropriate */
3575: // if (cinfo.progress != null && (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
3576: // if (++cinfo.progress.pass_counter >= cinfo.progress.pass_limit) {
3577: // /* jdmaster underestimated number of scans; ratchet up one scan */
3578: // cinfo.progress.pass_limit += (long) cinfo.total_iMCU_rows;
3579: // }
3580: // }
3581: }
3582: //#else
3583: // ERREXIT(cinfo, JERR_NOT_COMPILED);
3584: //#endif /* D_MULTISCAN_FILES_SUPPORTED */
3585: }
3586: cinfo.output_scan_number = cinfo.input_scan_number;
3587: } else if (cinfo.global_state != DSTATE_PRESCAN)
3588: error();
3589: // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
3590: /* Perform any dummy output passes, and set up for the final pass */
3591: return output_pass_setup(cinfo);
3592: }
3593:
3594: static void prepare_for_output_pass(jpeg_decompress_struct cinfo) {
3595: jpeg_decomp_master master = cinfo.master;
3596:
3597: if (master.is_dummy_pass) {
3598: //#ifdef QUANT_2PASS_SUPPORTED
3599: // /* Final pass of 2-pass quantization */
3600: // master.pub.is_dummy_pass = FALSE;
3601: // (*cinfo.cquantize.start_pass) (cinfo, FALSE);
3602: // (*cinfo.post.start_pass) (cinfo, JBUF_CRANK_DEST);
3603: // (*cinfo.main.start_pass) (cinfo, JBUF_CRANK_DEST);
3604: //#else
3605: error(SWT.ERROR_NOT_IMPLEMENTED);
3606: // ERREXIT(cinfo, JERR_NOT_COMPILED);
3607: //#endif /* QUANT_2PASS_SUPPORTED */
3608: } else {
3609: if (cinfo.quantize_colors && cinfo.colormap == null) {
3610: /* Select new quantization method */
3611: if (cinfo.two_pass_quantize && cinfo.enable_2pass_quant) {
3612: cinfo.cquantize = master.quantizer_2pass;
3613: master.is_dummy_pass = true;
3614: } else if (cinfo.enable_1pass_quant) {
3615: cinfo.cquantize = master.quantizer_1pass;
3616: } else {
3617: error();
3618: // ERREXIT(cinfo, JERR_MODE_CHANGE);
3619: }
3620: }
3621: cinfo.idct.start_pass(cinfo);
3622: start_output_pass(cinfo);
3623: if (!cinfo.raw_data_out) {
3624: if (!master.using_merged_upsample)
3625: cinfo.cconvert.start_pass(cinfo);
3626: cinfo.upsample.start_pass(cinfo);
3627: if (cinfo.quantize_colors)
3628: cinfo.cquantize.start_pass(cinfo,
3629: master.is_dummy_pass);
3630: cinfo.post.start_pass(cinfo,
3631: (master.is_dummy_pass ? JBUF_SAVE_AND_PASS
3632: : JBUF_PASS_THRU));
3633: cinfo.main.start_pass(cinfo, JBUF_PASS_THRU);
3634: }
3635: }
3636:
3637: // /* Set up progress monitor's pass info if present */
3638: // if (cinfo.progress != NULL) {
3639: // cinfo.progress.completed_passes = master.pass_number;
3640: // cinfo.progress.total_passes = master.pass_number +
3641: // (master.pub.is_dummy_pass ? 2 : 1);
3642: // /* In buffered-image mode, we assume one more output pass if EOI not
3643: // * yet reached, but no more passes if EOI has been reached.
3644: // */
3645: // if (cinfo.buffered_image && ! cinfo.inputctl.eoi_reached) {
3646: // cinfo.progress.total_passes += (cinfo.enable_2pass_quant ? 2 : 1);
3647: // }
3648: // }
3649: }
3650:
3651: static boolean jpeg_resync_to_restart(jpeg_decompress_struct cinfo,
3652: int desired) {
3653: int marker = cinfo.unread_marker;
3654: int action = 1;
3655:
3656: /* Always put up a warning. */
3657: // WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
3658: /* Outer loop handles repeated decision after scanning forward. */
3659: for (;;) {
3660: if (marker < M_SOF0)
3661: action = 2; /* invalid marker */
3662: else if (marker < M_RST0 || marker > M_RST7)
3663: action = 3; /* valid non-restart marker */
3664: else {
3665: if (marker == (M_RST0 + ((desired + 1) & 7))
3666: || marker == (M_RST0 + ((desired + 2) & 7)))
3667: action = 3; /* one of the next two expected restarts */
3668: else if (marker == (M_RST0 + ((desired - 1) & 7))
3669: || marker == (M_RST0 + ((desired - 2) & 7)))
3670: action = 2; /* a prior restart, so advance */
3671: else
3672: action = 1; /* desired restart or too far away */
3673: }
3674: // TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
3675: switch (action) {
3676: case 1:
3677: /* Discard marker and let entropy decoder resume processing. */
3678: cinfo.unread_marker = 0;
3679: return true;
3680: case 2:
3681: /* Scan to the next marker, and repeat the decision loop. */
3682: if (!next_marker(cinfo))
3683: return false;
3684: marker = cinfo.unread_marker;
3685: break;
3686: case 3:
3687: /* Return without advancing past this marker. */
3688: /* Entropy decoder will be forced to process an empty segment. */
3689: return true;
3690: }
3691: } /* end loop */
3692: }
3693:
3694: static boolean read_restart_marker(jpeg_decompress_struct cinfo) {
3695: /* Obtain a marker unless we already did. */
3696: /* Note that next_marker will complain if it skips any data. */
3697: if (cinfo.unread_marker == 0) {
3698: if (!next_marker(cinfo))
3699: return false;
3700: }
3701:
3702: if (cinfo.unread_marker == (M_RST0 + cinfo.marker.next_restart_num)) {
3703: /* Normal case --- swallow the marker and let entropy decoder continue */
3704: // TRACEMS1(cinfo, 3, JTRC_RST, cinfo.marker.next_restart_num);
3705: cinfo.unread_marker = 0;
3706: } else {
3707: /* Uh-oh, the restart markers have been messed up. */
3708: /* Let the data source manager determine how to resync. */
3709: if (!jpeg_resync_to_restart(cinfo,
3710: cinfo.marker.next_restart_num))
3711: return false;
3712: }
3713:
3714: /* Update next-restart state */
3715: cinfo.marker.next_restart_num = (cinfo.marker.next_restart_num + 1) & 7;
3716:
3717: return true;
3718: }
3719:
3720: static boolean jpeg_fill_bit_buffer(bitread_working_state state,
3721: int get_buffer, int bits_left, int nbits)
3722: /* Load up the bit buffer to a depth of at least nbits */
3723: {
3724: /* Copy heavily used state fields into locals (hopefully registers) */
3725: byte[] buffer = state.buffer;
3726: int bytes_in_buffer = state.bytes_in_buffer;
3727: int bytes_offset = state.bytes_offset;
3728: jpeg_decompress_struct cinfo = state.cinfo;
3729:
3730: /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
3731: /* (It is assumed that no request will be for more than that many bits.) */
3732: /* We fail to do so only if we hit a marker or are forced to suspend. */
3733:
3734: if (cinfo.unread_marker == 0) { /* cannot advance past a marker */
3735: while (bits_left < MIN_GET_BITS) {
3736: int c;
3737:
3738: /* Attempt to read a byte */
3739: if (bytes_offset == bytes_in_buffer) {
3740: if (!fill_input_buffer(cinfo))
3741: return false;
3742: buffer = cinfo.buffer;
3743: bytes_in_buffer = cinfo.bytes_in_buffer;
3744: bytes_offset = cinfo.bytes_offset;
3745: }
3746: c = buffer[bytes_offset++] & 0xFF;
3747:
3748: /* If it's 0xFF, check and discard stuffed zero byte */
3749: if (c == 0xFF) {
3750: /* Loop here to discard any padding FF's on terminating marker,
3751: * so that we can save a valid unread_marker value. NOTE: we will
3752: * accept multiple FF's followed by a 0 as meaning a single FF data
3753: * byte. This data pattern is not valid according to the standard.
3754: */
3755: do {
3756: if (bytes_offset == bytes_in_buffer) {
3757: if (!fill_input_buffer(cinfo))
3758: return false;
3759: buffer = cinfo.buffer;
3760: bytes_in_buffer = cinfo.bytes_in_buffer;
3761: bytes_offset = cinfo.bytes_offset;
3762: }
3763: c = buffer[bytes_offset++] & 0xFF;
3764: } while (c == 0xFF);
3765:
3766: if (c == 0) {
3767: /* Found FF/00, which represents an FF data byte */
3768: c = 0xFF;
3769: } else {
3770: /* Oops, it's actually a marker indicating end of compressed data.
3771: * Save the marker code for later use.
3772: * Fine point: it might appear that we should save the marker into
3773: * bitread working state, not straight into permanent state. But
3774: * once we have hit a marker, we cannot need to suspend within the
3775: * current MCU, because we will read no more bytes from the data
3776: * source. So it is OK to update permanent state right away.
3777: */
3778: cinfo.unread_marker = c;
3779: /* See if we need to insert some fake zero bits. */
3780: // goto no_more_bytes;
3781: if (nbits > bits_left) {
3782: /* Uh-oh. Report corrupted data to user and stuff zeroes into
3783: * the data stream, so that we can produce some kind of image.
3784: * We use a nonvolatile flag to ensure that only one warning message
3785: * appears per data segment.
3786: */
3787: if (!cinfo.entropy.insufficient_data) {
3788: // WARNMS(cinfo, JWRN_HIT_MARKER);
3789: cinfo.entropy.insufficient_data = true;
3790: }
3791: /* Fill the buffer with zero bits */
3792: get_buffer <<= MIN_GET_BITS - bits_left;
3793: bits_left = MIN_GET_BITS;
3794: }
3795:
3796: /* Unload the local registers */
3797: state.buffer = buffer;
3798: state.bytes_in_buffer = bytes_in_buffer;
3799: state.bytes_offset = bytes_offset;
3800: state.get_buffer = get_buffer;
3801: state.bits_left = bits_left;
3802:
3803: return true;
3804:
3805: }
3806: }
3807:
3808: /* OK, load c into get_buffer */
3809: get_buffer = (get_buffer << 8) | c;
3810: bits_left += 8;
3811: } /* end while */
3812: } else {
3813: // no_more_bytes:
3814: /* We get here if we've read the marker that terminates the compressed
3815: * data segment. There should be enough bits in the buffer register
3816: * to satisfy the request; if so, no problem.
3817: */
3818: if (nbits > bits_left) {
3819: /* Uh-oh. Report corrupted data to user and stuff zeroes into
3820: * the data stream, so that we can produce some kind of image.
3821: * We use a nonvolatile flag to ensure that only one warning message
3822: * appears per data segment.
3823: */
3824: if (!cinfo.entropy.insufficient_data) {
3825: // WARNMS(cinfo, JWRN_HIT_MARKER);
3826: cinfo.entropy.insufficient_data = true;
3827: }
3828: /* Fill the buffer with zero bits */
3829: get_buffer <<= MIN_GET_BITS - bits_left;
3830: bits_left = MIN_GET_BITS;
3831: }
3832: }
3833:
3834: /* Unload the local registers */
3835: state.buffer = buffer;
3836: state.bytes_in_buffer = bytes_in_buffer;
3837: state.bytes_offset = bytes_offset;
3838: state.get_buffer = get_buffer;
3839: state.bits_left = bits_left;
3840:
3841: return true;
3842: }
3843:
3844: static int jpeg_huff_decode(bitread_working_state state,
3845: int get_buffer, int bits_left, d_derived_tbl htbl,
3846: int min_bits) {
3847: int l = min_bits;
3848: int code;
3849:
3850: /* HUFF_DECODE has determined that the code is at least min_bits */
3851: /* bits long, so fetch that many bits in one swoop. */
3852:
3853: // CHECK_BIT_BUFFER(*state, l, return -1);
3854: {
3855: if (bits_left < (l)) {
3856: if (!jpeg_fill_bit_buffer(state, get_buffer, bits_left,
3857: l)) {
3858: return -1;
3859: }
3860: get_buffer = (state).get_buffer;
3861: bits_left = (state).bits_left;
3862: }
3863: }
3864: // code = GET_BITS(l);
3865: code = (((get_buffer >> (bits_left -= (l)))) & ((1 << (l)) - 1));
3866:
3867: /* Collect the rest of the Huffman code one bit at a time. */
3868: /* This is per Figure F.16 in the JPEG spec. */
3869:
3870: while (code > htbl.maxcode[l]) {
3871: code <<= 1;
3872: // CHECK_BIT_BUFFER(*state, 1, return -1);
3873: {
3874: if (bits_left < (1)) {
3875: if (!jpeg_fill_bit_buffer(state, get_buffer,
3876: bits_left, 1)) {
3877: return -1;
3878: }
3879: get_buffer = (state).get_buffer;
3880: bits_left = (state).bits_left;
3881: }
3882: }
3883: // code |= GET_BITS(1);
3884: code |= (((get_buffer >> (bits_left -= (1)))) & ((1 << (1)) - 1));
3885: l++;
3886: }
3887:
3888: /* Unload the local registers */
3889: state.get_buffer = get_buffer;
3890: state.bits_left = bits_left;
3891:
3892: /* With garbage input we may reach the sentinel value l = 17. */
3893:
3894: if (l > 16) {
3895: // WARNMS(state.cinfo, JWRN_HUFF_BAD_CODE);
3896: return 0; /* fake a zero as the safest result */
3897: }
3898:
3899: return htbl.pub.huffval[(code + htbl.valoffset[l])] & 0xFF;
3900: }
3901:
3902: static int decompress_onepass(jpeg_decompress_struct cinfo,
3903: byte[][][] output_buf, int[] output_buf_offset) {
3904: jpeg_d_coef_controller coef = cinfo.coef;
3905: int MCU_col_num; /* index of current MCU within row */
3906: int last_MCU_col = cinfo.MCUs_per_row - 1;
3907: int last_iMCU_row = cinfo.total_iMCU_rows - 1;
3908: int blkn, ci, xindex, yindex, yoffset, useful_width;
3909: byte[][] output_ptr;
3910: int start_col, output_col;
3911: jpeg_component_info compptr;
3912: // inverse_DCT_method_ptr inverse_DCT;
3913:
3914: /* Loop to process as much as one whole iMCU row */
3915: for (yoffset = coef.MCU_vert_offset; yoffset < coef.MCU_rows_per_iMCU_row; yoffset++) {
3916: for (MCU_col_num = coef.MCU_ctr; MCU_col_num <= last_MCU_col; MCU_col_num++) {
3917: /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
3918: for (int i = 0; i < cinfo.blocks_in_MCU; i++) {
3919: short[] blk = coef.MCU_buffer[i];
3920: for (int j = 0; j < blk.length; j++) {
3921: blk[j] = 0;
3922: }
3923: }
3924: if (!cinfo.entropy.decode_mcu(cinfo, coef.MCU_buffer)) {
3925: /* Suspension forced; update state counters and exit */
3926: coef.MCU_vert_offset = yoffset;
3927: coef.MCU_ctr = MCU_col_num;
3928: return JPEG_SUSPENDED;
3929: }
3930: /* Determine where data should go in output_buf and do the IDCT thing.
3931: * We skip dummy blocks at the right and bottom edges (but blkn gets
3932: * incremented past them!). Note the inner loop relies on having
3933: * allocated the MCU_buffer[] blocks sequentially.
3934: */
3935: blkn = 0; /* index of current DCT block within MCU */
3936: for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
3937: compptr = cinfo.cur_comp_info[ci];
3938: /* Don't bother to IDCT an uninteresting component. */
3939: if (!compptr.component_needed) {
3940: blkn += compptr.MCU_blocks;
3941: continue;
3942: }
3943: // inverse_DCT = cinfo.idct.inverse_DCT[compptr.component_index];
3944: useful_width = (MCU_col_num < last_MCU_col) ? compptr.MCU_width
3945: : compptr.last_col_width;
3946: output_ptr = output_buf[compptr.component_index];
3947: int output_ptr_offset = output_buf_offset[compptr.component_index]
3948: + yoffset * compptr.DCT_scaled_size;
3949: start_col = MCU_col_num * compptr.MCU_sample_width;
3950: for (yindex = 0; yindex < compptr.MCU_height; yindex++) {
3951: if (cinfo.input_iMCU_row < last_iMCU_row
3952: || yoffset + yindex < compptr.last_row_height) {
3953: output_col = start_col;
3954: for (xindex = 0; xindex < useful_width; xindex++) {
3955: jpeg_idct_islow(cinfo, compptr,
3956: coef.MCU_buffer[blkn + xindex],
3957: output_ptr, output_ptr_offset,
3958: output_col);
3959: output_col += compptr.DCT_scaled_size;
3960: }
3961: }
3962: blkn += compptr.MCU_width;
3963: output_ptr_offset += compptr.DCT_scaled_size;
3964: }
3965: }
3966: }
3967: /* Completed an MCU row, but perhaps not an iMCU row */
3968: coef.MCU_ctr = 0;
3969: }
3970: /* Completed the iMCU row, advance counters for next one */
3971: cinfo.output_iMCU_row++;
3972: if (++(cinfo.input_iMCU_row) < cinfo.total_iMCU_rows) {
3973: coef.start_iMCU_row(cinfo);
3974: return JPEG_ROW_COMPLETED;
3975: }
3976: /* Completed the scan */
3977: finish_input_pass(cinfo);
3978: return JPEG_SCAN_COMPLETED;
3979: }
3980:
3981: static int decompress_smooth_data(jpeg_decompress_struct cinfo,
3982: byte[][][] output_buf, int[] output_buf_offset) {
3983: jpeg_d_coef_controller coef = cinfo.coef;
3984: int last_iMCU_row = cinfo.total_iMCU_rows - 1;
3985: int block_num, last_block_column;
3986: int ci, block_row, block_rows, access_rows;
3987: short[][][] buffer;
3988: short[][] buffer_ptr, prev_block_row, next_block_row;
3989: byte[][] output_ptr;
3990: int output_col;
3991: jpeg_component_info compptr;
3992: // inverse_DCT_method_ptr inverse_DCT;
3993: boolean first_row, last_row;
3994: short[] workspace = coef.workspace;
3995: if (workspace == null)
3996: workspace = coef.workspace = new short[DCTSIZE2];
3997: int[] coef_bits;
3998: JQUANT_TBL quanttbl;
3999: int Q00, Q01, Q02, Q10, Q11, Q20, num;
4000: int DC1, DC2, DC3, DC4, DC5, DC6, DC7, DC8, DC9;
4001: int Al, pred;
4002:
4003: /* Force some input to be done if we are getting ahead of the input. */
4004: while (cinfo.input_scan_number <= cinfo.output_scan_number
4005: && !cinfo.inputctl.eoi_reached) {
4006: if (cinfo.input_scan_number == cinfo.output_scan_number) {
4007: /* If input is working on current scan, we ordinarily want it to
4008: * have completed the current row. But if input scan is DC,
4009: * we want it to keep one row ahead so that next block row's DC
4010: * values are up to date.
4011: */
4012: int delta = (cinfo.Ss == 0) ? 1 : 0;
4013: if (cinfo.input_iMCU_row > cinfo.output_iMCU_row
4014: + delta)
4015: break;
4016: }
4017: if (consume_input(cinfo) == JPEG_SUSPENDED)
4018: return JPEG_SUSPENDED;
4019: }
4020:
4021: /* OK, output from the virtual arrays. */
4022: for (ci = 0; ci < cinfo.num_components; ci++) {
4023: compptr = cinfo.comp_info[ci];
4024: /* Don't bother to IDCT an uninteresting component. */
4025: if (!compptr.component_needed)
4026: continue;
4027: /* Count non-dummy DCT block rows in this iMCU row. */
4028: if (cinfo.output_iMCU_row < last_iMCU_row) {
4029: block_rows = compptr.v_samp_factor;
4030: access_rows = block_rows * 2; /* this and next iMCU row */
4031: last_row = false;
4032: } else {
4033: /* NB: can't use last_row_height here; it is input-side-dependent! */
4034: block_rows = (compptr.height_in_blocks % compptr.v_samp_factor);
4035: if (block_rows == 0)
4036: block_rows = compptr.v_samp_factor;
4037: access_rows = block_rows; /* this iMCU row only */
4038: last_row = true;
4039: }
4040: /* Align the virtual buffer for this component. */
4041: int buffer_offset;
4042: if (cinfo.output_iMCU_row > 0) {
4043: access_rows += compptr.v_samp_factor; /* prior iMCU row too */
4044: buffer = coef.whole_image[ci];
4045: buffer_offset = (cinfo.output_iMCU_row - 1)
4046: * compptr.v_samp_factor;
4047: buffer_offset += compptr.v_samp_factor; /* point to current iMCU row */
4048: first_row = false;
4049: } else {
4050: buffer = coef.whole_image[ci];
4051: buffer_offset = 0;
4052: first_row = true;
4053: }
4054: /* Fetch component-dependent info */
4055: coef_bits = coef.coef_bits_latch;
4056: int coef_offset = (ci * SAVED_COEFS);
4057: quanttbl = compptr.quant_table;
4058: Q00 = quanttbl.quantval[0];
4059: Q01 = quanttbl.quantval[Q01_POS];
4060: Q10 = quanttbl.quantval[Q10_POS];
4061: Q20 = quanttbl.quantval[Q20_POS];
4062: Q11 = quanttbl.quantval[Q11_POS];
4063: Q02 = quanttbl.quantval[Q02_POS];
4064: // inverse_DCT = cinfo.idct.inverse_DCT[ci];
4065: output_ptr = output_buf[ci];
4066: int output_ptr_offset = output_buf_offset[ci];
4067: /* Loop over all DCT blocks to be processed. */
4068: for (block_row = 0; block_row < block_rows; block_row++) {
4069: buffer_ptr = buffer[block_row + buffer_offset];
4070: int buffer_ptr_offset = 0, prev_block_row_offset = 0, next_block_row_offset = 0;
4071: if (first_row && block_row == 0) {
4072: prev_block_row = buffer_ptr;
4073: prev_block_row_offset = buffer_ptr_offset;
4074: } else {
4075: prev_block_row = buffer[block_row - 1
4076: + buffer_offset];
4077: prev_block_row_offset = 0;
4078: }
4079: if (last_row && block_row == block_rows - 1) {
4080: next_block_row = buffer_ptr;
4081: next_block_row_offset = buffer_ptr_offset;
4082: } else {
4083: next_block_row = buffer[block_row + 1
4084: + buffer_offset];
4085: next_block_row_offset = 0;
4086: }
4087: /* We fetch the surrounding DC values using a sliding-register approach.
4088: * Initialize all nine here so as to do the right thing on narrow pics.
4089: */
4090: DC1 = DC2 = DC3 = prev_block_row[0 + prev_block_row_offset][0];
4091: DC4 = DC5 = DC6 = buffer_ptr[0 + buffer_ptr_offset][0];
4092: DC7 = DC8 = DC9 = next_block_row[0 + next_block_row_offset][0];
4093: output_col = 0;
4094: last_block_column = compptr.width_in_blocks - 1;
4095: for (block_num = 0; block_num <= last_block_column; block_num++) {
4096: /* Fetch current DCT block into workspace so we can modify it. */
4097: // jcopy_block_row(buffer_ptr, workspace, 1);
4098: System.arraycopy(buffer_ptr[buffer_ptr_offset], 0,
4099: workspace, 0, workspace.length);
4100: /* Update DC values */
4101: if (block_num < last_block_column) {
4102: DC3 = prev_block_row[1 + prev_block_row_offset][0];
4103: DC6 = buffer_ptr[1 + buffer_ptr_offset][0];
4104: DC9 = next_block_row[1 + next_block_row_offset][0];
4105: }
4106: /* Compute coefficient estimates per K.8.
4107: * An estimate is applied only if coefficient is still zero,
4108: * and is not known to be fully accurate.
4109: */
4110: /* AC01 */
4111: if ((Al = coef_bits[1 + coef_offset]) != 0
4112: && workspace[1] == 0) {
4113: num = 36 * Q00 * (DC4 - DC6);
4114: if (num >= 0) {
4115: pred = (((Q01 << 7) + num) / (Q01 << 8));
4116: if (Al > 0 && pred >= (1 << Al))
4117: pred = (1 << Al) - 1;
4118: } else {
4119: pred = (((Q01 << 7) - num) / (Q01 << 8));
4120: if (Al > 0 && pred >= (1 << Al))
4121: pred = (1 << Al) - 1;
4122: pred = -pred;
4123: }
4124: workspace[1] = (short) pred;
4125: }
4126: /* AC10 */
4127: if ((Al = coef_bits[2 + coef_offset]) != 0
4128: && workspace[8] == 0) {
4129: num = 36 * Q00 * (DC2 - DC8);
4130: if (num >= 0) {
4131: pred = (((Q10 << 7) + num) / (Q10 << 8));
4132: if (Al > 0 && pred >= (1 << Al))
4133: pred = (1 << Al) - 1;
4134: } else {
4135: pred = (((Q10 << 7) - num) / (Q10 << 8));
4136: if (Al > 0 && pred >= (1 << Al))
4137: pred = (1 << Al) - 1;
4138: pred = -pred;
4139: }
4140: workspace[8] = (short) pred;
4141: }
4142: /* AC20 */
4143: if ((Al = coef_bits[3 + coef_offset]) != 0
4144: && workspace[16] == 0) {
4145: num = 9 * Q00 * (DC2 + DC8 - 2 * DC5);
4146: if (num >= 0) {
4147: pred = (((Q20 << 7) + num) / (Q20 << 8));
4148: if (Al > 0 && pred >= (1 << Al))
4149: pred = (1 << Al) - 1;
4150: } else {
4151: pred = (((Q20 << 7) - num) / (Q20 << 8));
4152: if (Al > 0 && pred >= (1 << Al))
4153: pred = (1 << Al) - 1;
4154: pred = -pred;
4155: }
4156: workspace[16] = (short) pred;
4157: }
4158: /* AC11 */
4159: if ((Al = coef_bits[4 + coef_offset]) != 0
4160: && workspace[9] == 0) {
4161: num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
4162: if (num >= 0) {
4163: pred = (((Q11 << 7) + num) / (Q11 << 8));
4164: if (Al > 0 && pred >= (1 << Al))
4165: pred = (1 << Al) - 1;
4166: } else {
4167: pred = (((Q11 << 7) - num) / (Q11 << 8));
4168: if (Al > 0 && pred >= (1 << Al))
4169: pred = (1 << Al) - 1;
4170: pred = -pred;
4171: }
4172: workspace[9] = (short) pred;
4173: }
4174: /* AC02 */
4175: if ((Al = coef_bits[5 + coef_offset]) != 0
4176: && workspace[2] == 0) {
4177: num = 9 * Q00 * (DC4 + DC6 - 2 * DC5);
4178: if (num >= 0) {
4179: pred = (((Q02 << 7) + num) / (Q02 << 8));
4180: if (Al > 0 && pred >= (1 << Al))
4181: pred = (1 << Al) - 1;
4182: } else {
4183: pred = (((Q02 << 7) - num) / (Q02 << 8));
4184: if (Al > 0 && pred >= (1 << Al))
4185: pred = (1 << Al) - 1;
4186: pred = -pred;
4187: }
4188: workspace[2] = (short) pred;
4189: }
4190: /* OK, do the IDCT */
4191: jpeg_idct_islow(cinfo, compptr, workspace,
4192: output_ptr, output_ptr_offset, output_col);
4193: /* Advance for next column */
4194: DC1 = DC2;
4195: DC2 = DC3;
4196: DC4 = DC5;
4197: DC5 = DC6;
4198: DC7 = DC8;
4199: DC8 = DC9;
4200: buffer_ptr_offset++;
4201: prev_block_row_offset++;
4202: next_block_row_offset++;
4203: output_col += compptr.DCT_scaled_size;
4204: }
4205: output_ptr_offset += compptr.DCT_scaled_size;
4206: }
4207: }
4208:
4209: if (++(cinfo.output_iMCU_row) < cinfo.total_iMCU_rows)
4210: return JPEG_ROW_COMPLETED;
4211: return JPEG_SCAN_COMPLETED;
4212: }
4213:
4214: static int decompress_data(jpeg_decompress_struct cinfo,
4215: byte[][][] output_buf, int[] output_buf_offset) {
4216: jpeg_d_coef_controller coef = cinfo.coef;
4217: int last_iMCU_row = cinfo.total_iMCU_rows - 1;
4218: int block_num;
4219: int ci, block_row, block_rows;
4220: short[][][] buffer;
4221: short[][] buffer_ptr;
4222: byte[][] output_ptr;
4223: int output_col;
4224: jpeg_component_info compptr;
4225: // inverse_DCT_method_ptr inverse_DCT;
4226:
4227: /* Force some input to be done if we are getting ahead of the input. */
4228: while (cinfo.input_scan_number < cinfo.output_scan_number
4229: || (cinfo.input_scan_number == cinfo.output_scan_number && cinfo.input_iMCU_row <= cinfo.output_iMCU_row)) {
4230: if (consume_input(cinfo) == JPEG_SUSPENDED)
4231: return JPEG_SUSPENDED;
4232: }
4233:
4234: /* OK, output from the virtual arrays. */
4235: for (ci = 0; ci < cinfo.num_components; ci++) {
4236: compptr = cinfo.comp_info[ci];
4237: /* Don't bother to IDCT an uninteresting component. */
4238: if (!compptr.component_needed)
4239: continue;
4240: /* Align the virtual buffer for this component. */
4241: buffer = coef.whole_image[ci];
4242: int buffer_offset = cinfo.output_iMCU_row
4243: * compptr.v_samp_factor;
4244: /* Count non-dummy DCT block rows in this iMCU row. */
4245: if (cinfo.output_iMCU_row < last_iMCU_row)
4246: block_rows = compptr.v_samp_factor;
4247: else {
4248: /* NB: can't use last_row_height here; it is input-side-dependent! */
4249: block_rows = (compptr.height_in_blocks % compptr.v_samp_factor);
4250: if (block_rows == 0)
4251: block_rows = compptr.v_samp_factor;
4252: }
4253: // inverse_DCT = cinfo.idct.inverse_DCT[ci];
4254: output_ptr = output_buf[ci];
4255: int output_ptr_offset = output_buf_offset[ci];
4256: /* Loop over all DCT blocks to be processed. */
4257: for (block_row = 0; block_row < block_rows; block_row++) {
4258: buffer_ptr = buffer[block_row + buffer_offset];
4259: int buffer_ptr_offset = 0;
4260: output_col = 0;
4261: for (block_num = 0; block_num < compptr.width_in_blocks; block_num++) {
4262: jpeg_idct_islow(cinfo, compptr,
4263: buffer_ptr[buffer_ptr_offset], output_ptr,
4264: output_ptr_offset, output_col);
4265:
4266: buffer_ptr_offset++;
4267: output_col += compptr.DCT_scaled_size;
4268: }
4269: output_ptr_offset += compptr.DCT_scaled_size;
4270: }
4271: }
4272:
4273: if (++(cinfo.output_iMCU_row) < cinfo.total_iMCU_rows)
4274: return JPEG_ROW_COMPLETED;
4275: return JPEG_SCAN_COMPLETED;
4276: }
4277:
4278: static void post_process_data(jpeg_decompress_struct cinfo,
4279: byte[][][] input_buf, int[] input_buf_offset,
4280: int[] in_row_group_ctr, int in_row_groups_avail,
4281: byte[][] output_buf, int[] out_row_ctr, int out_rows_avail) {
4282: upsample(cinfo, input_buf, input_buf_offset, in_row_group_ctr,
4283: in_row_groups_avail, output_buf, out_row_ctr,
4284: out_rows_avail);
4285: }
4286:
4287: static void set_bottom_pointers(jpeg_decompress_struct cinfo)
4288: /* Change the pointer lists to duplicate the last sample row at the bottom
4289: * of the image. whichptr indicates which xbuffer holds the final iMCU row.
4290: * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
4291: */
4292: {
4293: jpeg_d_main_controller main = cinfo.main;
4294: int ci, i, rgroup, iMCUheight, rows_left;
4295: jpeg_component_info compptr;
4296: byte[][] xbuf;
4297:
4298: for (ci = 0; ci < cinfo.num_components; ci++) {
4299: compptr = cinfo.comp_info[ci];
4300: /* Count sample rows in one iMCU row and in one row group */
4301: iMCUheight = compptr.v_samp_factor
4302: * compptr.DCT_scaled_size;
4303: rgroup = iMCUheight / cinfo.min_DCT_scaled_size;
4304: /* Count nondummy sample rows remaining for this component */
4305: rows_left = (compptr.downsampled_height % iMCUheight);
4306: if (rows_left == 0)
4307: rows_left = iMCUheight;
4308: /* Count nondummy row groups. Should get same answer for each component,
4309: * so we need only do it once.
4310: */
4311: if (ci == 0) {
4312: main.rowgroups_avail = ((rows_left - 1) / rgroup + 1);
4313: }
4314: /* Duplicate the last real sample row rgroup*2 times; this pads out the
4315: * last partial rowgroup and ensures at least one full rowgroup of context.
4316: */
4317: xbuf = main.xbuffer[main.whichptr][ci];
4318: int xbuf_offset = main.xbuffer_offset[main.whichptr][ci];
4319: for (i = 0; i < rgroup * 2; i++) {
4320: xbuf[rows_left + i + xbuf_offset] = xbuf[rows_left - 1
4321: + xbuf_offset];
4322: }
4323: }
4324: }
4325:
4326: static void set_wraparound_pointers(jpeg_decompress_struct cinfo)
4327: /* Set up the "wraparound" pointers at top and bottom of the pointer lists.
4328: * This changes the pointer list state from top-of-image to the normal state.
4329: */
4330: {
4331: jpeg_d_main_controller main = cinfo.main;
4332: int ci, i, rgroup;
4333: int M = cinfo.min_DCT_scaled_size;
4334: jpeg_component_info compptr;
4335: byte[][] xbuf0, xbuf1;
4336:
4337: for (ci = 0; ci < cinfo.num_components; ci++) {
4338: compptr = cinfo.comp_info[ci];
4339: rgroup = (compptr.v_samp_factor * compptr.DCT_scaled_size)
4340: / cinfo.min_DCT_scaled_size; /* height of a row group of component */
4341: xbuf0 = main.xbuffer[0][ci];
4342: int xbuf0_offset = main.xbuffer_offset[0][ci];
4343: xbuf1 = main.xbuffer[1][ci];
4344: int xbuf1_offset = main.xbuffer_offset[1][ci];
4345: for (i = 0; i < rgroup; i++) {
4346: xbuf0[i - rgroup + xbuf0_offset] = xbuf0[rgroup
4347: * (M + 1) + i + xbuf0_offset];
4348: xbuf1[i - rgroup + xbuf1_offset] = xbuf1[rgroup
4349: * (M + 1) + i + xbuf1_offset];
4350: xbuf0[rgroup * (M + 2) + i + xbuf0_offset] = xbuf0[i
4351: + xbuf0_offset];
4352: xbuf1[rgroup * (M + 2) + i + xbuf1_offset] = xbuf1[i
4353: + xbuf1_offset];
4354: }
4355: }
4356: }
4357:
4358: static void process_data_crank_post(jpeg_decompress_struct cinfo,
4359: byte[][] output_buf, int[] out_row_ctr, int out_rows_avail) {
4360: error();
4361: }
4362:
4363: static void process_data_context_main(jpeg_decompress_struct cinfo,
4364: byte[][] output_buf, int[] out_row_ctr, int out_rows_avail) {
4365: jpeg_d_main_controller main = cinfo.main;
4366:
4367: /* Read input data if we haven't filled the main buffer yet */
4368: if (!main.buffer_full) {
4369: int result;
4370: switch (cinfo.coef.decompress_data) {
4371: case DECOMPRESS_DATA:
4372: result = decompress_data(cinfo,
4373: main.xbuffer[main.whichptr],
4374: main.xbuffer_offset[main.whichptr]);
4375: break;
4376: case DECOMPRESS_SMOOTH_DATA:
4377: result = decompress_smooth_data(cinfo,
4378: main.xbuffer[main.whichptr],
4379: main.xbuffer_offset[main.whichptr]);
4380: break;
4381: case DECOMPRESS_ONEPASS:
4382: result = decompress_onepass(cinfo,
4383: main.xbuffer[main.whichptr],
4384: main.xbuffer_offset[main.whichptr]);
4385: break;
4386: default:
4387: result = 0;
4388: }
4389: if (result == 0)
4390: return; /* suspension forced, can do nothing more */
4391: main.buffer_full = true; /* OK, we have an iMCU row to work with */
4392: main.iMCU_row_ctr++; /* count rows received */
4393: }
4394:
4395: /* Postprocessor typically will not swallow all the input data it is handed
4396: * in one call (due to filling the output buffer first). Must be prepared
4397: * to exit and restart. This switch lets us keep track of how far we got.
4398: * Note that each case falls through to the next on successful completion.
4399: */
4400: switch (main.context_state) {
4401: case CTX_POSTPONED_ROW:
4402: /* Call postprocessor using previously set pointers for postponed row */
4403: post_process_data(cinfo, main.xbuffer[main.whichptr],
4404: main.xbuffer_offset[main.whichptr],
4405: main.rowgroup_ctr, main.rowgroups_avail,
4406: output_buf, out_row_ctr, out_rows_avail);
4407: if (main.rowgroup_ctr[0] < main.rowgroups_avail)
4408: return; /* Need to suspend */
4409: main.context_state = CTX_PREPARE_FOR_IMCU;
4410: if (out_row_ctr[0] >= out_rows_avail)
4411: return; /* Postprocessor exactly filled output buf */
4412: /*FALLTHROUGH*/
4413: case CTX_PREPARE_FOR_IMCU:
4414: /* Prepare to process first M-1 row groups of this iMCU row */
4415: main.rowgroup_ctr[0] = 0;
4416: main.rowgroups_avail = (cinfo.min_DCT_scaled_size - 1);
4417: /* Check for bottom of image: if so, tweak pointers to "duplicate"
4418: * the last sample row, and adjust rowgroups_avail to ignore padding rows.
4419: */
4420: if (main.iMCU_row_ctr == cinfo.total_iMCU_rows)
4421: set_bottom_pointers(cinfo);
4422: main.context_state = CTX_PROCESS_IMCU;
4423: /*FALLTHROUGH*/
4424: case CTX_PROCESS_IMCU:
4425: /* Call postprocessor using previously set pointers */
4426: post_process_data(cinfo, main.xbuffer[main.whichptr],
4427: main.xbuffer_offset[main.whichptr],
4428: main.rowgroup_ctr, main.rowgroups_avail,
4429: output_buf, out_row_ctr, out_rows_avail);
4430: if (main.rowgroup_ctr[0] < main.rowgroups_avail)
4431: return; /* Need to suspend */
4432: /* After the first iMCU, change wraparound pointers to normal state */
4433: if (main.iMCU_row_ctr == 1)
4434: set_wraparound_pointers(cinfo);
4435: /* Prepare to load new iMCU row using other xbuffer list */
4436: main.whichptr ^= 1; /* 0=>1 or 1=>0 */
4437: main.buffer_full = false;
4438: /* Still need to process last row group of this iMCU row, */
4439: /* which is saved at index M+1 of the other xbuffer */
4440: main.rowgroup_ctr[0] = (cinfo.min_DCT_scaled_size + 1);
4441: main.rowgroups_avail = (cinfo.min_DCT_scaled_size + 2);
4442: main.context_state = CTX_POSTPONED_ROW;
4443: }
4444: }
4445:
4446: static void process_data_simple_main(jpeg_decompress_struct cinfo,
4447: byte[][] output_buf, int[] out_row_ctr, int out_rows_avail) {
4448: jpeg_d_main_controller main = cinfo.main;
4449: int rowgroups_avail;
4450:
4451: /* Read input data if we haven't filled the main buffer yet */
4452: if (!main.buffer_full) {
4453: int result;
4454: switch (cinfo.coef.decompress_data) {
4455: case DECOMPRESS_DATA:
4456: result = decompress_data(cinfo, main.buffer,
4457: main.buffer_offset);
4458: break;
4459: case DECOMPRESS_SMOOTH_DATA:
4460: result = decompress_smooth_data(cinfo, main.buffer,
4461: main.buffer_offset);
4462: break;
4463: case DECOMPRESS_ONEPASS:
4464: result = decompress_onepass(cinfo, main.buffer,
4465: main.buffer_offset);
4466: break;
4467: default:
4468: result = 0;
4469: }
4470: if (result == 0)
4471: return; /* suspension forced, can do nothing more */
4472: main.buffer_full = true; /* OK, we have an iMCU row to work with */
4473: }
4474:
4475: /* There are always min_DCT_scaled_size row groups in an iMCU row. */
4476: rowgroups_avail = cinfo.min_DCT_scaled_size;
4477: /* Note: at the bottom of the image, we may pass extra garbage row groups
4478: * to the postprocessor. The postprocessor has to check for bottom
4479: * of image anyway (at row resolution), so no point in us doing it too.
4480: */
4481:
4482: /* Feed the postprocessor */
4483: post_process_data(cinfo, main.buffer, main.buffer_offset,
4484: main.rowgroup_ctr, rowgroups_avail, output_buf,
4485: out_row_ctr, out_rows_avail);
4486:
4487: /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
4488: if (main.rowgroup_ctr[0] >= rowgroups_avail) {
4489: main.buffer_full = false;
4490: main.rowgroup_ctr[0] = 0;
4491: }
4492: }
4493:
4494: static int jpeg_read_scanlines(jpeg_decompress_struct cinfo,
4495: byte[][] scanlines, int max_lines) {
4496:
4497: if (cinfo.global_state != DSTATE_SCANNING)
4498: error();
4499: // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
4500: if (cinfo.output_scanline >= cinfo.output_height) {
4501: // WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
4502: return 0;
4503: }
4504:
4505: /* Call progress monitor hook if present */
4506: // if (cinfo.progress != NULL) {
4507: // cinfo.progress.pass_counter = (long) cinfo.output_scanline;
4508: // cinfo.progress.pass_limit = (long) cinfo.output_height;
4509: // (*cinfo.progress.progress_monitor) ((j_common_ptr) cinfo);
4510: // }
4511: /* Process some data */
4512: cinfo.row_ctr[0] = 0;
4513: switch (cinfo.main.process_data) {
4514: case PROCESS_DATA_SIMPLE_MAIN:
4515: process_data_simple_main(cinfo, scanlines, cinfo.row_ctr,
4516: max_lines);
4517: break;
4518: case PROCESS_DATA_CONTEXT_MAIN:
4519: process_data_context_main(cinfo, scanlines, cinfo.row_ctr,
4520: max_lines);
4521: break;
4522: case PROCESS_DATA_CRANK_POST:
4523: process_data_crank_post(cinfo, scanlines, cinfo.row_ctr,
4524: max_lines);
4525: break;
4526: default:
4527: error();
4528: }
4529: cinfo.output_scanline += cinfo.row_ctr[0];
4530: return cinfo.row_ctr[0];
4531: }
4532:
4533: static boolean output_pass_setup(jpeg_decompress_struct cinfo) {
4534: if (cinfo.global_state != DSTATE_PRESCAN) {
4535: /* First call: do pass setup */
4536: prepare_for_output_pass(cinfo);
4537: cinfo.output_scanline = 0;
4538: cinfo.global_state = DSTATE_PRESCAN;
4539: }
4540: /* Loop over any required dummy passes */
4541: while (cinfo.master.is_dummy_pass) {
4542: error();
4543: //#ifdef QUANT_2PASS_SUPPORTED
4544: // /* Crank through the dummy pass */
4545: // while (cinfo.output_scanline < cinfo.output_height) {
4546: // JDIMENSION last_scanline;
4547: // /* Call progress monitor hook if present */
4548: // if (cinfo.progress != NULL) {
4549: // cinfo.progress.pass_counter = (long) cinfo.output_scanline;
4550: // cinfo.progress.pass_limit = (long) cinfo.output_height;
4551: // (*cinfo.progress.progress_monitor) ((j_common_ptr) cinfo);
4552: // }
4553: // /* Process some data */
4554: // last_scanline = cinfo.output_scanline;
4555: // (*cinfo.main.process_data) (cinfo, (JSAMPARRAY) NULL,
4556: // &cinfo.output_scanline, (JDIMENSION) 0);
4557: // if (cinfo.output_scanline == last_scanline)
4558: // return FALSE; /* No progress made, must suspend */
4559: // }
4560: // /* Finish up dummy pass, and set up for another one */
4561: // (*cinfo.master.finish_output_pass) (cinfo);
4562: // (*cinfo.master.prepare_for_output_pass) (cinfo);
4563: // cinfo.output_scanline = 0;
4564: //#else
4565: // ERREXIT(cinfo, JERR_NOT_COMPILED);
4566: //#endif /* QUANT_2PASS_SUPPORTED */
4567: }
4568: /* Ready for application to drive output pass through
4569: * jpeg_read_scanlines or jpeg_read_raw_data.
4570: */
4571: cinfo.global_state = cinfo.raw_data_out ? DSTATE_RAW_OK
4572: : DSTATE_SCANNING;
4573: return true;
4574: }
4575:
4576: static boolean get_dht(jpeg_decompress_struct cinfo)
4577: /* Process a DHT marker */
4578: {
4579: int length;
4580: byte[] bits = new byte[17];
4581: byte[] huffval = new byte[256];
4582: int i, index, count;
4583: JHUFF_TBL htblptr;
4584:
4585: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4586: fill_input_buffer(cinfo);
4587: length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4588: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4589: fill_input_buffer(cinfo);
4590: length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4591: length -= 2;
4592:
4593: while (length > 16) {
4594: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4595: fill_input_buffer(cinfo);
4596: index = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4597:
4598: // TRACEMS1(cinfo, 1, JTRC_DHT, index);
4599:
4600: bits[0] = 0;
4601: count = 0;
4602: for (i = 1; i <= 16; i++) {
4603: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4604: fill_input_buffer(cinfo);
4605: bits[i] = cinfo.buffer[cinfo.bytes_offset++];
4606: count += bits[i] & 0xFF;
4607: }
4608:
4609: length -= 1 + 16;
4610:
4611: // TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
4612: // bits[1], bits[2], bits[3], bits[4],
4613: // bits[5], bits[6], bits[7], bits[8]);
4614: // TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
4615: // bits[9], bits[10], bits[11], bits[12],
4616: // bits[13], bits[14], bits[15], bits[16]);
4617:
4618: /* Here we just do minimal validation of the counts to avoid walking
4619: * off the end of our table space. jdhuff.c will check more carefully.
4620: */
4621: if (count > 256 || (count) > length)
4622: error();
4623: // ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
4624:
4625: for (i = 0; i < count; i++) {
4626: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4627: fill_input_buffer(cinfo);
4628: huffval[i] = cinfo.buffer[cinfo.bytes_offset++];
4629: }
4630:
4631: length -= count;
4632:
4633: if ((index & 0x10) != 0) { /* AC table definition */
4634: index -= 0x10;
4635: htblptr = cinfo.ac_huff_tbl_ptrs[index] = new JHUFF_TBL();
4636: } else { /* DC table definition */
4637: htblptr = cinfo.dc_huff_tbl_ptrs[index] = new JHUFF_TBL();
4638: }
4639:
4640: if (index < 0 || index >= NUM_HUFF_TBLS)
4641: error();
4642: // ERREXIT1(cinfo, JERR_DHT_INDEX, index);
4643:
4644: System.arraycopy(bits, 0, htblptr.bits, 0, bits.length);
4645: System.arraycopy(huffval, 0, htblptr.huffval, 0,
4646: huffval.length);
4647: }
4648:
4649: if (length != 0)
4650: error();
4651: // ERREXIT(cinfo, JERR_BAD_LENGTH);
4652:
4653: return true;
4654: }
4655:
4656: static boolean get_dqt(jpeg_decompress_struct cinfo)
4657: /* Process a DQT marker */
4658: {
4659: int length;
4660: int n, i, prec;
4661: int tmp;
4662: JQUANT_TBL quant_ptr;
4663:
4664: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4665: fill_input_buffer(cinfo);
4666: length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4667: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4668: fill_input_buffer(cinfo);
4669: length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4670: length -= 2;
4671:
4672: while (length > 0) {
4673: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4674: fill_input_buffer(cinfo);
4675: n = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4676: prec = n >> 4;
4677: n &= 0x0F;
4678:
4679: // TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
4680:
4681: if (n >= NUM_QUANT_TBLS)
4682: error();
4683: // ERREXIT1(cinfo, JERR_DQT_INDEX, n);
4684:
4685: if (cinfo.quant_tbl_ptrs[n] == null)
4686: cinfo.quant_tbl_ptrs[n] = new JQUANT_TBL();
4687: quant_ptr = cinfo.quant_tbl_ptrs[n];
4688:
4689: for (i = 0; i < DCTSIZE2; i++) {
4690: if (prec != 0) {
4691: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4692: fill_input_buffer(cinfo);
4693: tmp = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4694: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4695: fill_input_buffer(cinfo);
4696: tmp |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4697: } else {
4698: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4699: fill_input_buffer(cinfo);
4700: tmp = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4701: }
4702: /* We convert the zigzag-order table to natural array order. */
4703: quant_ptr.quantval[jpeg_natural_order[i]] = (short) tmp;
4704: }
4705:
4706: // if (cinfo.err.trace_level >= 2) {
4707: // for (i = 0; i < DCTSIZE2; i += 8) {
4708: // TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
4709: // quant_ptr.quantval[i], quant_ptr.quantval[i+1],
4710: // quant_ptr.quantval[i+2], quant_ptr.quantval[i+3],
4711: // quant_ptr.quantval[i+4], quant_ptr.quantval[i+5],
4712: // quant_ptr.quantval[i+6], quant_ptr.quantval[i+7]);
4713: // }
4714: // }
4715:
4716: length -= (DCTSIZE2 + 1);
4717: if (prec != 0)
4718: length -= DCTSIZE2;
4719: }
4720:
4721: if (length != 0)
4722: error();
4723: // ERREXIT(cinfo, JERR_BAD_LENGTH);
4724:
4725: return true;
4726: }
4727:
4728: static boolean get_dri(jpeg_decompress_struct cinfo)
4729: /* Process a DRI marker */
4730: {
4731: int length;
4732: int tmp;
4733:
4734: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4735: fill_input_buffer(cinfo);
4736: length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4737: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4738: fill_input_buffer(cinfo);
4739: length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4740:
4741: if (length != 4)
4742: error();
4743: // ERREXIT(cinfo, JERR_BAD_LENGTH);
4744:
4745: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4746: fill_input_buffer(cinfo);
4747: tmp = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4748: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4749: fill_input_buffer(cinfo);
4750: tmp |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4751:
4752: // TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
4753:
4754: cinfo.restart_interval = tmp;
4755:
4756: return true;
4757: }
4758:
4759: static boolean get_dac(jpeg_decompress_struct cinfo)
4760: /* Process a DAC marker */
4761: {
4762: int length;
4763: int index, val;
4764: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4765: fill_input_buffer(cinfo);
4766: length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4767: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4768: fill_input_buffer(cinfo);
4769: length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4770: length -= 2;
4771:
4772: while (length > 0) {
4773: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4774: fill_input_buffer(cinfo);
4775: index = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4776: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4777: fill_input_buffer(cinfo);
4778: val = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4779:
4780: length -= 2;
4781:
4782: // TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
4783:
4784: if (index < 0 || index >= (2 * NUM_ARITH_TBLS))
4785: error();
4786: // ERREXIT1(cinfo, JERR_DAC_INDEX, index);
4787:
4788: if (index >= NUM_ARITH_TBLS) { /* define AC table */
4789: cinfo.arith_ac_K[index - NUM_ARITH_TBLS] = (byte) val;
4790: } else { /* define DC table */
4791: cinfo.arith_dc_L[index] = (byte) (val & 0x0F);
4792: cinfo.arith_dc_U[index] = (byte) (val >> 4);
4793: if (cinfo.arith_dc_L[index] > cinfo.arith_dc_U[index])
4794: error();
4795: // ERREXIT1(cinfo, JERR_DAC_VALUE, val);
4796: }
4797: }
4798:
4799: if (length != 0)
4800: error();
4801: // ERREXIT(cinfo, JERR_BAD_LENGTH);
4802:
4803: return true;
4804: }
4805:
4806: static boolean get_sos(jpeg_decompress_struct cinfo)
4807: /* Process a SOS marker */
4808: {
4809: int length;
4810: int i, ci, n, c, cc;
4811: jpeg_component_info compptr = null;
4812:
4813: if (!cinfo.marker.saw_SOF)
4814: error();
4815: // ERREXIT(cinfo, JERR_SOS_NO_SOF);
4816:
4817: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4818: fill_input_buffer(cinfo);
4819: length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4820: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4821: fill_input_buffer(cinfo);
4822: length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4823:
4824: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4825: fill_input_buffer(cinfo);
4826: n = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4827:
4828: // TRACEMS1(cinfo, 1, JTRC_SOS, n);
4829:
4830: if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
4831: error();
4832: // ERREXIT(cinfo, JERR_BAD_LENGTH);
4833:
4834: cinfo.comps_in_scan = n;
4835:
4836: /* Collect the component-spec parameters */
4837:
4838: for (i = 0; i < n; i++) {
4839: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4840: fill_input_buffer(cinfo);
4841: cc = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4842: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4843: fill_input_buffer(cinfo);
4844: c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4845:
4846: for (ci = 0; ci < cinfo.num_components; ci++) {
4847: compptr = cinfo.comp_info[ci];
4848: if (cc == compptr.component_id)
4849: break;
4850: }
4851:
4852: if (ci == cinfo.num_components)
4853: error();
4854: // ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
4855:
4856: cinfo.cur_comp_info[i] = compptr;
4857: compptr.dc_tbl_no = (c >> 4) & 15;
4858: compptr.ac_tbl_no = (c) & 15;
4859:
4860: // TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc, compptr.dc_tbl_no, compptr.ac_tbl_no);
4861: }
4862:
4863: /* Collect the additional scan parameters Ss, Se, Ah/Al. */
4864: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4865: fill_input_buffer(cinfo);
4866: c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4867: cinfo.Ss = c;
4868: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4869: fill_input_buffer(cinfo);
4870: c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4871: cinfo.Se = c;
4872: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4873: fill_input_buffer(cinfo);
4874: c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4875: cinfo.Ah = (c >> 4) & 15;
4876: cinfo.Al = (c) & 15;
4877:
4878: // TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo.Ss, cinfo.Se, cinfo.Ah, cinfo.Al);
4879:
4880: /* Prepare to scan data & restart markers */
4881: cinfo.marker.next_restart_num = 0;
4882:
4883: /* Count another SOS marker */
4884: cinfo.input_scan_number++;
4885:
4886: return true;
4887: }
4888:
4889: static boolean get_sof(jpeg_decompress_struct cinfo,
4890: boolean is_prog, boolean is_arith) {
4891: int length;
4892: int c, ci;
4893:
4894: cinfo.progressive_mode = is_prog;
4895: cinfo.arith_code = is_arith;
4896:
4897: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4898: fill_input_buffer(cinfo);
4899: length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4900: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4901: fill_input_buffer(cinfo);
4902: length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4903:
4904: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4905: fill_input_buffer(cinfo);
4906: cinfo.data_precision = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4907:
4908: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4909: fill_input_buffer(cinfo);
4910: cinfo.image_height = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4911: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4912: fill_input_buffer(cinfo);
4913: cinfo.image_height |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4914:
4915: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4916: fill_input_buffer(cinfo);
4917: cinfo.image_width = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
4918: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4919: fill_input_buffer(cinfo);
4920: cinfo.image_width |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4921:
4922: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4923: fill_input_buffer(cinfo);
4924: cinfo.num_components = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4925:
4926: length -= 8;
4927:
4928: // TRACEMS4(cinfo, 1, JTRC_SOF, cinfo.unread_marker,
4929: // (int) cinfo.image_width, (int) cinfo.image_height,
4930: // cinfo.num_components);
4931:
4932: if (cinfo.marker.saw_SOF)
4933: error();
4934: // ERREXIT(cinfo, JERR_SOF_DUPLICATE);
4935:
4936: /* We don't support files in which the image height is initially specified */
4937: /* as 0 and is later redefined by DNL. As long as we have to check that, */
4938: /* might as well have a general sanity check. */
4939: if (cinfo.image_height <= 0 || cinfo.image_width <= 0
4940: || cinfo.num_components <= 0)
4941: error();
4942: // ERREXIT(cinfo, JERR_EMPTY_IMAGE);
4943:
4944: if (length != (cinfo.num_components * 3))
4945: error();
4946: // ERREXIT(cinfo, JERR_BAD_LENGTH);
4947:
4948: if (cinfo.comp_info == null) /* do only once, even if suspend */
4949: cinfo.comp_info = new jpeg_component_info[cinfo.num_components];
4950:
4951: for (ci = 0; ci < cinfo.num_components; ci++) {
4952: jpeg_component_info compptr = cinfo.comp_info[ci] = new jpeg_component_info();
4953: compptr.component_index = ci;
4954: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4955: fill_input_buffer(cinfo);
4956: compptr.component_id = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4957: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4958: fill_input_buffer(cinfo);
4959: c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4960: compptr.h_samp_factor = (c >> 4) & 15;
4961: compptr.v_samp_factor = (c) & 15;
4962: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
4963: fill_input_buffer(cinfo);
4964: compptr.quant_tbl_no = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
4965:
4966: // TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
4967: // compptr.component_id, compptr.h_samp_factor,
4968: // compptr.v_samp_factor, compptr.quant_tbl_no);
4969: }
4970:
4971: cinfo.marker.saw_SOF = true;
4972:
4973: return true;
4974: }
4975:
4976: static void sep_upsample(jpeg_decompress_struct cinfo,
4977: byte[][][] input_buf, int[] input_buf_offset,
4978: int[] in_row_group_ctr, int in_row_groups_avail,
4979: byte[][] output_buf, int[] out_row_ctr, int out_rows_avail) {
4980: jpeg_upsampler upsample = cinfo.upsample;
4981: int ci;
4982: jpeg_component_info compptr;
4983: int num_rows;
4984:
4985: /* Fill the conversion buffer, if it's empty */
4986: if (upsample.next_row_out >= cinfo.max_v_samp_factor) {
4987: for (ci = 0; ci < cinfo.num_components; ci++) {
4988: compptr = cinfo.comp_info[ci];
4989: /* Invoke per-component upsample method. Notice we pass a POINTER
4990: * to color_buf[ci], so that fullsize_upsample can change it.
4991: */
4992: int offset = input_buf_offset[ci]
4993: + (in_row_group_ctr[0] * upsample.rowgroup_height[ci]);
4994: switch (upsample.methods[ci]) {
4995: case NOOP_UPSAMPLE:
4996: noop_upsample(cinfo, compptr, input_buf[ci],
4997: offset, upsample.color_buf,
4998: upsample.color_buf_offset, ci);
4999: break;
5000: case FULLSIZE_UPSAMPLE:
5001: fullsize_upsample(cinfo, compptr, input_buf[ci],
5002: offset, upsample.color_buf,
5003: upsample.color_buf_offset, ci);
5004: break;
5005: case H2V1_FANCY_UPSAMPLE:
5006: h2v1_fancy_upsample(cinfo, compptr, input_buf[ci],
5007: offset, upsample.color_buf,
5008: upsample.color_buf_offset, ci);
5009: break;
5010: case H2V1_UPSAMPLE:
5011: h2v1_upsample(cinfo, compptr, input_buf[ci],
5012: offset, upsample.color_buf,
5013: upsample.color_buf_offset, ci);
5014: break;
5015: case H2V2_FANCY_UPSAMPLE:
5016: h2v2_fancy_upsample(cinfo, compptr, input_buf[ci],
5017: offset, upsample.color_buf,
5018: upsample.color_buf_offset, ci);
5019: break;
5020: case H2V2_UPSAMPLE:
5021: h2v2_upsample(cinfo, compptr, input_buf[ci],
5022: offset, upsample.color_buf,
5023: upsample.color_buf_offset, ci);
5024: break;
5025: case INT_UPSAMPLE:
5026: int_upsample(cinfo, compptr, input_buf[ci], offset,
5027: upsample.color_buf,
5028: upsample.color_buf_offset, ci);
5029: break;
5030: }
5031: }
5032: upsample.next_row_out = 0;
5033: }
5034:
5035: /* Color-convert and emit rows */
5036:
5037: /* How many we have in the buffer: */
5038: num_rows = (cinfo.max_v_samp_factor - upsample.next_row_out);
5039: /* Not more than the distance to the end of the image. Need this test
5040: * in case the image height is not a multiple of max_v_samp_factor:
5041: */
5042: if (num_rows > upsample.rows_to_go)
5043: num_rows = upsample.rows_to_go;
5044: /* And not more than what the client can accept: */
5045: out_rows_avail -= out_row_ctr[0];
5046: if (num_rows > out_rows_avail)
5047: num_rows = out_rows_avail;
5048:
5049: switch (cinfo.cconvert.color_convert) {
5050: case NULL_CONVERT:
5051: null_convert(cinfo, upsample.color_buf,
5052: upsample.color_buf_offset, upsample.next_row_out,
5053: output_buf, out_row_ctr[0], num_rows);
5054: break;
5055: case GRAYSCALE_CONVERT:
5056: grayscale_convert(cinfo, upsample.color_buf,
5057: upsample.color_buf_offset, upsample.next_row_out,
5058: output_buf, out_row_ctr[0], num_rows);
5059: break;
5060: case YCC_RGB_CONVERT:
5061: ycc_rgb_convert(cinfo, upsample.color_buf,
5062: upsample.color_buf_offset, upsample.next_row_out,
5063: output_buf, out_row_ctr[0], num_rows);
5064: break;
5065: case GRAY_RGB_CONVERT:
5066: gray_rgb_convert(cinfo, upsample.color_buf,
5067: upsample.color_buf_offset, upsample.next_row_out,
5068: output_buf, out_row_ctr[0], num_rows);
5069: break;
5070: case YCCK_CMYK_CONVERT:
5071: error();
5072: break;
5073: }
5074:
5075: /* Adjust counts */
5076: out_row_ctr[0] += num_rows;
5077: upsample.rows_to_go -= num_rows;
5078: upsample.next_row_out += num_rows;
5079: /* When the buffer is emptied, declare this input row group consumed */
5080: if (upsample.next_row_out >= cinfo.max_v_samp_factor) {
5081: in_row_group_ctr[0]++;
5082: }
5083: }
5084:
5085: static void noop_upsample(jpeg_decompress_struct cinfo,
5086: jpeg_component_info compptr, byte[][] input_data,
5087: int input_data_offset, byte[][][] output_data_ptr,
5088: int[] output_data_offset, int output_data_index) {
5089: output_data_ptr[output_data_index] = null; /* safety check */
5090: }
5091:
5092: static void fullsize_upsample(jpeg_decompress_struct cinfo,
5093: jpeg_component_info compptr, byte[][] input_data,
5094: int input_data_offset, byte[][][] output_data_ptr,
5095: int[] output_data_offset, int output_data_index) {
5096: output_data_ptr[output_data_index] = input_data;
5097: output_data_offset[output_data_index] = input_data_offset;
5098: }
5099:
5100: static void h2v1_upsample(jpeg_decompress_struct cinfo,
5101: jpeg_component_info compptr, byte[][] input_data,
5102: int input_data_offset, byte[][][] output_data_ptr,
5103: int[] output_data_offset, int output_data_index) {
5104: byte[][] output_data = output_data_ptr[output_data_index];
5105: byte[] inptr, outptr;
5106: byte invalue;
5107: int outend;
5108: int inrow;
5109: output_data_offset[output_data_index] = 0;
5110:
5111: for (inrow = 0; inrow < cinfo.max_v_samp_factor; inrow++) {
5112: inptr = input_data[inrow + input_data_offset];
5113: outptr = output_data[inrow];
5114: int inptr_offset = 0, outptr_offset = 0;
5115: outend = outptr_offset + cinfo.output_width;
5116: while (outptr_offset < outend) {
5117: invalue = inptr[inptr_offset++]; /* don't need GETJSAMPLE() here */
5118: outptr[outptr_offset++] = invalue;
5119: outptr[outptr_offset++] = invalue;
5120: }
5121: }
5122: }
5123:
5124: static void h2v2_upsample(jpeg_decompress_struct cinfo,
5125: jpeg_component_info compptr, byte[][] input_data,
5126: int input_data_offset, byte[][][] output_data_ptr,
5127: int[] output_data_offset, int output_data_index) {
5128: byte[][] output_data = output_data_ptr[output_data_index];
5129: byte[] inptr, outptr;
5130: byte invalue;
5131: int outend;
5132: int inrow, outrow;
5133: output_data_offset[output_data_index] = 0;
5134:
5135: inrow = outrow = 0;
5136: while (outrow < cinfo.max_v_samp_factor) {
5137: inptr = input_data[inrow + input_data_offset];
5138: outptr = output_data[outrow];
5139: int inptr_offset = 0, outptr_offset = 0;
5140: outend = outptr_offset + cinfo.output_width;
5141: while (outptr_offset < outend) {
5142: invalue = inptr[inptr_offset++]; /* don't need GETJSAMPLE() here */
5143: outptr[outptr_offset++] = invalue;
5144: outptr[outptr_offset++] = invalue;
5145: }
5146: jcopy_sample_rows(output_data, outrow, output_data,
5147: outrow + 1, 1, cinfo.output_width);
5148: inrow++;
5149: outrow += 2;
5150: }
5151: }
5152:
5153: static void h2v1_fancy_upsample(jpeg_decompress_struct cinfo,
5154: jpeg_component_info compptr, byte[][] input_data,
5155: int input_data_offset, byte[][][] output_data_ptr,
5156: int[] output_data_offset, int output_data_index) {
5157: byte[][] output_data = output_data_ptr[output_data_index];
5158: byte[] inptr, outptr;
5159: int invalue;
5160: int colctr;
5161: int inrow;
5162: output_data_offset[output_data_index] = 0;
5163:
5164: for (inrow = 0; inrow < cinfo.max_v_samp_factor; inrow++) {
5165: inptr = input_data[inrow + input_data_offset];
5166: outptr = output_data[inrow];
5167: int inptr_offset = 0, outptr_offset = 0;
5168: /* Special case for first column */
5169: invalue = inptr[inptr_offset++] & 0xFF;
5170: outptr[outptr_offset++] = (byte) invalue;
5171: outptr[outptr_offset++] = (byte) ((invalue * 3
5172: + (inptr[inptr_offset] & 0xFF) + 2) >> 2);
5173:
5174: for (colctr = compptr.downsampled_width - 2; colctr > 0; colctr--) {
5175: /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
5176: invalue = (inptr[inptr_offset++] & 0xFF) * 3;
5177: outptr[outptr_offset++] = (byte) ((invalue
5178: + (inptr[inptr_offset - 2] & 0xFF) + 1) >> 2);
5179: outptr[outptr_offset++] = (byte) ((invalue
5180: + (inptr[inptr_offset] & 0xFF) + 2) >> 2);
5181: }
5182:
5183: /* Special case for last column */
5184: invalue = (inptr[inptr_offset] & 0xFF);
5185: outptr[outptr_offset++] = (byte) ((invalue * 3
5186: + (inptr[inptr_offset - 1] & 0xFF) + 1) >> 2);
5187: outptr[outptr_offset++] = (byte) invalue;
5188: }
5189: }
5190:
5191: static void h2v2_fancy_upsample(jpeg_decompress_struct cinfo,
5192: jpeg_component_info compptr, byte[][] input_data,
5193: int input_data_offset, byte[][][] output_data_ptr,
5194: int[] output_data_offset, int output_data_index) {
5195: byte[][] output_data = output_data_ptr[output_data_index];
5196: byte[] inptr0, inptr1, outptr;
5197: int this colsum, lastcolsum, nextcolsum;
5198: int colctr;
5199: int inrow, outrow, v;
5200: output_data_offset[output_data_index] = 0;
5201:
5202: inrow = outrow = 0;
5203: while (outrow < cinfo.max_v_samp_factor) {
5204: for (v = 0; v < 2; v++) {
5205: /* inptr0 points to nearest input row, inptr1 points to next nearest */
5206: inptr0 = input_data[inrow + input_data_offset];
5207: if (v == 0) /* next nearest is row above */
5208: inptr1 = input_data[inrow - 1 + input_data_offset];
5209: else
5210: /* next nearest is row below */
5211: inptr1 = input_data[inrow + 1 + input_data_offset];
5212: outptr = output_data[outrow++];
5213:
5214: int inptr0_offset = 0, inptr1_offset = 0, outptr_offset = 0;
5215:
5216: /* Special case for first column */
5217: this colsum = (inptr0[inptr0_offset++] & 0xFF) * 3
5218: + (inptr1[inptr1_offset++] & 0xFF);
5219: nextcolsum = (inptr0[inptr0_offset++] & 0xFF) * 3
5220: + (inptr1[inptr1_offset++] & 0xFF);
5221: outptr[outptr_offset++] = (byte) ((this colsum * 4 + 8) >> 4);
5222: outptr[outptr_offset++] = (byte) ((this colsum * 3
5223: + nextcolsum + 7) >> 4);
5224: lastcolsum = this colsum;
5225: this colsum = nextcolsum;
5226:
5227: for (colctr = compptr.downsampled_width - 2; colctr > 0; colctr--) {
5228: /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
5229: /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
5230: nextcolsum = (inptr0[inptr0_offset++] & 0xFF) * 3
5231: + (inptr1[inptr1_offset++] & 0xFF);
5232: outptr[outptr_offset++] = (byte) ((this colsum * 3
5233: + lastcolsum + 8) >> 4);
5234: outptr[outptr_offset++] = (byte) ((this colsum * 3
5235: + nextcolsum + 7) >> 4);
5236: lastcolsum = this colsum;
5237: this colsum = nextcolsum;
5238: }
5239:
5240: /* Special case for last column */
5241: outptr[outptr_offset++] = (byte) ((this colsum * 3
5242: + lastcolsum + 8) >> 4);
5243: outptr[outptr_offset++] = (byte) ((this colsum * 4 + 7) >> 4);
5244: }
5245: inrow++;
5246: }
5247: }
5248:
5249: static void int_upsample(jpeg_decompress_struct cinfo,
5250: jpeg_component_info compptr, byte[][] input_data,
5251: int input_data_offset, byte[][][] output_data_ptr,
5252: int[] output_data_offset, int output_data_index) {
5253: jpeg_upsampler upsample = cinfo.upsample;
5254: byte[][] output_data = output_data_ptr[output_data_index];
5255: byte[] inptr, outptr;
5256: byte invalue;
5257: int h;
5258: int outend;
5259: int h_expand, v_expand;
5260: int inrow, outrow;
5261: output_data_offset[output_data_index] = 0;
5262:
5263: h_expand = upsample.h_expand[compptr.component_index];
5264: v_expand = upsample.v_expand[compptr.component_index];
5265:
5266: inrow = outrow = 0;
5267: while (outrow < cinfo.max_v_samp_factor) {
5268: /* Generate one output row with proper horizontal expansion */
5269: inptr = input_data[inrow + input_data_offset];
5270: int inptr_offset = 0;
5271: outptr = output_data[outrow];
5272: int outptr_offset = 0;
5273: outend = outptr_offset + cinfo.output_width;
5274: while (outptr_offset < outend) {
5275: invalue = inptr[inptr_offset++]; /* don't need GETJSAMPLE() here */
5276: for (h = h_expand; h > 0; h--) {
5277: outptr[outptr_offset++] = invalue;
5278: }
5279: }
5280: /* Generate any additional output rows by duplicating the first one */
5281: if (v_expand > 1) {
5282: jcopy_sample_rows(output_data, outrow, output_data,
5283: outrow + 1, v_expand - 1, cinfo.output_width);
5284: }
5285: inrow++;
5286: outrow += v_expand;
5287: }
5288: }
5289:
5290: static void null_convert(jpeg_decompress_struct cinfo,
5291: byte[][][] input_buf, int[] input_buf_offset,
5292: int input_row, byte[][] output_buf, int output_buf_offset,
5293: int num_rows) {
5294: byte[] inptr, outptr;
5295: int count;
5296: int num_components = cinfo.num_components;
5297: int num_cols = cinfo.output_width;
5298: int ci;
5299:
5300: while (--num_rows >= 0) {
5301: for (ci = 0; ci < num_components; ci++) {
5302: inptr = input_buf[ci][input_row + input_buf_offset[0]];
5303: outptr = output_buf[output_buf_offset];
5304: /* BGR instead of RGB */
5305: int offset = 0;
5306: switch (ci) {
5307: case 2:
5308: offset = RGB_BLUE;
5309: break;
5310: case 1:
5311: offset = RGB_GREEN;
5312: break;
5313: case 0:
5314: offset = RGB_RED;
5315: break;
5316: }
5317: int outptr_offset = offset, inptr_offset = 0;
5318: for (count = num_cols; count > 0; count--) {
5319: outptr[outptr_offset] = inptr[inptr_offset++]; /* needn't bother with GETJSAMPLE() here */
5320: outptr_offset += num_components;
5321: }
5322: }
5323: input_row++;
5324: output_buf_offset++;
5325: }
5326: }
5327:
5328: static void grayscale_convert(jpeg_decompress_struct cinfo,
5329: byte[][][] input_buf, int[] input_buf_offset,
5330: int input_row, byte[][] output_buf, int output_buf_offset,
5331: int num_rows) {
5332: jcopy_sample_rows(input_buf[0],
5333: input_row + input_buf_offset[0], output_buf,
5334: output_buf_offset, num_rows, cinfo.output_width);
5335: }
5336:
5337: static void gray_rgb_convert(jpeg_decompress_struct cinfo,
5338: byte[][][] input_buf, int[] input_buf_offset,
5339: int input_row, byte[][] output_buf, int output_buf_offset,
5340: int num_rows) {
5341: byte[] inptr, outptr;
5342: int col;
5343: int num_cols = cinfo.output_width;
5344:
5345: while (--num_rows >= 0) {
5346: inptr = input_buf[0][input_row++ + input_buf_offset[0]];
5347: outptr = output_buf[output_buf_offset++];
5348: int outptr_offset = 0;
5349: for (col = 0; col < num_cols; col++) {
5350: /* We can dispense with GETJSAMPLE() here */
5351: outptr[RGB_RED + outptr_offset] = outptr[RGB_GREEN
5352: + outptr_offset] = outptr[RGB_BLUE
5353: + outptr_offset] = inptr[col];
5354: outptr_offset += RGB_PIXELSIZE;
5355: }
5356: }
5357: }
5358:
5359: static void ycc_rgb_convert(jpeg_decompress_struct cinfo,
5360: byte[][][] input_buf, int[] input_buf_offset,
5361: int input_row, byte[][] output_buf, int output_buf_offset,
5362: int num_rows) {
5363: jpeg_color_deconverter cconvert = cinfo.cconvert;
5364: int y, cb, cr;
5365: byte[] outptr;
5366: byte[] inptr0, inptr1, inptr2;
5367: int col;
5368: int num_cols = cinfo.output_width;
5369: /* copy these pointers into registers if possible */
5370: byte[] range_limit = cinfo.sample_range_limit;
5371: int range_limit_offset = cinfo.sample_range_limit_offset;
5372: int[] Crrtab = cconvert.Cr_r_tab;
5373: int[] Cbbtab = cconvert.Cb_b_tab;
5374: int[] Crgtab = cconvert.Cr_g_tab;
5375: int[] Cbgtab = cconvert.Cb_g_tab;
5376: // SHIFT_TEMPS
5377:
5378: while (--num_rows >= 0) {
5379: inptr0 = input_buf[0][input_row + input_buf_offset[0]];
5380: inptr1 = input_buf[1][input_row + input_buf_offset[1]];
5381: inptr2 = input_buf[2][input_row + input_buf_offset[2]];
5382: input_row++;
5383: outptr = output_buf[output_buf_offset++];
5384: int outptr_offset = 0;
5385: for (col = 0; col < num_cols; col++) {
5386: y = (inptr0[col] & 0xFF);
5387: cb = (inptr1[col] & 0xFF);
5388: cr = (inptr2[col] & 0xFF);
5389: /* Range-limiting is essential due to noise introduced by DCT losses. */
5390: outptr[outptr_offset + RGB_RED] = range_limit[y
5391: + Crrtab[cr] + range_limit_offset];
5392: outptr[outptr_offset + RGB_GREEN] = range_limit[y
5393: + ((Cbgtab[cb] + Crgtab[cr] >> SCALEBITS))
5394: + range_limit_offset];
5395: outptr[outptr_offset + RGB_BLUE] = range_limit[y
5396: + Cbbtab[cb] + range_limit_offset];
5397: outptr_offset += RGB_PIXELSIZE;
5398: }
5399: }
5400: }
5401:
5402: static boolean process_APPn(int n, jpeg_decompress_struct cinfo) {
5403: if (n == 0 || n == 14) {
5404: return get_interesting_appn(cinfo);
5405: }
5406: return skip_variable(cinfo);
5407: }
5408:
5409: static boolean process_COM(jpeg_decompress_struct cinfo) {
5410: return skip_variable(cinfo);
5411: }
5412:
5413: static void skip_input_data(jpeg_decompress_struct cinfo,
5414: int num_bytes) {
5415: if (num_bytes > 0) {
5416: while (num_bytes > cinfo.bytes_in_buffer
5417: - cinfo.bytes_offset) {
5418: num_bytes -= cinfo.bytes_in_buffer - cinfo.bytes_offset;
5419: if (!fill_input_buffer(cinfo))
5420: error();
5421: /* note we assume that fill_input_buffer will never return FALSE,
5422: * so suspension need not be handled.
5423: */
5424: }
5425: cinfo.bytes_offset += num_bytes;
5426: }
5427: }
5428:
5429: static boolean skip_variable(jpeg_decompress_struct cinfo)
5430: /* Skip over an unknown or uninteresting variable-length marker */
5431: {
5432: int length;
5433:
5434: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5435: fill_input_buffer(cinfo);
5436: length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
5437: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5438: fill_input_buffer(cinfo);
5439: length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
5440:
5441: length -= 2;
5442:
5443: // TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo.unread_marker, (int) length);
5444:
5445: if (length > 0) {
5446: skip_input_data(cinfo, length);
5447: }
5448:
5449: return true;
5450: }
5451:
5452: static boolean get_interesting_appn(jpeg_decompress_struct cinfo)
5453: /* Process an APP0 or APP14 marker without saving it */
5454: {
5455: int length;
5456: byte[] b = new byte[APPN_DATA_LEN];
5457: int i, numtoread;
5458:
5459: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5460: fill_input_buffer(cinfo);
5461: length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
5462: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5463: fill_input_buffer(cinfo);
5464: length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
5465: length -= 2;
5466:
5467: /* get the interesting part of the marker data */
5468: if (length >= APPN_DATA_LEN)
5469: numtoread = APPN_DATA_LEN;
5470: else if (length > 0)
5471: numtoread = length;
5472: else
5473: numtoread = 0;
5474: for (i = 0; i < numtoread; i++) {
5475: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5476: fill_input_buffer(cinfo);
5477: b[i] = cinfo.buffer[cinfo.bytes_offset++];
5478: }
5479: length -= numtoread;
5480:
5481: /* process it */
5482: switch (cinfo.unread_marker) {
5483: case M_APP0:
5484: examine_app0(cinfo, b, numtoread, length);
5485: break;
5486: case M_APP14:
5487: examine_app14(cinfo, b, numtoread, length);
5488: break;
5489: default:
5490: /* can't get here unless jpeg_save_markers chooses wrong processor */
5491: error();
5492: // ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo.unread_marker);
5493: break;
5494: }
5495:
5496: /* skip any remaining data -- could be lots */
5497: if (length > 0)
5498: skip_input_data(cinfo, length);
5499:
5500: return true;
5501: }
5502:
5503: static void examine_app0(jpeg_decompress_struct cinfo, byte[] data,
5504: int datalen, int remaining)
5505: /* Examine first few bytes from an APP0.
5506: * Take appropriate action if it is a JFIF marker.
5507: * datalen is # of bytes at data[], remaining is length of rest of marker data.
5508: */
5509: {
5510: int totallen = datalen + remaining;
5511:
5512: if (datalen >= APP0_DATA_LEN && (data[0] & 0xFF) == 0x4A
5513: && (data[1] & 0xFF) == 0x46 && (data[2] & 0xFF) == 0x49
5514: && (data[3] & 0xFF) == 0x46 && (data[4] & 0xFF) == 0) {
5515: /* Found JFIF APP0 marker: save info */
5516: cinfo.saw_JFIF_marker = true;
5517: cinfo.JFIF_major_version = (data[5]);
5518: cinfo.JFIF_minor_version = (byte) (data[6] & 0xFF);
5519: cinfo.density_unit = (byte) (data[7] & 0xFF);
5520: cinfo.X_density = (short) (((data[8] & 0xFF) << 8) + (data[9] & 0xFF));
5521: cinfo.Y_density = (short) (((data[10] & 0xFF) << 8) + (data[11] & 0xFF));
5522: /* Check version.
5523: * Major version must be 1, anything else signals an incompatible change.
5524: * (We used to treat this as an error, but now it's a nonfatal warning,
5525: * because some bozo at Hijaak couldn't read the spec.)
5526: * Minor version should be 0..2, but process anyway if newer.
5527: */
5528: if (cinfo.JFIF_major_version != 1) {
5529: // WARNMS2(cinfo, JWRN_JFIF_MAJOR,
5530: // cinfo.JFIF_major_version, cinfo.JFIF_minor_version);
5531: }
5532: /* Generate trace messages */
5533: // TRACEMS5(cinfo, 1, JTRC_JFIF,
5534: // cinfo.JFIF_major_version, cinfo.JFIF_minor_version,
5535: // cinfo.X_density, cinfo.Y_density, cinfo.density_unit);
5536: /* Validate thumbnail dimensions and issue appropriate messages */
5537: if (((data[12] & 0xFF) | (data[13]) & 0xFF) != 0) {
5538: // TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
5539: // GETJOCTET(data[12]), GETJOCTET(data[13]));
5540: }
5541: totallen -= APP0_DATA_LEN;
5542: if (totallen != ((data[12] & 0xFF) * (data[13] & 0xFF) * 3)) {
5543: // TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
5544: }
5545: } else if (datalen >= 6 && (data[0] & 0xFF) == 0x4A
5546: && (data[1] & 0xFF) == 0x46 && (data[2] & 0xFF) == 0x58
5547: && (data[3] & 0xFF) == 0x58 && (data[4] & 0xFF) == 0) {
5548: /* Found JFIF "JFXX" extension APP0 marker */
5549: /* The library doesn't actually do anything with these,
5550: * but we try to produce a helpful trace message.
5551: */
5552: switch ((data[5]) & 0xFF) {
5553: case 0x10:
5554: // TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
5555: break;
5556: case 0x11:
5557: // TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
5558: break;
5559: case 0x13:
5560: // TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
5561: break;
5562: default:
5563: // TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION, GETJOCTET(data[5]), (int) totallen);
5564: break;
5565: }
5566: } else {
5567: /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
5568: // TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
5569: }
5570: }
5571:
5572: static void examine_app14(jpeg_decompress_struct cinfo,
5573: byte[] data, int datalen, int remaining)
5574: /* Examine first few bytes from an APP14.
5575: * Take appropriate action if it is an Adobe marker.
5576: * datalen is # of bytes at data[], remaining is length of rest of marker data.
5577: */
5578: {
5579: int /*version, flags0, flags1, */transform;
5580:
5581: if (datalen >= APP14_DATA_LEN && (data[0] & 0xFF) == 0x41
5582: && (data[1] & 0xFF) == 0x64 && (data[2] & 0xFF) == 0x6F
5583: && (data[3] & 0xFF) == 0x62 && (data[4] & 0xFF) == 0x65) {
5584: /* Found Adobe APP14 marker */
5585: // version = ((data[5] & 0xFF) << 8) + (data[6] & 0xFF);
5586: // flags0 = ((data[7] & 0xFF) << 8) + (data[8] & 0xFF);
5587: // flags1 = ((data[9] & 0xFF) << 8) + (data[10] & 0xFF);
5588: transform = (data[11] & 0xFF);
5589: // TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
5590: cinfo.saw_Adobe_marker = true;
5591: cinfo.Adobe_transform = (byte) transform;
5592: } else {
5593: /* Start of APP14 does not match "Adobe", or too short */
5594: // TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
5595: }
5596: }
5597:
5598: static boolean get_soi(jpeg_decompress_struct cinfo) /* Process an SOI marker */{
5599: int i;
5600:
5601: // TRACEMS(cinfo, 1, JTRC_SOI);
5602:
5603: if (cinfo.marker.saw_SOI)
5604: error();
5605: // ERREXIT(cinfo, JERR_SOI_DUPLICATE);
5606:
5607: /* Reset all parameters that are defined to be reset by SOI */
5608:
5609: for (i = 0; i < NUM_ARITH_TBLS; i++) {
5610: cinfo.arith_dc_L[i] = 0;
5611: cinfo.arith_dc_U[i] = 1;
5612: cinfo.arith_ac_K[i] = 5;
5613: }
5614: cinfo.restart_interval = 0;
5615:
5616: /* Set initial assumptions for colorspace etc */
5617:
5618: cinfo.jpeg_color_space = JCS_UNKNOWN;
5619: cinfo.CCIR601_sampling = false; /* Assume non-CCIR sampling??? */
5620:
5621: cinfo.saw_JFIF_marker = false;
5622: cinfo.JFIF_major_version = 1; /* set default JFIF APP0 values */
5623: cinfo.JFIF_minor_version = 1;
5624: cinfo.density_unit = 0;
5625: cinfo.X_density = 1;
5626: cinfo.Y_density = 1;
5627: cinfo.saw_Adobe_marker = false;
5628: cinfo.Adobe_transform = 0;
5629:
5630: cinfo.marker.saw_SOI = true;
5631:
5632: return true;
5633: }
5634:
5635: static void jinit_input_controller(jpeg_decompress_struct cinfo) {
5636: /* Initialize state: can't use reset_input_controller since we don't
5637: * want to try to reset other modules yet.
5638: */
5639: jpeg_input_controller inputctl = cinfo.inputctl = new jpeg_input_controller();
5640: inputctl.has_multiple_scans = false; /* "unknown" would be better */
5641: inputctl.eoi_reached = false;
5642: inputctl.inheaders = true;
5643: }
5644:
5645: static void reset_marker_reader(jpeg_decompress_struct cinfo) {
5646: jpeg_marker_reader marker = cinfo.marker;
5647:
5648: cinfo.comp_info = null; /* until allocated by get_sof */
5649: cinfo.input_scan_number = 0; /* no SOS seen yet */
5650: cinfo.unread_marker = 0; /* no pending marker */
5651: marker.saw_SOI = false; /* set internal state too */
5652: marker.saw_SOF = false;
5653: marker.discarded_bytes = 0;
5654: // marker.cur_marker = null;
5655: }
5656:
5657: static void reset_input_controller(jpeg_decompress_struct cinfo) {
5658: jpeg_input_controller inputctl = cinfo.inputctl;
5659:
5660: inputctl.has_multiple_scans = false; /* "unknown" would be better */
5661: inputctl.eoi_reached = false;
5662: inputctl.inheaders = true;
5663: /* Reset other modules */
5664: reset_marker_reader(cinfo);
5665: /* Reset progression state -- would be cleaner if entropy decoder did this */
5666: cinfo.coef_bits = null;
5667: }
5668:
5669: static void finish_output_pass(jpeg_decompress_struct cinfo) {
5670: jpeg_decomp_master master = cinfo.master;
5671:
5672: if (cinfo.quantize_colors) {
5673: error(SWT.ERROR_NOT_IMPLEMENTED);
5674: // (*cinfo.cquantize.finish_pass) (cinfo);
5675: }
5676: master.pass_number++;
5677: }
5678:
5679: static void jpeg_destroy(jpeg_decompress_struct cinfo) {
5680: /* We need only tell the memory manager to release everything. */
5681: /* NB: mem pointer is NULL if memory mgr failed to initialize. */
5682: // if (cinfo.mem != NULL)
5683: // (*cinfo.mem.self_destruct) (cinfo);
5684: // cinfo.mem = NULL; /* be safe if jpeg_destroy is called twice */
5685: cinfo.global_state = 0; /* mark it destroyed */
5686: }
5687:
5688: static void jpeg_destroy_decompress(jpeg_decompress_struct cinfo) {
5689: jpeg_destroy(cinfo); /* use common routine */
5690: }
5691:
5692: static boolean jpeg_input_complete(jpeg_decompress_struct cinfo) {
5693: /* Check for valid jpeg object */
5694: if (cinfo.global_state < DSTATE_START
5695: || cinfo.global_state > DSTATE_STOPPING)
5696: error();
5697: // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
5698: return cinfo.inputctl.eoi_reached;
5699: }
5700:
5701: static boolean jpeg_start_output(jpeg_decompress_struct cinfo,
5702: int scan_number) {
5703: if (cinfo.global_state != DSTATE_BUFIMAGE
5704: && cinfo.global_state != DSTATE_PRESCAN)
5705: error();
5706: // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
5707: /* Limit scan number to valid range */
5708: if (scan_number <= 0)
5709: scan_number = 1;
5710: if (cinfo.inputctl.eoi_reached
5711: && scan_number > cinfo.input_scan_number)
5712: scan_number = cinfo.input_scan_number;
5713: cinfo.output_scan_number = scan_number;
5714: /* Perform any dummy output passes, and set up for the real pass */
5715: return output_pass_setup(cinfo);
5716: }
5717:
5718: static boolean jpeg_finish_output(jpeg_decompress_struct cinfo) {
5719: if ((cinfo.global_state == DSTATE_SCANNING || cinfo.global_state == DSTATE_RAW_OK)
5720: && cinfo.buffered_image) {
5721: /* Terminate this pass. */
5722: /* We do not require the whole pass to have been completed. */
5723: finish_output_pass(cinfo);
5724: cinfo.global_state = DSTATE_BUFPOST;
5725: } else if (cinfo.global_state != DSTATE_BUFPOST) {
5726: /* BUFPOST = repeat call after a suspension, anything else is error */
5727: error();
5728: // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
5729: }
5730: /* Read markers looking for SOS or EOI */
5731: while (cinfo.input_scan_number <= cinfo.output_scan_number
5732: && !cinfo.inputctl.eoi_reached) {
5733: if (consume_input(cinfo) == JPEG_SUSPENDED)
5734: return false; /* Suspend, come back later */
5735: }
5736: cinfo.global_state = DSTATE_BUFIMAGE;
5737: return true;
5738: }
5739:
5740: static boolean jpeg_finish_decompress(jpeg_decompress_struct cinfo) {
5741: if ((cinfo.global_state == DSTATE_SCANNING || cinfo.global_state == DSTATE_RAW_OK)
5742: && !cinfo.buffered_image) {
5743: /* Terminate final pass of non-buffered mode */
5744: if (cinfo.output_scanline < cinfo.output_height)
5745: error();
5746: // ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
5747: finish_output_pass(cinfo);
5748: cinfo.global_state = DSTATE_STOPPING;
5749: } else if (cinfo.global_state == DSTATE_BUFIMAGE) {
5750: /* Finishing after a buffered-image operation */
5751: cinfo.global_state = DSTATE_STOPPING;
5752: } else if (cinfo.global_state != DSTATE_STOPPING) {
5753: /* STOPPING = repeat call after a suspension, anything else is error */
5754: error();
5755: // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
5756: }
5757: /* Read until EOI */
5758: while (!cinfo.inputctl.eoi_reached) {
5759: if (consume_input(cinfo) == JPEG_SUSPENDED)
5760: return false; /* Suspend, come back later */
5761: }
5762: /* Do final cleanup */
5763: // (*cinfo.src.term_source) (cinfo);
5764: /* We can use jpeg_abort to release memory and reset global_state */
5765: jpeg_abort(cinfo);
5766: return true;
5767: }
5768:
5769: static int jpeg_read_header(jpeg_decompress_struct cinfo,
5770: boolean require_image) {
5771: int retcode;
5772:
5773: if (cinfo.global_state != DSTATE_START
5774: && cinfo.global_state != DSTATE_INHEADER)
5775: error();
5776: // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
5777:
5778: retcode = jpeg_consume_input(cinfo);
5779:
5780: switch (retcode) {
5781: case JPEG_REACHED_SOS:
5782: retcode = JPEG_HEADER_OK;
5783: break;
5784: case JPEG_REACHED_EOI:
5785: if (require_image) /* Complain if application wanted an image */
5786: error();
5787: // ERREXIT(cinfo, JERR_NO_IMAGE);
5788: /* Reset to start state; it would be safer to require the application to
5789: * call jpeg_abort, but we can't change it now for compatibility reasons.
5790: * A side effect is to free any temporary memory (there shouldn't be any).
5791: */
5792: jpeg_abort(cinfo); /* sets state = DSTATE_START */
5793: retcode = JPEG_HEADER_TABLES_ONLY;
5794: break;
5795: case JPEG_SUSPENDED:
5796: /* no work */
5797: break;
5798: }
5799:
5800: return retcode;
5801: }
5802:
5803: static int dummy_consume_data(jpeg_decompress_struct cinfo) {
5804: return JPEG_SUSPENDED; /* Always indicate nothing was done */
5805: }
5806:
5807: static int consume_data(jpeg_decompress_struct cinfo) {
5808: jpeg_d_coef_controller coef = cinfo.coef;
5809: int MCU_col_num; /* index of current MCU within row */
5810: int blkn, ci, xindex, yindex, yoffset;
5811: int start_col;
5812: // short[][][][] buffer = new short[MAX_COMPS_IN_SCAN][][][];
5813: short[][] buffer_ptr;
5814: jpeg_component_info compptr;
5815:
5816: // /* Align the virtual buffers for the components used in this scan. */
5817: // for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
5818: // compptr = cinfo.cur_comp_info[ci];
5819: // buffer[ci] = coef.whole_image[compptr.component_index];
5820: // /* Note: entropy decoder expects buffer to be zeroed,
5821: // * but this is handled automatically by the memory manager
5822: // * because we requested a pre-zeroed array.
5823: // */
5824: // }
5825:
5826: /* Loop to process one whole iMCU row */
5827: for (yoffset = coef.MCU_vert_offset; yoffset < coef.MCU_rows_per_iMCU_row; yoffset++) {
5828: for (MCU_col_num = coef.MCU_ctr; MCU_col_num < cinfo.MCUs_per_row; MCU_col_num++) {
5829: /* Construct list of pointers to DCT blocks belonging to this MCU */
5830: blkn = 0; /* index of current DCT block within MCU */
5831: for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
5832: compptr = cinfo.cur_comp_info[ci];
5833: start_col = MCU_col_num * compptr.MCU_width;
5834: for (yindex = 0; yindex < compptr.MCU_height; yindex++) {
5835: // buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
5836: buffer_ptr = coef.whole_image[compptr.component_index][yindex
5837: + yoffset
5838: + cinfo.input_iMCU_row
5839: * compptr.v_samp_factor];
5840: int buffer_ptr_offset = start_col;
5841: for (xindex = 0; xindex < compptr.MCU_width; xindex++) {
5842: coef.MCU_buffer[blkn++] = buffer_ptr[buffer_ptr_offset++];
5843: }
5844: }
5845: }
5846: /* Try to fetch the MCU. */
5847: if (!cinfo.entropy.decode_mcu(cinfo, coef.MCU_buffer)) {
5848: /* Suspension forced; update state counters and exit */
5849: coef.MCU_vert_offset = yoffset;
5850: coef.MCU_ctr = MCU_col_num;
5851: return JPEG_SUSPENDED;
5852: }
5853: }
5854: /* Completed an MCU row, but perhaps not an iMCU row */
5855: coef.MCU_ctr = 0;
5856: }
5857: /* Completed the iMCU row, advance counters for next one */
5858: if (++(cinfo.input_iMCU_row) < cinfo.total_iMCU_rows) {
5859: coef.start_iMCU_row(cinfo);
5860: return JPEG_ROW_COMPLETED;
5861: }
5862: /* Completed the scan */
5863: finish_input_pass(cinfo);
5864: return JPEG_SCAN_COMPLETED;
5865: }
5866:
5867: static int consume_input(jpeg_decompress_struct cinfo) {
5868: switch (cinfo.inputctl.consume_input) {
5869: case COEF_CONSUME_INPUT:
5870: switch (cinfo.coef.consume_data) {
5871: case CONSUME_DATA:
5872: return consume_data(cinfo);
5873: case DUMMY_CONSUME_DATA:
5874: return dummy_consume_data(cinfo);
5875: default:
5876: error();
5877: }
5878: break;
5879: case INPUT_CONSUME_INPUT:
5880: return consume_markers(cinfo);
5881: default:
5882: error();
5883: }
5884: return 0;
5885: }
5886:
5887: static boolean fill_input_buffer(jpeg_decompress_struct cinfo) {
5888: try {
5889: InputStream inputStream = cinfo.inputStream;
5890: int nbytes = inputStream.read(cinfo.buffer);
5891: if (nbytes <= 0) {
5892: if (cinfo.start_of_file) /* Treat empty input file as fatal error */
5893: error();
5894: // ERREXIT(cinfo, JERR_INPUT_EMPTY);
5895: // WARNMS(cinfo, JWRN_JPEG_EOF);
5896: /* Insert a fake EOI marker */
5897: cinfo.buffer[0] = (byte) 0xFF;
5898: cinfo.buffer[1] = (byte) M_EOI;
5899: nbytes = 2;
5900: }
5901: cinfo.bytes_in_buffer = nbytes;
5902: cinfo.bytes_offset = 0;
5903: cinfo.start_of_file = false;
5904: } catch (IOException e) {
5905: error(SWT.ERROR_IO);
5906: return false;
5907: }
5908: return true;
5909: }
5910:
5911: static boolean first_marker(jpeg_decompress_struct cinfo) {
5912: /* Like next_marker, but used to obtain the initial SOI marker. */
5913: /* For this marker, we do not allow preceding garbage or fill; otherwise,
5914: * we might well scan an entire input file before realizing it ain't JPEG.
5915: * If an application wants to process non-JFIF files, it must seek to the
5916: * SOI before calling the JPEG library.
5917: */
5918: int c, c2;
5919:
5920: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5921: fill_input_buffer(cinfo);
5922: c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
5923: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5924: fill_input_buffer(cinfo);
5925: c2 = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
5926: if (c != 0xFF || c2 != M_SOI)
5927: error();
5928: // ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
5929:
5930: cinfo.unread_marker = c2;
5931:
5932: return true;
5933: }
5934:
5935: static boolean next_marker(jpeg_decompress_struct cinfo) {
5936: int c;
5937:
5938: for (;;) {
5939: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5940: fill_input_buffer(cinfo);
5941: c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
5942: /* Skip any non-FF bytes.
5943: * This may look a bit inefficient, but it will not occur in a valid file.
5944: * We sync after each discarded byte so that a suspending data source
5945: * can discard the byte from its buffer.
5946: */
5947: while (c != 0xFF) {
5948: cinfo.marker.discarded_bytes++;
5949: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5950: fill_input_buffer(cinfo);
5951: c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
5952: }
5953: /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
5954: * pad bytes, so don't count them in discarded_bytes. We assume there
5955: * will not be so many consecutive FF bytes as to overflow a suspending
5956: * data source's input buffer.
5957: */
5958: do {
5959: if (cinfo.bytes_offset == cinfo.bytes_in_buffer)
5960: fill_input_buffer(cinfo);
5961: c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
5962: } while (c == 0xFF);
5963: if (c != 0)
5964: break; /* found a valid marker, exit loop */
5965: /* Reach here if we found a stuffed-zero data sequence (FF/00).
5966: * Discard it and loop back to try again.
5967: */
5968: cinfo.marker.discarded_bytes += 2;
5969: }
5970:
5971: if (cinfo.marker.discarded_bytes != 0) {
5972: // WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo.marker.discarded_bytes, c);
5973: cinfo.marker.discarded_bytes = 0;
5974: }
5975:
5976: cinfo.unread_marker = c;
5977:
5978: return true;
5979: }
5980:
5981: static int read_markers(jpeg_decompress_struct cinfo) {
5982: /* Outer loop repeats once for each marker. */
5983: for (;;) {
5984: /* Collect the marker proper, unless we already did. */
5985: /* NB: first_marker() enforces the requirement that SOI appear first. */
5986: if (cinfo.unread_marker == 0) {
5987: if (!cinfo.marker.saw_SOI) {
5988: if (!first_marker(cinfo))
5989: return JPEG_SUSPENDED;
5990: } else {
5991: if (!next_marker(cinfo))
5992: return JPEG_SUSPENDED;
5993: }
5994: }
5995: /* At this point cinfo.unread_marker contains the marker code and the
5996: * input point is just past the marker proper, but before any parameters.
5997: * A suspension will cause us to return with this state still true.
5998: */
5999: switch (cinfo.unread_marker) {
6000: case M_SOI:
6001: if (!get_soi(cinfo))
6002: return JPEG_SUSPENDED;
6003: break;
6004:
6005: case M_SOF0: /* Baseline */
6006: case M_SOF1: /* Extended sequential, Huffman */
6007: if (!get_sof(cinfo, false, false))
6008: return JPEG_SUSPENDED;
6009: break;
6010:
6011: case M_SOF2: /* Progressive, Huffman */
6012: if (!get_sof(cinfo, true, false))
6013: return JPEG_SUSPENDED;
6014: break;
6015:
6016: case M_SOF9: /* Extended sequential, arithmetic */
6017: if (!get_sof(cinfo, false, true))
6018: return JPEG_SUSPENDED;
6019: break;
6020:
6021: case M_SOF10: /* Progressive, arithmetic */
6022: if (!get_sof(cinfo, true, true))
6023: return JPEG_SUSPENDED;
6024: break;
6025:
6026: /* Currently unsupported SOFn types */
6027: case M_SOF3: /* Lossless, Huffman */
6028: case M_SOF5: /* Differential sequential, Huffman */
6029: case M_SOF6: /* Differential progressive, Huffman */
6030: case M_SOF7: /* Differential lossless, Huffman */
6031: case M_JPG: /* Reserved for JPEG extensions */
6032: case M_SOF11: /* Lossless, arithmetic */
6033: case M_SOF13: /* Differential sequential, arithmetic */
6034: case M_SOF14: /* Differential progressive, arithmetic */
6035: case M_SOF15: /* Differential lossless, arithmetic */
6036: error();
6037: // ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo.unread_marker);
6038: break;
6039:
6040: case M_SOS:
6041: if (!get_sos(cinfo))
6042: return JPEG_SUSPENDED;
6043: cinfo.unread_marker = 0; /* processed the marker */
6044: return JPEG_REACHED_SOS;
6045:
6046: case M_EOI:
6047: // TRACEMS(cinfo, 1, JTRC_EOI);
6048: cinfo.unread_marker = 0; /* processed the marker */
6049: return JPEG_REACHED_EOI;
6050:
6051: case M_DAC:
6052: if (!get_dac(cinfo))
6053: return JPEG_SUSPENDED;
6054: break;
6055:
6056: case M_DHT:
6057: if (!get_dht(cinfo))
6058: return JPEG_SUSPENDED;
6059: break;
6060:
6061: case M_DQT:
6062: if (!get_dqt(cinfo))
6063: return JPEG_SUSPENDED;
6064: break;
6065:
6066: case M_DRI:
6067: if (!get_dri(cinfo))
6068: return JPEG_SUSPENDED;
6069: break;
6070:
6071: case M_APP0:
6072: case M_APP1:
6073: case M_APP2:
6074: case M_APP3:
6075: case M_APP4:
6076: case M_APP5:
6077: case M_APP6:
6078: case M_APP7:
6079: case M_APP8:
6080: case M_APP9:
6081: case M_APP10:
6082: case M_APP11:
6083: case M_APP12:
6084: case M_APP13:
6085: case M_APP14:
6086: case M_APP15:
6087: if (!process_APPn(cinfo.unread_marker - M_APP0, cinfo))
6088: return JPEG_SUSPENDED;
6089: break;
6090:
6091: case M_COM:
6092: if (!process_COM(cinfo))
6093: return JPEG_SUSPENDED;
6094: break;
6095:
6096: case M_RST0: /* these are all parameterless */
6097: case M_RST1:
6098: case M_RST2:
6099: case M_RST3:
6100: case M_RST4:
6101: case M_RST5:
6102: case M_RST6:
6103: case M_RST7:
6104: case M_TEM:
6105: // TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo.unread_marker);
6106: break;
6107:
6108: case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
6109: if (!skip_variable(cinfo))
6110: return JPEG_SUSPENDED;
6111: break;
6112:
6113: default: /* must be DHP, EXP, JPGn, or RESn */
6114: /* For now, we treat the reserved markers as fatal errors since they are
6115: * likely to be used to signal incompatible JPEG Part 3 extensions.
6116: * Once the JPEG 3 version-number marker is well defined, this code
6117: * ought to change!
6118: */
6119: error();
6120: // ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo.unread_marker);
6121: break;
6122: }
6123: /* Successfully processed marker, so reset state variable */
6124: cinfo.unread_marker = 0;
6125: } /* end loop */
6126: }
6127:
6128: static long jdiv_round_up(long a, long b)
6129: /* Compute a/b rounded up to next integer, ie, ceil(a/b) */
6130: /* Assumes a >= 0, b > 0 */
6131: {
6132: return (a + b - 1) / b;
6133: }
6134:
6135: static void initial_setup(jpeg_decompress_struct cinfo)
6136: /* Called once, when first SOS marker is reached */
6137: {
6138: int ci;
6139: jpeg_component_info compptr;
6140:
6141: /* Make sure image isn't bigger than I can handle */
6142: if (cinfo.image_height > JPEG_MAX_DIMENSION
6143: || cinfo.image_width > JPEG_MAX_DIMENSION)
6144: error();
6145: // ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
6146:
6147: /* For now, precision must match compiled-in value... */
6148: if (cinfo.data_precision != BITS_IN_JSAMPLE)
6149: error(" [data precision=" + cinfo.data_precision + "]");
6150: // ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo.data_precision);
6151:
6152: /* Check that number of components won't exceed internal array sizes */
6153: if (cinfo.num_components > MAX_COMPONENTS)
6154: error();
6155: // ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo.num_components, MAX_COMPONENTS);
6156:
6157: /* Compute maximum sampling factors; check factor validity */
6158: cinfo.max_h_samp_factor = 1;
6159: cinfo.max_v_samp_factor = 1;
6160: for (ci = 0; ci < cinfo.num_components; ci++) {
6161: compptr = cinfo.comp_info[ci];
6162: if (compptr.h_samp_factor <= 0
6163: || compptr.h_samp_factor > MAX_SAMP_FACTOR
6164: || compptr.v_samp_factor <= 0
6165: || compptr.v_samp_factor > MAX_SAMP_FACTOR)
6166: error();
6167: // ERREXIT(cinfo, JERR_BAD_SAMPLING);
6168: cinfo.max_h_samp_factor = Math.max(cinfo.max_h_samp_factor,
6169: compptr.h_samp_factor);
6170: cinfo.max_v_samp_factor = Math.max(cinfo.max_v_samp_factor,
6171: compptr.v_samp_factor);
6172: }
6173:
6174: /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
6175: * In the full decompressor, this will be overridden by jdmaster.c;
6176: * but in the transcoder, jdmaster.c is not used, so we must do it here.
6177: */
6178: cinfo.min_DCT_scaled_size = DCTSIZE;
6179:
6180: /* Compute dimensions of components */
6181: for (ci = 0; ci < cinfo.num_components; ci++) {
6182: compptr = cinfo.comp_info[ci];
6183: compptr.DCT_scaled_size = DCTSIZE;
6184: /* Size in DCT blocks */
6185: compptr.width_in_blocks = (int) jdiv_round_up(
6186: (long) cinfo.image_width
6187: * (long) compptr.h_samp_factor,
6188: (cinfo.max_h_samp_factor * DCTSIZE));
6189: compptr.height_in_blocks = (int) jdiv_round_up(
6190: (long) cinfo.image_height
6191: * (long) compptr.v_samp_factor,
6192: (cinfo.max_v_samp_factor * DCTSIZE));
6193: /* downsampled_width and downsampled_height will also be overridden by
6194: * jdmaster.c if we are doing full decompression. The transcoder library
6195: * doesn't use these values, but the calling application might.
6196: */
6197: /* Size in samples */
6198: compptr.downsampled_width = (int) jdiv_round_up(
6199: (long) cinfo.image_width
6200: * (long) compptr.h_samp_factor,
6201: cinfo.max_h_samp_factor);
6202: compptr.downsampled_height = (int) jdiv_round_up(
6203: (long) cinfo.image_height
6204: * (long) compptr.v_samp_factor,
6205: cinfo.max_v_samp_factor);
6206: /* Mark component needed, until color conversion says otherwise */
6207: compptr.component_needed = true;
6208: /* Mark no quantization table yet saved for component */
6209: compptr.quant_table = null;
6210: }
6211:
6212: /* Compute number of fully interleaved MCU rows. */
6213: cinfo.total_iMCU_rows = (int) jdiv_round_up(cinfo.image_height,
6214: (cinfo.max_v_samp_factor * DCTSIZE));
6215:
6216: /* Decide whether file contains multiple scans */
6217: if (cinfo.comps_in_scan < cinfo.num_components
6218: || cinfo.progressive_mode)
6219: cinfo.inputctl.has_multiple_scans = true;
6220: else
6221: cinfo.inputctl.has_multiple_scans = false;
6222: }
6223:
6224: static void per_scan_setup(jpeg_decompress_struct cinfo)
6225: /* Do computations that are needed before processing a JPEG scan */
6226: /* cinfo.comps_in_scan and cinfo.cur_comp_info[] were set from SOS marker */
6227: {
6228: int ci, mcublks, tmp = 0;
6229: jpeg_component_info compptr;
6230:
6231: if (cinfo.comps_in_scan == 1) {
6232:
6233: /* Noninterleaved (single-component) scan */
6234: compptr = cinfo.cur_comp_info[0];
6235:
6236: /* Overall image size in MCUs */
6237: cinfo.MCUs_per_row = compptr.width_in_blocks;
6238: cinfo.MCU_rows_in_scan = compptr.height_in_blocks;
6239:
6240: /* For noninterleaved scan, always one block per MCU */
6241: compptr.MCU_width = 1;
6242: compptr.MCU_height = 1;
6243: compptr.MCU_blocks = 1;
6244: compptr.MCU_sample_width = compptr.DCT_scaled_size;
6245: compptr.last_col_width = 1;
6246: /* For noninterleaved scans, it is convenient to define last_row_height
6247: * as the number of block rows present in the last iMCU row.
6248: */
6249: tmp = (compptr.height_in_blocks % compptr.v_samp_factor);
6250: if (tmp == 0)
6251: tmp = compptr.v_samp_factor;
6252: compptr.last_row_height = tmp;
6253:
6254: /* Prepare array describing MCU composition */
6255: cinfo.blocks_in_MCU = 1;
6256: cinfo.MCU_membership[0] = 0;
6257:
6258: } else {
6259:
6260: /* Interleaved (multi-component) scan */
6261: if (cinfo.comps_in_scan <= 0
6262: || cinfo.comps_in_scan > MAX_COMPS_IN_SCAN)
6263: error();
6264: // ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo.comps_in_scan, MAX_COMPS_IN_SCAN);
6265:
6266: /* Overall image size in MCUs */
6267: cinfo.MCUs_per_row = (int) jdiv_round_up(cinfo.image_width,
6268: (cinfo.max_h_samp_factor * DCTSIZE));
6269: cinfo.MCU_rows_in_scan = (int) jdiv_round_up(
6270: cinfo.image_height,
6271: (cinfo.max_v_samp_factor * DCTSIZE));
6272:
6273: cinfo.blocks_in_MCU = 0;
6274:
6275: for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
6276: compptr = cinfo.cur_comp_info[ci];
6277: /* Sampling factors give # of blocks of component in each MCU */
6278: compptr.MCU_width = compptr.h_samp_factor;
6279: compptr.MCU_height = compptr.v_samp_factor;
6280: compptr.MCU_blocks = compptr.MCU_width
6281: * compptr.MCU_height;
6282: compptr.MCU_sample_width = compptr.MCU_width
6283: * compptr.DCT_scaled_size;
6284: /* Figure number of non-dummy blocks in last MCU column & row */
6285: tmp = (compptr.width_in_blocks % compptr.MCU_width);
6286: if (tmp == 0)
6287: tmp = compptr.MCU_width;
6288: compptr.last_col_width = tmp;
6289: tmp = (compptr.height_in_blocks % compptr.MCU_height);
6290: if (tmp == 0)
6291: tmp = compptr.MCU_height;
6292: compptr.last_row_height = tmp;
6293: /* Prepare array describing MCU composition */
6294: mcublks = compptr.MCU_blocks;
6295: if (cinfo.blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
6296: error();
6297: // ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
6298: while (mcublks-- > 0) {
6299: cinfo.MCU_membership[cinfo.blocks_in_MCU++] = ci;
6300: }
6301: }
6302:
6303: }
6304: }
6305:
6306: static void latch_quant_tables(jpeg_decompress_struct cinfo) {
6307: int ci, qtblno;
6308: jpeg_component_info compptr;
6309: JQUANT_TBL qtbl;
6310:
6311: for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
6312: compptr = cinfo.cur_comp_info[ci];
6313: /* No work if we already saved Q-table for this component */
6314: if (compptr.quant_table != null)
6315: continue;
6316: /* Make sure specified quantization table is present */
6317: qtblno = compptr.quant_tbl_no;
6318: if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS
6319: || cinfo.quant_tbl_ptrs[qtblno] == null)
6320: error();
6321: // ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
6322: /* OK, save away the quantization table */
6323: qtbl = new JQUANT_TBL();
6324: System.arraycopy(cinfo.quant_tbl_ptrs[qtblno].quantval, 0,
6325: qtbl.quantval, 0, qtbl.quantval.length);
6326: qtbl.sent_table = cinfo.quant_tbl_ptrs[qtblno].sent_table;
6327: compptr.quant_table = qtbl;
6328: }
6329: }
6330:
6331: static void jpeg_make_d_derived_tbl(jpeg_decompress_struct cinfo,
6332: boolean isDC, int tblno, d_derived_tbl dtbl) {
6333: JHUFF_TBL htbl;
6334: int p, i = 0, l, si, numsymbols;
6335: int lookbits, ctr;
6336: byte[] huffsize = new byte[257];
6337: int[] huffcode = new int[257];
6338: int code;
6339:
6340: /* Note that huffsize[] and huffcode[] are filled in code-length order,
6341: * paralleling the order of the symbols themselves in htbl.huffval[].
6342: */
6343:
6344: /* Find the input Huffman table */
6345: if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
6346: error();
6347: // ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
6348: htbl = isDC ? cinfo.dc_huff_tbl_ptrs[tblno]
6349: : cinfo.ac_huff_tbl_ptrs[tblno];
6350: if (htbl == null)
6351: error();
6352: // ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
6353:
6354: /* Allocate a workspace if we haven't already done so. */
6355: dtbl.pub = htbl; /* fill in back link */
6356:
6357: /* Figure C.1: make table of Huffman code length for each symbol */
6358:
6359: p = 0;
6360: for (l = 1; l <= 16; l++) {
6361: i = htbl.bits[l] & 0xFF;
6362: if (i < 0 || p + i > 256) /* protect against table overrun */
6363: error();
6364: // ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
6365: while (i-- != 0)
6366: huffsize[p++] = (byte) l;
6367: }
6368: huffsize[p] = 0;
6369: numsymbols = p;
6370:
6371: /* Figure C.2: generate the codes themselves */
6372: /* We also validate that the counts represent a legal Huffman code tree. */
6373:
6374: code = 0;
6375: si = huffsize[0];
6376: p = 0;
6377: while ((huffsize[p]) != 0) {
6378: while ((huffsize[p]) == si) {
6379: huffcode[p++] = code;
6380: code++;
6381: }
6382: /* code is now 1 more than the last code used for codelength si; but
6383: * it must still fit in si bits, since no code is allowed to be all ones.
6384: */
6385: if ((code) >= ((1) << si))
6386: error();
6387: // ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
6388: code <<= 1;
6389: si++;
6390: }
6391:
6392: /* Figure F.15: generate decoding tables for bit-sequential decoding */
6393:
6394: p = 0;
6395: for (l = 1; l <= 16; l++) {
6396: if ((htbl.bits[l] & 0xFF) != 0) {
6397: /* valoffset[l] = huffval[] index of 1st symbol of code length l,
6398: * minus the minimum code of length l
6399: */
6400: dtbl.valoffset[l] = p - huffcode[p];
6401: p += (htbl.bits[l] & 0xFF);
6402: dtbl.maxcode[l] = huffcode[p - 1]; /* maximum code of length l */
6403: } else {
6404: dtbl.maxcode[l] = -1; /* -1 if no codes of this length */
6405: }
6406: }
6407: dtbl.maxcode[17] = 0xFFFFF; /* ensures jpeg_huff_decode terminates */
6408:
6409: /* Compute lookahead tables to speed up decoding.
6410: * First we set all the table entries to 0, indicating "too long";
6411: * then we iterate through the Huffman codes that are short enough and
6412: * fill in all the entries that correspond to bit sequences starting
6413: * with that code.
6414: */
6415:
6416: for (int j = 0; j < dtbl.look_nbits.length; j++) {
6417: dtbl.look_nbits[j] = 0;
6418: }
6419:
6420: p = 0;
6421: for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
6422: for (i = 1; i <= (htbl.bits[l] & 0xFF); i++, p++) {
6423: /* l = current code's length, p = its index in huffcode[] & huffval[]. */
6424: /* Generate left-justified code followed by all possible bit sequences */
6425: lookbits = huffcode[p] << (HUFF_LOOKAHEAD - l);
6426: for (ctr = 1 << (HUFF_LOOKAHEAD - l); ctr > 0; ctr--) {
6427: dtbl.look_nbits[lookbits] = l;
6428: dtbl.look_sym[lookbits] = htbl.huffval[p];
6429: lookbits++;
6430: }
6431: }
6432: }
6433:
6434: /* Validate symbols as being reasonable.
6435: * For AC tables, we make no check, but accept all byte values 0..255.
6436: * For DC tables, we require the symbols to be in range 0..15.
6437: * (Tighter bounds could be applied depending on the data depth and mode,
6438: * but this is sufficient to ensure safe decoding.)
6439: */
6440: if (isDC) {
6441: for (i = 0; i < numsymbols; i++) {
6442: int sym = htbl.huffval[i] & 0xFF;
6443: if (sym < 0 || sym > 15)
6444: error();
6445: // ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
6446: }
6447: }
6448: }
6449:
6450: static void start_input_pass(jpeg_decompress_struct cinfo) {
6451: per_scan_setup(cinfo);
6452: latch_quant_tables(cinfo);
6453: cinfo.entropy.start_pass(cinfo);
6454: cinfo.coef.start_input_pass(cinfo);
6455: cinfo.inputctl.consume_input = COEF_CONSUME_INPUT;
6456: }
6457:
6458: static void finish_input_pass(jpeg_decompress_struct cinfo) {
6459: cinfo.inputctl.consume_input = INPUT_CONSUME_INPUT;
6460: }
6461:
6462: static int consume_markers(jpeg_decompress_struct cinfo) {
6463: jpeg_input_controller inputctl = cinfo.inputctl;
6464: int val;
6465:
6466: if (inputctl.eoi_reached) /* After hitting EOI, read no further */
6467: return JPEG_REACHED_EOI;
6468:
6469: val = read_markers(cinfo);
6470:
6471: switch (val) {
6472: case JPEG_REACHED_SOS: /* Found SOS */
6473: if (inputctl.inheaders) { /* 1st SOS */
6474: initial_setup(cinfo);
6475: inputctl.inheaders = false;
6476: /* Note: start_input_pass must be called by jdmaster.c
6477: * before any more input can be consumed. jdapimin.c is
6478: * responsible for enforcing this sequencing.
6479: */
6480: } else { /* 2nd or later SOS marker */
6481: if (!inputctl.has_multiple_scans)
6482: error();
6483: // ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
6484: start_input_pass(cinfo);
6485: }
6486: break;
6487: case JPEG_REACHED_EOI: /* Found EOI */
6488: inputctl.eoi_reached = true;
6489: if (inputctl.inheaders) { /* Tables-only datastream, apparently */
6490: if (cinfo.marker.saw_SOF)
6491: error();
6492: // ERREXIT(cinfo, JERR_SOF_NO_SOS);
6493: } else {
6494: /* Prevent infinite loop in coef ctlr's decompress_data routine
6495: * if user set output_scan_number larger than number of scans.
6496: */
6497: if (cinfo.output_scan_number > cinfo.input_scan_number)
6498: cinfo.output_scan_number = cinfo.input_scan_number;
6499: }
6500: break;
6501: case JPEG_SUSPENDED:
6502: break;
6503: }
6504:
6505: return val;
6506: }
6507:
6508: static void default_decompress_parms(jpeg_decompress_struct cinfo) {
6509: /* Guess the input colorspace, and set output colorspace accordingly. */
6510: /* (Wish JPEG committee had provided a real way to specify this...) */
6511: /* Note application may override our guesses. */
6512: switch (cinfo.num_components) {
6513: case 1:
6514: cinfo.jpeg_color_space = JCS_GRAYSCALE;
6515: cinfo.out_color_space = JCS_GRAYSCALE;
6516: break;
6517:
6518: case 3:
6519: if (cinfo.saw_JFIF_marker) {
6520: cinfo.jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
6521: } else if (cinfo.saw_Adobe_marker) {
6522: switch (cinfo.Adobe_transform) {
6523: case 0:
6524: cinfo.jpeg_color_space = JCS_RGB;
6525: break;
6526: case 1:
6527: cinfo.jpeg_color_space = JCS_YCbCr;
6528: break;
6529: default:
6530: // WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo.Adobe_transform);
6531: cinfo.jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
6532: break;
6533: }
6534: } else {
6535: /* Saw no special markers, try to guess from the component IDs */
6536: int cid0 = cinfo.comp_info[0].component_id;
6537: int cid1 = cinfo.comp_info[1].component_id;
6538: int cid2 = cinfo.comp_info[2].component_id;
6539:
6540: if (cid0 == 1 && cid1 == 2 && cid2 == 3)
6541: cinfo.jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
6542: else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
6543: cinfo.jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
6544: else {
6545: // TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
6546: cinfo.jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
6547: }
6548: }
6549: /* Always guess RGB is proper output colorspace. */
6550: cinfo.out_color_space = JCS_RGB;
6551: break;
6552:
6553: case 4:
6554: if (cinfo.saw_Adobe_marker) {
6555: switch (cinfo.Adobe_transform) {
6556: case 0:
6557: cinfo.jpeg_color_space = JCS_CMYK;
6558: break;
6559: case 2:
6560: cinfo.jpeg_color_space = JCS_YCCK;
6561: break;
6562: default:
6563: // WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo.Adobe_transform);
6564: cinfo.jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
6565: break;
6566: }
6567: } else {
6568: /* No special markers, assume straight CMYK. */
6569: cinfo.jpeg_color_space = JCS_CMYK;
6570: }
6571: cinfo.out_color_space = JCS_CMYK;
6572: break;
6573:
6574: default:
6575: cinfo.jpeg_color_space = JCS_UNKNOWN;
6576: cinfo.out_color_space = JCS_UNKNOWN;
6577: break;
6578: }
6579:
6580: /* Set defaults for other decompression parameters. */
6581: cinfo.scale_num = 1; /* 1:1 scaling */
6582: cinfo.scale_denom = 1;
6583: cinfo.output_gamma = 1.0;
6584: cinfo.buffered_image = false;
6585: cinfo.raw_data_out = false;
6586: cinfo.dct_method = JDCT_DEFAULT;
6587: cinfo.do_fancy_upsampling = true;
6588: cinfo.do_block_smoothing = true;
6589: cinfo.quantize_colors = false;
6590: /* We set these in case application only sets quantize_colors. */
6591: cinfo.dither_mode = JDITHER_FS;
6592: cinfo.two_pass_quantize = true;
6593: cinfo.desired_number_of_colors = 256;
6594: cinfo.colormap = null;
6595: /* Initialize for no mode change in buffered-image mode. */
6596: cinfo.enable_1pass_quant = false;
6597: cinfo.enable_external_quant = false;
6598: cinfo.enable_2pass_quant = false;
6599: }
6600:
6601: static void init_source(jpeg_decompress_struct cinfo) {
6602: cinfo.buffer = new byte[INPUT_BUFFER_SIZE];
6603: cinfo.bytes_in_buffer = 0;
6604: cinfo.bytes_offset = 0;
6605: cinfo.start_of_file = true;
6606: }
6607:
6608: static int jpeg_consume_input(jpeg_decompress_struct cinfo) {
6609: int retcode = JPEG_SUSPENDED;
6610:
6611: /* NB: every possible DSTATE value should be listed in this switch */
6612: switch (cinfo.global_state) {
6613: case DSTATE_START:
6614: /* Start-of-datastream actions: reset appropriate modules */
6615: reset_input_controller(cinfo);
6616: /* Initialize application's data source module */
6617: init_source(cinfo);
6618: cinfo.global_state = DSTATE_INHEADER;
6619: /*FALLTHROUGH*/
6620: case DSTATE_INHEADER:
6621: retcode = consume_input(cinfo);
6622: if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
6623: /* Set up default parameters based on header data */
6624: default_decompress_parms(cinfo);
6625: /* Set global state: ready for start_decompress */
6626: cinfo.global_state = DSTATE_READY;
6627: }
6628: break;
6629: case DSTATE_READY:
6630: /* Can't advance past first SOS until start_decompress is called */
6631: retcode = JPEG_REACHED_SOS;
6632: break;
6633: case DSTATE_PRELOAD:
6634: case DSTATE_PRESCAN:
6635: case DSTATE_SCANNING:
6636: case DSTATE_RAW_OK:
6637: case DSTATE_BUFIMAGE:
6638: case DSTATE_BUFPOST:
6639: case DSTATE_STOPPING:
6640: retcode = consume_input(cinfo);
6641: break;
6642: default:
6643: error();
6644: // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
6645: }
6646: return retcode;
6647: }
6648:
6649: static void jpeg_abort(jpeg_decompress_struct cinfo) {
6650: // int pool;
6651: //
6652: // /* Releasing pools in reverse order might help avoid fragmentation
6653: // * with some (brain-damaged) malloc libraries.
6654: // */
6655: // for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
6656: // (*cinfo.mem.free_pool) (cinfo, pool);
6657: // }
6658:
6659: /* Reset overall state for possible reuse of object */
6660: if (cinfo.is_decompressor) {
6661: cinfo.global_state = DSTATE_START;
6662: /* Try to keep application from accessing now-deleted marker list.
6663: * A bit kludgy to do it here, but this is the most central place.
6664: */
6665: // ((j_decompress_ptr) cinfo).marker_list = null;
6666: } else {
6667: cinfo.global_state = CSTATE_START;
6668: }
6669: }
6670:
6671: static boolean isFileFormat(LEDataInputStream stream) {
6672: try {
6673: byte[] buffer = new byte[2];
6674: stream.read(buffer);
6675: stream.unread(buffer);
6676: return (buffer[0] & 0xFF) == 0xFF
6677: && (buffer[1] & 0xFF) == M_SOI;
6678: } catch (Exception e) {
6679: return false;
6680: }
6681: }
6682:
6683: static ImageData[] loadFromByteStream(InputStream inputStream,
6684: ImageLoader loader) {
6685: jpeg_decompress_struct cinfo = new jpeg_decompress_struct();
6686: cinfo.inputStream = inputStream;
6687: jpeg_create_decompress(cinfo);
6688: jpeg_read_header(cinfo, true);
6689: cinfo.buffered_image = cinfo.progressive_mode
6690: && loader.hasListeners();
6691: jpeg_start_decompress(cinfo);
6692: PaletteData palette = null;
6693: switch (cinfo.out_color_space) {
6694: case JCS_RGB:
6695: palette = new PaletteData(0xFF, 0xFF00, 0xFF0000);
6696: break;
6697: case JCS_GRAYSCALE:
6698: RGB[] colors = new RGB[256];
6699: for (int i = 0; i < colors.length; i++) {
6700: colors[i] = new RGB(i, i, i);
6701: }
6702: palette = new PaletteData(colors);
6703: break;
6704: default:
6705: error();
6706: }
6707: int scanlinePad = 4;
6708: int row_stride = (((cinfo.output_width
6709: * cinfo.out_color_components * 8 + 7) / 8) + (scanlinePad - 1))
6710: / scanlinePad * scanlinePad;
6711: byte[][] buffer = new byte[1][row_stride];
6712: byte[] data = new byte[row_stride * cinfo.output_height];
6713: ImageData imageData = ImageData.internal_new(
6714: cinfo.output_width, cinfo.output_height,
6715: palette.isDirect ? 24 : 8, palette, scanlinePad, data,
6716: 0, null, null, -1, -1, SWT.IMAGE_JPEG, 0, 0, 0, 0);
6717: if (cinfo.buffered_image) {
6718: boolean done;
6719: do {
6720: int incrementCount = cinfo.input_scan_number - 1;
6721: jpeg_start_output(cinfo, cinfo.input_scan_number);
6722: while (cinfo.output_scanline < cinfo.output_height) {
6723: int offset = row_stride * cinfo.output_scanline;
6724: jpeg_read_scanlines(cinfo, buffer, 1);
6725: System.arraycopy(buffer[0], 0, data, offset,
6726: row_stride);
6727: }
6728: jpeg_finish_output(cinfo);
6729: loader.notifyListeners(new ImageLoaderEvent(loader,
6730: (ImageData) imageData.clone(), incrementCount,
6731: done = jpeg_input_complete(cinfo)));
6732: } while (!done);
6733: } else {
6734: while (cinfo.output_scanline < cinfo.output_height) {
6735: int offset = row_stride * cinfo.output_scanline;
6736: jpeg_read_scanlines(cinfo, buffer, 1);
6737: System
6738: .arraycopy(buffer[0], 0, data, offset,
6739: row_stride);
6740: }
6741: }
6742: jpeg_finish_decompress(cinfo);
6743: jpeg_destroy_decompress(cinfo);
6744: return new ImageData[] { imageData };
6745: }
6746:
6747: }
|