Total Variation Inpainting using Split Bregman
drawtext.c
Go to the documentation of this file.
1 
24 #include "drawtext.h"
25 
26 
28 static struct fontinfo
29 {
30  int Offset;
31  unsigned char Width;
32  unsigned char Height;
33  char YShift;
34  char Spacing;
35 } FontInfo[] = {
36  { 0, 0, 0, 0, 6}, /* space */
37  { 0, 2, 13, 0, 1}, /* ! */
38  { 4, 6, 5, 0, 1}, /* " */
39  { 8, 12, 14, -1, 1}, /* # */
40  { 29, 9, 17, -1, 1}, /* $ */
41  { 49, 15, 13, 0, 1}, /* % */
42  { 74, 12, 13, 0, 1}, /* & */
43  { 94, 2, 5, 0, 1}, /* ' */
44  { 96, 4, 16, -1, 1}, /* ( */
45  { 104, 4, 16, -1, 1}, /* ) */
46  { 112, 7, 8, 5, 1}, /* * */
47  { 119, 12, 12, 1, 1}, /* + */
48  { 137, 3, 4, 11, 1}, /* , */
49  { 139, 5, 2, 7, 1}, /* - */
50  { 141, 2, 2, 11, 1}, /* . */
51  { 142, 6, 15, 0, 1}, /* / */
52  { 154, 9, 13, 0, 1}, /* 0 */
53  { 169, 8, 13, 0, 1}, /* 1 */
54  { 182, 8, 13, 0, 1}, /* 2 */
55  { 195, 9, 13, 0, 1}, /* 3 */
56  { 210, 10, 13, 0, 1}, /* 4 */
57  { 227, 8, 13, 0, 1}, /* 5 */
58  { 240, 9, 13, 0, 1}, /* 6 */
59  { 255, 8, 13, 0, 1}, /* 7 */
60  { 268, 9, 13, 0, 1}, /* 8 */
61  { 283, 9, 13, 0, 1}, /* 9 */
62  { 298, 2, 9, 4, 2}, /* : */
63  { 301, 3, 11, 4, 2}, /* ; */
64  { 306, 11, 10, 3, 2}, /* < */
65  { 320, 11, 6, 4, 2}, /* = */
66  { 329, 11, 10, 3, 2}, /* > */
67  { 343, 7, 13, 0, 1}, /* ? */
68  { 355, 16, 16, 0, 1}, /* @ */
69  { 387, 12, 13, 0, 1}, /* A */
70  { 407, 9, 13, 0, 1}, /* B */
71  { 422, 11, 13, 0, 1}, /* C */
72  { 440, 11, 13, 0, 1}, /* D */
73  { 458, 8, 13, 0, 1}, /* E */
74  { 471, 8, 13, 0, 1}, /* F */
75  { 484, 11, 13, 0, 1}, /* G */
76  { 502, 10, 13, 0, 1}, /* H */
77  { 519, 2, 13, 0, 1}, /* I */
78  { 523, 5, 17, -4, 1}, /* J */
79  { 534, 10, 13, 0, 1}, /* K */
80  { 551, 8, 13, 0, 1}, /* L */
81  { 564, 12, 13, 0, 1}, /* M */
82  { 584, 10, 13, 0, 1}, /* N */
83  { 601, 12, 13, 0, 1}, /* O */
84  { 621, 8, 13, 0, 1}, /* P */
85  { 634, 12, 15, -2, 1}, /* Q */
86  { 657, 10, 13, 0, 1}, /* R */
87  { 674, 9, 13, 0, 1}, /* S */
88  { 689, 12, 13, 0, 1}, /* T */
89  { 709, 10, 13, 0, 1}, /* U */
90  { 726, 12, 13, 0, 1}, /* V */
91  { 746, 17, 13, 0, 0}, /* W */
92  { 774, 11, 13, 0, 0}, /* X */
93  { 792, 12, 13, 0, 0}, /* Y */
94  { 812, 11, 13, 0, 1}, /* Z */
95  { 830, 4, 16, -1, 1}, /* [ */
96  { 838, 6, 15, -2, 1}, /* \ */
97  { 850, 4, 16, -1, 1}, /* ] */
98  { 858, 11, 5, 1, 1}, /* ^ */
99  { 865, 9, 2, 16, 1}, /* _ */
100  { 868, 4, 3, 10, 1}, /* ` */
101  { 870, 8, 10, 3, 1}, /* a */
102  { 880, 9, 14, -1, 1}, /* b */
103  { 896, 8, 10, 3, 1}, /* c */
104  { 906, 9, 14, -1, 1}, /* d */
105  { 922, 10, 10, 3, 1}, /* e */
106  { 935, 7, 14, -1, 1}, /* f */
107  { 948, 9, 14, 3, 1}, /* g */
108  { 964, 8, 14, -1, 1}, /* h */
109  { 978, 2, 14, -1, 1}, /* i */
110  { 982, 4, 18, -1, 1}, /* j */
111  { 991, 8, 14, -1, 1}, /* k */
112  {1005, 2, 14, -1, 1}, /* l */
113  {1009, 14, 10, 3, 1}, /* m */
114  {1027, 8, 10, 3, 1}, /* n */
115  {1037, 10, 10, 3, 1}, /* o */
116  {1050, 9, 14, 3, 1}, /* p */
117  {1066, 9, 14, 3, 1}, /* q */
118  {1082, 6, 10, 3, 1}, /* r */
119  {1090, 7, 10, 3, 1}, /* s */
120  {1099, 6, 13, 0, 1}, /* t */
121  {1109, 8, 10, 3, 1}, /* u */
122  {1119, 10, 10, 3, 0}, /* v */
123  {1132, 13, 10, 3, 1}, /* w */
124  {1149, 10, 10, 3, 1}, /* x */
125  {1162, 10, 14, 3, 1}, /* y */
126  {1180, 8, 10, 3, 1}, /* z */
127  {1190, 8, 17, -1, 1}, /* { */
128  {1207, 2, 18, -1, 1}, /* | */
129  {1212, 8, 17, -1, 1}, /* } */
130  {1229, 11, 3, 5, 1}}; /* ~ */
131 
132 
134 static unsigned char FontBitmapData[] =
135  {255, 255, 195, 192, 207, 60, 243, 204, 4, 64, 68, 12, 192, 200, 127,
136  247, 255, 9, 129, 144, 255, 239, 254, 19, 3, 48, 50, 2, 32, 8, 4, 15,
137  143, 238, 150, 67, 224, 252, 31, 4, 194, 113, 127, 243, 240, 32, 16, 8,
138  0, 120, 17, 152, 67, 49, 134, 98, 12, 200, 25, 144, 30, 79, 1, 51, 2,
139  102, 8, 204, 49, 152, 67, 49, 3, 192, 15, 1, 248, 48, 131, 0, 56, 3, 192,
140  110, 108, 118, 195, 204, 24, 225, 199, 254, 62, 112, 255, 192, 50, 102,
141  76, 204, 204, 196, 102, 35, 196, 102, 35, 51, 51, 50, 102, 76, 17, 37,
142  81, 195, 138, 164, 136, 6, 0, 96, 6, 0, 96, 6, 15, 255, 255, 240, 96, 6,
143  0, 96, 6, 0, 96, 109, 64, 255, 192, 240, 12, 49, 134, 24, 227, 12, 49,
144  198, 24, 99, 12, 0, 62, 63, 152, 216, 60, 30, 15, 7, 131, 193, 224, 216,
145  207, 227, 224, 56, 248, 216, 24, 24, 24, 24, 24, 24, 24, 24, 255, 255,
146  124, 254, 135, 3, 3, 7, 6, 12, 24, 48, 96, 255, 255, 126, 127, 160, 224,
147  48, 57, 248, 252, 7, 1, 128, 224, 255, 231, 224, 7, 1, 192, 176, 108, 19,
148  8, 198, 49, 12, 255, 255, 240, 48, 12, 3, 0, 126, 126, 96, 96, 124, 126,
149  71, 3, 3, 3, 135, 254, 124, 30, 31, 156, 92, 12, 6, 243, 253, 199, 193,
150  224, 216, 239, 227, 224, 255, 255, 6, 6, 6, 14, 12, 12, 28, 24, 24, 56,
151  48, 62, 63, 184, 248, 62, 59, 249, 253, 199, 193, 224, 248, 239, 227,
152  224, 62, 63, 184, 216, 60, 31, 29, 254, 123, 1, 129, 209, 207, 195, 192,
153  240, 3, 192, 108, 0, 3, 106, 0, 0, 32, 60, 31, 31, 15, 129, 240, 15, 128,
154  62, 1, 224, 4, 255, 255, 252, 0, 0, 15, 255, 255, 192, 128, 30, 1, 240,
155  7, 192, 62, 7, 195, 227, 224, 240, 16, 0, 121, 254, 24, 48, 97, 134, 24,
156  48, 96, 1, 131, 0, 7, 224, 31, 248, 60, 28, 112, 6, 96, 3, 227, 99, 199,
157  227, 198, 99, 198, 102, 199, 252, 227, 112, 96, 0, 112, 0, 56, 16, 31,
158  240, 7, 192, 6, 0, 96, 15, 0, 240, 25, 129, 152, 25, 131, 12, 63, 199,
159  254, 96, 102, 6, 192, 48, 254, 127, 176, 216, 108, 55, 243, 249, 134,
160  193, 224, 240, 255, 239, 224, 15, 199, 253, 192, 176, 12, 1, 128, 48, 6,
161  0, 192, 12, 1, 192, 159, 240, 252, 254, 31, 243, 7, 96, 124, 7, 128, 240,
162  30, 3, 192, 120, 31, 7, 127, 207, 224, 255, 255, 192, 192, 192, 255, 255,
163  192, 192, 192, 192, 255, 255, 255, 255, 192, 192, 192, 254, 254, 192,
164  192, 192, 192, 192, 192, 15, 199, 253, 192, 176, 12, 1, 135, 240, 254, 3,
165  192, 108, 13, 193, 159, 225, 248, 192, 240, 60, 15, 3, 192, 255, 255,
166  255, 3, 192, 240, 60, 15, 3, 192, 192, 255, 255, 255, 192, 24, 198, 49,
167  140, 99, 24, 198, 49, 140, 254, 224, 193, 176, 204, 99, 48, 216, 60, 15,
168  3, 96, 204, 49, 140, 51, 6, 192, 192, 192, 192, 192, 192, 192, 192, 192,
169  192, 192, 192, 192, 255, 255, 224, 127, 15, 240, 253, 139, 217, 189, 155,
170  207, 60, 243, 198, 60, 99, 192, 60, 3, 192, 48, 224, 248, 63, 15, 195,
171  216, 246, 60, 207, 27, 198, 240, 252, 63, 7, 193, 192, 31, 131, 252, 112,
172  230, 6, 192, 60, 3, 192, 60, 3, 192, 54, 6, 112, 227, 252, 31, 128, 252,
173  254, 199, 195, 195, 199, 254, 252, 192, 192, 192, 192, 192, 31, 131, 252,
174  112, 230, 6, 192, 60, 3, 192, 60, 3, 192, 54, 6, 112, 227, 252, 31, 128,
175  24, 0, 192, 252, 63, 140, 115, 12, 195, 49, 207, 227, 240, 198, 48, 204,
176  51, 6, 193, 192, 62, 63, 184, 88, 12, 3, 224, 252, 7, 1, 128, 224, 255,
177  231, 224, 255, 255, 255, 6, 0, 96, 6, 0, 96, 6, 0, 96, 6, 0, 96, 6, 0,
178  96, 6, 0, 192, 240, 60, 15, 3, 192, 240, 60, 15, 3, 192, 240, 54, 25,
179  254, 63, 0, 192, 54, 6, 96, 102, 6, 48, 195, 12, 25, 129, 152, 25, 128,
180  240, 15, 0, 96, 6, 0, 193, 193, 224, 224, 216, 216, 204, 108, 102, 54,
181  51, 27, 24, 216, 216, 108, 108, 54, 54, 27, 27, 7, 7, 3, 131, 129, 193,
182  192, 112, 230, 24, 230, 13, 192, 240, 28, 3, 128, 120, 27, 7, 48, 199,
183  48, 110, 14, 224, 118, 6, 48, 193, 152, 25, 128, 240, 6, 0, 96, 6, 0, 96,
184  6, 0, 96, 6, 0, 255, 255, 252, 7, 1, 192, 48, 14, 3, 128, 224, 24, 6, 1,
185  192, 127, 255, 254, 255, 204, 204, 204, 204, 204, 204, 255, 195, 6, 24,
186  97, 195, 12, 48, 225, 134, 24, 48, 192, 255, 51, 51, 51, 51, 51, 51, 255,
187  14, 3, 96, 198, 48, 108, 6, 255, 255, 192, 198, 48, 60, 126, 71, 3, 63,
188  255, 195, 199, 255, 123, 192, 96, 48, 24, 13, 231, 251, 143, 131, 193,
189  224, 240, 124, 127, 246, 240, 30, 127, 97, 192, 192, 192, 192, 97, 127,
190  30, 1, 128, 192, 96, 51, 219, 255, 143, 131, 193, 224, 240, 124, 119,
191  249, 236, 31, 31, 230, 31, 3, 255, 255, 252, 1, 129, 127, 199, 224, 30,
192  124, 193, 143, 255, 204, 24, 48, 96, 193, 131, 6, 0, 61, 191, 248, 248,
193  60, 30, 15, 7, 199, 127, 158, 192, 104, 103, 241, 240, 192, 192, 192,
194  192, 222, 254, 231, 195, 195, 195, 195, 195, 195, 195, 240, 255, 255,
195  240, 51, 0, 51, 51, 51, 51, 51, 51, 254, 192, 192, 192, 192, 195, 198,
196  204, 216, 240, 240, 216, 204, 198, 195, 255, 255, 255, 240, 222, 123,
197  251, 238, 56, 240, 195, 195, 15, 12, 60, 48, 240, 195, 195, 15, 12, 48,
198  222, 254, 231, 195, 195, 195, 195, 195, 195, 195, 30, 31, 230, 27, 3,
199  192, 240, 60, 13, 134, 127, 135, 128, 222, 127, 184, 248, 60, 30, 15, 7,
200  199, 255, 111, 48, 24, 12, 6, 0, 61, 191, 248, 248, 60, 30, 15, 7, 199,
201  127, 158, 192, 96, 48, 24, 12, 223, 254, 48, 195, 12, 48, 195, 0, 125,
202  255, 15, 7, 195, 193, 195, 254, 248, 97, 134, 63, 253, 134, 24, 97, 134,
203  31, 60, 195, 195, 195, 195, 195, 195, 195, 231, 127, 123, 192, 240, 54,
204  25, 134, 51, 12, 195, 48, 120, 30, 3, 0, 199, 30, 56, 241, 70, 219, 102,
205  219, 54, 217, 162, 199, 28, 56, 225, 199, 0, 225, 216, 99, 48, 204, 30,
206  7, 131, 48, 204, 97, 184, 112, 192, 240, 54, 25, 134, 51, 12, 193, 224,
207  120, 12, 3, 0, 192, 96, 120, 28, 0, 255, 255, 6, 12, 28, 56, 48, 112,
208  255, 255, 15, 31, 24, 24, 24, 24, 24, 240, 240, 56, 24, 24, 24, 24, 24,
209  31, 15, 255, 255, 255, 255, 240, 240, 248, 24, 24, 24, 24, 24, 15, 15,
210  28, 24, 24, 24, 24, 24, 248, 240, 124, 63, 254, 31, 0};
211 
212 
214 static void PutPixel(unsigned char *Image, int Width, int Height,
215  int x, int y, unsigned char Value)
216 {
217  if(0 <= x && x < Width && 0 <= y && y < Height)
218  Image[x + Width*y] = Value;
219 }
220 
221 
231 static void DrawGlyph(unsigned char *Image, int Width, int Height,
232  const unsigned char *Glyph, int GlyphWidth, int GlyphHeight,
233  int x0, int y0, unsigned Value)
234 {
235  unsigned int Packed, Mask;
236  int x = 0, y = 0;
237 
238  if(GlyphWidth)
239  while(1)
240  {
241  /* Unpack each byte in the glyph data into eight pixels */
242  for(Mask = 0x80, Packed = *Glyph; Mask; Mask >>= 1)
243  {
244  if((Packed & Mask))
245  PutPixel(Image, Width, Height, x0 + x, y0 + y, Value);
246 
247  if(++x >= GlyphWidth)
248  {
249  x = 0;
250 
251  if(++y >= GlyphHeight)
252  return;
253  }
254  }
255 
256  Glyph++;
257  }
258 }
259 
260 
262 int TextWidth(const char *Text)
263 {
264  int c, Width = 0;
265 
266  if(!Text || !*Text)
267  return 0;
268 
269  while((c = *(Text++)))
270  {
271  if(!(32 <= c && c <= 126))
272  c = 63;
273 
274  c -= 32;
275  Width += FontInfo[c].Width + FontInfo[c].Spacing;
276  }
277 
278  Width -= FontInfo[c].Spacing;
279  return Width;
280 }
281 
282 
291 void DrawText(unsigned char *Image, int Width, int Height,
292  int x0, int y0, unsigned char Value, const char *Text)
293 {
294  int c;
295 
296  if(!Text || !*Text)
297  return;
298 
299  while((c = *(Text++)))
300  {
301  if(!(32 <= c && c <= 126))
302  c = 63;
303 
304  c -= 32;
305  DrawGlyph(Image, Width, Height,
306  FontBitmapData + FontInfo[c].Offset,
307  FontInfo[c].Width, FontInfo[c].Height,
308  x0, y0 + FontInfo[c].YShift, Value);
309 
310  x0 += FontInfo[c].Width + FontInfo[c].Spacing;
311  }
312 }
313