jdtrans.c 5.3 KB

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