From 9105408ba52b7639d8553855e47ec452d2e22e17 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 10 Mar 2014 15:07:37 +0300 Subject: remove duplicate code from imagehelper. --- src/resources/imagehelper.cpp | 82 +++++++++++++++++++++++++++++++ src/resources/imagehelper.h | 9 ++-- src/resources/sdl2imagehelper.cpp | 80 ------------------------------ src/resources/sdl2imagehelper.h | 3 -- src/resources/sdl2softwareimagehelper.cpp | 81 ------------------------------ src/resources/sdl2softwareimagehelper.h | 3 -- src/resources/sdlimagehelper.cpp | 18 ------- src/resources/sdlimagehelper.h | 3 -- src/resources/surfaceimagehelper.cpp | 81 ------------------------------ src/resources/surfaceimagehelper.h | 3 -- 10 files changed, 85 insertions(+), 278 deletions(-) diff --git a/src/resources/imagehelper.cpp b/src/resources/imagehelper.cpp index ff7c196c4..0fbc04776 100644 --- a/src/resources/imagehelper.cpp +++ b/src/resources/imagehelper.cpp @@ -25,6 +25,8 @@ #include "logger.h" #include "main.h" +#include "resources/dye.h" + #include "utils/sdlcheckutils.h" #include @@ -52,6 +54,68 @@ Image *ImageHelper::load(SDL_RWops *const rw) const return image; } +Image *ImageHelper::load(SDL_RWops *const rw, Dye const &dye) const +{ + SDL_Surface *const tmpImage = loadPng(rw); + if (!tmpImage) + { + logger->log("Error, image load failed: %s", IMG_GetError()); + return nullptr; + } + + SDL_PixelFormat rgba; + rgba.palette = nullptr; + rgba.BitsPerPixel = 32; + rgba.BytesPerPixel = 4; + +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + rgba.Rmask = 0x000000FF; + rgba.Gmask = 0x0000FF00; + rgba.Bmask = 0x00FF0000; + rgba.Amask = 0xFF000000; +#else + rgba.Rmask = 0xFF000000; + rgba.Gmask = 0x00FF0000; + rgba.Bmask = 0x0000FF00; + rgba.Amask = 0x000000FF; +#endif + + SDL_Surface *const surf = MSDL_ConvertSurface( + tmpImage, &rgba, SDL_SWSURFACE); + MSDL_FreeSurface(tmpImage); + + uint32_t *pixels = static_cast(surf->pixels); + const int type = dye.getType(); + + switch (type) + { + case 1: + { + const DyePalette *const pal = dye.getSPalete(); + if (pal) + pal->replaceSColor(pixels, surf->w * surf->h); + break; + } + case 2: + { + const DyePalette *const pal = dye.getAPalete(); + if (pal) + pal->replaceAColor(pixels, surf->w * surf->h); + break; + } + case 0: + default: + { + dye.normalDye(pixels, surf->w * surf->h); + break; + } + } + + Image *const image = load(surf); + MSDL_FreeSurface(surf); + return image; +} + SDL_Surface* ImageHelper::convertTo32Bit(SDL_Surface *const tmpImage) { if (!tmpImage) @@ -138,3 +202,21 @@ SDL_Surface *ImageHelper::loadPng(SDL_RWops *const rw) SDL_RWclose(rw); return nullptr; } + +SDL_Surface *ImageHelper::create32BitSurface(int width, int height) const +{ +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + const int rmask = 0xff000000; + const int gmask = 0x00ff0000; + const int bmask = 0x0000ff00; + const int amask = 0x000000ff; +#else + const int rmask = 0x000000ff; + const int gmask = 0x0000ff00; + const int bmask = 0x00ff0000; + const int amask = 0xff000000; +#endif + + return MSDL_CreateRGBSurface(SDL_SWSURFACE, + width, height, 32, rmask, gmask, bmask, amask); +} diff --git a/src/resources/imagehelper.h b/src/resources/imagehelper.h index 2e3b96771..e7ec4cfad 100644 --- a/src/resources/imagehelper.h +++ b/src/resources/imagehelper.h @@ -58,19 +58,16 @@ class ImageHelper */ Image *load(SDL_RWops *const rw) const A_WARN_UNUSED; -#ifdef __GNUC__ virtual Image *load(SDL_RWops *const rw, Dye - const &dye) const A_WARN_UNUSED = 0; + const &dye) const A_WARN_UNUSED; +#ifdef __GNUC__ virtual Image *load(SDL_Surface *const) const A_WARN_UNUSED = 0; virtual Image *createTextSurface(SDL_Surface *const tmpImage, const int width, const int height, float alpha) const A_WARN_UNUSED = 0; #else - virtual Image *load(SDL_RWops *rw, Dye const &dye) const A_WARN_UNUSED - { return nullptr; } - virtual Image *load(SDL_Surface *) const A_WARN_UNUSED { return nullptr; } @@ -85,7 +82,7 @@ class ImageHelper static void dumpSurfaceFormat(const SDL_Surface *const image); virtual SDL_Surface *create32BitSurface(int width, int height) - const A_WARN_UNUSED = 0; + const A_WARN_UNUSED; static void setEnableAlpha(const bool n) { mEnableAlpha = n; } diff --git a/src/resources/sdl2imagehelper.cpp b/src/resources/sdl2imagehelper.cpp index 80110735f..b31622e51 100644 --- a/src/resources/sdl2imagehelper.cpp +++ b/src/resources/sdl2imagehelper.cpp @@ -43,68 +43,6 @@ bool SDLImageHelper::mEnableAlphaCache = false; SDL_Renderer *SDLImageHelper::mRenderer = nullptr; #endif -Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const -{ - SDL_Surface *const tmpImage = loadPng(rw); - if (!tmpImage) - { - logger->log("Error, image load failed: %s", IMG_GetError()); - return nullptr; - } - - SDL_PixelFormat rgba; - rgba.palette = nullptr; - rgba.BitsPerPixel = 32; - rgba.BytesPerPixel = 4; - -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - rgba.Rmask = 0x000000FF; - rgba.Gmask = 0x0000FF00; - rgba.Bmask = 0x00FF0000; - rgba.Amask = 0xFF000000; -#else - rgba.Rmask = 0xFF000000; - rgba.Gmask = 0x00FF0000; - rgba.Bmask = 0x0000FF00; - rgba.Amask = 0x000000FF; -#endif - - SDL_Surface *const surf = MSDL_ConvertSurface( - tmpImage, &rgba, SDL_SWSURFACE); - MSDL_FreeSurface(tmpImage); - - uint32_t *pixels = static_cast(surf->pixels); - const int type = dye.getType(); - - switch (type) - { - case 1: - { - const DyePalette *const pal = dye.getSPalete(); - if (pal) - pal->replaceSColor(pixels, surf->w * surf->h); - break; - } - case 2: - { - const DyePalette *const pal = dye.getAPalete(); - if (pal) - pal->replaceAColor(pixels, surf->w * surf->h); - break; - } - case 0: - default: - { - dye.normalDye(pixels, surf->w * surf->h); - break; - } - } - - Image *const image = load(surf); - MSDL_FreeSurface(surf); - return image; -} - Image *SDLImageHelper::load(SDL_Surface *const tmpImage) const { return _SDLload(tmpImage); @@ -145,24 +83,6 @@ Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage) const return new Image(texture, tmpImage->w, tmpImage->h); } -SDL_Surface *SDLImageHelper::create32BitSurface(int width, int height) const -{ -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - const int rmask = 0xff000000; - const int gmask = 0x00ff0000; - const int bmask = 0x0000ff00; - const int amask = 0x000000ff; -#else - const int rmask = 0x000000ff; - const int gmask = 0x0000ff00; - const int bmask = 0x00ff0000; - const int amask = 0xff000000; -#endif - - return MSDL_CreateRGBSurface(SDL_SWSURFACE, - width, height, 32, rmask, gmask, bmask, amask); -} - int SDLImageHelper::combineSurface(SDL_Surface *restrict const src, SDL_Rect *restrict const srcrect, SDL_Surface *restrict const dst, diff --git a/src/resources/sdl2imagehelper.h b/src/resources/sdl2imagehelper.h index 70ea9c12f..44f81c433 100644 --- a/src/resources/sdl2imagehelper.h +++ b/src/resources/sdl2imagehelper.h @@ -83,9 +83,6 @@ class SDLImageHelper final : public ImageHelper static SDL_Surface* SDLDuplicateSurface(SDL_Surface *const tmpImage) A_WARN_UNUSED; - SDL_Surface *create32BitSurface(int width, int height) const - override final; - static int combineSurface(SDL_Surface *restrict const src, SDL_Rect *restrict const srcrect, SDL_Surface *restrict const dst, diff --git a/src/resources/sdl2softwareimagehelper.cpp b/src/resources/sdl2softwareimagehelper.cpp index 3e8802f41..fa0a51606 100644 --- a/src/resources/sdl2softwareimagehelper.cpp +++ b/src/resources/sdl2softwareimagehelper.cpp @@ -41,68 +41,6 @@ bool SDL2SoftwareImageHelper::mEnableAlphaCache = false; SDL_PixelFormat *SDL2SoftwareImageHelper::mFormat = nullptr; -Image *SDL2SoftwareImageHelper::load(SDL_RWops *const rw, Dye const &dye) const -{ - SDL_Surface *const tmpImage = loadPng(rw); - if (!tmpImage) - { - logger->log("Error, image load failed: %s", IMG_GetError()); - return nullptr; - } - - SDL_PixelFormat rgba; - rgba.palette = nullptr; - rgba.BitsPerPixel = 32; - rgba.BytesPerPixel = 4; - -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - rgba.Rmask = 0x000000FF; - rgba.Gmask = 0x0000FF00; - rgba.Bmask = 0x00FF0000; - rgba.Amask = 0xFF000000; -#else - rgba.Rmask = 0xFF000000; - rgba.Gmask = 0x00FF0000; - rgba.Bmask = 0x0000FF00; - rgba.Amask = 0x000000FF; -#endif - - SDL_Surface *const surf = MSDL_ConvertSurface( - tmpImage, &rgba, SDL_SWSURFACE); - MSDL_FreeSurface(tmpImage); - - uint32_t *pixels = static_cast(surf->pixels); - const int type = dye.getType(); - - switch (type) - { - case 1: - { - const DyePalette *const pal = dye.getSPalete(); - if (pal) - pal->replaceSColor(pixels, surf->w * surf->h); - break; - } - case 2: - { - const DyePalette *const pal = dye.getAPalete(); - if (pal) - pal->replaceAColor(pixels, surf->w * surf->h); - break; - } - case 0: - default: - { - dye.normalDye(pixels, surf->w * surf->h); - break; - } - } - - Image *const image = load(surf); - MSDL_FreeSurface(surf); - return image; -} - Image *SDL2SoftwareImageHelper::load(SDL_Surface *const tmpImage) const { return _SDLload(tmpImage); @@ -140,25 +78,6 @@ Image *SDL2SoftwareImageHelper::_SDLload(SDL_Surface *tmpImage) const return new Image(image, false, nullptr); } -SDL_Surface *SDL2SoftwareImageHelper::create32BitSurface(int width, - int height) const -{ -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - const int rmask = 0xff000000; - const int gmask = 0x00ff0000; - const int bmask = 0x0000ff00; - const int amask = 0x000000ff; -#else - const int rmask = 0x000000ff; - const int gmask = 0x0000ff00; - const int bmask = 0x00ff0000; - const int amask = 0xff000000; -#endif - - return MSDL_CreateRGBSurface(SDL_SWSURFACE, - width, height, 32, rmask, gmask, bmask, amask); -} - int SDL2SoftwareImageHelper::combineSurface(SDL_Surface *restrict const src, SDL_Rect *restrict const srcrect, SDL_Surface *restrict const dst, diff --git a/src/resources/sdl2softwareimagehelper.h b/src/resources/sdl2softwareimagehelper.h index 210b0f8b1..01d6428b5 100644 --- a/src/resources/sdl2softwareimagehelper.h +++ b/src/resources/sdl2softwareimagehelper.h @@ -83,9 +83,6 @@ class SDL2SoftwareImageHelper final : public ImageHelper static SDL_Surface* SDLDuplicateSurface(SDL_Surface *const tmpImage) A_WARN_UNUSED; - SDL_Surface *create32BitSurface(int width, - int height) const override final; - static int combineSurface(SDL_Surface *restrict const src, SDL_Rect *restrict const srcrect, SDL_Surface *restrict const dst, diff --git a/src/resources/sdlimagehelper.cpp b/src/resources/sdlimagehelper.cpp index cced7ad8a..7b9aeda5e 100644 --- a/src/resources/sdlimagehelper.cpp +++ b/src/resources/sdlimagehelper.cpp @@ -276,24 +276,6 @@ Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage) const return new Image(image, hasAlpha, alphaChannel); } -SDL_Surface *SDLImageHelper::create32BitSurface(int width, int height) const -{ -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - const int rmask = 0xff000000; - const int gmask = 0x00ff0000; - const int bmask = 0x0000ff00; - const int amask = 0x000000ff; -#else - const int rmask = 0x000000ff; - const int gmask = 0x0000ff00; - const int bmask = 0x00ff0000; - const int amask = 0xff000000; -#endif - - return MSDL_CreateRGBSurface(SDL_SWSURFACE, - width, height, 32, rmask, gmask, bmask, amask); -} - int SDLImageHelper::combineSurface(SDL_Surface *restrict const src, SDL_Rect *restrict const srcrect, SDL_Surface *restrict const dst, diff --git a/src/resources/sdlimagehelper.h b/src/resources/sdlimagehelper.h index 725b34ebc..42433ec55 100644 --- a/src/resources/sdlimagehelper.h +++ b/src/resources/sdlimagehelper.h @@ -84,9 +84,6 @@ class SDLImageHelper final : public ImageHelper static SDL_Surface* SDLDuplicateSurface(SDL_Surface *const tmpImage) A_WARN_UNUSED; - SDL_Surface *create32BitSurface(int width, - int height) const override final; - static int combineSurface(SDL_Surface *restrict const src, SDL_Rect *restrict const srcrect, SDL_Surface *restrict const dst, diff --git a/src/resources/surfaceimagehelper.cpp b/src/resources/surfaceimagehelper.cpp index 740c1c41d..ec7d9af9b 100644 --- a/src/resources/surfaceimagehelper.cpp +++ b/src/resources/surfaceimagehelper.cpp @@ -40,68 +40,6 @@ bool SurfaceImageHelper::mEnableAlphaCache = false; -Image *SurfaceImageHelper::load(SDL_RWops *const rw, Dye const &dye) const -{ - SDL_Surface *const tmpImage = loadPng(rw); - if (!tmpImage) - { - logger->log("Error, image load failed: %s", IMG_GetError()); - return nullptr; - } - - SDL_PixelFormat rgba; - rgba.palette = nullptr; - rgba.BitsPerPixel = 32; - rgba.BytesPerPixel = 4; - -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - rgba.Rmask = 0x000000FF; - rgba.Gmask = 0x0000FF00; - rgba.Bmask = 0x00FF0000; - rgba.Amask = 0xFF000000; -#else - rgba.Rmask = 0xFF000000; - rgba.Gmask = 0x00FF0000; - rgba.Bmask = 0x0000FF00; - rgba.Amask = 0x000000FF; -#endif - - SDL_Surface *const surf = MSDL_ConvertSurface( - tmpImage, &rgba, SDL_SWSURFACE); - MSDL_FreeSurface(tmpImage); - - uint32_t *pixels = static_cast(surf->pixels); - const int type = dye.getType(); - - switch (type) - { - case 1: - { - const DyePalette *const pal = dye.getSPalete(); - if (pal) - pal->replaceSColor(pixels, surf->w * surf->h); - break; - } - case 2: - { - const DyePalette *const pal = dye.getAPalete(); - if (pal) - pal->replaceAColor(pixels, surf->w * surf->h); - break; - } - case 0: - default: - { - dye.normalDye(pixels, surf->w * surf->h); - break; - } - } - - Image *const image = load(surf); - MSDL_FreeSurface(surf); - return image; -} - Image *SurfaceImageHelper::load(SDL_Surface *const tmpImage) const { return _SDLload(tmpImage); @@ -148,25 +86,6 @@ RenderType SurfaceImageHelper::useOpenGL() const return RENDER_SOFTWARE; } -SDL_Surface *SurfaceImageHelper::create32BitSurface(int width, - int height) const -{ -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - const int rmask = 0xff000000; - const int gmask = 0x00ff0000; - const int bmask = 0x0000ff00; - const int amask = 0x000000ff; -#else - const int rmask = 0x000000ff; - const int gmask = 0x0000ff00; - const int bmask = 0x00ff0000; - const int amask = 0xff000000; -#endif - - return MSDL_CreateRGBSurface(SDL_SWSURFACE, - width, height, 32, rmask, gmask, bmask, amask); -} - int SurfaceImageHelper::combineSurface(SDL_Surface *restrict const src, SDL_Rect *restrict const srcrect, SDL_Surface *restrict const dst, diff --git a/src/resources/surfaceimagehelper.h b/src/resources/surfaceimagehelper.h index fb3649f06..b526c9b64 100644 --- a/src/resources/surfaceimagehelper.h +++ b/src/resources/surfaceimagehelper.h @@ -91,9 +91,6 @@ class SurfaceImageHelper final : public ImageHelper static SDL_Surface* SDLDuplicateSurface(SDL_Surface *const tmpImage) A_WARN_UNUSED; - SDL_Surface *create32BitSurface(int width, - int height) const override final; - static int combineSurface(SDL_Surface *restrict const src, SDL_Rect *restrict const srcrect, SDL_Surface *restrict const dst, -- cgit v1.2.3-60-g2f50