jdtrans.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. Unit JdTrans;
  2. { This file contains library routines for transcoding decompression,
  3. that is, reading raw DCT coefficient arrays from an input JPEG file.
  4. The routines in jdapimin.c will also be needed by a transcoder. }
  5. { Original : jdtrans.c ; Copyright (C) 1995-1997, Thomas G. Lane. }
  6. interface
  7. {$I jconfig.inc}
  8. uses
  9. jmorecfg,
  10. jinclude,
  11. jdeferr,
  12. jerror,
  13. jpeglib,
  14. jdhuff, jdphuff, jdcoefct;
  15. { Read the coefficient arrays from a JPEG file.
  16. jpeg_read_header must be completed before calling this.
  17. The entire image is read into a set of virtual coefficient-block arrays,
  18. one per component. The return value is a pointer to the array of
  19. virtual-array descriptors. These can be manipulated directly via the
  20. JPEG memory manager, or handed off to jpeg_write_coefficients().
  21. To release the memory occupied by the virtual arrays, call
  22. jpeg_finish_decompress() when done with the data.
  23. An alternative usage is to simply obtain access to the coefficient arrays
  24. during a buffered-image-mode decompression operation. This is allowed
  25. after any jpeg_finish_output() call. The arrays can be accessed until
  26. jpeg_finish_decompress() is called. (Note that any call to the library
  27. may reposition the arrays, so don't rely on access_virt_barray() results
  28. to stay valid across library calls.)
  29. Returns NIL if suspended. This case need be checked only if
  30. a suspending data source is used. }
  31. {GLOBAL}
  32. function jpeg_read_coefficients
  33. (cinfo : j_decompress_ptr) : jvirt_barray_tbl_ptr;
  34. implementation
  35. { Forward declarations }
  36. {LOCAL}
  37. procedure transdecode_master_selection (cinfo : j_decompress_ptr); forward;
  38. { Read the coefficient arrays from a JPEG file.
  39. jpeg_read_header must be completed before calling this.
  40. The entire image is read into a set of virtual coefficient-block arrays,
  41. one per component. The return value is a pointer to the array of
  42. virtual-array descriptors. These can be manipulated directly via the
  43. JPEG memory manager, or handed off to jpeg_write_coefficients().
  44. To release the memory occupied by the virtual arrays, call
  45. jpeg_finish_decompress() when done with the data.
  46. Returns NIL if suspended. This case need be checked only if
  47. a suspending data source is used. }
  48. {GLOBAL}
  49. function jpeg_read_coefficients
  50. (cinfo : j_decompress_ptr) : jvirt_barray_tbl_ptr;
  51. var
  52. retcode : int;
  53. begin
  54. if (cinfo^.global_state = DSTATE_READY) then
  55. begin
  56. { First call: initialize active modules }
  57. transdecode_master_selection(cinfo);
  58. cinfo^.global_state := DSTATE_RDCOEFS;
  59. end;
  60. if (cinfo^.global_state = DSTATE_RDCOEFS) then
  61. begin
  62. { Absorb whole file into the coef buffer }
  63. while TRUE do
  64. begin
  65. { Call progress monitor hook if present }
  66. if (cinfo^.progress <> NIL) then
  67. cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  68. { Absorb some more input }
  69. retcode := cinfo^.inputctl^.consume_input (cinfo);
  70. if (retcode = JPEG_SUSPENDED) then
  71. begin
  72. jpeg_read_coefficients := NIL;
  73. exit;
  74. end;
  75. if (retcode = JPEG_REACHED_EOI) then
  76. break;
  77. { Advance progress counter if appropriate }
  78. if (cinfo^.progress <> NIL) and
  79. ((retcode = JPEG_ROW_COMPLETED) or (retcode = JPEG_REACHED_SOS)) then
  80. begin
  81. Inc(cinfo^.progress^.pass_counter);
  82. if (cinfo^.progress^.pass_counter >= cinfo^.progress^.pass_limit) then
  83. begin
  84. { startup underestimated number of scans; ratchet up one scan }
  85. Inc(cinfo^.progress^.pass_limit, long(cinfo^.total_iMCU_rows));
  86. end;
  87. end;
  88. end;
  89. { Set state so that jpeg_finish_decompress does the right thing }
  90. cinfo^.global_state := DSTATE_STOPPING;
  91. end;
  92. { At this point we should be in state DSTATE_STOPPING if being used
  93. standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
  94. to the coefficients during a full buffered-image-mode decompression. }
  95. if ((cinfo^.global_state = DSTATE_STOPPING) or
  96. (cinfo^.global_state = DSTATE_BUFIMAGE)) and (cinfo^.buffered_image) then
  97. begin
  98. jpeg_read_coefficients := cinfo^.coef^.coef_arrays;
  99. exit;
  100. end;
  101. { Oops, improper usage }
  102. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  103. jpeg_read_coefficients := NIL; { keep compiler happy }
  104. end;
  105. { Master selection of decompression modules for transcoding.
  106. This substitutes for jdmaster.c's initialization of the full decompressor. }
  107. {LOCAL}
  108. procedure transdecode_master_selection (cinfo : j_decompress_ptr);
  109. var
  110. nscans : int;
  111. begin
  112. { This is effectively a buffered-image operation. }
  113. cinfo^.buffered_image := TRUE;
  114. { Entropy decoding: either Huffman or arithmetic coding. }
  115. if (cinfo^.arith_code) then
  116. begin
  117. ERREXIT(j_common_ptr(cinfo), JERR_ARITH_NOTIMPL);
  118. end
  119. else
  120. begin
  121. if (cinfo^.progressive_mode) then
  122. begin
  123. {$ifdef D_PROGRESSIVE_SUPPORTED}
  124. jinit_phuff_decoder(cinfo);
  125. {$else}
  126. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  127. {$endif}
  128. end
  129. else
  130. jinit_huff_decoder(cinfo);
  131. end;
  132. { Always get a full-image coefficient buffer. }
  133. jinit_d_coef_controller(cinfo, TRUE);
  134. { We can now tell the memory manager to allocate virtual arrays. }
  135. cinfo^.mem^.realize_virt_arrays (j_common_ptr(cinfo));
  136. { Initialize input side of decompressor to consume first scan. }
  137. cinfo^.inputctl^.start_input_pass (cinfo);
  138. { Initialize progress monitoring. }
  139. if (cinfo^.progress <> NIL) then
  140. begin
  141. { Estimate number of scans to set pass_limit. }
  142. if (cinfo^.progressive_mode) then
  143. begin
  144. { Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. }
  145. nscans := 2 + 3 * cinfo^.num_components;
  146. end
  147. else
  148. if (cinfo^.inputctl^.has_multiple_scans) then
  149. begin
  150. { For a nonprogressive multiscan file, estimate 1 scan per component. }
  151. nscans := cinfo^.num_components;
  152. end
  153. else
  154. begin
  155. nscans := 1;
  156. end;
  157. cinfo^.progress^.pass_counter := long(0);
  158. cinfo^.progress^.pass_limit := long(cinfo^.total_iMCU_rows * nscans);
  159. cinfo^.progress^.completed_passes := 0;
  160. cinfo^.progress^.total_passes := 1;
  161. end;
  162. end;
  163. end.