| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, 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 |
// |
| 12 |
#include <mrpt/config.h> |
| 13 |
#include <mrpt/img/CEnhancedMetaFile.h> |
| 14 |
#include <mrpt/img/CImage.h> |
| 15 |
#include <mrpt/system/os.h> |
| 16 |
|
| 17 |
#ifdef _WIN32 |
| 18 |
#define WIN32_LEAN_AND_MEAN |
| 19 |
#include <windows.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
using namespace mrpt; |
| 23 |
using namespace mrpt::img; |
| 24 |
using namespace mrpt::system; |
| 25 |
|
| 26 |
static int LINUX_IMG_WIDTH_value = 800; |
| 27 |
static int LINUX_IMG_HEIGHT_value = 600; |
| 28 |
|
| 29 |
void CEnhancedMetaFile::LINUX_IMG_WIDTH(int value) { LINUX_IMG_WIDTH_value = value; } |
| 30 |
int CEnhancedMetaFile::LINUX_IMG_WIDTH() { return LINUX_IMG_WIDTH_value; } |
| 31 |
void CEnhancedMetaFile::LINUX_IMG_HEIGHT(int value) { LINUX_IMG_HEIGHT_value = value; } |
| 32 |
int CEnhancedMetaFile::LINUX_IMG_HEIGHT() { return LINUX_IMG_HEIGHT_value; } |
| 33 |
/*--------------------------------------------------------------- |
| 34 |
Constructor |
| 35 |
---------------------------------------------------------------*/ |
| 36 |
CEnhancedMetaFile::CEnhancedMetaFile(const std::string& targetFileName, int scaleFactor) : |
| 37 |
m_scale(scaleFactor), m_targetFile(targetFileName) |
| 38 |
{ |
| 39 |
#ifdef _WIN32 |
| 40 |
m_hdc = CreateEnhMetaFileA(nullptr, targetFileName.c_str(), nullptr, nullptr); |
| 41 |
if (!m_hdc.get()) THROW_EXCEPTION("Can't create EMF file!!!"); |
| 42 |
#else |
| 43 |
m_hdc = (void*)new CImage(LINUX_IMG_WIDTH_value, LINUX_IMG_HEIGHT_value); |
| 44 |
((CImage*)m_hdc.get()) |
| 45 |
->filledRectangle( |
| 46 |
0, 0, LINUX_IMG_WIDTH_value - 1, LINUX_IMG_HEIGHT_value - 1, TColor(0, 0, 0)); |
| 47 |
#endif |
| 48 |
} |
| 49 |
|
| 50 |
/*--------------------------------------------------------------- |
| 51 |
Destructor |
| 52 |
---------------------------------------------------------------*/ |
| 53 |
CEnhancedMetaFile::~CEnhancedMetaFile() |
| 54 |
{ |
| 55 |
#ifdef _WIN32 |
| 56 |
// Free objects: |
| 57 |
if (m_hFont.get()) |
| 58 |
{ |
| 59 |
DeleteObject(m_hFont.get()); |
| 60 |
m_hFont = nullptr; |
| 61 |
} |
| 62 |
|
| 63 |
// Finish EMF: |
| 64 |
DeleteEnhMetaFile(CloseEnhMetaFile((HDC)m_hdc.get())); |
| 65 |
#else |
| 66 |
bool ok = ((CImage*)m_hdc.get())->saveToFile(m_targetFile + ".png"); |
| 67 |
if (!ok) std::cerr << "Error saving image to file: " << m_targetFile << std::endl; |
| 68 |
|
| 69 |
// Free objects: |
| 70 |
delete ((CImage*)m_hdc.get()); |
| 71 |
#endif |
| 72 |
} |
| 73 |
|
| 74 |
/*--------------------------------------------------------------- |
| 75 |
drawImage |
| 76 |
---------------------------------------------------------------*/ |
| 77 |
void CEnhancedMetaFile::drawImage(int x, int y, const mrpt::img::CImage& img) |
| 78 |
{ |
| 79 |
#ifdef _WIN32 |
| 80 |
try |
| 81 |
{ |
| 82 |
LPBITMAPINFO pBmpInfo = |
| 83 |
(LPBITMAPINFO) new unsigned char[sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD))]; |
| 84 |
// LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new unsigned |
| 85 |
// char[sizeof(BITMAPINFOHEADER) ]; |
| 86 |
|
| 87 |
unsigned int imgWidth = (unsigned int)img.getWidth(); |
| 88 |
unsigned int imgHeight = (unsigned int)img.getHeight(); |
| 89 |
|
| 90 |
pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 91 |
pBmpInfo->bmiHeader.biWidth = imgWidth; |
| 92 |
pBmpInfo->bmiHeader.biHeight = imgHeight; |
| 93 |
pBmpInfo->bmiHeader.biPlanes = 1; |
| 94 |
// pBmpInfo->bmiHeader.biBitCount = 24; |
| 95 |
pBmpInfo->bmiHeader.biBitCount = 8; |
| 96 |
pBmpInfo->bmiHeader.biCompression = BI_RGB; |
| 97 |
pBmpInfo->bmiHeader.biSizeImage = 0; |
| 98 |
pBmpInfo->bmiHeader.biXPelsPerMeter = pBmpInfo->bmiHeader.biYPelsPerMeter = 0; |
| 99 |
pBmpInfo->bmiHeader.biClrUsed = 0; |
| 100 |
pBmpInfo->bmiHeader.biClrImportant = 0; |
| 101 |
|
| 102 |
// Palette |
| 103 |
for (unsigned char i = 0; i < 255; i++) |
| 104 |
{ |
| 105 |
pBmpInfo->bmiColors[i].rgbRed = i; |
| 106 |
pBmpInfo->bmiColors[i].rgbGreen = i; |
| 107 |
pBmpInfo->bmiColors[i].rgbBlue = i; |
| 108 |
pBmpInfo->bmiColors[i].rgbReserved = 0; |
| 109 |
} |
| 110 |
|
| 111 |
// unsigned int lineBytes = 3*bmp.Width; |
| 112 |
unsigned int lineBytes = imgWidth; |
| 113 |
if (lineBytes % 2) lineBytes++; |
| 114 |
if (lineBytes % 4) lineBytes += 2; |
| 115 |
|
| 116 |
BYTE* ptrBits = new BYTE[lineBytes * imgHeight]; |
| 117 |
|
| 118 |
for (unsigned int py = 0; py < imgHeight; py++) |
| 119 |
for (unsigned int px = 0; px < imgWidth; px++) |
| 120 |
ptrBits[(py * lineBytes + px) + 0] = *img(px, py); |
| 121 |
|
| 122 |
HBITMAP hBitmap = CreateDIBitmap( |
| 123 |
(HDC)m_hdc.get(), &pBmpInfo->bmiHeader, CBM_INIT, ptrBits, pBmpInfo, DIB_RGB_COLORS); |
| 124 |
|
| 125 |
ASSERT_(hBitmap != nullptr); |
| 126 |
|
| 127 |
BITMAP bm; |
| 128 |
GetObject(hBitmap, sizeof(bm), &bm); |
| 129 |
|
| 130 |
HDC hdcMem = CreateCompatibleDC((HDC)m_hdc.get()); |
| 131 |
HBITMAP hbmT = (HBITMAP)SelectObject(hdcMem, hBitmap); |
| 132 |
|
| 133 |
BitBlt( |
| 134 |
(HDC)m_hdc.get(), x, y, (int)(m_scale * imgWidth), (int)(m_scale * imgHeight), hdcMem, 0, 0, |
| 135 |
SRCCOPY); |
| 136 |
|
| 137 |
SelectObject(hdcMem, hbmT); |
| 138 |
DeleteDC(hdcMem); |
| 139 |
|
| 140 |
// Free mem: |
| 141 |
// --------------------------------------- |
| 142 |
DeleteObject(hBitmap); |
| 143 |
delete[] pBmpInfo; |
| 144 |
delete[] ptrBits; |
| 145 |
} |
| 146 |
catch (...) |
| 147 |
{ |
| 148 |
THROW_EXCEPTION("Unexpected runtime error!!"); |
| 149 |
} |
| 150 |
#else |
| 151 |
((CImage*)m_hdc.get())->drawImage(x, y, img); |
| 152 |
#endif |
| 153 |
} |
| 154 |
|
| 155 |
/*--------------------------------------------------------------- |
| 156 |
drawBitmap |
| 157 |
---------------------------------------------------------------*/ |
| 158 |
void CEnhancedMetaFile::line( |
| 159 |
int x0, |
| 160 |
int y0, |
| 161 |
int x1, |
| 162 |
int y1, |
| 163 |
const mrpt::img::TColor color, |
| 164 |
unsigned int width, |
| 165 |
TPenStyle penStyle) |
| 166 |
{ |
| 167 |
#ifdef _WIN32 |
| 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 |
---------------------------------------------------------------*/ |
| 190 |
void CEnhancedMetaFile::textOut( |
| 191 |
int x0, int y0, const std::string& str, const mrpt::img::TColor color) |
| 192 |
{ |
| 193 |
#ifdef _WIN32 |
| 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 |
---------------------------------------------------------------*/ |
| 209 |
void CEnhancedMetaFile::selectVectorTextFont( |
| 210 |
[[maybe_unused]] const std::string& fontName, |
| 211 |
[[maybe_unused]] int fontSize, |
| 212 |
[[maybe_unused]] bool bold, |
| 213 |
[[maybe_unused]] bool italic) |
| 214 |
{ |
| 215 |
#ifdef _WIN32 |
| 216 |
HFONT hFont, oldFont; |
| 217 |
LOGFONTA lpf; |
| 218 |
|
| 219 |
lpf.lfHeight = fontSize; |
| 220 |
lpf.lfWidth = 0; |
| 221 |
lpf.lfEscapement = 0; |
| 222 |
lpf.lfOrientation = 0; |
| 223 |
lpf.lfWeight = bold ? 700 : 400; |
| 224 |
lpf.lfItalic = italic ? 1 : 0; |
| 225 |
lpf.lfUnderline = 0; |
| 226 |
lpf.lfStrikeOut = 0; |
| 227 |
lpf.lfCharSet = DEFAULT_CHARSET; |
| 228 |
lpf.lfOutPrecision = OUT_DEFAULT_PRECIS; |
| 229 |
lpf.lfClipPrecision = CLIP_DEFAULT_PRECIS; |
| 230 |
lpf.lfQuality = DEFAULT_QUALITY; |
| 231 |
lpf.lfPitchAndFamily = DEFAULT_PITCH; |
| 232 |
os::strcpy(lpf.lfFaceName, LF_FACESIZE, fontName.c_str()); |
| 233 |
|
| 234 |
hFont = ::CreateFontIndirectA(&lpf); |
| 235 |
|
| 236 |
oldFont = (HFONT)::SelectObject((HDC)m_hdc.get(), hFont); |
| 237 |
|
| 238 |
if (oldFont) ::DeleteObject(oldFont); |
| 239 |
#else |
| 240 |
MRPT_TRY_START; |
| 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 _WIN32 |
| 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 |
---------------------------------------------------------------*/ |
| 263 |
void CEnhancedMetaFile::rectangle(int x0, int y0, int x1, int y1, TColor color, unsigned int width) |
| 264 |
{ |
| 265 |
line(x0, y0, x1, y0, color, width); |
| 266 |
line(x1, y0, x1, y1, color, width); |
| 267 |
line(x1, y1, x0, y1, color, width); |
| 268 |
line(x0, y1, x0, y0, color, width); |
| 269 |
} |