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-2020, 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 {
30  LINUX_IMG_WIDTH_value = value;
31 }
34 {
35  LINUX_IMG_HEIGHT_value = value;
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 ---------------------------------------------------------------*/
84 void CEnhancedMetaFile::drawImage(int x, int y, const mrpt::img::CImage& img)
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  [[maybe_unused]] const std::string& fontName, [[maybe_unused]] int fontSize,
215  [[maybe_unused]] bool bold, [[maybe_unused]] bool italic)
216 {
217 #ifdef _WIN32
218  HFONT hFont, oldFont;
219  LOGFONTA lpf;
220 
221  lpf.lfHeight = fontSize;
222  lpf.lfWidth = 0;
223  lpf.lfEscapement = 0;
224  lpf.lfOrientation = 0;
225  lpf.lfWeight = bold ? 700 : 400;
226  lpf.lfItalic = italic ? 1 : 0;
227  lpf.lfUnderline = 0;
228  lpf.lfStrikeOut = 0;
229  lpf.lfCharSet = DEFAULT_CHARSET;
230  lpf.lfOutPrecision = OUT_DEFAULT_PRECIS;
231  lpf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
232  lpf.lfQuality = DEFAULT_QUALITY;
233  lpf.lfPitchAndFamily = DEFAULT_PITCH;
234  os::strcpy(lpf.lfFaceName, LF_FACESIZE, fontName.c_str());
235 
236  hFont = ::CreateFontIndirectA(&lpf);
237 
238  oldFont = (HFONT)::SelectObject((HDC)m_hdc.get(), hFont);
239 
240  if (oldFont) ::DeleteObject(oldFont);
241 #else
243 
244  ((CImage*)m_hdc.get())->selectTextFont(fontName);
245 
246  MRPT_TRY_END;
247 #endif
248 }
249 
250 /*---------------------------------------------------------------
251  setPixel
252 ---------------------------------------------------------------*/
253 void CEnhancedMetaFile::setPixel(int x, int y, size_t color)
254 {
255 #ifdef _WIN32
256  ::SetPixel((HDC)m_hdc.get(), x * m_scale, y * m_scale, color);
257 #else
258  ((CImage*)m_hdc.get())->setPixel(x, y, color);
259 #endif
260 }
261 
262 /*---------------------------------------------------------------
263  rectangle
264 ---------------------------------------------------------------*/
266  int x0, int y0, int x1, int y1, TColor color, unsigned int width)
267 {
268  line(x0, y0, x1, y0, color, width);
269  line(x1, y0, x1, y1, color, width);
270  line(x1, y1, x0, y1, color, width);
271  line(x0, y1, x0, y0, color, width);
272 }
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.
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:849
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
#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
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:818
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:220
virtual void filledRectangle(int x0, int y0, int x1, int y1, const mrpt::img::TColor color)
Draws a filled rectangle.
Definition: CCanvas.cpp:205
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.
~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)
A RGB color - 8bit.
Definition: TColor.h:25
static int LINUX_IMG_HEIGHT_value
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020