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-2018, 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 "img-precomp.h" // Precompiled headers
11 
13 #include <mrpt/system/os.h>
14 #include <mrpt/img/CImage.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 
40 /*---------------------------------------------------------------
41  Constructor
42 ---------------------------------------------------------------*/
44  const std::string& targetFileName, int scaleFactor)
45  : m_scale(scaleFactor), m_targetFile(targetFileName)
46 {
47 #ifdef _WIN32
48  m_hdc =
49  CreateEnhMetaFileA(nullptr, targetFileName.c_str(), nullptr, nullptr);
50  if (!m_hdc.get()) THROW_EXCEPTION("Can't create EMF file!!!");
51 #else
53  ((CImage*)m_hdc.get())
56  TColor(0, 0, 0));
57 #endif
58 }
59 
60 /*---------------------------------------------------------------
61  Destructor
62 ---------------------------------------------------------------*/
64 {
65 #ifdef _WIN32
66  // Free objects:
67  if (m_hFont.get())
68  {
69  DeleteObject(m_hFont.get());
70  m_hFont = nullptr;
71  }
72 
73  // Finish EMF:
74  DeleteEnhMetaFile(CloseEnhMetaFile((HDC)m_hdc.get()));
75 #else
76  ((CImage*)m_hdc.get())->saveToFile(m_targetFile + ".png");
77 
78  // Free objects:
79  delete ((CImage*)m_hdc.get());
80 #endif
81 }
82 
83 /*---------------------------------------------------------------
84  drawImage
85 ---------------------------------------------------------------*/
87 {
88 #ifdef _WIN32
89  try
90  {
91  LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new unsigned char
92  [sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD))];
93  // LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new unsigned
94  // char[sizeof(BITMAPINFOHEADER) ];
95 
96  unsigned int imgWidth = (unsigned int)img.getWidth();
97  unsigned int imgHeight = (unsigned int)img.getHeight();
98 
99  pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
100  pBmpInfo->bmiHeader.biWidth = imgWidth;
101  pBmpInfo->bmiHeader.biHeight = imgHeight;
102  pBmpInfo->bmiHeader.biPlanes = 1;
103  // pBmpInfo->bmiHeader.biBitCount = 24;
104  pBmpInfo->bmiHeader.biBitCount = 8;
105  pBmpInfo->bmiHeader.biCompression = BI_RGB;
106  pBmpInfo->bmiHeader.biSizeImage = 0;
107  pBmpInfo->bmiHeader.biXPelsPerMeter =
108  pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
109  pBmpInfo->bmiHeader.biClrUsed = 0;
110  pBmpInfo->bmiHeader.biClrImportant = 0;
111 
112  // Palette
113  for (unsigned char i = 0; i < 255; i++)
114  {
115  pBmpInfo->bmiColors[i].rgbRed = i;
116  pBmpInfo->bmiColors[i].rgbGreen = i;
117  pBmpInfo->bmiColors[i].rgbBlue = i;
118  pBmpInfo->bmiColors[i].rgbReserved = 0;
119  }
120 
121  // unsigned int lineBytes = 3*bmp.Width;
122  unsigned int lineBytes = imgWidth;
123  if (lineBytes % 2) lineBytes++;
124  if (lineBytes % 4) lineBytes += 2;
125 
126  BYTE* ptrBits = new BYTE[lineBytes * imgHeight];
127 
128  for (unsigned int py = 0; py < imgHeight; py++)
129  for (unsigned int px = 0; px < imgWidth; px++)
130  ptrBits[(py * lineBytes + px) + 0] = *img(px, py);
131 
132  HBITMAP hBitmap = CreateDIBitmap(
133  (HDC)m_hdc.get(), &pBmpInfo->bmiHeader, CBM_INIT, ptrBits, pBmpInfo,
134  DIB_RGB_COLORS);
135 
136  ASSERT_(hBitmap != nullptr);
137 
138  BITMAP bm;
139  GetObject(hBitmap, sizeof(bm), &bm);
140 
141  HDC hdcMem = CreateCompatibleDC((HDC)m_hdc.get());
142  HBITMAP hbmT = (HBITMAP)SelectObject(hdcMem, hBitmap);
143 
144  BitBlt(
145  (HDC)m_hdc.get(), x, y, (int)(m_scale * imgWidth),
146  (int)(m_scale * imgHeight), hdcMem, 0, 0, SRCCOPY);
147 
148  SelectObject(hdcMem, hbmT);
149  DeleteDC(hdcMem);
150 
151  // Free mem:
152  // ---------------------------------------
153  DeleteObject(hBitmap);
154  delete[] pBmpInfo;
155  delete[] ptrBits;
156  }
157  catch (...)
158  {
159  THROW_EXCEPTION("Unexpected runtime error!!");
160  }
161 #else
162  ((CImage*)m_hdc.get())->drawImage(x, y, img);
163 #endif
164 }
165 
166 /*---------------------------------------------------------------
167  drawBitmap
168 ---------------------------------------------------------------*/
170  int x0, int y0, int x1, int y1, const mrpt::img::TColor color,
171  unsigned int width, TPenStyle penStyle)
172 {
173 #ifdef _WIN32
174  x0 *= m_scale;
175  y0 *= m_scale;
176  x1 *= m_scale;
177  y1 *= m_scale;
178 
179  HPEN hPen = CreatePen(penStyle, width, (unsigned int)color);
180 
181  HPEN hOldPen = (HPEN)SelectObject((HDC)m_hdc.get(), hPen);
182 
183  MoveToEx((HDC)m_hdc.get(), x0, y0, nullptr);
184  LineTo((HDC)m_hdc.get(), x1, y1);
185 
186  SelectObject((HDC)m_hdc.get(), hOldPen);
187  DeleteObject(hPen);
188 #else
189  ((CImage*)m_hdc.get())->line(x0, y0, x1, y1, color, width, penStyle);
190 #endif
191 }
192 
193 /*---------------------------------------------------------------
194  drawBitmap
195 ---------------------------------------------------------------*/
197  int x0, int y0, const std::string& str, const mrpt::img::TColor color)
198 {
199 #ifdef _WIN32
200  x0 *= m_scale;
201  y0 *= m_scale;
202 
203  ::SetBkMode((HDC)m_hdc.get(), TRANSPARENT);
204  ::SetTextColor((HDC)m_hdc.get(), (unsigned int)color);
205 
206  ::TextOutA((HDC)m_hdc.get(), x0, y0, str.c_str(), (int)str.size());
207 #else
208  ((CImage*)m_hdc.get())->textOut(x0, y0, str, color);
209 #endif
210 }
211 
212 /*---------------------------------------------------------------
213  selectVectorTextFont
214 ---------------------------------------------------------------*/
216  const std::string& fontName, int fontSize, bool bold, bool italic)
217 {
218 #ifdef _WIN32
219  HFONT hFont, oldFont;
220  LOGFONTA lpf;
221 
222  lpf.lfHeight = fontSize;
223  lpf.lfWidth = 0;
224  lpf.lfEscapement = 0;
225  lpf.lfOrientation = 0;
226  lpf.lfWeight = bold ? 700 : 400;
227  lpf.lfItalic = italic ? 1 : 0;
228  lpf.lfUnderline = 0;
229  lpf.lfStrikeOut = 0;
230  lpf.lfCharSet = DEFAULT_CHARSET;
231  lpf.lfOutPrecision = OUT_DEFAULT_PRECIS;
232  lpf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
233  lpf.lfQuality = DEFAULT_QUALITY;
234  lpf.lfPitchAndFamily = DEFAULT_PITCH;
235  os::strcpy(lpf.lfFaceName, LF_FACESIZE, fontName.c_str());
236 
237  hFont = ::CreateFontIndirectA(&lpf);
238 
239  oldFont = (HFONT)::SelectObject((HDC)m_hdc.get(), hFont);
240 
241  if (oldFont) ::DeleteObject(oldFont);
242 #else
243  MRPT_UNUSED_PARAM(fontSize);
244  MRPT_UNUSED_PARAM(bold);
245  MRPT_UNUSED_PARAM(italic);
247 
248  ((CImage*)m_hdc.get())->selectTextFont(fontName);
249 
250  MRPT_TRY_END;
251 #endif
252 }
253 
254 /*---------------------------------------------------------------
255  setPixel
256 ---------------------------------------------------------------*/
257 void CEnhancedMetaFile::setPixel(int x, int y, size_t color)
258 {
259 #ifdef _WIN32
260  ::SetPixel((HDC)m_hdc.get(), x * m_scale, y * m_scale, color);
261 #else
262  ((CImage*)m_hdc.get())->setPixel(x, y, color);
263 #endif
264 }
265 
266 /*---------------------------------------------------------------
267  rectangle
268 ---------------------------------------------------------------*/
270  int x0, int y0, int x1, int y1, TColor color, unsigned int width)
271 {
272  line(x0, y0, x1, y0, color, width);
273  line(x1, y0, x1, y1, color, width);
274  line(x1, y1, x0, y1, color, width);
275  line(x0, y1, x0, y0, color, width);
276 }
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:231
TPenStyle
Definition of pen styles.
Definition: CCanvas.h:54
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:41
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:3531
GLuint color
Definition: glext.h:8300
#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:224
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
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:229
virtual void filledRectangle(int x0, int y0, int x1, int y1, const mrpt::img::TColor color)
Draws a filled rectangle.
Definition: CCanvas.cpp:214
GLint GLvoid * img
Definition: glext.h:3763
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:4101
virtual ~CEnhancedMetaFile()
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:3538
GLsizei const GLfloat * value
Definition: glext.h:4117
A RGB color - 8bit.
Definition: TColor.h:20
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:297
static int LINUX_IMG_HEIGHT_value
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
#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: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020