Main MRPT website > C++ reference for MRPT 1.9.9
CEnhancedMetaFile.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 
13 #include <mrpt/system/os.h>
14 #include <mrpt/utils/CImage.h>
15 
16 static int LINUX_IMG_WIDTH_value = 800;
17 static int LINUX_IMG_HEIGHT_value = 600;
18 
19 using namespace mrpt;
20 using namespace mrpt::utils;
21 using namespace mrpt::system;
22 
27 
28 #include <mrpt/config.h>
29 #ifdef MRPT_OS_WINDOWS
30 #include <windows.h>
31 #endif
32 
33 
34 /*---------------------------------------------------------------
35  Constructor
36 ---------------------------------------------------------------*/
38  const std::string& targetFileName, int scaleFactor)
39  : m_scale(scaleFactor), m_targetFile(targetFileName)
40 {
41 #ifdef MRPT_OS_WINDOWS
42  m_hdc =
43  CreateEnhMetaFileA(nullptr, targetFileName.c_str(), nullptr, nullptr);
44  if (!m_hdc.get()) THROW_EXCEPTION("Can't create EMF file!!!");
45 #else
47  ((CImage*)m_hdc.get())
49  LINUX_IMG_HEIGHT_value - 1, TColor(0, 0, 0));
50 #endif
51 }
52 
53 /*---------------------------------------------------------------
54  Destructor
55 ---------------------------------------------------------------*/
57 {
58 #ifdef MRPT_OS_WINDOWS
59  // Free objects:
60  if (m_hFont.get())
61  {
62  DeleteObject(m_hFont.get());
63  m_hFont = nullptr;
64  }
65 
66  // Finish EMF:
67  DeleteEnhMetaFile(CloseEnhMetaFile((HDC)m_hdc.get()));
68 #else
69 ((CImage*)m_hdc.get())->saveToFile(m_targetFile + ".png");
70 
71 // Free objects:
72 delete ((CImage*)m_hdc.get());
73 #endif
74 }
75 
76 /*---------------------------------------------------------------
77  drawImage
78 ---------------------------------------------------------------*/
80 {
81 #ifdef MRPT_OS_WINDOWS
82  try
83  {
84  LPBITMAPINFO pBmpInfo =
85  (LPBITMAPINFO) new unsigned char[sizeof(BITMAPINFOHEADER) +
86  (256 * sizeof(RGBQUAD))];
87  // LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new unsigned
88  // char[sizeof(BITMAPINFOHEADER) ];
89 
90  unsigned int imgWidth = (unsigned int)img.getWidth();
91  unsigned int imgHeight = (unsigned int)img.getHeight();
92 
93  pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
94  pBmpInfo->bmiHeader.biWidth = imgWidth;
95  pBmpInfo->bmiHeader.biHeight = imgHeight;
96  pBmpInfo->bmiHeader.biPlanes = 1;
97  // pBmpInfo->bmiHeader.biBitCount = 24;
98  pBmpInfo->bmiHeader.biBitCount = 8;
99  pBmpInfo->bmiHeader.biCompression = BI_RGB;
100  pBmpInfo->bmiHeader.biSizeImage = 0;
101  pBmpInfo->bmiHeader.biXPelsPerMeter =
102  pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
103  pBmpInfo->bmiHeader.biClrUsed = 0;
104  pBmpInfo->bmiHeader.biClrImportant = 0;
105 
106  // Palette
107  for (unsigned char i = 0; i < 255; i++)
108  {
109  pBmpInfo->bmiColors[i].rgbRed = i;
110  pBmpInfo->bmiColors[i].rgbGreen = i;
111  pBmpInfo->bmiColors[i].rgbBlue = i;
112  pBmpInfo->bmiColors[i].rgbReserved = 0;
113  }
114 
115  // unsigned int lineBytes = 3*bmp.Width;
116  unsigned int lineBytes = imgWidth;
117  if (lineBytes % 2) lineBytes++;
118  if (lineBytes % 4) lineBytes += 2;
119 
120  BYTE* ptrBits = new BYTE[lineBytes * imgHeight];
121 
122  for (unsigned int py = 0; py < imgHeight; py++)
123  for (unsigned int px = 0; px < imgWidth; px++)
124  ptrBits[(py * lineBytes + px) + 0] = *img(px, py);
125 
126  HBITMAP hBitmap = CreateDIBitmap(
127  (HDC)m_hdc.get(), &pBmpInfo->bmiHeader, CBM_INIT, ptrBits, pBmpInfo,
128  DIB_RGB_COLORS);
129 
130  ASSERT_(hBitmap != nullptr);
131 
132  BITMAP bm;
133  GetObject(hBitmap, sizeof(bm), &bm);
134 
135  HDC hdcMem = CreateCompatibleDC((HDC)m_hdc.get());
136  HBITMAP hbmT = (HBITMAP)SelectObject(hdcMem, hBitmap);
137 
138  BitBlt(
139  (HDC)m_hdc.get(), x, y, (int)(m_scale * imgWidth),
140  (int)(m_scale * imgHeight), hdcMem, 0, 0, SRCCOPY);
141 
142  SelectObject(hdcMem, hbmT);
143  DeleteDC(hdcMem);
144 
145  // Free mem:
146  // ---------------------------------------
147  DeleteObject(hBitmap);
148  delete[] pBmpInfo;
149  delete[] ptrBits;
150  }
151  catch (...)
152  {
153  THROW_EXCEPTION("Unexpected runtime error!!");
154  }
155 #else
156  ((CImage*)m_hdc.get())->drawImage(x, y, img);
157 #endif
158 }
159 
160 /*---------------------------------------------------------------
161  drawBitmap
162 ---------------------------------------------------------------*/
164  int x0, int y0, int x1, int y1, const mrpt::utils::TColor color,
165  unsigned int width, TPenStyle penStyle)
166 {
167 #ifdef MRPT_OS_WINDOWS
168  x0 *= m_scale;
169  y0 *= m_scale;
170  x1 *= m_scale;
171  y1 *= m_scale;
172 
173  HPEN hPen = CreatePen(penStyle, width, (unsigned int)color);
174 
175  HPEN hOldPen = (HPEN)SelectObject((HDC)m_hdc.get(), hPen);
176 
177  MoveToEx((HDC)m_hdc.get(), x0, y0, nullptr);
178  LineTo((HDC)m_hdc.get(), x1, y1);
179 
180  SelectObject((HDC)m_hdc.get(), hOldPen);
181  DeleteObject(hPen);
182 #else
183  ((CImage*)m_hdc.get())->line(x0, y0, x1, y1, color, width, penStyle);
184 #endif
185 }
186 
187 /*---------------------------------------------------------------
188  drawBitmap
189 ---------------------------------------------------------------*/
191  int x0, int y0, const std::string& str, const mrpt::utils::TColor color)
192 {
193 #ifdef MRPT_OS_WINDOWS
194  x0 *= m_scale;
195  y0 *= m_scale;
196 
197  ::SetBkMode((HDC)m_hdc.get(), TRANSPARENT);
198  ::SetTextColor((HDC)m_hdc.get(), (unsigned int)color);
199 
200  ::TextOutA((HDC)m_hdc.get(), x0, y0, str.c_str(), (int)str.size());
201 #else
202  ((CImage*)m_hdc.get())->textOut(x0, y0, str, color);
203 #endif
204 }
205 
206 /*---------------------------------------------------------------
207  selectVectorTextFont
208 ---------------------------------------------------------------*/
210  const std::string& fontName, int fontSize, bool bold, bool italic)
211 {
212 #ifdef MRPT_OS_WINDOWS
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) ::DeleteObject(oldFont);
236 #else
237  MRPT_UNUSED_PARAM(fontSize);
238  MRPT_UNUSED_PARAM(bold);
239  MRPT_UNUSED_PARAM(italic);
241 
242  ((CImage*)m_hdc.get())->selectTextFont(fontName);
243 
244  MRPT_TRY_END;
245 #endif
246 }
247 
248 /*---------------------------------------------------------------
249  setPixel
250 ---------------------------------------------------------------*/
251 void CEnhancedMetaFile::setPixel(int x, int y, size_t color)
252 {
253 #ifdef MRPT_OS_WINDOWS
254  ::SetPixel((HDC)m_hdc.get(), x * m_scale, y * m_scale, color);
255 #else
256  ((CImage*)m_hdc.get())->setPixel(x, y, color);
257 #endif
258 }
259 
260 /*---------------------------------------------------------------
261  rectangle
262 ---------------------------------------------------------------*/
264  int x0, int y0, int x1, int y1, TColor color, unsigned int width)
265 {
266  line(x0, y0, x1, y0, color, width);
267  line(x1, y0, x1, y1, color, width);
268  line(x1, y1, x0, y1, color, width);
269  line(x0, y1, x0, y0, color, width);
270 }
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_WIDTH_value
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:118
void textOut(int x0, int y0, const std::string &str, const mrpt::utils::TColor color) override
Places a text label.
#define THROW_EXCEPTION(msg)
GLenum GLsizei width
Definition: glext.h:3531
GLuint color
Definition: glext.h:8300
#define MRPT_TRY_END
virtual ~CEnhancedMetaFile()
Destructor.
virtual void selectVectorTextFont(const std::string &fontName, int fontSize, bool bold=false, bool italic=false)
Select the current font used when drawing text.
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
GLint GLvoid * img
Definition: glext.h:3763
virtual void selectTextFont(const std::string &fontName)
Select the current font used when drawing text.
Definition: CCanvas.cpp:231
A RGB color - 8bit.
Definition: TColor.h:25
GLsizei const GLchar ** string
Definition: glext.h:4101
CEnhancedMetaFile(const std::string &targetFileName, int scaleFactor=1)
Constructor.
virtual void filledRectangle(int x0, int y0, int x1, int y1, const mrpt::utils::TColor color)
Draws a filled rectangle.
Definition: CCanvas.cpp:216
void line(int x0, int y0, int x1, int y1, const mrpt::utils::TColor color, unsigned int width=1, TPenStyle penStyle=psSolid) override
Draws a line.
void drawImage(int x, int y, const utils::CImage &img) override
Draws an image as a bitmap at a given position.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MRPT_TRY_START
#define ASSERT_(f)
GLenum GLint GLint y
Definition: glext.h:3538
GLsizei const GLfloat * value
Definition: glext.h:4117
GLenum GLint x
Definition: glext.h:3538
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
Definition: os.cpp:296
static int LINUX_IMG_HEIGHT_value
TPenStyle
Definition of pen styles.
Definition: CCanvas.h:57



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019