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

#include <sdlimagehelper.h>

Inheritance diagram for SDLImageHelper:
ImageHelper

Public Member Functions

 SDLImageHelper ()
 
Imageload (SDL_RWops *const rw, Dye const &dye)
 
ImageloadSurface (SDL_Surface *const tmpImage)
 
ImagecreateTextSurface (SDL_Surface *const tmpImage, const int width, const int height, const float alpha)
 
void copySurfaceToImage (const Image *const image, const int x, const int y, SDL_Surface *const surface) const
 
- Public Member Functions inherited from ImageHelper
virtual ~ImageHelper ()
 
Imageload (SDL_RWops *const rw)
 
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 RenderType useOpenGL () const
 
virtual void postInit ()
 

Static Public Member Functions

static void SDLSetEnableAlphaCache (const bool n)
 
static bool SDLGetEnableAlphaCache ()
 
static SDL_Surface * SDLDuplicateSurface (SDL_Surface *const tmpImage)
 
static int combineSurface (SDL_Surface *const src, SDL_Rect *const srcrect, SDL_Surface *const dst, SDL_Rect *const dstrect)
 
- Static Public Member Functions inherited from ImageHelper
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)
 

Static Protected Member Functions

static Image_SDLload (SDL_Surface *tmpImage)
 

Static Protected Attributes

static bool mEnableAlphaCache = false
 
- Static Protected Attributes inherited from ImageHelper
static bool mEnableAlpha = true
 
static RenderType mUseOpenGL = RENDER_SOFTWARE
 

Friends

class Image
 

Additional Inherited Members

- Protected Member Functions inherited from ImageHelper
 ImageHelper ()
 

Detailed Description

Defines a class for loading and storing images.

Definition at line 43 of file sdlimagehelper.h.

Constructor & Destructor Documentation

◆ SDLImageHelper()

SDLImageHelper::SDLImageHelper ( )
inline

Definition at line 48 of file sdlimagehelper.h.

48  :
49  ImageHelper()
50  { }

Member Function Documentation

◆ _SDLload()

Image * SDLImageHelper::_SDLload ( SDL_Surface *  tmpImage)
staticprotected

SDL_Surface to SDL_Surface Image loader

Definition at line 218 of file sdlimagehelper.cpp.

219 {
220  if (tmpImage == nullptr)
221  return nullptr;
222 
223  bool hasAlpha = false;
224  bool converted = false;
225 
226  if (tmpImage->format->BitsPerPixel != 32)
227  {
228  reportAlways("Non 32 bit image detected")
229  tmpImage = convertTo32Bit(tmpImage);
230 
231  if (tmpImage == nullptr)
232  return nullptr;
233  converted = true;
234  }
235 
236  const size_t sz = tmpImage->w * tmpImage->h;
237 
238  // The alpha channel to be filled with alpha values
239  uint8_t *alphaChannel = new uint8_t[sz];
240 
241  // Figure out whether the image uses its alpha layer
242  if (tmpImage->format->palette == nullptr)
243  {
244  const SDL_PixelFormat *const fmt = tmpImage->format;
245  if (fmt->Amask != 0U)
246  {
247  const uint32_t amask = fmt->Amask;
248  const uint8_t ashift = fmt->Ashift;
249  const uint8_t aloss = fmt->Aloss;
250  const uint32_t *pixels = static_cast<uint32_t*>(tmpImage->pixels);
251  for (size_t i = 0; i < sz; ++ i)
252  {
253  const unsigned v = (pixels[i] & amask) >> ashift;
254  const uint8_t a = static_cast<uint8_t>((v << aloss)
255  + (v >> (8 - (aloss << 1))));
256 
257  if (a != 255)
258  hasAlpha = true;
259 
260  alphaChannel[i] = a;
261  }
262  }
263  else
264  {
265  ifconstexpr (SDL_ALPHA_OPAQUE != 255)
266  {
267  hasAlpha = true;
268  memset(alphaChannel, SDL_ALPHA_OPAQUE, sz);
269  }
270  }
271  }
272  else
273  {
274  ifconstexpr (SDL_ALPHA_OPAQUE != 255)
275  {
276  hasAlpha = true;
277  memset(alphaChannel, SDL_ALPHA_OPAQUE, sz);
278  }
279  }
280 
281  SDL_Surface *image;
282 
283  // Convert the surface to the current display format
284  if (hasAlpha)
285  {
286  image = MSDL_DisplayFormatAlpha(tmpImage);
287  }
288  else
289  {
290  image = MSDL_DisplayFormat(tmpImage);
291 
292  // We also delete the alpha channel since
293  // it's not used.
294  delete [] alphaChannel;
295  alphaChannel = nullptr;
296  }
297 
298  if (image == nullptr)
299  {
300  reportAlways("Error: Image convert failed.")
301  delete [] alphaChannel;
302  return nullptr;
303  }
304 
305  if (converted)
306  MSDL_FreeSurface(tmpImage);
307  return new Image(image, hasAlpha, alphaChannel);
308 }
#define reportAlways(...)
Definition: checkutils.h:253
static SDL_Surface * convertTo32Bit(SDL_Surface *const tmpImage)
#define MSDL_DisplayFormatAlpha(surface)
Definition: debug.h:62
#define MSDL_DisplayFormat(surface)
Definition: debug.h:63
#define MSDL_FreeSurface(surface)
Definition: debug.h:54
#define new
Definition: debug_new.h:147
if(!vert) return
#define ifconstexpr
Definition: localconsts.h:284

