- moved bitmap.cpp to 'common' and remove the duplicate IceRemap.
This commit is contained in:
parent
9dce6d88fa
commit
8f8aed5ee3
10 changed files with 9 additions and 29 deletions
495
src/common/textures/bitmap.cpp
Normal file
495
src/common/textures/bitmap.cpp
Normal file
|
|
@ -0,0 +1,495 @@
|
|||
/*
|
||||
** bitmap.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2008 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include "bitmap.h"
|
||||
#include "palutil.h"
|
||||
|
||||
uint8_t IcePalette[16][3] =
|
||||
{
|
||||
{ 10, 8, 18 },
|
||||
{ 15, 15, 26 },
|
||||
{ 20, 16, 36 },
|
||||
{ 30, 26, 46 },
|
||||
{ 40, 36, 57 },
|
||||
{ 50, 46, 67 },
|
||||
{ 59, 57, 78 },
|
||||
{ 69, 67, 88 },
|
||||
{ 79, 77, 99 },
|
||||
{ 89, 87,109 },
|
||||
{ 99, 97,120 },
|
||||
{ 109,107,130 },
|
||||
{ 118,118,141 },
|
||||
{ 128,128,151 },
|
||||
{ 138,138,162 },
|
||||
{ 148,148,172 }
|
||||
};
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// multi-format pixel copy with colormap application
|
||||
// requires the previously defined conversion classes to work
|
||||
//
|
||||
//===========================================================================
|
||||
template<class TSrc, class TDest, class TBlend>
|
||||
void iCopyColors(uint8_t *pout, const uint8_t *pin, int count, int step, FCopyInfo *inf,
|
||||
uint8_t tr, uint8_t tg, uint8_t tb)
|
||||
{
|
||||
int i;
|
||||
int fac;
|
||||
uint8_t r,g,b;
|
||||
int gray;
|
||||
int a;
|
||||
|
||||
switch(inf? inf->blend : BLEND_NONE)
|
||||
{
|
||||
case BLEND_NONE:
|
||||
for(i=0;i<count;i++)
|
||||
{
|
||||
a = TSrc::A(pin, tr, tg, tb);
|
||||
if (TBlend::ProcessAlpha0() || a)
|
||||
{
|
||||
TBlend::OpC(pout[TDest::RED], TSrc::R(pin), a, inf);
|
||||
TBlend::OpC(pout[TDest::GREEN], TSrc::G(pin), a, inf);
|
||||
TBlend::OpC(pout[TDest::BLUE], TSrc::B(pin), a, inf);
|
||||
TBlend::OpA(pout[TDest::ALPHA], a, inf);
|
||||
}
|
||||
pout+=4;
|
||||
pin+=step;
|
||||
}
|
||||
break;
|
||||
|
||||
case BLEND_ICEMAP:
|
||||
// Create the ice translation table, based on Hexen's.
|
||||
// Since this is done in True Color the purplish tint is fully preserved - even in Doom!
|
||||
for(i=0;i<count;i++)
|
||||
{
|
||||
a = TSrc::A(pin, tr, tg, tb);
|
||||
if (TBlend::ProcessAlpha0() || a)
|
||||
{
|
||||
int gray = TSrc::Gray(pin)>>4;
|
||||
|
||||
TBlend::OpC(pout[TDest::RED], IcePalette[gray][0], a, inf);
|
||||
TBlend::OpC(pout[TDest::GREEN], IcePalette[gray][1], a, inf);
|
||||
TBlend::OpC(pout[TDest::BLUE], IcePalette[gray][2], a, inf);
|
||||
TBlend::OpA(pout[TDest::ALPHA], a, inf);
|
||||
}
|
||||
pout+=4;
|
||||
pin+=step;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
if (inf->blend >= BLEND_SPECIALCOLORMAP1)
|
||||
{
|
||||
FSpecialColormap *cm = &SpecialColormaps[inf->blend - BLEND_SPECIALCOLORMAP1];
|
||||
for(i=0;i<count;i++)
|
||||
{
|
||||
a = TSrc::A(pin, tr, tg, tb);
|
||||
if (TBlend::ProcessAlpha0() || a)
|
||||
{
|
||||
gray = clamp<int>(TSrc::Gray(pin),0,255);
|
||||
|
||||
PalEntry pe = cm->GrayscaleToColor[gray];
|
||||
TBlend::OpC(pout[TDest::RED], pe.r , a, inf);
|
||||
TBlend::OpC(pout[TDest::GREEN], pe.g, a, inf);
|
||||
TBlend::OpC(pout[TDest::BLUE], pe.b, a, inf);
|
||||
TBlend::OpA(pout[TDest::ALPHA], a, inf);
|
||||
}
|
||||
pout+=4;
|
||||
pin+=step;
|
||||
}
|
||||
}
|
||||
else if (inf->blend >= BLEND_DESATURATE1 && inf->blend<=BLEND_DESATURATE31)
|
||||
{
|
||||
// Desaturated light settings.
|
||||
fac=inf->blend-BLEND_DESATURATE1+1;
|
||||
for(i=0;i<count;i++)
|
||||
{
|
||||
a = TSrc::A(pin, tr, tg, tb);
|
||||
if (TBlend::ProcessAlpha0() || a)
|
||||
{
|
||||
gray = TSrc::Gray(pin);
|
||||
r = (TSrc::R(pin)*(31-fac) + gray*fac)/31;
|
||||
g = (TSrc::G(pin)*(31-fac) + gray*fac)/31;
|
||||
b = (TSrc::B(pin)*(31-fac) + gray*fac)/31;
|
||||
|
||||
TBlend::OpC(pout[TDest::RED], r, a, inf);
|
||||
TBlend::OpC(pout[TDest::GREEN], g, a, inf);
|
||||
TBlend::OpC(pout[TDest::BLUE], b, a, inf);
|
||||
TBlend::OpA(pout[TDest::ALPHA], a, inf);
|
||||
}
|
||||
pout+=4;
|
||||
pin+=step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLEND_MODULATE:
|
||||
for(i=0;i<count;i++)
|
||||
{
|
||||
a = TSrc::A(pin, tr, tg, tb);
|
||||
if (TBlend::ProcessAlpha0() || a)
|
||||
{
|
||||
r = (TSrc::R(pin)*inf->blendcolor[0])>>BLENDBITS;
|
||||
g = (TSrc::G(pin)*inf->blendcolor[1])>>BLENDBITS;
|
||||
b = (TSrc::B(pin)*inf->blendcolor[2])>>BLENDBITS;
|
||||
|
||||
TBlend::OpC(pout[TDest::RED], r, a, inf);
|
||||
TBlend::OpC(pout[TDest::GREEN], g, a, inf);
|
||||
TBlend::OpC(pout[TDest::BLUE], b, a, inf);
|
||||
TBlend::OpA(pout[TDest::ALPHA], a, inf);
|
||||
}
|
||||
pout+=4;
|
||||
pin+=step;
|
||||
}
|
||||
break;
|
||||
|
||||
case BLEND_OVERLAY:
|
||||
for(i=0;i<count;i++)
|
||||
{
|
||||
// color blend
|
||||
a = TSrc::A(pin, tr, tg, tb);
|
||||
if (TBlend::ProcessAlpha0() || a)
|
||||
{
|
||||
r = (TSrc::R(pin)*inf->blendcolor[3] + inf->blendcolor[0]) >> BLENDBITS;
|
||||
g = (TSrc::G(pin)*inf->blendcolor[3] + inf->blendcolor[1]) >> BLENDBITS;
|
||||
b = (TSrc::B(pin)*inf->blendcolor[3] + inf->blendcolor[2]) >> BLENDBITS;
|
||||
|
||||
TBlend::OpC(pout[TDest::RED], r, a, inf);
|
||||
TBlend::OpC(pout[TDest::GREEN], g, a, inf);
|
||||
TBlend::OpC(pout[TDest::BLUE], b, a, inf);
|
||||
TBlend::OpA(pout[TDest::ALPHA], a, inf);
|
||||
}
|
||||
pout+=4;
|
||||
pin+=step;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
typedef void (*CopyFunc)(uint8_t *pout, const uint8_t *pin, int count, int step, FCopyInfo *inf, uint8_t r, uint8_t g, uint8_t b);
|
||||
|
||||
#define COPY_FUNCS(op) \
|
||||
{ \
|
||||
iCopyColors<cRGB, cBGRA, op>, \
|
||||
iCopyColors<cRGBT, cBGRA, op>, \
|
||||
iCopyColors<cRGBA, cBGRA, op>, \
|
||||
iCopyColors<cIA, cBGRA, op>, \
|
||||
iCopyColors<cCMYK, cBGRA, op>, \
|
||||
iCopyColors<cYCbCr, cBGRA, op>, \
|
||||
iCopyColors<cBGR, cBGRA, op>, \
|
||||
iCopyColors<cBGRA, cBGRA, op>, \
|
||||
iCopyColors<cI16, cBGRA, op>, \
|
||||
iCopyColors<cRGB555, cBGRA, op>, \
|
||||
iCopyColors<cPalEntry, cBGRA, op> \
|
||||
}
|
||||
static const CopyFunc copyfuncs[][11]={
|
||||
COPY_FUNCS(bCopy),
|
||||
COPY_FUNCS(bBlend),
|
||||
COPY_FUNCS(bAdd),
|
||||
COPY_FUNCS(bSubtract),
|
||||
COPY_FUNCS(bReverseSubtract),
|
||||
COPY_FUNCS(bModulate),
|
||||
COPY_FUNCS(bCopyAlpha),
|
||||
COPY_FUNCS(bCopyNewAlpha),
|
||||
COPY_FUNCS(bOverlay),
|
||||
COPY_FUNCS(bOverwrite)
|
||||
};
|
||||
#undef COPY_FUNCS
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// Clips the copy area for CopyPixelData functions
|
||||
//
|
||||
//===========================================================================
|
||||
bool ClipCopyPixelRect(const FClipRect *cr, int &originx, int &originy,
|
||||
const uint8_t *&patch, int &srcwidth, int &srcheight,
|
||||
int &pstep_x, int &pstep_y, int rotate)
|
||||
{
|
||||
int pixxoffset;
|
||||
int pixyoffset;
|
||||
|
||||
int step_x;
|
||||
int step_y;
|
||||
|
||||
assert(cr != NULL);
|
||||
// First adjust the settings for the intended rotation
|
||||
switch (rotate)
|
||||
{
|
||||
default:
|
||||
case 0: // normal
|
||||
pixxoffset = 0;
|
||||
pixyoffset = 0;
|
||||
step_x = pstep_x;
|
||||
step_y = pstep_y;
|
||||
break;
|
||||
|
||||
case 1: // rotate 90° right
|
||||
pixxoffset = 0;
|
||||
pixyoffset = srcheight - 1;
|
||||
step_x = -pstep_y;
|
||||
step_y = pstep_x;
|
||||
break;
|
||||
|
||||
case 2: // rotate 180°
|
||||
pixxoffset = srcwidth - 1;
|
||||
pixyoffset = srcheight - 1;
|
||||
step_x = -pstep_x;
|
||||
step_y = -pstep_y;
|
||||
break;
|
||||
|
||||
case 3: // rotate 90° left
|
||||
pixxoffset = srcwidth - 1;
|
||||
pixyoffset = 0;
|
||||
step_x = pstep_y;
|
||||
step_y = -pstep_x;
|
||||
break;
|
||||
|
||||
case 4: // flip horizontally
|
||||
pixxoffset = srcwidth - 1;
|
||||
pixyoffset = 0;
|
||||
step_x = -pstep_x;
|
||||
step_y = pstep_y;
|
||||
break;
|
||||
|
||||
case 5: // flip horizontally and rotate 90° right
|
||||
pixxoffset = srcwidth - 1;
|
||||
pixyoffset = srcheight - 1;
|
||||
step_x = -pstep_y;
|
||||
step_y = -pstep_x;
|
||||
break;
|
||||
|
||||
case 6: // flip vertically
|
||||
pixxoffset = 0;
|
||||
pixyoffset = srcheight - 1;
|
||||
step_x = pstep_x;
|
||||
step_y = -pstep_y;
|
||||
break;
|
||||
|
||||
case 7: // flip horizontally and rotate 90° left
|
||||
pixxoffset = 0;
|
||||
pixyoffset = 0;
|
||||
step_x = pstep_y;
|
||||
step_y = pstep_x;
|
||||
break;
|
||||
}
|
||||
if (rotate&1)
|
||||
{
|
||||
int v = srcwidth;
|
||||
srcwidth = srcheight;
|
||||
srcheight = v;
|
||||
}
|
||||
|
||||
patch += pixxoffset * pstep_x + pixyoffset * pstep_y;
|
||||
pstep_x = step_x;
|
||||
pstep_y = step_y;
|
||||
|
||||
// clip source rectangle to destination
|
||||
if (originx < cr->x)
|
||||
{
|
||||
int skip = cr->x - originx;
|
||||
|
||||
srcwidth -= skip;
|
||||
patch +=skip * step_x;
|
||||
originx = cr->x;
|
||||
if (srcwidth<=0) return false;
|
||||
}
|
||||
if (originx + srcwidth > cr->x + cr->width)
|
||||
{
|
||||
srcwidth = cr->x + cr->width - originx;
|
||||
if (srcwidth<=0) return false;
|
||||
}
|
||||
|
||||
if (originy < cr->y)
|
||||
{
|
||||
int skip = cr->y - originy;
|
||||
|
||||
srcheight -= skip;
|
||||
patch += skip*step_y;
|
||||
originy = cr->y;
|
||||
if (srcheight <= 0) return false;
|
||||
}
|
||||
if (originy + srcheight > cr->y + cr->height)
|
||||
{
|
||||
srcheight = cr->y + cr->height - originy;
|
||||
if (srcheight <= 0) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//===========================================================================
|
||||
|
||||
bool FClipRect::Intersect(int ix, int iy, int iw, int ih)
|
||||
{
|
||||
if (ix > x)
|
||||
{
|
||||
width -= (ix-x);
|
||||
x = ix;
|
||||
}
|
||||
else
|
||||
{
|
||||
iw -= (x-ix);
|
||||
}
|
||||
if (iy > y)
|
||||
{
|
||||
height -= (iy-y);
|
||||
y = iy;
|
||||
}
|
||||
else
|
||||
{
|
||||
ih -= (y-iy);
|
||||
}
|
||||
if (iw < width) width = iw;
|
||||
if (ih < height) height = ih;
|
||||
return width > 0 && height > 0;
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// True Color texture copy function
|
||||
//
|
||||
//===========================================================================
|
||||
void FBitmap::CopyPixelDataRGB(int originx, int originy, const uint8_t *patch, int srcwidth,
|
||||
int srcheight, int step_x, int step_y, int rotate, int ct, FCopyInfo *inf,
|
||||
int r, int g, int b)
|
||||
{
|
||||
if (ClipCopyPixelRect(&ClipRect, originx, originy, patch, srcwidth, srcheight, step_x, step_y, rotate))
|
||||
{
|
||||
uint8_t *buffer = data + 4 * originx + Pitch * originy;
|
||||
int op = inf==NULL? OP_COPY : inf->op;
|
||||
for (int y=0;y<srcheight;y++)
|
||||
{
|
||||
copyfuncs[op][ct](&buffer[y*Pitch], &patch[y*step_y], srcwidth, step_x, inf, r, g, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class TDest, class TBlend>
|
||||
void iCopyPaletted(uint8_t *buffer, const uint8_t * patch, int srcwidth, int srcheight, int Pitch,
|
||||
int step_x, int step_y, int rotate, const PalEntry * palette, FCopyInfo *inf)
|
||||
{
|
||||
int x,y,pos;
|
||||
|
||||
for (y=0;y<srcheight;y++)
|
||||
{
|
||||
pos = y*Pitch;
|
||||
for (x=0;x<srcwidth;x++,pos+=4)
|
||||
{
|
||||
int v=(unsigned char)patch[y*step_y+x*step_x];
|
||||
int a = palette[v].a;
|
||||
|
||||
if (TBlend::ProcessAlpha0() || a)
|
||||
{
|
||||
TBlend::OpC(buffer[pos + TDest::RED], palette[v].r, a, inf);
|
||||
TBlend::OpC(buffer[pos + TDest::GREEN], palette[v].g, a, inf);
|
||||
TBlend::OpC(buffer[pos + TDest::BLUE], palette[v].b, a, inf);
|
||||
TBlend::OpA(buffer[pos + TDest::ALPHA], a, inf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef void (*CopyPalettedFunc)(uint8_t *buffer, const uint8_t * patch, int srcwidth, int srcheight, int Pitch,
|
||||
int step_x, int step_y, int rotate, const PalEntry * palette, FCopyInfo *inf);
|
||||
|
||||
|
||||
static const CopyPalettedFunc copypalettedfuncs[]=
|
||||
{
|
||||
iCopyPaletted<cBGRA, bCopy>,
|
||||
iCopyPaletted<cBGRA, bBlend>,
|
||||
iCopyPaletted<cBGRA, bAdd>,
|
||||
iCopyPaletted<cBGRA, bSubtract>,
|
||||
iCopyPaletted<cBGRA, bReverseSubtract>,
|
||||
iCopyPaletted<cBGRA, bModulate>,
|
||||
iCopyPaletted<cBGRA, bCopyAlpha>,
|
||||
iCopyPaletted<cBGRA, bCopyNewAlpha>,
|
||||
iCopyPaletted<cBGRA, bOverlay>,
|
||||
iCopyPaletted<cBGRA, bOverwrite>
|
||||
};
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// Paletted to True Color texture copy function
|
||||
//
|
||||
//===========================================================================
|
||||
void FBitmap::CopyPixelData(int originx, int originy, const uint8_t * patch, int srcwidth, int srcheight,
|
||||
int step_x, int step_y, int rotate, const PalEntry * palette, FCopyInfo *inf)
|
||||
{
|
||||
if (ClipCopyPixelRect(&ClipRect, originx, originy, patch, srcwidth, srcheight, step_x, step_y, rotate))
|
||||
{
|
||||
uint8_t *buffer = data + 4*originx + Pitch*originy;
|
||||
PalEntry penew[256];
|
||||
|
||||
memset(penew, 0, sizeof(penew));
|
||||
if (inf)
|
||||
{
|
||||
if (inf->blend)
|
||||
{
|
||||
iCopyColors<cPalEntry, cBGRA, bCopy>((uint8_t*)penew, (const uint8_t*)palette, 256, 4, inf, 0, 0, 0);
|
||||
palette = penew;
|
||||
}
|
||||
else if (inf->palette)
|
||||
{
|
||||
palette = inf->palette;
|
||||
}
|
||||
}
|
||||
|
||||
copypalettedfuncs[inf==NULL? OP_COPY : inf->op](buffer, patch, srcwidth, srcheight, Pitch,
|
||||
step_x, step_y, rotate, palette, inf);
|
||||
}
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// Clear buffer
|
||||
//
|
||||
//===========================================================================
|
||||
void FBitmap::Zero()
|
||||
{
|
||||
uint8_t *buffer = data;
|
||||
for (int y = ClipRect.y; y < ClipRect.height; ++y)
|
||||
{
|
||||
memset(buffer + ClipRect.x, 0, ClipRect.width*4);
|
||||
buffer += Pitch;
|
||||
}
|
||||
}
|
||||
498
src/common/textures/bitmap.h
Normal file
498
src/common/textures/bitmap.h
Normal file
|
|
@ -0,0 +1,498 @@
|
|||
/*
|
||||
** bitmap.h
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2008 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __BITMAP_H__
|
||||
#define __BITMAP_H__
|
||||
|
||||
#include "basics.h"
|
||||
#include "templates.h"
|
||||
#include "palentry.h"
|
||||
|
||||
struct FCopyInfo;
|
||||
|
||||
struct FClipRect
|
||||
{
|
||||
int x, y, width, height;
|
||||
|
||||
bool Intersect(int ix, int iy, int iw, int ih);
|
||||
};
|
||||
|
||||
typedef int blend_t;
|
||||
|
||||
enum
|
||||
{
|
||||
BLENDBITS = 16,
|
||||
BLENDUNIT = (1<<BLENDBITS)
|
||||
};
|
||||
|
||||
enum ColorType
|
||||
{
|
||||
CF_RGB,
|
||||
CF_RGBT,
|
||||
CF_RGBA,
|
||||
CF_IA,
|
||||
CF_CMYK,
|
||||
CF_YCbCr,
|
||||
CF_BGR,
|
||||
CF_BGRA,
|
||||
CF_I16,
|
||||
CF_RGB555,
|
||||
CF_PalEntry
|
||||
};
|
||||
|
||||
|
||||
class FBitmap
|
||||
{
|
||||
protected:
|
||||
uint8_t *data;
|
||||
int Width;
|
||||
int Height;
|
||||
int Pitch;
|
||||
bool FreeBuffer;
|
||||
FClipRect ClipRect;
|
||||
|
||||
|
||||
public:
|
||||
FBitmap()
|
||||
{
|
||||
data = NULL;
|
||||
Width = Height = 0;
|
||||
Pitch = 0;
|
||||
FreeBuffer = false;
|
||||
ClipRect.x = ClipRect.y = ClipRect.width = ClipRect.height = 0;
|
||||
}
|
||||
|
||||
FBitmap(uint8_t *buffer, int pitch, int width, int height)
|
||||
{
|
||||
data = buffer;
|
||||
|
||||
Pitch = pitch;
|
||||
Width = width;
|
||||
Height = height;
|
||||
FreeBuffer = false;
|
||||
ClipRect.x = ClipRect.y = 0;
|
||||
ClipRect.width = width;
|
||||
ClipRect.height = height;
|
||||
}
|
||||
|
||||
FBitmap(const FBitmap &other) = delete; // disallow because in nearly all cases this creates an unwanted copy.
|
||||
|
||||
FBitmap(FBitmap &&other)
|
||||
{
|
||||
data = other.data;
|
||||
Pitch = other.Pitch;
|
||||
Width = other.Width;
|
||||
Height = other.Height;
|
||||
FreeBuffer = other.FreeBuffer;
|
||||
ClipRect = other.ClipRect;
|
||||
other.data = nullptr;
|
||||
other.FreeBuffer = false;
|
||||
}
|
||||
|
||||
FBitmap &operator=(const FBitmap &other) = delete; // disallow because in nearly all cases this creates an unwanted copy. Use Copy instead.
|
||||
|
||||
FBitmap &operator=(FBitmap &&other)
|
||||
{
|
||||
if (data != nullptr && FreeBuffer) delete[] data;
|
||||
data = other.data;
|
||||
Pitch = other.Pitch;
|
||||
Width = other.Width;
|
||||
Height = other.Height;
|
||||
FreeBuffer = other.FreeBuffer;
|
||||
ClipRect = other.ClipRect;
|
||||
other.data = nullptr;
|
||||
other.FreeBuffer = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Copy(const FBitmap &other, bool deep = true)
|
||||
{
|
||||
if (data != nullptr && FreeBuffer) delete[] data;
|
||||
Pitch = other.Pitch;
|
||||
Width = other.Width;
|
||||
Height = other.Height;
|
||||
FreeBuffer = deep;
|
||||
ClipRect = other.ClipRect;
|
||||
if (deep)
|
||||
{
|
||||
data = new uint8_t[Pitch * Height];
|
||||
memcpy(data, other.data, Pitch * Height);
|
||||
}
|
||||
else
|
||||
{
|
||||
data = other.data;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
~FBitmap()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
void Destroy()
|
||||
{
|
||||
if (data != NULL && FreeBuffer) delete [] data;
|
||||
data = NULL;
|
||||
FreeBuffer = false;
|
||||
}
|
||||
|
||||
bool Create (int w, int h)
|
||||
{
|
||||
Pitch = w*4;
|
||||
Width = w;
|
||||
Height = h;
|
||||
data = new uint8_t[4*w*h];
|
||||
memset(data, 0, 4*w*h);
|
||||
FreeBuffer = true;
|
||||
ClipRect.x = ClipRect.y = 0;
|
||||
ClipRect.width = w;
|
||||
ClipRect.height = h;
|
||||
return data != NULL;
|
||||
}
|
||||
|
||||
int GetHeight() const
|
||||
{
|
||||
return Height;
|
||||
}
|
||||
|
||||
int GetWidth() const
|
||||
{
|
||||
return Width;
|
||||
}
|
||||
|
||||
int GetPitch() const
|
||||
{
|
||||
return Pitch;
|
||||
}
|
||||
|
||||
const uint8_t *GetPixels() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
uint8_t *GetPixels()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
void SetClipRect(FClipRect &clip)
|
||||
{
|
||||
ClipRect = clip;
|
||||
}
|
||||
|
||||
void IntersectClipRect(FClipRect &clip)
|
||||
{
|
||||
ClipRect.Intersect(clip.x, clip.y, clip.width, clip.height);
|
||||
}
|
||||
|
||||
void IntersectClipRect(int cx, int cy, int cw, int ch)
|
||||
{
|
||||
ClipRect.Intersect(cx, cy, cw, ch);
|
||||
}
|
||||
|
||||
const FClipRect &GetClipRect() const
|
||||
{
|
||||
return ClipRect;
|
||||
}
|
||||
|
||||
void Zero();
|
||||
|
||||
|
||||
void CopyPixelDataRGB(int originx, int originy, const uint8_t *patch, int srcwidth,
|
||||
int srcheight, int step_x, int step_y, int rotate, int ct, FCopyInfo *inf = NULL,
|
||||
/* for PNG tRNS */ int r=0, int g=0, int b=0);
|
||||
void CopyPixelData(int originx, int originy, const uint8_t * patch, int srcwidth, int srcheight,
|
||||
int step_x, int step_y, int rotate, const PalEntry * palette, FCopyInfo *inf = NULL);
|
||||
|
||||
|
||||
void Blit(int originx, int originy, const FBitmap &src, int width, int height, int rotate = 0, FCopyInfo *inf = NULL)
|
||||
{
|
||||
CopyPixelDataRGB(originx, originy, src.GetPixels(), width, height, 4, src.GetWidth()*4, rotate, CF_BGRA, inf);
|
||||
}
|
||||
|
||||
void Blit(int originx, int originy, const FBitmap &src, FCopyInfo *inf = NULL)
|
||||
{
|
||||
CopyPixelDataRGB(originx, originy, src.GetPixels(), src.GetWidth(), src.GetHeight(), 4, src.GetWidth()*4, 0, CF_BGRA, inf);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
bool ClipCopyPixelRect(const FClipRect *cr, int &originx, int &originy,
|
||||
const uint8_t *&patch, int &srcwidth, int &srcheight,
|
||||
int &step_x, int &step_y, int rotate);
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// True color conversion classes for the different pixel formats
|
||||
// used by the supported texture formats
|
||||
//
|
||||
//===========================================================================
|
||||
struct cRGB
|
||||
{
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return p[0]; }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return p[1]; }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return p[2]; }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return 255; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (p[0]*77 + p[1]*143 + p[2]*36)>>8; }
|
||||
};
|
||||
|
||||
struct cRGBT
|
||||
{
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return p[0]; }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return p[1]; }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return p[2]; }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t r, uint8_t g, uint8_t b) { return (p[0] != r || p[1] != g || p[2] != b) ? 255 : 0; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (p[0]*77 + p[1]*143 + p[2]*36)>>8; }
|
||||
};
|
||||
|
||||
struct cRGBA
|
||||
{
|
||||
enum
|
||||
{
|
||||
RED = 0,
|
||||
GREEN = 1,
|
||||
BLUE = 1,
|
||||
ALPHA = 3
|
||||
};
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return p[0]; }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return p[1]; }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return p[2]; }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return p[3]; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (p[0]*77 + p[1]*143 + p[2]*36)>>8; }
|
||||
};
|
||||
|
||||
struct cIA
|
||||
{
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return p[0]; }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return p[0]; }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return p[0]; }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return p[1]; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return p[0]; }
|
||||
};
|
||||
|
||||
struct cCMYK
|
||||
{
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return p[3] - (((256-p[0])*p[3]) >> 8); }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return p[3] - (((256-p[1])*p[3]) >> 8); }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return p[3] - (((256-p[2])*p[3]) >> 8); }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return 255; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (R(p)*77 + G(p)*143 + B(p)*36)>>8; }
|
||||
};
|
||||
|
||||
struct cYCbCr
|
||||
{
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return clamp((int)(p[0] + 1.40200 * (int(p[2]) - 0x80)), 0, 255); }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return clamp((int)(p[0] - 0.34414 * (int(p[1] - 0x80)) - 0.71414 * (int(p[2]) - 0x80)), 0, 255); }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return clamp((int)(p[0] + 1.77200 * (int(p[1]) - 0x80)), 0, 255); }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return 255; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (R(p) * 77 + G(p) * 143 + B(p) * 36) >> 8; }
|
||||
};
|
||||
|
||||
struct cBGR
|
||||
{
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return p[2]; }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return p[1]; }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return p[0]; }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return 255; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (p[2]*77 + p[1]*143 + p[0]*36)>>8; }
|
||||
};
|
||||
|
||||
struct cBGRA
|
||||
{
|
||||
enum
|
||||
{
|
||||
RED = 2,
|
||||
GREEN = 1,
|
||||
BLUE = 0,
|
||||
ALPHA = 3
|
||||
};
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return p[2]; }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return p[1]; }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return p[0]; }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return p[3]; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (p[2]*77 + p[1]*143 + p[0]*36)>>8; }
|
||||
};
|
||||
|
||||
struct cARGB
|
||||
{
|
||||
enum
|
||||
{
|
||||
RED = 1,
|
||||
GREEN = 2,
|
||||
BLUE = 3,
|
||||
ALPHA = 0
|
||||
};
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return p[1]; }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return p[2]; }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return p[3]; }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return p[0]; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (p[1]*77 + p[2]*143 + p[3]*36)>>8; }
|
||||
};
|
||||
|
||||
struct cI16
|
||||
{
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return p[1]; }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return p[1]; }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return p[1]; }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return 255; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return p[1]; }
|
||||
};
|
||||
|
||||
struct cRGB555
|
||||
{
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return (((*(uint16_t*)p)&0x1f)<<3); }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return (((*(uint16_t*)p)&0x3e0)>>2); }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return (((*(uint16_t*)p)&0x7c00)>>7); }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return 255; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (R(p)*77 + G(p)*143 + B(p)*36)>>8; }
|
||||
};
|
||||
|
||||
struct cPalEntry
|
||||
{
|
||||
static __forceinline unsigned char R(const unsigned char * p) { return ((PalEntry*)p)->r; }
|
||||
static __forceinline unsigned char G(const unsigned char * p) { return ((PalEntry*)p)->g; }
|
||||
static __forceinline unsigned char B(const unsigned char * p) { return ((PalEntry*)p)->b; }
|
||||
static __forceinline unsigned char A(const unsigned char * p, uint8_t x, uint8_t y, uint8_t z) { return ((PalEntry*)p)->a; }
|
||||
static __forceinline int Gray(const unsigned char * p) { return (R(p)*77 + G(p)*143 + B(p)*36)>>8; }
|
||||
};
|
||||
|
||||
enum EBlend
|
||||
{
|
||||
BLEND_NONE = 0,
|
||||
BLEND_ICEMAP = 1,
|
||||
BLEND_DESATURATE1 = 2,
|
||||
BLEND_DESATURATE31 = 32,
|
||||
BLEND_SPECIALCOLORMAP1 = 33,
|
||||
BLEND_MODULATE = -1,
|
||||
BLEND_OVERLAY = -2,
|
||||
};
|
||||
|
||||
enum ECopyOp
|
||||
{
|
||||
OP_COPY,
|
||||
OP_BLEND,
|
||||
OP_ADD,
|
||||
OP_SUBTRACT,
|
||||
OP_REVERSESUBTRACT,
|
||||
OP_MODULATE,
|
||||
OP_COPYALPHA,
|
||||
OP_COPYNEWALPHA,
|
||||
OP_OVERLAY,
|
||||
OP_OVERWRITE
|
||||
};
|
||||
|
||||
struct FCopyInfo
|
||||
{
|
||||
ECopyOp op;
|
||||
EBlend blend;
|
||||
blend_t blendcolor[4];
|
||||
blend_t alpha;
|
||||
blend_t invalpha;
|
||||
PalEntry *palette;
|
||||
};
|
||||
|
||||
struct bOverwrite
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = s; }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = s; }
|
||||
static __forceinline bool ProcessAlpha0() { return true; }
|
||||
};
|
||||
|
||||
struct bCopy
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = s; }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = s; }
|
||||
static __forceinline bool ProcessAlpha0() { return false; }
|
||||
};
|
||||
|
||||
struct bCopyNewAlpha
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = s; }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = (s*i->alpha) >> BLENDBITS; }
|
||||
static __forceinline bool ProcessAlpha0() { return false; }
|
||||
};
|
||||
|
||||
struct bCopyAlpha
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = (s*a + d*(255-a))/255; }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = s; }
|
||||
static __forceinline bool ProcessAlpha0() { return false; }
|
||||
};
|
||||
|
||||
struct bOverlay
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = (s*a + d*(255-a))/255; }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = MAX(s,d); }
|
||||
static __forceinline bool ProcessAlpha0() { return false; }
|
||||
};
|
||||
|
||||
struct bBlend
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = (d*i->invalpha + s*i->alpha) >> BLENDBITS; }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = s; }
|
||||
static __forceinline bool ProcessAlpha0() { return false; }
|
||||
};
|
||||
|
||||
struct bAdd
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = MIN<int>((d*BLENDUNIT + s*i->alpha) >> BLENDBITS, 255); }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = s; }
|
||||
static __forceinline bool ProcessAlpha0() { return false; }
|
||||
};
|
||||
|
||||
struct bSubtract
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = MAX<int>((d*BLENDUNIT - s*i->alpha) >> BLENDBITS, 0); }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = s; }
|
||||
static __forceinline bool ProcessAlpha0() { return false; }
|
||||
};
|
||||
|
||||
struct bReverseSubtract
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = MAX<int>((-d*BLENDUNIT + s*i->alpha) >> BLENDBITS, 0); }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = s; }
|
||||
static __forceinline bool ProcessAlpha0() { return false; }
|
||||
};
|
||||
|
||||
struct bModulate
|
||||
{
|
||||
static __forceinline void OpC(uint8_t &d, uint8_t s, uint8_t a, FCopyInfo *i) { d = (s*d)/255; }
|
||||
static __forceinline void OpA(uint8_t &d, uint8_t s, FCopyInfo *i) { d = s; }
|
||||
static __forceinline bool ProcessAlpha0() { return false; }
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -28,9 +28,6 @@
|
|||
#include "texturemanager.h"
|
||||
#include "c_cvars.h"
|
||||
|
||||
EXTERN_CVAR(Bool, gl_texture_usehires)
|
||||
IHardwareTexture* CreateHardwareTexture();
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// Constructor
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include "tarray.h"
|
||||
#include "textures/bitmap.h"
|
||||
#include "bitmap.h"
|
||||
#include "memarena.h"
|
||||
|
||||
class FImageSource;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue