jdtrans.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // leave this as first line for PCH reasons...
  13. //
  14. #include "../server/exe_headers.h"
  15. #define JPEG_INTERNALS
  16. #include "jinclude.h"
  17. #include "jpeglib.h"
  18. /* Forward declarations */
  19. LOCAL void transdecode_master_selection JPP((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. * Returns NULL if suspended. This case need be checked only if
  32. * a suspending data source is used.
  33. */
  34. GLOBAL jvirt_barray_ptr *
  35. jpeg_read_coefficients (j_decompress_ptr cinfo)
  36. {
  37. if (cinfo->global_state == DSTATE_READY) {
  38. /* First call: initialize active modules */
  39. transdecode_master_selection(cinfo);
  40. cinfo->global_state = DSTATE_RDCOEFS;
  41. } else if (cinfo->global_state != DSTATE_RDCOEFS)
  42. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  43. /* Absorb whole file into the coef buffer */
  44. for (;;) {
  45. int retcode;
  46. /* Call progress monitor hook if present */
  47. if (cinfo->progress != NULL)
  48. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  49. /* Absorb some more input */
  50. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  51. if (retcode == JPEG_SUSPENDED)
  52. return NULL;
  53. if (retcode == JPEG_REACHED_EOI)
  54. break;
  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. {
  75. /* Entropy decoding: either Huffman or arithmetic coding. */
  76. if (cinfo->arith_code) {
  77. ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  78. } else {
  79. if (cinfo->progressive_mode) {
  80. #ifdef D_PROGRESSIVE_SUPPORTED
  81. jinit_phuff_decoder(cinfo);
  82. #else
  83. ERREXIT(cinfo, JERR_NOT_COMPILED);
  84. #endif
  85. } else
  86. jinit_huff_decoder(cinfo);
  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. }