jdtrans.c 5.3 KB

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