| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#define JPEG_INTERNALS |
| 36 |
#include "jinclude.h" |
| 37 |
#include "jpeglib.h" |
| 38 |
|
| 39 |
#ifdef UPSAMPLE_MERGING_SUPPORTED |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
typedef struct { |
| 45 |
struct jpeg_upsampler pub; |
| 46 |
|
| 47 |
|
| 48 |
JMETHOD(void, upmethod, (j_decompress_ptr cinfo, |
| 49 |
JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
| 50 |
JSAMPARRAY output_buf)); |
| 51 |
|
| 52 |
|
| 53 |
int * Cr_r_tab; |
| 54 |
int * Cb_b_tab; |
| 55 |
INT32 * Cr_g_tab; |
| 56 |
INT32 * Cb_g_tab; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
JSAMPROW spare_row; |
| 64 |
boolean spare_full; |
| 65 |
|
| 66 |
JDIMENSION out_row_width; |
| 67 |
JDIMENSION rows_to_go; |
| 68 |
} my_upsampler; |
| 69 |
|
| 70 |
typedef my_upsampler * my_upsample_ptr; |
| 71 |
|
| 72 |
#define SCALEBITS 16 |
| 73 |
#define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) |
| 74 |
#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
LOCAL(void) |
| 83 |
build_ycc_rgb_table (j_decompress_ptr cinfo) |
| 84 |
{ |
| 85 |
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 86 |
int i; |
| 87 |
INT32 x; |
| 88 |
SHIFT_TEMPS |
| 89 |
|
| 90 |
upsample->Cr_r_tab = (int *) |
| 91 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 92 |
(MAXJSAMPLE+1) * SIZEOF(int)); |
| 93 |
upsample->Cb_b_tab = (int *) |
| 94 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 95 |
(MAXJSAMPLE+1) * SIZEOF(int)); |
| 96 |
upsample->Cr_g_tab = (INT32 *) |
| 97 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 98 |
(MAXJSAMPLE+1) * SIZEOF(INT32)); |
| 99 |
upsample->Cb_g_tab = (INT32 *) |
| 100 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 101 |
(MAXJSAMPLE+1) * SIZEOF(INT32)); |
| 102 |
|
| 103 |
for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
upsample->Cr_r_tab[i] = (int) |
| 108 |
RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); |
| 109 |
|
| 110 |
upsample->Cb_b_tab[i] = (int) |
| 111 |
RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); |
| 112 |
|
| 113 |
upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x; |
| 114 |
|
| 115 |
|
| 116 |
upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
METHODDEF(void) |
| 126 |
start_pass_merged_upsample (j_decompress_ptr cinfo) |
| 127 |
{ |
| 128 |
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 129 |
|
| 130 |
|
| 131 |
upsample->spare_full = FALSE; |
| 132 |
|
| 133 |
upsample->rows_to_go = cinfo->output_height; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
METHODDEF(void) |
| 144 |
merged_2v_upsample (j_decompress_ptr cinfo, |
| 145 |
JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, |
| 146 |
JDIMENSION in_row_groups_avail, |
| 147 |
JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
| 148 |
JDIMENSION out_rows_avail) |
| 149 |
|
| 150 |
{ |
| 151 |
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 152 |
JSAMPROW work_ptrs[2]; |
| 153 |
JDIMENSION num_rows; |
| 154 |
|
| 155 |
if (upsample->spare_full) { |
| 156 |
|
| 157 |
jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, |
| 158 |
1, upsample->out_row_width); |
| 159 |
num_rows = 1; |
| 160 |
upsample->spare_full = FALSE; |
| 161 |
} else { |
| 162 |
|
| 163 |
num_rows = 2; |
| 164 |
|
| 165 |
if (num_rows > upsample->rows_to_go) |
| 166 |
num_rows = upsample->rows_to_go; |
| 167 |
|
| 168 |
out_rows_avail -= *out_row_ctr; |
| 169 |
if (num_rows > out_rows_avail) |
| 170 |
num_rows = out_rows_avail; |
| 171 |
|
| 172 |
work_ptrs[0] = output_buf[*out_row_ctr]; |
| 173 |
if (num_rows > 1) { |
| 174 |
work_ptrs[1] = output_buf[*out_row_ctr + 1]; |
| 175 |
} else { |
| 176 |
work_ptrs[1] = upsample->spare_row; |
| 177 |
upsample->spare_full = TRUE; |
| 178 |
} |
| 179 |
|
| 180 |
(*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
*out_row_ctr += num_rows; |
| 185 |
upsample->rows_to_go -= num_rows; |
| 186 |
|
| 187 |
if (! upsample->spare_full) |
| 188 |
(*in_row_group_ctr)++; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
METHODDEF(void) |
| 193 |
merged_1v_upsample (j_decompress_ptr cinfo, |
| 194 |
JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, |
| 195 |
JDIMENSION in_row_groups_avail, |
| 196 |
JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
| 197 |
JDIMENSION out_rows_avail) |
| 198 |
|
| 199 |
{ |
| 200 |
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 201 |
|
| 202 |
|
| 203 |
(*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, |
| 204 |
output_buf + *out_row_ctr); |
| 205 |
|
| 206 |
(*out_row_ctr)++; |
| 207 |
(*in_row_group_ctr)++; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
METHODDEF(void) |
| 226 |
h2v1_merged_upsample (j_decompress_ptr cinfo, |
| 227 |
JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
| 228 |
JSAMPARRAY output_buf) |
| 229 |
{ |
| 230 |
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 231 |
register int y, cred, cgreen, cblue; |
| 232 |
int cb, cr; |
| 233 |
register JSAMPROW outptr; |
| 234 |
JSAMPROW inptr0, inptr1, inptr2; |
| 235 |
JDIMENSION col; |
| 236 |
|
| 237 |
register JSAMPLE * range_limit = cinfo->sample_range_limit; |
| 238 |
int * Crrtab = upsample->Cr_r_tab; |
| 239 |
int * Cbbtab = upsample->Cb_b_tab; |
| 240 |
INT32 * Crgtab = upsample->Cr_g_tab; |
| 241 |
INT32 * Cbgtab = upsample->Cb_g_tab; |
| 242 |
SHIFT_TEMPS |
| 243 |
|
| 244 |
inptr0 = input_buf[0][in_row_group_ctr]; |
| 245 |
inptr1 = input_buf[1][in_row_group_ctr]; |
| 246 |
inptr2 = input_buf[2][in_row_group_ctr]; |
| 247 |
outptr = output_buf[0]; |
| 248 |
|
| 249 |
for (col = cinfo->output_width >> 1; col > 0; col--) { |
| 250 |
|
| 251 |
cb = GETJSAMPLE(*inptr1++); |
| 252 |
cr = GETJSAMPLE(*inptr2++); |
| 253 |
cred = Crrtab[cr]; |
| 254 |
cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); |
| 255 |
cblue = Cbbtab[cb]; |
| 256 |
|
| 257 |
y = GETJSAMPLE(*inptr0++); |
| 258 |
outptr[RGB_RED] = range_limit[y + cred]; |
| 259 |
outptr[RGB_GREEN] = range_limit[y + cgreen]; |
| 260 |
outptr[RGB_BLUE] = range_limit[y + cblue]; |
| 261 |
outptr += RGB_PIXELSIZE; |
| 262 |
y = GETJSAMPLE(*inptr0++); |
| 263 |
outptr[RGB_RED] = range_limit[y + cred]; |
| 264 |
outptr[RGB_GREEN] = range_limit[y + cgreen]; |
| 265 |
outptr[RGB_BLUE] = range_limit[y + cblue]; |
| 266 |
outptr += RGB_PIXELSIZE; |
| 267 |
} |
| 268 |
|
| 269 |
if (cinfo->output_width & 1) { |
| 270 |
cb = GETJSAMPLE(*inptr1); |
| 271 |
cr = GETJSAMPLE(*inptr2); |
| 272 |
cred = Crrtab[cr]; |
| 273 |
cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); |
| 274 |
cblue = Cbbtab[cb]; |
| 275 |
y = GETJSAMPLE(*inptr0); |
| 276 |
outptr[RGB_RED] = range_limit[y + cred]; |
| 277 |
outptr[RGB_GREEN] = range_limit[y + cgreen]; |
| 278 |
outptr[RGB_BLUE] = range_limit[y + cblue]; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
METHODDEF(void) |
| 288 |
h2v2_merged_upsample (j_decompress_ptr cinfo, |
| 289 |
JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
| 290 |
JSAMPARRAY output_buf) |
| 291 |
{ |
| 292 |
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 293 |
register int y, cred, cgreen, cblue; |
| 294 |
int cb, cr; |
| 295 |
register JSAMPROW outptr0, outptr1; |
| 296 |
JSAMPROW inptr00, inptr01, inptr1, inptr2; |
| 297 |
JDIMENSION col; |
| 298 |
|
| 299 |
register JSAMPLE * range_limit = cinfo->sample_range_limit; |
| 300 |
int * Crrtab = upsample->Cr_r_tab; |
| 301 |
int * Cbbtab = upsample->Cb_b_tab; |
| 302 |
INT32 * Crgtab = upsample->Cr_g_tab; |
| 303 |
INT32 * Cbgtab = upsample->Cb_g_tab; |
| 304 |
SHIFT_TEMPS |
| 305 |
|
| 306 |
inptr00 = input_buf[0][in_row_group_ctr*2]; |
| 307 |
inptr01 = input_buf[0][in_row_group_ctr*2 + 1]; |
| 308 |
inptr1 = input_buf[1][in_row_group_ctr]; |
| 309 |
inptr2 = input_buf[2][in_row_group_ctr]; |
| 310 |
outptr0 = output_buf[0]; |
| 311 |
outptr1 = output_buf[1]; |
| 312 |
|
| 313 |
for (col = cinfo->output_width >> 1; col > 0; col--) { |
| 314 |
|
| 315 |
cb = GETJSAMPLE(*inptr1++); |
| 316 |
cr = GETJSAMPLE(*inptr2++); |
| 317 |
cred = Crrtab[cr]; |
| 318 |
cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); |
| 319 |
cblue = Cbbtab[cb]; |
| 320 |
|
| 321 |
y = GETJSAMPLE(*inptr00++); |
| 322 |
outptr0[RGB_RED] = range_limit[y + cred]; |
| 323 |
outptr0[RGB_GREEN] = range_limit[y + cgreen]; |
| 324 |
outptr0[RGB_BLUE] = range_limit[y + cblue]; |
| 325 |
outptr0 += RGB_PIXELSIZE; |
| 326 |
y = GETJSAMPLE(*inptr00++); |
| 327 |
outptr0[RGB_RED] = range_limit[y + cred]; |
| 328 |
outptr0[RGB_GREEN] = range_limit[y + cgreen]; |
| 329 |
outptr0[RGB_BLUE] = range_limit[y + cblue]; |
| 330 |
outptr0 += RGB_PIXELSIZE; |
| 331 |
y = GETJSAMPLE(*inptr01++); |
| 332 |
outptr1[RGB_RED] = range_limit[y + cred]; |
| 333 |
outptr1[RGB_GREEN] = range_limit[y + cgreen]; |
| 334 |
outptr1[RGB_BLUE] = range_limit[y + cblue]; |
| 335 |
outptr1 += RGB_PIXELSIZE; |
| 336 |
y = GETJSAMPLE(*inptr01++); |
| 337 |
outptr1[RGB_RED] = range_limit[y + cred]; |
| 338 |
outptr1[RGB_GREEN] = range_limit[y + cgreen]; |
| 339 |
outptr1[RGB_BLUE] = range_limit[y + cblue]; |
| 340 |
outptr1 += RGB_PIXELSIZE; |
| 341 |
} |
| 342 |
|
| 343 |
if (cinfo->output_width & 1) { |
| 344 |
cb = GETJSAMPLE(*inptr1); |
| 345 |
cr = GETJSAMPLE(*inptr2); |
| 346 |
cred = Crrtab[cr]; |
| 347 |
cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); |
| 348 |
cblue = Cbbtab[cb]; |
| 349 |
y = GETJSAMPLE(*inptr00); |
| 350 |
outptr0[RGB_RED] = range_limit[y + cred]; |
| 351 |
outptr0[RGB_GREEN] = range_limit[y + cgreen]; |
| 352 |
outptr0[RGB_BLUE] = range_limit[y + cblue]; |
| 353 |
y = GETJSAMPLE(*inptr01); |
| 354 |
outptr1[RGB_RED] = range_limit[y + cred]; |
| 355 |
outptr1[RGB_GREEN] = range_limit[y + cgreen]; |
| 356 |
outptr1[RGB_BLUE] = range_limit[y + cblue]; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
GLOBAL(void) |
| 370 |
jinit_merged_upsampler (j_decompress_ptr cinfo) |
| 371 |
{ |
| 372 |
my_upsample_ptr upsample; |
| 373 |
|
| 374 |
upsample = (my_upsample_ptr) |
| 375 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 376 |
SIZEOF(my_upsampler)); |
| 377 |
cinfo->upsample = (struct jpeg_upsampler *) upsample; |
| 378 |
upsample->pub.start_pass = start_pass_merged_upsample; |
| 379 |
upsample->pub.need_context_rows = FALSE; |
| 380 |
|
| 381 |
upsample->out_row_width = cinfo->output_width * cinfo->out_color_components; |
| 382 |
|
| 383 |
if (cinfo->max_v_samp_factor == 2) { |
| 384 |
upsample->pub.upsample = merged_2v_upsample; |
| 385 |
upsample->upmethod = h2v2_merged_upsample; |
| 386 |
|
| 387 |
upsample->spare_row = (JSAMPROW) |
| 388 |
(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 389 |
(size_t) (upsample->out_row_width * SIZEOF(JSAMPLE))); |
| 390 |
} else { |
| 391 |
upsample->pub.upsample = merged_1v_upsample; |
| 392 |
upsample->upmethod = h2v1_merged_upsample; |
| 393 |
|
| 394 |
upsample->spare_row = NULL; |
| 395 |
} |
| 396 |
|
| 397 |
build_ycc_rgb_table(cinfo); |
| 398 |
} |
| 399 |
|
| 400 |
#endif |