Main MRPT website > C++ reference for MRPT 1.5.7
CEnhancedMetaFile_WIN.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 
14 #include <MRPT/config.h>
15 
16 #ifdef MRPT_OS_WINDOWS
17 
19 #include <mrpt/system/os.h>
20 #include <mrpt/utils/CImage.h>
21 
22 #include <windows.h>
23 
24 
25 using namespace mrpt;
26 using namespace mrpt::utils;
27 using namespace mrpt::system;
28 
29 
32 
33 /*---------------------------------------------------------------
34  Constructor
35 ---------------------------------------------------------------*/
37  const std::string &targetFileName,
38  int scaleFactor ) :
39  m_scale(scaleFactor),
40  m_hFont(NULL)
41 {
42  m_hdc = CreateEnhMetaFileA( NULL, targetFileName.c_str(), NULL, NULL );
43  if (!m_hdc.get())
44  THROW_EXCEPTION("Can't create EMF file!!!");
45 }
46 
47 /*---------------------------------------------------------------
48  Destructor
49 ---------------------------------------------------------------*/
51 {
52  // Free objects:
53  if (m_hFont.get())
54  {
55  DeleteObject(m_hFont.get());
56  m_hFont = NULL;
57  }
58 
59  // Finish EMF:
60  DeleteEnhMetaFile( CloseEnhMetaFile( (HDC)m_hdc.get() ) );
61 }
62 
63 /*---------------------------------------------------------------
64  drawImage
65 ---------------------------------------------------------------*/
67  int x,
68  int y,
69  const utils::CImage &img
70  )
71 {
72  try
73  {
74  LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new unsigned char[sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD))];
75 // LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new unsigned char[sizeof(BITMAPINFOHEADER) ];
76 
77  unsigned int imgWidth = (unsigned int)img.getWidth();
78  unsigned int imgHeight = (unsigned int)img.getHeight();
79 
80  pBmpInfo->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
81  pBmpInfo->bmiHeader.biWidth = imgWidth;
82  pBmpInfo->bmiHeader.biHeight = imgHeight;
83  pBmpInfo->bmiHeader.biPlanes = 1;
84 // pBmpInfo->bmiHeader.biBitCount = 24;
85  pBmpInfo->bmiHeader.biBitCount = 8;
86  pBmpInfo->bmiHeader.biCompression = BI_RGB;
87  pBmpInfo->bmiHeader.biSizeImage = 0;
88  pBmpInfo->bmiHeader.biXPelsPerMeter =
89  pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
90  pBmpInfo->bmiHeader.biClrUsed = 0;
91  pBmpInfo->bmiHeader.biClrImportant = 0;
92 
93  // Palette
94  for (unsigned char i=0; i<255; i++)
95  {
96  pBmpInfo->bmiColors[i].rgbRed = i;
97  pBmpInfo->bmiColors[i].rgbGreen = i;
98  pBmpInfo->bmiColors[i].rgbBlue = i;
99  pBmpInfo->bmiColors[i].rgbReserved = 0;
100  }
101 
102 
103 // unsigned int lineBytes = 3*bmp.Width;
104  unsigned int lineBytes = imgWidth;
105  if (lineBytes % 2) lineBytes++;
106  if (lineBytes % 4) lineBytes+=2;
107 
108  BYTE *ptrBits = new BYTE[lineBytes * imgHeight];
109 
110  for (unsigned int py=0;py<imgHeight;py++)
111  for (unsigned int px=0;px<imgWidth;px++)
112  ptrBits[(py*lineBytes+px)+0] = *img(px,py);
113 
114  HBITMAP hBitmap = CreateDIBitmap(
115  (HDC)m_hdc.get(),
116  &pBmpInfo->bmiHeader,
117  CBM_INIT,
118  ptrBits,
119  pBmpInfo,
120  DIB_RGB_COLORS);
121 
122  ASSERT_(hBitmap!=NULL);
123 
124  BITMAP bm;
125  GetObject(hBitmap,sizeof(bm),&bm);
126 
127  HDC hdcMem = CreateCompatibleDC( (HDC)m_hdc.get() );
128  HBITMAP hbmT = (HBITMAP)SelectObject(hdcMem,hBitmap);
129 
130  BitBlt(
131  (HDC)m_hdc.get(),
132  x,
133  y,
134  (int)(m_scale*imgWidth),
135  (int)(m_scale*imgHeight),
136  hdcMem,
137  0,
138  0,
139  SRCCOPY);
140 
141  SelectObject(hdcMem,hbmT);
142  DeleteDC(hdcMem);
143 
144  // Free mem:
145  // ---------------------------------------
146  DeleteObject( hBitmap );
147  delete[] pBmpInfo;
148  delete[] ptrBits;
149  }
150  catch(...)
151  {
152  THROW_EXCEPTION("Unexpected runtime error!!");
153  }
154 }
155 
156 /*---------------------------------------------------------------
157  drawBitmap
158 ---------------------------------------------------------------*/
160  int x0,
161  int y0,
162  int x1,
163  int y1,
165  unsigned int width,
166  TPenStyle penStyle
167  )
168 {
169  x0*= m_scale; y0*= m_scale;
170  x1*= m_scale; y1*= m_scale;
171 
172  HPEN hPen = CreatePen(
173  penStyle,
174  width,
175  (unsigned int)color );
176 
177  HPEN hOldPen = (HPEN) SelectObject( (HDC)m_hdc.get(), hPen );
178 
179  MoveToEx( (HDC)m_hdc.get(), x0,y0, NULL );
180  LineTo( (HDC)m_hdc.get(), x1,y1);
181 
182  SelectObject( (HDC)m_hdc.get(), hOldPen );
183  DeleteObject( hPen );
184 }
185 
186 /*---------------------------------------------------------------
187  drawBitmap
188 ---------------------------------------------------------------*/
190  int x0,
191  int y0,
192  const std::string &str,
194  )
195 {
196  x0*=m_scale; y0*=m_scale;
197 
198  ::SetBkMode((HDC)m_hdc.get(), TRANSPARENT);
199  ::SetTextColor( (HDC)m_hdc.get(),(unsigned int)color);
200 
201  ::TextOutA( (HDC)m_hdc.get(), x0,y0, str.c_str(), (int)str.size());
202 }
203 
204 /*---------------------------------------------------------------
205  selectVectorTextFont
206 ---------------------------------------------------------------*/
208  const std::string &fontName,
209  int fontSize,
210  bool bold,
211  bool italic )
212 {
213  HFONT hFont,oldFont;
214  LOGFONTA lpf;
215 
216  lpf.lfHeight = fontSize;
217  lpf.lfWidth = 0;
218  lpf.lfEscapement = 0;
219  lpf.lfOrientation = 0;
220  lpf.lfWeight = bold ? 700:400;
221  lpf.lfItalic = italic ? 1:0;
222  lpf.lfUnderline = 0;
223  lpf.lfStrikeOut = 0;
224  lpf.lfCharSet = DEFAULT_CHARSET;
225  lpf.lfOutPrecision = OUT_DEFAULT_PRECIS;
226  lpf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
227  lpf.lfQuality = DEFAULT_QUALITY;
228  lpf.lfPitchAndFamily = DEFAULT_PITCH;
229  os::strcpy(lpf.lfFaceName,LF_FACESIZE,fontName.c_str());
230 
231  hFont = ::CreateFontIndirectA( &lpf );
232 
233  oldFont = (HFONT)::SelectObject( (HDC)m_hdc.get(),hFont );
234 
235  if (oldFont)
236  ::DeleteObject(oldFont);
237 }
238 
239 
240 /*---------------------------------------------------------------
241  setPixel
242 ---------------------------------------------------------------*/
243 void CEnhancedMetaFile::setPixel( int x, int y, size_t color)
244 {
245  ::SetPixel((HDC)m_hdc.get(),x*m_scale,y*m_scale,color);
246 }
247 
248 /*---------------------------------------------------------------
249  rectangle
250 ---------------------------------------------------------------*/
252  int x0,
253  int y0,
254  int x1,
255  int y1,
256  TColor color,
257  unsigned int width)
258 {
259  line(x0,y0,x1,y0,color,width);
260  line(x1,y0,x1,y1,color,width);
261  line(x1,y1,x0,y1,color,width);
262  line(x0,y1,x0,y0,color,width);
263 }
264 
265 #endif // MRPT_OS_WINDOWS
266 
CEnhancedMetaFile(const std::string &targetFileName, int scaleFactor=1)
Constructor.
void setPixel(int x, int y, size_t color) MRPT_OVERRIDE
Changes the value of the pixel (x,y).
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
virtual ~CEnhancedMetaFile()
Destructor.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
virtual void selectVectorTextFont(const std::string &fontName, int fontSize, bool bold=false, bool italic=false)
Select the current font used when drawing text.
#define THROW_EXCEPTION(msg)
char BASE_IMPEXP * strcpy(char *dest, size_t destSize, const char *source) MRPT_NO_THROWS
An OS-independent version of strcpy.
Definition: os.cpp:296
GLenum GLsizei width
Definition: glext.h:3513
GLuint color
Definition: glext.h:7093
GLint GLvoid * img
Definition: glext.h:3645
void line(int x0, int y0, int x1, int y1, const mrpt::utils::TColor color, unsigned int width=1, TPenStyle penStyle=psSolid) MRPT_OVERRIDE
Draws a line.
A RGB color - 8bit.
Definition: TColor.h:26
void textOut(int x0, int y0, const std::string &str, const mrpt::utils::TColor color) MRPT_OVERRIDE
Places a text label.
GLsizei const GLchar ** string
Definition: glext.h:3919
virtual 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)
static int LINUX_IMG_HEIGHT
In Linux, the size of the bitmap image that emulates the EMF (Default:600)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_(f)
GLenum GLint GLint y
Definition: glext.h:3516
GLenum GLint x
Definition: glext.h:3516
static int LINUX_IMG_WIDTH
In Linux, the size of the bitmap image that emulates the EMF (Default:800)
void drawImage(int x, int y, const utils::CImage &img) MRPT_OVERRIDE
Draws an image as a bitmap at a given position.



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