jdsample.cpp 18 KB

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