Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jdmerge.c |
| 3 | * |
DRC | a73e870 | 2012-12-31 02:52:30 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1994-1996, Thomas G. Lane. |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 6 | * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB |
DRC | a6ef282 | 2013-09-28 03:23:49 +0000 | [diff] [blame] | 7 | * libjpeg-turbo Modifications: |
DRC | 72a3cc0 | 2014-08-30 20:37:50 +0000 | [diff] [blame^] | 8 | * Copyright (C) 2009, 2011, 2014 D. R. Commander. |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 9 | * Copyright (C) 2013, Linaro Limited. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 10 | * For conditions of distribution and use, see the accompanying README file. |
| 11 | * |
| 12 | * This file contains code for merged upsampling/color conversion. |
| 13 | * |
| 14 | * This file combines functions from jdsample.c and jdcolor.c; |
| 15 | * read those files first to understand what's going on. |
| 16 | * |
| 17 | * When the chroma components are to be upsampled by simple replication |
| 18 | * (ie, box filtering), we can save some work in color conversion by |
| 19 | * calculating all the output pixels corresponding to a pair of chroma |
| 20 | * samples at one time. In the conversion equations |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 21 | * R = Y + K1 * Cr |
| 22 | * G = Y + K2 * Cb + K3 * Cr |
| 23 | * B = Y + K4 * Cb |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 24 | * only the Y term varies among the group of pixels corresponding to a pair |
| 25 | * of chroma samples, so the rest of the terms can be calculated just once. |
| 26 | * At typical sampling ratios, this eliminates half or three-quarters of the |
| 27 | * multiplications needed for color conversion. |
| 28 | * |
| 29 | * This file currently provides implementations for the following cases: |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 30 | * YCbCr => RGB color conversion only. |
| 31 | * Sampling ratios of 2h1v or 2h2v. |
| 32 | * No scaling needed at upsample time. |
| 33 | * Corner-aligned (non-CCIR601) sampling alignment. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 34 | * Other special cases could be added, but in most applications these are |
| 35 | * the only common cases. (For uncommon cases we fall back on the more |
| 36 | * general code in jdsample.c and jdcolor.c.) |
| 37 | */ |
| 38 | |
| 39 | #define JPEG_INTERNALS |
| 40 | #include "jinclude.h" |
| 41 | #include "jpeglib.h" |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 42 | #include "jsimd.h" |
DRC | ff6961f | 2014-04-20 09:17:11 +0000 | [diff] [blame] | 43 | #include "jconfigint.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 44 | |
| 45 | #ifdef UPSAMPLE_MERGING_SUPPORTED |
| 46 | |
| 47 | |
| 48 | /* Private subobject */ |
| 49 | |
| 50 | typedef struct { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 51 | struct jpeg_upsampler pub; /* public fields */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 52 | |
| 53 | /* Pointer to routine to do actual upsampling/conversion of one row group */ |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 54 | void (*upmethod) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
| 55 | JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 56 | |
| 57 | /* Private state for YCC->RGB conversion */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 58 | int * Cr_r_tab; /* => table for Cr to R conversion */ |
| 59 | int * Cb_b_tab; /* => table for Cb to B conversion */ |
| 60 | INT32 * Cr_g_tab; /* => table for Cr to G conversion */ |
| 61 | INT32 * Cb_g_tab; /* => table for Cb to G conversion */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 62 | |
| 63 | /* For 2:1 vertical sampling, we produce two output rows at a time. |
| 64 | * We need a "spare" row buffer to hold the second output row if the |
| 65 | * application provides just a one-row buffer; we also use the spare |
| 66 | * to discard the dummy last row if the image height is odd. |
| 67 | */ |
| 68 | JSAMPROW spare_row; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 69 | boolean spare_full; /* T if spare buffer is occupied */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 70 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 71 | JDIMENSION out_row_width; /* samples per output row */ |
| 72 | JDIMENSION rows_to_go; /* counts rows remaining in image */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 73 | } my_upsampler; |
| 74 | |
| 75 | typedef my_upsampler * my_upsample_ptr; |
| 76 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 77 | #define SCALEBITS 16 /* speediest right-shift on some machines */ |
| 78 | #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) |
| 79 | #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 80 | |
| 81 | |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 82 | /* Include inline routines for colorspace extensions */ |
| 83 | |
| 84 | #include "jdmrgext.c" |
| 85 | #undef RGB_RED |
| 86 | #undef RGB_GREEN |
| 87 | #undef RGB_BLUE |
| 88 | #undef RGB_PIXELSIZE |
| 89 | |
| 90 | #define RGB_RED EXT_RGB_RED |
| 91 | #define RGB_GREEN EXT_RGB_GREEN |
| 92 | #define RGB_BLUE EXT_RGB_BLUE |
| 93 | #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE |
| 94 | #define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal |
| 95 | #define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal |
| 96 | #include "jdmrgext.c" |
| 97 | #undef RGB_RED |
| 98 | #undef RGB_GREEN |
| 99 | #undef RGB_BLUE |
| 100 | #undef RGB_PIXELSIZE |
| 101 | #undef h2v1_merged_upsample_internal |
| 102 | #undef h2v2_merged_upsample_internal |
| 103 | |
| 104 | #define RGB_RED EXT_RGBX_RED |
| 105 | #define RGB_GREEN EXT_RGBX_GREEN |
| 106 | #define RGB_BLUE EXT_RGBX_BLUE |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 107 | #define RGB_ALPHA 3 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 108 | #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE |
| 109 | #define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal |
| 110 | #define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal |
| 111 | #include "jdmrgext.c" |
| 112 | #undef RGB_RED |
| 113 | #undef RGB_GREEN |
| 114 | #undef RGB_BLUE |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 115 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 116 | #undef RGB_PIXELSIZE |
| 117 | #undef h2v1_merged_upsample_internal |
| 118 | #undef h2v2_merged_upsample_internal |
| 119 | |
| 120 | #define RGB_RED EXT_BGR_RED |
| 121 | #define RGB_GREEN EXT_BGR_GREEN |
| 122 | #define RGB_BLUE EXT_BGR_BLUE |
| 123 | #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE |
| 124 | #define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal |
| 125 | #define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal |
| 126 | #include "jdmrgext.c" |
| 127 | #undef RGB_RED |
| 128 | #undef RGB_GREEN |
| 129 | #undef RGB_BLUE |
| 130 | #undef RGB_PIXELSIZE |
| 131 | #undef h2v1_merged_upsample_internal |
| 132 | #undef h2v2_merged_upsample_internal |
| 133 | |
| 134 | #define RGB_RED EXT_BGRX_RED |
| 135 | #define RGB_GREEN EXT_BGRX_GREEN |
| 136 | #define RGB_BLUE EXT_BGRX_BLUE |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 137 | #define RGB_ALPHA 3 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 138 | #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE |
| 139 | #define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal |
| 140 | #define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal |
| 141 | #include "jdmrgext.c" |
| 142 | #undef RGB_RED |
| 143 | #undef RGB_GREEN |
| 144 | #undef RGB_BLUE |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 145 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 146 | #undef RGB_PIXELSIZE |
| 147 | #undef h2v1_merged_upsample_internal |
| 148 | #undef h2v2_merged_upsample_internal |
| 149 | |
| 150 | #define RGB_RED EXT_XBGR_RED |
| 151 | #define RGB_GREEN EXT_XBGR_GREEN |
| 152 | #define RGB_BLUE EXT_XBGR_BLUE |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 153 | #define RGB_ALPHA 0 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 154 | #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE |
| 155 | #define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal |
| 156 | #define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal |
| 157 | #include "jdmrgext.c" |
| 158 | #undef RGB_RED |
| 159 | #undef RGB_GREEN |
| 160 | #undef RGB_BLUE |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 161 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 162 | #undef RGB_PIXELSIZE |
| 163 | #undef h2v1_merged_upsample_internal |
| 164 | #undef h2v2_merged_upsample_internal |
| 165 | |
| 166 | #define RGB_RED EXT_XRGB_RED |
| 167 | #define RGB_GREEN EXT_XRGB_GREEN |
| 168 | #define RGB_BLUE EXT_XRGB_BLUE |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 169 | #define RGB_ALPHA 0 |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 170 | #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE |
| 171 | #define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal |
| 172 | #define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal |
| 173 | #include "jdmrgext.c" |
| 174 | #undef RGB_RED |
| 175 | #undef RGB_GREEN |
| 176 | #undef RGB_BLUE |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 177 | #undef RGB_ALPHA |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 178 | #undef RGB_PIXELSIZE |
| 179 | #undef h2v1_merged_upsample_internal |
| 180 | #undef h2v2_merged_upsample_internal |
| 181 | |
| 182 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 183 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 184 | * Initialize tables for YCC->RGB colorspace conversion. |
| 185 | * This is taken directly from jdcolor.c; see that file for more info. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 186 | */ |
| 187 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 188 | LOCAL(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 189 | build_ycc_rgb_table (j_decompress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 190 | { |
| 191 | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 192 | int i; |
| 193 | INT32 x; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 194 | SHIFT_TEMPS |
| 195 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 196 | upsample->Cr_r_tab = (int *) |
| 197 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 198 | (MAXJSAMPLE+1) * sizeof(int)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 199 | upsample->Cb_b_tab = (int *) |
| 200 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 201 | (MAXJSAMPLE+1) * sizeof(int)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 202 | upsample->Cr_g_tab = (INT32 *) |
| 203 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 204 | (MAXJSAMPLE+1) * sizeof(INT32)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 205 | upsample->Cb_g_tab = (INT32 *) |
| 206 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 207 | (MAXJSAMPLE+1) * sizeof(INT32)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 208 | |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 209 | for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 210 | /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 211 | /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 212 | /* Cr=>R value is nearest int to 1.40200 * x */ |
| 213 | upsample->Cr_r_tab[i] = (int) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 214 | RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 215 | /* Cb=>B value is nearest int to 1.77200 * x */ |
| 216 | upsample->Cb_b_tab[i] = (int) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 217 | RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 218 | /* Cr=>G value is scaled-up -0.71414 * x */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 219 | upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 220 | /* Cb=>G value is scaled-up -0.34414 * x */ |
| 221 | /* We also add in ONE_HALF so that need not do it in inner loop */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 222 | upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /* |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 228 | * Initialize for an upsampling pass. |
| 229 | */ |
| 230 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 231 | METHODDEF(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 232 | start_pass_merged_upsample (j_decompress_ptr cinfo) |
| 233 | { |
| 234 | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 235 | |
| 236 | /* Mark the spare buffer empty */ |
| 237 | upsample->spare_full = FALSE; |
| 238 | /* Initialize total-height counter for detecting bottom of image */ |
| 239 | upsample->rows_to_go = cinfo->output_height; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 244 | * Control routine to do upsampling (and color conversion). |
| 245 | * |
| 246 | * The control routine just handles the row buffering considerations. |
| 247 | */ |
| 248 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 249 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 250 | merged_2v_upsample (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 251 | JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, |
| 252 | JDIMENSION in_row_groups_avail, |
| 253 | JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
| 254 | JDIMENSION out_rows_avail) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 255 | /* 2:1 vertical sampling case: may need a spare row. */ |
| 256 | { |
| 257 | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 258 | JSAMPROW work_ptrs[2]; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 259 | JDIMENSION num_rows; /* number of rows returned to caller */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 260 | |
| 261 | if (upsample->spare_full) { |
| 262 | /* If we have a spare row saved from a previous cycle, just return it. */ |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 263 | JDIMENSION size = upsample->out_row_width; |
| 264 | if (cinfo->out_color_space == JCS_RGB565) |
| 265 | size = cinfo->output_width * 2; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 266 | jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 267 | 1, size); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 268 | num_rows = 1; |
| 269 | upsample->spare_full = FALSE; |
| 270 | } else { |
| 271 | /* Figure number of rows to return to caller. */ |
| 272 | num_rows = 2; |
| 273 | /* Not more than the distance to the end of the image. */ |
| 274 | if (num_rows > upsample->rows_to_go) |
| 275 | num_rows = upsample->rows_to_go; |
| 276 | /* And not more than what the client can accept: */ |
| 277 | out_rows_avail -= *out_row_ctr; |
| 278 | if (num_rows > out_rows_avail) |
| 279 | num_rows = out_rows_avail; |
| 280 | /* Create output pointer array for upsampler. */ |
| 281 | work_ptrs[0] = output_buf[*out_row_ctr]; |
| 282 | if (num_rows > 1) { |
| 283 | work_ptrs[1] = output_buf[*out_row_ctr + 1]; |
| 284 | } else { |
| 285 | work_ptrs[1] = upsample->spare_row; |
| 286 | upsample->spare_full = TRUE; |
| 287 | } |
| 288 | /* Now do the upsampling. */ |
| 289 | (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs); |
| 290 | } |
| 291 | |
| 292 | /* Adjust counts */ |
| 293 | *out_row_ctr += num_rows; |
| 294 | upsample->rows_to_go -= num_rows; |
| 295 | /* When the buffer is emptied, declare this input row group consumed */ |
| 296 | if (! upsample->spare_full) |
| 297 | (*in_row_group_ctr)++; |
| 298 | } |
| 299 | |
| 300 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 301 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 302 | merged_1v_upsample (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 303 | JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, |
| 304 | JDIMENSION in_row_groups_avail, |
| 305 | JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
| 306 | JDIMENSION out_rows_avail) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 307 | /* 1:1 vertical sampling case: much easier, never need a spare row. */ |
| 308 | { |
| 309 | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 310 | |
| 311 | /* Just do the upsampling. */ |
| 312 | (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 313 | output_buf + *out_row_ctr); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 314 | /* Adjust counts */ |
| 315 | (*out_row_ctr)++; |
| 316 | (*in_row_group_ctr)++; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /* |
| 321 | * These are the routines invoked by the control routines to do |
| 322 | * the actual upsampling/conversion. One row group is processed per call. |
| 323 | * |
| 324 | * Note: since we may be writing directly into application-supplied buffers, |
| 325 | * we have to be honest about the output width; we can't assume the buffer |
| 326 | * has been rounded up to an even width. |
| 327 | */ |
| 328 | |
| 329 | |
| 330 | /* |
| 331 | * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical. |
| 332 | */ |
| 333 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 334 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 335 | h2v1_merged_upsample (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 336 | JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
| 337 | JSAMPARRAY output_buf) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 338 | { |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 339 | switch (cinfo->out_color_space) { |
| 340 | case JCS_EXT_RGB: |
| 341 | extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 342 | output_buf); |
| 343 | break; |
| 344 | case JCS_EXT_RGBX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 345 | case JCS_EXT_RGBA: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 346 | extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 347 | output_buf); |
| 348 | break; |
| 349 | case JCS_EXT_BGR: |
| 350 | extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 351 | output_buf); |
| 352 | break; |
| 353 | case JCS_EXT_BGRX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 354 | case JCS_EXT_BGRA: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 355 | extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 356 | output_buf); |
| 357 | break; |
| 358 | case JCS_EXT_XBGR: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 359 | case JCS_EXT_ABGR: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 360 | extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 361 | output_buf); |
| 362 | break; |
| 363 | case JCS_EXT_XRGB: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 364 | case JCS_EXT_ARGB: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 365 | extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 366 | output_buf); |
| 367 | break; |
| 368 | default: |
| 369 | h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 370 | output_buf); |
| 371 | break; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /* |
| 377 | * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical. |
| 378 | */ |
| 379 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 380 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 381 | h2v2_merged_upsample (j_decompress_ptr cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 382 | JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
| 383 | JSAMPARRAY output_buf) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 384 | { |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 385 | switch (cinfo->out_color_space) { |
| 386 | case JCS_EXT_RGB: |
| 387 | extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 388 | output_buf); |
| 389 | break; |
| 390 | case JCS_EXT_RGBX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 391 | case JCS_EXT_RGBA: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 392 | extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 393 | output_buf); |
| 394 | break; |
| 395 | case JCS_EXT_BGR: |
| 396 | extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 397 | output_buf); |
| 398 | break; |
| 399 | case JCS_EXT_BGRX: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 400 | case JCS_EXT_BGRA: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 401 | extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 402 | output_buf); |
| 403 | break; |
| 404 | case JCS_EXT_XBGR: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 405 | case JCS_EXT_ABGR: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 406 | extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 407 | output_buf); |
| 408 | break; |
| 409 | case JCS_EXT_XRGB: |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 410 | case JCS_EXT_ARGB: |
DRC | b4570bb | 2011-09-07 06:31:00 +0000 | [diff] [blame] | 411 | extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 412 | output_buf); |
| 413 | break; |
| 414 | default: |
| 415 | h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, |
| 416 | output_buf); |
| 417 | break; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | |
DRC | 72a3cc0 | 2014-08-30 20:37:50 +0000 | [diff] [blame^] | 422 | /* |
| 423 | * RGB565 conversion |
| 424 | */ |
| 425 | |
| 426 | #define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \ |
| 427 | (((g) << 3) & 0x7E0) | ((b) >> 3)) |
| 428 | #define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \ |
| 429 | (((g) << 11) & 0xE000) | \ |
| 430 | (((b) << 5) & 0x1F00)) |
| 431 | |
| 432 | #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l) |
| 433 | #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r) |
| 434 | |
| 435 | #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3) |
| 436 | |
| 437 | #define WRITE_TWO_PIXELS_LE(addr, pixels) { \ |
| 438 | ((INT16*)(addr))[0] = (pixels); \ |
| 439 | ((INT16*)(addr))[1] = (pixels) >> 16; \ |
| 440 | } |
| 441 | #define WRITE_TWO_PIXELS_BE(addr, pixels) { \ |
| 442 | ((INT16*)(addr))[1] = (pixels); \ |
| 443 | ((INT16*)(addr))[0] = (pixels) >> 16; \ |
| 444 | } |
| 445 | |
| 446 | #define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(INT32 *)(addr)) = pixels) |
| 447 | |
| 448 | #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF)) |
| 449 | #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1)) |
| 450 | #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF)) |
| 451 | |
| 452 | |
| 453 | /* Declarations for ordered dithering |
| 454 | * |
| 455 | * We use a 4x4 ordered dither array packed into 32 bits. This array is |
| 456 | * sufficent for dithering RGB888 to RGB565. |
| 457 | */ |
| 458 | |
| 459 | #define DITHER_MASK 0x3 |
| 460 | #define DITHER_ROTATE(x) (((x) << 24) | (((x) >> 8) & 0x00FFFFFF)) |
| 461 | static const INT32 dither_matrix[4] = { |
| 462 | 0x0008020A, |
| 463 | 0x0C040E06, |
| 464 | 0x030B0109, |
| 465 | 0x0F070D05 |
| 466 | }; |
| 467 | |
| 468 | |
| 469 | /* Include inline routines for RGB565 conversion */ |
| 470 | |
| 471 | #define PACK_SHORT_565 PACK_SHORT_565_LE |
| 472 | #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE |
| 473 | #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_LE |
| 474 | #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_le |
| 475 | #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_le |
| 476 | #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_le |
| 477 | #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_le |
| 478 | #include "jdmrg565.c" |
| 479 | #undef PACK_SHORT_565 |
| 480 | #undef PACK_TWO_PIXELS |
| 481 | #undef WRITE_TWO_PIXELS |
| 482 | #undef h2v1_merged_upsample_565_internal |
| 483 | #undef h2v1_merged_upsample_565D_internal |
| 484 | #undef h2v2_merged_upsample_565_internal |
| 485 | #undef h2v2_merged_upsample_565D_internal |
| 486 | |
| 487 | #define PACK_SHORT_565 PACK_SHORT_565_BE |
| 488 | #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE |
| 489 | #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_BE |
| 490 | #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_be |
| 491 | #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_be |
| 492 | #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_be |
| 493 | #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_be |
| 494 | #include "jdmrg565.c" |
| 495 | #undef PACK_SHORT_565 |
| 496 | #undef PACK_TWO_PIXELS |
| 497 | #undef WRITE_TWO_PIXELS |
| 498 | #undef h2v1_merged_upsample_565_internal |
| 499 | #undef h2v1_merged_upsample_565D_internal |
| 500 | #undef h2v2_merged_upsample_565_internal |
| 501 | #undef h2v2_merged_upsample_565D_internal |
| 502 | |
| 503 | |
| 504 | static INLINE boolean is_big_endian(void) |
| 505 | { |
| 506 | int test_value = 1; |
| 507 | if(*(char *)&test_value != 1) |
| 508 | return TRUE; |
| 509 | return FALSE; |
| 510 | } |
| 511 | |
| 512 | |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 513 | METHODDEF(void) |
| 514 | h2v1_merged_upsample_565 (j_decompress_ptr cinfo, |
| 515 | JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
| 516 | JSAMPARRAY output_buf) |
| 517 | { |
DRC | 72a3cc0 | 2014-08-30 20:37:50 +0000 | [diff] [blame^] | 518 | if (is_big_endian()) |
| 519 | h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr, |
| 520 | output_buf); |
| 521 | else |
| 522 | h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr, |
| 523 | output_buf); |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | |
| 527 | METHODDEF(void) |
| 528 | h2v1_merged_upsample_565D (j_decompress_ptr cinfo, |
| 529 | JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
| 530 | JSAMPARRAY output_buf) |
| 531 | { |
DRC | 72a3cc0 | 2014-08-30 20:37:50 +0000 | [diff] [blame^] | 532 | if (is_big_endian()) |
| 533 | h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr, |
| 534 | output_buf); |
| 535 | else |
| 536 | h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr, |
| 537 | output_buf); |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
| 541 | METHODDEF(void) |
| 542 | h2v2_merged_upsample_565 (j_decompress_ptr cinfo, |
| 543 | JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
| 544 | JSAMPARRAY output_buf) |
| 545 | { |
DRC | 72a3cc0 | 2014-08-30 20:37:50 +0000 | [diff] [blame^] | 546 | if (is_big_endian()) |
| 547 | h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr, |
| 548 | output_buf); |
| 549 | else |
| 550 | h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr, |
| 551 | output_buf); |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
| 555 | METHODDEF(void) |
| 556 | h2v2_merged_upsample_565D (j_decompress_ptr cinfo, |
| 557 | JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
| 558 | JSAMPARRAY output_buf) |
| 559 | { |
DRC | 72a3cc0 | 2014-08-30 20:37:50 +0000 | [diff] [blame^] | 560 | if (is_big_endian()) |
| 561 | h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr, |
| 562 | output_buf); |
| 563 | else |
| 564 | h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr, |
| 565 | output_buf); |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 569 | /* |
| 570 | * Module initialization routine for merged upsampling/color conversion. |
| 571 | * |
| 572 | * NB: this is called under the conditions determined by use_merged_upsample() |
| 573 | * in jdmaster.c. That routine MUST correspond to the actual capabilities |
| 574 | * of this module; no safety checks are made here. |
| 575 | */ |
| 576 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 577 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 578 | jinit_merged_upsampler (j_decompress_ptr cinfo) |
| 579 | { |
| 580 | my_upsample_ptr upsample; |
| 581 | |
| 582 | upsample = (my_upsample_ptr) |
| 583 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 584 | sizeof(my_upsampler)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 585 | cinfo->upsample = (struct jpeg_upsampler *) upsample; |
| 586 | upsample->pub.start_pass = start_pass_merged_upsample; |
| 587 | upsample->pub.need_context_rows = FALSE; |
| 588 | |
| 589 | upsample->out_row_width = cinfo->output_width * cinfo->out_color_components; |
| 590 | |
| 591 | if (cinfo->max_v_samp_factor == 2) { |
| 592 | upsample->pub.upsample = merged_2v_upsample; |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 593 | if (jsimd_can_h2v2_merged_upsample()) |
| 594 | upsample->upmethod = jsimd_h2v2_merged_upsample; |
| 595 | else |
| 596 | upsample->upmethod = h2v2_merged_upsample; |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 597 | if (cinfo->out_color_space == JCS_RGB565) { |
| 598 | if (cinfo->dither_mode != JDITHER_NONE) { |
| 599 | upsample->upmethod = h2v2_merged_upsample_565D; |
| 600 | } else { |
| 601 | upsample->upmethod = h2v2_merged_upsample_565; |
| 602 | } |
| 603 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 604 | /* Allocate a spare row buffer */ |
| 605 | upsample->spare_row = (JSAMPROW) |
| 606 | (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 607 | (size_t) (upsample->out_row_width * sizeof(JSAMPLE))); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 608 | } else { |
| 609 | upsample->pub.upsample = merged_1v_upsample; |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 610 | if (jsimd_can_h2v1_merged_upsample()) |
| 611 | upsample->upmethod = jsimd_h2v1_merged_upsample; |
| 612 | else |
| 613 | upsample->upmethod = h2v1_merged_upsample; |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 614 | if (cinfo->out_color_space == JCS_RGB565) { |
| 615 | if (cinfo->dither_mode != JDITHER_NONE) { |
| 616 | upsample->upmethod = h2v1_merged_upsample_565D; |
| 617 | } else { |
| 618 | upsample->upmethod = h2v1_merged_upsample_565; |
| 619 | } |
| 620 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 621 | /* No spare row needed */ |
| 622 | upsample->spare_row = NULL; |
| 623 | } |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 624 | |
| 625 | build_ycc_rgb_table(cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | #endif /* UPSAMPLE_MERGING_SUPPORTED */ |