jcapistd.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. {$IFNDEF FPC_DOTTEDUNITS}
  2. Unit JcAPIstd;
  3. {$ENDIF FPC_DOTTEDUNITS}
  4. { Original : jcapistd.c ; Copyright (C) 1994-1996, 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. This file contains application interface code for the compression half
  8. of the JPEG library. These are the "standard" API routines that are
  9. used in the normal full-compression case. They are not used by a
  10. transcoding-only application. Note that if an application links in
  11. jpeg_start_compress, it will end up linking in the entire compressor.
  12. We thus must separate this file from jcapimin.c to avoid linking the
  13. whole compression library into a transcoder. }
  14. interface
  15. {$I jconfig.inc}
  16. {$IFDEF FPC_DOTTEDUNITS}
  17. uses
  18. System.Jpeg.Jmorecfg,
  19. System.Jpeg.Jinclude,
  20. System.Jpeg.Jdeferr,
  21. System.Jpeg.Jerror,
  22. System.Jpeg.Jpeglib,
  23. System.Jpeg.Jcapimin, System.Jpeg.Jcinit;
  24. {$ELSE FPC_DOTTEDUNITS}
  25. uses
  26. jmorecfg,
  27. jinclude,
  28. jdeferr,
  29. jerror,
  30. jpeglib,
  31. jcapimin, jcinit;
  32. {$ENDIF FPC_DOTTEDUNITS}
  33. { Compression initialization.
  34. Before calling this, all parameters and a data destination must be set up.
  35. We require a write_all_tables parameter as a failsafe check when writing
  36. multiple datastreams from the same compression object. Since prior runs
  37. will have left all the tables marked sent_table=TRUE, a subsequent run
  38. would emit an abbreviated stream (no tables) by default. This may be what
  39. is wanted, but for safety's sake it should not be the default behavior:
  40. programmers should have to make a deliberate choice to emit abbreviated
  41. images. Therefore the documentation and examples should encourage people
  42. to pass write_all_tables=TRUE; then it will take active thought to do the
  43. wrong thing. }
  44. {GLOBAL}
  45. procedure jpeg_start_compress (cinfo : j_compress_ptr;
  46. write_all_tables : boolean);
  47. { Write some scanlines of data to the JPEG compressor.
  48. The return value will be the number of lines actually written.
  49. This should be less than the supplied num_lines only in case that
  50. the data destination module has requested suspension of the compressor,
  51. or if more than image_height scanlines are passed in.
  52. Note: we warn about excess calls to jpeg_write_scanlines() since
  53. this likely signals an application programmer error. However,
  54. excess scanlines passed in the last valid call are *silently* ignored,
  55. so that the application need not adjust num_lines for end-of-image
  56. when using a multiple-scanline buffer. }
  57. {GLOBAL}
  58. function jpeg_write_scanlines (cinfo : j_compress_ptr;
  59. scanlines : JSAMPARRAY;
  60. num_lines : JDIMENSION) : JDIMENSION;
  61. { Alternate entry point to write raw data.
  62. Processes exactly one iMCU row per call, unless suspended. }
  63. {GLOBAL}
  64. function jpeg_write_raw_data (cinfo : j_compress_ptr;
  65. data : JSAMPIMAGE;
  66. num_lines : JDIMENSION) : JDIMENSION;
  67. implementation
  68. { Compression initialization.
  69. Before calling this, all parameters and a data destination must be set up.
  70. We require a write_all_tables parameter as a failsafe check when writing
  71. multiple datastreams from the same compression object. Since prior runs
  72. will have left all the tables marked sent_table=TRUE, a subsequent run
  73. would emit an abbreviated stream (no tables) by default. This may be what
  74. is wanted, but for safety's sake it should not be the default behavior:
  75. programmers should have to make a deliberate choice to emit abbreviated
  76. images. Therefore the documentation and examples should encourage people
  77. to pass write_all_tables=TRUE; then it will take active thought to do the
  78. wrong thing. }
  79. {GLOBAL}
  80. procedure jpeg_start_compress (cinfo : j_compress_ptr;
  81. write_all_tables : boolean);
  82. begin
  83. if (cinfo^.global_state <> CSTATE_START) then
  84. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  85. if (write_all_tables) then
  86. jpeg_suppress_tables(cinfo, FALSE); { mark all tables to be written }
  87. { (Re)initialize error mgr and destination modules }
  88. cinfo^.err^.reset_error_mgr (j_common_ptr(cinfo));
  89. cinfo^.dest^.init_destination (cinfo);
  90. { Perform master selection of active modules }
  91. jinit_compress_master(cinfo);
  92. { Set up for the first pass }
  93. cinfo^.master^.prepare_for_pass (cinfo);
  94. { Ready for application to drive first pass through jpeg_write_scanlines
  95. or jpeg_write_raw_data. }
  96. cinfo^.next_scanline := 0;
  97. if cinfo^.raw_data_in then
  98. cinfo^.global_state := CSTATE_RAW_OK
  99. else
  100. cinfo^.global_state := CSTATE_SCANNING;
  101. end;
  102. { Write some scanlines of data to the JPEG compressor.
  103. The return value will be the number of lines actually written.
  104. This should be less than the supplied num_lines only in case that
  105. the data destination module has requested suspension of the compressor,
  106. or if more than image_height scanlines are passed in.
  107. Note: we warn about excess calls to jpeg_write_scanlines() since
  108. this likely signals an application programmer error. However,
  109. excess scanlines passed in the last valid call are *silently* ignored,
  110. so that the application need not adjust num_lines for end-of-image
  111. when using a multiple-scanline buffer. }
  112. {GLOBAL}
  113. function jpeg_write_scanlines (cinfo : j_compress_ptr;
  114. scanlines : JSAMPARRAY;
  115. num_lines : JDIMENSION) : JDIMENSION;
  116. var
  117. row_ctr, rows_left : JDIMENSION;
  118. begin
  119. if (cinfo^.global_state <> CSTATE_SCANNING) then
  120. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  121. if (cinfo^.next_scanline >= cinfo^.image_height) then
  122. WARNMS(j_common_ptr(cinfo), JWRN_TOO_MUCH_DATA);
  123. { Call progress monitor hook if present }
  124. if (cinfo^.progress <> NIL) then
  125. begin
  126. cinfo^.progress^.pass_counter := long (cinfo^.next_scanline);
  127. cinfo^.progress^.pass_limit := long (cinfo^.image_height);
  128. cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  129. end;
  130. { Give master control module another chance if this is first call to
  131. jpeg_write_scanlines. This lets output of the frame/scan headers be
  132. delayed so that application can write COM, etc, markers between
  133. jpeg_start_compress and jpeg_write_scanlines. }
  134. if (cinfo^.master^.call_pass_startup) then
  135. cinfo^.master^.pass_startup (cinfo);
  136. { Ignore any extra scanlines at bottom of image. }
  137. rows_left := cinfo^.image_height - cinfo^.next_scanline;
  138. if (num_lines > rows_left) then
  139. num_lines := rows_left;
  140. row_ctr := 0;
  141. cinfo^.main^.process_data (cinfo, scanlines, {var}row_ctr, num_lines);
  142. Inc(cinfo^.next_scanline, row_ctr);
  143. jpeg_write_scanlines := row_ctr;
  144. end;
  145. { Alternate entry point to write raw data.
  146. Processes exactly one iMCU row per call, unless suspended. }
  147. {GLOBAL}
  148. function jpeg_write_raw_data (cinfo : j_compress_ptr;
  149. data : JSAMPIMAGE;
  150. num_lines : JDIMENSION) : JDIMENSION;
  151. var
  152. lines_per_iMCU_row : JDIMENSION;
  153. begin
  154. if (cinfo^.global_state <> CSTATE_RAW_OK) then
  155. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  156. if (cinfo^.next_scanline >= cinfo^.image_height) then
  157. begin
  158. WARNMS(j_common_ptr(cinfo), JWRN_TOO_MUCH_DATA);
  159. jpeg_write_raw_data := 0;
  160. exit;
  161. end;
  162. { Call progress monitor hook if present }
  163. if (cinfo^.progress <> NIL) then
  164. begin
  165. cinfo^.progress^.pass_counter := long(cinfo^.next_scanline);
  166. cinfo^.progress^.pass_limit := long(cinfo^.image_height);
  167. cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  168. end;
  169. { Give master control module another chance if this is first call to
  170. jpeg_write_raw_data. This lets output of the frame/scan headers be
  171. delayed so that application can write COM, etc, markers between
  172. jpeg_start_compress and jpeg_write_raw_data. }
  173. if (cinfo^.master^.call_pass_startup) then
  174. cinfo^.master^.pass_startup (cinfo);
  175. { Verify that at least one iMCU row has been passed. }
  176. lines_per_iMCU_row := cinfo^.max_v_samp_factor * DCTSIZE;
  177. if (num_lines < lines_per_iMCU_row) then
  178. ERREXIT(j_common_ptr(cinfo), JERR_BUFFER_SIZE);
  179. { Directly compress the row. }
  180. if (not cinfo^.coef^.compress_data (cinfo, data)) then
  181. begin
  182. { If compressor did not consume the whole row, suspend processing. }
  183. jpeg_write_raw_data := 0;
  184. exit;
  185. end;
  186. { OK, we processed one iMCU row. }
  187. Inc(cinfo^.next_scanline, lines_per_iMCU_row);
  188. jpeg_write_raw_data := lines_per_iMCU_row;
  189. end;
  190. end.