ManaPlus
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Attributes | Friends
ImageHelper Class Reference

#include <imagehelper.h>

Inheritance diagram for ImageHelper:
OpenGLImageHelper SafeOpenGLImageHelper SDLImageHelper

Public Member Functions

virtual ~ImageHelper ()
 
Imageload (SDL_RWops *const rw)
 
virtual Imageload (SDL_RWops *const rw, Dye const &dye)
 
virtual ImageloadSurface (SDL_Surface *const)
 
virtual ImagecreateTextSurface (SDL_Surface *const tmpImage, const int width, const int height, const float alpha) const
 
virtual SDL_Surface * create32BitSurface (int width, int height) const
 
virtual void copySurfaceToImage (const Image *const image, const int x, const int y, SDL_Surface *const surface) const
 
virtual RenderType useOpenGL () const
 
virtual void postInit ()
 

Static Public Member Functions

static SDL_Surface * convertTo32Bit (SDL_Surface *const tmpImage)
 
static void dumpSurfaceFormat (const SDL_Surface *const image)
 
static void setEnableAlpha (const bool n)
 
static SDL_Surface * loadPng (SDL_RWops *const rw)
 
static void setOpenGlMode (const RenderType useOpenGL)
 

Protected Member Functions

 ImageHelper ()
 

Static Protected Attributes

static bool mEnableAlpha = true
 
static RenderType mUseOpenGL = RENDER_SOFTWARE
 

Friends

class CompoundSprite
 
class Image
 

Detailed Description

Defines a class for loading and storing images.

Definition at line 41 of file imagehelper.h.

Constructor & Destructor Documentation

◆ ~ImageHelper()

virtual ImageHelper::~ImageHelper ( )
inlinevirtual

Definition at line 49 of file imagehelper.h.

References A_WARN_UNUSED, createTextSurface(), ServerCommandType::dye, load(), and loadSurface().

50  { }

◆ ImageHelper()

ImageHelper::ImageHelper ( )
inlineprotected

Definition at line 113 of file imagehelper.h.

114  { }

Member Function Documentation

◆ convertTo32Bit()

SDL_Surface * ImageHelper::convertTo32Bit ( SDL_Surface *const  tmpImage)
static

< Surface is in system memory

Definition at line 133 of file imagehelper.cpp.

References MSDL_ConvertSurface.

Referenced by SDLImageHelper::_SDLload(), copySurfaceToImage(), SafeOpenGLImageHelper::load(), OpenGLImageHelper::load(), main(), and TestLauncher::testTextures().

134 {
135  if (tmpImage == nullptr)
136  return nullptr;
137  SDL_PixelFormat RGBAFormat;
138  RGBAFormat.palette = nullptr;
139 #ifndef USE_SDL2
140  RGBAFormat.colorkey = 0;
141  RGBAFormat.alpha = 0;
142 #endif // USE_SDL2
143 
144  RGBAFormat.BitsPerPixel = 32;
145  RGBAFormat.BytesPerPixel = 4;
146 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
147  RGBAFormat.Rmask = 0xFF000000;
148  RGBAFormat.Rshift = 0;
149  RGBAFormat.Rloss = 0;
150  RGBAFormat.Gmask = 0x00FF0000;
151  RGBAFormat.Gshift = 8;
152  RGBAFormat.Gloss = 0;
153  RGBAFormat.Bmask = 0x0000FF00;
154  RGBAFormat.Bshift = 16;
155  RGBAFormat.Bloss = 0;
156  RGBAFormat.Amask = 0x000000FF;
157  RGBAFormat.Ashift = 24;
158  RGBAFormat.Aloss = 0;
159 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
160 
161  RGBAFormat.Rmask = 0x000000FF;
162  RGBAFormat.Rshift = 24;
163  RGBAFormat.Rloss = 0;
164  RGBAFormat.Gmask = 0x0000FF00;
165  RGBAFormat.Gshift = 16;
166  RGBAFormat.Gloss = 0;
167  RGBAFormat.Bmask = 0x00FF0000;
168  RGBAFormat.Bshift = 8;
169  RGBAFormat.Bloss = 0;
170  RGBAFormat.Amask = 0xFF000000;
171  RGBAFormat.Ashift = 0;
172  RGBAFormat.Aloss = 0;
173 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
174 
175  return MSDL_ConvertSurface(tmpImage, &RGBAFormat, SDL_SWSURFACE);
176 }
#define MSDL_ConvertSurface(src, fmt, flags)
Definition: debug.h:56

