ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

yuv_to_rgb.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // The YUV to RGB conversion code is derived from SDL's YUV overlay code, which
00024 // in turn appears to be derived from mpeg_play. The following copyright
00025 // notices have been included in accordance with the original license. Please
00026 // note that the term "software" in this context only applies to the
00027 // buildLookup() and plotYUV*() functions below.
00028 
00029 // Copyright (c) 1995 The Regents of the University of California.
00030 // All rights reserved.
00031 //
00032 // Permission to use, copy, modify, and distribute this software and its
00033 // documentation for any purpose, without fee, and without written agreement is
00034 // hereby granted, provided that the above copyright notice and the following
00035 // two paragraphs appear in all copies of this software.
00036 //
00037 // IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
00038 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00039 // OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
00040 // CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00041 //
00042 // THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00043 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00044 // AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
00045 // ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
00046 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00047 
00048 // Copyright (c) 1995 Erik Corry
00049 // All rights reserved.
00050 //
00051 // Permission to use, copy, modify, and distribute this software and its
00052 // documentation for any purpose, without fee, and without written agreement is
00053 // hereby granted, provided that the above copyright notice and the following
00054 // two paragraphs appear in all copies of this software.
00055 //
00056 // IN NO EVENT SHALL ERIK CORRY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
00057 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
00058 // THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIK CORRY HAS BEEN ADVISED
00059 // OF THE POSSIBILITY OF SUCH DAMAGE.
00060 //
00061 // ERIK CORRY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
00062 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00063 // PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
00064 // BASIS, AND ERIK CORRY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
00065 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00066 
00067 // Portions of this software Copyright (c) 1995 Brown University.
00068 // All rights reserved.
00069 //
00070 // Permission to use, copy, modify, and distribute this software and its
00071 // documentation for any purpose, without fee, and without written agreement
00072 // is hereby granted, provided that the above copyright notice and the
00073 // following two paragraphs appear in all copies of this software.
00074 //
00075 // IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
00076 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00077 // OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
00078 // UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00079 //
00080 // BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
00081 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00082 // PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
00083 // BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
00084 // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00085 
00086 #include "graphics/surface.h"
00087 #include "graphics/yuv_to_rgb.h"
00088 
00089 namespace Common {
00090 DECLARE_SINGLETON(Graphics::YUVToRGBManager);
00091 }
00092 
00093 namespace Graphics {
00094 
00095 class YUVToRGBLookup {
00096 public:
00097     YUVToRGBLookup(Graphics::PixelFormat format, YUVToRGBManager::LuminanceScale scale);
00098 
00099     Graphics::PixelFormat getFormat() const { return _format; }
00100     YUVToRGBManager::LuminanceScale getScale() const { return _scale; }
00101     const uint32 *getRGBToPix() const { return _rgbToPix; }
00102 
00103 private:
00104     Graphics::PixelFormat _format;
00105     YUVToRGBManager::LuminanceScale _scale;
00106     uint32 _rgbToPix[3 * 768]; // 9216 bytes
00107 };
00108 
00109 YUVToRGBLookup::YUVToRGBLookup(Graphics::PixelFormat format, YUVToRGBManager::LuminanceScale scale) {
00110     _format = format;
00111     _scale = scale;
00112 
00113     uint32 *r_2_pix_alloc = &_rgbToPix[0 * 768];
00114     uint32 *g_2_pix_alloc = &_rgbToPix[1 * 768];
00115     uint32 *b_2_pix_alloc = &_rgbToPix[2 * 768];
00116 
00117     if (scale == YUVToRGBManager::kScaleFull) {
00118         // Set up entries 0-255 in rgb-to-pixel value tables.
00119         for (int i = 0; i < 256; i++) {
00120             r_2_pix_alloc[i + 256] = format.RGBToColor(i, 0, 0);
00121             g_2_pix_alloc[i + 256] = format.RGBToColor(0, i, 0);
00122             b_2_pix_alloc[i + 256] = format.RGBToColor(0, 0, i);
00123         }
00124 
00125         // Spread out the values we have to the rest of the array so that we do
00126         // not need to check for overflow.
00127         for (int i = 0; i < 256; i++) {
00128             r_2_pix_alloc[i] = r_2_pix_alloc[256];
00129             r_2_pix_alloc[i + 512] = r_2_pix_alloc[511];
00130             g_2_pix_alloc[i] = g_2_pix_alloc[256];
00131             g_2_pix_alloc[i + 512] = g_2_pix_alloc[511];
00132             b_2_pix_alloc[i] = b_2_pix_alloc[256];
00133             b_2_pix_alloc[i + 512] = b_2_pix_alloc[511];
00134         }
00135     } else {
00136         // Set up entries 16-235 in rgb-to-pixel value tables
00137         for (int i = 16; i < 236; i++) {
00138             int scaledValue = (i - 16) * 255 / 219;
00139             r_2_pix_alloc[i + 256] = format.RGBToColor(scaledValue, 0, 0);
00140             g_2_pix_alloc[i + 256] = format.RGBToColor(0, scaledValue, 0);
00141             b_2_pix_alloc[i + 256] = format.RGBToColor(0, 0, scaledValue);
00142         }
00143 
00144         // Spread out the values we have to the rest of the array so that we do
00145         // not need to check for overflow. We have to do it here in two steps.
00146         for (int i = 0; i < 256 + 16; i++) {
00147             r_2_pix_alloc[i] = r_2_pix_alloc[256 + 16];
00148             g_2_pix_alloc[i] = g_2_pix_alloc[256 + 16];
00149             b_2_pix_alloc[i] = b_2_pix_alloc[256 + 16];
00150         }
00151 
00152         for (int i = 256 + 236; i < 768; i++) {
00153             r_2_pix_alloc[i] = r_2_pix_alloc[256 + 236 - 1];
00154             g_2_pix_alloc[i] = g_2_pix_alloc[256 + 236 - 1];
00155             b_2_pix_alloc[i] = b_2_pix_alloc[256 + 236 - 1];
00156         }
00157     }
00158 }
00159 
00160 YUVToRGBManager::YUVToRGBManager() {
00161     _lookup = 0;
00162 
00163     int16 *Cr_r_tab = &_colorTab[0 * 256];
00164     int16 *Cr_g_tab = &_colorTab[1 * 256];
00165     int16 *Cb_g_tab = &_colorTab[2 * 256];
00166     int16 *Cb_b_tab = &_colorTab[3 * 256];
00167 
00168     // Generate the tables for the display surface
00169 
00170     for (int i = 0; i < 256; i++) {
00171         // Gamma correction (luminescence table) and chroma correction
00172         // would be done here. See the Berkeley mpeg_play sources.
00173 
00174         int16 CR = (i - 128), CB = CR;
00175         Cr_r_tab[i] = (int16) ( (0.419 / 0.299) * CR) + 0 * 768 + 256;
00176         Cr_g_tab[i] = (int16) (-(0.299 / 0.419) * CR) + 1 * 768 + 256;
00177         Cb_g_tab[i] = (int16) (-(0.114 / 0.331) * CB);
00178         Cb_b_tab[i] = (int16) ( (0.587 / 0.331) * CB) + 2 * 768 + 256;
00179     }
00180 }
00181 
00182 YUVToRGBManager::~YUVToRGBManager() {
00183     delete _lookup;
00184 }
00185 
00186 const YUVToRGBLookup *YUVToRGBManager::getLookup(Graphics::PixelFormat format, YUVToRGBManager::LuminanceScale scale) {
00187     if (_lookup && _lookup->getFormat() == format && _lookup->getScale() == scale)
00188         return _lookup;
00189 
00190     delete _lookup;
00191     _lookup = new YUVToRGBLookup(format, scale);
00192     return _lookup;
00193 }
00194 
00195 #define PUT_PIXEL(s, d) \
00196     L = &rgbToPix[(s)]; \
00197     *((PixelInt *)(d)) = (L[cr_r] | L[crb_g] | L[cb_b])
00198 
00199 template<typename PixelInt>
00200 void convertYUV444ToRGB(byte *dstPtr, int dstPitch, const YUVToRGBLookup *lookup, int16 *colorTab, const byte *ySrc, const byte *uSrc, const byte *vSrc, int yWidth, int yHeight, int yPitch, int uvPitch) {
00201     // Keep the tables in pointers here to avoid a dereference on each pixel
00202     const int16 *Cr_r_tab = colorTab;
00203     const int16 *Cr_g_tab = Cr_r_tab + 256;
00204     const int16 *Cb_g_tab = Cr_g_tab + 256;
00205     const int16 *Cb_b_tab = Cb_g_tab + 256;
00206     const uint32 *rgbToPix = lookup->getRGBToPix();
00207 
00208     for (int h = 0; h < yHeight; h++) {
00209         for (int w = 0; w < yWidth; w++) {
00210             const uint32 *L;
00211 
00212             int16 cr_r  = Cr_r_tab[*vSrc];
00213             int16 crb_g = Cr_g_tab[*vSrc] + Cb_g_tab[*uSrc];
00214             int16 cb_b  = Cb_b_tab[*uSrc];
00215             ++uSrc;
00216             ++vSrc;
00217 
00218             PUT_PIXEL(*ySrc, dstPtr);
00219             ySrc++;
00220             dstPtr += sizeof(PixelInt);
00221         }
00222 
00223         dstPtr += dstPitch - yWidth * sizeof(PixelInt);
00224         ySrc += yPitch - yWidth;
00225         uSrc += uvPitch - yWidth;
00226         vSrc += uvPitch - yWidth;
00227     }
00228 }
00229 
00230 void YUVToRGBManager::convert444(Graphics::Surface *dst, YUVToRGBManager::LuminanceScale scale, const byte *ySrc, const byte *uSrc, const byte *vSrc, int yWidth, int yHeight, int yPitch, int uvPitch) {
00231     // Sanity checks
00232     assert(dst && dst->getPixels());
00233     assert(dst->format.bytesPerPixel == 2 || dst->format.bytesPerPixel == 4);
00234     assert(ySrc && uSrc && vSrc);
00235 
00236     const YUVToRGBLookup *lookup = getLookup(dst->format, scale);
00237 
00238     // Use a templated function to avoid an if check on every pixel
00239     if (dst->format.bytesPerPixel == 2)
00240         convertYUV444ToRGB<uint16>((byte *)dst->getPixels(), dst->pitch, lookup, _colorTab, ySrc, uSrc, vSrc, yWidth, yHeight, yPitch, uvPitch);
00241     else
00242         convertYUV444ToRGB<uint32>((byte *)dst->getPixels(), dst->pitch, lookup, _colorTab, ySrc, uSrc, vSrc, yWidth, yHeight, yPitch, uvPitch);
00243 }
00244 
00245 template<typename PixelInt>
00246 void convertYUV420ToRGB(byte *dstPtr, int dstPitch, const YUVToRGBLookup *lookup, int16 *colorTab, const byte *ySrc, const byte *uSrc, const byte *vSrc, int yWidth, int yHeight, int yPitch, int uvPitch) {
00247     int halfHeight = yHeight >> 1;
00248     int halfWidth = yWidth >> 1;
00249 
00250     // Keep the tables in pointers here to avoid a dereference on each pixel
00251     const int16 *Cr_r_tab = colorTab;
00252     const int16 *Cr_g_tab = Cr_r_tab + 256;
00253     const int16 *Cb_g_tab = Cr_g_tab + 256;
00254     const int16 *Cb_b_tab = Cb_g_tab + 256;
00255     const uint32 *rgbToPix = lookup->getRGBToPix();
00256 
00257     for (int h = 0; h < halfHeight; h++) {
00258         for (int w = 0; w < halfWidth; w++) {
00259             const uint32 *L;
00260 
00261             int16 cr_r  = Cr_r_tab[*vSrc];
00262             int16 crb_g = Cr_g_tab[*vSrc] + Cb_g_tab[*uSrc];
00263             int16 cb_b  = Cb_b_tab[*uSrc];
00264             ++uSrc;
00265             ++vSrc;
00266 
00267             PUT_PIXEL(*ySrc, dstPtr);
00268             PUT_PIXEL(*(ySrc + yPitch), dstPtr + dstPitch);
00269             ySrc++;
00270             dstPtr += sizeof(PixelInt);
00271             PUT_PIXEL(*ySrc, dstPtr);
00272             PUT_PIXEL(*(ySrc + yPitch), dstPtr + dstPitch);
00273             ySrc++;
00274             dstPtr += sizeof(PixelInt);
00275         }
00276 
00277         dstPtr += dstPitch;
00278         ySrc += (yPitch << 1) - yWidth;
00279         uSrc += uvPitch - halfWidth;
00280         vSrc += uvPitch - halfWidth;
00281     }
00282 }
00283 
00284 void YUVToRGBManager::convert420(Graphics::Surface *dst, YUVToRGBManager::LuminanceScale scale, const byte *ySrc, const byte *uSrc, const byte *vSrc, int yWidth, int yHeight, int yPitch, int uvPitch) {
00285     // Sanity checks
00286     assert(dst && dst->getPixels());
00287     assert(dst->format.bytesPerPixel == 2 || dst->format.bytesPerPixel == 4);
00288     assert(ySrc && uSrc && vSrc);
00289     assert((yWidth & 1) == 0);
00290     assert((yHeight & 1) == 0);
00291 
00292     const YUVToRGBLookup *lookup = getLookup(dst->format, scale);
00293 
00294     // Use a templated function to avoid an if check on every pixel
00295     if (dst->format.bytesPerPixel == 2)
00296         convertYUV420ToRGB<uint16>((byte *)dst->getPixels(), dst->pitch, lookup, _colorTab, ySrc, uSrc, vSrc, yWidth, yHeight, yPitch, uvPitch);
00297     else
00298         convertYUV420ToRGB<uint32>((byte *)dst->getPixels(), dst->pitch, lookup, _colorTab, ySrc, uSrc, vSrc, yWidth, yHeight, yPitch, uvPitch);
00299 }
00300 
00301 #define READ_QUAD(ptr, prefix) \
00302     byte prefix##A = ptr[index]; \
00303     byte prefix##B = ptr[index + 1]; \
00304     byte prefix##C = ptr[index + uvPitch]; \
00305     byte prefix##D = ptr[index + uvPitch + 1]
00306 
00307 #define DO_INTERPOLATION(out) \
00308     out = (out##A * (4 - xDiff) * (4 - yDiff) + out##B * xDiff * (4 - yDiff) + \
00309             out##C * yDiff * (4 - xDiff) + out##D * xDiff * yDiff) >> 4
00310 
00311 #define DO_YUV410_PIXEL() \
00312     DO_INTERPOLATION(u); \
00313     DO_INTERPOLATION(v); \
00314     \
00315     cr_r  = Cr_r_tab[v]; \
00316     crb_g = Cr_g_tab[v] + Cb_g_tab[u]; \
00317     cb_b  = Cb_b_tab[u]; \
00318     \
00319     PUT_PIXEL(*ySrc, dstPtr); \
00320     dstPtr += sizeof(PixelInt); \
00321     \
00322     ySrc++; \
00323     xDiff++
00324 
00325 template<typename PixelInt>
00326 void convertYUV410ToRGB(byte *dstPtr, int dstPitch, const YUVToRGBLookup *lookup, int16 *colorTab, const byte *ySrc, const byte *uSrc, const byte *vSrc, int yWidth, int yHeight, int yPitch, int uvPitch) {
00327     // Keep the tables in pointers here to avoid a dereference on each pixel
00328     const int16 *Cr_r_tab = colorTab;
00329     const int16 *Cr_g_tab = Cr_r_tab + 256;
00330     const int16 *Cb_g_tab = Cr_g_tab + 256;
00331     const int16 *Cb_b_tab = Cb_g_tab + 256;
00332     const uint32 *rgbToPix = lookup->getRGBToPix();
00333 
00334     int quarterWidth = yWidth >> 2;
00335 
00336     for (int y = 0; y < yHeight; y++) {
00337         for (int x = 0; x < quarterWidth; x++) {
00338             // Perform bilinear interpolation on the the chroma values
00339             // Based on the algorithm found here: http://tech-algorithm.com/articles/bilinear-image-scaling/
00340             // Feel free to optimize further
00341             int targetY = y >> 2;
00342             int xDiff = 0;
00343             int yDiff = y & 3;
00344             int index = targetY * uvPitch + x;
00345 
00346             // Declare some variables for the following macros
00347             byte u, v;
00348             int16 cr_r, crb_g, cb_b;
00349             const uint32 *L;
00350 
00351             READ_QUAD(uSrc, u);
00352             READ_QUAD(vSrc, v);
00353 
00354             DO_YUV410_PIXEL();
00355             DO_YUV410_PIXEL();
00356             DO_YUV410_PIXEL();
00357             DO_YUV410_PIXEL();
00358         }
00359 
00360         dstPtr += dstPitch - yWidth * sizeof(PixelInt);
00361         ySrc += yPitch - yWidth;
00362     }
00363 }
00364 
00365 #undef READ_QUAD
00366 #undef DO_INTERPOLATION
00367 #undef DO_YUV410_PIXEL
00368 
00369 void YUVToRGBManager::convert410(Graphics::Surface *dst, YUVToRGBManager::LuminanceScale scale, const byte *ySrc, const byte *uSrc, const byte *vSrc, int yWidth, int yHeight, int yPitch, int uvPitch) {
00370     // Sanity checks
00371     assert(dst && dst->getPixels());
00372     assert(dst->format.bytesPerPixel == 2 || dst->format.bytesPerPixel == 4);
00373     assert(ySrc && uSrc && vSrc);
00374     assert((yWidth & 3) == 0);
00375     assert((yHeight & 3) == 0);
00376 
00377     const YUVToRGBLookup *lookup = getLookup(dst->format, scale);
00378 
00379     // Use a templated function to avoid an if check on every pixel
00380     if (dst->format.bytesPerPixel == 2)
00381         convertYUV410ToRGB<uint16>((byte *)dst->getPixels(), dst->pitch, lookup, _colorTab, ySrc, uSrc, vSrc, yWidth, yHeight, yPitch, uvPitch);
00382     else
00383         convertYUV410ToRGB<uint32>((byte *)dst->getPixels(), dst->pitch, lookup, _colorTab, ySrc, uSrc, vSrc, yWidth, yHeight, yPitch, uvPitch);
00384 }
00385 
00386 } // End of namespace Graphics


Generated on Sat Mar 16 2019 05:02:03 for ResidualVM by doxygen 1.7.1
curved edge   curved edge