jdsample.cpp 17 KB

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