minor update

This commit is contained in:
Blaise Tine
2021-04-01 19:42:53 -04:00
parent 387fe55610
commit 8add5efe66
7 changed files with 96 additions and 25 deletions

View File

@@ -1,2 +1,3 @@
main.o: main.cpp ../../include/vortex.h common.h utils.h main.o: main.cpp ../../include/vortex.h common.h utils.h blitter.h \
utils.o: utils.cpp utils.h format.h int24.h color.h
utils.o: utils.cpp utils.h blitter.h format.h int24.h color.h

View File

@@ -61,4 +61,11 @@ struct ColorARGB {
void operator=(const ColorARGB &rhs) { void operator=(const ColorARGB &rhs) {
this->value = rhs.value; this->value = rhs.value;
} }
uint32_t toRGBA() const {
// swap B/R channels
return (this->value & 0xff00ff00)
| ((this->value >> 16) & 0xff)
| ((this->value & 0xff) << 16);
}
}; };

Binary file not shown.

View File

@@ -20,7 +20,7 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
const char* kernel_file = "kernel.bin"; const char* kernel_file = "kernel.bin";
const char* input_file = "palette16.tga"; const char* input_file = "palette64.tga";
const char* output_file = "output.tga"; const char* output_file = "output.tga";
int wrap = 0; int wrap = 0;
int filter = 0; int filter = 0;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 108 B

View File

