jdtrans.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. {
  34. if (cinfo->global_state == DSTATE_READY) {
  35. /* First call: initialize active modules */
  36. transdecode_master_selection(cinfo);
  37. cinfo->global_state = DSTATE_RDCOEFS;
  38. } else if (cinfo->global_state != DSTATE_RDCOEFS)
  39. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  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. /* Absorb some more input */
  47. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  48. if (retcode == JPEG_SUSPENDED)
  49. return NULL;
  50. if (retcode == JPEG_REACHED_EOI)
  51. break;
  52. /* Advance progress counter if appropriate */
  53. if (cinfo->progress != NULL &&
  54. (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
  55. if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
  56. /* startup underestimated number of scans; ratchet up one scan */
  57. cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
  58. }
  59. }
  60. }
  61. /* Set state so that jpeg_finish_decompress does the right thing */
  62. cinfo->global_state = DSTATE_STOPPING;
  63. return cinfo->coef->coef_arrays;
  64. }
  65. /*
  66. * Master selection of decompression modules for transcoding.
  67. * This substitutes for jdmaster.c's initialization of the full decompressor.
  68. */
  69. LOCAL void
  70. transdecode_master_selection (j_decompress_ptr cinfo)
  71. {
  72. /* Entropy decoding: either Huffman or arithmetic coding. */
  73. if (cinfo->arith_code) {
  74. ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  75. } else {
  76. if (cinfo->progressive_mode) {
  77. #ifdef D_PROGRESSIVE_SUPPORTED
  78. jinit_phuff_decoder(cinfo);
  79. #else
  80. ERREXIT(cinfo, JERR_NOT_COMPILED);
  81. #endif
  82. } else
  83. jinit_huff_decoder(cinfo);
  84. }
  85. /* Always get a full-image coefficient buffer. */
  86. jinit_d_coef_controller(cinfo, TRUE);
  87. /* We can now tell the memory manager to allocate virtual arrays. */
  88. (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  89. /* Initialize input side of decompressor to consume first scan. */
  90. (*cinfo->inputctl->start_input_pass) (cinfo);
  91. /* Initialize progress monitoring. */
  92. if (cinfo->progress != NULL) {
  93. int nscans;
  94. /* Estimate number of scans to set pass_limit. */
  95. if (cinfo->progressive_mode) {
  96. /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  97. nscans = 2 + 3 * cinfo->num_components;
  98. } else if (cinfo->inputctl->has_multiple_scans) {
  99. /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  100. nscans = cinfo->num_components;
  101. } else {
  102. nscans = 1;
  103. }
  104. cinfo->progress->pass_counter = 0L;
  105. cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  106. cinfo->progress->completed_passes = 0;
  107. cinfo->progress->total_passes = 1;
  108. }
  109. }