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