jdtrans.c 4.9 KB

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