Audio plugin host https://kx.studio/carla
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.

jdsample.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * jdsample.c
  3. *
  4. * Copyright (C) 1991-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. *
  8. * This file contains upsampling routines.
  9. *
  10. * Upsampling input data is counted in "row groups". A row group
  11. * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
  12. * sample rows of each component. Upsampling will normally produce
  13. * max_v_samp_factor pixel rows from each row group (but this could vary
  14. * if the upsampler is applying a scale factor of its own).
  15. *
  16. * An excellent reference for image resampling is
  17. * Digital Image Warping, George Wolberg, 1990.
  18. * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
  19. */
  20. #define JPEG_INTERNALS
  21. #include "jinclude.h"
  22. #include "jpeglib.h"
  23. /* Pointer to routine to upsample a single component */
  24. typedef JMETHOD(void, upsample1_ptr,
  25. (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  26. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
  27. /* Private subobject */
  28. typedef struct {
  29. struct jpeg_upsampler pub; /* public fields */
  30. /* Color conversion buffer. When using separate upsampling and color
  31. * conversion steps, this buffer holds one upsampled row group until it
  32. * has been color converted and output.
  33. * Note: we do not allocate any storage for component(s) which are full-size,
  34. * ie do not need rescaling. The corresponding entry of color_buf[] is
  35. * simply set to point to the input data array, thereby avoiding copying.
  36. */
  37. JSAMPARRAY color_buf[MAX_COMPONENTS];
  38. /* Per-component upsampling method pointers */
  39. upsample1_ptr methods[MAX_COMPONENTS];
  40. int next_row_out; /* counts rows emitted from color_buf */
  41. JDIMENSION rows_to_go; /* counts rows remaining in image */
  42. /* Height of an input row group for each component. */
  43. int rowgroup_height[MAX_COMPONENTS];
  44. /* These arrays save pixel expansion factors so that int_expand need not
  45. * recompute them each time. They are unused for other upsampling methods.
  46. */
  47. UINT8 h_expand[MAX_COMPONENTS];
  48. UINT8 v_expand[MAX_COMPONENTS];
  49. } my_upsampler2;
  50. typedef my_upsampler2 * my_upsample_ptr2;
  51. /*
  52. * Initialize for an upsampling pass.
  53. */
  54. METHODDEF(void)
  55. start_pass_upsample (j_decompress_ptr cinfo)
  56. {
  57. my_upsample_ptr2 upsample = (my_upsample_ptr2) cinfo->upsample;
  58. /* Mark the conversion buffer empty */
  59. upsample->next_row_out = cinfo->max_v_samp_factor;
  60. /* Initialize total-height counter for detecting bottom of image */
  61. upsample->rows_to_go = cinfo->output_height;
  62. }
  63. /*
  64. * Control routine to do upsampling (and color conversion).
  65. *
  66. * In this version we upsample each component independently.
  67. * We upsample one row group into the conversion buffer, then apply
  68. * color conversion a row at a time.
  69. */
  70. METHODDEF(void)
  71. sep_upsample (j_decompress_ptr cinfo,
  72. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  73. JDIMENSION,
  74. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  75. JDIMENSION out_rows_avail)
  76. {
  77. my_upsample_ptr2 upsample = (my_upsample_ptr2) cinfo->upsample;
  78. int ci;
  79. jpeg_component_info * compptr;
  80. JDIMENSION num_rows;
  81. /* Fill the conversion buffer, if it's empty */
  82. if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
  83. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  84. ci++, compptr++) {
  85. /* Invoke per-component upsample method. Notice we pass a POINTER
  86. * to color_buf[ci], so that fullsize_upsample can change it.
  87. */
  88. (*upsample->methods[ci]) (cinfo, compptr,
  89. input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
  90. upsample->color_buf + ci);
  91. }
  92. upsample->next_row_out = 0;
  93. }
  94. /* Color-convert and emit rows */
  95. /* How many we have in the buffer: */
  96. num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
  97. /* Not more than the distance to the end of the image. Need this test
  98. * in case the image height is not a multiple of max_v_samp_factor:
  99. */
  100. if (num_rows > upsample->rows_to_go)
  101. num_rows = upsample->rows_to_go;
  102. /* And not more than what the client can accept: */
  103. out_rows_avail -= *out_row_ctr;
  104. if (num_rows > out_rows_avail)
  105. num_rows = out_rows_avail;
  106. (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
  107. (JDIMENSION) upsample->next_row_out,
  108. output_buf + *out_row_ctr,
  109. (int) num_rows);
  110. /* Adjust counts */
  111. *out_row_ctr += num_rows;
  112. upsample->rows_to_go -= num_rows;
  113. upsample->next_row_out += num_rows;
  114. /* When the buffer is emptied, declare this input row group consumed */
  115. if (upsample->next_row_out >= cinfo->max_v_samp_factor)
  116. (*in_row_group_ctr)++;
  117. }
  118. /*
  119. * These are the routines invoked by sep_upsample to upsample pixel values
  120. * of a single component. One row group is processed per call.
  121. */
  122. /*
  123. * For full-size components, we just make color_buf[ci] point at the
  124. * input buffer, and thus avoid copying any data. Note that this is
  125. * safe only because sep_upsample doesn't declare the input row group
  126. * "consumed" until we are done color converting and emitting it.
  127. */
  128. METHODDEF(void)
  129. fullsize_upsample (j_decompress_ptr, jpeg_component_info *,
  130. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  131. {
  132. *output_data_ptr = input_data;
  133. }
  134. /*
  135. * This is a no-op version used for "uninteresting" components.
  136. * These components will not be referenced by color conversion.
  137. */
  138. METHODDEF(void)
  139. noop_upsample (j_decompress_ptr, jpeg_component_info *,
  140. JSAMPARRAY, JSAMPARRAY * output_data_ptr)
  141. {
  142. *output_data_ptr = NULL; /* safety check */
  143. }
  144. /*
  145. * This version handles any integral sampling ratios.
  146. * This is not used for typical JPEG files, so it need not be fast.
  147. * Nor, for that matter, is it particularly accurate: the algorithm is
  148. * simple replication of the input pixel onto the corresponding output
  149. * pixels. The hi-falutin sampling literature refers to this as a
  150. * "box filter". A box filter tends to introduce visible artifacts,
  151. * so if you are actually going to use 3:1 or 4:1 sampling ratios
  152. * you would be well advised to improve this code.
  153. */
  154. METHODDEF(void)
  155. int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  156. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  157. {
  158. my_upsample_ptr2 upsample = (my_upsample_ptr2) cinfo->upsample;
  159. JSAMPARRAY output_data = *output_data_ptr;
  160. register JSAMPROW inptr, outptr;
  161. register JSAMPLE invalue;
  162. register int h;
  163. JSAMPROW outend;
  164. int h_expand, v_expand;
  165. int inrow, outrow;
  166. h_expand = upsample->h_expand[compptr->component_index];
  167. v_expand = upsample->v_expand[compptr->component_index];
  168. inrow = outrow = 0;
  169. while (outrow < cinfo->max_v_samp_factor) {
  170. /* Generate one output row with proper horizontal expansion */
  171. inptr = input_data[inrow];
  172. outptr = output_data[outrow];
  173. outend = outptr + cinfo->output_width;
  174. while (outptr < outend) {
  175. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  176. for (h = h_expand; h > 0; h--) {
  177. *outptr++ = invalue;
  178. }
  179. }
  180. /* Generate any additional output rows by duplicating the first one */
  181. if (v_expand > 1) {
  182. jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  183. v_expand-1, cinfo->output_width);
  184. }
  185. inrow++;
  186. outrow += v_expand;
  187. }
  188. }
  189. /*
  190. * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
  191. * It's still a box filter.
  192. */
  193. METHODDEF(void)
  194. h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info *,
  195. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  196. {
  197. JSAMPARRAY output_data = *output_data_ptr;
  198. register JSAMPROW inptr, outptr;
  199. register JSAMPLE invalue;
  200. JSAMPROW outend;
  201. int inrow;
  202. for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
  203. inptr = input_data[inrow];
  204. outptr = output_data[inrow];
  205. outend = outptr + cinfo->output_width;
  206. while (outptr < outend) {
  207. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  208. *outptr++ = invalue;
  209. *outptr++ = invalue;
  210. }
  211. }
  212. }
  213. /*
  214. * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
  215. * It's still a box filter.
  216. */
  217. METHODDEF(void)
  218. h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info *,
  219. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  220. {
  221. JSAMPARRAY output_data = *output_data_ptr;
  222. register JSAMPROW inptr, outptr;
  223. register JSAMPLE invalue;
  224. JSAMPROW outend;
  225. int inrow, outrow;
  226. inrow = outrow = 0;
  227. while (outrow < cinfo->max_v_samp_factor) {
  228. inptr = input_data[inrow];
  229. outptr = output_data[outrow];
  230. outend = outptr + cinfo->output_width;
  231. while (outptr < outend) {
  232. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  233. *outptr++ = invalue;
  234. *outptr++ = invalue;
  235. }
  236. jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  237. 1, cinfo->output_width);
  238. inrow++;
  239. outrow += 2;
  240. }
  241. }
  242. /*
  243. * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
  244. *
  245. * The upsampling algorithm is linear interpolation between pixel centers,
  246. * also known as a "triangle filter". This is a good compromise between
  247. * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
  248. * of the way between input pixel centers.
  249. *
  250. * A note about the "bias" calculations: when rounding fractional values to
  251. * integer, we do not want to always round 0.5 up to the next integer.
  252. * If we did that, we'd introduce a noticeable bias towards larger values.
  253. * Instead, this code is arranged so that 0.5 will be rounded up or down at
  254. * alternate pixel locations (a simple ordered dither pattern).
  255. */
  256. METHODDEF(void)
  257. h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  258. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  259. {
  260. JSAMPARRAY output_data = *output_data_ptr;
  261. register JSAMPROW inptr, outptr;
  262. register int invalue;
  263. register JDIMENSION colctr;
  264. int inrow;
  265. for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
  266. inptr = input_data[inrow];
  267. outptr = output_data[inrow];
  268. /* Special case for first column */
  269. invalue = GETJSAMPLE(*inptr++);
  270. *outptr++ = (JSAMPLE) invalue;
  271. *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
  272. for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
  273. /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
  274. invalue = GETJSAMPLE(*inptr++) * 3;
  275. *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
  276. *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
  277. }
  278. /* Special case for last column */
  279. invalue = GETJSAMPLE(*inptr);
  280. *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
  281. *outptr++ = (JSAMPLE) invalue;
  282. }
  283. }
  284. /*
  285. * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
  286. * Again a triangle filter; see comments for h2v1 case, above.
  287. *
  288. * It is OK for us to reference the adjacent input rows because we demanded
  289. * context from the main buffer controller (see initialization code).
  290. */
  291. METHODDEF(void)
  292. h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  293. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  294. {
  295. JSAMPARRAY output_data = *output_data_ptr;
  296. register JSAMPROW inptr0, inptr1, outptr;
  297. #if BITS_IN_JSAMPLE == 8
  298. register int thiscolsum, lastcolsum, nextcolsum;
  299. #else
  300. register INT32 thiscolsum, lastcolsum, nextcolsum;
  301. #endif
  302. register JDIMENSION colctr;
  303. int inrow, outrow, v;
  304. inrow = outrow = 0;
  305. while (outrow < cinfo->max_v_samp_factor) {
  306. for (v = 0; v < 2; v++) {
  307. /* inptr0 points to nearest input row, inptr1 points to next nearest */
  308. inptr0 = input_data[inrow];
  309. if (v == 0) /* next nearest is row above */
  310. inptr1 = input_data[inrow-1];
  311. else /* next nearest is row below */
  312. inptr1 = input_data[inrow+1];
  313. outptr = output_data[outrow++];
  314. /* Special case for first column */
  315. thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  316. nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  317. *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
  318. *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
  319. lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  320. for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
  321. /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
  322. /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
  323. nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  324. *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  325. *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
  326. lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  327. }
  328. /* Special case for last column */
  329. *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  330. *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
  331. }
  332. inrow++;
  333. }
  334. }
  335. /*
  336. * Module initialization routine for upsampling.
  337. */
  338. GLOBAL(void)
  339. jinit_upsampler (j_decompress_ptr cinfo)
  340. {
  341. my_upsample_ptr2 upsample;
  342. int ci;
  343. jpeg_component_info * compptr;
  344. boolean need_buffer, do_fancy;
  345. int h_in_group, v_in_group, h_out_group, v_out_group;
  346. upsample = (my_upsample_ptr2)
  347. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  348. SIZEOF(my_upsampler2));
  349. cinfo->upsample = (struct jpeg_upsampler *) upsample;
  350. upsample->pub.start_pass = start_pass_upsample;
  351. upsample->pub.upsample = sep_upsample;
  352. upsample->pub.need_context_rows = FALSE; /* until we find out differently */
  353. if (cinfo->CCIR601_sampling) /* this isn't supported */
  354. ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  355. /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
  356. * so don't ask for it.
  357. */
  358. do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
  359. /* Verify we can handle the sampling factors, select per-component methods,
  360. * and create storage as needed.
  361. */
  362. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  363. ci++, compptr++) {
  364. /* Compute size of an "input group" after IDCT scaling. This many samples
  365. * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
  366. */
  367. h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
  368. cinfo->min_DCT_scaled_size;
  369. v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  370. cinfo->min_DCT_scaled_size;
  371. h_out_group = cinfo->max_h_samp_factor;
  372. v_out_group = cinfo->max_v_samp_factor;
  373. upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
  374. need_buffer = TRUE;
  375. if (! compptr->component_needed) {
  376. /* Don't bother to upsample an uninteresting component. */
  377. upsample->methods[ci] = noop_upsample;
  378. need_buffer = FALSE;
  379. } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
  380. /* Fullsize components can be processed without any work. */
  381. upsample->methods[ci] = fullsize_upsample;
  382. need_buffer = FALSE;
  383. } else if (h_in_group * 2 == h_out_group &&
  384. v_in_group == v_out_group) {
  385. /* Special cases for 2h1v upsampling */
  386. if (do_fancy && compptr->downsampled_width > 2)
  387. upsample->methods[ci] = h2v1_fancy_upsample;
  388. else
  389. upsample->methods[ci] = h2v1_upsample;
  390. } else if (h_in_group * 2 == h_out_group &&
  391. v_in_group * 2 == v_out_group) {
  392. /* Special cases for 2h2v upsampling */
  393. if (do_fancy && compptr->downsampled_width > 2) {
  394. upsample->methods[ci] = h2v2_fancy_upsample;
  395. upsample->pub.need_context_rows = TRUE;
  396. } else
  397. upsample->methods[ci] = h2v2_upsample;
  398. } else if ((h_out_group % h_in_group) == 0 &&
  399. (v_out_group % v_in_group) == 0) {
  400. /* Generic integral-factors upsampling method */
  401. upsample->methods[ci] = int_upsample;
  402. upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
  403. upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
  404. } else
  405. ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  406. if (need_buffer) {
  407. upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  408. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  409. (JDIMENSION) jround_up((long) cinfo->output_width,
  410. (long) cinfo->max_h_samp_factor),
  411. (JDIMENSION) cinfo->max_v_samp_factor);
  412. }
  413. }
  414. }