Image.cpp
Go to the documentation of this file.
1 /* ========================================================================
2  * Copyright (C) 2004-2005 Graz University of Technology
3  *
4  * This framework is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This framework is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this framework; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * For further information please contact Dieter Schmalstieg under
19  * <schmalstieg@icg.tu-graz.ac.at> or write to Dieter Schmalstieg,
20  * Graz University of Technology, Institut für Maschinelles Sehen und Darstellen,
21  * Inffeldgasse 16a, 8010 Graz, Austria.
22  * ========================================================================
23  * PROJECT: PocketKnife
24  * ======================================================================== */
29 /* ======================================================================== */
30 
31 
32 #include "PocketKnife.h"
33 #include "Image.h"
34 #include "ImageTool.h"
35 
36 #include <cstdio>
37 #include <cstdlib>
38 #include <string>
39 #include <cstring>
40 
41 #if defined(TARGET_HOST_WIN32) || defined(TARGET_HOST_WINCE)
42 #include <windows.h>
43 #endif
44 
45 
46 namespace PN {
47 
48 
49 void
50 Image::clear(unsigned short nColor)
51 {
52  int size = width*height;
53 
54  if((size&7)==0)
55  {
56  size >>= 3;
57  unsigned int col32 = nColor | (nColor<<16);
58  unsigned int* dst = (unsigned int*)getPixels();
59 
60  while(size--)
61  {
62  dst[0] = col32;
63  dst[1] = col32;
64  dst[2] = col32;
65  dst[3] = col32;
66 
67  dst += 4;
68  }
69  }
70  else
71  {
72  unsigned short *dst = getPixels();
73 
74  while(size--)
75  *dst++ = nColor;
76  }
77 }
78 
79 
80 void
81 Image::clear(int nRed, int nGreen, int nBlue)
82 {
83  clear(ImageTool::convertPixel24To16(nRed, nGreen, nBlue));
84 }
85 
86 
87 void
88 Image::drawImage(int nX, int nY, const Image* nImage,
89  int nSx0, int nSy0, int nSx1, int nSy1,
90  bool nTransparent)
91 {
92  int sw = nImage->getWidth(),
93  sx0 = nSx0, sy0 = nSy0, sx1 = nSx1, sy1 = nSy1,
94  dx0 = nX, dy0 = nY, dx1 = nX+nSx1-nSx0, dy1 = nY+nSy1-nSy0;
95 
96  if(dx0>=width || dx1<0 || dy0>=height || dy1<0)
97  return;
98 
99  const unsigned short* src = nImage->getPixels();
100  unsigned short* dst = getPixels();
101 
102  if(dx0<0)
103  { sx0 += -dx0; dx0 = 0; }
104  if(dy0<0)
105  { sy0 += -dy0; dy0 = 0; }
106 
107  if(dx1>width)
108  { sx1 -= dx1-width; dx1 = width; }
109  if(dy1>height)
110  { sy1 -= dy1-height; dy1 = height; }
111 
112  int w = sx1-sx0, h = sy1-sy0, x,y;
113 
114  src += sy0*sw + sx0;
115  dst += dy0*width + dx0;
116 
117  if(!nTransparent)
118  for(y=0; y<h; y++)
119  {
120  memcpy(dst, src, 2*w);
121  dst += width;
122  src += sw;
123  }
124  else
125  {
126  unsigned short key = nImage->getColorKey();
127 
128  for(y=0; y<h; y++)
129  {
130  for(x=0; x<w; x++)
131  {
132  if(*src != key)
133  *dst = *src;
134  dst++;
135  src++;
136  }
137  dst += width-w;
138  src += sw-w;
139  }
140  }
141 }
142 
143 void
144 Image::drawImage(int nX, int nY, const Image* nImage, bool nTransparent)
145 {
146  drawImage(nX, nY, nImage,
147  0,0, nImage->getWidth(),nImage->getHeight(),
148  nTransparent);
149 }
150 
151 
152 void
153 Image::fillRect(int nX0, int nY0, int nX1, int nY1, int nRed, int nGreen, int nBlue, int nOpacity)
154 {
155  fillRect(nX0,nY0, nX1,nY1, ImageTool::convertPixel24To16(nRed,nGreen,nBlue), nOpacity);
156 }
157 
158 void
159 Image::fillRect(int nX0, int nY0, int nX1, int nY1, unsigned short nColor, int nOpacity)
160 {
161  if(nX0<0) nX0 = 0;
162  if(nY0<0) nY0 = 0;
163  if(nX1>width) nX1 = width;
164  if(nY1>height) nY1 = height;
165 
166  int w,w0 = nX1-nX0, h = nY1-nY0;
167  unsigned short* dst = (unsigned short*)getPixels();
168 
169  dst += nX0 + nY0*width;
170 
171  if(nOpacity==0)
172  while(h--)
173  {
174  w = w0;
175  while(w--)
176  *dst++ = nColor;
177 
178  dst += width-w0;
179  }
180  else
181  {
182  while(h--)
183  {
184  w = w0;
185  while(w--)
186  {
187  *dst = ImageTool::blendPixel16(*dst, nColor, nOpacity);
188  dst++;
189  }
190 
191  dst += width-w0;
192  }
193  }
194 }
195 
196 
197 void
198 Image::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b)
199 {
200  drawLine(x1,y1, x2,y2, ImageTool::convertPixel24To16(r, g, b));
201 }
202 
203 void
204 Image::drawLine(int x1, int y1, int x2, int y2, unsigned short color)
205 {
206  int x=x1, y=y1;
207  int dx, dy;
208  int incx, incy;
209  int balance;
210 
211  if(x2 >= x1)
212  { dx = x2 - x1; incx = 1; }
213  else
214  { dx = x1 - x2; incx = -1; }
215 
216  if (y2 >= y1)
217  { dy = y2 - y1; incy = 1; }
218  else
219  { dy = y1 - y2; incy = -1; }
220 
221  int offset = y*width + x,
222  incxBuf = incx,
223  incyBuf = incy*width;
224 
225  unsigned short* pixels = getPixels();
226 
227  if(dx==0 && dy==0)
228  {
229  pixels[offset] = color;
230  return;
231  }
232 
233  if (dx >= dy)
234  {
235  dy <<= 1;
236  balance = dy - dx;
237  dx <<= 1;
238 
239  while (x != x2)
240  {
241  pixels[offset] = color;
242  if (balance >= 0)
243  {
244  y += incy; offset += incyBuf;
245  balance -= dx;
246  }
247  balance += dy;
248  x += incx; offset += incxBuf;
249  }
250  pixels[offset] = color;
251  }
252  else
253  {
254  dx <<= 1;
255  balance = dx - dy;
256  dy <<= 1;
257  while (y != y2)
258  {
259  pixels[offset] = color;
260  if (balance >= 0)
261  {
262  x += incx; offset += incxBuf;
263  balance -= dy;
264  }
265  balance += dx;
266  y += incy; offset += incyBuf;
267  }
268  pixels[offset] = color;
269  }
270 }
271 
272 
273 void
274 Image::setPixel(int x, int y, unsigned short col)
275 {
276  unsigned short* pixels = getPixels();
277  pixels[y*width + x] = col;
278 }
279 
280 
281 void
282 Image::setPixel(int x, int y, int r, int g, int b)
283 {
284  unsigned short* pixels = getPixels();
285  pixels[y*width + x] = ImageTool::convertPixel24To16(r, g, b);
286 }
287 
288 Image* Image::createFromPixelBuffer(int nWidth, int nHeight, unsigned short* nPixels, bool nOwner)
289 {
290  if(nOwner && nPixels==NULL)
291  nPixels = new unsigned short[nWidth*nHeight];
292  return new Image(nWidth, nHeight, nPixels, nOwner);
293 }
294 
295 
296 void Image::setPixels(int nWidth, int nHeight, unsigned short* nPixels, bool nPixelsOwner)
297 {
298  if( pixelsOwner )
299  delete pixels;
300 
301  width = nWidth;
302  height = nHeight;
303  pixels = nPixels;
304  pixelsOwner = nPixelsOwner;
305 }
306 
307 
308 } //namespace PN
void setPixel(int x, int y, unsigned short col)
Sets a pixel.
Definition: Image.cpp:274
int getHeight() const
Returns the height of the image.
Definition: Image.h:62
void drawLine(int x1, int y1, int x2, int y2, unsigned short col)
Renders a straight line from x1/y1 to x2/y1 with the color &#39;col&#39;.
Definition: Image.cpp:204
void drawImage(int nX, int nY, const Image *nImage, int nSx0, int nSy0, int nSx1, int nSy1, bool nTransparent=false)
Draws another bitmap inside this bitmap.
Definition: Image.cpp:88
Definition: Image.cpp:46
static Image * createFromPixelBuffer(int nWidth, int nHeight, unsigned short *nPixels, bool nOwner)
Creates an Image object directly from a pixel buffer.
Definition: Image.cpp:288
void fillRect(int nX0, int nY0, int nX1, int nY1, int nRed, int nGreen, int nBlue, int nTransparency=0)
Fills a rectangle with the given color ans transparency.
Definition: Image.cpp:153
unsigned short convertPixel24To16(int nRed, int nGreen, int nBlue)
Converts a single pixel from 24-bits RGB to 16-bits (565) RGB.
Definition: ImageTool.h:57
TFSIMD_FORCE_INLINE const tfScalar & y() const
unsigned short getColorKey() const
Definition: Image.h:151
void clear(int nRed, int nGreen, int nBlue)
Clears the bitmap with the given RGB color.
Definition: Image.cpp:81
unsigned short * pixels
Definition: Image.h:166
void setPixels(int nWidth, int nHeight, unsigned short *nPixels, bool nPixelsOwner)
Sets a new pixel buffer.
Definition: Image.cpp:296
TFSIMD_FORCE_INLINE const tfScalar & x() const
Image(int nWidth, int nHeight, unsigned short *nPixels, bool nPixelsOwner)
Definition: Image.h:154
int getWidth() const
Returns the width of the image.
Definition: Image.h:59
TFSIMD_FORCE_INLINE const tfScalar & w() const
#define NULL
Definition: PocketKnife.h:38
unsigned short blendPixel16(unsigned short nSrc, unsigned short nDst, int nOpacity)
Blends a single pixel in 16-bits RGB565 format.
Definition: ImageTool.h:73
The Image class provides basic RGB565 image handing capabilities.
Definition: Image.h:42
bool pixelsOwner
Definition: Image.h:167
unsigned short * getPixels()
Returns the pixel buffer.
Definition: Image.h:133
int width
Definition: Image.h:165
int height
Definition: Image.h:165


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun Sep 4 2016 03:24:33