Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

631 lines
22 KiB

3 years ago
  1. unit imjcsample;
  2. { This file contains downsampling routines.
  3. Downsampling input data is counted in "row groups". A row group
  4. is defined to be max_v_samp_factor pixel rows of each component,
  5. from which the downsampler produces v_samp_factor sample rows.
  6. A single row group is processed in each call to the downsampler module.
  7. The downsampler is responsible for edge-expansion of its output data
  8. to fill an integral number of DCT blocks horizontally. The source buffer
  9. may be modified if it is helpful for this purpose (the source buffer is
  10. allocated wide enough to correspond to the desired output width).
  11. The caller (the prep controller) is responsible for vertical padding.
  12. The downsampler may request "context rows" by setting need_context_rows
  13. during startup. In this case, the input arrays will contain at least
  14. one row group's worth of pixels above and below the passed-in data;
  15. the caller will create dummy rows at image top and bottom by replicating
  16. the first or last real pixel row.
  17. An excellent reference for image resampling is
  18. Digital Image Warping, George Wolberg, 1990.
  19. Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
  20. The downsampling algorithm used here is a simple average of the source
  21. pixels covered by the output pixel. The hi-falutin sampling literature
  22. refers to this as a "box filter". In general the characteristics of a box
  23. filter are not very good, but for the specific cases we normally use (1:1
  24. and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
  25. nearly so bad. If you intend to use other sampling ratios, you'd be well
  26. advised to improve this code.
  27. A simple input-smoothing capability is provided. This is mainly intended
  28. for cleaning up color-dithered GIF input files (if you find it inadequate,
  29. we suggest using an external filtering program such as pnmconvol). When
  30. enabled, each input pixel P is replaced by a weighted sum of itself and its
  31. eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF,
  32. where SF := (smoothing_factor / 1024).
  33. Currently, smoothing is only supported for 2h2v sampling factors. }
  34. { Original: jcsample.c ; Copyright (C) 1991-1996, Thomas G. Lane. }
  35. interface
  36. {$I imjconfig.inc}
  37. uses
  38. imjmorecfg,
  39. imjinclude,
  40. imjutils,
  41. imjdeferr,
  42. imjerror,
  43. imjpeglib;
  44. { Module initialization routine for downsampling.
  45. Note that we must select a routine for each component. }
  46. {GLOBAL}
  47. procedure jinit_downsampler (cinfo : j_compress_ptr);
  48. implementation
  49. { Pointer to routine to downsample a single component }
  50. type
  51. downsample1_ptr = procedure(cinfo : j_compress_ptr;
  52. compptr : jpeg_component_info_ptr;
  53. input_data : JSAMPARRAY;
  54. output_data : JSAMPARRAY);
  55. { Private subobject }
  56. type
  57. my_downsample_ptr = ^my_downsampler;
  58. my_downsampler = record
  59. pub : jpeg_downsampler; { public fields }
  60. { Downsampling method pointers, one per component }
  61. methods : array[0..MAX_COMPONENTS-1] of downsample1_ptr;
  62. end;
  63. { Initialize for a downsampling pass. }
  64. {METHODDEF}
  65. procedure start_pass_downsample (cinfo : j_compress_ptr);
  66. begin
  67. { no work for now }
  68. end;
  69. { Expand a component horizontally from width input_cols to width output_cols,
  70. by duplicating the rightmost samples. }
  71. {LOCAL}
  72. procedure expand_right_edge (image_data : JSAMPARRAY;
  73. num_rows : int;
  74. input_cols : JDIMENSION;
  75. output_cols : JDIMENSION);
  76. var
  77. {register} ptr : JSAMPLE_PTR;
  78. {register} pixval : JSAMPLE;
  79. {register} count : int;
  80. row : int;
  81. numcols : int;
  82. begin
  83. numcols := int (output_cols - input_cols);
  84. if (numcols > 0) then
  85. begin
  86. for row := 0 to pred(num_rows) do
  87. begin
  88. ptr := JSAMPLE_PTR(@(image_data^[row]^[input_cols-1]));
  89. pixval := ptr^; { don't need GETJSAMPLE() here }
  90. for count := pred(numcols) downto 0 do
  91. begin
  92. Inc(ptr);
  93. ptr^ := pixval;
  94. end;
  95. end;
  96. end;
  97. end;
  98. { Do downsampling for a whole row group (all components).
  99. In this version we simply downsample each component independently. }
  100. {METHODDEF}
  101. procedure sep_downsample (cinfo : j_compress_ptr;
  102. input_buf : JSAMPIMAGE;
  103. in_row_index : JDIMENSION;
  104. output_buf : JSAMPIMAGE;
  105. out_row_group_index : JDIMENSION);
  106. var
  107. downsample : my_downsample_ptr;
  108. ci : int;
  109. compptr : jpeg_component_info_ptr;
  110. in_ptr, out_ptr : JSAMPARRAY;
  111. begin
  112. downsample := my_downsample_ptr (cinfo^.downsample);
  113. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  114. for ci := 0 to pred(cinfo^.num_components) do
  115. begin
  116. in_ptr := JSAMPARRAY(@ input_buf^[ci]^[in_row_index]);
  117. out_ptr := JSAMPARRAY(@ output_buf^[ci]^
  118. [out_row_group_index * JDIMENSION(compptr^.v_samp_factor)]);
  119. downsample^.methods[ci] (cinfo, compptr, in_ptr, out_ptr);
  120. Inc(compptr);
  121. end;
  122. end;
  123. { Downsample pixel values of a single component.
  124. One row group is processed per call.
  125. This version handles arbitrary integral sampling ratios, without smoothing.
  126. Note that this version is not actually used for customary sampling ratios. }
  127. {METHODDEF}
  128. procedure int_downsample (cinfo : j_compress_ptr;
  129. compptr : jpeg_component_info_ptr;
  130. input_data : JSAMPARRAY;
  131. output_data : JSAMPARRAY);
  132. var
  133. inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v : int;
  134. outcol, outcol_h : JDIMENSION; { outcol_h = outcol*h_expand }
  135. output_cols : JDIMENSION;
  136. inptr,
  137. outptr : JSAMPLE_PTR;
  138. outvalue : INT32;
  139. begin
  140. output_cols := compptr^.width_in_blocks * DCTSIZE;
  141. h_expand := cinfo^.max_h_samp_factor div compptr^.h_samp_factor;
  142. v_expand := cinfo^.max_v_samp_factor div compptr^.v_samp_factor;
  143. numpix := h_expand * v_expand;
  144. numpix2 := numpix div 2;
  145. { Expand input data enough to let all the output samples be generated
  146. by the standard loop. Special-casing padded output would be more
  147. efficient. }
  148. expand_right_edge(input_data, cinfo^.max_v_samp_factor,
  149. cinfo^.image_width, output_cols * JDIMENSION(h_expand));
  150. inrow := 0;
  151. for outrow := 0 to pred(compptr^.v_samp_factor) do
  152. begin
  153. outptr := JSAMPLE_PTR(output_data^[outrow]);
  154. outcol_h := 0;
  155. for outcol := 0 to pred(output_cols) do
  156. begin
  157. outvalue := 0;
  158. for v := 0 to pred(v_expand) do
  159. begin
  160. inptr := @(input_data^[inrow+v]^[outcol_h]);
  161. for h := 0 to pred(h_expand) do
  162. begin
  163. Inc(outvalue, INT32 (GETJSAMPLE(inptr^)) );
  164. Inc(inptr);
  165. end;
  166. end;
  167. outptr^ := JSAMPLE ((outvalue + numpix2) div numpix);
  168. Inc(outptr);
  169. Inc(outcol_h, h_expand);
  170. end;
  171. Inc(inrow, v_expand);
  172. end;
  173. end;
  174. { Downsample pixel values of a single component.
  175. This version handles the special case of a full-size component,
  176. without smoothing. }
  177. {METHODDEF}
  178. procedure fullsize_downsample (cinfo : j_compress_ptr;
  179. compptr : jpeg_component_info_ptr;
  180. input_data : JSAMPARRAY;
  181. output_data : JSAMPARRAY);
  182. begin
  183. { Copy the data }
  184. jcopy_sample_rows(input_data, 0, output_data, 0,
  185. cinfo^.max_v_samp_factor, cinfo^.image_width);
  186. { Edge-expand }
  187. expand_right_edge(output_data, cinfo^.max_v_samp_factor,
  188. cinfo^.image_width, compptr^.width_in_blocks * DCTSIZE);
  189. end;
  190. { Downsample pixel values of a single component.
  191. This version handles the common case of 2:1 horizontal and 1:1 vertical,
  192. without smoothing.
  193. A note about the "bias" calculations: when rounding fractional values to
  194. integer, we do not want to always round 0.5 up to the next integer.
  195. If we did that, we'd introduce a noticeable bias towards larger values.
  196. Instead, this code is arranged so that 0.5 will be rounded up or down at
  197. alternate pixel locations (a simple ordered dither pattern). }
  198. {METHODDEF}
  199. procedure h2v1_downsample (cinfo : j_compress_ptr;
  200. compptr : jpeg_component_info_ptr;
  201. input_data : JSAMPARRAY;
  202. output_data : JSAMPARRAY);
  203. var
  204. outrow : int;
  205. outcol : JDIMENSION;
  206. output_cols : JDIMENSION;
  207. {register} inptr, outptr : JSAMPLE_PTR;
  208. {register} bias : int;
  209. begin
  210. output_cols := compptr^.width_in_blocks * DCTSIZE;
  211. { Expand input data enough to let all the output samples be generated
  212. by the standard loop. Special-casing padded output would be more
  213. efficient. }
  214. expand_right_edge(input_data, cinfo^.max_v_samp_factor,
  215. cinfo^.image_width, output_cols * 2);
  216. for outrow := 0 to pred(compptr^.v_samp_factor) do
  217. begin
  218. outptr := JSAMPLE_PTR(output_data^[outrow]);
  219. inptr := JSAMPLE_PTR(input_data^[outrow]);
  220. bias := 0; { bias := 0,1,0,1,... for successive samples }
  221. for outcol := 0 to pred(output_cols) do
  222. begin
  223. outptr^ := JSAMPLE ((GETJSAMPLE(inptr^) +
  224. GETJSAMPLE(JSAMPROW(inptr)^[1]) + bias) shr 1);
  225. Inc(outptr);
  226. bias := bias xor 1; { 0=>1, 1=>0 }
  227. Inc(inptr, 2);
  228. end;
  229. end;
  230. end;
  231. { Downsample pixel values of a single component.
  232. This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  233. without smoothing. }
  234. {METHODDEF}
  235. procedure h2v2_downsample (cinfo : j_compress_ptr;
  236. compptr : jpeg_component_info_ptr;
  237. input_data : JSAMPARRAY;
  238. output_data : JSAMPARRAY);
  239. var
  240. inrow, outrow : int;
  241. outcol : JDIMENSION;
  242. output_cols : JDIMENSION;
  243. {register} inptr0, inptr1, outptr : JSAMPLE_PTR;
  244. {register} bias : int;
  245. begin
  246. output_cols := compptr^.width_in_blocks * DCTSIZE;
  247. { Expand input data enough to let all the output samples be generated
  248. by the standard loop. Special-casing padded output would be more
  249. efficient. }
  250. expand_right_edge(input_data, cinfo^.max_v_samp_factor,
  251. cinfo^.image_width, output_cols * 2);
  252. inrow := 0;
  253. for outrow := 0 to pred(compptr^.v_samp_factor) do
  254. begin
  255. outptr := JSAMPLE_PTR(output_data^[outrow]);
  256. inptr0 := JSAMPLE_PTR(input_data^[inrow]);
  257. inptr1 := JSAMPLE_PTR(input_data^[inrow+1]);
  258. bias := 1; { bias := 1,2,1,2,... for successive samples }
  259. for outcol := 0 to pred(output_cols) do
  260. begin
  261. outptr^ := JSAMPLE ((GETJSAMPLE(inptr0^) +
  262. GETJSAMPLE(JSAMPROW(inptr0)^[1]) +
  263. GETJSAMPLE(inptr1^) +
  264. GETJSAMPLE(JSAMPROW(inptr1)^[1]) + bias) shr 2);
  265. Inc(outptr);
  266. bias := bias xor 3; { 1=>2, 2=>1 }
  267. Inc(inptr0, 2);
  268. Inc(inptr1, 2);
  269. end;
  270. Inc(inrow, 2);
  271. end;
  272. end;
  273. {$ifdef INPUT_SMOOTHING_SUPPORTED}
  274. { Downsample pixel values of a single component.
  275. This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  276. with smoothing. One row of context is required. }
  277. {METHODDEF}
  278. procedure h2v2_smooth_downsample (cinfo : j_compress_ptr;
  279. compptr : jpeg_component_info_ptr;
  280. input_data : JSAMPARRAY;
  281. output_data : JSAMPARRAY);
  282. var
  283. inrow, outrow : int;
  284. colctr : JDIMENSION;
  285. output_cols : JDIMENSION;
  286. {register} inptr0, inptr1, above_ptr, below_ptr, outptr : JSAMPLE_PTR;
  287. membersum, neighsum, memberscale, neighscale : INT32;
  288. var
  289. prev_input_data : JSAMPARRAY;
  290. prev_inptr0, prev_inptr1, prev_above_ptr, prev_below_ptr : JSAMPLE_PTR;
  291. begin
  292. output_cols := compptr^.width_in_blocks * DCTSIZE;
  293. { Expand input data enough to let all the output samples be generated
  294. by the standard loop. Special-casing padded output would be more
  295. efficient. }
  296. prev_input_data := input_data;
  297. Dec(JSAMPROW_PTR(prev_input_data));
  298. expand_right_edge(prev_input_data, cinfo^.max_v_samp_factor + 2,
  299. cinfo^.image_width, output_cols * 2);
  300. { We don't bother to form the individual "smoothed" input pixel values;
  301. we can directly compute the output which is the average of the four
  302. smoothed values. Each of the four member pixels contributes a fraction
  303. (1-8*SF) to its own smoothed image and a fraction SF to each of the three
  304. other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
  305. output. The four corner-adjacent neighbor pixels contribute a fraction
  306. SF to just one smoothed pixel, or SF/4 to the final output; while the
  307. eight edge-adjacent neighbors contribute SF to each of two smoothed
  308. pixels, or SF/2 overall. In order to use integer arithmetic, these
  309. factors are scaled by 2^16 := 65536.
  310. Also recall that SF := smoothing_factor / 1024. }
  311. memberscale := 16384 - cinfo^.smoothing_factor * 80; { scaled (1-5*SF)/4 }
  312. neighscale := cinfo^.smoothing_factor * 16; { scaled SF/4 }
  313. inrow := 0;
  314. for outrow := 0 to pred(compptr^.v_samp_factor) do
  315. begin
  316. outptr := JSAMPLE_PTR(output_data^[outrow]);
  317. inptr0 := JSAMPLE_PTR(input_data^[inrow]);
  318. inptr1 := JSAMPLE_PTR(input_data^[inrow+1]);
  319. above_ptr := JSAMPLE_PTR(input_data^[inrow-1]);
  320. below_ptr := JSAMPLE_PTR(input_data^[inrow+2]);
  321. { Special case for first column: pretend column -1 is same as column 0 }
  322. membersum := GETJSAMPLE(inptr0^) + GETJSAMPLE(JSAMPROW(inptr0)^[1]) +
  323. GETJSAMPLE(inptr1^) + GETJSAMPLE(JSAMPROW(inptr1)^[1]);
  324. neighsum := GETJSAMPLE(above_ptr^) + GETJSAMPLE(JSAMPROW(above_ptr)^[1]) +
  325. GETJSAMPLE(below_ptr^) + GETJSAMPLE(JSAMPROW(below_ptr)^[1]) +
  326. GETJSAMPLE(inptr0^) + GETJSAMPLE(JSAMPROW(inptr0)^[2]) +
  327. GETJSAMPLE(inptr1^) + GETJSAMPLE(JSAMPROW(inptr1)^[2]);
  328. Inc(neighsum, neighsum);
  329. Inc(neighsum, GETJSAMPLE(above_ptr^) +
  330. GETJSAMPLE(JSAMPROW(above_ptr)^[2]) +
  331. GETJSAMPLE(below_ptr^) +
  332. GETJSAMPLE(JSAMPROW(below_ptr)^[2]) );
  333. membersum := membersum * memberscale + neighsum * neighscale;
  334. outptr^ := JSAMPLE ((membersum + 32768) shr 16);
  335. Inc(outptr);
  336. prev_inptr0 := inptr0;
  337. prev_inptr1 := inptr1;
  338. Inc(prev_inptr0);
  339. Inc(prev_inptr1);
  340. Inc(inptr0, 2);
  341. Inc(inptr1, 2);
  342. prev_above_ptr := above_ptr;
  343. prev_below_ptr := below_ptr;
  344. Inc(above_ptr, 2);
  345. Inc(below_ptr, 2);
  346. Inc(prev_above_ptr, 1);
  347. Inc(prev_below_ptr, 1);
  348. for colctr := pred(output_cols - 2) downto 0 do
  349. begin
  350. { sum of pixels directly mapped to this output element }
  351. membersum := GETJSAMPLE(inptr0^) + GETJSAMPLE(JSAMPROW(inptr0)^[1]) +
  352. GETJSAMPLE(inptr1^) + GETJSAMPLE(JSAMPROW(inptr1)^[1]);
  353. { sum of edge-neighbor pixels }
  354. neighsum := GETJSAMPLE(above_ptr^) + GETJSAMPLE(JSAMPROW(above_ptr)^[1]) +
  355. GETJSAMPLE(below_ptr^) + GETJSAMPLE(JSAMPROW(below_ptr)^[1]) +
  356. GETJSAMPLE(prev_inptr0^) + GETJSAMPLE(JSAMPROW(inptr0)^[2]) +
  357. GETJSAMPLE(prev_inptr1^) + GETJSAMPLE(JSAMPROW(inptr1)^[2]);
  358. { The edge-neighbors count twice as much as corner-neighbors }
  359. Inc(neighsum, neighsum);
  360. { Add in the corner-neighbors }
  361. Inc(neighsum, GETJSAMPLE(prev_above_ptr^) +
  362. GETJSAMPLE(JSAMPROW(above_ptr)^[2]) +
  363. GETJSAMPLE(prev_below_ptr^) +
  364. GETJSAMPLE(JSAMPROW(below_ptr)^[2]) );
  365. { form final output scaled up by 2^16 }
  366. membersum := membersum * memberscale + neighsum * neighscale;
  367. { round, descale and output it }
  368. outptr^ := JSAMPLE ((membersum + 32768) shr 16);
  369. Inc(outptr);
  370. Inc(inptr0, 2);
  371. Inc(inptr1, 2);
  372. Inc(prev_inptr0, 2);
  373. Inc(prev_inptr1, 2);
  374. Inc(above_ptr, 2);
  375. Inc(below_ptr, 2);
  376. Inc(prev_above_ptr, 2);
  377. Inc(prev_below_ptr, 2);
  378. end;
  379. { Special case for last column }
  380. membersum := GETJSAMPLE(inptr0^) + GETJSAMPLE(JSAMPROW(inptr0)^[1]) +
  381. GETJSAMPLE(inptr1^) + GETJSAMPLE(JSAMPROW(inptr1)^[1]);
  382. neighsum := GETJSAMPLE(above_ptr^) + GETJSAMPLE(JSAMPROW(above_ptr)^[1]) +
  383. GETJSAMPLE(below_ptr^) + GETJSAMPLE(JSAMPROW(below_ptr)^[1]) +
  384. GETJSAMPLE(prev_inptr0^) + GETJSAMPLE(JSAMPROW(inptr0)^[1]) +
  385. GETJSAMPLE(prev_inptr1^) + GETJSAMPLE(JSAMPROW(inptr1)^[1]);
  386. Inc(neighsum, neighsum);
  387. Inc(neighsum, GETJSAMPLE(prev_above_ptr^) +
  388. GETJSAMPLE(JSAMPROW(above_ptr)^[1]) +
  389. GETJSAMPLE(prev_below_ptr^) +
  390. GETJSAMPLE(JSAMPROW(below_ptr)^[1]) );
  391. membersum := membersum * memberscale + neighsum * neighscale;
  392. outptr^ := JSAMPLE ((membersum + 32768) shr 16);
  393. Inc(inrow, 2);
  394. end;
  395. end;
  396. { Downsample pixel values of a single component.
  397. This version handles the special case of a full-size component,
  398. with smoothing. One row of context is required. }
  399. {METHODDEF}
  400. procedure fullsize_smooth_downsample (cinfo : j_compress_ptr;
  401. compptr : jpeg_component_info_ptr;
  402. input_data : JSAMPARRAY;
  403. output_data : JSAMPARRAY);
  404. var
  405. outrow : int;
  406. colctr : JDIMENSION;
  407. output_cols : JDIMENSION;
  408. {register} inptr, above_ptr, below_ptr, outptr : JSAMPLE_PTR;
  409. membersum, neighsum, memberscale, neighscale : INT32;
  410. colsum, lastcolsum, nextcolsum : int;
  411. var
  412. prev_input_data : JSAMPARRAY;
  413. begin
  414. output_cols := compptr^.width_in_blocks * DCTSIZE;
  415. { Expand input data enough to let all the output samples be generated
  416. by the standard loop. Special-casing padded output would be more
  417. efficient. }
  418. prev_input_data := input_data;
  419. Dec(JSAMPROW_PTR(prev_input_data));
  420. expand_right_edge(prev_input_data, cinfo^.max_v_samp_factor + 2,
  421. cinfo^.image_width, output_cols);
  422. { Each of the eight neighbor pixels contributes a fraction SF to the
  423. smoothed pixel, while the main pixel contributes (1-8*SF). In order
  424. to use integer arithmetic, these factors are multiplied by 2^16 := 65536.
  425. Also recall that SF := smoothing_factor / 1024. }
  426. memberscale := long(65536) - cinfo^.smoothing_factor * long(512); { scaled 1-8*SF }
  427. neighscale := cinfo^.smoothing_factor * 64; { scaled SF }
  428. for outrow := 0 to pred(compptr^.v_samp_factor) do
  429. begin
  430. outptr := JSAMPLE_PTR(output_data^[outrow]);
  431. inptr := JSAMPLE_PTR(input_data^[outrow]);
  432. above_ptr := JSAMPLE_PTR(input_data^[outrow-1]);
  433. below_ptr := JSAMPLE_PTR(input_data^[outrow+1]);
  434. { Special case for first column }
  435. colsum := GETJSAMPLE(above_ptr^) + GETJSAMPLE(below_ptr^) +
  436. GETJSAMPLE(inptr^);
  437. Inc(above_ptr);
  438. Inc(below_ptr);
  439. membersum := GETJSAMPLE(inptr^);
  440. Inc(inptr);
  441. nextcolsum := GETJSAMPLE(above_ptr^) + GETJSAMPLE(below_ptr^) +
  442. GETJSAMPLE(inptr^);
  443. neighsum := colsum + (colsum - membersum) + nextcolsum;
  444. membersum := membersum * memberscale + neighsum * neighscale;
  445. outptr^ := JSAMPLE ((membersum + 32768) shr 16);
  446. Inc(outptr);
  447. lastcolsum := colsum; colsum := nextcolsum;
  448. for colctr := pred(output_cols - 2) downto 0 do
  449. begin
  450. membersum := GETJSAMPLE(inptr^);
  451. Inc(inptr);
  452. Inc(above_ptr);
  453. Inc(below_ptr);
  454. nextcolsum := GETJSAMPLE(above_ptr^) + GETJSAMPLE(below_ptr^) +
  455. GETJSAMPLE(inptr^);
  456. neighsum := lastcolsum + (colsum - membersum) + nextcolsum;
  457. membersum := membersum * memberscale + neighsum * neighscale;
  458. outptr^ := JSAMPLE ((membersum + 32768) shr 16);
  459. Inc(outptr);
  460. lastcolsum := colsum; colsum := nextcolsum;
  461. end;
  462. { Special case for last column }
  463. membersum := GETJSAMPLE(inptr^);
  464. neighsum := lastcolsum + (colsum - membersum) + colsum;
  465. membersum := membersum * memberscale + neighsum * neighscale;
  466. outptr^ := JSAMPLE ((membersum + 32768) shr 16);
  467. end;
  468. end;
  469. {$endif} { INPUT_SMOOTHING_SUPPORTED }
  470. { Module initialization routine for downsampling.
  471. Note that we must select a routine for each component. }
  472. {GLOBAL}
  473. procedure jinit_downsampler (cinfo : j_compress_ptr);
  474. var
  475. downsample : my_downsample_ptr;
  476. ci : int;
  477. compptr : jpeg_component_info_ptr;
  478. smoothok : boolean;
  479. begin
  480. smoothok := TRUE;
  481. downsample := my_downsample_ptr(
  482. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  483. SIZEOF(my_downsampler)) );
  484. cinfo^.downsample := jpeg_downsampler_ptr (downsample);
  485. downsample^.pub.start_pass := start_pass_downsample;
  486. downsample^.pub.downsample := sep_downsample;
  487. downsample^.pub.need_context_rows := FALSE;
  488. if (cinfo^.CCIR601_sampling) then
  489. ERREXIT(j_common_ptr(cinfo), JERR_CCIR601_NOTIMPL);
  490. { Verify we can handle the sampling factors, and set up method pointers }
  491. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  492. for ci := 0 to pred(cinfo^.num_components) do
  493. begin
  494. if (compptr^.h_samp_factor = cinfo^.max_h_samp_factor) and
  495. (compptr^.v_samp_factor = cinfo^.max_v_samp_factor) then
  496. begin
  497. {$ifdef INPUT_SMOOTHING_SUPPORTED}
  498. if (cinfo^.smoothing_factor <> 0) then
  499. begin
  500. downsample^.methods[ci] := fullsize_smooth_downsample;
  501. downsample^.pub.need_context_rows := TRUE;
  502. end
  503. else
  504. {$endif}
  505. downsample^.methods[ci] := fullsize_downsample;
  506. end
  507. else
  508. if (compptr^.h_samp_factor * 2 = cinfo^.max_h_samp_factor) and
  509. (compptr^.v_samp_factor = cinfo^.max_v_samp_factor) then
  510. begin
  511. smoothok := FALSE;
  512. downsample^.methods[ci] := h2v1_downsample;
  513. end
  514. else
  515. if (compptr^.h_samp_factor * 2 = cinfo^.max_h_samp_factor) and
  516. (compptr^.v_samp_factor * 2 = cinfo^.max_v_samp_factor) then
  517. begin
  518. {$ifdef INPUT_SMOOTHING_SUPPORTED}
  519. if (cinfo^.smoothing_factor <> 0) then
  520. begin
  521. downsample^.methods[ci] := h2v2_smooth_downsample;
  522. downsample^.pub.need_context_rows := TRUE;
  523. end
  524. else
  525. {$endif}
  526. downsample^.methods[ci] := h2v2_downsample;
  527. end
  528. else
  529. if ((cinfo^.max_h_samp_factor mod compptr^.h_samp_factor) = 0) and
  530. ((cinfo^.max_v_samp_factor mod compptr^.v_samp_factor) = 0) then
  531. begin
  532. smoothok := FALSE;
  533. downsample^.methods[ci] := int_downsample;
  534. end
  535. else
  536. ERREXIT(j_common_ptr(cinfo), JERR_FRACT_SAMPLE_NOTIMPL);
  537. Inc(compptr);
  538. end;
  539. {$ifdef INPUT_SMOOTHING_SUPPORTED}
  540. if (cinfo^.smoothing_factor <> 0) and (not smoothok) then
  541. TRACEMS(j_common_ptr(cinfo), 0, JTRC_SMOOTH_NOTIMPL);
  542. {$endif}
  543. end;
  544. end.