Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * jdtrans.c |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 1995-1997, Thomas G. Lane. |
michael@0 | 5 | * This file is part of the Independent JPEG Group's software. |
michael@0 | 6 | * For conditions of distribution and use, see the accompanying README file. |
michael@0 | 7 | * |
michael@0 | 8 | * This file contains library routines for transcoding decompression, |
michael@0 | 9 | * that is, reading raw DCT coefficient arrays from an input JPEG file. |
michael@0 | 10 | * The routines in jdapimin.c will also be needed by a transcoder. |
michael@0 | 11 | */ |
michael@0 | 12 | |
michael@0 | 13 | #define JPEG_INTERNALS |
michael@0 | 14 | #include "jinclude.h" |
michael@0 | 15 | #include "jpeglib.h" |
michael@0 | 16 | |
michael@0 | 17 | |
michael@0 | 18 | /* Forward declarations */ |
michael@0 | 19 | LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo)); |
michael@0 | 20 | |
michael@0 | 21 | |
michael@0 | 22 | /* |
michael@0 | 23 | * Read the coefficient arrays from a JPEG file. |
michael@0 | 24 | * jpeg_read_header must be completed before calling this. |
michael@0 | 25 | * |
michael@0 | 26 | * The entire image is read into a set of virtual coefficient-block arrays, |
michael@0 | 27 | * one per component. The return value is a pointer to the array of |
michael@0 | 28 | * virtual-array descriptors. These can be manipulated directly via the |
michael@0 | 29 | * JPEG memory manager, or handed off to jpeg_write_coefficients(). |
michael@0 | 30 | * To release the memory occupied by the virtual arrays, call |
michael@0 | 31 | * jpeg_finish_decompress() when done with the data. |
michael@0 | 32 | * |
michael@0 | 33 | * An alternative usage is to simply obtain access to the coefficient arrays |
michael@0 | 34 | * during a buffered-image-mode decompression operation. This is allowed |
michael@0 | 35 | * after any jpeg_finish_output() call. The arrays can be accessed until |
michael@0 | 36 | * jpeg_finish_decompress() is called. (Note that any call to the library |
michael@0 | 37 | * may reposition the arrays, so don't rely on access_virt_barray() results |
michael@0 | 38 | * to stay valid across library calls.) |
michael@0 | 39 | * |
michael@0 | 40 | * Returns NULL if suspended. This case need be checked only if |
michael@0 | 41 | * a suspending data source is used. |
michael@0 | 42 | */ |
michael@0 | 43 | |
michael@0 | 44 | GLOBAL(jvirt_barray_ptr *) |
michael@0 | 45 | jpeg_read_coefficients (j_decompress_ptr cinfo) |
michael@0 | 46 | { |
michael@0 | 47 | if (cinfo->global_state == DSTATE_READY) { |
michael@0 | 48 | /* First call: initialize active modules */ |
michael@0 | 49 | transdecode_master_selection(cinfo); |
michael@0 | 50 | cinfo->global_state = DSTATE_RDCOEFS; |
michael@0 | 51 | } |
michael@0 | 52 | if (cinfo->global_state == DSTATE_RDCOEFS) { |
michael@0 | 53 | /* Absorb whole file into the coef buffer */ |
michael@0 | 54 | for (;;) { |
michael@0 | 55 | int retcode; |
michael@0 | 56 | /* Call progress monitor hook if present */ |
michael@0 | 57 | if (cinfo->progress != NULL) |
michael@0 | 58 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
michael@0 | 59 | /* Absorb some more input */ |
michael@0 | 60 | retcode = (*cinfo->inputctl->consume_input) (cinfo); |
michael@0 | 61 | if (retcode == JPEG_SUSPENDED) |
michael@0 | 62 | return NULL; |
michael@0 | 63 | if (retcode == JPEG_REACHED_EOI) |
michael@0 | 64 | break; |
michael@0 | 65 | /* Advance progress counter if appropriate */ |
michael@0 | 66 | if (cinfo->progress != NULL && |
michael@0 | 67 | (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { |
michael@0 | 68 | if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { |
michael@0 | 69 | /* startup underestimated number of scans; ratchet up one scan */ |
michael@0 | 70 | cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | /* Set state so that jpeg_finish_decompress does the right thing */ |
michael@0 | 75 | cinfo->global_state = DSTATE_STOPPING; |
michael@0 | 76 | } |
michael@0 | 77 | /* At this point we should be in state DSTATE_STOPPING if being used |
michael@0 | 78 | * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access |
michael@0 | 79 | * to the coefficients during a full buffered-image-mode decompression. |
michael@0 | 80 | */ |
michael@0 | 81 | if ((cinfo->global_state == DSTATE_STOPPING || |
michael@0 | 82 | cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) { |
michael@0 | 83 | return cinfo->coef->coef_arrays; |
michael@0 | 84 | } |
michael@0 | 85 | /* Oops, improper usage */ |
michael@0 | 86 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 87 | return NULL; /* keep compiler happy */ |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | |
michael@0 | 91 | /* |
michael@0 | 92 | * Master selection of decompression modules for transcoding. |
michael@0 | 93 | * This substitutes for jdmaster.c's initialization of the full decompressor. |
michael@0 | 94 | */ |
michael@0 | 95 | |
michael@0 | 96 | LOCAL(void) |
michael@0 | 97 | transdecode_master_selection (j_decompress_ptr cinfo) |
michael@0 | 98 | { |
michael@0 | 99 | /* This is effectively a buffered-image operation. */ |
michael@0 | 100 | cinfo->buffered_image = TRUE; |
michael@0 | 101 | |
michael@0 | 102 | #if JPEG_LIB_VERSION >= 80 |
michael@0 | 103 | /* Compute output image dimensions and related values. */ |
michael@0 | 104 | jpeg_core_output_dimensions(cinfo); |
michael@0 | 105 | #endif |
michael@0 | 106 | |
michael@0 | 107 | /* Entropy decoding: either Huffman or arithmetic coding. */ |
michael@0 | 108 | if (cinfo->arith_code) { |
michael@0 | 109 | #ifdef D_ARITH_CODING_SUPPORTED |
michael@0 | 110 | jinit_arith_decoder(cinfo); |
michael@0 | 111 | #else |
michael@0 | 112 | ERREXIT(cinfo, JERR_ARITH_NOTIMPL); |
michael@0 | 113 | #endif |
michael@0 | 114 | } else { |
michael@0 | 115 | if (cinfo->progressive_mode) { |
michael@0 | 116 | #ifdef D_PROGRESSIVE_SUPPORTED |
michael@0 | 117 | jinit_phuff_decoder(cinfo); |
michael@0 | 118 | #else |
michael@0 | 119 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
michael@0 | 120 | #endif |
michael@0 | 121 | } else |
michael@0 | 122 | jinit_huff_decoder(cinfo); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | /* Always get a full-image coefficient buffer. */ |
michael@0 | 126 | jinit_d_coef_controller(cinfo, TRUE); |
michael@0 | 127 | |
michael@0 | 128 | /* We can now tell the memory manager to allocate virtual arrays. */ |
michael@0 | 129 | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); |
michael@0 | 130 | |
michael@0 | 131 | /* Initialize input side of decompressor to consume first scan. */ |
michael@0 | 132 | (*cinfo->inputctl->start_input_pass) (cinfo); |
michael@0 | 133 | |
michael@0 | 134 | /* Initialize progress monitoring. */ |
michael@0 | 135 | if (cinfo->progress != NULL) { |
michael@0 | 136 | int nscans; |
michael@0 | 137 | /* Estimate number of scans to set pass_limit. */ |
michael@0 | 138 | if (cinfo->progressive_mode) { |
michael@0 | 139 | /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ |
michael@0 | 140 | nscans = 2 + 3 * cinfo->num_components; |
michael@0 | 141 | } else if (cinfo->inputctl->has_multiple_scans) { |
michael@0 | 142 | /* For a nonprogressive multiscan file, estimate 1 scan per component. */ |
michael@0 | 143 | nscans = cinfo->num_components; |
michael@0 | 144 | } else { |
michael@0 | 145 | nscans = 1; |
michael@0 | 146 | } |
michael@0 | 147 | cinfo->progress->pass_counter = 0L; |
michael@0 | 148 | cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; |
michael@0 | 149 | cinfo->progress->completed_passes = 0; |
michael@0 | 150 | cinfo->progress->total_passes = 1; |
michael@0 | 151 | } |
michael@0 | 152 | } |