@@ -47,30 +47,41 @@ int LoadTGA(const char *filename,
} }
switch (header.bitsperpixel) { switch (header.bitsperpixel) {
case 16:
case 24: case 24:
case 32: { case 32: {
auto stride = header.bitsperpixel / 8; auto stride = header.bitsperpixel / 8;
auto pitch = header.width * stride; std::vector<uint8_t> staging(stride * header.width * header.height);
pixels.resize(header.height * pitch);
// we are going to load the pixel data line by line // Read pixels data
for (int y = 0; y < header.height; ++y) { ifs.read((char*)staging.data(), staging.size());
// Read current line of pixels if (ifs.fail()) {
auto line = pixels.data() + y * pitch; std::cerr << "invalid TGA file!" << std::endl;
ifs.read(reinterpret_cast<char *>(line), pitch); return -1;
if (ifs.fail()) { }
std::cerr << "invalid TGA file!" << std::endl;
return -1;
}
// TGA uses BGR instead of RGB, we must swap RG components // format conversion to RGBA
if (stride >= 3) { pixels.resize(4 * header.width * header.height);
for (int i = 0; i < pitch; i += stride) { const uint8_t* src_bytes = staging.data();
auto tmp = line[i]; uint32_t* dst_bytes = (uint32_t*)pixels.data();
line[i] = line[i + 2]; for (const uint8_t* const src_end = src_bytes + staging.size();
line[i + 2] = tmp; src_bytes != src_end;
} src_bytes += stride) {
ColorARGB color;
switch (stride) {
case 2:
color = Format::ConvertFrom<FORMAT_A1R5G5B5, true>(src_bytes);
break;
case 3:
color = Format::ConvertFrom<FORMAT_R8G8B8, true>(src_bytes);
break;
case 4:
color = Format::ConvertFrom<FORMAT_A8R8G8B8, true>(src_bytes);
break;
default:
std::abort();
} }
*dst_bytes++ = color.toRGBA();
} }
break; break;
} }
@@ -81,7 +92,7 @@ int LoadTGA(const char *filename,
*width = header.width; *width = header.width;
*height = header.height; *height = header.height;
*bpp = header.bitsperpixel / 8; *bpp = 4;
return 0; return 0;
} }
@@ -111,14 +122,16 @@ int SaveTGA(const char *filename,
header.bitsperpixel = bpp * 8; header.bitsperpixel = bpp * 8;
header.imagedescriptor = 0; header.imagedescriptor = 0;
// write header
ofs.write(reinterpret_cast<char *>(&header), sizeof(tga_header_t)); ofs.write(reinterpret_cast<char *>(&header), sizeof(tga_header_t));
// write pixel data
uint32_t pitch = bpp * width; uint32_t pitch = bpp * width;
const uint8_t* pixel_bytes = pixels.data() + (height - 1) * pitch; const uint8_t* pixel_bytes = pixels.data() + (height - 1) * pitch;
for (uint32_t y = 0; y < height; ++y) { for (uint32_t y = 0; y < height; ++y) {
const uint8_t* pixel_row = pixel_bytes; const uint8_t* pixel_row = pixel_bytes;
for (uint32_t x = 0; x < width; ++x) { for (uint32_t x = 0; x < width; ++x) {
// TGA uses BGR instead of RGB, we must swap RG components // swap R/B color channels
if (bpp == 4) { if (bpp == 4) {
ofs.write((const char*)pixel_row + 2, 1); ofs.write((const char*)pixel_row + 2, 1);
ofs.write((const char*)pixel_row + 1, 1); ofs.write((const char*)pixel_row + 1, 1);
@@ -129,7 +142,8 @@ int SaveTGA(const char *filename,
ofs.write((const char*)pixel_row + 1, 1); ofs.write((const char*)pixel_row + 1, 1);
ofs.write((const char*)pixel_row + 0, 1); ofs.write((const char*)pixel_row + 0, 1);
} else{ } else{
ofs.write((const char*)pixel_row, bpp); std::cerr << "unsupported TGA bitsperpixel!" << std::endl;
return -1;
} }
pixel_row += bpp; pixel_row += bpp;
} }
@@ -155,3 +169,42 @@ void dump_image(const std::vector<uint8_t>& pixels, uint32_t width, uint32_t hei
std::cout << std::endl; std::cout << std::endl;
} }
} }
int CopyBuffers(const GLSurfaceDesc &dstDesc,
int32_t dstOffsetX,
int32_t dstOffsetY,
int32_t copyWidth,
int32_t copyHeight,
const GLSurfaceDesc &srcDesc,
int32_t srcOffsetX,
int32_t srcOffsetY) {
static const BlitTable s_blitTable;
if ((srcOffsetX >= srcDesc.Width) || (srcOffsetY >= srcDesc.Height) ||
(dstOffsetX >= dstDesc.Width) || (dstOffsetY >= dstDesc.Height)) {
return -1;
}
if (copyWidth > dstDesc.Width) {
copyWidth = dstDesc.Width;
}
if (copyWidth > srcDesc.Width) {
copyWidth = srcDesc.Width;
}
if (copyHeight > dstDesc.Height) {
copyHeight = dstDesc.Height;
}
if (copyHeight > srcDesc.Height) {
copyHeight = srcDesc.Height;
}
s_blitTable.get(srcDesc.Format, dstDesc.Format)(
dstDesc, dstOffsetX, dstOffsetY, copyWidth, copyHeight, srcDesc,
srcOffsetX, srcOffsetY);
return 0;
}

View File

@@ -1,6 +1,7 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include "blitter.h"
int LoadTGA(const char *filename, int LoadTGA(const char *filename,
std::vector<uint8_t> &pixels, std::vector<uint8_t> &pixels,
@@ -14,4 +15,13 @@ int SaveTGA(const char *filename,
uint32_t height, uint32_t height,
uint32_t bpp); uint32_t bpp);
int CopyBuffers(const GLSurfaceDesc &dstDesc,
int32_t dstOffsetX,
int32_t dstOffsetY,
int32_t copyWidth,
int32_t copyHeight,
const GLSurfaceDesc &srcDesc,
int32_t srcOffsetX,
int32_t srcOffsetY);
void dump_image(const std::vector<uint8_t>& pixels, uint32_t width, uint32_t height, uint32_t bpp); void dump_image(const std::vector<uint8_t>& pixels, uint32_t width, uint32_t height, uint32_t bpp);