◆ copySurfaceToImage()

virtual void ImageHelper::copySurfaceToImage ( const Image *const  image,
const int  x,
const int  y,
SDL_Surface *const  surface 
) const
inlinevirtual

Reimplemented in OpenGLImageHelper, SafeOpenGLImageHelper, and SDLImageHelper.

Definition at line 85 of file imagehelper.h.

References A_WARN_UNUSED, convertTo32Bit(), and dumpSurfaceFormat().

Referenced by AtlasManager::createSDLAtlas().

90  { }

◆ create32BitSurface()

SDL_Surface * ImageHelper::create32BitSurface ( int  width,
int  height 
) const
virtual

< Surface is in system memory

Reimplemented in OpenGLImageHelper, and SafeOpenGLImageHelper.

Definition at line 238 of file imagehelper.cpp.

References MSDL_CreateRGBSurface.

Referenced by createTextSurface(), TextChunk::generate(), TestLauncher::testBlitSpeed(), and TestLauncher::testTextures().

240 {
241 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
242  const int rmask = 0xff000000;
243  const int gmask = 0x00ff0000;
244  const int bmask = 0x0000ff00;
245  const int amask = 0x000000ff;
246 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
247 
248  const int rmask = 0x000000ff;
249  const int gmask = 0x0000ff00;
250  const int bmask = 0x00ff0000;
251  const int amask = 0xff000000;
252 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
253 
254  return MSDL_CreateRGBSurface(SDL_SWSURFACE,
255  width, height, 32, rmask, gmask, bmask, amask);
256 }
#define MSDL_CreateRGBSurface(flags, w, h, d, r, g, b, a)
Definition: debug.h:54

◆ createTextSurface()

virtual Image* ImageHelper::createTextSurface ( SDL_Surface *const  tmpImage,
const int  width,
const int  height,
const float  alpha 
) const
inlinevirtual

Definition at line 75 of file imagehelper.h.

References A_WARN_UNUSED, and create32BitSurface().

Referenced by TextChunk::generate(), and ~ImageHelper().

78  { return nullptr; }

◆ dumpSurfaceFormat()

void ImageHelper::dumpSurfaceFormat ( const SDL_Surface *const  image)
static

Definition at line 178 of file imagehelper.cpp.

References CAST_S32, CAST_U32, Logger::log(), and logger.

Referenced by copySurfaceToImage(), and Graphics::videoInfo().

179 {
180  if (image == nullptr)
181  return;
182  if (image->format != nullptr)
183  {
184  const SDL_PixelFormat * const format = image->format;
185  logger->log("Bytes per pixel: %d", format->BytesPerPixel);
186 #ifdef USE_SDL2
187  logger->log("Format: %u", format->format);
188 #else // USE_SDL2
189 
190  logger->log("Alpha: %d", format->alpha);
191  logger->log("Color key: %u", format->colorkey);
192 #endif // USE_SDL2
193 
194  logger->log("Loss: %02x, %02x, %02x, %02x",
195  CAST_U32(format->Rloss),
196  CAST_U32(format->Gloss),
197  CAST_U32(format->Bloss),
198  CAST_U32(format->Aloss));
199  logger->log("Shift: %02x, %02x, %02x, %02x",
200  CAST_U32(format->Rshift),
201  CAST_U32(format->Gshift),
202  CAST_U32(format->Bshift),
203  CAST_U32(format->Ashift));
204  logger->log("Mask: %08x, %08x, %08x, %08x", format->Rmask,
205  format->Gmask, format->Bmask, format->Amask);
206  }
207  logger->log("Flags: %u", image->flags);
208  logger->log("Pitch: %d", CAST_S32(image->pitch));
209 #ifndef USE_SDL2
210  logger->log("Offset: %d", image->offset);
211 #endif // USE_SDL2
212 }
#define CAST_U32
Definition: cast.h:30
Logger * logger
Definition: logger.cpp:95
#define CAST_S32
Definition: cast.h:29
void log(const char *const log_text,...)
Definition: logger.cpp:243

