Debian Codegraph @

converter/ppm/ppmtompeg/rgbtoycc.c

raw symbols SCIP
1 /*===========================================================================*
2 * rgbtoycc.c *
3 * *
4 * Procedures to convert from RGB space to YUV space *
5 * *
6 * EXPORTED PROCEDURES: *
7 * PNMtoYUV *
8 * PPMtoYUV *
9 * *
10 *===========================================================================*/
11
12 /*
13 * Copyright (c) 1995 The Regents of the University of California.
14 * All rights reserved.
15 *
16 * Permission to use, copy, modify, and distribute this software and its
17 * documentation for any purpose, without fee, and without written agreement is
18 * hereby granted, provided that the above copyright notice and the following
19 * two paragraphs appear in all copies of this software.
20 *
21 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
22 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
23 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
24 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
29 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
30 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
31 */
32
33 /*==============*
34 * HEADER FILES *
35 *==============*/
36
37 #include "pnm.h"
38 #include "all.h"
39 #include "frame.h"
40 #include "rgbtoycc.h"
41
42
43 static float *mult299, *mult587, *mult114, *mult16874, *mult33126,
44 *mult5, *mult41869, *mult08131; /* malloc'ed */
45 /* These are tables we use for fast arithmetic */
46 static pixval table_maxval = 0;
47 /* The maxval used to compute the above arrays. Zero means
48 the above arrays don't exist yet
49 */
50
51 static void
52 compute_mult_tables(const pixval maxval) {
53
54 /* For speed, we do the arithmetic with eight tables that reduce a
55 bunch of multiplications and divisions to a simple table lookup.
56
57 Because a large maxval could require a significant amount of
58 table space, we allocate the space dynamically.
59
60 If we had to compute the tables for every frame, it wouldn't be
61 fast at all, but since all the frames normally have the same
62 maxval, we only need to compute them once. But just in case,
63 we check each frame to see if it has a different maxval and
64 recompute the tables if so.
65 */
66
67 if (table_maxval != maxval) {
68 /* We need to compute or re-compute the multiplication tables */
69 if (table_maxval != 0) {
70 free(mult299); free(mult587); free(mult114); free(mult16874);
71 free(mult33126); free(mult5); free(mult41869); free(mult08131);
72 }
73 table_maxval = maxval;
74
75 mult299 = malloc((table_maxval+1)*sizeof(float));
76 mult587 = malloc((table_maxval+1)*sizeof(float));
77 mult114 = malloc((table_maxval+1)*sizeof(float));
78 mult16874 = malloc((table_maxval+1)*sizeof(float));
79 mult33126 = malloc((table_maxval+1)*sizeof(float));
80 mult5 = malloc((table_maxval+1)*sizeof(float));
81 mult41869 = malloc((table_maxval+1)*sizeof(float));
82 mult08131 = malloc((table_maxval+1)*sizeof(float));
83
84 if (mult299 == NULL || mult587 == NULL || mult114 == NULL ||
85 mult16874 == NULL || mult33126 == NULL || mult5 == NULL ||
86 mult41869 == NULL || mult08131 == NULL)
87 pm_error("Unable to allocate storage for arithmetic tables. "
88 "We need %d bytes, which is the maxval of the input "
89 "image, plus 1, "
90 "times the storage size of a floating point value.",
91 (unsigned)(8 * (table_maxval+1)*sizeof(float)));
92
93 {
94 int index;
95
96 for (index = 0; index <= table_maxval; index++ ) {
97 mult299[index] = index*0.29900 * 255 / table_maxval;
98 mult587[index] = index*0.58700 * 255 / table_maxval;
99 mult114[index] = index*0.11400 * 255 / table_maxval;
100 mult16874[index] = -0.16874*index * 255 / table_maxval;
101 mult33126[index] = -0.33126*index * 255 / table_maxval;
102 mult5[index] = index*0.50000 * 255 / table_maxval;
103 mult41869[index] = -0.41869*index * 255 / table_maxval;
104 mult08131[index] = -0.08131*index * 255 / table_maxval;
105 }
106 }
107 }
108 }
109
110
111
112 /*=====================*
113 * EXPORTED PROCEDURES *
114 *=====================*/
115
116
117 void
118 PNMtoYUV(MpegFrame * const frameP,
119 xel ** const xels,
120 unsigned int const cols,
121 unsigned int const rows,
122 xelval const maxval) {
123 /*----------------------------------------------------------------------------
124 Set the raster of the MPEG frame *frameP from the libnetpbm input
125 'xels'. Note that the raster information in a MpegFrame is in YUV
126 form.
127 -----------------------------------------------------------------------------*/
128 int x, y;
129 uint8 *dy0, *dy1;
130 uint8 *dcr, *dcb;
131 pixel *src0, *src1;
132
133 compute_mult_tables(maxval); /* This sets up mult299[], etc. */
134
135 Frame_AllocYCC(frameP);
136
137 /*
138 * okay. Now, convert everything into YCbCr space. (the specific
139 * numbers come from the JPEG source, jccolor.c) The conversion
140 * equations to be implemented are therefore
141 *
142 * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
143 * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B
144 * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B
145 *
146 * With Y, Cb, and Cr then normalized to the range 0 - 255.
147 */
148
149 for (y = 0; y < rows; y += 2) {
150 for (x = 0, src0 = xels[y], src1 = xels[y + 1],
151 dy0 = frameP->orig_y[y], dy1 = frameP->orig_y[y + 1],
152 dcr = frameP->orig_cr[y >> 1], dcb = frameP->orig_cb[y >> 1];
153 x < cols;
154 x += 2, dy0 += 2, dy1 += 2, dcr++,
155 dcb++, src0 += 2, src1 += 2) {
156
157 *dy0 = (mult299[PPM_GETR(*src0)] +
158 mult587[PPM_GETG(*src0)] +
159 mult114[PPM_GETB(*src0)]);
160
161 *dy1 = (mult299[PPM_GETR(*src1)] +
162 mult587[PPM_GETG(*src1)] +
163 mult114[PPM_GETB(*src1)]);
164
165 dy0[1] = (mult299[PPM_GETR(src0[1])] +
166 mult587[PPM_GETG(src0[1])] +
167 mult114[PPM_GETB(src0[1])]);
168
169 dy1[1] = (mult299[PPM_GETR(src1[1])] +
170 mult587[PPM_GETG(src1[1])] +
171 mult114[PPM_GETB(src1[1])]);
172
173 *dcb = ((mult16874[PPM_GETR(*src0)] +
174 mult33126[PPM_GETG(*src0)] +
175 mult5[PPM_GETB(*src0)] +
176 mult16874[PPM_GETR(*src1)] +
177 mult33126[PPM_GETG(*src1)] +
178 mult5[PPM_GETB(*src1)] +
179 mult16874[PPM_GETR(src0[1])] +
180 mult33126[PPM_GETG(src0[1])] +
181 mult5[PPM_GETB(src0[1])] +
182 mult16874[PPM_GETR(src1[1])] +
183 mult33126[PPM_GETG(src1[1])] +
184 mult5[PPM_GETB(src1[1])]) / 4) + 128;
185
186 *dcr = ((mult5[PPM_GETR(*src0)] +
187 mult41869[PPM_GETG(*src0)] +
188 mult08131[PPM_GETB(*src0)] +
189 mult5[PPM_GETR(*src1)] +
190 mult41869[PPM_GETG(*src1)] +
191 mult08131[PPM_GETB(*src1)] +
192 mult5[PPM_GETR(src0[1])] +
193 mult41869[PPM_GETG(src0[1])] +
194 mult08131[PPM_GETB(src0[1])] +
195 mult5[PPM_GETR(src1[1])] +
196 mult41869[PPM_GETG(src1[1])] +
197 mult08131[PPM_GETB(src1[1])]) / 4) + 128;
198
199 DBG_PRINT(("%3d,%3d: (%3d,%3d,%3d) --> (%3d,%3d,%3d)\n",
200 x, y,
201 PPM_GETR(*src0), PPM_GETG(*src0), PPM_GETB(*src0),
202 *dy0, *dcb, *dcr));
203 }
204 }
205 }
206
207
208