1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program 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 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program 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 this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "graphics/conversion.h"
24 #include "graphics/pixelformat.h"
25
26 #include "common/endian.h"
27
28 namespace Graphics {
29
30 // TODO: YUV to RGB conversion function
31
32 namespace {
33
34 template<typename SrcColor, typename DstColor, bool backward>
crossBlitLogic(byte * dst,const byte * src,const uint w,const uint h,const PixelFormat & srcFmt,const PixelFormat & dstFmt,const uint srcDelta,const uint dstDelta)35 inline void crossBlitLogic(byte *dst, const byte *src, const uint w, const uint h,
36 const PixelFormat &srcFmt, const PixelFormat &dstFmt,
37 const uint srcDelta, const uint dstDelta) {
38 for (uint y = 0; y < h; ++y) {
39 for (uint x = 0; x < w; ++x) {
40 const uint32 color = *(const SrcColor *)src;
41 byte a, r, g, b;
42 srcFmt.colorToARGB(color, a, r, g, b);
43 *(DstColor *)dst = dstFmt.ARGBToColor(a, r, g, b);
44
45 if (backward) {
46 src -= sizeof(SrcColor);
47 dst -= sizeof(DstColor);
48 } else {
49 src += sizeof(SrcColor);
50 dst += sizeof(DstColor);
51 }
52 }
53
54 if (backward) {
55 src -= srcDelta;
56 dst -= dstDelta;
57 } else {
58 src += srcDelta;
59 dst += dstDelta;
60 }
61 }
62 }
63
64 template<typename DstColor, bool backward>
crossBlitLogic3BppSource(byte * dst,const byte * src,const uint w,const uint h,const PixelFormat & srcFmt,const PixelFormat & dstFmt,const uint srcDelta,const uint dstDelta)65 inline void crossBlitLogic3BppSource(byte *dst, const byte *src, const uint w, const uint h,
66 const PixelFormat &srcFmt, const PixelFormat &dstFmt,
67 const uint srcDelta, const uint dstDelta) {
68 uint32 color;
69 byte r, g, b, a;
70 uint8 *col = (uint8 *)&color;
71 #ifdef SCUMM_BIG_ENDIAN
72 col++;
73 #endif
74 for (uint y = 0; y < h; ++y) {
75 for (uint x = 0; x < w; ++x) {
76 memcpy(col, src, 3);
77 srcFmt.colorToARGB(color, a, r, g, b);
78 *(DstColor *)dst = dstFmt.ARGBToColor(a, r, g, b);
79
80 if (backward) {
81 src -= 3;
82 dst -= sizeof(DstColor);
83 } else {
84 src += 3;
85 dst += sizeof(DstColor);
86 }
87 }
88
89 if (backward) {
90 src -= srcDelta;
91 dst -= dstDelta;
92 } else {
93 src += srcDelta;
94 dst += dstDelta;
95 }
96 }
97 }
98
99 } // End of anonymous namespace
100
101 // Function to blit a rect from one color format to another
crossBlit(byte * dst,const byte * src,const uint dstPitch,const uint srcPitch,const uint w,const uint h,const Graphics::PixelFormat & dstFmt,const Graphics::PixelFormat & srcFmt)102 bool crossBlit(byte *dst, const byte *src,
103 const uint dstPitch, const uint srcPitch,
104 const uint w, const uint h,
105 const Graphics::PixelFormat &dstFmt, const Graphics::PixelFormat &srcFmt) {
106 // Error out if conversion is impossible
107 if ((srcFmt.bytesPerPixel == 1) || (dstFmt.bytesPerPixel == 1)
108 || (dstFmt.bytesPerPixel == 3)
109 || (!srcFmt.bytesPerPixel) || (!dstFmt.bytesPerPixel))
110 return false;
111
112 // Don't perform unnecessary conversion
113 if (srcFmt == dstFmt) {
114 if (dst != src) {
115 if (dstPitch == srcPitch && ((w * dstFmt.bytesPerPixel) == dstPitch)) {
116 memcpy(dst, src, dstPitch * h);
117 } else {
118 for (uint i = 0; i < h; ++i) {
119 memcpy(dst, src, w * dstFmt.bytesPerPixel);
120 dst += dstPitch;
121 src += srcPitch;
122 }
123 }
124 }
125
126 return true;
127 }
128
129 // Faster, but larger, to provide optimized handling for each case.
130 const uint srcDelta = (srcPitch - w * srcFmt.bytesPerPixel);
131 const uint dstDelta = (dstPitch - w * dstFmt.bytesPerPixel);
132
133 // TODO: optimized cases for dstDelta of 0
134 if (dstFmt.bytesPerPixel == 2) {
135 if (srcFmt.bytesPerPixel == 2) {
136 crossBlitLogic<uint16, uint16, false>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
137 } else if (srcFmt.bytesPerPixel == 3) {
138 crossBlitLogic3BppSource<uint16, false>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
139 } else {
140 crossBlitLogic<uint32, uint16, false>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
141 }
142 } else if (dstFmt.bytesPerPixel == 4) {
143 if (srcFmt.bytesPerPixel == 2) {
144 // We need to blit the surface from bottom right to top left here.
145 // This is neeeded, because when we convert to the same memory
146 // buffer copying the surface from top left to bottom right would
147 // overwrite the source, since we have more bits per destination
148 // color than per source color.
149 dst += h * dstPitch - dstDelta - dstFmt.bytesPerPixel;
150 src += h * srcPitch - srcDelta - srcFmt.bytesPerPixel;
151 crossBlitLogic<uint16, uint32, true>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
152 } else if (srcFmt.bytesPerPixel == 3) {
153 // We need to blit the surface from bottom right to top left here.
154 // This is neeeded, because when we convert to the same memory
155 // buffer copying the surface from top left to bottom right would
156 // overwrite the source, since we have more bits per destination
157 // color than per source color.
158 dst += h * dstPitch - dstDelta - dstFmt.bytesPerPixel;
159 src += h * srcPitch - srcDelta - srcFmt.bytesPerPixel;
160 crossBlitLogic3BppSource<uint32, true>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
161 } else {
162 crossBlitLogic<uint32, uint32, false>(dst, src, w, h, srcFmt, dstFmt, srcDelta, dstDelta);
163 }
164 } else {
165 return false;
166 }
167 return true;
168 }
169
170 } // End of namespace Graphics
171