◆ load() [1/2]

Image * ImageHelper::load ( SDL_RWops *const  rw)

Loads an image from an SDL_RWops structure.

Parameters
rwThe SDL_RWops to load the image from.
Returns
NULL if an error occurred, a valid pointer otherwise.

Definition at line 49 of file imagehelper.cpp.

References loadPng(), loadSurface(), Logger::log(), logger, and MSDL_FreeSurface.

Referenced by anonymous_namespace{imageloader.cpp}::DyedImageLoader::load(), AtlasManager::loadImages(), TestLauncher::testTextures(), and ~ImageHelper().

50 {
51  SDL_Surface *const tmpImage = loadPng(rw);
52  if (tmpImage == nullptr)
53  {
54  logger->log("Error, image load failed: %s", SDL_GetError());
55  return nullptr;
56  }
57 
58  Image *const image = loadSurface(tmpImage);
59 
60  MSDL_FreeSurface(tmpImage);
61  return image;
62 }
static SDL_Surface * loadPng(SDL_RWops *const rw)
#define MSDL_FreeSurface(surface)
Definition: debug.h:53
Logger * logger
Definition: logger.cpp:95
virtual Image * loadSurface(SDL_Surface *const)
Definition: imagehelper.h:72
Definition: image.h:61
void log(const char *const log_text,...)
Definition: logger.cpp:243

◆ load() [2/2]

Image * ImageHelper::load ( SDL_RWops *const  rw,
Dye const &  dye 
)
virtual

< Surface is in system memory

Reimplemented in OpenGLImageHelper, SafeOpenGLImageHelper, and SDLImageHelper.

Definition at line 64 of file imagehelper.cpp.

References BLOCK_END, BLOCK_START, DYEPALETTEP, Dye::getAPalete(), Dye::getSPalete(), Dye::getType(), loadPng(), loadSurface(), Logger::log(), logger, MSDL_ConvertSurface, MSDL_FreeSurface, and Dye::normalDye().

65 {
66  BLOCK_START("ImageHelper::load")
67  SDL_Surface *const tmpImage = loadPng(rw);
68  if (tmpImage == nullptr)
69  {
70  logger->log("Error, image load failed: %s", SDL_GetError());
71  BLOCK_END("ImageHelper::load")
72  return nullptr;
73  }
74 
75  SDL_PixelFormat rgba;
76  rgba.palette = nullptr;
77  rgba.BitsPerPixel = 32;
78  rgba.BytesPerPixel = 4;
79 
80 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
81  rgba.Rmask = 0x000000FF;
82  rgba.Gmask = 0x0000FF00;
83  rgba.Bmask = 0x00FF0000;
84  rgba.Amask = 0xFF000000;
85 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
86 
87  rgba.Rmask = 0xFF000000;
88  rgba.Gmask = 0x00FF0000;
89  rgba.Bmask = 0x0000FF00;
90  rgba.Amask = 0x000000FF;
91 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
92 
93  SDL_Surface *const surf = MSDL_ConvertSurface(
94  tmpImage, &rgba, SDL_SWSURFACE);
95  MSDL_FreeSurface(tmpImage);
96 
97  if (surf == nullptr)
98  return nullptr;
99 
100  uint32_t *const pixels = static_cast<uint32_t *>(surf->pixels);
101  const int type = dye.getType();
102 
103  switch (type)
104  {
105  case 1:
106  {
107  const DyePalette *const pal = dye.getSPalete();
108  if (pal != nullptr)
109  DYEPALETTEP(pal, SColor)(pixels, surf->w * surf->h);
110  break;
111  }
112  case 2:
113  {
114  const DyePalette *const pal = dye.getAPalete();
115  if (pal != nullptr)
116  DYEPALETTEP(pal, AColor)(pixels, surf->w * surf->h);
117  break;
118  }
119  case 0:
120  default:
121  {
122  dye.normalDye(pixels, surf->w * surf->h);
123  break;
124  }
125  }
126 
127  Image *const image = loadSurface(surf);
128  MSDL_FreeSurface(surf);
129  BLOCK_END("ImageHelper::load")
130  return image;
131 }
static SDL_Surface * loadPng(SDL_RWops *const rw)
#define MSDL_FreeSurface(surface)
Definition: debug.h:53
#define BLOCK_START(name)
Definition: perfomance.h:78
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:79
Logger * logger
Definition: logger.cpp:95
#define DYEPALETTEP(palette, color)
Definition: dyepalette.h:39
virtual Image * loadSurface(SDL_Surface *const)
Definition: imagehelper.h:72
#define MSDL_ConvertSurface(src, fmt, flags)
Definition: debug.h:56
Definition: image.h:61
void log(const char *const log_text,...)
Definition: logger.cpp:243

