Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * jdtrans.c |
| 3 | * |
| 4 | * Copyright (C) 1995-1997, Thomas G. Lane. |
| 5 | * This file is part of the Independent JPEG Group's software. |
| 6 | * For conditions of distribution and use, see the accompanying README file. |
| 7 | * |
| 8 | * This file contains library routines for transcoding decompression, |
| 9 | * that is, reading raw DCT coefficient arrays from an input JPEG file. |
| 10 | * The routines in jdapimin.c will also be needed by a transcoder. |
| 11 | */ |
| 12 | |
| 13 | #define JPEG_INTERNALS |
| 14 | #include "jinclude.h" |
| 15 | #include "jpeglib.h" |
| 16 | |
| 17 | |
| 18 | /* Forward declarations */ |
| 19 | LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo)); |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | * Read the coefficient arrays from a JPEG file. |
| 24 | * jpeg_read_header must be completed before calling this. |
| 25 | * |
| 26 | * The entire image is read into a set of virtual coefficient-block arrays, |
| 27 | * one per component. The return value is a pointer to the array of |
| 28 | * virtual-array descriptors. These can be manipulated directly via the |
| 29 | * JPEG memory manager, or handed off to jpeg_write_coefficients(). |
| 30 | * To release the memory occupied by the virtual arrays, call |
| 31 | * jpeg_finish_decompress() when done with the data. |
| 32 | * |
| 33 | * An alternative usage is to simply obtain access to the coefficient arrays |
| 34 | * during a buffered-image-mode decompression operation. This is allowed |
| 35 | * after any jpeg_finish_output() call. The arrays can be accessed until |
| 36 | * jpeg_finish_decompress() is called. (Note that any call to the library |
| 37 | * may reposition the arrays, so don't rely on access_virt_barray() results |
| 38 | * to stay valid across library calls.) |
| 39 | * |
| 40 | * Returns NULL if suspended. This case need be checked only if |
| 41 | * a suspending data source is used. |
| 42 | */ |
| 43 | |
| 44 | GLOBAL(jvirt_barray_ptr *) |
| 45 | jpeg_read_coefficients (j_decompress_ptr cinfo) |
| 46 | { |
| 47 | if (cinfo->global_state == DSTATE_READY) { |
| 48 | /* First call: initialize active modules */ |
| 49 | transdecode_master_selection(cinfo); |
| 50 | cinfo->global_state = DSTATE_RDCOEFS; |
| 51 | } |
| 52 | if (cinfo->global_state == DSTATE_RDCOEFS) { |
| 53 | /* Absorb whole file into the coef buffer */ |
| 54 | for (;;) { |
| 55 | int retcode; |
| 56 | /* Call progress monitor hook if present */ |
| 57 | if (cinfo->progress != NULL) |
| 58 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
| 59 | /* Absorb some more input */ |
| 60 | retcode = (*cinfo->inputctl->consume_input) (cinfo); |
| 61 | if (retcode == JPEG_SUSPENDED) |
| 62 | return NULL; |
| 63 | if (retcode == JPEG_REACHED_EOI) |
| 64 | break; |
| 65 | /* Advance progress counter if appropriate */ |
| 66 | if (cinfo->progress != NULL && |
| 67 | (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { |
| 68 | if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { |
| 69 | /* startup underestimated number of scans; ratchet up one scan */ |
| 70 | cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | /* Set state so that jpeg_finish_decompress does the right thing */ |
| 75 | cinfo->global_state = DSTATE_STOPPING; |
| 76 | } |
| 77 | /* At this point we should be in state DSTATE_STOPPING if being used |
| 78 | * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access |
| 79 | * to the coefficients during a full buffered-image-mode decompression. |
| 80 | */ |
| 81 | if ((cinfo->global_state == DSTATE_STOPPING || |
| 82 | cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) { |
| 83 | return cinfo->coef->coef_arrays; |
| 84 | } |
| 85 | /* Oops, improper usage */ |
| 86 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 87 | return NULL; /* keep compiler happy */ |
| 88 | } |
| 89 | |
| 90 | LOCAL(boolean) |
| 91 | jpeg_build_huffman_index_progressive(j_decompress_ptr cinfo, |
| 92 | huffman_index *index) |
| 93 | { |
| 94 | if (cinfo->global_state == DSTATE_READY) { |
| 95 | printf("Progressive Mode\n"); |
| 96 | /* First call: initialize active modules */ |
| 97 | transdecode_master_selection(cinfo); |
| 98 | cinfo->global_state = DSTATE_RDCOEFS; |
| 99 | } |
| 100 | if (cinfo->global_state == DSTATE_RDCOEFS) { |
| 101 | unsigned int mcu; |
| 102 | int i; |
| 103 | cinfo->marker->get_sos_marker_position(cinfo, index); |
| 104 | |
| 105 | /* Absorb whole file into the coef buffer */ |
| 106 | for (mcu = 0; mcu < cinfo->total_iMCU_rows; mcu++) { |
| 107 | int retcode = 0; |
| 108 | /* Call progress monitor hook if present */ |
| 109 | if (cinfo->progress != NULL) |
| 110 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
| 111 | /* Absorb some more input */ |
| 112 | jinit_phuff_decoder(cinfo); |
| 113 | for (i = 0; i < index->scan_count; i++) { |
| 114 | (*cinfo->inputctl->finish_input_pass) (cinfo); |
| 115 | jset_input_stream_position(cinfo, index->scan[i].bitstream_offset); |
| 116 | cinfo->unread_marker = 0; |
| 117 | retcode = (*cinfo->inputctl->consume_input_build_huffman_index) |
| 118 | (cinfo, index, i); |
| 119 | if (retcode == JPEG_REACHED_EOI) |
| 120 | break; |
| 121 | cinfo->input_iMCU_row = mcu; |
| 122 | if (mcu != 0) |
| 123 | (*cinfo->entropy->configure_huffman_decoder) |
| 124 | (cinfo, index->scan[i].prev_MCU_offset); |
| 125 | cinfo->input_scan_number = i; |
| 126 | retcode = (*cinfo->inputctl->consume_input_build_huffman_index) |
| 127 | (cinfo, index, i); |
| 128 | } |
| 129 | if (retcode == JPEG_SUSPENDED) |
| 130 | return FALSE; |
| 131 | if (retcode == JPEG_REACHED_EOI) |
| 132 | break; |
| 133 | /* Advance progress counter if appropriate */ |
| 134 | if (cinfo->progress != NULL && |
| 135 | (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { |
| 136 | if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { |
| 137 | /* startup underestimated number of scans; ratchet up one scan */ |
| 138 | cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | cinfo->global_state = DSTATE_STOPPING; |
| 143 | } |
| 144 | /* At this point we should be in state DSTATE_STOPPING if being used |
| 145 | * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access |
| 146 | * to the coefficients during a full buffered-image-mode decompression. |
| 147 | */ |
| 148 | if ((cinfo->global_state == DSTATE_STOPPING || |
| 149 | cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) { |
| 150 | return TRUE; |
| 151 | } |
| 152 | /* Oops, improper usage */ |
| 153 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 154 | return FALSE; /* keep compiler happy */ |
| 155 | } |
| 156 | |
| 157 | LOCAL(boolean) |
| 158 | jpeg_build_huffman_index_baseline(j_decompress_ptr cinfo, huffman_index *index) |
| 159 | { |
| 160 | if (cinfo->global_state == DSTATE_READY) { |
| 161 | printf("Baseline Mode\n"); |
| 162 | /* First call: initialize active modules */ |
| 163 | transdecode_master_selection(cinfo); |
| 164 | cinfo->global_state = DSTATE_RDCOEFS; |
| 165 | } |
| 166 | if (cinfo->global_state == DSTATE_RDCOEFS) { |
| 167 | /* Absorb whole file into the coef buffer */ |
| 168 | for (;;) { |
| 169 | int retcode; |
| 170 | /* Call progress monitor hook if present */ |
| 171 | if (cinfo->progress != NULL) |
| 172 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
| 173 | /* Absorb some more input */ |
| 174 | retcode = (*cinfo->inputctl->consume_input_build_huffman_index) |
| 175 | (cinfo, index, 0); |
| 176 | if (retcode == JPEG_SUSPENDED) |
| 177 | return FALSE; |
| 178 | if (retcode == JPEG_REACHED_EOI) |
| 179 | break; |
| 180 | if (retcode == JPEG_SCAN_COMPLETED) |
| 181 | break; |
| 182 | |
| 183 | /* Advance progress counter if appropriate */ |
| 184 | if (cinfo->progress != NULL && |
| 185 | (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { |
| 186 | if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { |
| 187 | /* startup underestimated number of scans; ratchet up one scan */ |
| 188 | cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | /* Set state so that jpeg_finish_decompress does the right thing */ |
| 193 | cinfo->global_state = DSTATE_STOPPING; |
| 194 | } |
| 195 | /* At this point we should be in state DSTATE_STOPPING if being used |
| 196 | * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access |
| 197 | * to the coefficients during a full buffered-image-mode decompression. |
| 198 | */ |
| 199 | if ((cinfo->global_state == DSTATE_STOPPING || |
| 200 | cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) { |
| 201 | return TRUE; |
| 202 | } |
| 203 | /* Oops, improper usage */ |
| 204 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 205 | return FALSE; /* keep compiler happy */ |
| 206 | } |
| 207 | |
| 208 | GLOBAL(boolean) |
| 209 | jpeg_build_huffman_index(j_decompress_ptr cinfo, huffman_index *index) |
| 210 | { |
| 211 | cinfo->tile_decode = TRUE; |
| 212 | if (cinfo->progressive_mode) |
| 213 | return jpeg_build_huffman_index_progressive(cinfo, index); |
| 214 | else |
| 215 | return jpeg_build_huffman_index_baseline(cinfo, index); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Master selection of decompression modules for transcoding. |
| 220 | * This substitutes for jdmaster.c's initialization of the full decompressor. |
| 221 | */ |
| 222 | |
| 223 | LOCAL(void) |
| 224 | transdecode_master_selection (j_decompress_ptr cinfo) |
| 225 | { |
| 226 | /* This is effectively a buffered-image operation. */ |
| 227 | cinfo->buffered_image = TRUE; |
| 228 | |
| 229 | /* Entropy decoding: either Huffman or arithmetic coding. */ |
| 230 | if (cinfo->arith_code) { |
| 231 | ERREXIT(cinfo, JERR_ARITH_NOTIMPL); |
| 232 | } else { |
| 233 | if (cinfo->progressive_mode) { |
| 234 | #ifdef D_PROGRESSIVE_SUPPORTED |
| 235 | jinit_phuff_decoder(cinfo); |
| 236 | #else |
| 237 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 238 | #endif |
| 239 | } else { |
| 240 | jinit_huff_decoder(cinfo); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* Always get a full-image coefficient buffer. */ |
| 245 | jinit_d_coef_controller(cinfo, TRUE); |
| 246 | |
| 247 | /* We can now tell the memory manager to allocate virtual arrays. */ |
| 248 | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); |
| 249 | |
| 250 | /* Initialize input side of decompressor to consume first scan. */ |
| 251 | (*cinfo->inputctl->start_input_pass) (cinfo); |
| 252 | |
| 253 | /* Initialize progress monitoring. */ |
| 254 | if (cinfo->progress != NULL) { |
| 255 | int nscans; |
| 256 | /* Estimate number of scans to set pass_limit. */ |
| 257 | if (cinfo->progressive_mode) { |
| 258 | /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ |
| 259 | nscans = 2 + 3 * cinfo->num_components; |
| 260 | } else if (cinfo->inputctl->has_multiple_scans) { |
| 261 | /* For a nonprogressive multiscan file, estimate 1 scan per component. */ |
| 262 | nscans = cinfo->num_components; |
| 263 | } else { |
| 264 | nscans = 1; |
| 265 | } |
| 266 | cinfo->progress->pass_counter = 0L; |
| 267 | cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; |
| 268 | cinfo->progress->completed_passes = 0; |
| 269 | cinfo->progress->total_passes = 1; |
| 270 | } |
| 271 | } |