jdtrans.pas 6.4 KB

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