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

conversion.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 #include "graphics/conversion.h"
00024 #include "graphics/pixelformat.h"
00025 
00026 #include "common/endian.h"
00027 
00028 namespace Graphics {
00029 
00030 // TODO: YUV to RGB conversion function
00031 
00032 namespace {
00033 
00034 template<typename SrcColor, typename DstColor, bool backward>
00035 inline void crossBlitLogic(byte *dst, const byte *src, const uint w, const uint h,
00036                            const PixelFormat &srcFmt, const PixelFormat &dstFmt,
00037                            const uint srcDelta, const uint dstDelta) {
00038     for (uint y = 0; y < h; ++y) {
00039         for (uint x = 0; x < w; ++x) {
00040             const uint32 color = *(const SrcColor *)src;
00041             byte a, r, g, b;
00042             srcFmt.colorToARGB(color, a, r, g, b);
00043             *(DstColor *)dst = dstFmt.ARGBToColor(a, r, g, b);
00044 
00045             if (backward) {
00046                 src -= sizeof(SrcColor);
00047                 dst -= sizeof(DstColor);
00048             } else {
00049                 src += sizeof(SrcColor);
00050                 dst += sizeof(DstColor);
00051             }
00052         }
00053 
00054         if (backward) {
00055             src -= srcDelta;
00056             dst -= dstDelta;
00057         } else {
00058             src += srcDelta;
00059             dst += dstDelta;
00060         }
00061     }
00062 }
00063 
00064 template<typename DstColor, bool backward>
00065 inline void crossBlitLogic3BppSource(byte *dst, const byte *src, const uint w, const uint h,
00066                                      const PixelFormat &srcFmt, const PixelFormat &dstFmt,
00067                                      const uint srcDelta, const uint dstDelta) {
00068     uint32 color;
00069     byte r, g, b, a;
00070     uint8 *col = (uint8 *)&color;
00071 #ifdef SCUMM_BIG_ENDIAN
00072     col++;
00073 #endif
00074     for (uint y = 0; y < h; ++y) {
00075         for (uint x = 0; x < w; ++x) {
00076             memcpy(col, src, 3);
00077             srcFmt.colorToARGB(color, a, r, g, b);
00078             *(DstColor *)dst = dstFmt.ARGBToColor(a, r, g, b);
00079 
00080             if (backward) {
00081                 src -= 3;
00082                 dst -= sizeof(DstColor);
00083             } else {
00084                 src += 3;
00085                 dst += sizeof(DstColor);
00086             }
00087         }
00088 
00089         if (backward) {
00090             src -= srcDelta;
00091             dst -= dstDelta;
00092         } else {
00093             src += srcDelta;
00094             dst += dstDelta;
00095         }
00096     }
00097 }
00098 
00099 } // End of anonymous namespace
00100 
00101 // Function to blit a rect from one color format to another
00102 bool crossBlit(byte *dst, const byte *src,
00103                const uint dstPitch, const uint srcPitch,
00104                const uint w, const uint h,
00105                const Graphics::PixelFormat &dstFmt, const Graphics::PixelFormat &srcFmt) {
00106     // Error out if conversion is impossible
00107     if ((srcFmt.bytesPerPixel == 1) || (dstFmt.bytesPerPixel == 1)
00108              || (dstFmt.bytesPerPixel == 3)
00109              || (!srcFmt.bytesPerPixel) || (!dstFmt.bytesPerPixel))
00110         return false;
00111 
00112     // Don't perform unnecessary conversion
00113     if (srcFmt == dstFmt) {
00114         if (dst != src) {
00115             if (dstPitch == srcPitch && ((w * dstFmt.bytesPerPixel) == dstPitch)) {
00116                 memcpy(dst, src, dstPitch * h);
00117             } else {
00118                 for (uint i = 0; i < h; ++i) {
00119                     memcpy(dst, src, w * dstFmt.bytesPerPixel);
00120                     dst += dstPitch;
00121                     src += srcPitch;
00122                 }
00123             }
00124         }
00125 
00126         return true;
00127     }
00128 
00129     // Faster, but larger, to provide optimized handling for each case.
00130     const uint srcDelta = (srcPitch - w * srcFmt.bytesPerPixel);
00131     const uint dstDelta = (dstPitch - w * dstFmt.bytesPerPixel);
00132 
00133     // TODO: optimized cases for dstDelta of 0
00134     if (dstFmt.bytesPerPixel == 2) {
00135         if (srcFmt.bytesPerPixel == 2) {
00136             crossBlitLogic<uint16, uint16, false>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
00137         } else if (srcFmt.bytesPerPixel == 3) {
00138             crossBlitLogic3BppSource<uint16, false>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
00139         } else {
00140             crossBlitLogic<uint32, uint16, false>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
00141         }
00142     } else if (dstFmt.bytesPerPixel == 4) {
00143         if (srcFmt.bytesPerPixel == 2) {
00144             // We need to blit the surface from bottom right to top left here.
00145             // This is neeeded, because when we convert to the same memory
00146             // buffer copying the surface from top left to bottom right would
00147             // overwrite the source, since we have more bits per destination
00148             // color than per source color.
00149             dst += h * dstPitch - dstDelta - dstFmt.bytesPerPixel;
00150             src += h * srcPitch - srcDelta - srcFmt.bytesPerPixel;
00151             crossBlitLogic<uint16, uint32, true>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
00152         } else if (srcFmt.bytesPerPixel == 3) {
00153             // We need to blit the surface from bottom right to top left here.
00154             // This is neeeded, because when we convert to the same memory
00155             // buffer copying the surface from top left to bottom right would
00156             // overwrite the source, since we have more bits per destination
00157             // color than per source color.
00158             dst += h * dstPitch - dstDelta - dstFmt.bytesPerPixel;
00159             src += h * srcPitch - srcDelta - srcFmt.bytesPerPixel;
00160             crossBlitLogic3BppSource<uint32, true>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
00161         } else {
00162             crossBlitLogic<uint32, uint32, false>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
00163         }
00164     } else {
00165         return false;
00166     }
00167     return true;
00168 }
00169 
00170 } // End of namespace Graphics


Generated on Sat Mar 16 2019 05:01:22 for ResidualVM by doxygen 1.7.1
curved edge   curved edge