Simplify sdl-imageviewer's SDL pixel format use

This commit is contained in:
Nigel Tao committed 2021-04-02 19:40:11 +11:00
1 parent 97e30c29da
commit a1506ad446
1 file changed
+26 -49
+26 -49
View File
@@ -85,12 +85,7 @@ isn't otherwise doing anything.
class Wuffs_Load_RW_Callbacks : public wuffs_aux::DecodeImageCallbacks { class Wuffs_Load_RW_Callbacks : public wuffs_aux::DecodeImageCallbacks {
public: public:
Wuffs_Load_RW_Callbacks(SDL_PixelFormat* pixfmt) Wuffs_Load_RW_Callbacks() : m_surface(NULL) {}
: m_rmask(pixfmt->Rmask),
m_gmask(pixfmt->Gmask),
m_bmask(pixfmt->Bmask),
m_amask(0xFFFFFFFFu ^ pixfmt->Rmask ^ pixfmt->Gmask ^ pixfmt->Bmask),
m_surface(NULL) {}
~Wuffs_Load_RW_Callbacks() { ~Wuffs_Load_RW_Callbacks() {
if (m_surface) { if (m_surface) {
@@ -114,23 +109,10 @@ class Wuffs_Load_RW_Callbacks : public wuffs_aux::DecodeImageCallbacks {
private: private:
wuffs_base__pixel_format // wuffs_base__pixel_format //
SelectPixfmt(const wuffs_base__image_config& image_config) override { SelectPixfmt(const wuffs_base__image_config& image_config) override {
#if SDL_BYTEORDER == SDL_BIG_ENDIAN // Regardless of endianness, SDL_PIXELFORMAT_BGRA32 (from a few lines
bool red_first = (m_rmask)&0x80000000u; // below) is equivalent to WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL.
#else return wuffs_base__make_pixel_format(
bool red_first = (m_rmask)&0x00000001u; WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL);
#endif
// (§) Uncomment this line of code to invert the BGRA/RGBA color order.
// This isn't a generally useful feature for an image viewer, but it should
// make it obvious, when pressing the TAB key, whether you're using the
// Wuffs (inverted) or SDL_image (correct) decoder.
//
// red_first = !red_first;
wuffs_base__pixel_format pixfmt = wuffs_base__make_pixel_format(
red_first ? WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL
: WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL);
return pixfmt;
} }
AllocResult // AllocResult //
@@ -146,9 +128,17 @@ class Wuffs_Load_RW_Callbacks : public wuffs_aux::DecodeImageCallbacks {
if ((w > 0xFFFFFF) || (h > 0xFFFFFF)) { if ((w > 0xFFFFFF) || (h > 0xFFFFFF)) {
return AllocResult("Wuffs_Load_RW_Callbacks: image is too large"); return AllocResult("Wuffs_Load_RW_Callbacks: image is too large");
} }
m_surface = uint32_t sdl_pixelformat = SDL_PIXELFORMAT_BGRA32;
SDL_CreateRGBSurface(0, static_cast<int>(w), static_cast<int>(h), 32,
m_rmask, m_gmask, m_bmask, m_amask); // (§) Uncomment this line of code to invert the BGRA/RGBA color order.
// This isn't a generally useful feature for an image viewer, but it should
// make it obvious, when pressing the TAB key, whether you're using the
// Wuffs (inverted) or SDL_image (correct) decoder.
//
// sdl_pixelformat = SDL_PIXELFORMAT_RGBA32;
m_surface = SDL_CreateRGBSurfaceWithFormat(
0, static_cast<int>(w), static_cast<int>(h), 32, sdl_pixelformat);
if (!m_surface) { if (!m_surface) {
return AllocResult( return AllocResult(
"Wuffs_Load_RW_Callbacks: SDL_CreateRGBSurface failed"); "Wuffs_Load_RW_Callbacks: SDL_CreateRGBSurface failed");
@@ -160,10 +150,6 @@ class Wuffs_Load_RW_Callbacks : public wuffs_aux::DecodeImageCallbacks {
m_surface->h * m_surface->pitch)); m_surface->h * m_surface->pitch));
} }
const uint32_t m_rmask;
const uint32_t m_gmask;
const uint32_t m_bmask;
const uint32_t m_amask;
SDL_Surface* m_surface; SDL_Surface* m_surface;
}; };
@@ -205,26 +191,17 @@ class Wuffs_Load_RW_Input : public wuffs_aux::sync_io::Input {
// -------- // --------
// Wuffs_Load_RW loads the image from the given file into the given format // Wuffs_Load_RW loads the image from the input rw. It is like SDL_image's
// (which must be 32-bits per pixel). It is similar to SDL_image's IMG_Load_RW // IMG_Load_RW function but it returns any error in-band (as a std::string)
// function but it returns any error in-band (as a std::string) instead of // instead of separately (global state accessible via SDL_GetError).
// separately (global state accessible via SDL_GetError).
// //
// On success, the SDL_Surface* returned will be non-NULL and the caller owns // On success, the SDL_Surface* returned will be non-NULL and the caller owns
// it. Ownership means that they are responsible for calling SDL_FreeSurface on // it. Ownership means that they are responsible for calling SDL_FreeSurface on
// it when done. // it when done.
std::pair<SDL_Surface*, std::string> // std::pair<SDL_Surface*, std::string> //
Wuffs_Load_RW(SDL_RWops* rw, Wuffs_Load_RW(SDL_RWops* rw, bool take_ownership_of_rw) {
bool take_ownership_of_rw, Wuffs_Load_RW_Callbacks callbacks;
SDL_PixelFormat* format) {
Wuffs_Load_RW_Input input(rw, take_ownership_of_rw); Wuffs_Load_RW_Input input(rw, take_ownership_of_rw);
if (!format || (format->BitsPerPixel != 32)) {
return std::make_pair<SDL_Surface*, std::string>(
NULL, "Wuffs_Load_RW: invalid pixel format");
}
Wuffs_Load_RW_Callbacks callbacks(format);
wuffs_aux::DecodeImageResult res = wuffs_aux::DecodeImage(callbacks, input); wuffs_aux::DecodeImageResult res = wuffs_aux::DecodeImage(callbacks, input);
if (!res.error_message.empty()) { if (!res.error_message.empty()) {
return std::make_pair<SDL_Surface*, std::string>( return std::make_pair<SDL_Surface*, std::string>(
@@ -251,7 +228,7 @@ draw(SDL_Window* window) {
} }
bool // bool //
load_image(SDL_Window* window, const char* filename) { load_image(const char* filename) {
if (g_image) { if (g_image) {
SDL_FreeSurface(g_image); SDL_FreeSurface(g_image);
g_image = NULL; g_image = NULL;
@@ -273,8 +250,8 @@ load_image(SDL_Window* window, const char* filename) {
return false; return false;
} }
} else { } else {
std::pair<SDL_Surface*, std::string> p = Wuffs_Load_RW( std::pair<SDL_Surface*, std::string> p =
rw, take_ownership_of_rw, SDL_GetWindowSurface(window)->format); Wuffs_Load_RW(rw, take_ownership_of_rw);
if (!p.second.empty()) { if (!p.second.empty()) {
fprintf(stderr, "main: Wuffs_Load_RW(\"%s\"): %s\n", filename, fprintf(stderr, "main: Wuffs_Load_RW(\"%s\"): %s\n", filename,
p.second.c_str()); p.second.c_str());
@@ -304,7 +281,7 @@ main(int argc, char** argv) {
return 1; return 1;
} }
if (!load_image(window, argv[1])) { if (!load_image(argv[1])) {
return 1; return 1;
} }
@@ -337,7 +314,7 @@ main(int argc, char** argv) {
g_load_via_sdl_image = !g_load_via_sdl_image; g_load_via_sdl_image = !g_load_via_sdl_image;
printf("Switched to %s.\n", printf("Switched to %s.\n",
g_load_via_sdl_image ? "SDL_image" : "Wuffs"); g_load_via_sdl_image ? "SDL_image" : "Wuffs");
if (!load_image(window, argv[1]) || !draw(window)) { if (!load_image(argv[1]) || !draw(window)) {
return 1; return 1;
} }
break; break;