Commit | Line | Data |
---|---|---|
26b4bb70 PG |
1 | /* |
2 | * imlib2 based hook | |
3 | * Copyright (c) 2002 Philip Gladstone | |
4 | * | |
5 | * This module implements a text overlay for a video image. Currently it | |
6 | * supports a fixed overlay or reading the text from a file. The string | |
7 | * is passed through strftime so that it is easy to imprint the date and | |
8 | * time onto the image. | |
9 | * | |
10 | * Options: | |
11 | * | |
12 | * -c <color> The color of the text | |
13 | * -F <fontname> The font face and size | |
14 | * -t <text> The text | |
15 | * -f <filename> The filename to read text from | |
16 | * -x <num> X coordinate to start text | |
17 | * -y <num> Y coordinate to start text | |
18 | * | |
19 | * This module is very much intended as an example of what could be done. | |
20 | * For example, you could overlay an image (even semi-transparent) like | |
21 | * TV stations do. You can manipulate the image using imlib2 functions | |
22 | * in any way. | |
23 | * | |
24 | * One caution is that this is an expensive process -- in particular the | |
25 | * conversion of the image into RGB and back is time consuming. For some | |
26 | * special cases -- e.g. painting black text -- it would be faster to paint | |
27 | * the text into a bitmap and then combine it directly into the YUV | |
28 | * image. However, this code is fast enough to handle 10 fps of 320x240 on a | |
29 | * 900MHz Duron in maybe 15% of the CPU. | |
30 | * | |
31 | * This library is free software; you can redistribute it and/or | |
32 | * modify it under the terms of the GNU Lesser General Public | |
33 | * License as published by the Free Software Foundation; either | |
34 | * version 2 of the License, or (at your option) any later version. | |
35 | * | |
36 | * This library is distributed in the hope that it will be useful, | |
37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
39 | * Lesser General Public License for more details. | |
40 | * | |
41 | * You should have received a copy of the GNU Lesser General Public | |
42 | * License along with this library; if not, write to the Free Software | |
43 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
44 | */ | |
45 | #include <stdio.h> | |
46 | #include <stdlib.h> | |
47 | #include <fcntl.h> | |
26b4bb70 PG |
48 | #include <stdarg.h> |
49 | #include <string.h> | |
50 | #include <unistd.h> | |
51 | #include <sys/time.h> | |
52 | #include <X11/Xlib.h> | |
53 | #include <Imlib2.h> | |
54 | ||
55 | #include "framehook.h" | |
56 | ||
57 | typedef struct { | |
58 | int dummy; | |
59 | Imlib_Font fn; | |
60 | char *text; | |
61 | char *file; | |
62 | int r, g, b; | |
63 | int x; | |
64 | int y; | |
65 | struct _CachedImage *cache; | |
66 | } ContextInfo; | |
67 | ||
68 | typedef struct _CachedImage { | |
69 | struct _CachedImage *next; | |
70 | Imlib_Image image; | |
71 | int width; | |
72 | int height; | |
73 | } CachedImage; | |
74 | ||
75 | ||
76 | int Configure(void **ctxp, int argc, char *argv[]) | |
77 | { | |
78 | int c; | |
79 | ContextInfo *ci; | |
80 | char *font = "LucidaSansDemiBold/16"; | |
81 | char *fp = getenv("FONTPATH"); | |
82 | char *color = 0; | |
83 | FILE *f; | |
84 | ||
85 | *ctxp = av_mallocz(sizeof(ContextInfo)); | |
86 | ci = (ContextInfo *) *ctxp; | |
87 | ||
88 | optind = 0; | |
89 | ||
90 | if (fp) | |
91 | imlib_add_path_to_font_path(fp); | |
92 | ||
93 | while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) { | |
94 | switch (c) { | |
95 | case 'c': | |
96 | color = optarg; | |
97 | break; | |
98 | case 'F': | |
99 | font = optarg; | |
100 | break; | |
101 | case 't': | |
102 | ci->text = strdup(optarg); | |
103 | break; | |
104 | case 'f': | |
105 | ci->file = strdup(optarg); | |
106 | break; | |
107 | case 'x': | |
108 | ci->x = atoi(optarg); | |
109 | break; | |
110 | case 'y': | |
111 | ci->y = atoi(optarg); | |
112 | break; | |
113 | case '?': | |
114 | fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]); | |
115 | return -1; | |
116 | } | |
117 | } | |
118 | ||
119 | ci->fn = imlib_load_font(font); | |
120 | if (!ci->fn) { | |
121 | fprintf(stderr, "Failed to load font '%s'\n", font); | |
122 | return -1; | |
123 | } | |
124 | imlib_context_set_font(ci->fn); | |
125 | imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT); | |
126 | ||
127 | if (color) { | |
128 | char buff[256]; | |
129 | int done = 0; | |
130 | ||
131 | f = fopen("/usr/lib/X11/rgb.txt", "r"); | |
132 | if (!f) { | |
133 | fprintf(stderr, "Failed to find rgb.txt\n"); | |
134 | return -1; | |
135 | } | |
136 | while (fgets(buff, sizeof(buff), f)) { | |
137 | int r, g, b; | |
138 | char colname[80]; | |
139 | ||
140 | if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 && | |
141 | strcasecmp(colname, color) == 0) { | |
142 | ci->r = r; | |
143 | ci->g = g; | |
144 | ci->b = b; | |
145 | /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */ | |
146 | done = 1; | |
147 | break; | |
148 | } | |
149 | } | |
150 | fclose(f); | |
151 | if (!done) { | |
152 | fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color); | |
153 | return -1; | |
154 | } | |
155 | } | |
156 | imlib_context_set_color(ci->r, ci->g, ci->b, 255); | |
157 | return 0; | |
158 | } | |
159 | ||
160 | static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height) | |
161 | { | |
162 | CachedImage *cache; | |
163 | ||
164 | for (cache = ci->cache; cache; cache = cache->next) { | |
165 | if (width == cache->width && height == cache->height) | |
166 | return cache->image; | |
167 | } | |
168 | ||
169 | return NULL; | |
170 | } | |
171 | ||
172 | static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height) | |
173 | { | |
174 | CachedImage *cache = av_mallocz(sizeof(*cache)); | |
175 | ||
176 | cache->image = image; | |
177 | cache->width = width; | |
178 | cache->height = height; | |
179 | cache->next = ci->cache; | |
180 | ci->cache = cache; | |
181 | } | |
182 | ||
183 | void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, INT64 pts) | |
184 | { | |
185 | ContextInfo *ci = (ContextInfo *) ctx; | |
186 | AVPicture picture1; | |
187 | Imlib_Image image; | |
188 | DATA32 *data; | |
189 | ||
190 | image = get_cached_image(ci, width, height); | |
191 | ||
192 | if (!image) { | |
193 | image = imlib_create_image(width, height); | |
194 | put_cached_image(ci, image, width, height); | |
195 | } | |
196 | ||
197 | imlib_context_set_image(image); | |
198 | data = imlib_image_get_data(); | |
199 | ||
200 | if (pix_fmt != PIX_FMT_BGRA32) { | |
201 | avpicture_fill(&picture1, (UINT8 *) data, PIX_FMT_BGRA32, width, height); | |
202 | if (img_convert(&picture1, PIX_FMT_BGRA32, | |
203 | picture, pix_fmt, width, height) < 0) { | |
204 | goto done; | |
205 | } | |
206 | } else { | |
207 | av_abort(); | |
208 | } | |
209 | ||
210 | imlib_image_set_has_alpha(0); | |
211 | ||
212 | { | |
213 | int wid, hig, h_a, v_a; | |
214 | char buff[1000]; | |
215 | char tbuff[1000]; | |
216 | char *tbp = ci->text; | |
217 | time_t now = time(0); | |
218 | char *p, *q; | |
219 | int x, y; | |
220 | ||
221 | if (ci->file) { | |
222 | int fd = open(ci->file, O_RDONLY); | |
223 | ||
224 | if (fd < 0) { | |
225 | tbp = "[File not found]"; | |
226 | } else { | |
227 | int l = read(fd, tbuff, sizeof(tbuff) - 1); | |
228 | ||
229 | if (l >= 0) { | |
230 | tbuff[l] = 0; | |
231 | tbp = tbuff; | |
232 | } else { | |
233 | tbp = "[I/O Error]"; | |
234 | } | |
235 | close(fd); | |
236 | } | |
237 | } | |
238 | ||
239 | strftime(buff, sizeof(buff), tbp, localtime(&now)); | |
240 | ||
241 | x = ci->x; | |
242 | y = ci->y; | |
243 | ||
244 | for (p = buff; p; p = q) { | |
245 | q = strchr(p, '\n'); | |
246 | if (q) | |
247 | *q++ = 0; | |
248 | ||
249 | imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a); | |
250 | y += v_a; | |
251 | } | |
252 | } | |
253 | ||
254 | if (pix_fmt != PIX_FMT_BGRA32) { | |
255 | if (img_convert(picture, pix_fmt, | |
256 | &picture1, PIX_FMT_BGRA32, width, height) < 0) { | |
257 | } | |
258 | } | |
259 | ||
260 | done: | |
261 | ; | |
262 | } | |
263 | ||
264 | /* To ensure correct typing */ | |
265 | FrameHookConfigureFn ConfigureFn = Configure; | |
266 | FrameHookProcessFn ProcessFn = Process; |