ROLAND  0.70
Amstrad Emulator based on Caprice Source rewritten in C++.
pixel.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) by Fred Klaus *
3  * development@fkweb.de *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 #ifndef SDLTK_PIXEL_H
21 #define SDLTK_PIXEL_H
22 
23 #include "color.h"
24 #include "SDL.h"
25 
26 namespace sdltk
27 {
28  class Pixel
29  {
30 
31  public:
34 
40  inline static const Color get(SDL_Surface* surface, Sint16 x, Sint16 y)
41  {
42  Sint32 bpp = surface->format->BytesPerPixel;
43 
44  SDL_LockSurface(surface);
45 
46  Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
47 
48  Uint32 color = 0;
49 
50  switch(bpp)
51  {
52  case 1:
53  color = *p;
54  break;
55 
56  case 2:
57  color = *(Uint16 *)p;
58  break;
59 
60  case 3:
61  if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
62  {
63  color = p[0] << 16 | p[1] << 8 | p[2];
64  }
65  else
66  {
67  color = p[0] | p[1] << 8 | p[2] << 16;
68  }
69  break;
70 
71  case 4:
72  color = *(Uint32 *)p;
73  break;
74  }
75 
76  Uint8 r,g,b,a;
77 
78  SDL_GetRGBA(color, surface->format, &r, &g, &b, &a);
79  SDL_UnlockSurface(surface);
80 
81  return Color(r,g,b,a);
82  }
83 
89  inline static void put(SDL_Surface* surface, Sint32 x, Sint32 y, const Color& color)
90  {
91  Sint32 bpp = surface->format->BytesPerPixel;
92 
93  SDL_LockSurface(surface);
94 
95  Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
96 
97  Uint32 pixel = SDL_MapRGB(surface->format, color.r(), color.g(), color.b());
98 
99  switch(bpp)
100  {
101  case 1:
102  *p = pixel;
103  break;
104 
105  case 2:
106  *(Uint16 *)p = pixel;
107  break;
108 
109  case 3:
110  if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
111  {
112  p[0] = (pixel >> 16) & 0xff;
113  p[1] = (pixel >> 8) & 0xff;
114  p[2] = pixel & 0xff;
115  }
116  else
117  {
118  p[0] = pixel & 0xff;
119  p[1] = (pixel >> 8) & 0xff;
120  p[2] = (pixel >> 16) & 0xff;
121  }
122  break;
123 
124  case 4:
125  *(Uint32 *)p = pixel;
126  break;
127  }
128 
129  SDL_UnlockSurface(surface);
130  }
131 
136  inline static Uint32 alpha32(Uint32 src, Uint32 dst, Uint8 a)
137  {
138  Uint32 b = ((src & 0xff) * a + (dst & 0xff) * (255 - a)) >> 8;
139  Uint32 g = ((src & 0xff00) * a + (dst & 0xff00) * (255 - a)) >> 8;
140  Uint32 r = ((src & 0xff0000) * a + (dst & 0xff0000) * (255 - a)) >> 8;
141 
142  return (b & 0xff) | (g & 0xff00) | (r & 0xff0000);
143  }
144 
149  inline static Uint16 alpha16(Uint16 src, Uint16 dst, Uint8 a, const SDL_PixelFormat *f)
150  {
151  Uint32 b = ((src & f->Rmask) * a + (dst & f->Rmask) * (255 - a)) >> 8;
152  Uint32 g = ((src & f->Gmask) * a + (dst & f->Gmask) * (255 - a)) >> 8;
153  Uint32 r = ((src & f->Bmask) * a + (dst & f->Bmask) * (255 - a)) >> 8;
154 
155  return (Uint16)((b & f->Rmask) | (g & f->Gmask) | (r & f->Bmask));
156  }
157 
158  /*
159  typedef struct{
160  SDL_Palette *palette;
161  Uint8 BitsPerPixel;
162  Uint8 BytesPerPixel;
163  Uint32 Rmask, Gmask, Bmask, Amask;
164  Uint8 Rshift, Gshift, Bshift, Ashift;
165  Uint8 Rloss, Gloss, Bloss, Aloss;
166  Uint32 colorkey;
167  Uint8 alpha;
168  } SDL_PixelFormat;
169  */
170 
175  inline static void putAlpha(SDL_Surface* surface, Sint32 x, Sint32 y, const Color& color)
176  {
177  Uint32 bpp = surface->format->BytesPerPixel;
178 
179  SDL_LockSurface(surface);
180 
181  Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
182 
183  Uint32 pixel = SDL_MapRGB(surface->format, color.r(), color.g(), color.b());
184 
185  switch(bpp)
186  {
187  case 1:
188  *p = pixel;
189  break;
190 
191  case 2:
192  *(Uint16 *)p = alpha16(pixel, *(Uint32 *)p, color.a(), surface->format);
193  break;
194 
195  case 3:
196  if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
197  {
198  Uint32 r = (p[0] * (255 - color.a()) + color.r() * color.a()) >> 8;
199  Uint32 g = (p[1] * (255 - color.a()) + color.g() * color.a()) >> 8;
200  Uint32 b = (p[2] * (255 - color.a()) + color.b() * color.a()) >> 8;
201 
202  p[2] = b;
203  p[1] = g;
204  p[0] = r;
205  }
206  else
207  {
208  Uint32 r = (p[2] * (255 - color.a()) + color.r() * color.a()) >> 8;
209  Uint32 g = (p[1] * (255 - color.a()) + color.g() * color.a()) >> 8;
210  Uint32 b = (p[0] * (255 - color.a()) + color.b() * color.a()) >> 8;
211 
212  p[0] = b;
213  p[1] = g;
214  p[2] = r;
215  }
216  break;
217 
218  case 4:
219  *(Uint32 *)p = alpha32(pixel, *(Uint32 *)p, color.a());
220  break;
221  }
222 
223  SDL_UnlockSurface(surface);
224  }
225 
226  };
227 
228 } // sdltk
229 
230 #endif // SDLTK_PIXEL_H
Uint8 a
Definition: pixel.h:76
Uint8 g() const
Definition: color.h:57
Uint8 a() const
Definition: color.h:59
Definition: pixel.h:28
static Uint32 alpha32(Uint32 src, Uint32 dst, Uint8 a)
Definition: pixel.h:136
Pixel() ROLAND_DELETE~Pixel() ROLAND_DELETE inline static const Color get(SDL_Surface *surface
Uint8 * p
Definition: pixel.h:46
Sint16 Sint16 y
Definition: pixel.h:41
SDL_GetRGBA(color, surface->format,&r,&g,&b,&a)
static void put(SDL_Surface *surface, Sint32 x, Sint32 y, const Color &color)
Definition: pixel.h:89
return Color(r, g, b, a)
Uint8 g
Definition: pixel.h:76
Uint8 r() const
Definition: color.h:56
Uint8 b
Definition: pixel.h:76
Sint16 x
Definition: pixel.h:40
SDL_UnlockSurface(surface)
Uint32 color
Definition: pixel.h:48
Uint8 b() const
Definition: color.h:58
static void putAlpha(SDL_Surface *surface, Sint32 x, Sint32 y, const Color &color)
Definition: pixel.h:175
SDL_LockSurface(surface)
RGBA Color Type.
Definition: color.h:30
Uint8 r
Definition: pixel.h:76
the SDL based Stuff
Definition: audio.cpp:22
static Uint16 alpha16(Uint16 src, Uint16 dst, Uint8 a, const SDL_PixelFormat *f)
Definition: pixel.h:149
#define ROLAND_DELETE
Definition: compspec.h:47