2002-04-12 10:35:29 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2001-2006 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-04-12 10:35:29 +00:00
|
|
|
*
|
2006-02-11 09:53:53 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-04-12 10:35:29 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-02-20 20:29:02 +00:00
|
|
|
#include "graphics/scaler/intern.h"
|
|
|
|
#include "graphics/scaler/scalebit.h"
|
2003-11-06 23:54:12 +00:00
|
|
|
#include "common/util.h"
|
2003-09-21 12:10:32 +00:00
|
|
|
|
2002-04-12 10:35:29 +00:00
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
int gBitFormat = 565;
|
|
|
|
|
2005-05-14 22:56:41 +00:00
|
|
|
#ifndef DISABLE_HQ_SCALERS
|
2003-09-29 16:02:47 +00:00
|
|
|
// RGB-to-YUV lookup table
|
2004-05-21 02:08:48 +00:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
#ifdef USE_NASM
|
|
|
|
// NOTE: if your compiler uses different mangled names, add another
|
|
|
|
// condition here
|
|
|
|
|
2004-05-21 17:30:51 +00:00
|
|
|
#ifndef _WIN32
|
2006-03-14 12:18:04 +00:00
|
|
|
#ifndef MACOSX
|
2004-05-21 02:08:48 +00:00
|
|
|
#define RGBtoYUV _RGBtoYUV
|
|
|
|
#define LUT16to32 _LUT16to32
|
|
|
|
#endif
|
2006-03-14 12:18:04 +00:00
|
|
|
#endif
|
2004-05-21 02:08:48 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-04-27 08:43:23 +00:00
|
|
|
// FIXME/TODO: The following two tables suck up 512 KB.
|
|
|
|
// They should at least be allocated on the heap, to reduce the size of the
|
2005-07-30 21:11:48 +00:00
|
|
|
// binary.
|
2005-04-27 08:43:23 +00:00
|
|
|
//
|
|
|
|
// Note: a memory lookup table is *not* necessarily faster than computing
|
2005-07-30 21:11:48 +00:00
|
|
|
// these things on the fly, because of its size. Both tables together, plus
|
2005-04-27 08:43:23 +00:00
|
|
|
// the code, plus the input/output GFX data, won't fit in the cache on many
|
|
|
|
// systems, so main memory has to be accessed, which is about the worst thing
|
|
|
|
// that can happen to code which tries to be fast...
|
|
|
|
//
|
|
|
|
// So we should think about ways to get these smaller / removed. The LUT16to32
|
|
|
|
// is only used by the HQX asm right now; maybe somebody can modify the code
|
|
|
|
// there to work w/o it (and do some benchmarking, too?). To do that, just
|
|
|
|
// do the conversion on the fly, or even do w/o it (as the C++ code manages to),
|
|
|
|
// by making different versions of the code based on gBitFormat (or by writing
|
|
|
|
// bit masks into registers which are computed based on gBitFormat).
|
|
|
|
//
|
|
|
|
// RGBtoYUV is also used by the C(++) version of the HQX code. Maybe we can
|
|
|
|
// use the same technique which is employed by our MPEG code to reduce the
|
|
|
|
// size of the lookup tables at the cost of some additional computations? That
|
|
|
|
// might actually result in a speedup, too, if done right (and the code code
|
|
|
|
// might actually be suitable for AltiVec/MMX/SSE speedup).
|
2005-07-30 21:11:48 +00:00
|
|
|
//
|
2005-04-27 08:43:23 +00:00
|
|
|
// Of course, the above is largely a conjecture, and the actual speed
|
|
|
|
// differences are likely to vary a lot between different architectures and
|
|
|
|
// CPUs.
|
|
|
|
uint RGBtoYUVstorage[65536];
|
|
|
|
uint *RGBtoYUV = RGBtoYUVstorage;
|
2004-05-21 02:08:48 +00:00
|
|
|
uint LUT16to32[65536];
|
|
|
|
}
|
2005-05-14 22:56:41 +00:00
|
|
|
#endif
|
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) {
|
2003-03-02 16:36:52 +00:00
|
|
|
dotmatrix = dotmatrix_565;
|
2002-04-12 10:35:29 +00:00
|
|
|
} else if (BitFormat == 555) {
|
2003-03-02 16:36:52 +00:00
|
|
|
dotmatrix = dotmatrix_555;
|
2002-04-12 10:35:29 +00:00
|
|
|
} else {
|
2004-02-27 15:52:23 +00:00
|
|
|
error("Unknown bit format %d", 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) {
|
2005-05-14 23:28:22 +00:00
|
|
|
#ifndef DISABLE_HQ_SCALERS
|
2003-09-29 16:02:47 +00:00
|
|
|
int r, g, b;
|
|
|
|
int Y, u, v;
|
|
|
|
int gInc, gShift;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-05-21 02:08:48 +00:00
|
|
|
for (int i = 0; i < 65536; i++) {
|
|
|
|
LUT16to32[i] = ((i & 0xF800) << 8) + ((i & 0x07E0) << 5) + ((i & 0x001F) << 3);
|
|
|
|
}
|
|
|
|
|
2003-09-29 16:02:47 +00:00
|
|
|
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);
|
2004-05-21 02:08:48 +00:00
|
|
|
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
|
|
|
}
|
2005-05-14 22:56:41 +00:00
|
|
|
#endif
|
2005-05-14 23:28:22 +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
|
|
|
}
|
|
|
|
|
2005-09-20 18:16:09 +00:00
|
|
|
#ifndef DISABLE_SCALERS
|
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
|
|
|
|
2005-06-23 14:18:25 +00:00
|
|
|
assert(((long)dstPtr & 3) == 0);
|
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) {
|
2004-08-08 15:45:58 +00:00
|
|
|
uint32 color = *(((const uint16 *)srcPtr) + i);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-08-08 15:45:58 +00:00
|
|
|
color |= color << 16;
|
2003-06-10 16:25:49 +00:00
|
|
|
|
2004-08-08 15:45:58 +00:00
|
|
|
*(uint32 *)(r) = color;
|
|
|
|
*(uint32 *)(r + 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;
|
2004-06-27 22:04:19 +00:00
|
|
|
const uint32 dstPitch2 = dstPitch * 2;
|
|
|
|
const uint32 dstPitch3 = dstPitch * 3;
|
2002-04-12 10:35:29 +00:00
|
|
|
|
2005-06-23 14:18:25 +00:00
|
|
|
assert(((long)dstPtr & 1) == 0);
|
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
|
|
|
}
|
|
|
|
|
2005-02-17 23:01:00 +00:00
|
|
|
#define INTERPOLATE INTERPOLATE<bitFormat>
|
|
|
|
#define Q_INTERPOLATE Q_INTERPOLATE<bitFormat>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trivial nearest-neighbour 1.5x scaler.
|
|
|
|
*/
|
|
|
|
template<int bitFormat>
|
|
|
|
void Normal1o5xTemplate(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
|
|
|
|
int width, int height) {
|
|
|
|
uint8 *r;
|
|
|
|
const uint32 dstPitch2 = dstPitch * 2;
|
|
|
|
const uint32 dstPitch3 = dstPitch * 3;
|
|
|
|
const uint32 srcPitch2 = srcPitch * 2;
|
|
|
|
|
2005-06-23 14:18:25 +00:00
|
|
|
assert(((long)dstPtr & 1) == 0);
|
2005-03-06 11:28:10 +00:00
|
|
|
while (height > 0) {
|
2005-02-17 23:01:00 +00:00
|
|
|
r = dstPtr;
|
|
|
|
for (int i = 0; i < width; i += 2, r += 6) {
|
|
|
|
uint16 color0 = *(((const uint16 *)srcPtr) + i);
|
|
|
|
uint16 color1 = *(((const uint16 *)srcPtr) + i + 1);
|
|
|
|
uint16 color2 = *(((const uint16 *)(srcPtr + srcPitch)) + i);
|
|
|
|
uint16 color3 = *(((const uint16 *)(srcPtr + srcPitch)) + i + 1);
|
|
|
|
|
|
|
|
*(uint16 *)(r + 0) = color0;
|
|
|
|
*(uint16 *)(r + 2) = INTERPOLATE(color0, color1);
|
|
|
|
*(uint16 *)(r + 4) = color1;
|
|
|
|
*(uint16 *)(r + 0 + dstPitch) = INTERPOLATE(color0, color2);
|
|
|
|
*(uint16 *)(r + 2 + dstPitch) = Q_INTERPOLATE(color0, color1, color2, color3);
|
|
|
|
*(uint16 *)(r + 4 + dstPitch) = INTERPOLATE(color1, color3);
|
|
|
|
*(uint16 *)(r + 0 + dstPitch2) = color2;
|
|
|
|
*(uint16 *)(r + 2 + dstPitch2) = INTERPOLATE(color2, color3);
|
|
|
|
*(uint16 *)(r + 4 + dstPitch2) = color3;
|
|
|
|
}
|
|
|
|
srcPtr += srcPitch2;
|
|
|
|
dstPtr += dstPitch3;
|
|
|
|
height -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MAKE_WRAPPER(Normal1o5x)
|
|
|
|
|
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) {
|
2004-08-07 17:21:53 +00:00
|
|
|
scale(2, dstPtr, dstPitch, srcPtr - srcPitch, srcPitch, 2, width, height);
|
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) {
|
2004-08-07 17:21:53 +00:00
|
|
|
scale(3, dstPtr, dstPitch, srcPtr - srcPitch, srcPitch, 2, width, height);
|
2003-05-09 22:44:16 +00:00
|
|
|
}
|
|
|
|
|
2003-09-29 18:38:51 +00:00
|
|
|
template<int bitFormat>
|
2005-07-30 21:11:48 +00:00
|
|
|
void TV2xTemplate(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-09-29 18:38:51 +00:00
|
|
|
MAKE_WRAPPER(TV2x)
|
2003-03-02 16:36:52 +00:00
|
|
|
|
|
|
|
static inline uint16 DOT_16(uint16 c, int j, int i) {
|
2003-11-08 22:43:46 +00:00
|
|
|
return c - ((c >> 2) & *(dotmatrix + ((j & 3) << 2) + (i & 3)));
|
2003-03-02 16:36:52 +00:00
|
|
|
}
|
|
|
|
|
2003-10-24 17:14:56 +00:00
|
|
|
// FIXME: This scaler doesn't quite work. Either it needs to know where on the
|
|
|
|
// screen it's drawing, or the dirty rects will have to be adjusted so that
|
|
|
|
// access to the dotmatrix array are made in a consistent way. (Doing that in
|
|
|
|
// a way that also works together with aspect-ratio correction is left as an
|
|
|
|
// exercise for the reader.)
|
|
|
|
|
2003-05-25 12:08:01 +00:00
|
|
|
void DotMatrix(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
|
2003-11-08 22:43:46 +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;
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 18:16:09 +00:00
|
|
|
|
|
|
|
#endif
|