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
| void Watermark::Nv12AddDateWatermark(unsigned char* nv12Buf, int width, int height, const char *text){
unsigned int picSize = width * height * 3 / 2;
unsigned int index = 0;
int text_alpha = 0;
int x = m_x_pos;
for (const char* p = text; *p; p++) {
if (FT_Load_Char(face, *p, FT_LOAD_RENDER)) {
mylog(E, "Failed to load glyph %c", *p);
continue;
}
FT_GlyphSlot slot = face->glyph;
// Calculate the y position for bottom alignment
int y_pos = m_y_pos - slot->bitmap_top;// Align the bottom
// Merge text bitmap (monochrome) onto NV12 image with alpha masking
for (int i = 0; i < slot->bitmap.rows; i++) {
for (int j = 0; j < slot->bitmap.width; j++) {
text_alpha = slot->bitmap.buffer[i * slot->bitmap.pitch + j]; // Alpha value (0-255) of text pixel
// Only overlay if text pixel is not fully transparent (adjust threshold as needed)
// int y_pos = y + i - slot->bitmap_top;
if (text_alpha > 0) {
// Overlay text pixel on Y plane
index = (y_pos + i) * width + x + j;
float alpha = slot->bitmap.buffer[i * slot->bitmap.pitch + j] / 255.0f;
nv12Buf[index] = (unsigned char)((1.0f - alpha) * nv12Buf[index] + alpha * 255);
mylog(E, " %d", slot->bitmap.buffer[i * slot->bitmap.pitch + j]);
assert(index < picSize);
}
}
}
x += slot->advance.x >> 6; // Advance to the next character position
}
}
|