◆ loadPng()

SDL_Surface * ImageHelper::loadPng ( SDL_RWops *const  rw)
static

Definition at line 214 of file imagehelper.cpp.

References Logger::log(), logger, MIMG_LoadJPG_RW, and MIMG_LoadPNG_RW.

Referenced by load(), SDLImageHelper::load(), SafeOpenGLImageHelper::load(), OpenGLImageHelper::load(), and setEnableAlpha().

215 {
216  if (rw == nullptr)
217  return nullptr;
218 
219  if (IMG_isPNG(rw) != 0)
220  {
221  SDL_Surface *const tmpImage = MIMG_LoadPNG_RW(rw);
222  SDL_RWclose(rw);
223  return tmpImage;
224  }
225 
226  if (IMG_isJPG(rw) != 0)
227  {
228  SDL_Surface *const tmpImage = MIMG_LoadJPG_RW(rw);
229  SDL_RWclose(rw);
230  return tmpImage;
231  }
232 
233  logger->log("Error, image is not png");
234  SDL_RWclose(rw);
235  return nullptr;
236 }
#define MIMG_LoadPNG_RW(src)
Definition: debug.h:51
Logger * logger
Definition: logger.cpp:95
#define MIMG_LoadJPG_RW(src)
Definition: debug.h:52
void log(const char *const log_text,...)
Definition: logger.cpp:243

◆ loadSurface()

virtual Image* ImageHelper::loadSurface ( SDL_Surface *  const)
inlinevirtual

◆ postInit()

virtual void ImageHelper::postInit ( )
inlinevirtual

Reimplemented in OpenGLImageHelper, and SafeOpenGLImageHelper.

Definition at line 109 of file imagehelper.h.

Referenced by Client::initConfigListeners(), and Client::initGraphics().

110  { }

◆ setEnableAlpha()

static void ImageHelper::setEnableAlpha ( const bool  n)
inlinestatic

Definition at line 97 of file imagehelper.h.

References loadPng(), and mEnableAlpha.

Referenced by GraphicsManager::initGraphics(), and Client::optionChanged().

98  { mEnableAlpha = n; }
static bool mEnableAlpha
Definition: imagehelper.h:116

◆ setOpenGlMode()

static void ImageHelper::setOpenGlMode ( const RenderType  useOpenGL)
inlinestatic

Definition at line 102 of file imagehelper.h.

References mUseOpenGL, and useOpenGL().

Referenced by GraphicsManager::createRenderers().

104  { mUseOpenGL = useOpenGL; }
virtual RenderType useOpenGL() const
Definition: imagehelper.h:106
static RenderType mUseOpenGL
Definition: imagehelper.h:117

◆ useOpenGL()

virtual RenderType ImageHelper::useOpenGL ( ) const
inlinevirtual

Friends And Related Function Documentation

◆ CompoundSprite

friend class CompoundSprite
friend

Definition at line 43 of file imagehelper.h.

◆ Image

friend class Image
friend

Definition at line 44 of file imagehelper.h.

Field Documentation

◆ mEnableAlpha

bool ImageHelper::mEnableAlpha = true
staticprotected

Definition at line 116 of file imagehelper.h.

Referenced by CompoundSprite::redraw(), Image::setAlpha(), and setEnableAlpha().

◆ mUseOpenGL

RenderType ImageHelper::mUseOpenGL = RENDER_SOFTWARE
staticprotected

The documentation for this class was generated from the following files: