image utils refactoring

This commit is contained in:
Blaise Tine 2022-07-01 16:51:49 -04:00
parent 2f9a1d747a
commit 935eb88d03
17 changed files with 18 additions and 836 deletions

View file

@ -119,16 +119,14 @@ graphics()
echo "begin graphics tests..."
CONFIGS="-DEXT_IMADD_ENABLE" ./ci/blackbox.sh --driver=simx --app=imadd
CONFIGS="-DEXT_IMADD_ENABLE" ./ci/blackbox.sh --driver=rtlsim --app=imadd
CONFIGS="-DEXT_IMADD_ENABLE" ./ci/blackbox.sh --driver=rtlsim --app=imadd --args="-n32 -z"
CONFIGS="-DEXT_GFX_ENABLE -DL1_DISABLE -DSM_DISABLE -DTCACHE_DISABLE -DRCACHE_DISABLE -DOCACHE_DISABLE" ./ci/blackbox.sh --driver=simx --app=draw3d --args="-tbox.cgltrace -rbox_ref_128.png" --clusters=2 --cores=2 --warps=1 --threads=2
CONFIGS="-DEXT_GFX_ENABLE -DL1_DISABLE -DSM_DISABLE -DTCACHE_DISABLE -DRCACHE_DISABLE -DOCACHE_DISABLE" ./ci/blackbox.sh --driver=rtlsim --app=draw3d --args="-tbox.cgltrace -rbox_ref_128.png" --clusters=2 --cores=2 --warps=1 --threads=2
CONFIGS="-DEXT_GFX_ENABLE" ./ci/blackbox.sh --driver=simx --app=draw3d --args="-ttriangle.cgltrace -rtriangle_ref.png"
CONFIGS="-DEXT_GFX_ENABLE" ./ci/blackbox.sh --driver=rtlsim --app=draw3d --args="-ttriangle.cgltrace -rtriangle_ref.png" --cores=2 --warps=2 --threads=2
CONFIGS="-DEXT_GFX_ENABLE" ./ci/blackbox.sh --driver=rtlsim --app=draw3d --args="-ttriangle.cgltrace -rtriangle_ref_8.png -w8 -h8"
CONFIGS="-DEXT_GFX_ENABLE" ./ci/blackbox.sh --driver=vlsim --app=draw3d --args="-ttriangle.cgltrace -rtriangle_ref_8.png -w8 -h8" --warps=1 --threads=1 --debug=3
CONFIGS="-DEXT_GFX_ENABLE -DL1_DISABLE -DSM_DISABLE -DTCACHE_DISABLE -DRCACHE_DISABLE -DOCACHE_DISABLE" ./ci/blackbox.sh --driver=rtlsim --app=draw3d --args="-tbox.cgltrace -rbox_ref_128.png" --cores=2 --l3cache
CONFIGS="-DEXT_GFX_ENABLE" ./ci/blackbox.sh --driver=simx --app=draw3d --args="-ttriangle.cgltrace -rtriangle_ref_8.png -w8 -h8" --warps=1 --threads=2 --debug=3
CONFIGS="-DEXT_GFX_ENABLE" ./ci/blackbox.sh --driver=rtlsim --app=draw3d --args="-ttriangle.cgltrace -rtriangle_ref_8.png -w8 -h8" --warps=1 --threads=2 --debug=3
CONFIGS="-DEXT_GFX_ENABLE -DL1_DISABLE -DSM_DISABLE -DTCACHE_DISABLE -DRCACHE_DISABLE -DOCACHE_DISABLE" ./ci/blackbox.sh --driver=simx --app=draw3d --args="-tvase.cgltrace -rvase_ref_32.png -w32 -h32" --warps=2 --threads=2
CONFIGS="-DEXT_GFX_ENABLE -DL1_DISABLE -DSM_DISABLE -DTCACHE_DISABLE -DRCACHE_DISABLE -DOCACHE_DISABLE" ./ci/blackbox.sh --driver=rtlsim --app=draw3d --args="-tvase.cgltrace -rvase_ref_32.png -w32 -h32" --warps=2 --threads=2
echo "graphics tests done!"
}

