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-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 static int LINUX_IMG_WIDTH_value = 800;
17 static int LINUX_IMG_HEIGHT_value = 600;
18 
19 using namespace mrpt;
20 using namespace mrpt::img;
21 using namespace mrpt::system;
22 
24 {
26 }
29 {
31 }
33 #include <mrpt/config.h>
34 #ifdef _WIN32
35 #include <windows.h>
36 #endif
37 
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 =
90  (LPBITMAPINFO) new unsigned char[sizeof(BITMAPINFOHEADER) +
91  (256 * sizeof(RGBQUAD))];
92  // LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new unsigned
93  // char[sizeof(BITMAPINFOHEADER) ];
94 
95  unsigned int imgWidth = (unsigned int)img.getWidth();
96  unsigned int imgHeight = (unsigned int)img.getHeight();
97 
98  pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
99  pBmpInfo->bmiHeader.biWidth = imgWidth;
100  pBmpInfo->bmiHeader.biHeight = imgHeight;
101  pBmpInfo->bmiHeader.biPlanes = 1;
102  // pBmpInfo->bmiHeader.biBitCount = 24;
103  pBmpInfo->bmiHeader.biBitCount = 8;
104  pBmpInfo->bmiHeader.biCompression = BI_RGB;
105  pBmpInfo->bmiHeader.biSizeImage = 0;
106  pBmpInfo->bmiHeader.biXPelsPerMeter =
107  pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
108  pBmpInfo->bmiHeader.biClrUsed = 0;
109  pBmpInfo->bmiHeader.biClrImportant = 0;
110 
111  // Palette
112  for (unsigned char i = 0; i < 255; i++)
113  {
114  pBmpInfo->bmiColors[i].rgbRed = i;
115  pBmpInfo->bmiColors[i].rgbGreen = i;
116  pBmpInfo->bmiColors[i].rgbBlue = i;
117  pBmpInfo->bmiColors[i].rgbReserved = 0;
118  }
119 
120  // unsigned int lineBytes = 3*bmp.Width;
121  unsigned int lineBytes = imgWidth;
122  if (lineBytes % 2) lineBytes++;
123  if (lineBytes % 4) lineBytes += 2;
124 
125  BYTE* ptrBits = new BYTE[lineBytes * imgHeight];
126 
127  for (unsigned int py = 0; py < imgHeight; py++)
128  for (unsigned int px = 0; px < imgWidth; px++)
129  ptrBits[(py * lineBytes + px) + 0] = *img(px, py);
130 
131  HBITMAP hBitmap = CreateDIBitmap(
132  (HDC)m_hdc.get(), &pBmpInfo->bmiHeader, CBM_INIT, ptrBits, pBmpInfo,
133  DIB_RGB_COLORS);
134 
135  ASSERT_(hBitmap != nullptr);
136 
137  BITMAP bm;
138  GetObject(hBitmap, sizeof(bm), &bm);
139 
140  HDC hdcMem = CreateCompatibleDC((HDC)m_hdc.get());
141  HBITMAP hbmT = (HBITMAP)SelectObject(hdcMem, hBitmap);
142 
143  BitBlt(
144  (HDC)m_hdc.get(), x, y, (int)(m_scale * imgWidth),
145  (int)(m_scale * imgHeight), hdcMem, 0, 0, SRCCOPY);
146 
147  SelectObject(hdcMem, hbmT);
148  DeleteDC(hdcMem);
149 
150  // Free mem:
151  // ---------------------------------------
152  DeleteObject(hBitmap);
153  delete[] pBmpInfo;
154  delete[] ptrBits;
155  }
156  catch (...)
157  {
158  THROW_EXCEPTION("Unexpected runtime error!!");
159  }
160 #else
161  ((CImage*)m_hdc.get())->drawImage(x, y, img);
162 #endif
163 }
164 
165 /*---------------------------------------------------------------
166  drawBitmap
167 ---------------------------------------------------------------*/
169  int x0, int y0, int x1, int y1, const mrpt::img::TColor color,
170  unsigned int width, TPenStyle penStyle)
171 {
172 #ifdef _WIN32
173  x0 *= m_scale;
174  y0 *= m_scale;
175  x1 *= m_scale;
176  y1 *= m_scale;
177 
178  HPEN hPen = CreatePen(penStyle, width, (unsigned int)color);
179 
180  HPEN hOldPen = (HPEN)SelectObject((HDC)m_hdc.get(), hPen);
181 
182  MoveToEx((HDC)m_hdc.get(), x0, y0, nullptr);
183  LineTo((HDC)m_hdc.get(), x1, y1);
184 
185  SelectObject((HDC)m_hdc.get(), hOldPen);
186  DeleteObject(hPen);
187 #else
188  ((CImage*)m_hdc.get())->line(x0, y0, x1, y1, color, width, penStyle);
189 #endif
190 }
191 
192 /*---------------------------------------------------------------
193  drawBitmap
194 ---------------------------------------------------------------*/
196  int x0, int y0, const std::string& str, const mrpt::img::TColor color)
197 {
198 #ifdef _WIN32
199  x0 *= m_scale;
200  y0 *= m_scale;
201 
202  ::SetBkMode((HDC)m_hdc.get(), TRANSPARENT);
203  ::SetTextColor((HDC)m_hdc.get(), (unsigned int)color);
204 
205  ::TextOutA((HDC)m_hdc.get(), x0, y0, str.c_str(), (int)str.size());
206 #else
207  ((CImage*)m_hdc.get())->textOut(x0, y0, str, color);
208 #endif
209 }
210 
211 /*---------------------------------------------------------------
212  selectVectorTextFont
213 ---------------------------------------------------------------*/
215  const std::string& fontName, int fontSize, bool bold, 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
242  MRPT_UNUSED_PARAM(fontSize);
243  MRPT_UNUSED_PARAM(bold);
244  MRPT_UNUSED_PARAM(italic);
246 
247  ((CImage*)m_hdc.get())->selectTextFont(fontName);
248 
249  MRPT_TRY_END;
250 #endif
251 }
252 
253 /*---------------------------------------------------------------
254  setPixel
255 ---------------------------------------------------------------*/
256 void CEnhancedMetaFile::setPixel(int x, int y, size_t color)
257 {
258 #ifdef _WIN32
259  ::SetPixel((HDC)m_hdc.get(), x * m_scale, y * m_scale, color);
260 #else
261  ((CImage*)m_hdc.get())->setPixel(x, y, color);
262 #endif
263 }
264 
265 /*---------------------------------------------------------------
266  rectangle
267 ---------------------------------------------------------------*/
269  int x0, int y0, int x1, int y1, TColor color, unsigned int width)
270 {
271  line(x0, y0, x1, y0, color, width);
272  line(x1, y0, x1, y1, color, width);
273  line(x1, y1, x0, y1, color, width);
274  line(x0, y1, x0, y0, color, width);
275 }
os.h
mrpt::img::CEnhancedMetaFile::m_targetFile
std::string m_targetFile
Definition: CEnhancedMetaFile.h:30
mrpt::img::CEnhancedMetaFile::CEnhancedMetaFile
CEnhancedMetaFile(const std::string &targetFileName, int scaleFactor=1)
Constructor.
Definition: CEnhancedMetaFile.cpp:41
mrpt::img::CEnhancedMetaFile::m_hdc
void_ptr_noncopy m_hdc
Definition: CEnhancedMetaFile.h:27
mrpt::img::CEnhancedMetaFile::setPixel
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
Definition: CEnhancedMetaFile.cpp:256
mrpt::img::CEnhancedMetaFile::selectVectorTextFont
virtual void selectVectorTextFont(const std::string &fontName, int fontSize, bool bold=false, bool italic=false)
Select the current font used when drawing text.
Definition: CEnhancedMetaFile.cpp:214
CEnhancedMetaFile.h
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
mrpt::img::CEnhancedMetaFile::textOut
void textOut(int x0, int y0, const std::string &str, const mrpt::img::TColor color) override
Places a text label.
Definition: CEnhancedMetaFile.cpp:195
LINUX_IMG_WIDTH_value
static int LINUX_IMG_WIDTH_value
Definition: CEnhancedMetaFile.cpp:16
mrpt::img::CEnhancedMetaFile::drawImage
void drawImage(int x, int y, const mrpt::img::CImage &img) override
Draws an image as a bitmap at a given position.
Definition: CEnhancedMetaFile.cpp:84
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::img::CEnhancedMetaFile::LINUX_IMG_HEIGHT
static int LINUX_IMG_HEIGHT()
Definition: CEnhancedMetaFile.cpp:32
MRPT_TRY_END
#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
mrpt::img::CEnhancedMetaFile::rectangle
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)
Definition: CEnhancedMetaFile.cpp:268
mrpt::utils::CImage
mrpt::img::CImage CImage
Definition: utils/CImage.h:7
mrpt::img
Definition: CCanvas.h:17
mrpt::img::CEnhancedMetaFile::line
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.
Definition: CEnhancedMetaFile.cpp:168
MRPT_TRY_START
#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
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
color
GLuint color
Definition: glext.h:8300
img-precomp.h
mrpt::img::CEnhancedMetaFile::~CEnhancedMetaFile
virtual ~CEnhancedMetaFile()
Destructor.
Definition: CEnhancedMetaFile.cpp:61
mrpt::system::os::strcpy
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
Definition: os.cpp:297
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
img
GLint GLvoid * img
Definition: glext.h:3763
mrpt::img::CEnhancedMetaFile::m_scale
int m_scale
Definition: CEnhancedMetaFile.h:28
value
GLsizei const GLfloat * value
Definition: glext.h:4117
width
GLenum GLsizei width
Definition: glext.h:3531
string
GLsizei const GLchar ** string
Definition: glext.h:4101
CImage.h
LINUX_IMG_HEIGHT_value
static int LINUX_IMG_HEIGHT_value
Definition: CEnhancedMetaFile.cpp:17
mrpt::img::CEnhancedMetaFile::LINUX_IMG_WIDTH
static int LINUX_IMG_WIDTH()
Definition: CEnhancedMetaFile.cpp:27
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::img::CCanvas::TPenStyle
TPenStyle
Definition of pen styles.
Definition: CCanvas.h:56
x
GLenum GLint x
Definition: glext.h:3538
mrpt::img::CEnhancedMetaFile::m_hFont
void_ptr_noncopy m_hFont
Definition: CEnhancedMetaFile.h:29
mrpt::non_copiable_ptr_basic::get
T *& get()
Definition: safe_pointers.h:146
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
mrpt::img::CCanvas::filledRectangle
virtual void filledRectangle(int x0, int y0, int x1, int y1, const mrpt::img::TColor color)
Draws a filled rectangle.
Definition: CCanvas.cpp:214
mrpt::img::CCanvas::selectTextFont
virtual void selectTextFont(const std::string &fontName)
Select the current font used when drawing text.
Definition: CCanvas.cpp:229



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST