Main MRPT website > C++ reference for MRPT 1.5.7
CCanvas.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 
13 #include <mrpt/utils/CCanvas.h>
14 #include <mrpt/system/os.h>
15 #include <mrpt/utils/CImage.h>
19 #include <mrpt/utils/round.h>
20 
21 #include <mrpt/compress/zip.h>
22 #include <map>
23 
24 
25 // Include the MRPT bitmap fonts:
26 #include "mrpt_font_5x7.h"
27 #include "mrpt_font_6x13.h"
28 #include "mrpt_font_6x13B.h"
29 #include "mrpt_font_6x13O.h"
30 #include "mrpt_font_9x15.h"
31 #include "mrpt_font_9x15B.h"
32 #include "mrpt_font_10x20.h"
33 
34 
35 // Japanese fonts?
36 #if MRPT_HAS_ASIAN_FONTS
37  #include "mrpt_font_18x18ja.h"
38 #endif
39 
40 // Each font has a block a data with this header (It's actually zip-compressed since mrpt >0.6.5)
41 // const uint32_t mrpt_font_9x15B [] = {
42 // 9,15, /* width, height */
43 // 0x0000,0x00FF, /* UNICODE characters range: */
44 
45 
46 using namespace mrpt;
47 using namespace mrpt::utils;
48 using namespace std;
49 
50 //map<string,const uint32_t*> list_registered_fonts;
51 map<string,vector_byte> list_registered_fonts; // Each vector is the target place where to uncompress each font.
52 bool list_fonts_init = false;
53 
55 {
56  if (!list_fonts_init)
57  {
58  list_registered_fonts.clear();
59 
60  // This was used only once
61 #if 0
62  #define SAVE_COMPRESSED( ARR ) \
63  { \
64  list_registered_fonts[#ARR].resize(sizeof(mrpt_font_##ARR)); \
65  memcpy(&list_registered_fonts[#ARR][0], mrpt_font_##ARR, sizeof(mrpt_font_##ARR)); \
66  cout << #ARR << " -> " << sizeof(mrpt_font_##ARR) << endl; \
67  CFileGZOutputStream f(string("mrpt_font_")+string(#ARR)+string(".gz")); \
68  f.WriteBuffer(mrpt_font_##ARR, sizeof(mrpt_font_##ARR)); \
69  /*mrpt::compress::zip::compress( list_registered_fonts[#ARR], f ); */ \
70  }
71 
72  SAVE_COMPRESSED(5x7)
73 // SAVE_COMPRESSED(6x13)
74 // SAVE_COMPRESSED(6x13B)
75 // SAVE_COMPRESSED(6x13O)
76 // SAVE_COMPRESSED(9x15)
77 // SAVE_COMPRESSED(9x15B)
78 // SAVE_COMPRESSED(10x20)
79 
80  #if MRPT_HAS_ASIAN_FONTS
81 // SAVE_COMPRESSED(18x18ja)
82  #endif
83 
84 #endif
85 
86 #if 1 // Normal operation: Load fonts and uncompress them:
87 
88  #define LOAD_FONT(FONTNAME) \
89  { \
90  vector_byte tmpBuf(sizeof(mrpt_font_gz_##FONTNAME)); \
91  memcpy(&tmpBuf[0], mrpt_font_gz_##FONTNAME, sizeof(mrpt_font_gz_##FONTNAME)); \
92  mrpt::compress::zip::decompress_gz_data_block(tmpBuf,list_registered_fonts[#FONTNAME]); \
93  }
94 
95  LOAD_FONT(5x7)
96  LOAD_FONT(6x13)
97  LOAD_FONT(6x13B)
98  LOAD_FONT(6x13O)
99  LOAD_FONT(9x15)
100  LOAD_FONT(9x15B)
101  LOAD_FONT(10x20)
102 #if MRPT_HAS_ASIAN_FONTS
103  LOAD_FONT(18x18ja)
104 #endif
105 
106 #endif
107 
108  list_fonts_init=true;
109  }
110 }
111 
112 
113 /*---------------------------------------------------------------
114  Constructor
115 ---------------------------------------------------------------*/
117  m_selectedFont("9x15"),
118  m_selectedFontBitmaps(NULL)
119 {
120 }
121 
122 /*---------------------------------------------------------------
123  line
124 ---------------------------------------------------------------*/
126  int x0,
127  int y0,
128  int x1,
129  int y1,
131  unsigned int width,
132  TPenStyle penStyle
133  )
134 {
136  MRPT_UNUSED_PARAM(penStyle);
137 
138 /* // JL: worthy annoying so much?
139  static bool warningFirst = true;
140  if (warningFirst)
141  {
142  warningFirst=false;
143  printf("[CCanvas::line] WARNING: Using default drawing method, ignoring 'width' and 'penStyle'!!\n");
144  }*/
145 
146  float x,y;
147 
148  float Ax = (float)( x1-x0 );
149  float Ay = (float)( y1-y0 );
150 
151  // In this cases, there is nothing to do!
152  if (Ax==0 && Ay==0) return;
153  if (x0<0 && x1<0) return;
154  if (y0<0 && y1<0) return;
155  if (x0>=(int)getWidth() && x1>=(int)getWidth()) return;
156  if (y0>=(int)getHeight() && y1>=(int)getHeight()) return;
157 
158  float dist = sqrt( square(Ax)+square(Ay) );
159  int i,N = (int)ceil(dist);
160 
161  // The N steps to perform next:
162  Ax/=N; Ay/=N;
163  x = (float)x0; y = (float)y0;
164 
165  for (i=0;i<N;i++)
166  {
167  x+= Ax; y+= Ay;
168  setPixel((int)x,(int)y,color);
169  } // end for i
170 
171 }
172 
173 /*---------------------------------------------------------------
174  rectangle
175 ---------------------------------------------------------------*/
177  int x0,
178  int y0,
179  int x1,
180  int y1,
182  unsigned int width)
183 {
184  int w_min = (int) -ceil(((float)width)/2);
185  int w_max = (int) floor(((float)width)/2);
186  // Draw "width" rectangles one into another:
187  for (int w=w_min;w<=w_max;w++)
188  {
189  line( x0-w,y0-w, x1+w, y0-w, color );
190  line( x1+w,y0-w, x1+w, y1+w, color );
191  line( x1+w, y1+w, x0-w, y1+w, color );
192  line( x0-w, y1+w, x0-w,y0-w, color );
193  } // end for "w"
194 }
195 
196 /*****************************************************AJOGD***************************************************/
197 /*---------------------------------------------------------------
198  triangle
199 ---------------------------------------------------------------*/
201  int x0,
202  int y0,
203  int size,
205  bool inferior,
206  unsigned int width)
207 {
208  int ts = round(0.866*size);
209  int tc = round(0.5*size);
210  if (inferior)
211  {
212  line(x0,y0+size,x0+ts,y0-tc,color,width);
213  line(x0,y0+size,x0-ts,y0-tc,color,width);
214  line(x0+ts,y0-tc,x0-ts,y0-tc,color,width);
215  }
216  else
217  {
218  line(x0,y0-size,x0+ts,y0+tc,color,width);
219  line(x0,y0-size,x0-ts,y0+tc,color,width);
220  line(x0+ts,y0+tc,x0-ts,y0+tc,color,width);
221  }
222 }
223 /************************************************************************************************************/
224 
225 
226 /*---------------------------------------------------------------
227  filledRectangle
228 ---------------------------------------------------------------*/
230  int x0,
231  int y0,
232  int x1,
233  int y1,
235 {
236  int x_min = max(x0,0);
237  int x_max = min(x1,(int)getWidth()-1);
238  int y_min = max(y0,0);
239  int y_max = min(y1,(int)getHeight()-1);
240 
241  for (int y=y_min;y<=y_max;y++)
242  for (int x=x_min;x<=x_max;x++)
243  setPixel(x,y,color);
244 }
245 
246 /*---------------------------------------------------------------
247  selectTextFont
248 ---------------------------------------------------------------*/
249 void CCanvas::selectTextFont( const std::string &fontName )
250 {
251  init_fonts_list();
252 
253  // Assure list name is in the list:
255  if (it==list_registered_fonts.end())
256  {
257  // Error
258  cerr << "[CCanvas::selectTextFont] Warning: Unknown font: " << fontName << endl;
259  return;
260  }
261  else
262  {
263  m_selectedFontBitmaps = reinterpret_cast<const uint32_t*>( &it->second[0] );
264  m_selectedFont = fontName;
265  }
266 }
267 
268 /*---------------------------------------------------------------
269  drawImage
270 ---------------------------------------------------------------*/
272  int x,
273  int y,
274  const utils::CImage &img )
275 {
276  MRPT_START
277 
278  int img_lx = img.getWidth();
279  int img_ly = img.getHeight();
280 
281  if (img.isColor())
282  {
283  for (int xx=0;xx<img_lx;xx++)
284  for (int yy=0;yy<img_ly;yy++)
285  setPixel(x+xx,y+yy, *((int*)img(xx,yy)) );
286  }
287  else
288  {
289  unsigned char c;
290  int col;
291  for (int xx=0;xx<img_lx;xx++)
292  for (int yy=0;yy<img_ly;yy++)
293  {
294  c = *((unsigned char *)img(xx,yy));
295  col = c | (c<<8) | (c<<16);
296  setPixel(x+xx,y+yy, col );
297  }
298  }
299 
300 
301  MRPT_END
302 }
303 
304 /*---------------------------------------------------------------
305  drawImage
306 ---------------------------------------------------------------*/
308  int x,
309  int y,
310  const utils::CImage &img,
311  float rotation,
312  float scale )
313 {
317 
318  MRPT_START
319 
320  THROW_EXCEPTION("Not implemented yet!! Try yourself! ;-)");
321 
322  MRPT_END
323 }
324 
325 
326 /*---------------------------------------------------------------
327  cross
328 ---------------------------------------------------------------*/
329 void CCanvas::cross(int x0,int y0, const mrpt::utils::TColor color, char type, unsigned int size, unsigned int width)
330 {
331  switch(type)
332  {
333  case '+':
334  line(x0-size,y0,x0+size,y0,color,width);
335  line(x0,y0-size,x0,y0+size,color,width);
336  break;
337  case 'x':
338  line(x0-size,y0-size,x0+size,y0+size,color,width);
339  line(x0+size,y0-size,x0-size,y0+size,color,width);
340  break;
341  case ':':
342  line(x0-size,y0,x0-2,y0,color,width);
343  line(x0+2,y0,x0+size,y0,color,width);
344  line(x0,y0-size,x0,y0-2,color,width);
345  line(x0,y0+2,x0,y0+size,color,width);
346  break;
347  default:
348  THROW_EXCEPTION("Unexpected 'type' of cross to be drawn")
349  }
350 }
351 
352 /*---------------------------------------------------------------
353  drawCircle
354 ---------------------------------------------------------------*/
356  int x,
357  int y,
358  int radius,
359  const mrpt::utils::TColor &color,
360  unsigned int width
361  )
362 {
363  if (radius<0) radius=-radius;
364 
365  int nSegments;
366 
367  if (radius==0)
368  {
369  nSegments=2;
370  }
371  else
372  {
373  nSegments = int(M_2PI * radius);
374  }
375 
376  int x1=0,y1=0,x2=0,y2=0;
377  double ang, Aa = M_2PI/(nSegments-1);
378  int i;
379 
380  for (i=0,ang=0;i<nSegments;i++,ang+=Aa)
381  {
382  x2 = round( x + radius * cos(ang) );
383  y2 = round( y + radius * sin(ang) );
384 
385  if (i>0)
386  line( x1, y1,x2, y2,color,width );
387 
388  x1 = x2;
389  y1 = y2;
390  } // end for points on ellipse
391 }
392 
393 
394 /*---------------------------------------------------------------
395  textOut
396 ---------------------------------------------------------------*/
398  int x0,
399  int y0,
400  const std::string &str,
402  )
403 {
404  MRPT_START
405 
406  if (!m_selectedFontBitmaps) // First call: load fonts
407  this->selectTextFont("9x15");
408 
409  // Am I an image?
410  bool y_axis_reversed = false;
411  CImage* im_image = dynamic_cast<CImage*>(this);
412  if (im_image)
413  y_axis_reversed = !im_image->isOriginTopLeft();
414 
415  // Decode UNICODE string:
416  vector_word uniStr;
417  mrpt::system::decodeUTF8( str, uniStr );
418 
419 
420  int px= x0;
421  int py= y0;
422 
423  // Char size:
424  uint32_t char_w = m_selectedFontBitmaps[0];
425  uint32_t char_h = m_selectedFontBitmaps[1];
426 
427 
428  for (size_t i=0; i<uniStr.size() ; i++)
429  {
430  const uint16_t &unichar = uniStr[i];
431 
432  // look for the character in the table:
433  const uint32_t *table_ptr = m_selectedFontBitmaps+2;
434  uint32_t charset_ini = table_ptr[0];
435  uint32_t charset_end = table_ptr[1];
436 
437  while (charset_end)
438  {
439  // Is in this range?
440  if ( unichar<=charset_end && unichar>=charset_ini )
441  {
442  // Draw this character:
443  unsigned pyy = y_axis_reversed ? (py+char_h-1) : py;
444  unsigned pxx;
445 
446  const uint32_t *char_bitmap = table_ptr+ 2 + char_h*(unichar-charset_ini);
447 
448  for (unsigned y=0;y<char_h;y++,pyy+= y_axis_reversed ? -1:1 )
449  {
450  pxx = px;
451  const uint32_t &row = *char_bitmap++;
452  for (unsigned x=0;x<char_w;x++,pxx++)
453  if (row & (1 << x))
454  setPixel(pxx,pyy,color);
455  }
456 
457  // Advance the raster cursor:
458  px+=char_w;
459 
460  // Next char!
461  break;
462  }
463  else
464  {
465  // No: Move to the next block and keep searching:
466  uint32_t n_chars = charset_end-charset_ini+1;
467  table_ptr+= 2 /* Header */ + n_chars * char_h;
468 
469  // get new block header:
470  charset_ini = table_ptr[0];
471  charset_end = table_ptr[1];
472  }
473  }
474  // Char not in the font!
475  }
476 
477  MRPT_END
478 }
479 
480 
481 
virtual void textOut(int x0, int y0, const std::string &str, const mrpt::utils::TColor color)
Renders 2D text using bitmap fonts.
Definition: CCanvas.cpp:397
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
#define min(a, b)
unsigned __int16 uint16_t
Definition: rptypes.h:46
std::string m_selectedFont
The selected font name.
Definition: CCanvas.h:43
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
virtual void line(int x0, int y0, int x1, int y1, const mrpt::utils::TColor color, unsigned int width=1, TPenStyle penStyle=psSolid)
Draws a line.
Definition: CCanvas.cpp:125
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:5717
#define THROW_EXCEPTION(msg)
bool list_fonts_init
Definition: CCanvas.cpp:52
map< string, vector_byte > list_registered_fonts
Definition: CCanvas.cpp:51
virtual size_t getHeight() const =0
Returns the height of the image in pixels.
void rectangle(int x0, int y0, int x1, int y1, const mrpt::utils::TColor color, unsigned int width=1)
Draws a rectangle (an empty rectangle, without filling)
Definition: CCanvas.cpp:176
STL namespace.
void triangle(int x0, int y0, int size, const mrpt::utils::TColor color, bool inferior=true, unsigned int width=1)
Draws a triangle.
Definition: CCanvas.cpp:200
const Scalar * const_iterator
Definition: eigen_plugins.h:24
GLenum GLsizei width
Definition: glext.h:3513
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3962
#define M_2PI
Definition: mrpt_macros.h:380
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:52
virtual void setPixel(int x, int y, size_t color)=0
Changes the value of the pixel (x,y).
GLuint color
Definition: glext.h:7093
#define LOAD_FONT(FONTNAME)
const uint32_t * m_selectedFontBitmaps
Direct access to character bitmaps.
Definition: CCanvas.h:45
void BASE_IMPEXP decodeUTF8(const std::string &strUTF8, vector_word &out_uniStr)
Decodes a UTF-8 string into an UNICODE string.
const GLfloat * tc
Definition: glext.h:5610
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
const GLubyte * c
Definition: glext.h:5590
GLint GLvoid * img
Definition: glext.h:3645
void cross(int x0, int y0, const mrpt::utils::TColor color, char type, unsigned int size=5, unsigned int width=1)
Draw a cross.
Definition: CCanvas.cpp:329
virtual void selectTextFont(const std::string &fontName)
Select the current font used when drawing text.
Definition: CCanvas.cpp:249
A RGB color - 8bit.
Definition: TColor.h:26
GLsizei const GLchar ** string
Definition: glext.h:3919
virtual void filledRectangle(int x0, int y0, int x1, int y1, const mrpt::utils::TColor color)
Draws a filled rectangle.
Definition: CCanvas.cpp:229
virtual size_t getWidth() const =0
Returns the width of the image in pixels.
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void init_fonts_list()
Definition: CCanvas.cpp:54
virtual void drawCircle(int x, int y, int radius, const mrpt::utils::TColor &color=mrpt::utils::TColor(255, 255, 255), unsigned int width=1)
Draws a circle of a given radius.
Definition: CCanvas.cpp:355
std::vector< uint16_t > vector_word
Definition: types_simple.h:27
GLenum GLenum GLvoid * row
Definition: glext.h:3533
bool isOriginTopLeft() const
Returns true if the coordinates origin is top-left, or false if it is bottom-left.
Definition: CImage.cpp:927
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:26
GLenum GLint GLint y
Definition: glext.h:3516
GLsizeiptr size
Definition: glext.h:3779
GLenum GLint x
Definition: glext.h:3516
TPenStyle
Definition of pen styles.
Definition: CCanvas.h:53
unsigned __int32 uint32_t
Definition: rptypes.h:49
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3512
virtual void drawImage(int x, int y, const utils::CImage &img)
Draws an image as a bitmap at a given position.
Definition: CCanvas.cpp:271



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019