View file

@ -10,6 +10,7 @@
#include "common.h"
#include "utils.h"
#include <cocogfx/include/cgltrace.hpp>
#include <cocogfx/include/imageutil.hpp>
using namespace cocogfx;
@ -501,7 +502,7 @@ int main(int argc, char *argv[]) {
cleanup();
if (reference_file) {
auto errors = CompareImages(output_file, reference_file, FORMAT_A8R8G8B8);
auto errors = CompareImages(output_file, reference_file, FORMAT_A8R8G8B8, 2);
if (0 == errors) {
std::cout << "PASSED!" << std::endl;
} else {

View file

@ -7,9 +7,6 @@
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cocogfx/include/tga.hpp>
#include <cocogfx/include/png.hpp>
#include <cocogfx/include/bmp.hpp>
#include <cocogfx/include/fixed.hpp>
#include <cocogfx/include/math.hpp>
#include "common.h"
@ -298,180 +295,6 @@ uint32_t Binning(std::vector<uint8_t>& tilebuf,
return tiles.size();
}
std::string getFileExt(const std::string& str) {
auto i = str.rfind('.');
if (i != std::string::npos) {
return str.substr(i+1);
}
return("");
}
bool iequals(const std::string& a, const std::string& b) {
auto sz = a.size();
if (b.size() != sz)
return false;
for (size_t i = 0; i < sz; ++i) {
if (tolower(a[i]) != tolower(b[i]))
return false;
}
return true;
}
int LoadImage(const char *filename,
ePixelFormat format,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height) {
uint32_t img_width;
uint32_t img_height;
uint32_t img_bpp;
auto ext = getFileExt(filename);
if (iequals(ext, "tga")) {
int ret = LoadTGA(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else
if (iequals(ext, "png")) {
int ret = LoadPNG(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else
if (iequals(ext, "bmp")) {
int ret = LoadBMP(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else {
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
return -1;
}
ePixelFormat img_format;
switch (img_bpp) {
case 1:
img_format = FORMAT_A8;
break;
case 2:
img_format = FORMAT_R5G6B5;
break;
case 3:
img_format = FORMAT_R8G8B8;
break;
case 4:
img_format = FORMAT_A8R8G8B8;
break;
default:
std::abort();
}
if (img_format != format) {
// format conversion to RGBA
std::vector<uint8_t> staging;
int ret = ConvertImage(staging, format, pixels.data(), img_format, img_width, img_height, img_width * img_bpp);
if (ret)
return ret;
pixels.swap(staging);
}
*width = img_width;
*height = img_height;
return 0;
}
int SaveImage(const char *filename,
ePixelFormat format,
const uint8_t* pixels,
uint32_t width,
uint32_t height,
int32_t pitch) {
uint32_t bpp = Format::GetInfo(format).BytePerPixel;
auto ext = getFileExt(filename);
if (iequals(ext, "tga")) {
return SaveTGA(filename, pixels, width, height, bpp, pitch);
} else
if (iequals(ext, "png")) {
return SavePNG(filename, pixels, width, height, bpp, pitch);
} else
if (iequals(ext, "bmp")) {
return SaveBMP(filename, pixels, width, height, bpp, pitch);
} else {
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
return -1;
}
return 0;
}
void dump_image(const std::vector<uint8_t>& pixels, uint32_t width, uint32_t height, uint32_t bpp) {
assert(width * height * bpp == pixels.size());
const uint8_t* pixel_bytes = pixels.data();
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
uint32_t pixel32 = 0;
for (uint32_t b = 0; b < bpp; ++b) {
uint32_t pixel8 = *pixel_bytes++;
pixel32 |= pixel8 << (b * 8);
}
if (x) std::cout << ", ";
std::cout << std::hex << std::setw(bpp * 2) << std::setfill('0') << pixel32;
}
std::cout << std::endl;
}
}
int CompareImages(const char* filename1,
const char* filename2,
cocogfx::ePixelFormat format) {
int ret;
std::vector<uint8_t> image1_bits;
uint32_t image1_width;
uint32_t image1_height;
std::vector<uint8_t> image2_bits;
uint32_t image2_width;
uint32_t image2_height;
ret = LoadImage(filename1, format, image1_bits, &image1_width, &image1_height);
if (ret)
return ret;
ret = LoadImage(filename2, format, image2_bits, &image2_width, &image2_height);
if (ret)
return ret;
if (image1_bits.size() != image2_bits.size())
return -1;
if (image1_width != image2_width)
return -1;
if (image1_height != image2_height)
return -1;
int errors = 0;
{
auto convert_from = Format::GetConvertFrom(format, true);
auto bpp = Format::GetInfo(format).BytePerPixel;
auto pixels1 = image1_bits.data();
auto pixels2 = image2_bits.data();
for (uint32_t y = 0; y < image1_height; ++y) {
for (uint32_t x = 0; x < image1_width; ++x) {
auto color1 = convert_from(pixels1);
auto color2 = convert_from(pixels2);
if (color1 != color2) {
printf("Error: pixel mismatch at (%d, %d), actual=0x%x, expected=0x%x\n", x, y, color1.value, color2.value);
++errors;
}
pixels1 += bpp;
pixels2 += bpp;
}
}
}
return errors;
}
uint32_t toVXFormat(ePixelFormat format) {
switch (format) {
case FORMAT_A8R8G8B8: return TEX_FORMAT_A8R8G8B8; break;

View file

@ -21,26 +21,4 @@ uint32_t Binning(std::vector<uint8_t>& tilebuf,
uint32_t height,
float near,
float far,
uint32_t tileSize);
int LoadImage(const char *filename,
cocogfx::ePixelFormat format,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height);
int SaveImage(const char *filename,
cocogfx::ePixelFormat format,
const uint8_t* pixels,
uint32_t width,
uint32_t height,
int32_t pitch);
void dump_image(const std::vector<uint8_t>& pixels,
uint32_t width,
uint32_t height,
uint32_t bpp);
int CompareImages(const char* filename1,
const char* filename2,
cocogfx::ePixelFormat format);
uint32_t tileSize);

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

View file

@ -10,6 +10,7 @@
#include "common.h"
#include "utils.h"
#include <cocogfx/include/cgltrace.hpp>
#include <cocogfx/include/imageutil.hpp>
using namespace cocogfx;

View file

@ -7,9 +7,6 @@
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cocogfx/include/tga.hpp>
#include <cocogfx/include/png.hpp>
#include <cocogfx/include/bmp.hpp>
#include <cocogfx/include/fixed.hpp>
#include <cocogfx/include/math.hpp>
#include "common.h"
@ -298,180 +295,6 @@ uint32_t Binning(std::vector<uint8_t>& tilebuf,
return tiles.size();
}
std::string getFileExt(const std::string& str) {
auto i = str.rfind('.');
if (i != std::string::npos) {
return str.substr(i+1);
}
return("");
}
bool iequals(const std::string& a, const std::string& b) {
auto sz = a.size();
if (b.size() != sz)
return false;
for (size_t i = 0; i < sz; ++i) {
if (tolower(a[i]) != tolower(b[i]))
return false;
}
return true;
}
int LoadImage(const char *filename,
ePixelFormat format,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height) {
uint32_t img_width;
uint32_t img_height;
uint32_t img_bpp;
auto ext = getFileExt(filename);
if (iequals(ext, "tga")) {
int ret = LoadTGA(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else
if (iequals(ext, "png")) {
int ret = LoadPNG(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else
if (iequals(ext, "bmp")) {
int ret = LoadBMP(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else {
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
return -1;
}
ePixelFormat img_format;
switch (img_bpp) {
case 1:
img_format = FORMAT_A8;
break;
case 2:
img_format = FORMAT_R5G6B5;
break;
case 3:
img_format = FORMAT_R8G8B8;
break;
case 4:
img_format = FORMAT_A8R8G8B8;
break;
default:
std::abort();
}
if (img_format != format) {
// format conversion to RGBA
std::vector<uint8_t> staging;
int ret = ConvertImage(staging, format, pixels.data(), img_format, img_width, img_height, img_width * img_bpp);
if (ret)
return ret;
pixels.swap(staging);
}
*width = img_width;
*height = img_height;
return 0;
}
int SaveImage(const char *filename,
ePixelFormat format,
const uint8_t* pixels,
uint32_t width,
uint32_t height,
int32_t pitch) {
uint32_t bpp = Format::GetInfo(format).BytePerPixel;
auto ext = getFileExt(filename);
if (iequals(ext, "tga")) {
return SaveTGA(filename, pixels, width, height, bpp, pitch);
} else
if (iequals(ext, "png")) {
return SavePNG(filename, pixels, width, height, bpp, pitch);
} else
if (iequals(ext, "bmp")) {
return SaveBMP(filename, pixels, width, height, bpp, pitch);
} else {
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
return -1;
}
return 0;
}
void dump_image(const std::vector<uint8_t>& pixels, uint32_t width, uint32_t height, uint32_t bpp) {
assert(width * height * bpp == pixels.size());
const uint8_t* pixel_bytes = pixels.data();
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
uint32_t pixel32 = 0;
for (uint32_t b = 0; b < bpp; ++b) {
uint32_t pixel8 = *pixel_bytes++;
pixel32 |= pixel8 << (b * 8);
}
if (x) std::cout << ", ";
std::cout << std::hex << std::setw(bpp * 2) << std::setfill('0') << pixel32;
}
std::cout << std::endl;
}
}
int CompareImages(const char* filename1,
const char* filename2,
cocogfx::ePixelFormat format) {
int ret;
std::vector<uint8_t> image1_bits;
uint32_t image1_width;
uint32_t image1_height;
std::vector<uint8_t> image2_bits;
uint32_t image2_width;
uint32_t image2_height;
ret = LoadImage(filename1, format, image1_bits, &image1_width, &image1_height);
if (ret)
return ret;
ret = LoadImage(filename2, format, image2_bits, &image2_width, &image2_height);
if (ret)
return ret;
if (image1_bits.size() != image2_bits.size())
return -1;
if (image1_width != image2_width)
return -1;
if (image1_height != image2_height)
return -1;
int errors = 0;
{
auto convert_from = Format::GetConvertFrom(format, true);
auto bpp = Format::GetInfo(format).BytePerPixel;
auto pixels1 = image1_bits.data();
auto pixels2 = image2_bits.data();
for (uint32_t y = 0; y < image1_height; ++y) {
for (uint32_t x = 0; x < image1_width; ++x) {
auto color1 = convert_from(pixels1);
auto color2 = convert_from(pixels2);
if (color1 != color2) {
printf("Error: pixel mismatch at (%d, %d), actual=0x%x, expected=0x%x\n", x, y, color1.value, color2.value);
++errors;
}
pixels1 += bpp;
pixels2 += bpp;
}
}
}
return errors;
}
uint32_t toVXFormat(ePixelFormat format) {
switch (format) {
case FORMAT_A8R8G8B8: return TEX_FORMAT_A8R8G8B8; break;

View file

@ -21,26 +21,4 @@ uint32_t Binning(std::vector<uint8_t>& tilebuf,
uint32_t height,
float near,
float far,
uint32_t tileSize);
int LoadImage(const char *filename,
cocogfx::ePixelFormat format,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height);
int SaveImage(const char *filename,
cocogfx::ePixelFormat format,
const uint8_t* pixels,
uint32_t width,
uint32_t height,
int32_t pitch);
void dump_image(const std::vector<uint8_t>& pixels,
uint32_t width,
uint32_t height,
uint32_t bpp);
int CompareImages(const char* filename1,
const char* filename2,
cocogfx::ePixelFormat format);
uint32_t tileSize);

View file

@ -33,7 +33,7 @@ endif
PROJECT = rop
SRCS = main.cpp utils.cpp
SRCS = main.cpp
all: $(PROJECT) kernel.bin kernel.dump

View file

@ -7,8 +7,8 @@
#include <assert.h>
#include <vortex.h>
#include "common.h"
#include "utils.h"
#include <cocogfx/include/fixed.hpp>
#include <cocogfx/include/imageutil.hpp>
using namespace cocogfx;

View file

@ -1,184 +0,0 @@
#include "utils.h"
#include <assert.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <cocogfx/include/tga.hpp>
#include <cocogfx/include/png.hpp>
#include <cocogfx/include/bmp.hpp>
using namespace cocogfx;
std::string getFileExt(const std::string& str) {
auto i = str.rfind('.');
if (i != std::string::npos) {
return str.substr(i+1);
}
return("");
}
bool iequals(const std::string& a, const std::string& b) {
auto sz = a.size();
if (b.size() != sz)
return false;
for (size_t i = 0; i < sz; ++i) {
if (tolower(a[i]) != tolower(b[i]))
return false;
}
return true;
}
int LoadImage(const char *filename,
ePixelFormat format,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height) {
uint32_t img_width;
uint32_t img_height;
uint32_t img_bpp;
auto ext = getFileExt(filename);
if (iequals(ext, "tga")) {
int ret = LoadTGA(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else
if (iequals(ext, "png")) {
int ret = LoadPNG(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else
if (iequals(ext, "bmp")) {
int ret = LoadBMP(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else {
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
return -1;
}
ePixelFormat img_format;
switch (img_bpp) {
case 1:
img_format = FORMAT_A8;
break;
case 2:
img_format = FORMAT_A1R5G5B5;
break;
case 3:
img_format = FORMAT_R8G8B8;
break;
case 4:
img_format = FORMAT_A8R8G8B8;
break;
default:
std::abort();
}
if (img_format != format) {
// format conversion to RGBA
std::vector<uint8_t> staging;
int ret = ConvertImage(staging, format, pixels.data(), img_format, img_width, img_height, img_width * img_bpp);
if (ret)
return ret;
pixels.swap(staging);
}
*width = img_width;
*height = img_height;
return 0;
}
int SaveImage(const char *filename,
ePixelFormat format,
const uint8_t* pixels,
uint32_t width,
uint32_t height,
int32_t pitch) {
auto bpp = Format::GetInfo(format).BytePerPixel;
auto ext = getFileExt(filename);
if (iequals(ext, "tga")) {
return SaveTGA(filename, pixels, width, height, bpp, pitch);
} else
if (iequals(ext, "png")) {
return SavePNG(filename, pixels, width, height, bpp, pitch);
} else
if (iequals(ext, "bmp")) {
return SaveBMP(filename, pixels, width, height, bpp, pitch);
} else {
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
return -1;
}
return 0;
}
void dump_image(const std::vector<uint8_t>& pixels, uint32_t width, uint32_t height, uint32_t bpp) {
assert(width * height * bpp == pixels.size());
const uint8_t* pixel_bytes = pixels.data();
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
uint32_t pixel32 = 0;
for (uint32_t b = 0; b < bpp; ++b) {
uint32_t pixel8 = *pixel_bytes++;
pixel32 |= pixel8 << (b * 8);
}
if (x) std::cout << ", ";
std::cout << std::hex << std::setw(bpp * 2) << std::setfill('0') << pixel32;
}
std::cout << std::endl;
}
}
int CompareImages(const char* filename1,
const char* filename2,
cocogfx::ePixelFormat format) {
int ret;
std::vector<uint8_t> image1_bits;
uint32_t image1_width;
uint32_t image1_height;
std::vector<uint8_t> image2_bits;
uint32_t image2_width;
uint32_t image2_height;
ret = LoadImage(filename1, format, image1_bits, &image1_width, &image1_height);
if (ret)
return ret;
ret = LoadImage(filename2, format, image2_bits, &image2_width, &image2_height);
if (ret)
return ret;
if (image1_bits.size() != image2_bits.size())
return -1;
if (image1_width != image2_width)
return -1;
if (image1_height != image2_height)
return -1;
int errors = 0;
{
auto convert_from = Format::GetConvertFrom(format, true);
auto bpp = Format::GetInfo(format).BytePerPixel;
auto pixels1 = image1_bits.data();
auto pixels2 = image2_bits.data();
for (uint32_t y = 0; y < image1_height; ++y) {
for (uint32_t x = 0; x < image1_width; ++x) {
auto color1 = convert_from(pixels1);
auto color2 = convert_from(pixels2);
if (color1 != color2) {
printf("Error: pixel mismatch at (%d, %d), actual=0x%x, expected=0x%x\n", x, y, color1.value, color2.value);
++errors;
}
pixels1 += bpp;
pixels2 += bpp;
}
}
}
return errors;
}

View file

@ -1,27 +0,0 @@
#include <cstdint>
#include <vector>
#include <bitmanip.h>
#include <cocogfx/include/format.hpp>
#include <cocogfx/include/blitter.hpp>
int LoadImage(const char *filename,
cocogfx::ePixelFormat format,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height);
int SaveImage(const char *filename,
cocogfx::ePixelFormat format,
const uint8_t* pixels,
uint32_t width,
uint32_t height,
int32_t pitch);
void dump_image(const std::vector<uint8_t>& pixels,
uint32_t width,
uint32_t height,
uint32_t bpp);
int CompareImages(const char* filename1,
const char* filename2,
cocogfx::ePixelFormat format);

View file

@ -33,7 +33,7 @@ endif
PROJECT = tex
SRCS = main.cpp utils.cpp
SRCS = main.cpp
all: $(PROJECT) kernel.bin kernel.dump

View file

@ -7,7 +7,9 @@
#include <assert.h>
#include <vortex.h>
#include "common.h"
#include "utils.h"
#include <bitmanip.h>
#include <cocogfx/include/blitter.hpp>
#include <cocogfx/include/imageutil.hpp>
using namespace cocogfx;
@ -139,7 +141,7 @@ int render(const kernel_arg_t& kernel_arg,
// save output image
std::cout << "save output image" << std::endl;
//dump_image(dst_pixels, width, height, 4);
//DumpImage(dst_pixels, width, height, 4);
RT_CHECK(SaveImage(output_file, FORMAT_A8R8G8B8, dst_pixels.data(), width, height, width * 4));
return 0;
@ -165,7 +167,7 @@ int main(int argc, char *argv[]) {
}
uint32_t src_bpp = Format::GetInfo(eformat).BytePerPixel;
uint32_t src_pitch = src_width * src_bpp;
//dump_image(staging, src_width, src_height, src_bpp);
//DumpImage(staging, src_width, src_height, src_bpp);
RT_CHECK(GenerateMipmaps(src_pixels, mip_offsets, staging.data(), eformat, src_width, src_height, src_pitch));
}

View file

@ -1,184 +0,0 @@
#include "utils.h"
#include <assert.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <cocogfx/include/tga.hpp>
#include <cocogfx/include/png.hpp>
#include <cocogfx/include/bmp.hpp>
using namespace cocogfx;
std::string getFileExt(const std::string& str) {
auto i = str.rfind('.');
if (i != std::string::npos) {
return str.substr(i+1);
}
return("");
}
bool iequals(const std::string& a, const std::string& b) {
auto sz = a.size();
if (b.size() != sz)
return false;
for (size_t i = 0; i < sz; ++i) {
if (tolower(a[i]) != tolower(b[i]))
return false;
}
return true;
}
int LoadImage(const char *filename,
ePixelFormat format,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height) {
uint32_t img_width;
uint32_t img_height;
uint32_t img_bpp;
auto ext = getFileExt(filename);
if (iequals(ext, "tga")) {
int ret = LoadTGA(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else
if (iequals(ext, "png")) {
int ret = LoadPNG(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else
if (iequals(ext, "bmp")) {
int ret = LoadBMP(filename, pixels, &img_width, &img_height, &img_bpp);
if (ret)
return ret;
} else {
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
return -1;
}
ePixelFormat img_format;
switch (img_bpp) {
case 1:
img_format = FORMAT_A8;
break;
case 2:
img_format = FORMAT_A1R5G5B5;
break;
case 3:
img_format = FORMAT_R8G8B8;
break;
case 4:
img_format = FORMAT_A8R8G8B8;
break;
default:
std::abort();
}
if (img_format != format) {
// format conversion to RGBA
std::vector<uint8_t> staging;
int ret = ConvertImage(staging, format, pixels.data(), img_format, img_width, img_height, img_width * img_bpp);
if (ret)
return ret;
pixels.swap(staging);
}
*width = img_width;
*height = img_height;
return 0;
}
int SaveImage(const char *filename,
ePixelFormat format,
const uint8_t* pixels,
uint32_t width,
uint32_t height,
int32_t pitch) {
auto bpp = Format::GetInfo(format).BytePerPixel;
auto ext = getFileExt(filename);
if (iequals(ext, "tga")) {
return SaveTGA(filename, pixels, width, height, bpp, pitch);
} else
if (iequals(ext, "png")) {
return SavePNG(filename, pixels, width, height, bpp, pitch);
} else
if (iequals(ext, "bmp")) {
return SaveBMP(filename, pixels, width, height, bpp, pitch);
} else {
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
return -1;
}
return 0;
}
void dump_image(const std::vector<uint8_t>& pixels, uint32_t width, uint32_t height, uint32_t bpp) {
assert(width * height * bpp == pixels.size());
const uint8_t* pixel_bytes = pixels.data();
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
uint32_t pixel32 = 0;
for (uint32_t b = 0; b < bpp; ++b) {
uint32_t pixel8 = *pixel_bytes++;
pixel32 |= pixel8 << (b * 8);
}
if (x) std::cout << ", ";
std::cout << std::hex << std::setw(bpp * 2) << std::setfill('0') << pixel32;
}
std::cout << std::endl;
}
}
int CompareImages(const char* filename1,
const char* filename2,
cocogfx::ePixelFormat format) {
int ret;
std::vector<uint8_t> image1_bits;
uint32_t image1_width;
uint32_t image1_height;
std::vector<uint8_t> image2_bits;
uint32_t image2_width;
uint32_t image2_height;
ret = LoadImage(filename1, format, image1_bits, &image1_width, &image1_height);
if (ret)
return ret;
ret = LoadImage(filename2, format, image2_bits, &image2_width, &image2_height);
if (ret)
return ret;
if (image1_bits.size() != image2_bits.size())
return -1;
if (image1_width != image2_width)
return -1;
if (image1_height != image2_height)
return -1;
int errors = 0;
{
auto convert_from = Format::GetConvertFrom(format, true);
auto bpp = Format::GetInfo(format).BytePerPixel;
auto pixels1 = image1_bits.data();
auto pixels2 = image2_bits.data();
for (uint32_t y = 0; y < image1_height; ++y) {
for (uint32_t x = 0; x < image1_width; ++x) {
auto color1 = convert_from(pixels1);
auto color2 = convert_from(pixels2);
if (color1 != color2) {
printf("Error: pixel mismatch at (%d, %d), actual=0x%x, expected=0x%x\n", x, y, color1.value, color2.value);
++errors;
}
pixels1 += bpp;
pixels2 += bpp;
}
}
}
return errors;
}

View file

@ -1,27 +0,0 @@
#include <cstdint>
#include <vector>
#include <bitmanip.h>
#include <cocogfx/include/format.hpp>
#include <cocogfx/include/blitter.hpp>
int LoadImage(const char *filename,
cocogfx::ePixelFormat format,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height);
int SaveImage(const char *filename,
cocogfx::ePixelFormat format,
const uint8_t* pixels,
uint32_t width,
uint32_t height,
int32_t pitch);
void dump_image(const std::vector<uint8_t>& pixels,
uint32_t width,
uint32_t height,
uint32_t bpp);
int CompareImages(const char* filename1,
const char* filename2,
cocogfx::ePixelFormat format);

2
third_party/cocogfx vendored

@ -1 +1 @@
Subproject commit c26f47467882ce7db9bda103ea618d018ad4b1b4
Subproject commit 6448e68330ca00a64ab3184e3c5c95b59fe8b62a