2002-04-12 10:35:29 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
2003-03-06 21:46:56 +00:00
|
|
|
* Copyright (C) 2001-2003 The ScummVM project
|
2002-04-12 10:35:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
#include "common/scaler/intern.h"
|
2003-09-21 12:10:32 +00:00
|
|
|
|
2002-04-12 10:35:29 +00:00
|
|
|
|
2003-07-19 11:35:01 +00:00
|
|
|
// TODO: get rid of the colorMask etc. variables and instead use templates.
|
|
|
|
// This should give a respectable boost, since variable access (i.e. memory reads)
|
|
|
|
// in the innermost loops of our operations would work with constant data instead.
|
|
|
|
// That should help the inliner; reduce memory access; thus improve cache efficeny
|
|
|
|
// etc. The drawback will be that each scaler will exist twice, once for 555 and
|
|
|
|
// once for 555, resulting in the object file being twice as big (but thanks to
|
|
|
|
// templates, no source code would be duplicated.
|
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
|
|
|
|
int gBitFormat = 565;
|
|
|
|
|
|
|
|
uint32 colorMask = 0xF7DEF7DE;
|
|
|
|
uint32 lowPixelMask = 0x08210821;
|
|
|
|
uint32 qcolorMask = 0xE79CE79C;
|
|
|
|
uint32 qlowpixelMask = 0x18631863;
|
|
|
|
static uint32 redblueMask = redblueMask_565;
|
|
|
|
static uint32 greenMask = greenMask_565;
|
|
|
|
|
|
|
|
// RGB-to-YUV lookup table
|
|
|
|
int RGBtoYUV[65536];
|
2002-04-12 10:35:29 +00:00
|
|
|
|
2003-03-02 16:36:52 +00:00
|
|
|
static const uint16 dotmatrix_565[16] = {
|
2003-03-06 16:27:06 +00:00
|
|
|
0x01E0, 0x0007, 0x3800, 0x0000,
|
|
|
|
0x39E7, 0x0000, 0x39E7, 0x0000,
|
|
|
|
0x3800, 0x0000, 0x01E0, 0x0007,
|
|
|
|
0x39E7, 0x0000, 0x39E7, 0x0000
|
2003-03-02 16:36:52 +00:00
|
|
|
};
|
|
|
|
static const uint16 dotmatrix_555[16] = {
|
2003-03-06 16:27:06 +00:00
|
|
|
0x00E0, 0x0007, 0x1C00, 0x0000,
|
|
|
|
0x1CE7, 0x0000, 0x1CE7, 0x0000,
|
|
|
|
0x1C00, 0x0000, 0x00E0, 0x0007,
|
|
|
|
0x1CE7, 0x0000, 0x1CE7, 0x0000
|
2003-03-02 16:36:52 +00:00
|
|
|
};
|
|
|
|
static const uint16 *dotmatrix;
|
|
|
|
|
2003-09-21 17:56:11 +00:00
|
|
|
static void InitLUT(uint32 BitFormat);
|
2003-09-21 12:10:32 +00:00
|
|
|
|
|
|
|
void InitScalers(uint32 BitFormat) {
|
2002-04-12 10:35:29 +00:00
|
|
|
if (BitFormat == 565) {
|
|
|
|
colorMask = 0xF7DEF7DE;
|
|
|
|
lowPixelMask = 0x08210821;
|
|
|
|
qcolorMask = 0xE79CE79C;
|
|
|
|
qlowpixelMask = 0x18631863;
|
2003-09-29 16:02:47 +00:00
|
|
|
redblueMask = redblueMask_565;
|
|
|
|
greenMask = greenMask_565;
|
2003-03-02 16:36:52 +00:00
|
|
|
dotmatrix = dotmatrix_565;
|
2002-04-12 10:35:29 +00:00
|
|
|
} else if (BitFormat == 555) {
|
|
|
|
colorMask = 0x7BDE7BDE;
|
|
|
|
lowPixelMask = 0x04210421;
|
|
|
|
qcolorMask = 0x739C739C;
|
|
|
|
qlowpixelMask = 0x0C630C63;
|
2003-09-29 16:02:47 +00:00
|
|
|
redblueMask = redblueMask_555;
|
|
|
|
greenMask = greenMask_555;
|
2003-03-02 16:36:52 +00:00
|
|
|
dotmatrix = dotmatrix_555;
|
2002-04-12 10:35:29 +00:00
|
|
|
} else {
|
2003-09-21 12:10:32 +00:00
|
|
|
error("Unknwon bit format %d\n", BitFormat);
|
2002-04-12 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
gBitFormat = BitFormat;
|
2003-09-21 17:56:11 +00:00
|
|
|
InitLUT(BitFormat);
|
2002-04-12 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
void InitLUT(uint32 BitFormat) {
|
|
|
|
int r, g, b;
|
|
|
|
int Y, u, v;
|
|
|
|
int gInc, gShift;
|
|
|
|
|
|
|
|
if (BitFormat == 565) {
|
|
|
|
gInc = 256 >> 6;
|
|
|
|
gShift = 6 - 3;
|
|
|
|
} else {
|
|
|
|
gInc = 256 >> 5;
|
|
|
|
gShift = 5 - 3;
|
|
|
|
}
|
2002-04-12 10:35:29 +00:00
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
for (r = 0; r < 256; r += 8) {
|
|
|
|
for (g = 0; g < 256; g += gInc) {
|
|
|
|
for (b = 0; b < 256; b += 8) {
|
|
|
|
Y = (r + g + b) >> 2;
|
|
|
|
u = 128 + ((r - b) >> 2);
|
|
|
|
v = 128 + ((-r + 2 * g -b) >> 3);
|
|
|
|
RGBtoYUV[ (r << (5+gShift)) + (g << gShift) + (b >> 3) ] = (Y << 16) + (u << 8) + v;
|
2003-05-25 22:01:19 +00:00
|
|
|
}
|
2003-06-10 16:25:49 +00:00
|
|
|
}
|
2003-09-29 16:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
2002-04-12 10:35:29 +00:00
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
/**
|
|
|
|
* Trivial 'scaler' - in fact it doesn't do any scaling but just copies the
|
|
|
|
* source to the destionation.
|
|
|
|
*/
|
|
|
|
void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
|
|
|
|
int width, int height) {
|
|
|
|
while (height--) {
|
|
|
|
memcpy(dstPtr, srcPtr, 2 * width);
|
2003-05-25 22:01:19 +00:00
|
|
|
srcPtr += srcPitch;
|
2003-09-29 16:02:47 +00:00
|
|
|
dstPtr += dstPitch;
|
2003-06-10 16:25:49 +00:00
|
|
|
}
|
2002-04-12 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
/**
|
|
|
|
* Trivial nearest-neighbour 2x scaler.
|
|
|
|
*/
|
|
|
|
void Normal2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
|
|
|
|
int width, int height) {
|
|
|
|
uint8 *r;
|
2002-04-12 10:35:29 +00:00
|
|
|
|
2003-05-25 22:01:19 +00:00
|
|
|
while (height--) {
|
2003-09-29 16:02:47 +00:00
|
|
|
r = dstPtr;
|
|
|
|
for (int i = 0; i < width; ++i, r += 4) {
|
|
|
|
uint16 color = *(((const uint16 *)srcPtr) + i);
|
2003-06-10 16:25:49 +00:00
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
*(uint16 *)(r + 0) = color;
|
|
|
|
*(uint16 *)(r + 2) = color;
|
|
|
|
*(uint16 *)(r + 0 + dstPitch) = color;
|
|
|
|
*(uint16 *)(r + 2 + dstPitch) = color;
|
2003-06-10 16:25:49 +00:00
|
|
|
}
|
2003-05-25 22:01:19 +00:00
|
|
|
srcPtr += srcPitch;
|
2003-09-29 16:02:47 +00:00
|
|
|
dstPtr += dstPitch << 1;
|
2003-06-10 16:25:49 +00:00
|
|
|
}
|
2002-04-12 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
/**
|
|
|
|
* Trivial nearest-neighbour 3x scaler.
|
|
|
|
*/
|
|
|
|
void Normal3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
|
|
|
|
int width, int height) {
|
|
|
|
uint8 *r;
|
|
|
|
uint32 dstPitch2 = dstPitch * 2;
|
|
|
|
uint32 dstPitch3 = dstPitch * 3;
|
2002-04-12 10:35:29 +00:00
|
|
|
|
2003-05-25 22:01:19 +00:00
|
|
|
while (height--) {
|
2003-09-29 16:02:47 +00:00
|
|
|
r = dstPtr;
|
|
|
|
for (int i = 0; i < width; ++i, r += 6) {
|
|
|
|
uint16 color = *(((const uint16 *)srcPtr) + i);
|
2002-04-18 08:34:47 +00:00
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
*(uint16 *)(r + 0) = color;
|
|
|
|
*(uint16 *)(r + 2) = color;
|
|
|
|
*(uint16 *)(r + 4) = color;
|
|
|
|
*(uint16 *)(r + 0 + dstPitch) = color;
|
|
|
|
*(uint16 *)(r + 2 + dstPitch) = color;
|
|
|
|
*(uint16 *)(r + 4 + dstPitch) = color;
|
|
|
|
*(uint16 *)(r + 0 + dstPitch2) = color;
|
|
|
|
*(uint16 *)(r + 2 + dstPitch2) = color;
|
|
|
|
*(uint16 *)(r + 4 + dstPitch2) = color;
|
2003-06-10 16:25:49 +00:00
|
|
|
}
|
2003-05-25 22:01:19 +00:00
|
|
|
srcPtr += srcPitch;
|
2003-09-29 16:02:47 +00:00
|
|
|
dstPtr += dstPitch3;
|
2003-06-10 16:25:49 +00:00
|
|
|
}
|
2002-04-12 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
/**
|
|
|
|
* The Scale2x filter, also known as AdvMame2x.
|
|
|
|
* See also http://scale2x.sourceforge.net
|
|
|
|
*/
|
2003-05-25 12:08:01 +00:00
|
|
|
void AdvMame2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
|
2003-03-06 16:27:06 +00:00
|
|
|
int width, int height) {
|
2003-09-22 23:29:47 +00:00
|
|
|
const uint32 nextlineSrc = srcPitch / sizeof(uint16);
|
2003-05-25 12:08:01 +00:00
|
|
|
const uint16 *p = (const uint16 *)srcPtr;
|
2002-04-24 07:42:29 +00:00
|
|
|
|
2003-09-22 23:29:47 +00:00
|
|
|
const uint32 nextlineDst = dstPitch / sizeof(uint16);
|
2003-03-28 19:13:59 +00:00
|
|
|
uint16 *q = (uint16 *)dstPtr;
|
|
|
|
|
|
|
|
uint16 A, B, C;
|
|
|
|
uint16 D, E, F;
|
|
|
|
uint16 G, H, I;
|
2002-04-24 07:42:29 +00:00
|
|
|
|
2002-05-05 18:05:56 +00:00
|
|
|
while (height--) {
|
2003-05-16 00:11:35 +00:00
|
|
|
B = *(p - 1 - nextlineSrc);
|
|
|
|
E = *(p - 1);
|
|
|
|
H = *(p - 1 + nextlineSrc);
|
|
|
|
C = *(p - nextlineSrc);
|
|
|
|
F = *(p);
|
|
|
|
I = *(p + nextlineSrc);
|
2003-09-29 16:02:47 +00:00
|
|
|
int tmpWidth = width;
|
|
|
|
while (tmpWidth--) {
|
2003-03-28 19:13:59 +00:00
|
|
|
p++;
|
|
|
|
A = B; B = C; C = *(p - nextlineSrc);
|
|
|
|
D = E; E = F; F = *(p);
|
|
|
|
G = H; H = I; I = *(p + nextlineSrc);
|
|
|
|
|
|
|
|
*(q) = D == B && B != F && D != H ? D : E;
|
|
|
|
*(q + 1) = B == F && B != D && F != H ? F : E;
|
|
|
|
*(q + nextlineDst) = D == H && D != B && H != F ? D : E;
|
|
|
|
*(q + nextlineDst + 1) = H == F && D != H && B != F ? F : E;
|
|
|
|
q += 2;
|
2002-04-24 07:42:29 +00:00
|
|
|
}
|
2003-03-28 19:13:59 +00:00
|
|
|
p += nextlineSrc - width;
|
|
|
|
q += (nextlineDst - width) << 1;
|
2002-04-24 07:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 00:05:45 +00:00
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
/**
|
|
|
|
* The Scale3x filter, also known as AdvMame3x.
|
|
|
|
* See also http://scale2x.sourceforge.net
|
|
|
|
*/
|
2003-05-25 12:08:01 +00:00
|
|
|
void AdvMame3x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
|
2003-05-09 22:44:16 +00:00
|
|
|
int width, int height) {
|
2003-09-22 23:29:47 +00:00
|
|
|
const uint32 nextlineSrc = srcPitch / sizeof(uint16);
|
2003-05-25 12:08:01 +00:00
|
|
|
const uint16 *p = (const uint16 *)srcPtr;
|
2003-05-09 22:44:16 +00:00
|
|
|
|
2003-09-22 23:29:47 +00:00
|
|
|
const uint32 nextlineDst = dstPitch / sizeof(uint16);
|
2003-05-09 22:44:16 +00:00
|
|
|
uint16 *q = (uint16 *)dstPtr;
|
|
|
|
|
|
|
|
uint16 A, B, C;
|
|
|
|
uint16 D, E, F;
|
|
|
|
uint16 G, H, I;
|
|
|
|
|
|
|
|
while (height--) {
|
2003-05-16 00:11:35 +00:00
|
|
|
B = *(p - 1 - nextlineSrc);
|
|
|
|
E = *(p - 1);
|
|
|
|
H = *(p - 1 + nextlineSrc);
|
|
|
|
C = *(p - nextlineSrc);
|
|
|
|
F = *(p);
|
|
|
|
I = *(p + nextlineSrc);
|
2003-09-29 16:02:47 +00:00
|
|
|
int tmpWidth = width;
|
|
|
|
while (tmpWidth--) {
|
2003-05-09 22:44:16 +00:00
|
|
|
p++;
|
|
|
|
A = B; B = C; C = *(p - nextlineSrc);
|
|
|
|
D = E; E = F; F = *(p);
|
|
|
|
G = H; H = I; I = *(p + nextlineSrc);
|
|
|
|
|
|
|
|
*(q) = D == B && B != F && D != H ? D : E;
|
|
|
|
*(q + 1) = E;
|
|
|
|
*(q + 2) = B == F && B != D && F != H ? F : E;
|
|
|
|
*(q + nextlineDst) = E;
|
|
|
|
*(q + nextlineDst + 1) = E;
|
|
|
|
*(q + nextlineDst + 2) = E;
|
|
|
|
*(q + 2 * nextlineDst) = D == H && D != B && H != F ? D : E;
|
|
|
|
*(q + 2 * nextlineDst + 1) = E;
|
|
|
|
*(q + 2 * nextlineDst + 2) = H == F && D != H && B != F ? F : E;
|
|
|
|
q += 3;
|
|
|
|
}
|
|
|
|
p += nextlineSrc - width;
|
|
|
|
q += (nextlineDst - width) * 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-25 12:08:01 +00:00
|
|
|
void TV2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
|
2003-03-06 16:27:06 +00:00
|
|
|
int width, int height) {
|
2003-09-22 23:29:47 +00:00
|
|
|
const uint32 nextlineSrc = srcPitch / sizeof(uint16);
|
2003-05-25 12:08:01 +00:00
|
|
|
const uint16 *p = (const uint16 *)srcPtr;
|
2003-01-15 02:11:37 +00:00
|
|
|
|
2003-09-22 23:29:47 +00:00
|
|
|
const uint32 nextlineDst = dstPitch / sizeof(uint16);
|
2003-01-19 22:28:36 +00:00
|
|
|
uint16 *q = (uint16 *)dstPtr;
|
2003-01-15 02:11:37 +00:00
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
while (height--) {
|
2003-01-15 02:11:37 +00:00
|
|
|
for (int i = 0, j = 0; i < width; ++i, j += 2) {
|
2003-01-19 22:28:36 +00:00
|
|
|
uint16 p1 = *(p + i);
|
|
|
|
uint32 pi;
|
|
|
|
|
|
|
|
pi = (((p1 & redblueMask) * 7) >> 3) & redblueMask;
|
|
|
|
pi |= (((p1 & greenMask) * 7) >> 3) & greenMask;
|
2003-01-15 02:11:37 +00:00
|
|
|
|
|
|
|
*(q + j) = p1;
|
|
|
|
*(q + j + 1) = p1;
|
2003-01-19 22:28:36 +00:00
|
|
|
*(q + j + nextlineDst) = (uint16)pi;
|
|
|
|
*(q + j + nextlineDst + 1) = (uint16)pi;
|
2003-01-15 02:11:37 +00:00
|
|
|
}
|
|
|
|
p += nextlineSrc;
|
|
|
|
q += nextlineDst << 1;
|
|
|
|
}
|
|
|
|
}
|
2003-03-02 16:36:52 +00:00
|
|
|
|
|
|
|
static inline uint16 DOT_16(uint16 c, int j, int i) {
|
|
|
|
return c - ((c >> 2) & *(dotmatrix + ((j & 3) << 2) + (i & 3)));
|
|
|
|
}
|
|
|
|
|
2003-05-25 12:08:01 +00:00
|
|
|
void DotMatrix(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
|
2003-03-02 16:36:52 +00:00
|
|
|
int width, int height)
|
|
|
|
{
|
2003-09-22 23:29:47 +00:00
|
|
|
const uint32 nextlineSrc = srcPitch / sizeof(uint16);
|
2003-05-25 12:08:01 +00:00
|
|
|
const uint16 *p = (const uint16 *)srcPtr;
|
2003-03-02 16:36:52 +00:00
|
|
|
|
2003-09-22 23:29:47 +00:00
|
|
|
const uint32 nextlineDst = dstPitch / sizeof(uint16);
|
2003-03-02 16:36:52 +00:00
|
|
|
uint16 *q = (uint16 *)dstPtr;
|
|
|
|
|
|
|
|
for (int j = 0, jj = 0; j < height; ++j, jj += 2) {
|
|
|
|
for (int i = 0, ii = 0; i < width; ++i, ii += 2) {
|
|
|
|
uint16 c = *(p + i);
|
|
|
|
*(q + ii) = DOT_16(c, jj, ii);
|
|
|
|
*(q + ii + 1) = DOT_16(c, jj, ii + 1);
|
|
|
|
*(q + ii + nextlineDst) = DOT_16(c, jj + 1, ii);
|
|
|
|
*(q + ii + nextlineDst + 1) = DOT_16(c, jj + 1, ii + 1);
|
|
|
|
}
|
|
|
|
p += nextlineSrc;
|
|
|
|
q += nextlineDst << 1;
|
|
|
|
}
|
|
|
|
}
|
2003-06-22 11:55:40 +00:00
|
|
|
|