Skip to content

Commit

Permalink
Move color conversion to textureconvert
Browse files Browse the repository at this point in the history
  • Loading branch information
cyxx committed Apr 23, 2018
1 parent d75ff60 commit b56216f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LDFLAGS += $(LTO)
SRCS = cabinet.cpp camera.cpp collision.cpp cutscene.cpp cutscenepsx.cpp decoder.cpp file.cpp \
font.cpp game.cpp icons.cpp input.cpp installer.cpp inventory.cpp main.cpp menu.cpp mixer.cpp \
opcodes.cpp raycast.cpp render.cpp resource.cpp resourcepsx.cpp saveload.cpp scaler.cpp \
screenshot.cpp sound.cpp spritecache.cpp stub.cpp texturecache.cpp \
screenshot.cpp sound.cpp spritecache.cpp stub.cpp texturecache.cpp textureconvert.cpp \
trigo.cpp util.cpp xmiplayer.cpp

OBJS = $(SRCS:.cpp=.o)
Expand Down
2 changes: 1 addition & 1 deletion Makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LIBS = $(FFMPEG_LIBS) $(SDL_LIBS) -lopengl32 -lWildMidi.dll -lfluidsynth.dll
SRCS = cabinet.cpp camera.cpp collision.cpp cutscene.cpp cutscenepsx.cpp decoder.cpp file.cpp \
font.cpp game.cpp icons.cpp input.cpp installer.cpp inventory.cpp main.cpp menu.cpp mixer.cpp \
opcodes.cpp raycast.cpp render.cpp resource.cpp resourcepsx.cpp saveload.cpp scaler.cpp \
screenshot.cpp sound.cpp spritecache.cpp stub.cpp texturecache.cpp \
screenshot.cpp sound.cpp spritecache.cpp stub.cpp texturecache.cpp textureconvert.cpp \
trigo.cpp util.cpp xmiplayer.cpp

OBJS = $(SRCS:.cpp=.o)
Expand Down
26 changes: 4 additions & 22 deletions cutscenepsx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "cutscenepsx.h"
#include "render.h"
#include "sound.h"
#include "textureconvert.h"

//
// FFmpeg
Expand Down Expand Up @@ -90,28 +91,6 @@ void DpsDecoder::initMdec(int w, int h) {
}
}

static uint32_t yuv420_to_rgba(int Y, int U, int V) {
float R = Y + 1.402 * (V - 128);
if (R < 0) {
R = 0;
} else if (R > 255) {
R = 255;
}
float G = Y - 0.344 * (U - 128) - 0.714 * (V - 128);
if (G < 0) {
G = 0;
} else if (G > 255) {
G = 255;
}
float B = Y + 1.772 * (U - 128);
if (B < 0) {
B = 0;
} else if (B > 255) {
B = 255;
}
return 0xFF000000 | (((uint8_t)B) << 16) | (((uint8_t)G) << 8) | ((uint8_t)R);
}

void DpsDecoder::decodeMdec(const uint8_t *data, int size) {
AVPacket pkt;
av_new_packet(&pkt, size);
Expand Down Expand Up @@ -258,6 +237,9 @@ bool CutscenePsx::readSector() {
}

bool CutscenePsx::play() {
if (!_fp) {
return false;
}
bool err = false;
// demux audio and video frames
uint8_t *videoData = 0;
Expand Down
8 changes: 3 additions & 5 deletions resourcepsx.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "resourcepsx.h"
#include "textureconvert.h"

static const char *_levels[] = {
"1", "2a", "2b", "2c", "3", "4a", "4b", "4c", "5a", "5b", "5c", "6a", "6b"
Expand Down Expand Up @@ -33,11 +34,8 @@ void ResourcePsx::loadLevelData(int level, int resType) {
for (int y = 0; y < kVrmLoadingScreenHeight; ++y) {
uint8_t *dst = _vrmLoadingBitmap + (kVrmLoadingScreenHeight - 1 - y) * dstPitch;
for (int x = 0; x < kVrmLoadingScreenWidth; ++x) {
const uint16_t color = fileReadUint16LE(fp);
*dst++ = ( color & 31) << 3;
*dst++ = ((color >> 5) & 31) << 3;
*dst++ = ((color >> 10) & 31) << 3;
*dst++ = 255;
const uint32_t color = bgr555_to_rgba(fileReadUint16LE(fp));
memcpy(dst + x * 4, &color, 4);
}
}
h -= kVrmLoadingScreenHeight;
Expand Down
15 changes: 4 additions & 11 deletions texturecache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,18 @@
#include "scaler.h"
#include "texturecache.h"

static const int kDefaultTexBufSize = 320 * 200;
static const int kLutTextureBufferSize = 320 * 200;

uint16_t convert_RGBA_5551(int r, int g, int b) {
static uint16_t convert_RGBA_5551(int r, int g, int b) {
return ((r >> 3) << 11) | ((g >> 3) << 6) | ((b >> 3) << 1) | 1;
}

uint16_t convert_BGRA_1555(int r, int g, int b) {
return 0x8000 | ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
}

static const struct {
int internal;
int format;
int type;
uint16_t (*convertColor)(int, int, int);
} _formats[] = {
#ifdef __amigaos4__
{ GL_RGB5_A1, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, &convert_BGRA_1555 },
#endif
#ifdef USE_GLES
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, &convert_RGBA_5551 },
#else
Expand Down Expand Up @@ -107,7 +100,7 @@ void TextureCache::init(const char *filter, const char *scaler) {
}
}
if (_scalers[_scaler].factor != 1) {
_texBuf = (uint16_t *)malloc(kDefaultTexBufSize * sizeof(uint16_t));
_texBuf = (uint16_t *)malloc(kLutTextureBufferSize * sizeof(uint16_t));
}
}

Expand Down Expand Up @@ -186,7 +179,7 @@ void TextureCache::convertTexture(const uint8_t *src, int w, int h, const uint16
dst += dstPitch;
}
} else {
assert(w * h <= kDefaultTexBufSize);
assert(w * h <= kLutTextureBufferSize);
int offset = 0;
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
Expand Down
31 changes: 31 additions & 0 deletions textureconvert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#include "textureconvert.h"

uint32_t yuv420_to_rgba(int Y, int U, int V) {
float R = Y + 1.402 * (V - 128);
if (R < 0) {
R = 0;
} else if (R > 255) {
R = 255;
}
float G = Y - 0.344 * (U - 128) - 0.714 * (V - 128);
if (G < 0) {
G = 0;
} else if (G > 255) {
G = 255;
}
float B = Y + 1.772 * (U - 128);
if (B < 0) {
B = 0;
} else if (B > 255) {
B = 255;
}
return 0xFF000000 | (((uint8_t)B) << 16) | (((uint8_t)G) << 8) | ((uint8_t)R);
}

uint32_t bgr555_to_rgba(uint16_t color) {
const uint8_t r = ( color & 31) << 3;
const uint8_t g = ((color >> 5) & 31) << 3;
const uint8_t b = ((color >> 10) & 31) << 3;
return 0xFF000000 | (b << 16) | (g << 8) | r;
}
10 changes: 10 additions & 0 deletions textureconvert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#ifndef TEXTURECONVERT_H__
#define TEXTURECONVERT_H__

#include "util.h"

uint32_t yuv420_to_rgba(int Y, int U, int V);
uint32_t bgr555_to_rgba(uint16_t color);

#endif // TEXTURECONVERT_H__

0 comments on commit b56216f

Please sign in to comment.