jdtrans.cpp 4.4 KB

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