1 /************************************************************************
2 *
3 * yuv2rgb.c, colour space conversion for tmndecode (H.263 decoder)
4 * Copyright (C) 1995, 1996 Telenor R&D, Norway
5 *
6 * Contacts:
7 * Robert Danielsen <Robert.Danielsen@nta.no>
8 *
9 * Telenor Research and Development http://www.nta.no/brukere/DVC/
10 * P.O.Box 83 tel.: +47 63 84 84 00
11 * N-2007 Kjeller, Norway fax.: +47 63 81 00 76
12 *
13 * Copyright (C) 1997 University of BC, Canada
14 * Modified by: Michael Gallant <mikeg@ee.ubc.ca>
15 * Guy Cote <guyc@ee.ubc.ca>
16 * Berna Erol <bernae@ee.ubc.ca>
17 *
18 * Contacts:
19 * Michael Gallant <mikeg@ee.ubc.ca>
20 *
21 * UBC Image Processing Laboratory http://www.ee.ubc.ca/image
22 * 2356 Main Mall tel.: +1 604 822 4051
23 * Vancouver BC Canada V6T1Z4 fax.: +1 604 822 5949
24 *
25 ************************************************************************/
26
27 /* Disclaimer of Warranty
28 *
29 * These software programs are available to the user without any license fee
30 * or royalty on an "as is" basis. The University of British Columbia
31 * disclaims any and all warranties, whether express, implied, or
32 * statuary, including any implied warranties or merchantability or of
33 * fitness for a particular purpose. In no event shall the
34 * copyright-holder be liable for any incidental, punitive, or
35 * consequential damages of any kind whatsoever arising from the use of
36 * these programs.
37 *
38 * This disclaimer of warranty extends to the user of these programs and
39 * user's customers, employees, agents, transferees, successors, and
40 * assigns.
41 *
42 * The University of British Columbia does not represent or warrant that the
43 * programs furnished hereunder are free of infringement of any
44 * third-party patents.
45 *
46 * Commercial implementations of H.263, including shareware, are subject to
47 * royalty fees to patent holders. Many of these patents are general
48 * enough such that they are unavoidable regardless of implementation
49 * design.
50 *
51 */
52
53
54 /* Copyright (c) 1995 Erik Corry All rights reserved.
55 *
56 * Permission to use, copy, modify, and distribute this software and its
57 * documentation for any purpose, without fee, and without written
58 * agreement is hereby granted, provided that the above copyright notice
59 * and the following two paragraphs appear in all copies of this software.
60 *
61 * IN NO EVENT SHALL ERIK CORRY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
62 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
63 * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIK CORRY HAS BEEN
64 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 * ERIK CORRY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
67 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
68 * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
69 * BASIS, AND ERIK CORRY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
70 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */
71
72
73 #include "config.h"
74 #include "tmndec.h"
75 #include "global.h"
76
77 #ifdef DISPLAY
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <math.h>
81 #include <X11/Xlib.h>
82 #include <X11/Xutil.h>
83 #endif
84
85
86 #ifdef DISPLAY
87
88 #undef INTERPOLATE
89
90 /* Erik Corry's multi-byte dither routines.
91 *
92 * The basic idea is that the Init generates all the necessary tables. The
93 * tables incorporate the information about the layout of pixels in the
94 * XImage, so that it should be able to cope with 15-bit, 16-bit 24-bit
95 * (non-packed) and 32-bit (10-11 bits per color!) screens. At present it
96 * cannot cope with 24-bit packed mode, since this involves getting down
97 * to byte level again. It is assumed that the bits for each color are
98 * contiguous in the longword.
99 *
100 * Writing to memory is done in shorts or ints. (Unfortunately, short is not
101 * very fast on Alpha, so there is room for improvement here). There is no
102 * dither time check for overflow - instead the tables have slack at each
103 * end. This is likely to be faster than an 'if' test as many modern
104 * architectures are really bad at ifs. Potentially, each '&&' causes a
105 * pipeline flush!
106 *
107 * There is no shifting and fixed point arithmetic, as I really doubt you can
108 * see the difference, and it costs. This may be just my bias, since I
109 * heard that Intel is really bad at shifting. */
110
111 /* Gamma correction stuff */
112
113 #define GAMMA_CORRECTION(x) ((int)(pow((x) / 255.0, 1.0 / gammaCorrect) * 255.0))
114 #define CHROMA_CORRECTION256(x) ((x) >= 128 /
115 ? 128 + mmin(127, (int)(((x) - 128.0) * chromaCorrect)) /
116 : 128 - mmin(128, (int)((128.0 - (x)) * chromaCorrect)))
117 #define CHROMA_CORRECTION128(x) ((x) >= 0 /
118 ? mmin(127, (int)(((x) * chromaCorrect))) /
119 : mmax(-128, (int)(((x) * chromaCorrect))))
120 #define CHROMA_CORRECTION256D(x) ((x) >= 128 /
121 ? 128.0 + mmin(127.0, (((x) - 128.0) * chromaCorrect)) /
122 : 128.0 - mmin(128.0, (((128.0 - (x)) * chromaCorrect))))
123 #define CHROMA_CORRECTION128D(x) ((x) >= 0 /
124 ? mmin(127.0, ((x) * chromaCorrect)) /
125 : mmax(-128.0, ((x) * chromaCorrect)))
126
127
128 /* Flag for gamma correction */
129 int gammaCorrectFlag = 0;
130 double gammaCorrect = 1.0;
131
132 /* Flag for chroma correction */
133 int chromaCorrectFlag = 0;
134 double chromaCorrect = 1.0;
135
136 /* How many 1 bits are there in the longword. Low performance, do not call
137 * often. */
138 static int
139 number_of_bits_set (a)
140 unsigned long a;
141 {
142 if (!a)
143 return 0;
144 if (a & 1)
145 return 1 + number_of_bits_set (a >> 1);
146 return (number_of_bits_set (a >> 1));
147 }
148
149 /* Shift the 0s in the least significant end out of the longword. Low
150 * performance, do not call often. */
151 static unsigned long
152 shifted_down (a)
153 unsigned long a;
154 {
155 if (!a)
156 return 0;
157 if (a & 1)
158 return a;
159 return a >> 1;
160 }
161
162 /* How many 0 bits are there at most significant end of longword. Low
163 * performance, do not call often. */
164 static int
165 free_bits_at_top (a)
166 unsigned long a;
167 {
168 /* assume char is 8 bits */
169 if (!a)
170 return sizeof (unsigned long) * 8;
171 /* assume twos complement */
172 if (((long) a) < 0l)
173 return 0;
174 return 1 + free_bits_at_top (a << 1);
175 }
176
177 /* How many 0 bits are there at least significant end of longword. Low
178 * performance, do not call often. */
179 static int
180 free_bits_at_bottom (a)
181 unsigned long a;
182 {
183 /* assume char is 8 bits */
184 if (!a)
185 return sizeof (unsigned long) * 8;
186 if (((long) a) & 1l)
187 return 0;
188 return 1 + free_bits_at_bottom (a >> 1);
189 }
190
191 static int *L_tab, *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab;
192
193 /* We define tables that convert a color value between -256 and 512 into
194 * the R, G and B parts of the pixel. The normal range is 0-255. */
195
196 static long *r_2_pix;
197 static long *g_2_pix;
198 static long *b_2_pix;
199 static long *r_2_pix_alloc;
200 static long *g_2_pix_alloc;
201 static long *b_2_pix_alloc;
202
203
204
205
206 /* --------------------------------------------------------------
207 *
208 * InitColor16Dither --
209 *
210 * To get rid of the multiply and other conversions in color dither, we use a
211 * lookup table.
212 *
213 * Results: None.
214 *
215 * Side effects: The lookup tables are initialized.
216 *
217 * -------------------------------------------------------------- */
218
219 void
220 InitColorDither (thirty2)
221 int thirty2;
222 {
223 extern XImage *ximage;
224 extern unsigned long wpixel[3];
225 /* misuse of the wpixel array for the pixel masks. Note that this
226 * implies that the window is created before this routine is called */
227
228
229 unsigned long red_mask = wpixel[0];
230 unsigned long green_mask = wpixel[1];
231 unsigned long blue_mask = wpixel[2];
232
233 int CR, CB, i;
234
235 if (ximage->bits_per_pixel == 24) /* not necessary in non-packed mode */
236 init_dither_tab ();
237
238 L_tab = (int *) malloc (256 * sizeof (int));
239 Cr_r_tab = (int *) malloc (256 * sizeof (int));
240 Cr_g_tab = (int *) malloc (256 * sizeof (int));
241 Cb_g_tab = (int *) malloc (256 * sizeof (int));
242 Cb_b_tab = (int *) malloc (256 * sizeof (int));
243
244 r_2_pix_alloc = (long *) malloc (768 * sizeof (long));
245 g_2_pix_alloc = (long *) malloc (768 * sizeof (long));
246 b_2_pix_alloc = (long *) malloc (768 * sizeof (long));
247
248 if (L_tab == NULL ||
249 Cr_r_tab == NULL ||
250 Cr_g_tab == NULL ||
251 Cb_g_tab == NULL ||
252 Cb_b_tab == NULL ||
253 r_2_pix_alloc == NULL ||
254 g_2_pix_alloc == NULL ||
255 b_2_pix_alloc == NULL)
256 {
257 fprintf (stderr, "Could not get enough memory in InitColorDither/n");
258 exit (1);
259 }
260 for (i = 0; i < 256; i++)
261 {
262 L_tab[i] = i;
263 if (gammaCorrectFlag)
264 {
265 L_tab[i] = GAMMA_CORRECTION (i);
266 }
267 CB = CR = i;
268
269 if (chromaCorrectFlag)
270 {
271 CB -= 128;
272 CB = CHROMA_CORRECTION128 (CB);
273 CR -= 128;
274 CR = CHROMA_CORRECTION128 (CR);
275 } else
276 {
277 CB -= 128;
278 CR -= 128;
279 }
280 /* was Cr_r_tab[i] = 1.596 * CR; Cr_g_tab[i] = -0.813 * CR;
281 * Cb_g_tab[i] = -0.391 * CB; Cb_b_tab[i] = 2.018 * CB; but they
282 * were just messed up. Then was (_Video Deymstified_): Cr_r_tab[i] =
283 * 1.366 * CR; Cr_g_tab[i] = -0.700 * CR; Cb_g_tab[i] = -0.334 * CB;
284 * Cb_b_tab[i] = 1.732 * CB; but really should be: (from ITU-R
285 * BT.470-2 System B, G and SMPTE 170M ) */
286 Cr_r_tab[i] = (0.419 / 0.299) * CR;
287 Cr_g_tab[i] = -(0.299 / 0.419) * CR;
288 Cb_g_tab[i] = -(0.114 / 0.331) * CB;
289 Cb_b_tab[i] = (0.587 / 0.331) * CB;
290
291 /* though you could argue for: SMPTE 240M Cr_r_tab[i] = (0.445/0.212) *
292 * CR; Cr_g_tab[i] = -(0.212/0.445) * CR; Cb_g_tab[i] = -(0.087/0.384) *
293 * CB; Cb_b_tab[i] = (0.701/0.384) * CB; FCC Cr_r_tab[i] =
294 * (0.421/0.30) * CR; Cr_g_tab[i] = -(0.30/0.421) * CR; Cb_g_tab[i] =
295 * -(0.11/0.331) * CB; Cb_b_tab[i] = (0.59/0.331) * CB; ITU-R BT.709
296 * Cr_r_tab[i] = (0.454/0.2125) * CR; Cr_g_tab[i] = -(0.2125/0.454) *
297 * CR; Cb_g_tab[i] = -(0.0721/0.386) * CB; Cb_b_tab[i] =
298 * (0.7154/0.386) * CB; */
299 }
300
301 /* Set up entries 0-255 in rgb-to-pixel value tables. */
302 for (i = 0; i < 256; i++)
303 {
304 r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set (red_mask));
305 r_2_pix_alloc[i + 256] <<= free_bits_at_bottom (red_mask);
306 g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set (green_mask));
307 g_2_pix_alloc[i + 256] <<= free_bits_at_bottom (green_mask);
308 b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set (blue_mask));
309 b_2_pix_alloc[i + 256] <<= free_bits_at_bottom (blue_mask);
310 /* If we have 16-bit output depth, then we double the value in the top
311 * word. This means that we can write out both pixels in the pixel
312 * doubling mode with one op. It is harmless in the normal case as
313 * storing a 32-bit value through a short pointer will lose the top
314 * bits anyway. A similar optimisation for Alpha for 64 bit has been
315 * prepared for, but is not yet implemented. */
316 if (!thirty2)
317 {
318
319 r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 16;
320 g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 16;
321 b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 16;
322
323 }
324 #ifdef SIXTYFOUR_BIT
325 if (thirty2)
326 {
327
328 r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 32;
329 g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 32;
330 b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 32;
331
332 }
333 #endif
334 }
335
336 /* Spread out the values we have to the rest of the array so that we do
337 * not need to check for overflow. */
338 for (i = 0; i < 256; i++)
339 {
340 r_2_pix_alloc[i] = r_2_pix_alloc[256];
341 r_2_pix_alloc[i + 512] = r_2_pix_alloc[511];
342 g_2_pix_alloc[i] = g_2_pix_alloc[256];
343 g_2_pix_alloc[i + 512] = g_2_pix_alloc[511];
344 b_2_pix_alloc[i] = b_2_pix_alloc[256];
345 b_2_pix_alloc[i + 512] = b_2_pix_alloc[511];
346 }
347
348 r_2_pix = r_2_pix_alloc + 256;
349 g_2_pix = g_2_pix_alloc + 256;
350 b_2_pix = b_2_pix_alloc + 256;
351
352 }
353
354
355
356
357 /* --------------------------------------------------------------
358 *
359 * Color16DitherImage --
360 *
361 * Converts image into 16 bit color.
362 *
363 * Results: None.
364 *
365 * Side effects: None.
366 *
367 * -------------------------------------------------------------- */
368
369 void
370 Color16DitherImage (src, out)
371 unsigned char *src[];
372 unsigned char *out;
373 {
374 unsigned char *lum = src[0];
375 unsigned char *cb = src[1];
376 unsigned char *cr = src[2];
377 int cols;
378 int rows;
379
380 int L, CR, CB;
381 unsigned short *row1, *row2;
382 unsigned char *lum2;
383 int x, y;
384 int cr_r;
385 int cr_g;
386 int cb_g;
387 int cb_b;
388 int cols_2;
389
390 cols = coded_picture_width;
391 rows = coded_picture_height;
392 if (expand)
393 {
394 cols *= 2;
395 rows *= 2;
396 }
397 cols_2 = cols / 2;
398
399 row1 = (unsigned short *) out;
400 row2 = row1 + cols_2 + cols_2;
401 lum2 = lum + cols_2 + cols_2;
402
403 for (y = 0; y < rows; y += 2)
404 {
405 for (x = 0; x < cols_2; x++)
406 {
407 int R, G, B;
408
409 CR = *cr++;
410 CB = *cb++;
411 cr_r = Cr_r_tab[CR];
412 cr_g = Cr_g_tab[CR];
413 cb_g = Cb_g_tab[CB];
414 cb_b = Cb_b_tab[CB];
415
416 L = L_tab[(int) *lum++];
417
418 R = L + cr_r;
419 G = L + cr_g + cb_g;
420 B = L + cb_b;
421
422 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
423
424 #ifdef INTERPOLATE
425 if (x != cols_2 - 1)
426 {
427 CR = (CR + *cr) >> 1;
428 CB = (CB + *cb) >> 1;
429 cr_r = Cr_r_tab[CR];
430 cr_g = Cr_g_tab[CR];
431 cb_g = Cb_g_tab[CB];
432 cb_b = Cb_b_tab[CB];
433 }
434 #endif
435
436 L = L_tab[(int) *lum++];
437
438 R = L + cr_r;
439 G = L + cr_g + cb_g;
440 B = L + cb_b;
441
442 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
443
444 /* Now, do second row. */
445 #ifdef INTERPOLATE
446 if (y != rows - 2)
447 {
448 CR = (CR + *(cr + cols_2 - 1)) >> 1;
449 CB = (CB + *(cb + cols_2 - 1)) >> 1;
450 cr_r = Cr_r_tab[CR];
451 cr_g = Cr_g_tab[CR];
452 cb_g = Cb_g_tab[CB];
453 cb_b = Cb_b_tab[CB];
454 }
455 #endif
456
457 L = L_tab[(int) *lum2++];
458 R = L + cr_r;
459 G = L + cr_g + cb_g;
460 B = L + cb_b;
461
462 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
463
464 L = L_tab[(int) *lum2++];
465 R = L + cr_r;
466 G = L + cr_g + cb_g;
467 B = L + cb_b;
468
469 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
470 }
471 /* These values are at the start of the next line, (due to the ++'s
472 * above),but they need to be at the start of the line after that. */
473 lum += cols_2 + cols_2;
474 lum2 += cols_2 + cols_2;
475 row1 += cols_2 + cols_2;
476 row2 += cols_2 + cols_2;
477 }
478 }
479
480
481
482
483
484 /* --------------------------------------------------------------
485 *
486 * Color32DitherImage --
487 *
488 * Converts image into 32 bit color (or 24-bit non-packed).
489 *
490 * Results: None.
491 *
492 * Side effects: None.
493 *
494 * -------------------------------------------------------------- */
495
496 /* This is a copysoft version of the function above with ints instead of
497 * shorts to cause a 4-byte pixel size */
498
499 void
500 Color32DitherImage (src, out)
501 unsigned char *src[];
502 unsigned char *out;
503 {
504 unsigned char *lum = src[0];
505 unsigned char *cb = src[1];
506 unsigned char *cr = src[2];
507 int cols;
508 int rows;
509
510 int L, CR, CB;
511 unsigned int *row1, *row2;
512 unsigned char *lum2;
513 int x, y;
514 int cr_r;
515 int cr_g;
516 int cb_g;
517 int cb_b;
518 int cols_2;
519
520 cols = coded_picture_width;
521 rows = coded_picture_height;
522 if (expand)
523 {
524 cols *= 2;
525 rows *= 2;
526 }
527 cols_2 = cols / 2;
528
529 row1 = (unsigned int *) out;
530 row2 = row1 + cols_2 + cols_2;
531 lum2 = lum + cols_2 + cols_2;
532 for (y = 0; y < rows; y += 2)
533 {
534 for (x = 0; x < cols_2; x++)
535 {
536 int R, G, B;
537
538 CR = *cr++;
539 CB = *cb++;
540 cr_r = Cr_r_tab[CR];
541 cr_g = Cr_g_tab[CR];
542 cb_g = Cb_g_tab[CB];
543 cb_b = Cb_b_tab[CB];
544
545 L = L_tab[(int) *lum++];
546
547 R = L + cr_r;
548 G = L + cr_g + cb_g;
549 B = L + cb_b;
550
551 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
552
553 #ifdef INTERPOLATE
554 if (x != cols_2 - 1)
555 {
556 CR = (CR + *cr) >> 1;
557 CB = (CB + *cb) >> 1;
558 cr_r = Cr_r_tab[CR];
559 cr_g = Cr_g_tab[CR];
560 cb_g = Cb_g_tab[CB];
561 cb_b = Cb_b_tab[CB];
562 }
563 #endif
564
565 L = L_tab[(int) *lum++];
566
567 R = L + cr_r;
568 G = L + cr_g + cb_g;
569 B = L + cb_b;
570
571 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
572
573 /* Now, do second row. */
574
575 #ifdef INTERPOLATE
576 if (y != rows - 2)
577 {
578 CR = (CR + *(cr + cols_2 - 1)) >> 1;
579 CB = (CB + *(cb + cols_2 - 1)) >> 1;
580 cr_r = Cr_r_tab[CR];
581 cr_g = Cr_g_tab[CR];
582 cb_g = Cb_g_tab[CB];
583 cb_b = Cb_b_tab[CB];
584 }
585 #endif
586
587 L = L_tab[(int) *lum2++];
588 R = L + cr_r;
589 G = L + cr_g + cb_g;
590 B = L + cb_b;
591
592 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
593
594 L = L_tab[(int) *lum2++];
595 R = L + cr_r;
596 G = L + cr_g + cb_g;
597 B = L + cb_b;
598
599 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
600 }
601 lum += cols_2 + cols_2;
602 lum2 += cols_2 + cols_2;
603 row1 += cols_2 + cols_2;
604 row2 += cols_2 + cols_2;
605 }
606 }
607
608
609
610
611
612 #endif
613
【来源】
2 *
3 * yuv2rgb.c, colour space conversion for tmndecode (H.263 decoder)
4 * Copyright (C) 1995, 1996 Telenor R&D, Norway
5 *
6 * Contacts:
7 * Robert Danielsen <Robert.Danielsen@nta.no>
8 *
9 * Telenor Research and Development http://www.nta.no/brukere/DVC/
10 * P.O.Box 83 tel.: +47 63 84 84 00
11 * N-2007 Kjeller, Norway fax.: +47 63 81 00 76
12 *
13 * Copyright (C) 1997 University of BC, Canada
14 * Modified by: Michael Gallant <mikeg@ee.ubc.ca>
15 * Guy Cote <guyc@ee.ubc.ca>
16 * Berna Erol <bernae@ee.ubc.ca>
17 *
18 * Contacts:
19 * Michael Gallant <mikeg@ee.ubc.ca>
20 *
21 * UBC Image Processing Laboratory http://www.ee.ubc.ca/image
22 * 2356 Main Mall tel.: +1 604 822 4051
23 * Vancouver BC Canada V6T1Z4 fax.: +1 604 822 5949
24 *
25 ************************************************************************/
26
27 /* Disclaimer of Warranty
28 *
29 * These software programs are available to the user without any license fee
30 * or royalty on an "as is" basis. The University of British Columbia
31 * disclaims any and all warranties, whether express, implied, or
32 * statuary, including any implied warranties or merchantability or of
33 * fitness for a particular purpose. In no event shall the
34 * copyright-holder be liable for any incidental, punitive, or
35 * consequential damages of any kind whatsoever arising from the use of
36 * these programs.
37 *
38 * This disclaimer of warranty extends to the user of these programs and
39 * user's customers, employees, agents, transferees, successors, and
40 * assigns.
41 *
42 * The University of British Columbia does not represent or warrant that the
43 * programs furnished hereunder are free of infringement of any
44 * third-party patents.
45 *
46 * Commercial implementations of H.263, including shareware, are subject to
47 * royalty fees to patent holders. Many of these patents are general
48 * enough such that they are unavoidable regardless of implementation
49 * design.
50 *
51 */
52
53
54 /* Copyright (c) 1995 Erik Corry All rights reserved.
55 *
56 * Permission to use, copy, modify, and distribute this software and its
57 * documentation for any purpose, without fee, and without written
58 * agreement is hereby granted, provided that the above copyright notice
59 * and the following two paragraphs appear in all copies of this software.
60 *
61 * IN NO EVENT SHALL ERIK CORRY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
62 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
63 * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIK CORRY HAS BEEN
64 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 * ERIK CORRY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
67 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
68 * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
69 * BASIS, AND ERIK CORRY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
70 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */
71
72
73 #include "config.h"
74 #include "tmndec.h"
75 #include "global.h"
76
77 #ifdef DISPLAY
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <math.h>
81 #include <X11/Xlib.h>
82 #include <X11/Xutil.h>
83 #endif
84
85
86 #ifdef DISPLAY
87
88 #undef INTERPOLATE
89
90 /* Erik Corry's multi-byte dither routines.
91 *
92 * The basic idea is that the Init generates all the necessary tables. The
93 * tables incorporate the information about the layout of pixels in the
94 * XImage, so that it should be able to cope with 15-bit, 16-bit 24-bit
95 * (non-packed) and 32-bit (10-11 bits per color!) screens. At present it
96 * cannot cope with 24-bit packed mode, since this involves getting down
97 * to byte level again. It is assumed that the bits for each color are
98 * contiguous in the longword.
99 *
100 * Writing to memory is done in shorts or ints. (Unfortunately, short is not
101 * very fast on Alpha, so there is room for improvement here). There is no
102 * dither time check for overflow - instead the tables have slack at each
103 * end. This is likely to be faster than an 'if' test as many modern
104 * architectures are really bad at ifs. Potentially, each '&&' causes a
105 * pipeline flush!
106 *
107 * There is no shifting and fixed point arithmetic, as I really doubt you can
108 * see the difference, and it costs. This may be just my bias, since I
109 * heard that Intel is really bad at shifting. */
110
111 /* Gamma correction stuff */
112
113 #define GAMMA_CORRECTION(x) ((int)(pow((x) / 255.0, 1.0 / gammaCorrect) * 255.0))
114 #define CHROMA_CORRECTION256(x) ((x) >= 128 /
115 ? 128 + mmin(127, (int)(((x) - 128.0) * chromaCorrect)) /
116 : 128 - mmin(128, (int)((128.0 - (x)) * chromaCorrect)))
117 #define CHROMA_CORRECTION128(x) ((x) >= 0 /
118 ? mmin(127, (int)(((x) * chromaCorrect))) /
119 : mmax(-128, (int)(((x) * chromaCorrect))))
120 #define CHROMA_CORRECTION256D(x) ((x) >= 128 /
121 ? 128.0 + mmin(127.0, (((x) - 128.0) * chromaCorrect)) /
122 : 128.0 - mmin(128.0, (((128.0 - (x)) * chromaCorrect))))
123 #define CHROMA_CORRECTION128D(x) ((x) >= 0 /
124 ? mmin(127.0, ((x) * chromaCorrect)) /
125 : mmax(-128.0, ((x) * chromaCorrect)))
126
127
128 /* Flag for gamma correction */
129 int gammaCorrectFlag = 0;
130 double gammaCorrect = 1.0;
131
132 /* Flag for chroma correction */
133 int chromaCorrectFlag = 0;
134 double chromaCorrect = 1.0;
135
136 /* How many 1 bits are there in the longword. Low performance, do not call
137 * often. */
138 static int
139 number_of_bits_set (a)
140 unsigned long a;
141 {
142 if (!a)
143 return 0;
144 if (a & 1)
145 return 1 + number_of_bits_set (a >> 1);
146 return (number_of_bits_set (a >> 1));
147 }
148
149 /* Shift the 0s in the least significant end out of the longword. Low
150 * performance, do not call often. */
151 static unsigned long
152 shifted_down (a)
153 unsigned long a;
154 {
155 if (!a)
156 return 0;
157 if (a & 1)
158 return a;
159 return a >> 1;
160 }
161
162 /* How many 0 bits are there at most significant end of longword. Low
163 * performance, do not call often. */
164 static int
165 free_bits_at_top (a)
166 unsigned long a;
167 {
168 /* assume char is 8 bits */
169 if (!a)
170 return sizeof (unsigned long) * 8;
171 /* assume twos complement */
172 if (((long) a) < 0l)
173 return 0;
174 return 1 + free_bits_at_top (a << 1);
175 }
176
177 /* How many 0 bits are there at least significant end of longword. Low
178 * performance, do not call often. */
179 static int
180 free_bits_at_bottom (a)
181 unsigned long a;
182 {
183 /* assume char is 8 bits */
184 if (!a)
185 return sizeof (unsigned long) * 8;
186 if (((long) a) & 1l)
187 return 0;
188 return 1 + free_bits_at_bottom (a >> 1);
189 }
190
191 static int *L_tab, *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab;
192
193 /* We define tables that convert a color value between -256 and 512 into
194 * the R, G and B parts of the pixel. The normal range is 0-255. */
195
196 static long *r_2_pix;
197 static long *g_2_pix;
198 static long *b_2_pix;
199 static long *r_2_pix_alloc;
200 static long *g_2_pix_alloc;
201 static long *b_2_pix_alloc;
202
203
204
205
206 /* --------------------------------------------------------------
207 *
208 * InitColor16Dither --
209 *
210 * To get rid of the multiply and other conversions in color dither, we use a
211 * lookup table.
212 *
213 * Results: None.
214 *
215 * Side effects: The lookup tables are initialized.
216 *
217 * -------------------------------------------------------------- */
218
219 void
220 InitColorDither (thirty2)
221 int thirty2;
222 {
223 extern XImage *ximage;
224 extern unsigned long wpixel[3];
225 /* misuse of the wpixel array for the pixel masks. Note that this
226 * implies that the window is created before this routine is called */
227
228
229 unsigned long red_mask = wpixel[0];
230 unsigned long green_mask = wpixel[1];
231 unsigned long blue_mask = wpixel[2];
232
233 int CR, CB, i;
234
235 if (ximage->bits_per_pixel == 24) /* not necessary in non-packed mode */
236 init_dither_tab ();
237
238 L_tab = (int *) malloc (256 * sizeof (int));
239 Cr_r_tab = (int *) malloc (256 * sizeof (int));
240 Cr_g_tab = (int *) malloc (256 * sizeof (int));
241 Cb_g_tab = (int *) malloc (256 * sizeof (int));
242 Cb_b_tab = (int *) malloc (256 * sizeof (int));
243
244 r_2_pix_alloc = (long *) malloc (768 * sizeof (long));
245 g_2_pix_alloc = (long *) malloc (768 * sizeof (long));
246 b_2_pix_alloc = (long *) malloc (768 * sizeof (long));
247
248 if (L_tab == NULL ||
249 Cr_r_tab == NULL ||
250 Cr_g_tab == NULL ||
251 Cb_g_tab == NULL ||
252 Cb_b_tab == NULL ||
253 r_2_pix_alloc == NULL ||
254 g_2_pix_alloc == NULL ||
255 b_2_pix_alloc == NULL)
256 {
257 fprintf (stderr, "Could not get enough memory in InitColorDither/n");
258 exit (1);
259 }
260 for (i = 0; i < 256; i++)
261 {
262 L_tab[i] = i;
263 if (gammaCorrectFlag)
264 {
265 L_tab[i] = GAMMA_CORRECTION (i);
266 }
267 CB = CR = i;
268
269 if (chromaCorrectFlag)
270 {
271 CB -= 128;
272 CB = CHROMA_CORRECTION128 (CB);
273 CR -= 128;
274 CR = CHROMA_CORRECTION128 (CR);
275 } else
276 {
277 CB -= 128;
278 CR -= 128;
279 }
280 /* was Cr_r_tab[i] = 1.596 * CR; Cr_g_tab[i] = -0.813 * CR;
281 * Cb_g_tab[i] = -0.391 * CB; Cb_b_tab[i] = 2.018 * CB; but they
282 * were just messed up. Then was (_Video Deymstified_): Cr_r_tab[i] =
283 * 1.366 * CR; Cr_g_tab[i] = -0.700 * CR; Cb_g_tab[i] = -0.334 * CB;
284 * Cb_b_tab[i] = 1.732 * CB; but really should be: (from ITU-R
285 * BT.470-2 System B, G and SMPTE 170M ) */
286 Cr_r_tab[i] = (0.419 / 0.299) * CR;
287 Cr_g_tab[i] = -(0.299 / 0.419) * CR;
288 Cb_g_tab[i] = -(0.114 / 0.331) * CB;
289 Cb_b_tab[i] = (0.587 / 0.331) * CB;
290
291 /* though you could argue for: SMPTE 240M Cr_r_tab[i] = (0.445/0.212) *
292 * CR; Cr_g_tab[i] = -(0.212/0.445) * CR; Cb_g_tab[i] = -(0.087/0.384) *
293 * CB; Cb_b_tab[i] = (0.701/0.384) * CB; FCC Cr_r_tab[i] =
294 * (0.421/0.30) * CR; Cr_g_tab[i] = -(0.30/0.421) * CR; Cb_g_tab[i] =
295 * -(0.11/0.331) * CB; Cb_b_tab[i] = (0.59/0.331) * CB; ITU-R BT.709
296 * Cr_r_tab[i] = (0.454/0.2125) * CR; Cr_g_tab[i] = -(0.2125/0.454) *
297 * CR; Cb_g_tab[i] = -(0.0721/0.386) * CB; Cb_b_tab[i] =
298 * (0.7154/0.386) * CB; */
299 }
300
301 /* Set up entries 0-255 in rgb-to-pixel value tables. */
302 for (i = 0; i < 256; i++)
303 {
304 r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set (red_mask));
305 r_2_pix_alloc[i + 256] <<= free_bits_at_bottom (red_mask);
306 g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set (green_mask));
307 g_2_pix_alloc[i + 256] <<= free_bits_at_bottom (green_mask);
308 b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set (blue_mask));
309 b_2_pix_alloc[i + 256] <<= free_bits_at_bottom (blue_mask);
310 /* If we have 16-bit output depth, then we double the value in the top
311 * word. This means that we can write out both pixels in the pixel
312 * doubling mode with one op. It is harmless in the normal case as
313 * storing a 32-bit value through a short pointer will lose the top
314 * bits anyway. A similar optimisation for Alpha for 64 bit has been
315 * prepared for, but is not yet implemented. */
316 if (!thirty2)
317 {
318
319 r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 16;
320 g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 16;
321 b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 16;
322
323 }
324 #ifdef SIXTYFOUR_BIT
325 if (thirty2)
326 {
327
328 r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 32;
329 g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 32;
330 b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 32;
331
332 }
333 #endif
334 }
335
336 /* Spread out the values we have to the rest of the array so that we do
337 * not need to check for overflow. */
338 for (i = 0; i < 256; i++)
339 {
340 r_2_pix_alloc[i] = r_2_pix_alloc[256];
341 r_2_pix_alloc[i + 512] = r_2_pix_alloc[511];
342 g_2_pix_alloc[i] = g_2_pix_alloc[256];
343 g_2_pix_alloc[i + 512] = g_2_pix_alloc[511];
344 b_2_pix_alloc[i] = b_2_pix_alloc[256];
345 b_2_pix_alloc[i + 512] = b_2_pix_alloc[511];
346 }
347
348 r_2_pix = r_2_pix_alloc + 256;
349 g_2_pix = g_2_pix_alloc + 256;
350 b_2_pix = b_2_pix_alloc + 256;
351
352 }
353
354
355
356
357 /* --------------------------------------------------------------
358 *
359 * Color16DitherImage --
360 *
361 * Converts image into 16 bit color.
362 *
363 * Results: None.
364 *
365 * Side effects: None.
366 *
367 * -------------------------------------------------------------- */
368
369 void
370 Color16DitherImage (src, out)
371 unsigned char *src[];
372 unsigned char *out;
373 {
374 unsigned char *lum = src[0];
375 unsigned char *cb = src[1];
376 unsigned char *cr = src[2];
377 int cols;
378 int rows;
379
380 int L, CR, CB;
381 unsigned short *row1, *row2;
382 unsigned char *lum2;
383 int x, y;
384 int cr_r;
385 int cr_g;
386 int cb_g;
387 int cb_b;
388 int cols_2;
389
390 cols = coded_picture_width;
391 rows = coded_picture_height;
392 if (expand)
393 {
394 cols *= 2;
395 rows *= 2;
396 }
397 cols_2 = cols / 2;
398
399 row1 = (unsigned short *) out;
400 row2 = row1 + cols_2 + cols_2;
401 lum2 = lum + cols_2 + cols_2;
402
403 for (y = 0; y < rows; y += 2)
404 {
405 for (x = 0; x < cols_2; x++)
406 {
407 int R, G, B;
408
409 CR = *cr++;
410 CB = *cb++;
411 cr_r = Cr_r_tab[CR];
412 cr_g = Cr_g_tab[CR];
413 cb_g = Cb_g_tab[CB];
414 cb_b = Cb_b_tab[CB];
415
416 L = L_tab[(int) *lum++];
417
418 R = L + cr_r;
419 G = L + cr_g + cb_g;
420 B = L + cb_b;
421
422 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
423
424 #ifdef INTERPOLATE
425 if (x != cols_2 - 1)
426 {
427 CR = (CR + *cr) >> 1;
428 CB = (CB + *cb) >> 1;
429 cr_r = Cr_r_tab[CR];
430 cr_g = Cr_g_tab[CR];
431 cb_g = Cb_g_tab[CB];
432 cb_b = Cb_b_tab[CB];
433 }
434 #endif
435
436 L = L_tab[(int) *lum++];
437
438 R = L + cr_r;
439 G = L + cr_g + cb_g;
440 B = L + cb_b;
441
442 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
443
444 /* Now, do second row. */
445 #ifdef INTERPOLATE
446 if (y != rows - 2)
447 {
448 CR = (CR + *(cr + cols_2 - 1)) >> 1;
449 CB = (CB + *(cb + cols_2 - 1)) >> 1;
450 cr_r = Cr_r_tab[CR];
451 cr_g = Cr_g_tab[CR];
452 cb_g = Cb_g_tab[CB];
453 cb_b = Cb_b_tab[CB];
454 }
455 #endif
456
457 L = L_tab[(int) *lum2++];
458 R = L + cr_r;
459 G = L + cr_g + cb_g;
460 B = L + cb_b;
461
462 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
463
464 L = L_tab[(int) *lum2++];
465 R = L + cr_r;
466 G = L + cr_g + cb_g;
467 B = L + cb_b;
468
469 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
470 }
471 /* These values are at the start of the next line, (due to the ++'s
472 * above),but they need to be at the start of the line after that. */
473 lum += cols_2 + cols_2;
474 lum2 += cols_2 + cols_2;
475 row1 += cols_2 + cols_2;
476 row2 += cols_2 + cols_2;
477 }
478 }
479
480
481
482
483
484 /* --------------------------------------------------------------
485 *
486 * Color32DitherImage --
487 *
488 * Converts image into 32 bit color (or 24-bit non-packed).
489 *
490 * Results: None.
491 *
492 * Side effects: None.
493 *
494 * -------------------------------------------------------------- */
495
496 /* This is a copysoft version of the function above with ints instead of
497 * shorts to cause a 4-byte pixel size */
498
499 void
500 Color32DitherImage (src, out)
501 unsigned char *src[];
502 unsigned char *out;
503 {
504 unsigned char *lum = src[0];
505 unsigned char *cb = src[1];
506 unsigned char *cr = src[2];
507 int cols;
508 int rows;
509
510 int L, CR, CB;
511 unsigned int *row1, *row2;
512 unsigned char *lum2;
513 int x, y;
514 int cr_r;
515 int cr_g;
516 int cb_g;
517 int cb_b;
518 int cols_2;
519
520 cols = coded_picture_width;
521 rows = coded_picture_height;
522 if (expand)
523 {
524 cols *= 2;
525 rows *= 2;
526 }
527 cols_2 = cols / 2;
528
529 row1 = (unsigned int *) out;
530 row2 = row1 + cols_2 + cols_2;
531 lum2 = lum + cols_2 + cols_2;
532 for (y = 0; y < rows; y += 2)
533 {
534 for (x = 0; x < cols_2; x++)
535 {
536 int R, G, B;
537
538 CR = *cr++;
539 CB = *cb++;
540 cr_r = Cr_r_tab[CR];
541 cr_g = Cr_g_tab[CR];
542 cb_g = Cb_g_tab[CB];
543 cb_b = Cb_b_tab[CB];
544
545 L = L_tab[(int) *lum++];
546
547 R = L + cr_r;
548 G = L + cr_g + cb_g;
549 B = L + cb_b;
550
551 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
552
553 #ifdef INTERPOLATE
554 if (x != cols_2 - 1)
555 {
556 CR = (CR + *cr) >> 1;
557 CB = (CB + *cb) >> 1;
558 cr_r = Cr_r_tab[CR];
559 cr_g = Cr_g_tab[CR];
560 cb_g = Cb_g_tab[CB];
561 cb_b = Cb_b_tab[CB];
562 }
563 #endif
564
565 L = L_tab[(int) *lum++];
566
567 R = L + cr_r;
568 G = L + cr_g + cb_g;
569 B = L + cb_b;
570
571 *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
572
573 /* Now, do second row. */
574
575 #ifdef INTERPOLATE
576 if (y != rows - 2)
577 {
578 CR = (CR + *(cr + cols_2 - 1)) >> 1;
579 CB = (CB + *(cb + cols_2 - 1)) >> 1;
580 cr_r = Cr_r_tab[CR];
581 cr_g = Cr_g_tab[CR];
582 cb_g = Cb_g_tab[CB];
583 cb_b = Cb_b_tab[CB];
584 }
585 #endif
586
587 L = L_tab[(int) *lum2++];
588 R = L + cr_r;
589 G = L + cr_g + cb_g;
590 B = L + cb_b;
591
592 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
593
594 L = L_tab[(int) *lum2++];
595 R = L + cr_r;
596 G = L + cr_g + cb_g;
597 B = L + cb_b;
598
599 *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
600 }
601 lum += cols_2 + cols_2;
602 lum2 += cols_2 + cols_2;
603 row1 += cols_2 + cols_2;
604 row2 += cols_2 + cols_2;
605 }
606 }
607
608
609
610
611
612 #endif
613
【来源】