-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcar_chtext.cpp
More file actions
executable file
·396 lines (289 loc) · 8.18 KB
/
car_chtext.cpp
File metadata and controls
executable file
·396 lines (289 loc) · 8.18 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <wchar.h>
#include <assert.h>
#include <locale.h>
#include <ctype.h>
#include "car_chtext.h"
CvxText::CvxText(const char *freeType)
{
assert(freeType != NULL);
if(FT_Init_FreeType(&m_library)) throw;
if(FT_New_Face(m_library, freeType, 0, &m_face)) throw;
restoreFont();
setlocale(LC_ALL, "");
}
CvxText::~CvxText()
{
FT_Done_Face (m_face);
FT_Done_FreeType(m_library);
}
void CvxText::getFont(int *type, CvScalar *size, bool *underline, float *diaphaneity)
{
if(type) *type = m_fontType;
if(size) *size = m_fontSize;
if(underline) *underline = m_fontUnderline;
if(diaphaneity) *diaphaneity = m_fontDiaphaneity;
}
void CvxText::setFont(int *type, CvScalar *size, bool *underline, float *diaphaneity)
{
if(type)
{
if(type >= 0) m_fontType = *type;
}
if(size)
{
m_fontSize.val[0] = fabs(size->val[0]);
m_fontSize.val[1] = fabs(size->val[1]);
m_fontSize.val[2] = fabs(size->val[2]);
m_fontSize.val[3] = fabs(size->val[3]);
}
if(underline)
{
m_fontUnderline = *underline;
}
if(diaphaneity)
{
m_fontDiaphaneity = *diaphaneity;
}
FT_Set_Pixel_Sizes(m_face, 0, m_fontSize.val[0]);
}
void CvxText::restoreFont()
{
m_fontType = 0;
m_fontSize.val[0] = 24;
m_fontSize.val[1] = 0.1;
m_fontSize.val[2] = 0.1;
m_fontSize.val[3] = 0;
m_fontUnderline = false;
m_fontDiaphaneity = 1.0;
FT_Set_Pixel_Sizes(m_face, 0, m_fontSize.val[0]);
}
#if 0
int CvxText::putText(cv::Mat &frame, const char *text, CvPoint pos)
{
return putText(frame, text, pos, CV_RGB(255,255,255));
}
int CvxText::putText(cv::Mat &frame, const wchar_t *text, CvPoint pos)
{
return putText(frame, text, pos, CV_RGB(255,255,255));
}
//
int CvxText::putText(cv::Mat &frame, const char *text, CvPoint pos, CvScalar color)
{
if(frame.empty() == NULL) return -1;
if(text == NULL) return -1;
//
int i;
for (i = 0; text[i] != '\0';)
{
int wc = text[i];
/* if(!isascii(wc)) mbtowc(&wc, &text[i++], 2);*/
if (!isascii(wc)) {
mbtowc(reinterpret_cast<wchar_t *>(&wc), &text[i], 3);
i += 3;
} else {
i++;
}
putWChar(frame, wc, pos, color);
}
return i;
}
int CvxText::putText(cv::Mat &frame, const wchar_t *text, CvPoint pos, CvScalar color)
{
if(frame.empty() == NULL) return -1;
if(text == NULL) return -1;
//
int i;
for(i = 0; text[i] != '\0'; ++i)
{
putWChar(frame, text[i], pos, color);
}
return i;
}
void CvxText::putWChar(cv::Mat &frame, wchar_t wc, CvPoint &pos, CvScalar color)
{
IplImage* img=NULL;
img = &(IplImage)frame;
//IplImage img = &IplImage(frame);
//IplImage* img = (IplImage*)&frame;
//IplImage img = IplImage(frame);
FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);
FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO);
FT_GlyphSlot slot = m_face->glyph;
int rows = slot->bitmap.rows;
int cols = slot->bitmap.width;
//
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < cols; ++j)
{
int off = ((img->origin==0)? i: (rows-1-i)) \
* slot->bitmap.pitch + j/8;
if(slot->bitmap.buffer[off] & (0xC0 >> (j%8)))
{
int r = (img->origin==0)? pos.y - (rows-1-i): pos.y + i;;
int c = pos.x + j;
if(r >= 0 && r < img->height
&& c >= 0 && c < img->width)
{
CvScalar scalar = cvGet2D(img, r, c);
float p = m_fontDiaphaneity;
for(int k = 0; k < 4; ++k)
{
scalar.val[k] = scalar.val[k]*(1-p) + color.val[k]*p;
}
cvSet2D(img, r, c, scalar);
}
}
} // end for
} // end for
double space = m_fontSize.val[0]*m_fontSize.val[1];
double sep = m_fontSize.val[0]*m_fontSize.val[2];
pos.x += (int)((cols? cols: space) + sep);
}
#endif
#if 1
int CvxText::putText(cv::Mat& img, char* text, cv::Point pos)
{
return putText(img, text, pos, CV_RGB(255, 255, 255));
}
int CvxText::putText(cv::Mat& img, const wchar_t* text, cv::Point pos)
{
return putText(img, text, pos, CV_RGB(255,255,255));
}
int CvxText::putText(cv::Mat& img, const char* text, cv::Point pos, cv::Scalar color)
{
if (img.data == nullptr) return -1;
if (text == nullptr) return -1;
int i;
for (i = 0; text[i] != '\0'; ++i) {
wchar_t wc = text[i];
if(!isascii(wc)) mbtowc(&wc, &text[i++], 2);
putWChar(img, wc, pos, color);
}
return i;
}
int CvxText::putText(cv::Mat& img, const wchar_t* text, cv::Point pos, cv::Scalar color)
{
if (img.data == nullptr) return -1;
if (text == nullptr) return -1;
int i;
for(i = 0; text[i] != '\0'; ++i) {
putWChar(img, text[i], pos, color);
}
return i;
}
void CvxText::putWChar(cv::Mat& img, wchar_t wc, cv::Point& pos, cv::Scalar color)
{
FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);
FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO);
FT_GlyphSlot slot = m_face->glyph;
int rows = slot->bitmap.rows;
int cols = slot->bitmap.width;
for (int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
int off = i * slot->bitmap.pitch + j/8;
if (slot->bitmap.buffer[off] & (0xC0 >> (j%8))) {
int r = pos.y - (rows-1-i);
int c = pos.x + j;
if(r >= 0 && r < img.rows && c >= 0 && c < img.cols) {
cv::Vec3b pixel = img.at<cv::Vec3b>(cv::Point(c, r));
cv::Scalar scalar = cv::Scalar(pixel.val[0], pixel.val[1], pixel.val[2]);
float p = m_fontDiaphaneity;
for (int k = 0; k < 4; ++k) {
scalar.val[k] = scalar.val[k]*(1-p) + color.val[k]*p;
}
img.at<cv::Vec3b>(cv::Point(c, r))[0] = (unsigned char)(scalar.val[0]);
img.at<cv::Vec3b>(cv::Point(c, r))[1] = (unsigned char)(scalar.val[1]);
img.at<cv::Vec3b>(cv::Point(c, r))[2] = (unsigned char)(scalar.val[2]);
}
}
}
}
// 修改下一个字的输出位置
double space = m_fontSize.val[0]*m_fontSize.val[1];
double sep = m_fontSize.val[0]*m_fontSize.val[2];
pos.x += (int)((cols? cols: space) + sep);
}
#endif
#if 0
int CvxText::putText(IplImage *img, const char *text, CvPoint pos)
{
return putText(img, text, pos, CV_RGB(255,255,255));
}
int CvxText::putText(IplImage *img, const wchar_t *text, CvPoint pos)
{
return putText(img, text, pos, CV_RGB(255,255,255));
}
//
int CvxText::putText(IplImage *img, const char *text, CvPoint pos, CvScalar color)
{
if(img == NULL) return -1;
if(text == NULL) return -1;
//
int i;
for (i = 0; text[i] != '\0';)
{
int wc = text[i];
/* if(!isascii(wc)) mbtowc(&wc, &text[i++], 2);*/
if (!isascii(wc)) {
mbtowc(reinterpret_cast<wchar_t *>(&wc), &text[i], 3);
i += 3;
} else {
i++;
}
putWChar(img, wc, pos, color);
}
return i;
}
int CvxText::putText(IplImage *img, const wchar_t *text, CvPoint pos, CvScalar color)
{
if(img == NULL) return -1;
if(text == NULL) return -1;
//
int i;
for(i = 0; text[i] != '\0'; ++i)
{
putWChar(img, text[i], pos, color);
}
return i;
}
void CvxText::putWChar(IplImage *img, wchar_t wc, CvPoint &pos, CvScalar color)
{
FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);
FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO);
FT_GlyphSlot slot = m_face->glyph;
int rows = slot->bitmap.rows;
int cols = slot->bitmap.width;
//
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < cols; ++j)
{
int off = ((img->origin==0)? i: (rows-1-i)) \
* slot->bitmap.pitch + j/8;
if(slot->bitmap.buffer[off] & (0xC0 >> (j%8)))
{
int r = (img->origin==0)? pos.y - (rows-1-i): pos.y + i;;
int c = pos.x + j;
if(r >= 0 && r < img->height
&& c >= 0 && c < img->width)
{
CvScalar scalar = cvGet2D(img, r, c);
float p = m_fontDiaphaneity;
for(int k = 0; k < 4; ++k)
{
scalar.val[k] = scalar.val[k]*(1-p) + color.val[k]*p;
}
cvSet2D(img, r, c, scalar);
}
}
} // end for
} // end for
double space = m_fontSize.val[0]*m_fontSize.val[1];
double sep = m_fontSize.val[0]*m_fontSize.val[2];
pos.x += (int)((cols? cols: space) + sep);
}
#endif