1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
| #include "OverlayOSD.h"
void OverlayOSD::GetStringSize(HDC hDC, const char* str, int* w, int* h) { SIZE size; GetTextExtentPoint32A(hDC, str, strlen(str), &size); if (w != 0) *w = size.cx; if (h != 0) *h = size.cy; }
void OverlayOSD::GetDateTime(int dateTimeType, char* dateTimeText) { SYSTEMTIME systm; GetLocalTime(&systm);
char text[1024]; int n = 0;
if (dateTimeType == 0) { n += sprintf_s(text + n, sizeof(text) - n - 1, "%d-%02d-%02d", systm.wYear, systm.wMonth, systm.wDay);
n += sprintf_s(text + n, sizeof(text) - n - 1, " %02d:%02d:%02d", systm.wHour, systm.wMinute, systm.wSecond); } else if (dateTimeType == 1) { n += sprintf_s(text + n, sizeof(text) - n - 1, "%d-%02d-%02d", systm.wYear, systm.wMonth, systm.wDay);
int hour = systm.wHour % 12; if (hour == 0) hour = 12;
n += sprintf_s(text + n, sizeof(text) - n - 1, " %02d:%02d:%02d", hour, systm.wMinute, systm.wSecond);
n += sprintf_s(text + n, sizeof(text) - n - 1, " %s", systm.wHour < 12 ? "AM" : "PM");
} else if (dateTimeType == 2) { n += sprintf_s(text + n, sizeof(text) - n - 1, "%d年%02d月%02d日", systm.wYear, systm.wMonth, systm.wDay);
n += sprintf_s(text + n, sizeof(text) - n - 1, " %02d:%02d:%02d", systm.wHour, systm.wMinute, systm.wSecond); } else if (dateTimeType == 3) { n += sprintf_s(text + n, sizeof(text) - n - 1, "%d年%02d月%02d日", systm.wYear, systm.wMonth, systm.wDay);
int hour = systm.wHour % 12; if (hour == 0) hour = 12;
n += sprintf_s(text + n, sizeof(text) - n - 1, " %02d:%02d:%02d", hour, systm.wMinute, systm.wSecond);
n += sprintf_s(text + n, sizeof(text) - n - 1, " %s", systm.wHour < 12 ? "AM" : "PM"); }
strcpy(dateTimeText, text); }
void OverlayOSD::InitOSD(OSDConfig osdConfig, int imageWidth, int imageHeight) { osdConfig_ = osdConfig;
char text[1024]; if (osdConfig_.osdType == DateTime) { GetDateTime(osdConfig_.dateTimeType, text); } else if (osdConfig_.osdType == FixedText) { strcpy(text, osdConfig_.fixedText); }
overlayText_ = text; imageWidth_ = imageWidth; imageHeight_ = imageHeight;
logFonta_.lfHeight = -osdConfig_.fontSize; logFonta_.lfWidth = 0; logFonta_.lfEscapement = 0; logFonta_.lfOrientation = 0; logFonta_.lfWeight = 5; logFonta_.lfItalic = false; logFonta_.lfUnderline = false; logFonta_.lfStrikeOut = 0; logFonta_.lfCharSet = DEFAULT_CHARSET; logFonta_.lfOutPrecision = 0; logFonta_.lfClipPrecision = 0; logFonta_.lfQuality = PROOF_QUALITY; logFonta_.lfPitchAndFamily = 0; strcpy_s(logFonta_.lfFaceName, osdConfig_.fontFamily);
hFont_ = CreateFontIndirectA(&logFonta_); hDC_ = CreateCompatibleDC(0); hOldFont_ = (HFONT)SelectObject(hDC_, hFont_);
int strBaseW = 0, strBaseH = 0; char buf[1 << 12]; strcpy_s(buf, text); char* bufT[1 << 12]; { int nnh = 0; int cw, ch;
const char* ln = strtok_s(buf, "\n", bufT); while (ln != 0) { GetStringSize(hDC_, ln, &cw, &ch); strBaseW = max(strBaseW, cw); strBaseH = max(strBaseH, ch);
ln = strtok_s(0, "\n", bufT); nnh++; } singleRow_ = strBaseH; strBaseH *= nnh; }
BITMAPINFO bmp = { 0 }; BITMAPINFOHEADER& bih = bmp.bmiHeader; drawLineStep_ = strBaseW * 3 % 4 == 0 ? strBaseW * 3 : (strBaseW * 3 + 4 - ((strBaseW * 3) % 4));
bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = strBaseW; bih.biHeight = strBaseH; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = BI_RGB; bih.biSizeImage = strBaseH * drawLineStep_; bih.biClrUsed = 0; bih.biClrImportant = 0;
hBmp_ = CreateDIBSection(hDC_, &bmp, DIB_RGB_COLORS, &pDibData_, 0, 0);
CV_Assert(pDibData_ != 0); hOldBmp_ = (HBITMAP)SelectObject(hDC_, hBmp_);
SetTextColor(hDC_, RGB(255, 255, 255)); SetBkColor(hDC_, 0);
Point startPoint = Point(osdConfig_.horizontalCoordinate, osdConfig_.verticalCoordinate);
int startX = 0; switch (osdConfig_.horizontalAlignment) { case OSDHorizontalAlignment::Left: startX = startPoint.x; break; case OSDHorizontalAlignment::HCenter: startX = imageWidth / 2 - strBaseW / 2 + startPoint.x; break; case OSDHorizontalAlignment::Right: startX = imageWidth - strBaseW + (-startPoint.x); break; }
int startY = 0; switch (osdConfig_.verticalAlignment) { case OSDVerticalAlignment::Top: startY = startPoint.y; break; case OSDVerticalAlignment::VCenter: startY = imageHeight / 2 - strBaseH / 2 + startPoint.y; break; case OSDVerticalAlignment::Bottom: startY = imageHeight - strBaseH + (-startPoint.y); break; }
OverlayCoordinate = Rect(startX, startY, strBaseW, strBaseH); }
void OverlayOSD::OverlayText(Mat& dst) { char text[1024]; if (osdConfig_.osdType == DateTime) { GetDateTime(osdConfig_.dateTimeType, text); } else if (osdConfig_.osdType == FixedText) { strcpy(text, osdConfig_.fixedText); }
char buf[1 << 12]; char* bufT[1 << 12];
strcpy_s(buf, text); const char* ln = strtok_s(buf, "\n", bufT); int outTextY = 0; while (ln != 0) { TextOutA(hDC_, 0, outTextY, ln, strlen(ln)); outTextY += singleRow_; ln = strtok_s(0, "\n", bufT); }
int x, y, r, b; Point org = Point(OverlayCoordinate.x, OverlayCoordinate.y); int strBaseW = OverlayCoordinate.width; int strBaseH = OverlayCoordinate.height; if (org.x > dst.cols || org.y > dst.rows) return; x = org.x < 0 ? -org.x : 0; y = org.y < 0 ? -org.y : 0;
r = org.x + strBaseW > dst.cols ? dst.cols - org.x - 1 : strBaseW - 1; b = org.y + strBaseH > dst.rows ? dst.rows - org.y - 1 : strBaseH - 1; org.x = org.x < 0 ? 0 : org.x; org.y = org.y < 0 ? 0 : org.y;
Scalar color = Scalar(255, 255, 255);
uchar* dstData = (uchar*)dst.data; int dstStep = dst.step / sizeof(dstData[0]); unsigned char* pImg = (unsigned char*)dst.data + org.x * dst.channels() + org.y * dstStep; unsigned char* pStr = (unsigned char*)pDibData_ + x * 3; for (int tty = y; tty <= b; ++tty) { unsigned char* subImg = pImg + (tty - y) * dstStep; unsigned char* subStr = pStr + (strBaseH - tty - 1) * drawLineStep_; for (int ttx = x; ttx <= r; ++ttx) { for (int n = 0; n < dst.channels(); ++n) { double vtxt = subStr[n] / 255.0; int cvv = vtxt * color.val[n] + (1 - vtxt) * subImg[n]; subImg[n] = cvv > 255 ? 255 : (cvv < 0 ? 0 : cvv); }
subStr += 3; subImg += dst.channels(); } } }
void OverlayOSD::OverlayText(unsigned char* yData, unsigned char* uvData) { char text[1024]; if (osdConfig_.osdType == DateTime) { GetDateTime(osdConfig_.dateTimeType, text); } else if (osdConfig_.osdType == FixedText) { strcpy(text, osdConfig_.fixedText); }
char buf[1 << 12]; char* bufT[1 << 12];
strcpy_s(buf, text); const char* ln = strtok_s(buf, "\n", bufT); int outTextY = 0; while (ln != 0) { TextOutA(hDC_, 0, outTextY, ln, strlen(ln)); outTextY += singleRow_; ln = strtok_s(0, "\n", bufT); }
auto osdText = (unsigned char*)pDibData_;
auto osd_size = sizeof(unsigned char) * OverlayCoordinate.width * OverlayCoordinate.height * 3; unsigned char* osdData = nullptr; cudaMalloc(&osdData, osd_size); cudaMemcpy(osdData, osdText, osd_size, cudaMemcpyHostToDevice);
OSDNearest(yData, uvData, imageWidth_, imageHeight_, osdData, OverlayCoordinate, drawLineStep_, osdConfig_.osdStyleType);
cudaFree(osdData); }
void OverlayOSD::Dispose() { SelectObject(hDC_, hOldBmp_); SelectObject(hDC_, hOldFont_); DeleteObject(hBmp_); DeleteObject(hFont_); DeleteDC(hDC_); }
|