优化文本绘制函数

serial
BlueMatthew 1 year ago
parent 37ebf5a55e
commit a3ec2f43c3

@ -462,6 +462,7 @@ namespace cv {
currentPos.x -= bbox.xMin * 64; currentPos.x -= bbox.xMin * 64;
currentPos.y -= bbox.yMax * 64; currentPos.y -= bbox.yMax * 64;
int row, col;
#if defined(USING_HB) #if defined(USING_HB)
for (unsigned int i = 0; i < textLen; i++) for (unsigned int i = 0; i < textLen; i++)
@ -530,44 +531,28 @@ namespace cv {
} }
float bearingX = slot->metrics.horiBearingX >> 6; float bearingX = slot->metrics.horiBearingX >> 6;
// float bearingY = slot->metrics.horiBearingY >> 6; float bearingY = slot->metrics.horiBearingY >> 6;
// float advance = slot->advance.x >> 6; // float advance = slot->advance.x >> 6;
// Allocate data for our image and clear it out to transparent. offsetY = bbox.yMax - (glyph_bbox.yMax >> 6);
// Pixel32 *pxl = new Pixel32[imgSize];
// memset(pxl, 0, sizeof(Pixel32) * imgSize);
offsetY = ((bbox.yMax - bbox.yMin) - ((glyph_bbox.yMax - glyph_bbox.yMin))) / 2;
// offsetY = ((bbox.yMax) - (glyph_bbox.yMax >> 6)) / 2;
// Loop over the outline spans and just draw them into the // Loop over the outline spans and just draw them into the
// image. // image.
for (Spans::iterator s = outlineSpans.begin(); s != outlineSpans.end(); ++s) for (Spans::iterator s = outlineSpans.begin(); s != outlineSpans.end(); ++s)
{ {
// int row = (imgHeight - 1 - (s->y - rect.ymin)) - imgHeight + (rect.ymax - rect.ymin); row = imgHeight - (s->y - (currentPos.y >> 6)) - bbox.yMax;
// int row = (imgHeight - 1 - (s->y - rect.ymin)); col = s->x - (currentPos.x >> 6);
int row = (bbox.yMax - bbox.yMin) - (s->y - rect.ymin) + _org.y - offsetY;
if (row < 0 || row >= imgHeight) if (row < 0 || row >= imgHeight)
{ {
continue; continue;
} }
for (int w = 0; w < s->width; ++w) for (int w = 0; w < s->width; ++w)
{ {
int col = s->x - rect.xmin + w + _org.x + bearingX; col++;
if (col < 0 || col >= imgWidth) if (col < 0 || col >= imgWidth)
{ {
continue; continue;
} }
// int row = ((bbox.yMax - bbox.yMin) - (glyph_bbox.yMax - glyph_bbox.yMin)) / 2 - (s->y - rect.ymin) + _org.y;
// mat.at<Vec3b>((imgHeight - 1 - (s->y - rect.ymin)), s->x - rect.xmin + w) = outlineColor;
mat.at<Vec3b>(row, col) = outlineColor; mat.at<Vec3b>(row, col) = outlineColor;
// mat.at<Vec3b>(-(s->y - rect.ymin), s->x - rect.xmin + w) = outlineColor;
// vec3b.
/*
pxl[(int)((imgHeight - 1 - (s->y - rect.ymin)) * imgWidth
+ s->x - rect.xmin + w)] =
Pixel32(outlineCol.r, outlineCol.g, outlineCol.b,
s->coverage);
*/
} }
} }
@ -575,23 +560,20 @@ namespace cv {
// the image. // the image.
for (Spans::iterator s = spans.begin(); s != spans.end(); ++s) for (Spans::iterator s = spans.begin(); s != spans.end(); ++s)
{ {
// int row = (imgHeight - 1 - (s->y - rect.ymin)) - imgHeight + (rect.ymax - rect.ymin); row = imgHeight - (s->y - (currentPos.y >> 6)) - bbox.yMax;
int row = (bbox.yMax - bbox.yMin) - (s->y - rect.ymin) + _org.y - offsetY; col = s->x - (currentPos.x >> 6);
if (row < 0 || row >= imgHeight) if (row < 0 || row >= imgHeight)
{ {
continue; continue;
} }
for (int w = 0; w < s->width; ++w) for (int w = 0; w < s->width; ++w)
{ {
// int col = s->x - rect.xmin + w + _org.x + bearingX; col++;
int col = s->x - rect.xmin + w + _org.x + bearingX;
if (col < 0 || col >= imgWidth) if (col < 0 || col >= imgWidth)
{ {
continue; continue;
} }
// int row = ((bbox.yMax - bbox.yMin) - (glyph_bbox.yMax - glyph_bbox.yMin)) / 2 - (s->y - rect.ymin) + _org.y;
mat.at<Vec3b>(row, col) = fontColor; mat.at<Vec3b>(row, col) = fontColor;
// mat.at<Vec3b>(-(s->y - rect.ymin), s->x - rect.xmin + w) = fontColor;
#if 0 #if 0
Pixel32 &dst = Pixel32 &dst =
pxl[(int)( pxl[(int)(
@ -619,17 +601,9 @@ namespace cv {
#endif #endif
// Update current position ( in FreeType coordinates ) // Update current position ( in FreeType coordinates )
float advance = mFace->glyph->advance.x >> 6; // float advance = mFace->glyph->advance.x >> 6;
currentPos.x += mFace->glyph->advance.x + mFace->glyph->metrics.horiBearingX; currentPos.x += mFace->glyph->advance.x;
currentPos.y += mFace->glyph->advance.y; currentPos.y += mFace->glyph->advance.y;
// currentPos.x += mFace->glyph->metrics.horiBearingX >> 6;
// currentPos.y += mFace->glyph->metrics.horiBearingY >> 6;
// currentPos.x += mFace->glyph->metrics.horiBearingX;
// currentPos.y += mFace->glyph->metrics.horiBearingY;
// break;
_org.x += (mFace->glyph->advance.x + mFace->glyph->metrics.horiBearingX) >> 6;
_org.y += mFace->glyph->advance.y >> 6;
} }
@ -664,7 +638,7 @@ namespace cv {
use_kerning = FT_HAS_KERNING(mFace); use_kerning = FT_HAS_KERNING(mFace);
previous = 0; previous = 0;
FT_Vector currentPos = { 0,0 }; FT_Vector currentPos = { 0, 0 };
FT_Set_Transform(mFace, 0, &currentPos); FT_Set_Transform(mFace, 0, &currentPos);
#if defined(USING_HB) #if defined(USING_HB)
hb_buffer_t *hb_buffer = hb_buffer_create(); hb_buffer_t *hb_buffer = hb_buffer_create();
@ -700,6 +674,7 @@ namespace cv {
// yMax = cv::max(yMax, currentPos.y + (mFace->glyph->advance.y)); // yMax = cv::max(yMax, currentPos.y + (mFace->glyph->advance.y));
continue; continue;
} }
// FT_Set_Transform(mFace, 0, &currentPos);
glyph_index = FT_Get_Char_Index(mFace, wstr[i]); glyph_index = FT_Get_Char_Index(mFace, wstr[i]);
CV_Assert(!FT_Load_Glyph(mFace, glyph_index, 0)); CV_Assert(!FT_Load_Glyph(mFace, glyph_index, 0));
#endif #endif
@ -716,8 +691,8 @@ namespace cv {
FT_BBox bbox; FT_BBox bbox;
// Flip ( in FreeType coordinates ) // Flip ( in FreeType coordinates )
FT_Matrix mtx = { 1 << 16 , 0 , 0 , -(1 << 16) }; // FT_Matrix mtx = { 1 << 16 , 0 , 0 , -(1 << 16) };
FT_Outline_Transform(&outline, &mtx); // FT_Outline_Transform(&outline, &mtx);
// Move to current position ( in FreeType coordinates ) // Move to current position ( in FreeType coordinates )
FT_Outline_Translate(&outline, currentPos.x, currentPos.y); FT_Outline_Translate(&outline, currentPos.x, currentPos.y);
@ -733,13 +708,13 @@ namespace cv {
) )
{ {
bbox.xMin = currentPos.x; bbox.xMin = currentPos.x;
bbox.xMax = currentPos.x + (mFace->glyph->advance.x + mFace->glyph->bitmap_left); bbox.xMax = currentPos.x + (mFace->glyph->advance.x + mFace->glyph->metrics.horiBearingX);
bbox.yMin = yMin; bbox.yMin = yMin;
bbox.yMax = yMax; bbox.yMax = yMax;
} }
// Update current position ( in FreeType coordinates ) // Update current position ( in FreeType coordinates )
currentPos.x += mFace->glyph->advance.x + mFace->glyph->bitmap_left; currentPos.x += mFace->glyph->advance.x;
currentPos.y += mFace->glyph->advance.y; currentPos.y += mFace->glyph->advance.y;
// Update BoundaryBox ( in OpenCV coordinates ) // Update BoundaryBox ( in OpenCV coordinates )
@ -772,7 +747,7 @@ namespace cv {
*_baseLine = yMax; *_baseLine = yMax;
} }
return Size(width, height); return Size(width, height + yMax);
} }

Loading…
Cancel
Save