References ImageHelper::convertTo32Bit(), ifconstexpr, Image, MSDL_DisplayFormat, MSDL_DisplayFormatAlpha, MSDL_FreeSurface, and reportAlways.

Referenced by loadSurface().

◆ combineSurface()

int SDLImageHelper::combineSurface ( SDL_Surface *const  src,
SDL_Rect *const  srcrect,
SDL_Surface *const  dst,
SDL_Rect *const  dstrect 
)
static

Definition at line 310 of file sdlimagehelper.cpp.

314 {
315 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
316  return SDLgfxBlitRGBA(src, srcrect, dst, dstrect);
317 #else // SDL_BYTEORDER == SDL_LIL_ENDIAN
318 
319  return SDL_gfxBlitRGBA(src, srcrect, dst, dstrect);
320 #endif // SDL_BYTEORDER == SDL_LIL_ENDIAN
321 }
int SDLgfxBlitRGBA(const SDL_Surface *const src, const SDL_Rect *const srcrect, SDL_Surface *const dst, const SDL_Rect *const dstrect)

References SDLgfxBlitRGBA().

◆ copySurfaceToImage()

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

Reimplemented from ImageHelper.

Definition at line 323 of file sdlimagehelper.cpp.

326 {
327  if ((image == nullptr) || (surface == nullptr))
328  return;
329 
330  SDL_SetAlpha(surface, 0, SDL_ALPHA_OPAQUE);
331  SDL_Rect rect =
332  {
333  CAST_S16(x), CAST_S16(y),
334  CAST_U16(surface->w), static_cast<uint16_t>(surface->h)
335  };
336 
337  SDL_BlitSurface(surface, nullptr, image->mSDLSurface, &rect);
338 }
#define CAST_U16
Definition: cast.h:29
#define CAST_S16
Definition: cast.h:28

References CAST_S16, CAST_U16, x, and y.

◆ createTextSurface()

Image * SDLImageHelper::createTextSurface ( SDL_Surface *const  tmpImage,
const int  width,
const int  height,
const float  alpha 
)

Definition at line 142 of file sdlimagehelper.cpp.

146 {
147  if (tmpImage == nullptr)
148  return nullptr;
149 
150  bool hasAlpha = false;
151  const size_t sz = tmpImage->w * tmpImage->h;
152 
153  // The alpha channel to be filled with alpha values
154  uint8_t *alphaChannel = new uint8_t[sz];
155 
156  const SDL_PixelFormat *const fmt = tmpImage->format;
157  if (fmt->Amask != 0U)
158  {
159  for (size_t i = 0; i < sz; ++ i)
160  {
161  uint32_t c = (static_cast<uint32_t*>(tmpImage->pixels))[i];
162 
163  const unsigned v = (c & fmt->Amask) >> fmt->Ashift;
164  const uint8_t a = static_cast<uint8_t>((v << fmt->Aloss)
165  + (v >> (8 - (fmt->Aloss << 1))));
166 
167  const uint8_t a2 = CAST_U8(
168  static_cast<float>(a) * alpha);
169 
170  c &= ~fmt->Amask;
171  c |= ((a2 >> fmt->Aloss) << fmt->Ashift & fmt->Amask);
172  (static_cast<uint32_t*>(tmpImage->pixels))[i] = c;
173 
174  if (a != 255)
175  hasAlpha = true;
176 
177  alphaChannel[i] = a;
178  }
179  }
180 
181  SDL_Surface *image;
182 
183  // Convert the surface to the current display format
184  if (hasAlpha)
185  {
186  image = MSDL_DisplayFormatAlpha(tmpImage);
187  }
188  else
189  {
190  image = MSDL_DisplayFormat(tmpImage);
191 
192  // We also delete the alpha channel since
193  // it's not used.
194  delete [] alphaChannel;
195  alphaChannel = nullptr;
196  }
197 
198  if (image == nullptr)
199  {
200  reportAlways("Error: Image convert failed.")
201  delete [] alphaChannel;
202  return nullptr;
203  }
204 
205  Image *const img = new Image(image, hasAlpha, alphaChannel);
206  img->mAlpha = alpha;
207  return img;
208 }
#define CAST_U8
Definition: cast.h:27

References CAST_U8, Image, MSDL_DisplayFormat, MSDL_DisplayFormatAlpha, and reportAlways.

◆ load()

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

Loads an image from an SDL_RWops structure and recolors it.

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

< Surface is in system memory

Reimplemented from ImageHelper.

Definition at line 55 of file sdlimagehelper.cpp.

