use av_strdup()
[libav.git] / vhook / imlib2.c
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>
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 void Release(void *ctx)
76 {
77 ContextInfo *ci;
78 ci = (ContextInfo *) ctx;
79
80 if (ci->cache) {
81 imlib_context_set_image(ci->cache->image);
82 imlib_free_image();
83 av_free(ci->cache);
84 }
85 if (ctx)
86 av_free(ctx);
87 }
88
89 int Configure(void **ctxp, int argc, char *argv[])
90 {
91 int c;
92 ContextInfo *ci;
93 char *font = "LucidaSansDemiBold/16";
94 char *fp = getenv("FONTPATH");
95 char *color = 0;
96 FILE *f;
97
98 *ctxp = av_mallocz(sizeof(ContextInfo));
99 ci = (ContextInfo *) *ctxp;
100
101 optind = 0;
102
103 if (fp)
104 imlib_add_path_to_font_path(fp);
105
106 while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) {
107 switch (c) {
108 case 'c':
109 color = optarg;
110 break;
111 case 'F':
112 font = optarg;
113 break;
114 case 't':
115 ci->text = av_strdup(optarg);
116 break;
117 case 'f':
118 ci->file = av_strdup(optarg);
119 break;
120 case 'x':
121 ci->x = atoi(optarg);
122 break;
123 case 'y':
124 ci->y = atoi(optarg);
125 break;
126 case '?':
127 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
128 return -1;
129 }
130 }
131
132 ci->fn = imlib_load_font(font);
133 if (!ci->fn) {
134 fprintf(stderr, "Failed to load font '%s'\n", font);
135 return -1;
136 }
137 imlib_context_set_font(ci->fn);
138 imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
139
140 if (color) {
141 char buff[256];
142 int done = 0;
143
144 f = fopen("/usr/lib/X11/rgb.txt", "r");
145 if (!f) {
146 fprintf(stderr, "Failed to find rgb.txt\n");
147 return -1;
148 }
149 while (fgets(buff, sizeof(buff), f)) {
150 int r, g, b;
151 char colname[80];
152
153 if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
154 strcasecmp(colname, color) == 0) {
155 ci->r = r;
156 ci->g = g;
157 ci->b = b;
158 /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
159 done = 1;
160 break;
161 }
162 }
163 fclose(f);
164 if (!done) {
165 fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
166 return -1;
167 }
168 }
169 imlib_context_set_color(ci->r, ci->g, ci->b, 255);
170 return 0;
171 }
172
173 static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
174 {
175 CachedImage *cache;
176
177 for (cache = ci->cache; cache; cache = cache->next) {
178 if (width == cache->width && height == cache->height)
179 return cache->image;
180 }
181
182 return NULL;
183 }
184
185 static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
186 {
187 CachedImage *cache = av_mallocz(sizeof(*cache));
188
189 cache->image = image;
190 cache->width = width;
191 cache->height = height;
192 cache->next = ci->cache;
193 ci->cache = cache;
194 }
195
196 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, INT64 pts)
197 {
198 ContextInfo *ci = (ContextInfo *) ctx;
199 AVPicture picture1;
200 Imlib_Image image;
201 DATA32 *data;
202
203 image = get_cached_image(ci, width, height);
204
205 if (!image) {
206 image = imlib_create_image(width, height);
207 put_cached_image(ci, image, width, height);
208 }
209
210 imlib_context_set_image(image);
211 data = imlib_image_get_data();
212
213 if (pix_fmt != PIX_FMT_RGBA32) {
214 avpicture_fill(&picture1, (UINT8 *) data, PIX_FMT_RGBA32, width, height);
215 if (img_convert(&picture1, PIX_FMT_RGBA32,
216 picture, pix_fmt, width, height) < 0) {
217 goto done;
218 }
219 } else {
220 av_abort();
221 }
222
223 imlib_image_set_has_alpha(0);
224
225 {
226 int wid, hig, h_a, v_a;
227 char buff[1000];
228 char tbuff[1000];
229 char *tbp = ci->text;
230 time_t now = time(0);
231 char *p, *q;
232 int x, y;
233
234 if (ci->file) {
235 int fd = open(ci->file, O_RDONLY);
236
237 if (fd < 0) {
238 tbp = "[File not found]";
239 } else {
240 int l = read(fd, tbuff, sizeof(tbuff) - 1);
241
242 if (l >= 0) {
243 tbuff[l] = 0;
244 tbp = tbuff;
245 } else {
246 tbp = "[I/O Error]";
247 }
248 close(fd);
249 }
250 }
251
252 strftime(buff, sizeof(buff), tbp, localtime(&now));
253
254 x = ci->x;
255 y = ci->y;
256
257 for (p = buff; p; p = q) {
258 q = strchr(p, '\n');
259 if (q)
260 *q++ = 0;
261
262 imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a);
263 y += v_a;
264 }
265 }
266
267 if (pix_fmt != PIX_FMT_RGBA32) {
268 if (img_convert(picture, pix_fmt,
269 &picture1, PIX_FMT_RGBA32, width, height) < 0) {
270 }
271 }
272
273 done:
274 ;
275 }
276