| 1 | /*  CCVT_C2: Convert an image from yuv colourspace to rgb 
 | 
|---|
| 2 |     Copyright (C) 2001 Tony Hague <no.email@noemail.com>
 | 
|---|
| 3 | 
 | 
|---|
| 4 |     
 | 
|---|
| 5 |     This program is free software; you can redistribute it and/or modify
 | 
|---|
| 6 |     it under the terms of the GNU General Public License as published by
 | 
|---|
| 7 |     the Free Software Foundation; either version 2 of the License, or
 | 
|---|
| 8 |     (at your option) any later version.
 | 
|---|
| 9 | 
 | 
|---|
| 10 |     This program is distributed in the hope that it will be useful,
 | 
|---|
| 11 |     but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 12 |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 13 |     GNU General Public License for more details.
 | 
|---|
| 14 | 
 | 
|---|
| 15 |     You should have received a copy of the GNU General Public License
 | 
|---|
| 16 |     along with this program; if not, write to the Free Software
 | 
|---|
| 17 |     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 | 
|---|
| 18 | 
 | 
|---|
| 19 |     For questions, remarks, patches, etc. for this program, the author can be
 | 
|---|
| 20 |     reached at nemosoft@smcc.demon.nl.
 | 
|---|
| 21 | */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include "ccvt.h"
 | 
|---|
| 24 | #include "ccvt_types.h"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | /* by suitable definition of PIXTYPE, can do yuv to rgb or bgr, with or
 | 
|---|
| 27 | without word alignment */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /* This doesn't exactly earn a prize in a programming beauty contest. */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #define WHOLE_FUNC2RGB(type)                    \
 | 
|---|
| 32 |         const unsigned char *y1, *y2, *u, *v;   \
 | 
|---|
| 33 |         PIXTYPE_##type *l1, *l2;                \
 | 
|---|
| 34 |         int r, g, b, cr, cg, cb, yp, j, i;      \
 | 
|---|
| 35 |                                                 \
 | 
|---|
| 36 |         if ((width & 1) || (height & 1))        \
 | 
|---|
| 37 |                 return;                         \
 | 
|---|
| 38 |                                                 \
 | 
