[SHELL32] SHChangeNotify: Add drive, remove drive (#6782)
[reactos.git] / dll / 3rdparty / libjpeg / jdmerge.c
1 /*
2 * jdmerge.c
3 *
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Modified 2013-2019 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains code for merged upsampling/color conversion.
10 *
11 * This file combines functions from jdsample.c and jdcolor.c;
12 * read those files first to understand what's going on.
13 *
14 * When the chroma components are to be upsampled by simple replication
15 * (ie, box filtering), we can save some work in color conversion by
16 * calculating all the output pixels corresponding to a pair of chroma
17 * samples at one time. In the conversion equations
18 * R = Y + K1 * Cr
19 * G = Y + K2 * Cb + K3 * Cr
20 * B = Y + K4 * Cb
21 * only the Y term varies among the group of pixels corresponding to a pair
22 * of chroma samples, so the rest of the terms can be calculated just once.
23 * At typical sampling ratios, this eliminates half or three-quarters of the
24 * multiplications needed for color conversion.
25 *
26 * This file currently provides implementations for the following cases:
27 * YCC => RGB color conversion only (YCbCr or BG_YCC).
28 * Sampling ratios of 2h1v or 2h2v.
29 * No scaling needed at upsample time.
30 * Corner-aligned (non-CCIR601) sampling alignment.
31 * Other special cases could be added, but in most applications these are
32 * the only common cases. (For uncommon cases we fall back on the more
33 * general code in jdsample.c and jdcolor.c.)
34 */
35
36 #define JPEG_INTERNALS
37 #include "jinclude.h"
38 #include "jpeglib.h"
39
40 #ifdef UPSAMPLE_MERGING_SUPPORTED
41
42
43 #if RANGE_BITS < 2
44 /* Deliberate syntax err */
45 Sorry, this code requires 2 or more range extension bits.
46 #endif
47
48
49 /* Private subobject */
50
51 typedef struct {
52 struct jpeg_upsampler pub; /* public fields */
53
54 /* Pointer to routine to do actual upsampling/conversion of one row group */
55 JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
56 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
57 JSAMPARRAY output_buf));
58
59 /* Private state for YCC->RGB conversion */
60 int * Cr_r_tab; /* => table for Cr to R conversion */
61 int * Cb_b_tab; /* => table for Cb to B conversion */
62 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
63 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
64
65 /* For 2:1 vertical sampling, we produce two output rows at a time.
66 * We need a "spare" row buffer to hold the second output row if the
67 * application provides just a one-row buffer; we also use the spare
68 * to discard the dummy last row if the image height is odd.
69 */
70 JSAMPROW spare_row;
71 boolean spare_full; /* T if spare buffer is occupied */
72
73 JDIMENSION out_row_width; /* samples per output row */
74 JDIMENSION rows_to_go; /* counts rows remaining in image */
75 } my_upsampler;
76
77 typedef my_upsampler * my_upsample_ptr;
78
79 #define SCALEBITS 16 /* speediest right-shift on some machines */
80 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
81 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
82
83
84 /*
85 * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
86 * This is taken directly from jdcolor.c; see that file for more info.
87 */
88
89 LOCAL(void)
90 build_ycc_rgb_table (j_decompress_ptr cinfo)
91 /* Normal case, sYCC */
92 {
93 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
94 int i;
95 INT32 x;
96 SHIFT_TEMPS
97
98 upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
99 ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
100 upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
101 ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
102 upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
103 ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
104 upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
105 ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
106
107 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
108 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
109 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
110 /* Cr=>R value is nearest int to 1.402 * x */
111 upsample->Cr_r_tab[i] = (int) DESCALE(FIX(1.402) * x, SCALEBITS);
112 /* Cb=>B value is nearest int to 1.772 * x */
113 upsample->Cb_b_tab[i] = (int) DESCALE(FIX(1.772) * x, SCALEBITS);
114 /* Cr=>G value is scaled-up -0.714136286 * x */
115 upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
116 /* Cb=>G value is scaled-up -0.344136286 * x */
117 /* We also add in ONE_HALF so that need not do it in inner loop */
118 upsample->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
119 }
120 }
121
122
123 LOCAL(void)
124 build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
125 /* Wide gamut case, bg-sYCC */
126 {
127 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
128 int i;
129 INT32 x;
130 SHIFT_TEMPS
131
132 upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
133 ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
134 upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
135 ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
136 upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
137 ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
138 upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
139 ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
140
141 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
142 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
143 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
144 /* Cr=>R value is nearest int to 2.804 * x */
145 upsample->Cr_r_tab[i] = (int) DESCALE(FIX(2.804) * x, SCALEBITS);
146 /* Cb=>B value is nearest int to 3.544 * x */
147 upsample->Cb_b_tab[i] = (int) DESCALE(FIX(3.544) * x, SCALEBITS);
148 /* Cr=>G value is scaled-up -1.428272572 * x */
149 upsample->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
150 /* Cb=>G value is scaled-up -0.688272572 * x */
151 /* We also add in ONE_HALF so that need not do it in inner loop */
152 upsample->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
153 }
154 }
155
156
157 /*
158 * Initialize for an upsampling pass.
159 */
160
161 METHODDEF(void)
162 start_pass_merged_upsample (j_decompress_ptr cinfo)
163 {
164 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
165
166 /* Mark the spare buffer empty */
167 upsample->spare_full = FALSE;
168 /* Initialize total-height counter for detecting bottom of image */
169 upsample->rows_to_go = cinfo->output_height;
170 }
171
172
173 /*
174 * Control routine to do upsampling (and color conversion).
175 *
176 * The control routine just handles the row buffering considerations.
177 */
178
179 METHODDEF(void)
180 merged_2v_upsample (j_decompress_ptr cinfo,
181 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
182 JDIMENSION in_row_groups_avail,
183 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
184 JDIMENSION out_rows_avail)
185 /* 2:1 vertical sampling case: may need a spare row. */
186 {
187 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
188 JSAMPROW work_ptrs[2];
189 JDIMENSION num_rows; /* number of rows returned to caller */
190
191 if (upsample->spare_full) {
192 /* If we have a spare row saved from a previous cycle, just return it. */
193 jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
194 1, upsample->out_row_width);
195 num_rows = 1;
196 upsample->spare_full = FALSE;
197 } else {
198 /* Figure number of rows to return to caller. */
199 num_rows = 2;
200 /* Not more than the distance to the end of the image. */
201 if (num_rows > upsample->rows_to_go)
202 num_rows = upsample->rows_to_go;
203 /* And not more than what the client can accept: */
204 out_rows_avail -= *out_row_ctr;
205 if (num_rows > out_rows_avail)
206 num_rows = out_rows_avail;
207 /* Create output pointer array for upsampler. */
208 work_ptrs[0] = output_buf[*out_row_ctr];
209 if (num_rows > 1) {
210 work_ptrs[1] = output_buf[*out_row_ctr + 1];
211 } else {
212 work_ptrs[1] = upsample->spare_row;
213 upsample->spare_full = TRUE;
214 }
215 /* Now do the upsampling. */
216 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
217 }
218
219 /* Adjust counts */
220 *out_row_ctr += num_rows;
221 upsample->rows_to_go -= num_rows;
222 /* When the buffer is emptied, declare this input row group consumed */
223 if (! upsample->spare_full)
224 (*in_row_group_ctr)++;
225 }
226
227
228 METHODDEF(void)
229 merged_1v_upsample (j_decompress_ptr cinfo,
230 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
231 JDIMENSION in_row_groups_avail,
232 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
233 JDIMENSION out_rows_avail)
234 /* 1:1 vertical sampling case: much easier, never need a spare row. */
235 {
236 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
237
238 /* Just do the upsampling. */
239 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
240 output_buf + *out_row_ctr);
241 /* Adjust counts */
242 (*out_row_ctr)++;
243 (*in_row_group_ctr)++;
244 }
245
246
247 /*
248 * These are the routines invoked by the control routines to do
249 * the actual upsampling/conversion. One row group is processed per call.
250 *
251 * Note: since we may be writing directly into application-supplied buffers,
252 * we have to be honest about the output width; we can't assume the buffer
253 * has been rounded up to an even width.
254 */
255
256
257 /*
258 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
259 */
260
261 METHODDEF(void)
262 h2v1_merged_upsample (j_decompress_ptr cinfo,
263 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
264 JSAMPARRAY output_buf)
265 {
266 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
267 register int y, cred, cgreen, cblue;
268 int cb, cr;
269 register JSAMPROW outptr;
270 JSAMPROW inptr0, inptr1, inptr2;
271 JDIMENSION col;
272 /* copy these pointers into registers if possible */
273 register JSAMPLE * range_limit = cinfo->sample_range_limit;
274 int * Crrtab = upsample->Cr_r_tab;
275 int * Cbbtab = upsample->Cb_b_tab;
276 INT32 * Crgtab = upsample->Cr_g_tab;
277 INT32 * Cbgtab = upsample->Cb_g_tab;
278 SHIFT_TEMPS
279
280 inptr0 = input_buf[0][in_row_group_ctr];
281 inptr1 = input_buf[1][in_row_group_ctr];
282 inptr2 = input_buf[2][in_row_group_ctr];
283 outptr = output_buf[0];
284 /* Loop for each pair of output pixels */
285 for (col = cinfo->output_width >> 1; col > 0; col--) {
286 /* Do the chroma part of the calculation */
287 cb = GETJSAMPLE(*inptr1++);
288 cr = GETJSAMPLE(*inptr2++);
289 cred = Crrtab[cr];
290 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
291 cblue = Cbbtab[cb];
292 /* Fetch 2 Y values and emit 2 pixels */
293 y = GETJSAMPLE(*inptr0++);
294 outptr[RGB_RED] = range_limit[y + cred];
295 outptr[RGB_GREEN] = range_limit[y + cgreen];
296 outptr[RGB_BLUE] = range_limit[y + cblue];
297 outptr += RGB_PIXELSIZE;
298 y = GETJSAMPLE(*inptr0++);
299 outptr[RGB_RED] = range_limit[y + cred];
300 outptr[RGB_GREEN] = range_limit[y + cgreen];
301 outptr[RGB_BLUE] = range_limit[y + cblue];
302 outptr += RGB_PIXELSIZE;
303 }
304 /* If image width is odd, do the last output column separately */
305 if (cinfo->output_width & 1) {
306 cb = GETJSAMPLE(*inptr1);
307 cr = GETJSAMPLE(*inptr2);
308 cred = Crrtab[cr];
309 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
310 cblue = Cbbtab[cb];
311 y = GETJSAMPLE(*inptr0);
312 outptr[RGB_RED] = range_limit[y + cred];
313 outptr[RGB_GREEN] = range_limit[y + cgreen];
314 outptr[RGB_BLUE] = range_limit[y + cblue];
315 }
316 }
317
318
319 /*
320 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
321 */
322
323 METHODDEF(void)
324 h2v2_merged_upsample (j_decompress_ptr cinfo,
325 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
326 JSAMPARRAY output_buf)
327 {
328 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
329 register int y, cred, cgreen, cblue;
330 int cb, cr;
331 register JSAMPROW outptr0, outptr1;
332 JSAMPROW inptr00, inptr01, inptr1, inptr2;
333 JDIMENSION col;
334 /* copy these pointers into registers if possible */
335 register JSAMPLE * range_limit = cinfo->sample_range_limit;
336 int * Crrtab = upsample->Cr_r_tab;
337 int * Cbbtab = upsample->Cb_b_tab;
338 INT32 * Crgtab = upsample->Cr_g_tab;
339 INT32 * Cbgtab = upsample->Cb_g_tab;
340 SHIFT_TEMPS
341
342 inptr00 = input_buf[0][in_row_group_ctr*2];
343 inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
344 inptr1 = input_buf[1][in_row_group_ctr];
345 inptr2 = input_buf[2][in_row_group_ctr];
346 outptr0 = output_buf[0];
347 outptr1 = output_buf[1];
348 /* Loop for each group of output pixels */
349 for (col = cinfo->output_width >> 1; col > 0; col--) {
350 /* Do the chroma part of the calculation */
351 cb = GETJSAMPLE(*inptr1++);
352 cr = GETJSAMPLE(*inptr2++);
353 cred = Crrtab[cr];
354 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
355 cblue = Cbbtab[cb];
356 /* Fetch 4 Y values and emit 4 pixels */
357 y = GETJSAMPLE(*inptr00++);
358 outptr0[RGB_RED] = range_limit[y + cred];
359 outptr0[RGB_GREEN] = range_limit[y + cgreen];
360 outptr0[RGB_BLUE] = range_limit[y + cblue];
361 outptr0 += RGB_PIXELSIZE;
362 y = GETJSAMPLE(*inptr00++);
363 outptr0[RGB_RED] = range_limit[y + cred];
364 outptr0[RGB_GREEN] = range_limit[y + cgreen];
365 outptr0[RGB_BLUE] = range_limit[y + cblue];
366 outptr0 += RGB_PIXELSIZE;
367 y = GETJSAMPLE(*inptr01++);
368 outptr1[RGB_RED] = range_limit[y + cred];
369 outptr1[RGB_GREEN] = range_limit[y + cgreen];
370 outptr1[RGB_BLUE] = range_limit[y + cblue];
371 outptr1 += RGB_PIXELSIZE;
372 y = GETJSAMPLE(*inptr01++);
373 outptr1[RGB_RED] = range_limit[y + cred];
374 outptr1[RGB_GREEN] = range_limit[y + cgreen];
375 outptr1[RGB_BLUE] = range_limit[y + cblue];
376 outptr1 += RGB_PIXELSIZE;
377 }
378 /* If image width is odd, do the last output column separately */
379 if (cinfo->output_width & 1) {
380 cb = GETJSAMPLE(*inptr1);
381 cr = GETJSAMPLE(*inptr2);
382 cred = Crrtab[cr];
383 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
384 cblue = Cbbtab[cb];
385 y = GETJSAMPLE(*inptr00);
386 outptr0[RGB_RED] = range_limit[y + cred];
387 outptr0[RGB_GREEN] = range_limit[y + cgreen];
388 outptr0[RGB_BLUE] = range_limit[y + cblue];
389 y = GETJSAMPLE(*inptr01);
390 outptr1[RGB_RED] = range_limit[y + cred];
391 outptr1[RGB_GREEN] = range_limit[y + cgreen];
392 outptr1[RGB_BLUE] = range_limit[y + cblue];
393 }
394 }
395
396
397 /*
398 * Module initialization routine for merged upsampling/color conversion.
399 *
400 * NB: this is called under the conditions determined by use_merged_upsample()
401 * in jdmaster.c. That routine MUST correspond to the actual capabilities
402 * of this module; no safety checks are made here.
403 */
404
405 GLOBAL(void)
406 jinit_merged_upsampler (j_decompress_ptr cinfo)
407 {
408 my_upsample_ptr upsample;
409
410 upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small)
411 ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler));
412 cinfo->upsample = &upsample->pub;
413 upsample->pub.start_pass = start_pass_merged_upsample;
414 upsample->pub.need_context_rows = FALSE;
415
416 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
417
418 if (cinfo->max_v_samp_factor == 2) {
419 upsample->pub.upsample = merged_2v_upsample;
420 upsample->upmethod = h2v2_merged_upsample;
421 /* Allocate a spare row buffer */
422 upsample->spare_row = (JSAMPROW) (*cinfo->mem->alloc_large)
423 ((j_common_ptr) cinfo, JPOOL_IMAGE,
424 (size_t) upsample->out_row_width * SIZEOF(JSAMPLE));
425 } else {
426 upsample->pub.upsample = merged_1v_upsample;
427 upsample->upmethod = h2v1_merged_upsample;
428 /* No spare row needed */
429 upsample->spare_row = NULL;
430 }
431
432 if (cinfo->jpeg_color_space == JCS_BG_YCC)
433 build_bg_ycc_rgb_table(cinfo);
434 else
435 build_ycc_rgb_table(cinfo);
436 }
437
438 #endif /* UPSAMPLE_MERGING_SUPPORTED */