MRPT  1.9.9
CEnhancedMetaFile.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "img-precomp.h" // Precompiled headers
11 
13 #include <mrpt/img/CImage.h>
14 #include <mrpt/system/os.h>
15 
16 #include <mrpt/config.h>
17 #ifdef _WIN32
18 #include <windows.h>
19 #endif
20 
21 using namespace mrpt;
22 using namespace mrpt::img;
23 using namespace mrpt::system;
24 
25 static int LINUX_IMG_WIDTH_value = 800;
26 static int LINUX_IMG_HEIGHT_value = 600;
27 
29 {
31 }
34 {
36 }
38 /*---------------------------------------------------------------
39  Constructor
40 ---------------------------------------------------------------*/
42  const std::string& targetFileName, int scaleFactor)
43  : m_scale(scaleFactor), m_targetFile(targetFileName)
44 {
45 #ifdef _WIN32
46  m_hdc =
47  CreateEnhMetaFileA(nullptr, targetFileName.c_str(), nullptr, nullptr);
48  if (!m_hdc.get()) THROW_EXCEPTION("Can't create EMF file!!!");
49 #else
51  ((CImage*)m_hdc.get())
54  TColor(0, 0, 0));
55 #endif
56 }
57 
58 /*---------------------------------------------------------------
59  Destructor
60 ---------------------------------------------------------------*/
62 {
63 #ifdef _WIN32
64  // Free objects:
65  if (m_hFont.get())
66  {
67  DeleteObject(m_hFont.get());
68  m_hFont = nullptr;
69  }
70 
71  // Finish EMF:
72  DeleteEnhMetaFile(CloseEnhMetaFile((HDC)m_hdc.get()));
73 #else
74  ((CImage*)m_hdc.get())->saveToFile(m_targetFile + ".png");
75 
76  // Free objects:
77  delete ((CImage*)m_hdc.get());
78 #endif
79 }
80 
81 /*---------------------------------------------------------------
82  drawImage
83 ---------------------------------------------------------------*/
85 {
86 #ifdef _WIN32
87  try
88  {
89  LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new unsigned char
90  [sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD))];
91  // LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new unsigned
92  // char[sizeof(BITMAPINFOHEADER) ];
93 
94  unsigned int imgWidth = (unsigned int)img.getWidth();
95  unsigned int imgHeight = (unsigned int)img.getHeight();
96 
97  pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
98  pBmpInfo->bmiHeader.biWidth = imgWidth;
99  pBmpInfo->bmiHeader.biHeight = imgHeight;
100  pBmpInfo->bmiHeader.biPlanes = 1;
101  // pBmpInfo->bmiHeader.biBitCount = 24;
102  pBmpInfo->bmiHeader.biBitCount = 8;
103  pBmpInfo->bmiHeader.biCompression = BI_RGB;
104  pBmpInfo->bmiHeader.biSizeImage = 0;
105  pBmpInfo->bmiHeader.biXPelsPerMeter =
106  pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
107  pBmpInfo->bmiHeader.biClrUsed = 0;
108  pBmpInfo->bmiHeader.biClrImportant = 0;
109 
110  // Palette
111  for (unsigned char i = 0; i < 255; i++)
112  {
113  pBmpInfo->bmiColors[i].rgbRed = i;
114  pBmpInfo->bmiColors[i].rgbGreen = i;
115  pBmpInfo->bmiColors[i].rgbBlue = i;
116  pBmpInfo->bmiColors[i].rgbReserved = 0;
117  }
118 
119  // unsigned int lineBytes = 3*bmp.Width;
120  unsigned int lineBytes = imgWidth;
121  if (lineBytes % 2) lineBytes++;
122  if (lineBytes % 4) lineBytes += 2;
123 
124  BYTE* ptrBits = new BYTE[lineBytes * imgHeight];
125 
126  for (unsigned int py = 0; py < imgHeight; py++)
127  for (unsigned int px = 0; px < imgWidth; px++)
128  ptrBits[(py * lineBytes + px) + 0] = *img(px, py);
129 
130  HBITMAP hBitmap = CreateDIBitmap(
131  (HDC)m_hdc.get(), &pBmpInfo->bmiHeader, CBM_INIT, ptrBits, pBmpInfo,
132  DIB_RGB_COLORS);
133 
134  ASSERT_(hBitmap != nullptr);
135 
136  BITMAP bm;
137  GetObject(hBitmap, sizeof(bm), &bm);
138 
139  HDC hdcMem = CreateCompatibleDC((HDC)m_hdc.get());
140  HBITMAP hbmT = (HBITMAP)SelectObject(hdcMem, hBitmap);
141 
142  BitBlt(
143  (HDC)m_hdc.get(), x, y, (int)(m_scale * imgWidth),
144  (int)(m_scale * imgHeight), hdcMem, 0, 0, SRCCOPY);
145 
146  SelectObject(hdcMem, hbmT);
147  DeleteDC(hdcMem);
148 
149  // Free mem:
150  // ---------------------------------------
151  DeleteObject(hBitmap);
152  delete[] pBmpInfo;
153  delete[] ptrBits;
154  }
155  catch (...)
156  {
157  THROW_EXCEPTION("Unexpected runtime error!!");
158  }
159 #else
160  ((CImage*)m_hdc.get())->drawImage(x, y, img);
161 #endif
162 }
163 
164 /*---------------------------------------------------------------
165  drawBitmap
166 ---------------------------------------------------------------*/
168  int x0, int y0, int x1, int y1, const mrpt::img::TColor color,
169  unsigned int width, TPenStyle penStyle)
170 {
171 #ifdef _WIN32
172  x0 *= m_scale;
173  y0 *= m_scale;
174  x1 *= m_scale;
175  y1 *= m_scale;
176 
177  HPEN hPen = CreatePen(penStyle, width, (unsigned int)color);
178 
179  HPEN hOldPen = (HPEN)SelectObject((HDC)m_hdc.get(), hPen);
180 
181  MoveToEx((HDC)m_hdc.get(), x0, y0, nullptr);
182  LineTo((HDC)m_hdc.get(), x1, y1);
183 
184  SelectObject((HDC)m_hdc.get(), hOldPen);
185  DeleteObject(hPen);
186 #else
187  ((CImage*)m_hdc.get())->line(x0, y0, x1, y1, color, width, penStyle);
188 #endif
189 }
190 
191 /*---------------------------------------------------------------
192  drawBitmap
193 ---------------------------------------------------------------*/
195  int x0, int y0, const std::string& str, const mrpt::img::TColor color)
196 {
197 #ifdef _WIN32
198  x0 *= m_scale;
199  y0 *= m_scale;
200 
201  ::SetBkMode((HDC)m_hdc.get(), TRANSPARENT);
202  ::SetTextColor((HDC)m_hdc.get(), (unsigned int)color);
203 
204  ::TextOutA((HDC)m_hdc.get(), x0, y0, str.c_str(), (int)str.size());
205 #else
206  ((CImage*)m_hdc.get())->textOut(x0, y0, str, color);
207 #endif
208 }
209 
210 /*---------------------------------------------------------------
211  selectVectorTextFont
212 ---------------------------------------------------------------*/
214  const std::string& fontName, int fontSize, bool bold, bool italic)
215 {
216 #ifdef _WIN32
217  HFONT hFont, oldFont;
218  LOGFONTA lpf;
219 
220  lpf.lfHeight = fontSize;
221  lpf.lfWidth = 0;
222  lpf.lfEscapement = 0;
223  lpf.lfOrientation = 0;
224  lpf.lfWeight = bold ? 700 : 400;
225  lpf.lfItalic = italic ? 1 : 0;
226  lpf.lfUnderline = 0;
227  lpf.lfStrikeOut = 0;
228  lpf.lfCharSet = DEFAULT_CHARSET;
229  lpf.lfOutPrecision = OUT_DEFAULT_PRECIS;
230  lpf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
231  lpf.lfQuality = DEFAULT_QUALITY;
232  lpf.lfPitchAndFamily = DEFAULT_PITCH;
233  os::strcpy(lpf.lfFaceName, LF_FACESIZE, fontName.c_str());
234 
235  hFont = ::CreateFontIndirectA(&lpf);
236 
237  oldFont = (HFONT)::SelectObject((HDC)m_hdc.get(), hFont);
238 
239  if (oldFont) ::DeleteObject(oldFont);
240 #else
241  MRPT_UNUSED_PARAM(fontSize);
242  MRPT_UNUSED_PARAM(bold);
243  MRPT_UNUSED_PARAM(italic);
245 
246  ((CImage*)m_hdc.get())->selectTextFont(fontName);
247 
248  MRPT_TRY_END;
249 #endif
250 }
251 
252 /*---------------------------------------------------------------
253  setPixel
254 ---------------------------------------------------------------*/
255 void CEnhancedMetaFile::setPixel(int x, int y, size_t color)
256 {
257 #ifdef _WIN32
258  ::SetPixel((HDC)m_hdc.get(), x * m_scale, y * m_scale, color);
259 #else
260  ((CImage*)m_hdc.get())->setPixel(x, y, color);
261 #endif
262 }
263 
264 /*---------------------------------------------------------------
265  rectangle
266 ---------------------------------------------------------------*/
268  int x0, int y0, int x1, int y1, TColor color, unsigned int width)
269 {
270  line(x0, y0, x1, y0, color, width);
271  line(x1, y0, x1, y1, color, width);
272  line(x1, y1, x0, y1, color, width);
273  line(x0, y1, x0, y0, color, width);
274 }
void textOut(int x0, int y0, const std::string &str, const mrpt::img::TColor color) override
Places a text label.
static int LINUX_IMG_WIDTH_value
#define MRPT_TRY_END
The end of a standard MRPT "try...catch()" block that allows tracing throw the call stack after an ex...
Definition: exceptions.h:213
TPenStyle
Definition of pen styles.
Definition: CCanvas.h:55
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
void drawImage(int x, int y, const mrpt::img::CImage &img) override
Draws an image as a bitmap at a given position.
GLenum GLsizei width
Definition: glext.h:3535
GLuint color
Definition: glext.h:8459
#define MRPT_TRY_START
The start of a standard MRPT "try...catch()" block that allows tracing throw the call stack after an ...
Definition: exceptions.h:206
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
virtual void selectVectorTextFont(const std::string &fontName, int fontSize, bool bold=false, bool italic=false)
Select the current font used when drawing text.
mrpt::img::CImage CImage
Definition: utils/CImage.h:5
CEnhancedMetaFile(const std::string &targetFileName, int scaleFactor=1)
Constructor.
virtual void selectTextFont(const std::string &fontName)
Select the current font used when drawing text.
Definition: CCanvas.cpp:232
virtual void filledRectangle(int x0, int y0, int x1, int y1, const mrpt::img::TColor color)
Draws a filled rectangle.
Definition: CCanvas.cpp:217
GLint GLvoid * img
Definition: glext.h:3769
void line(int x0, int y0, int x1, int y1, const mrpt::img::TColor color, unsigned int width=1, TPenStyle penStyle=psSolid) override
Draws a line.
GLsizei const GLchar ** string
Definition: glext.h:4116
~CEnhancedMetaFile() override
Destructor.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
virtual void rectangle(int x0, int y0, int x1, int y1, const mrpt::img::TColor color, unsigned int width=1)
Draws a rectangle (an empty rectangle, without filling)
GLenum GLint GLint y
Definition: glext.h:3542
GLsizei const GLfloat * value
Definition: glext.h:4134
A RGB color - 8bit.
Definition: TColor.h:20
GLenum GLint x
Definition: glext.h:3542
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
Definition: os.cpp:299
static int LINUX_IMG_HEIGHT_value
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:147
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 0cbd40372 Sun Nov 17 09:43:05 2019 +0100 at dom nov 17 09:45:09 CET 2019