|---|
| 39 |         l1 = (PIXTYPE_##type *)dst;             \
 | 
|---|
| 40 |         l2 = l1 + width;                        \
 | 
|---|
| 41 |         y1 = (unsigned char *)src;              \
 | 
|---|
| 42 |         y2 = y1 + width;                        \
 | 
|---|
| 43 |         u = (unsigned char *)src + width * height;              \
 | 
|---|
| 44 |         v = u + (width * height) / 4;           \
 | 
|---|
| 45 |         j = height / 2;                         \
 | 
|---|
| 46 |         while (j--) {                           \
 | 
|---|
| 47 |                 i = width / 2;                  \
 | 
|---|
| 48 |                 while (i--) {                   \
 | 
|---|
| 49 |                         /* Since U & V are valid for 4 pixels, repeat code 4    \
 | 
|---|
| 50 |                            times for different Y */                             \
 | 
|---|
| 51 |                         cb = ((*u-128) * 454)>>8;                               \
 | 
|---|
| 52 |                         cr = ((*v-128) * 359)>>8;                               \
 | 
|---|
| 53 |                         cg = ((*v-128) * 183 + (*u-128) * 88)>>8;               \
 | 
|---|
| 54 |                                                 \
 | 
|---|
| 55 |                         yp = *(y1++);           \
 | 
|---|
| 56 |                         r = yp + cr;            \
 | 
|---|
| 57 |                         b = yp + cb;            \
 | 
|---|
| 58 |                         g = yp - cg;            \
 | 
|---|
| 59 |                         SAT(r);                 \
 | 
|---|
| 60 |                         SAT(g);                 \
 | 
|---|
| 61 |                         SAT(b);                 \
 | 
|---|
| 62 |                         l1->b = b;              \
 | 
|---|
| 63 |                         l1->g = g;              \
 | 
|---|
| 64 |                         l1->r = r;              \
 | 
|---|
| 65 |                         l1++;                   \
 | 
|---|
| 66 |                                                 \
 | 
|---|
| 67 |                         yp = *(y1++);           \
 | 
|---|
| 68 |                         r = yp + cr;            \
 | 
|---|
| 69 |                         b = yp + cb;            \
 | 
|---|
| 70 |                         g = yp - cg;            \
 | 
|---|
| 71 |                         SAT(r);                 \
 | 
|---|
| 72 |                         SAT(g);                 \
 | 
|---|
| 73 |                         SAT(b);                 \
 | 
|---|
| 74 |                         l1->b = b;              \
 | 
|---|
| 75 |                         l1->g = g;              \
 | 
|---|
| 76 |                         l1->r = r;              \
 | 
|---|
| 77 |                         l1++;                   \
 | 
|---|
| 78 |                                                 \
 | 
|---|
| 79 |                         yp = *(y2++);           \
 | 
|---|
| 80 |                         r = yp + cr;            \
 | 
|---|
| 81 |                         b = yp + cb;            \
 | 
|---|
| 82 |                         g = yp - cg;            \
 | 
|---|
| 83 |                         SAT(r);                 \
 | 
|---|
| 84 |                         SAT(g);                 \
 | 
|---|
| 85 |                         SAT(b);                 \
 | 
|---|
| 86 |                         l2->b = b;              \
 | 
|---|
| 87 |                         l2->g = g;              \
 | 
|---|
| 88 |                         l2->r = r;              \
 | 
|---|
| 89 |                         l2++;                   \
 | 
|---|
| 90 |                                                 \
 | 
|---|
| 91 |                         yp = *(y2++);           \
 | 
|---|
| 92 |                         r = yp + cr;            \
 | 
|---|
| 93 |                         b = yp + cb;            \
 | 
|---|
| 94 |                         g = yp - cg;            \
 | 
|---|
| 95 |                         SAT(r);                 \
 | 
|---|
| 96 |                         SAT(g);                 \
 | 
|---|
| 97 |                         SAT(b);                 \
 | 
|---|
| 98 |                         l2->b = b;              \
 | 
|---|
| 99 |                         l2->g = g;              \
 | 
|---|
| 100 |                         l2->r = r;              \
 | 
|---|
| 101 |                         l2++;                   \
 | 
|---|
| 102 |                                                 \
 | 
|---|
| 103 |                         u++;                    \
 | 
|---|
| 104 |                         v++;                    \
 | 
|---|
| 105 |                 }                               \
 | 
|---|
| 106 |                 y1 = y2;                        \
 | 
|---|
| 107 |                 y2 += width;                    \
 | 
|---|
| 108 |                 l1 = l2;                        \
 | 
|---|
| 109 |                 l2 += width;                    \
 | 
|---|
| 110 |         }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | 
 | 
|---|
| 113 | 
 | 
|---|
| 114 | 
 | 
|---|
| 115 | void ccvt_420p_bgr32(int width, int height, const void *src, void *dst)
 | 
|---|
| 116 | {
 | 
|---|
| 117 |         WHOLE_FUNC2RGB(bgr32)
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 120 | void ccvt_420p_bgr24(int width, int height, const void *src, void *dst)
 | 
|---|
| 121 | {
 | 
|---|
| 122 |         WHOLE_FUNC2RGB(bgr24)
 | 
|---|
| 123 | }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | void ccvt_420p_rgb32(int width, int height, const void *src, void *dst)
 | 
|---|
| 126 | {
 | 
|---|
| 127 |         WHOLE_FUNC2RGB(rgb32)
 | 
|---|
| 128 | }
 | 
|---|
| 129 | 
 | 
|---|
| 130 | void ccvt_420p_rgb24(int width, int height, const void *src, void *dst)
 | 
|---|
| 131 | {
 | 
|---|
| 132 |         WHOLE_FUNC2RGB(rgb24)
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|