xoreos  0.0.5
yuv_to_rgb.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 // The YUV to RGB conversion code is derived from ScummVM's YUV conversion code,
26 // which is derived from SDL's YUV overlay code, which in turn appears to be
27 // derived from mpeg_play. The following copyright notices have been included
28 // in accordance with the original license. Please note that the term "software"
29 // in this context only applies to the YUVToRGBLookup constructor, YUVToRGBManager
30 // constructor and convert420() functions below.
31 
32 // Copyright (c) 1995 The Regents of the University of California.
33 // All rights reserved.
34 //
35 // Permission to use, copy, modify, and distribute this software and its
36 // documentation for any purpose, without fee, and without written agreement is
37 // hereby granted, provided that the above copyright notice and the following
38 // two paragraphs appear in all copies of this software.
39 //
40 // IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
41 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
42 // OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
43 // CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 //
45 // THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
46 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
47 // AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
48 // ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
49 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
50 
51 // Copyright (c) 1995 Erik Corry
52 // All rights reserved.
53 //
54 // Permission to use, copy, modify, and distribute this software and its
55 // documentation for any purpose, without fee, and without written agreement is
56 // hereby granted, provided that the above copyright notice and the following
57 // two paragraphs appear in all copies of this software.
58 //
59 // IN NO EVENT SHALL ERIK CORRY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
60 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
61 // THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIK CORRY HAS BEEN ADVISED
62 // OF THE POSSIBILITY OF SUCH DAMAGE.
63 //
64 // ERIK CORRY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
65 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
66 // PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
67 // BASIS, AND ERIK CORRY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
68 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
69 
70 // Portions of this software Copyright (c) 1995 Brown University.
71 // All rights reserved.
72 //
73 // Permission to use, copy, modify, and distribute this software and its
74 // documentation for any purpose, without fee, and without written agreement
75 // is hereby granted, provided that the above copyright notice and the
76 // following two paragraphs appear in all copies of this software.
77 //
78 // IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
79 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
80 // OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
81 // UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 //
83 // BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
84 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
85 // PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
86 // BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
87 // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
88 
89 #include "src/common/error.h"
90 #include "src/common/singleton.h"
91 #include "src/common/util.h"
92 
94 
96 
97 namespace Graphics {
98 
100 public:
102 
104  const byte *getRGBToPix() const { return _rgbToPix; }
105 
106 private:
108  byte _rgbToPix[3 * 768];
109 };
110 
112  _scale = scale;
113 
114  byte *r_2_pix_alloc = &_rgbToPix[0 * 768];
115  byte *g_2_pix_alloc = &_rgbToPix[1 * 768];
116  byte *b_2_pix_alloc = &_rgbToPix[2 * 768];
117 
118  if (scale == YUVToRGBManager::kScaleFull) {
119  // Set up entries 0-255 in rgb-to-pixel value tables.
120  for (int i = 0; i < 256; i++) {
121  r_2_pix_alloc[i + 256] = i;
122  g_2_pix_alloc[i + 256] = i;
123  b_2_pix_alloc[i + 256] = i;
124  }
125 
126  // Spread out the values we have to the rest of the array so that we do
127  // not need to check for overflow.
128  for (int i = 0; i < 256; i++) {
129  r_2_pix_alloc[i] = r_2_pix_alloc[256];
130  r_2_pix_alloc[i + 512] = r_2_pix_alloc[511];
131  g_2_pix_alloc[i] = g_2_pix_alloc[256];
132  g_2_pix_alloc[i + 512] = g_2_pix_alloc[511];
133  b_2_pix_alloc[i] = b_2_pix_alloc[256];
134  b_2_pix_alloc[i + 512] = b_2_pix_alloc[511];
135  }
136  } else {
137  // Set up entries 0-255 in rgb-to-pixel value tables.
138  for (int i = 16; i < 236; i++) {
139  int scaledValue = (i - 16) * 255 / 219;
140  r_2_pix_alloc[i + 256] = scaledValue;
141  g_2_pix_alloc[i + 256] = scaledValue;
142  b_2_pix_alloc[i + 256] = scaledValue;
143  }
144 
145  // Spread out the values we have to the rest of the array so that we do
146  // not need to check for overflow. We have to do it here in two steps.
147  for (int i = 0; i < 256 + 16; i++) {
148  r_2_pix_alloc[i] = r_2_pix_alloc[256 + 16];
149  g_2_pix_alloc[i] = g_2_pix_alloc[256 + 16];
150  b_2_pix_alloc[i] = b_2_pix_alloc[256 + 16];
151  }
152 
153  for (int i = 256 + 236; i < 768; i++) {
154  r_2_pix_alloc[i] = r_2_pix_alloc[256 + 236 - 1];
155  g_2_pix_alloc[i] = g_2_pix_alloc[256 + 236 - 1];
156  b_2_pix_alloc[i] = b_2_pix_alloc[256 + 236 - 1];
157  }
158  }
159 }
160 
162  int16 *Cr_r_tab = &_colorTab[0 * 256];
163  int16 *Cr_g_tab = &_colorTab[1 * 256];
164  int16 *Cb_g_tab = &_colorTab[2 * 256];
165  int16 *Cb_b_tab = &_colorTab[3 * 256];
166 
167  // Generate the tables for the display surface
168 
169  for (int i = 0; i < 256; i++) {
170  // Gamma correction (luminescence table) and chroma correction
171  // would be done here. See the Berkeley mpeg_play sources.
172 
173  int16 CR = (i - 128), CB = CR;
174  Cr_r_tab[i] = (int16) ( (0.419 / 0.299) * CR) + 0 * 768 + 256;
175  Cr_g_tab[i] = (int16) (-(0.299 / 0.419) * CR) + 1 * 768 + 256;
176  Cb_g_tab[i] = (int16) (-(0.114 / 0.331) * CB);
177  Cb_b_tab[i] = (int16) ( (0.587 / 0.331) * CB) + 2 * 768 + 256;
178  }
179 }
180 
182 }
183 
185  if (_lookup && _lookup->getScale() == scale)
186  return _lookup.get();
187 
188  _lookup.reset(new YUVToRGBLookup(scale));
189  return _lookup.get();
190 }
191 
192 #define PUT_PIXEL(s, a, d) \
193  L = &rgbToPix[(s)]; \
194  *((d)) = L[cb_b]; \
195  *((d) + 1) = L[crb_g]; \
196  *((d) + 2) = L[cr_r]; \
197  *((d) + 3) = (a)
198 
199 void YUVToRGBManager::convert420(LuminanceScale scale, byte *dst, int dstPitch, const byte *ySrc, const byte *uSrc, const byte *vSrc, const byte *aSrc, int yWidth, int yHeight, int yPitch, int uvPitch) {
200  const YUVToRGBLookup *lookup = YUVToRGBMan.getLookup(scale);
201  const byte *rgbToPix = lookup->getRGBToPix();
202 
203  int halfHeight = yHeight >> 1;
204  int halfWidth = yWidth >> 1;
205 
206  dst += dstPitch * (yHeight - 2);
207 
208  for (int h = 0; h < halfHeight; h++) {
209  for (int w = 0; w < halfWidth; w++) {
210  const byte *L;
211 
212  int16 cr_r = _colorTab[*vSrc + 0 * 256];
213  int16 crb_g = _colorTab[*vSrc + 1 * 256] + _colorTab[*uSrc + 2 * 256];
214  int16 cb_b = _colorTab[*uSrc + 3 * 256];
215  uSrc++;
216  vSrc++;
217 
218  PUT_PIXEL(*ySrc, *aSrc, dst + dstPitch);
219  PUT_PIXEL(*(ySrc + yPitch), *(aSrc + yPitch), dst);
220  ySrc++;
221  aSrc++;
222  dst += 4;
223  PUT_PIXEL(*ySrc, *aSrc, dst + dstPitch);
224  PUT_PIXEL(*(ySrc + yPitch), *(aSrc + yPitch), dst);
225  ySrc++;
226  aSrc++;
227  dst += 4;
228  }
229 
230  dst -= yWidth * 4 + dstPitch * 2;
231  ySrc += (yPitch << 1) - yWidth;
232  aSrc += (yPitch << 1) - yWidth;
233  uSrc += uvPitch - halfWidth;
234  vSrc += uvPitch - halfWidth;
235  }
236 }
237 
238 void YUVToRGBManager::convert420(LuminanceScale scale, byte *dst, int dstPitch, const byte *ySrc, const byte *uSrc, const byte *vSrc, int yWidth, int yHeight, int yPitch, int uvPitch) {
239  const YUVToRGBLookup *lookup = YUVToRGBMan.getLookup(scale);
240  const byte *rgbToPix = lookup->getRGBToPix();
241 
242  int halfHeight = yHeight >> 1;
243  int halfWidth = yWidth >> 1;
244 
245  dst += dstPitch * (yHeight - 2);
246 
247  for (int h = 0; h < halfHeight; h++) {
248  for (int w = 0; w < halfWidth; w++) {
249  const byte *L;
250 
251  int16 cr_r = _colorTab[*vSrc + 0 * 256];
252  int16 crb_g = _colorTab[*vSrc + 1 * 256] + _colorTab[*uSrc + 2 * 256];
253  int16 cb_b = _colorTab[*uSrc + 3 * 256];
254  uSrc++;
255  vSrc++;
256 
257  PUT_PIXEL(*ySrc, 0xFF, dst + dstPitch);
258  PUT_PIXEL(*(ySrc + yPitch), 0xFF, dst);
259  ySrc++;
260  dst += 4;
261  PUT_PIXEL(*ySrc, 0xFF, dst + dstPitch);
262  PUT_PIXEL(*(ySrc + yPitch), 0xFF, dst);
263  ySrc++;
264  dst += 4;
265  }
266 
267  dst -= yWidth * 4 + dstPitch * 2;
268  ySrc += (yPitch << 1) - yWidth;
269  uSrc += uvPitch - halfWidth;
270  vSrc += uvPitch - halfWidth;
271  }
272 }
273 
274 } // End of namespace Graphics
Class and macro for implementing singletons.
LuminanceScale
The scale of the luminance values.
Definition: yuv_to_rgb.h:40
void convert420(LuminanceScale scale, byte *dst, int dstPitch, const byte *ySrc, const byte *uSrc, const byte *vSrc, int yWidth, int yHeight, int yPitch, int uvPitch)
Convert a YUV420 image to an RGBA surface.
Definition: yuv_to_rgb.cpp:238
YUVToRGBLookup(YUVToRGBManager::LuminanceScale scale)
Definition: yuv_to_rgb.cpp:111
YUVToRGBManager::LuminanceScale getScale() const
Definition: yuv_to_rgb.cpp:103
int16 _colorTab[4 *256]
Definition: yuv_to_rgb.h:86
int16_t int16
Definition: types.h:201
Basic exceptions to throw.
Utility templates and functions.
#define PUT_PIXEL(s, a, d)
Definition: yuv_to_rgb.cpp:192
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
Common::ScopedPtr< YUVToRGBLookup > _lookup
Definition: yuv_to_rgb.h:85
#define YUVToRGBMan
Definition: yuv_to_rgb.h:91
Efficient YUV to RGB conversion.
const YUVToRGBLookup * getLookup(LuminanceScale scale)
Definition: yuv_to_rgb.cpp:184
YUVToRGBManager::LuminanceScale _scale
Definition: yuv_to_rgb.cpp:107
const byte * getRGBToPix() const
Definition: yuv_to_rgb.cpp:104
uint8 byte
Definition: types.h:209