5 #include <scene_rdl2/common/grid_util/Parser.h> 6 #include <scene_rdl2/common/math/BBox.h> 7 #include <scene_rdl2/common/math/Math.h> 14 #include <unordered_map> 17 #include <freetype2/ft2build.h> 18 #include FT_FREETYPE_H 23 using FreeTypeBBox = scene_rdl2::math::BBox<FT_Vector>;
32 C3(
unsigned char r,
unsigned char g,
unsigned char b) : mR(r), mG(g), mB(b) {}
33 C3(
const C3& c3) : mR(c3.mR), mG(c3.mG), mB(c3.mB) {}
35 bool isBlack()
const {
return (mR == 0x0 && mG == 0x0 && mB == 0x0); }
51 const FT_Bitmap& bitmap,
unsigned bitmapLeft,
unsigned bitmapTop,
unsigned advanceX);
53 unsigned char get(
int bx,
int by)
const {
return mBuffer[by * mPitch + bx]; }
55 bool isSpace()
const {
return std::isspace(static_cast<unsigned char>(mC)); }
57 unsigned getRows()
const {
return mRows; }
58 unsigned getWidth()
const {
return mWidth; }
59 unsigned getBitmapLeft()
const {
return mBitmapLeft; }
60 unsigned getBitmapTop()
const {
return mBitmapTop; }
61 unsigned getAdvanceX()
const {
return mAdvanceX; }
87 unsigned mBitmapLeft {0};
88 unsigned mBitmapTop {0};
89 unsigned mAdvanceX {0};
91 std::vector<unsigned char> mBuffer;
102 Font(
const std::string& fontTTFFileName,
int fontSizePoint)
103 : mFontTTFFileName(fontTTFFileName)
104 , mFontSizePoint(fontSizePoint)
109 const std::string& getFontTTFFileName()
const {
return mFontTTFFileName; }
111 int getFontSizePoint()
const {
return mFontSizePoint; }
112 const FT_Face& getFace()
const {
return mFace; }
114 std::shared_ptr<FontCacheItem> getFontCacheItem(
char c);
116 float getBgYAdjustScale()
const {
return mBgYAdjustScale; }
119 static FT_Pos iToftPos(
const int v) {
return static_cast<FT_Pos
>(v * 64); }
120 static int ftPosToi(
const FT_Pos v) {
return static_cast<int>(v) / 64; }
123 void setupFontFace();
127 const std::string mFontTTFFileName;
128 int mFontSizePoint {0};
130 FT_Library mFtLibrary;
133 float mBgYAdjustScale {0.0f};
135 std::unordered_map<unsigned, std::shared_ptr<FontCacheItem>> mFontCacheMap;
149 void set(std::shared_ptr<FontCacheItem> fontCacheItem,
150 const FT_Vector& fontPos,
152 float bgYAdjustScale,
156 mFontCacheItem = fontCacheItem;
158 mFontBasePos = fontPos;
159 mFontSize = FT_Vector{fontCacheItem->getAdvanceX(), Font::iToftPos(fontHeight)};
160 mFontDataPos = FT_Vector {mFontBasePos.x + Font::iToftPos(fontCacheItem->getBitmapLeft()),
161 mFontBasePos.y - Font::iToftPos(fontCacheItem->getBitmapTop())};
162 mBgYAdjustScale = bgYAdjustScale;
167 unsigned getAdvanceX()
const {
return mFontCacheItem->getAdvanceX(); }
169 std::shared_ptr<FontCacheItem> getFontCacheItem()
const {
return mFontCacheItem; }
171 unsigned getBaseX()
const {
return Font::ftPosToi(mFontBasePos.x); }
172 unsigned getBaseY()
const {
return Font::ftPosToi(mFontBasePos.y); }
173 unsigned getWidth()
const {
174 return static_cast<unsigned>(Font::ftPosToi(mFontBasePos.x + mFontSize.x) - getBaseX());
176 unsigned getHeight()
const {
177 return static_cast<unsigned>(Font::ftPosToi(mFontBasePos.y + mFontSize.y) - getBaseY());
180 unsigned getPosX()
const {
return Font::ftPosToi(mFontDataPos.x); }
181 unsigned getPosY()
const {
return Font::ftPosToi(mFontDataPos.y); }
183 unsigned getStepX()
const {
return Font::ftPosToi(getAdvanceX()); }
185 float getBgYAdjustScale()
const {
return mBgYAdjustScale; }
187 const C3& getFgC3()
const {
return mFgC3; }
188 const C3& getBgC3()
const {
return mBgC3; }
190 FreeTypeBBox getBBox()
const;
193 std::string show(
unsigned winHeight = 0)
const;
196 std::shared_ptr<FontCacheItem> mFontCacheItem {
nullptr};
217 FT_Vector mFontBasePos;
219 FT_Vector mFontDataPos;
221 float mBgYAdjustScale {0.0f};
238 void resetCharItemArray(
Overlay& overlay);
242 const unsigned startX,
const unsigned startY,
const unsigned overlayHeight,
243 const std::string& str,
const C3& c3);
245 void setupAllCharItems(std::vector<std::shared_ptr<OverlayCharItem>>& outCharItemArray);
247 unsigned getFirstCharStepX()
const;
249 FreeTypeBBox getBBox()
const;
253 bool entryNewCharItem(
Overlay& overlay,
255 const FT_Vector& fontPos,
263 unsigned mStartX {0};
264 unsigned mStartY {0};
265 unsigned mOverlayHeight {0};
267 std::vector<std::shared_ptr<OverlayCharItem>> mCharItemArray;
273 using BBox2i = scene_rdl2::math::BBox2i;
277 void set(
const BBox2i& bbox,
const C3& c,
unsigned char alpha)
284 const BBox2i& getBBox()
const {
return mBBox; }
285 const C3& getC()
const {
return mC; }
286 unsigned char getAlpha()
const {
return mAlpha; }
289 BBox2i mBBox {scene_rdl2::math::Vec2i {0, 0}, scene_rdl2::math::Vec2i {0, 0}};
291 unsigned char mAlpha {0};
297 using Arg = scene_rdl2::grid_util::Arg;
298 using Parser = scene_rdl2::grid_util::Parser;
299 using BBox2i = scene_rdl2::math::BBox2i;
307 Overlay() { parserConfigure(); }
308 Overlay(
const unsigned width,
const unsigned height)
310 resize(width, height);
311 clear({0, 0, 0}, 0,
true);
316 inline void resize(
const unsigned width,
const unsigned height);
317 void clear(
const C3& c3,
const unsigned char alpha,
bool doParallel);
318 void boxFill(
const C3& c3,
const unsigned char alpha,
const BBox2i& bbox,
bool doParallel);
319 void fill(
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a);
321 unsigned getWidth()
const {
return mWidth; }
322 unsigned getHeight()
const {
return mHeight; }
324 unsigned getMaxYLines(
const Font& font,
unsigned& offsetBottomPixY,
unsigned& stepPixY)
const;
326 std::shared_ptr<OverlayCharItem> getNewOverlayCharItem() {
return getMemOverlayCharItem(); }
327 void restoreOverlayCharItemMem(std::shared_ptr<OverlayCharItem> ptr) { setMemOverlayCharItem(ptr); }
330 bool drawStr(
Font& font,
331 const unsigned startX,
332 const unsigned startY,
333 const std::string& str,
336 void drawStrFlush(
bool doParallel);
339 void drawBox(
const BBox2i box,
const C3& c3,
const unsigned char alpha);
340 void drawBoxBar(
const BBox2i box,
const C3& c3,
const unsigned char alpha);
341 void drawBoxFlush(
bool doParallel);
344 unsigned getFontStepX()
const {
return mFontStepX; }
346 size_t getDrawStrItemTotal()
const {
return mDrawStrArray.size(); }
347 BBox2i calcDrawBbox(
const size_t startStrItemId,
const size_t endStrItemId)
const;
349 void finalizeRgb888(std::vector<unsigned char>& rgbFrame,
351 unsigned frameHeight,
355 std::vector<unsigned char>* bgArchive,
356 bool doParallel)
const;
358 bool savePPM(
const std::string& filename)
const;
360 Parser& getParser() {
return mParser; }
362 static size_t msgDisplayLen(
const std::string& msg);
363 static size_t msgDisplayWidth(
const std::string& msg);
366 inline unsigned getPixOffset(
const unsigned x,
const unsigned y)
const;
367 inline unsigned getPixDataOffset(
const unsigned x,
const unsigned y)
const;
368 inline unsigned char* getPixDataAddr(
const unsigned x,
const unsigned y);
369 inline const unsigned char* getPixDataAddr(
const unsigned x,
const unsigned y)
const;
370 inline bool isPixInside(
const unsigned x,
const unsigned y)
const;
372 inline void alphaBlendPixC4(
const C3& fgC3,
const unsigned char fgAlpha,
unsigned char* bgPixC4)
const;
373 inline void alphaBlendPixC3(
const C3& fgC3,
const unsigned char fgAlpha,
unsigned char* bgPixC3)
const;
374 inline C3 blendCol3(
const C3& fgC3,
float fgFraction,
unsigned char* bgPix)
const;
375 inline unsigned char clampCol0255(
const float v0255)
const;
376 inline void setCol4(
const C3& inC3,
unsigned char inAlpha,
unsigned char* outPix)
const;
377 inline void setCol3(
const C3& inC3,
unsigned char* outPix)
const;
379 void overlayDrawFontCache(std::shared_ptr<OverlayCharItem> charItem);
381 inline void resizeRgb888(std::vector<unsigned char>& rgbFrame,
unsigned width,
unsigned height)
const;
382 inline void clearRgb888(std::vector<unsigned char>& rgbFrame)
const;
383 void copyRgb888(
const std::vector<unsigned char>& in,
384 std::vector<unsigned char>& out,
385 const bool doParallel)
const;
387 void bakeOverlayMainRgb888(
const std::vector<unsigned char>& fgFrameRGBA,
388 const unsigned fgWidth,
389 const unsigned fgHeight,
392 std::vector<unsigned char>& bgFrameRGB,
393 const unsigned bgWidth,
394 const unsigned bgHeight,
395 const bool top2bottomFlag,
396 const bool doParallel)
const;
398 std::string showPixFrameRGBA(
const std::vector<unsigned char>& frameRGBA,
399 const unsigned frameWidth,
400 const unsigned frameHeight,
402 const unsigned pixY)
const;
404 std::shared_ptr<OverlayStrItem> getMemOverlayStrItem();
405 void setMemOverlayStrItem(std::shared_ptr<OverlayStrItem> ptr);
406 std::shared_ptr<OverlayCharItem> getMemOverlayCharItem();
407 void setMemOverlayCharItem(std::shared_ptr<OverlayCharItem> ptr);
408 std::shared_ptr<OverlayBoxItem> getMemOverlayBoxItem();
409 void setMemOverlayBoxItem(std::shared_ptr<OverlayBoxItem> ptr);
411 void parserConfigure();
412 std::string showMemPoolSize()
const;
416 std::vector<std::shared_ptr<OverlayStrItem>> mDrawStrArray;
417 std::vector<std::shared_ptr<OverlayBoxItem>> mDrawBoxArray;
418 std::vector<std::shared_ptr<OverlayBoxItem>> mDrawBoxBarArray;
420 unsigned mMaxOverlayStrItemMemPool {0};
421 unsigned mMaxOverlayCharItemMemPool {0};
422 unsigned mMaxOverlayBoxItemMemPool {0};
423 std::deque<std::shared_ptr<OverlayStrItem>> mOverlayStrItemMemPool;
424 std::deque<std::shared_ptr<OverlayCharItem>> mOverlayCharItemMemPool;
425 std::deque<std::shared_ptr<OverlayBoxItem>> mOverlayBoxItemMemPool;
427 unsigned mFontStepX {0};
430 unsigned mHeight {0};
431 std::vector<unsigned char> mPixelsRGBA;
437 Overlay::resize(
const unsigned width,
const unsigned height)
439 if (mWidth == width && mHeight == height)
return;
444 mPixelsRGBA.resize(mWidth * mHeight * 4);
450 Overlay::getPixOffset(
const unsigned x,
const unsigned y)
const 452 return y * mWidth + x;
456 Overlay::getPixDataOffset(
const unsigned x,
const unsigned y)
const 458 return getPixOffset(x, y) * 4;
461 inline unsigned char*
462 Overlay::getPixDataAddr(
const unsigned x,
const unsigned y)
464 return &mPixelsRGBA[getPixDataOffset(x, y)];
467 inline const unsigned char*
468 Overlay::getPixDataAddr(
const unsigned x,
const unsigned y)
const 470 return &mPixelsRGBA[getPixDataOffset(x, y)];
474 Overlay::isPixInside(
const unsigned x,
const unsigned y)
const 476 return (x < mWidth && y < mHeight);
482 Overlay::alphaBlendPixC4(
const C3& fgC3,
const unsigned char fgAlpha,
unsigned char* bgPixC4)
const 487 float currFgA =
static_cast<float>(fgAlpha) / 255.0f;
488 float currBgA =
static_cast<float>(bgPixC4[3]) / 255.0f;
489 float currOutA = currFgA + currBgA * (1.0f - currFgA);
490 if (currOutA == 0.0f) {
491 setCol4({0, 0, 0}, 0, bgPixC4);
493 auto calcC = [&](
const unsigned char fgC,
const float fgA,
494 const unsigned char bgC,
const float bgA,
496 float outC = (
static_cast<float>(fgC) * fgA + static_cast<float>(bgC) * bgA * (1.0f - fgA)) / outA;
497 return static_cast<unsigned char>(clampCol0255(outC));
500 setCol4({calcC(fgC3.mR, currFgA, bgPixC4[0], currBgA, currOutA),
501 calcC(fgC3.mG, currFgA, bgPixC4[1], currBgA, currOutA),
502 calcC(fgC3.mB, currFgA, bgPixC4[2], currBgA, currOutA)},
503 static_cast<unsigned char>(clampCol0255(currOutA * 255.0f)),
509 Overlay::alphaBlendPixC3(
const C3& fgC3,
const unsigned char fgAlpha,
unsigned char* bgPixC3)
const 514 setCol3(blendCol3(fgC3, static_cast<float>(fgAlpha) / 255.0f, bgPixC3),
519 Overlay::blendCol3(
const C3& fgC3,
float fgFraction,
unsigned char* bgPix)
const 521 auto blendCol = [&](
const unsigned char fg,
const unsigned char bg) {
522 return clampCol0255(static_cast<float>(fg) * fgFraction +
523 static_cast<float>(bg) * (1.0f - fgFraction));
525 return C3(blendCol(fgC3.mR, bgPix[0]),
526 blendCol(fgC3.mG, bgPix[1]),
527 blendCol(fgC3.mB, bgPix[2]));
531 Overlay::clampCol0255(
const float v0255)
const 533 return static_cast<unsigned char>(scene_rdl2::math::clamp(v0255, 0.0f, 255.0f));
537 Overlay::setCol4(
const C3& inC3,
unsigned char inAlpha,
unsigned char* outPix)
const 539 setCol3(inC3, outPix);
544 Overlay::setCol3(
const C3& inC3,
unsigned char* outPix)
const 554 Overlay::resizeRgb888(std::vector<unsigned char>& rgbFrame,
unsigned width,
unsigned height)
const 556 unsigned dataSize = width * height * 3;
557 if (rgbFrame.size() != dataSize) {
558 rgbFrame.resize(dataSize);
559 clearRgb888(rgbFrame);
564 Overlay::clearRgb888(std::vector<unsigned char>& rgbFrame)
const 566 for (
size_t i = 0; i < rgbFrame.size(); ++i) rgbFrame[i] = 0x0;
Definition: TelemetryOverlay.h:140
Definition: TelemetryOverlay.h:294
Definition: TelemetryOverlay.h:25
Definition: TelemetryOverlay.h:270
Definition: TelemetryOverlay.h:44
Definition: TelemetryOverlay.h:96
Definition: ClientReceiverConsoleDriver.cc:9
Definition: TelemetryOverlay.h:229