jdmerge.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. {$IFNDEF FPC_DOTTEDUNITS}
  2. Unit JdMerge;
  3. {$ENDIF FPC_DOTTEDUNITS}
  4. { This file contains code for merged upsampling/color conversion.
  5. This file combines functions from jdsample.c and jdcolor.c;
  6. read those files first to understand what's going on.
  7. When the chroma components are to be upsampled by simple replication
  8. (ie, box filtering), we can save some work in color conversion by
  9. calculating all the output pixels corresponding to a pair of chroma
  10. samples at one time. In the conversion equations
  11. R := Y + K1 * Cr
  12. G := Y + K2 * Cb + K3 * Cr
  13. B := Y + K4 * Cb
  14. only the Y term varies among the group of pixels corresponding to a pair
  15. of chroma samples, so the rest of the terms can be calculated just once.
  16. At typical sampling ratios, this eliminates half or three-quarters of the
  17. multiplications needed for color conversion.
  18. This file currently provides implementations for the following cases:
  19. YCbCr => RGB color conversion only.
  20. Sampling ratios of 2h1v or 2h2v.
  21. No scaling needed at upsample time.
  22. Corner-aligned (non-CCIR601) sampling alignment.
  23. Other special cases could be added, but in most applications these are
  24. the only common cases. (For uncommon cases we fall back on the more
  25. general code in jdsample.c and jdcolor.c.) }
  26. { Original: jdmerge.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  27. interface
  28. {$I jconfig.inc}
  29. {$IFDEF FPC_DOTTEDUNITS}
  30. uses
  31. System.Jpeg.Jmorecfg,
  32. System.Jpeg.Jinclude,
  33. System.Jpeg.Jpeglib,
  34. System.Jpeg.Jutils;
  35. {$ELSE FPC_DOTTEDUNITS}
  36. uses
  37. jmorecfg,
  38. jinclude,
  39. jpeglib,
  40. jutils;
  41. {$ENDIF FPC_DOTTEDUNITS}
  42. { Module initialization routine for merged upsampling/color conversion.
  43. NB: this is called under the conditions determined by use_merged_upsample()
  44. in jdmaster.c. That routine MUST correspond to the actual capabilities
  45. of this module; no safety checks are made here. }
  46. {GLOBAL}
  47. procedure jinit_merged_upsampler (cinfo : j_decompress_ptr);
  48. implementation
  49. { Private subobject }
  50. type { the same definition as in JdColor }
  51. int_Color_Table = array[0..MAXJSAMPLE+1-1] of int;
  52. int_CConvertPtr = ^int_Color_Table;
  53. INT32_Color_Table = array[0..MAXJSAMPLE+1-1] of INT32;
  54. INT32_CConvertPtr = ^INT32_Color_Table;
  55. type
  56. my_upsample_ptr = ^my_upsampler;
  57. my_upsampler = record
  58. pub : jpeg_upsampler; { public fields }
  59. { Pointer to routine to do actual upsampling/conversion of one row group }
  60. upmethod : procedure (cinfo : j_decompress_ptr;
  61. input_buf : JSAMPIMAGE;
  62. in_row_group_ctr : JDIMENSION;
  63. output_buf : JSAMPARRAY);
  64. { Private state for YCC->RGB conversion }
  65. Cr_r_tab : int_CConvertPtr; { => table for Cr to R conversion }
  66. Cb_b_tab : int_CConvertPtr; { => table for Cb to B conversion }
  67. Cr_g_tab : INT32_CConvertPtr; { => table for Cr to G conversion }
  68. Cb_g_tab : INT32_CConvertPtr; { => table for Cb to G conversion }
  69. { For 2:1 vertical sampling, we produce two output rows at a time.
  70. We need a "spare" row buffer to hold the second output row if the
  71. application provides just a one-row buffer; we also use the spare
  72. to discard the dummy last row if the image height is odd. }
  73. spare_row : JSAMPROW;
  74. spare_full : boolean; { TRUE if spare buffer is occupied }
  75. out_row_width : JDIMENSION; { samples per output row }
  76. rows_to_go : JDIMENSION; { counts rows remaining in image }
  77. end; {my_upsampler;}
  78. const
  79. SCALEBITS = 16; { speediest right-shift on some machines }
  80. ONE_HALF = (INT32(1) shl (SCALEBITS-1));
  81. { Initialize tables for YCC->RGB colorspace conversion.
  82. This is taken directly from jdcolor.c; see that file for more info. }
  83. {LOCAL}
  84. procedure build_ycc_rgb_table (cinfo : j_decompress_ptr);
  85. const
  86. FIX_1_40200 = INT32( Round(1.40200 * (INT32(1) shl SCALEBITS)) );
  87. FIX_1_77200 = INT32( Round(1.77200 * (INT32(1) shl SCALEBITS)) );
  88. FIX_0_71414 = INT32( Round(0.71414 * (INT32(1) shl SCALEBITS)) );
  89. FIX_0_34414 = INT32( Round(0.34414 * (INT32(1) shl SCALEBITS)) );
  90. var
  91. upsample : my_upsample_ptr;
  92. i : int;
  93. x : INT32;
  94. var
  95. shift_temp : INT32;
  96. begin
  97. upsample := my_upsample_ptr (cinfo^.upsample);
  98. upsample^.Cr_r_tab := int_CConvertPtr (
  99. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  100. (MAXJSAMPLE+1) * SIZEOF(int)) );
  101. upsample^.Cb_b_tab := int_CConvertPtr (
  102. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  103. (MAXJSAMPLE+1) * SIZEOF(int)) );
  104. upsample^.Cr_g_tab := INT32_CConvertPtr (
  105. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  106. (MAXJSAMPLE+1) * SIZEOF(INT32)) );
  107. upsample^.Cb_g_tab := INT32_CConvertPtr (
  108. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  109. (MAXJSAMPLE+1) * SIZEOF(INT32)) );
  110. x := -CENTERJSAMPLE;
  111. for i := 0 to pred(MAXJSAMPLE) do
  112. begin
  113. { i is the actual input pixel value, in the range 0..MAXJSAMPLE }
  114. { The Cb or Cr value we are thinking of is x := i - CENTERJSAMPLE }
  115. { Cr=>R value is nearest int to 1.40200 * x }
  116. {upsample^.Cr_r_tab^[i] := int(
  117. RIGHT_SHIFT(FIX_1_40200 * x + ONE_HALF, SCALEBITS) );}
  118. shift_temp := FIX_1_40200 * x + ONE_HALF;
  119. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  120. upsample^.Cr_r_tab^[i] := int((shift_temp shr SCALEBITS)
  121. or ( (not INT32(0)) shl (32-SCALEBITS)))
  122. else
  123. upsample^.Cr_r_tab^[i] := int(shift_temp shr SCALEBITS);
  124. { Cb=>B value is nearest int to 1.77200 * x }
  125. {upsample^.Cb_b_tab^[i] := int(
  126. RIGHT_SHIFT(FIX_1_77200 * x + ONE_HALF, SCALEBITS) );}
  127. shift_temp := FIX_1_77200 * x + ONE_HALF;
  128. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  129. upsample^.Cb_b_tab^[i] := int((shift_temp shr SCALEBITS)
  130. or ( (not INT32(0)) shl (32-SCALEBITS)))
  131. else
  132. upsample^.Cb_b_tab^[i] := int(shift_temp shr SCALEBITS);
  133. { Cr=>G value is scaled-up -0.71414 * x }
  134. upsample^.Cr_g_tab^[i] := (- FIX_0_71414) * x;
  135. { Cb=>G value is scaled-up -0.34414 * x }
  136. { We also add in ONE_HALF so that need not do it in inner loop }
  137. upsample^.Cb_g_tab^[i] := (- FIX_0_34414) * x + ONE_HALF;
  138. Inc(x);
  139. end;
  140. end;
  141. { Initialize for an upsampling pass. }
  142. {METHODDEF}
  143. procedure start_pass_merged_upsample (cinfo : j_decompress_ptr); far;
  144. var
  145. upsample : my_upsample_ptr;
  146. begin
  147. upsample := my_upsample_ptr (cinfo^.upsample);
  148. { Mark the spare buffer empty }
  149. upsample^.spare_full := FALSE;
  150. { Initialize total-height counter for detecting bottom of image }
  151. upsample^.rows_to_go := cinfo^.output_height;
  152. end;
  153. { Control routine to do upsampling (and color conversion).
  154. The control routine just handles the row buffering considerations. }
  155. {METHODDEF}
  156. procedure merged_2v_upsample (cinfo : j_decompress_ptr;
  157. input_buf : JSAMPIMAGE;
  158. var in_row_group_ctr : JDIMENSION;
  159. in_row_groups_avail : JDIMENSION;
  160. output_buf : JSAMPARRAY;
  161. var out_row_ctr : JDIMENSION;
  162. out_rows_avail : JDIMENSION); far;
  163. { 2:1 vertical sampling case: may need a spare row. }
  164. var
  165. upsample : my_upsample_ptr;
  166. work_ptrs : array[0..2-1] of JSAMPROW;
  167. num_rows : JDIMENSION; { number of rows returned to caller }
  168. begin
  169. upsample := my_upsample_ptr (cinfo^.upsample);
  170. if (upsample^.spare_full) then
  171. begin
  172. { If we have a spare row saved from a previous cycle, just return it. }
  173. jcopy_sample_rows(JSAMPARRAY(@upsample^.spare_row),
  174. 0,
  175. JSAMPARRAY(@ output_buf^[out_row_ctr]),
  176. 0, 1, upsample^.out_row_width);
  177. num_rows := 1;
  178. upsample^.spare_full := FALSE;
  179. end
  180. else
  181. begin
  182. { Figure number of rows to return to caller. }
  183. num_rows := 2;
  184. { Not more than the distance to the end of the image. }
  185. if (num_rows > upsample^.rows_to_go) then
  186. num_rows := upsample^.rows_to_go;
  187. { And not more than what the client can accept: }
  188. Dec(out_rows_avail, {var} out_row_ctr);
  189. if (num_rows > out_rows_avail) then
  190. num_rows := out_rows_avail;
  191. { Create output pointer array for upsampler. }
  192. work_ptrs[0] := output_buf^[out_row_ctr];
  193. if (num_rows > 1) then
  194. begin
  195. work_ptrs[1] := output_buf^[out_row_ctr + 1];
  196. end
  197. else
  198. begin
  199. work_ptrs[1] := upsample^.spare_row;
  200. upsample^.spare_full := TRUE;
  201. end;
  202. { Now do the upsampling. }
  203. upsample^.upmethod (cinfo, input_buf, {var}in_row_group_ctr,
  204. JSAMPARRAY(@work_ptrs));
  205. end;
  206. { Adjust counts }
  207. Inc(out_row_ctr, num_rows);
  208. Dec(upsample^.rows_to_go, num_rows);
  209. { When the buffer is emptied, declare this input row group consumed }
  210. if (not upsample^.spare_full) then
  211. Inc(in_row_group_ctr);
  212. end;
  213. {METHODDEF}
  214. procedure merged_1v_upsample (cinfo : j_decompress_ptr;
  215. input_buf : JSAMPIMAGE;
  216. var in_row_group_ctr : JDIMENSION;
  217. in_row_groups_avail : JDIMENSION;
  218. output_buf : JSAMPARRAY;
  219. var out_row_ctr : JDIMENSION;
  220. out_rows_avail : JDIMENSION); far;
  221. { 1:1 vertical sampling case: much easier, never need a spare row. }
  222. var
  223. upsample : my_upsample_ptr;
  224. begin
  225. upsample := my_upsample_ptr (cinfo^.upsample);
  226. { Just do the upsampling. }
  227. upsample^.upmethod (cinfo, input_buf, in_row_group_ctr,
  228. JSAMPARRAY(@ output_buf^[out_row_ctr]));
  229. { Adjust counts }
  230. Inc(out_row_ctr);
  231. Inc(in_row_group_ctr);
  232. end;
  233. { These are the routines invoked by the control routines to do
  234. the actual upsampling/conversion. One row group is processed per call.
  235. Note: since we may be writing directly into application-supplied buffers,
  236. we have to be honest about the output width; we can't assume the buffer
  237. has been rounded up to an even width. }
  238. { Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical. }
  239. {METHODDEF}
  240. procedure h2v1_merged_upsample (cinfo : j_decompress_ptr;
  241. input_buf : JSAMPIMAGE;
  242. in_row_group_ctr : JDIMENSION;
  243. output_buf : JSAMPARRAY); far;
  244. var
  245. upsample : my_upsample_ptr;
  246. {register} y, cred, cgreen, cblue : int;
  247. cb, cr : int;
  248. {register} outptr : JSAMPROW;
  249. inptr0, inptr1, inptr2 : JSAMPLE_PTR;
  250. col : JDIMENSION;
  251. { copy these pointers into registers if possible }
  252. {register} range_limit : range_limit_table_ptr;
  253. Crrtab : int_CConvertPtr;
  254. Cbbtab : int_CConvertPtr;
  255. Crgtab : INT32_CConvertPtr;
  256. Cbgtab : INT32_CConvertPtr;
  257. var
  258. shift_temp : INT32;
  259. begin
  260. upsample := my_upsample_ptr (cinfo^.upsample);
  261. range_limit := cinfo^.sample_range_limit;
  262. Crrtab := upsample^.Cr_r_tab;
  263. Cbbtab := upsample^.Cb_b_tab;
  264. Crgtab := upsample^.Cr_g_tab;
  265. Cbgtab := upsample^.Cb_g_tab;
  266. inptr0 := JSAMPLE_PTR(input_buf^[0]^[in_row_group_ctr]);
  267. inptr1 := JSAMPLE_PTR(input_buf^[1]^[in_row_group_ctr]);
  268. inptr2 := JSAMPLE_PTR(input_buf^[2]^[in_row_group_ctr]);
  269. outptr := output_buf^[0];
  270. { Loop for each pair of output pixels }
  271. for col := pred(cinfo^.output_width shr 1) downto 0 do
  272. begin
  273. { Do the chroma part of the calculation }
  274. cb := GETJSAMPLE(inptr1^);
  275. Inc(inptr1);
  276. cr := GETJSAMPLE(inptr2^);
  277. Inc(inptr2);
  278. cred := Crrtab^[cr];
  279. {cgreen := int( RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS) );}
  280. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  281. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  282. cgreen := int((shift_temp shr SCALEBITS)
  283. or ( (not INT32(0)) shl (32-SCALEBITS)))
  284. else
  285. cgreen := int(shift_temp shr SCALEBITS);
  286. cblue := Cbbtab^[cb];
  287. { Fetch 2 Y values and emit 2 pixels }
  288. y := GETJSAMPLE(inptr0^);
  289. Inc(inptr0);
  290. outptr^[RGB_RED] := range_limit^[y + cred];
  291. outptr^[RGB_GREEN] := range_limit^[y + cgreen];
  292. outptr^[RGB_BLUE] := range_limit^[y + cblue];
  293. Inc(JSAMPLE_PTR(outptr), RGB_PIXELSIZE);
  294. y := GETJSAMPLE(inptr0^);
  295. Inc(inptr0);
  296. outptr^[RGB_RED] := range_limit^[y + cred];
  297. outptr^[RGB_GREEN] := range_limit^[y + cgreen];
  298. outptr^[RGB_BLUE] := range_limit^[y + cblue];
  299. Inc(JSAMPLE_PTR(outptr), RGB_PIXELSIZE);
  300. end;
  301. { If image width is odd, do the last output column separately }
  302. if Odd(cinfo^.output_width) then
  303. begin
  304. cb := GETJSAMPLE(inptr1^);
  305. cr := GETJSAMPLE(inptr2^);
  306. cred := Crrtab^[cr];
  307. {cgreen := int ( RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS) );}
  308. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  309. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  310. cgreen := int((shift_temp shr SCALEBITS)
  311. or ( (not INT32(0)) shl (32-SCALEBITS)))
  312. else
  313. cgreen := int(shift_temp shr SCALEBITS);
  314. cblue := Cbbtab^[cb];
  315. y := GETJSAMPLE(inptr0^);
  316. outptr^[RGB_RED] := range_limit^[y + cred];
  317. outptr^[RGB_GREEN] := range_limit^[y + cgreen];
  318. outptr^[RGB_BLUE] := range_limit^[y + cblue];
  319. end;
  320. end;
  321. { Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical. }
  322. {METHODDEF}
  323. procedure h2v2_merged_upsample (cinfo : j_decompress_ptr;
  324. input_buf : JSAMPIMAGE;
  325. in_row_group_ctr : JDIMENSION;
  326. output_buf : JSAMPARRAY); far;
  327. var
  328. upsample : my_upsample_ptr;
  329. {register} y, cred, cgreen, cblue : int;
  330. cb, cr : int;
  331. {register} outptr0, outptr1 : JSAMPROW;
  332. inptr00, inptr01, inptr1, inptr2 : JSAMPLE_PTR;
  333. col : JDIMENSION;
  334. { copy these pointers into registers if possible }
  335. {register} range_limit : range_limit_table_ptr;
  336. Crrtab : int_CConvertPtr;
  337. Cbbtab : int_CConvertPtr;
  338. Crgtab : INT32_CConvertPtr;
  339. Cbgtab : INT32_CConvertPtr;
  340. var
  341. shift_temp : INT32;
  342. begin
  343. upsample := my_upsample_ptr (cinfo^.upsample);
  344. range_limit := cinfo^.sample_range_limit;
  345. Crrtab := upsample^.Cr_r_tab;
  346. Cbbtab := upsample^.Cb_b_tab;
  347. Crgtab := upsample^.Cr_g_tab;
  348. Cbgtab := upsample^.Cb_g_tab;
  349. inptr00 := JSAMPLE_PTR(input_buf^[0]^[in_row_group_ctr*2]);
  350. inptr01 := JSAMPLE_PTR(input_buf^[0]^[in_row_group_ctr*2 + 1]);
  351. inptr1 := JSAMPLE_PTR(input_buf^[1]^[in_row_group_ctr]);
  352. inptr2 := JSAMPLE_PTR(input_buf^[2]^[in_row_group_ctr]);
  353. outptr0 := output_buf^[0];
  354. outptr1 := output_buf^[1];
  355. { Loop for each group of output pixels }
  356. for col := pred(cinfo^.output_width shr 1) downto 0 do
  357. begin
  358. { Do the chroma part of the calculation }
  359. cb := GETJSAMPLE(inptr1^);
  360. Inc(inptr1);
  361. cr := GETJSAMPLE(inptr2^);
  362. Inc(inptr2);
  363. cred := Crrtab^[cr];
  364. {cgreen := int( RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS) );}
  365. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  366. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  367. cgreen := int((shift_temp shr SCALEBITS)
  368. or ( (not INT32(0)) shl (32-SCALEBITS)))
  369. else
  370. cgreen := int(shift_temp shr SCALEBITS);
  371. cblue := Cbbtab^[cb];
  372. { Fetch 4 Y values and emit 4 pixels }
  373. y := GETJSAMPLE(inptr00^);
  374. Inc(inptr00);
  375. outptr0^[RGB_RED] := range_limit^[y + cred];
  376. outptr0^[RGB_GREEN] := range_limit^[y + cgreen];
  377. outptr0^[RGB_BLUE] := range_limit^[y + cblue];
  378. Inc(JSAMPLE_PTR(outptr0), RGB_PIXELSIZE);
  379. y := GETJSAMPLE(inptr00^);
  380. Inc(inptr00);
  381. outptr0^[RGB_RED] := range_limit^[y + cred];
  382. outptr0^[RGB_GREEN] := range_limit^[y + cgreen];
  383. outptr0^[RGB_BLUE] := range_limit^[y + cblue];
  384. Inc(JSAMPLE_PTR(outptr0), RGB_PIXELSIZE);
  385. y := GETJSAMPLE(inptr01^);
  386. Inc(inptr01);
  387. outptr1^[RGB_RED] := range_limit^[y + cred];
  388. outptr1^[RGB_GREEN] := range_limit^[y + cgreen];
  389. outptr1^[RGB_BLUE] := range_limit^[y + cblue];
  390. Inc(JSAMPLE_PTR(outptr1), RGB_PIXELSIZE);
  391. y := GETJSAMPLE(inptr01^);
  392. Inc(inptr01);
  393. outptr1^[RGB_RED] := range_limit^[y + cred];
  394. outptr1^[RGB_GREEN] := range_limit^[y + cgreen];
  395. outptr1^[RGB_BLUE] := range_limit^[y + cblue];
  396. Inc(JSAMPLE_PTR(outptr1), RGB_PIXELSIZE);
  397. end;
  398. { If image width is odd, do the last output column separately }
  399. if Odd(cinfo^.output_width) then
  400. begin
  401. cb := GETJSAMPLE(inptr1^);
  402. cr := GETJSAMPLE(inptr2^);
  403. cred := Crrtab^[cr];
  404. {cgreen := int (RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS));}
  405. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  406. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  407. cgreen := int((shift_temp shr SCALEBITS)
  408. or ( (not INT32(0)) shl (32-SCALEBITS)))
  409. else
  410. cgreen := int(shift_temp shr SCALEBITS);
  411. cblue := Cbbtab^[cb];
  412. y := GETJSAMPLE(inptr00^);
  413. outptr0^[RGB_RED] := range_limit^[y + cred];
  414. outptr0^[RGB_GREEN] := range_limit^[y + cgreen];
  415. outptr0^[RGB_BLUE] := range_limit^[y + cblue];
  416. y := GETJSAMPLE(inptr01^);
  417. outptr1^[RGB_RED] := range_limit^[y + cred];
  418. outptr1^[RGB_GREEN] := range_limit^[y + cgreen];
  419. outptr1^[RGB_BLUE] := range_limit^[y + cblue];
  420. end;
  421. end;
  422. { Module initialization routine for merged upsampling/color conversion.
  423. NB: this is called under the conditions determined by use_merged_upsample()
  424. in jdmaster.c. That routine MUST correspond to the actual capabilities
  425. of this module; no safety checks are made here. }
  426. {GLOBAL}
  427. procedure jinit_merged_upsampler (cinfo : j_decompress_ptr);
  428. var
  429. upsample : my_upsample_ptr;
  430. begin
  431. upsample := my_upsample_ptr (
  432. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  433. SIZEOF(my_upsampler)) );
  434. cinfo^.upsample := jpeg_upsampler_ptr (upsample);
  435. upsample^.pub.start_pass := start_pass_merged_upsample;
  436. upsample^.pub.need_context_rows := FALSE;
  437. upsample^.out_row_width := cinfo^.output_width * cinfo^.out_color_components;
  438. if (cinfo^.max_v_samp_factor = 2) then
  439. begin
  440. upsample^.pub.upsample := merged_2v_upsample;
  441. upsample^.upmethod := h2v2_merged_upsample;
  442. { Allocate a spare row buffer }
  443. upsample^.spare_row := JSAMPROW(
  444. cinfo^.mem^.alloc_large ( j_common_ptr(cinfo), JPOOL_IMAGE,
  445. size_t (upsample^.out_row_width * SIZEOF(JSAMPLE))) );
  446. end
  447. else
  448. begin
  449. upsample^.pub.upsample := merged_1v_upsample;
  450. upsample^.upmethod := h2v1_merged_upsample;
  451. { No spare row needed }
  452. upsample^.spare_row := NIL;
  453. end;
  454. build_ycc_rgb_table(cinfo);
  455. end;
  456. end.