jdtrans.c 5.3 KB

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