56 {
57  SDL_Surface *const tmpImage = loadPng(rw);
58  if (tmpImage == nullptr)
59  {
60  reportAlways("Error, image load failed: %s",
61  SDL_GetError())
62  return nullptr;
63  }
64 
65  SDL_PixelFormat rgba;
66  rgba.palette = nullptr;
67  rgba.BitsPerPixel = 32;
68  rgba.BytesPerPixel = 4;
69  rgba.colorkey = 0;
70  rgba.alpha = 255;
71  rgba.Rloss = 0;
72  rgba.Gloss = 0;
73  rgba.Bloss = 0;
74  rgba.Aloss = 0;
75 
76 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
77  rgba.Rmask = 0x000000FFU;
78  rgba.Rshift = 24;
79  rgba.Gmask = 0x0000FF00U;
80  rgba.Gshift = 16;
81  rgba.Bmask = 0x00FF0000U;
82  rgba.Bshift = 8;
83  rgba.Amask = 0xFF000000U;
84  rgba.Ashift = 0;
85 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
86 
87  rgba.Rmask = 0xFF000000U;
88  rgba.Rshift = 0;
89  rgba.Gmask = 0x00FF0000U;
90  rgba.Gshift = 8;
91  rgba.Bmask = 0x0000FF00U;
92  rgba.Bshift = 16;
93  rgba.Amask = 0x000000FFU;
94  rgba.Ashift = 24;
95 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
96 
97  // +++ here is bug on ppc64le
98  SDL_Surface *const surf = MSDL_ConvertSurface(
99  tmpImage, &rgba, SDL_SWSURFACE);
100 
101  MSDL_FreeSurface(tmpImage);
102  if (surf == nullptr)
103  return nullptr;
104 
105  uint32_t *pixels = static_cast<uint32_t *>(surf->pixels);
106  const int type = dye.getType();
107 
108  switch (type)
109  {
110  case 1:
111  {
112  const DyePalette *const pal = dye.getSPalete();
113  if (pal != nullptr)
114  DYEPALETTEP(pal, SColor)(pixels, surf->w * surf->h);
115  break;
116  }
117  case 2:
118  {
119  const DyePalette *const pal = dye.getAPalete();
120  if (pal != nullptr)
121  DYEPALETTEP(pal, AColor)(pixels, surf->w * surf->h);
122  break;
123  }
124  case 0:
125  default:
126  {
127  dye.normalDye(pixels, surf->w * surf->h);
128  break;
129  }
130  }
131 
132  Image *const image = loadSurface(surf);
133  MSDL_FreeSurface(surf);
134  return image;
135 }
static SDL_Surface * loadPng(SDL_RWops *const rw)
Image * loadSurface(SDL_Surface *const tmpImage)
#define MSDL_ConvertSurface(src, fmt, flags)
Definition: debug.h:57
#define DYEPALETTEP(palette, color)
Definition: dyepalette.h:40

References DYEPALETTEP, Dye::getAPalete(), Dye::getSPalete(), Dye::getType(), ImageHelper::loadPng(), loadSurface(), MSDL_ConvertSurface, MSDL_FreeSurface, Dye::normalDye(), and reportAlways.

◆ loadSurface()

Image * SDLImageHelper::loadSurface ( SDL_Surface *const  tmpImage)
virtual

Loads an image from an SDL surface.

Reimplemented from ImageHelper.

Definition at line 137 of file sdlimagehelper.cpp.

138 {
139  return _SDLload(tmpImage);
140 }
static Image * _SDLload(SDL_Surface *tmpImage)

References _SDLload().

Referenced by load().

◆ SDLDuplicateSurface()

SDL_Surface * SDLImageHelper::SDLDuplicateSurface ( SDL_Surface *const  tmpImage)
static

< Surface is in system memory

Definition at line 210 of file sdlimagehelper.cpp.

211 {
212  if ((tmpImage == nullptr) || (tmpImage->format == nullptr))
213  return nullptr;
214 
215  return MSDL_ConvertSurface(tmpImage, tmpImage->format, SDL_SWSURFACE);
216 }

References MSDL_ConvertSurface.

◆ SDLGetEnableAlphaCache()

static bool SDLImageHelper::SDLGetEnableAlphaCache ( )
inlinestatic

Definition at line 85 of file sdlimagehelper.h.

86  { return mEnableAlphaCache; }
static bool mEnableAlphaCache

References mEnableAlphaCache.

◆ SDLSetEnableAlphaCache()

static void SDLImageHelper::SDLSetEnableAlphaCache ( const bool  n)
inlinestatic

Definition at line 82 of file sdlimagehelper.h.

83  { mEnableAlphaCache = n; }

References mEnableAlphaCache.

Friends And Related Function Documentation

◆ Image

friend class Image
friend

Definition at line 45 of file sdlimagehelper.h.

Referenced by _SDLload(), and createTextSurface().

Field Documentation

◆ mEnableAlphaCache

bool SDLImageHelper::mEnableAlphaCache = false
staticprotected

Definition at line 100 of file sdlimagehelper.h.

Referenced by SDLGetEnableAlphaCache(), and SDLSetEnableAlphaCache().


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