ManaPlus
sdlimagehelper.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef USE_SDL2
25 
27 
28 #include "resources/dye/dye.h"
30 
31 #include "resources/image/image.h"
32 
33 #include "utils/checkutils.h"
34 #include "utils/sdlcheckutils.h"
35 
36 #include "localconsts.h"
37 
38 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
40 #else // SDL_BYTEORDER == SDL_LIL_ENDIAN
41 PRAGMA48(GCC diagnostic push)
42 PRAGMA48(GCC diagnostic ignored "-Wshadow")
43 #include <SDL_gfxBlitFunc.h>
44 PRAGMA48(GCC diagnostic pop)
45 #endif // SDL_BYTEORDER == SDL_LIL_ENDIAN
46 
47 #ifndef SDL_BIG_ENDIAN
48 #error missing SDL_endian.h
49 #endif // SDL_BYTEORDER
50 
51 #include "debug.h"
52 
54 
55 Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye)
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 }
136 
137 Image *SDLImageHelper::loadSurface(SDL_Surface *const tmpImage)
138 {
139  return _SDLload(tmpImage);
140 }
141 
142 Image *SDLImageHelper::createTextSurface(SDL_Surface *const tmpImage,
143  const int width A_UNUSED,
144  const int height A_UNUSED,
145  const float alpha)
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 }
209 
210 SDL_Surface* SDLImageHelper::SDLDuplicateSurface(SDL_Surface *const tmpImage)
211 {
212  if ((tmpImage == nullptr) || (tmpImage->format == nullptr))
213  return nullptr;
214 
215  return MSDL_ConvertSurface(tmpImage, tmpImage->format, SDL_SWSURFACE);
216 }
217 
218 Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage)
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 }
309 
310 int SDLImageHelper::combineSurface(SDL_Surface *restrict const src,
311  SDL_Rect *restrict const srcrect,
312  SDL_Surface *restrict const dst,
313  SDL_Rect *restrict const dstrect)
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 }
322 
324  const int x, const int y,
325  SDL_Surface *const surface) const
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 }
339 
340 #endif // USE_SDL2
#define CAST_U16
Definition: cast.h:29
#define CAST_S16
Definition: cast.h:28
#define CAST_U8
Definition: cast.h:27
#define reportAlways(...)
Definition: checkutils.h:253
Definition: dye.h:41
const DyePalette * getSPalete() const
Definition: dye.h:67
const DyePalette * getAPalete() const
Definition: dye.h:73
void normalDye(uint32_t *pixels, const int bufSize) const
Definition: dye.cpp:158
int getType() const
Definition: dye.cpp:149
static SDL_Surface * convertTo32Bit(SDL_Surface *const tmpImage)
static SDL_Surface * loadPng(SDL_RWops *const rw)
static Image * _SDLload(SDL_Surface *tmpImage)
friend class Image
static int combineSurface(SDL_Surface *const src, SDL_Rect *const srcrect, SDL_Surface *const dst, SDL_Rect *const dstrect)
static SDL_Surface * SDLDuplicateSurface(SDL_Surface *const tmpImage)
void copySurfaceToImage(const Image *const image, const int x, const int y, SDL_Surface *const surface) const
Image * createTextSurface(SDL_Surface *const tmpImage, const int width, const int height, const float alpha)
Image * load(SDL_RWops *const rw, Dye const &dye)
static bool mEnableAlphaCache
Image * loadSurface(SDL_Surface *const tmpImage)
#define MSDL_ConvertSurface(src, fmt, flags)
Definition: debug.h:57
#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 DYEPALETTEP(palette, color)
Definition: dyepalette.h:40
#define restrict
Definition: localconsts.h:165
#define ifconstexpr
Definition: localconsts.h:284
#define PRAGMA48(str)
Definition: localconsts.h:199
#define A_UNUSED
Definition: localconsts.h:160
int SDLgfxBlitRGBA(const SDL_Surface *const src, const SDL_Rect *const srcrect, SDL_Surface *const dst, const SDL_Rect *const dstrect)