/packages/pasjpeg/src/jctrans.pas

https://github.com/slibre/freepascal · Pascal · 459 lines · 274 code · 63 blank · 122 comment · 29 complexity · 642dfaa1e8843b23787ec2949788a25f MD5 · raw file

  1. Unit JcTrans;
  2. { This file contains library routines for transcoding compression,
  3. that is, writing raw DCT coefficient arrays to an output JPEG file.
  4. The routines in jcapimin.c will also be needed by a transcoder. }
  5. { Original : jctrans.c - Copyright (C) 1995-1998, Thomas G. Lane. }
  6. interface
  7. {$I jconfig.inc}
  8. uses
  9. jmorecfg,
  10. jinclude,
  11. jdeferr,
  12. jerror,
  13. jutils,
  14. jpeglib,
  15. jcapimin, jcparam, jcomapi, jcmaster, jchuff, jcphuff, jcmarker;
  16. { Compression initialization for writing raw-coefficient data.
  17. Before calling this, all parameters and a data destination must be set up.
  18. Call jpeg_finish_compress() to actually write the data.
  19. The number of passed virtual arrays must match cinfo^.num_components.
  20. Note that the virtual arrays need not be filled or even realized at
  21. the time write_coefficients is called; indeed, if the virtual arrays
  22. were requested from this compression object's memory manager, they
  23. typically will be realized during this routine and filled afterwards. }
  24. {GLOBAL}
  25. procedure jpeg_write_coefficients (cinfo : j_compress_ptr;
  26. coef_arrays : jvirt_barray_tbl_ptr);
  27. { Initialize the compression object with default parameters,
  28. then copy from the source object all parameters needed for lossless
  29. transcoding. Parameters that can be varied without loss (such as
  30. scan script and Huffman optimization) are left in their default states. }
  31. {GLOBAL}
  32. procedure jpeg_copy_critical_parameters (srcinfo : j_decompress_ptr;
  33. dstinfo : j_compress_ptr);
  34. implementation
  35. { Forward declarations }
  36. {LOCAL}
  37. procedure transencode_master_selection(cinfo : j_compress_ptr;
  38. coef_arrays : jvirt_barray_tbl_ptr);
  39. forward;
  40. {LOCAL}
  41. procedure transencode_coef_controller(cinfo : j_compress_ptr;
  42. coef_arrays : jvirt_barray_tbl_ptr);
  43. forward;
  44. { Compression initialization for writing raw-coefficient data.
  45. Before calling this, all parameters and a data destination must be set up.
  46. Call jpeg_finish_compress() to actually write the data.
  47. The number of passed virtual arrays must match cinfo^.num_components.
  48. Note that the virtual arrays need not be filled or even realized at
  49. the time write_coefficients is called; indeed, if the virtual arrays
  50. were requested from this compression object's memory manager, they
  51. typically will be realized during this routine and filled afterwards. }
  52. {GLOBAL}
  53. procedure jpeg_write_coefficients (cinfo : j_compress_ptr;
  54. coef_arrays : jvirt_barray_tbl_ptr);
  55. begin
  56. if (cinfo^.global_state <> CSTATE_START) then
  57. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  58. { Mark all tables to be written }
  59. jpeg_suppress_tables(cinfo, FALSE);
  60. { (Re)initialize error mgr and destination modules }
  61. cinfo^.err^.reset_error_mgr (j_common_ptr(cinfo));
  62. cinfo^.dest^.init_destination (cinfo);
  63. { Perform master selection of active modules }
  64. transencode_master_selection(cinfo, coef_arrays);
  65. { Wait for jpeg_finish_compress() call }
  66. cinfo^.next_scanline := 0; { so jpeg_write_marker works }
  67. cinfo^.global_state := CSTATE_WRCOEFS;
  68. end;
  69. { Initialize the compression object with default parameters,
  70. then copy from the source object all parameters needed for lossless
  71. transcoding. Parameters that can be varied without loss (such as
  72. scan script and Huffman optimization) are left in their default states. }
  73. {GLOBAL}
  74. procedure jpeg_copy_critical_parameters (srcinfo : j_decompress_ptr;
  75. dstinfo : j_compress_ptr);
  76. var
  77. qtblptr : ^JQUANT_TBL_PTR;
  78. incomp, outcomp : jpeg_component_info_ptr;
  79. c_quant, slot_quant : JQUANT_TBL_PTR;
  80. tblno, ci, coefi : int;
  81. begin
  82. { Safety check to ensure start_compress not called yet. }
  83. if (dstinfo^.global_state <> CSTATE_START) then
  84. ERREXIT1(j_common_ptr(dstinfo), JERR_BAD_STATE, dstinfo^.global_state);
  85. { Copy fundamental image dimensions }
  86. dstinfo^.image_width := srcinfo^.image_width;
  87. dstinfo^.image_height := srcinfo^.image_height;
  88. dstinfo^.input_components := srcinfo^.num_components;
  89. dstinfo^.in_color_space := srcinfo^.jpeg_color_space;
  90. { Initialize all parameters to default values }
  91. jpeg_set_defaults(dstinfo);
  92. { jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
  93. Fix it to get the right header markers for the image colorspace. }
  94. jpeg_set_colorspace(dstinfo, srcinfo^.jpeg_color_space);
  95. dstinfo^.data_precision := srcinfo^.data_precision;
  96. dstinfo^.CCIR601_sampling := srcinfo^.CCIR601_sampling;
  97. { Copy the source's quantization tables. }
  98. for tblno := 0 to pred(NUM_QUANT_TBLS) do
  99. begin
  100. if (srcinfo^.quant_tbl_ptrs[tblno] <> NIL) then
  101. begin
  102. qtblptr := @dstinfo^.quant_tbl_ptrs[tblno];
  103. if (qtblptr^ = NIL) then
  104. qtblptr^ := jpeg_alloc_quant_table(j_common_ptr(dstinfo));
  105. MEMCOPY(@(qtblptr^)^.quantval,
  106. @srcinfo^.quant_tbl_ptrs[tblno]^.quantval,
  107. SIZEOF((qtblptr^)^.quantval));
  108. (qtblptr^)^.sent_table := FALSE;
  109. end;
  110. end;
  111. { Copy the source's per-component info.
  112. Note we assume jpeg_set_defaults has allocated the dest comp_info array. }
  113. dstinfo^.num_components := srcinfo^.num_components;
  114. if (dstinfo^.num_components < 1) or
  115. (dstinfo^.num_components > MAX_COMPONENTS) then
  116. ERREXIT2(j_common_ptr(dstinfo), JERR_COMPONENT_COUNT,
  117. dstinfo^.num_components, MAX_COMPONENTS);
  118. incomp := jpeg_component_info_ptr(srcinfo^.comp_info);
  119. outcomp := jpeg_component_info_ptr(dstinfo^.comp_info);
  120. for ci := 0 to pred(dstinfo^.num_components) do
  121. begin
  122. outcomp^.component_id := incomp^.component_id;
  123. outcomp^.h_samp_factor := incomp^.h_samp_factor;
  124. outcomp^.v_samp_factor := incomp^.v_samp_factor;
  125. outcomp^.quant_tbl_no := incomp^.quant_tbl_no;
  126. { Make sure saved quantization table for component matches the qtable
  127. slot. If not, the input file re-used this qtable slot.
  128. IJG encoder currently cannot duplicate this. }
  129. tblno := outcomp^.quant_tbl_no;
  130. if (tblno < 0) or (tblno >= NUM_QUANT_TBLS) or
  131. (srcinfo^.quant_tbl_ptrs[tblno] = NIL) then
  132. ERREXIT1(j_common_ptr(dstinfo), JERR_NO_QUANT_TABLE, tblno);
  133. slot_quant := srcinfo^.quant_tbl_ptrs[tblno];
  134. c_quant := incomp^.quant_table;
  135. if (c_quant <> NIL) then
  136. begin
  137. for coefi := 0 to pred(DCTSIZE2) do
  138. begin
  139. if (c_quant^.quantval[coefi] <> slot_quant^.quantval[coefi]) then
  140. ERREXIT1(j_common_ptr(dstinfo), JERR_MISMATCHED_QUANT_TABLE, tblno);
  141. end;
  142. end;
  143. { Note: we do not copy the source's Huffman table assignments;
  144. instead we rely on jpeg_set_colorspace to have made a suitable choice. }
  145. Inc(incomp);
  146. Inc(outcomp);
  147. end;
  148. { Also copy JFIF version and resolution information, if available.
  149. Strictly speaking this isn't "critical" info, but it's nearly
  150. always appropriate to copy it if available. In particular,
  151. if the application chooses to copy JFIF 1.02 extension markers from
  152. the source file, we need to copy the version to make sure we don't
  153. emit a file that has 1.02 extensions but a claimed version of 1.01.
  154. We will *not*, however, copy version info from mislabeled "2.01" files. }
  155. if (srcinfo^.saw_JFIF_marker) then
  156. begin
  157. if (srcinfo^.JFIF_major_version = 1) then
  158. begin
  159. dstinfo^.JFIF_major_version := srcinfo^.JFIF_major_version;
  160. dstinfo^.JFIF_minor_version := srcinfo^.JFIF_minor_version;
  161. end;
  162. dstinfo^.density_unit := srcinfo^.density_unit;
  163. dstinfo^.X_density := srcinfo^.X_density;
  164. dstinfo^.Y_density := srcinfo^.Y_density;
  165. end;
  166. end;
  167. { Master selection of compression modules for transcoding.
  168. This substitutes for jcinit.c's initialization of the full compressor. }
  169. {LOCAL}
  170. procedure transencode_master_selection (cinfo : j_compress_ptr;
  171. coef_arrays : jvirt_barray_tbl_ptr);
  172. begin
  173. { Although we don't actually use input_components for transcoding,
  174. jcmaster.c's initial_setup will complain if input_components is 0. }
  175. cinfo^.input_components := 1;
  176. { Initialize master control (includes parameter checking/processing) }
  177. jinit_c_master_control(cinfo, TRUE { transcode only });
  178. { Entropy encoding: either Huffman or arithmetic coding. }
  179. if (cinfo^.arith_code) then
  180. begin
  181. ERREXIT(j_common_ptr(cinfo), JERR_ARITH_NOTIMPL);
  182. end
  183. else
  184. begin
  185. if (cinfo^.progressive_mode) then
  186. begin
  187. {$ifdef C_PROGRESSIVE_SUPPORTED}
  188. jinit_phuff_encoder(cinfo);
  189. {$else}
  190. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  191. {$endif}
  192. end
  193. else
  194. jinit_huff_encoder(cinfo);
  195. end;
  196. { We need a special coefficient buffer controller. }
  197. transencode_coef_controller(cinfo, coef_arrays);
  198. jinit_marker_writer(cinfo);
  199. { We can now tell the memory manager to allocate virtual arrays. }
  200. cinfo^.mem^.realize_virt_arrays (j_common_ptr(cinfo));
  201. { Write the datastream header (SOI, JFIF) immediately.
  202. Frame and scan headers are postponed till later.
  203. This lets application insert special markers after the SOI. }
  204. cinfo^.marker^.write_file_header (cinfo);
  205. end;
  206. { The rest of this file is a special implementation of the coefficient
  207. buffer controller. This is similar to jccoefct.c, but it handles only
  208. output from presupplied virtual arrays. Furthermore, we generate any
  209. dummy padding blocks on-the-fly rather than expecting them to be present
  210. in the arrays. }
  211. { Private buffer controller object }
  212. type
  213. my_coef_ptr = ^my_coef_controller;
  214. my_coef_controller = record
  215. pub : jpeg_c_coef_controller; { public fields }
  216. iMCU_row_num : JDIMENSION; { iMCU row # within image }
  217. mcu_ctr : JDIMENSION; { counts MCUs processed in current row }
  218. MCU_vert_offset : int; { counts MCU rows within iMCU row }
  219. MCU_rows_per_iMCU_row : int; { number of such rows needed }
  220. { Virtual block array for each component. }
  221. whole_image : jvirt_barray_tbl_ptr;
  222. { Workspace for constructing dummy blocks at right/bottom edges. }
  223. dummy_buffer : array[0..C_MAX_BLOCKS_IN_MCU-1] of JBLOCKROW;
  224. end; {my_coef_controller;}
  225. {LOCAL}
  226. procedure start_iMCU_row (cinfo : j_compress_ptr);
  227. { Reset within-iMCU-row counters for a new row }
  228. var
  229. coef : my_coef_ptr;
  230. begin
  231. coef := my_coef_ptr (cinfo^.coef);
  232. { In an interleaved scan, an MCU row is the same as an iMCU row.
  233. In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  234. But at the bottom of the image, process only what's left. }
  235. if (cinfo^.comps_in_scan > 1) then
  236. begin
  237. coef^.MCU_rows_per_iMCU_row := 1;
  238. end
  239. else
  240. begin
  241. if (coef^.iMCU_row_num < (cinfo^.total_iMCU_rows-1)) then
  242. coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.v_samp_factor
  243. else
  244. coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.last_row_height;
  245. end;
  246. coef^.mcu_ctr := 0;
  247. coef^.MCU_vert_offset := 0;
  248. end;
  249. { Initialize for a processing pass. }
  250. {METHODDEF}
  251. procedure start_pass_coef (cinfo : j_compress_ptr;
  252. pass_mode : J_BUF_MODE); far;
  253. var
  254. coef : my_coef_ptr;
  255. begin
  256. coef := my_coef_ptr (cinfo^.coef);
  257. if (pass_mode <> JBUF_CRANK_DEST) then
  258. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  259. coef^.iMCU_row_num := 0;
  260. start_iMCU_row(cinfo);
  261. end;
  262. { Process some data.
  263. We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  264. per call, ie, v_samp_factor block rows for each component in the scan.
  265. The data is obtained from the virtual arrays and fed to the entropy coder.
  266. Returns TRUE if the iMCU row is completed, FALSE if suspended.
  267. NB: input_buf is ignored; it is likely to be a NIL pointer. }
  268. {METHODDEF}
  269. function compress_output (cinfo : j_compress_ptr;
  270. input_buf : JSAMPIMAGE) : boolean; far;
  271. var
  272. coef : my_coef_ptr;
  273. MCU_col_num : JDIMENSION; { index of current MCU within row }
  274. last_MCU_col : JDIMENSION;
  275. last_iMCU_row : JDIMENSION;
  276. blkn, ci, xindex, yindex, yoffset, blockcnt : int;
  277. start_col : JDIMENSION;
  278. buffer : array[0..MAX_COMPS_IN_SCAN-1] of JBLOCKARRAY;
  279. MCU_buffer : array[0..C_MAX_BLOCKS_IN_MCU-1] of JBLOCKROW;
  280. buffer_ptr : JBLOCKROW;
  281. compptr : jpeg_component_info_ptr;
  282. begin
  283. coef := my_coef_ptr (cinfo^.coef);
  284. last_MCU_col := cinfo^.MCUs_per_row - 1;
  285. last_iMCU_row := cinfo^.total_iMCU_rows - 1;
  286. { Align the virtual buffers for the components used in this scan. }
  287. for ci := 0 to pred(cinfo^.comps_in_scan) do
  288. begin
  289. compptr := cinfo^.cur_comp_info[ci];
  290. buffer[ci] := cinfo^.mem^.access_virt_barray
  291. (j_common_ptr(cinfo), coef^.whole_image^[compptr^.component_index],
  292. coef^.iMCU_row_num * compptr^.v_samp_factor,
  293. JDIMENSION(compptr^.v_samp_factor), FALSE);
  294. end;
  295. { Loop to process one whole iMCU row }
  296. for yoffset := coef^.MCU_vert_offset to pred(coef^.MCU_rows_per_iMCU_row) do
  297. begin
  298. for MCU_col_num := coef^.mcu_ctr to pred(cinfo^.MCUs_per_row) do
  299. begin
  300. { Construct list of pointers to DCT blocks belonging to this MCU }
  301. blkn := 0; { index of current DCT block within MCU }
  302. for ci := 0 to pred(cinfo^.comps_in_scan) do
  303. begin
  304. compptr := cinfo^.cur_comp_info[ci];
  305. start_col := MCU_col_num * compptr^.MCU_width;
  306. if (MCU_col_num < last_MCU_col) then
  307. blockcnt := compptr^.MCU_width
  308. else
  309. blockcnt := compptr^.last_col_width;
  310. for yindex := 0 to pred(compptr^.MCU_height) do
  311. begin
  312. if (coef^.iMCU_row_num < last_iMCU_row) or
  313. (yindex+yoffset < compptr^.last_row_height) then
  314. begin
  315. { Fill in pointers to real blocks in this row }
  316. buffer_ptr := JBLOCKROW(@ buffer[ci]^[yindex+yoffset]^[start_col]);
  317. for xindex := 0 to pred(blockcnt) do
  318. begin
  319. MCU_buffer[blkn] := buffer_ptr;
  320. Inc(blkn);
  321. Inc(JBLOCK_PTR(buffer_ptr));
  322. end;
  323. xindex := blockcnt;
  324. end
  325. else
  326. begin
  327. { At bottom of image, need a whole row of dummy blocks }
  328. xindex := 0;
  329. end;
  330. { Fill in any dummy blocks needed in this row.
  331. Dummy blocks are filled in the same way as in jccoefct.c:
  332. all zeroes in the AC entries, DC entries equal to previous
  333. block's DC value. The init routine has already zeroed the
  334. AC entries, so we need only set the DC entries correctly. }
  335. while (xindex < compptr^.MCU_width) do
  336. begin
  337. MCU_buffer[blkn] := coef^.dummy_buffer[blkn];
  338. MCU_buffer[blkn]^[0][0] := MCU_buffer[blkn-1]^[0][0];
  339. Inc(xindex);
  340. Inc(blkn);
  341. end;
  342. end;
  343. end;
  344. { Try to write the MCU. }
  345. if (not cinfo^.entropy^.encode_mcu (cinfo, MCU_buffer)) then
  346. begin
  347. { Suspension forced; update state counters and exit }
  348. coef^.MCU_vert_offset := yoffset;
  349. coef^.mcu_ctr := MCU_col_num;
  350. compress_output := FALSE;
  351. exit;
  352. end;
  353. end;
  354. { Completed an MCU row, but perhaps not an iMCU row }
  355. coef^.mcu_ctr := 0;
  356. end;
  357. { Completed the iMCU row, advance counters for next one }
  358. Inc(coef^.iMCU_row_num);
  359. start_iMCU_row(cinfo);
  360. compress_output := TRUE;
  361. end;
  362. { Initialize coefficient buffer controller.
  363. Each passed coefficient array must be the right size for that
  364. coefficient: width_in_blocks wide and height_in_blocks high,
  365. with unitheight at least v_samp_factor. }
  366. {LOCAL}
  367. procedure transencode_coef_controller (cinfo : j_compress_ptr;
  368. coef_arrays : jvirt_barray_tbl_ptr);
  369. var
  370. coef : my_coef_ptr;
  371. buffer : JBLOCKROW;
  372. i : int;
  373. begin
  374. coef := my_coef_ptr(
  375. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  376. SIZEOF(my_coef_controller)));
  377. cinfo^.coef := jpeg_c_coef_controller_ptr (coef);
  378. coef^.pub.start_pass := start_pass_coef;
  379. coef^.pub.compress_data := compress_output;
  380. { Save pointer to virtual arrays }
  381. coef^.whole_image := coef_arrays;
  382. { Allocate and pre-zero space for dummy DCT blocks. }
  383. buffer := JBLOCKROW(
  384. cinfo^.mem^.alloc_large (j_common_ptr(cinfo), JPOOL_IMAGE,
  385. C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)) );
  386. jzero_far({FAR} voidp(buffer), C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  387. for i := 0 to pred(C_MAX_BLOCKS_IN_MCU) do
  388. begin
  389. coef^.dummy_buffer[i] := JBLOCKROW(@ buffer^[i]);
  390. end;
  391. end;
  392. end.