3 * Copyright (c) 2002 Philip Gladstone
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
12 * -c <color> The color of the text
13 * -F <fontname> The font face and size
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
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
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.
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.
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.
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
55 #include "framehook.h"
65 struct _CachedImage
*cache
;
68 typedef struct _CachedImage
{
69 struct _CachedImage
*next
;
76 int Configure(void **ctxp
, int argc
, char *argv
[])
80 char *font
= "LucidaSansDemiBold/16";
81 char *fp
= getenv("FONTPATH");
85 *ctxp
= av_mallocz(sizeof(ContextInfo
));
86 ci
= (ContextInfo
*) *ctxp
;
91 imlib_add_path_to_font_path(fp
);
93 while ((c
= getopt(argc
, argv
, "c:f:F:t:x:y:")) > 0) {
102 ci
->text
= strdup(optarg
);
105 ci
->file
= strdup(optarg
);
108 ci
->x
= atoi(optarg
);
111 ci
->y
= atoi(optarg
);
114 fprintf(stderr
, "Unrecognized argument '%s'\n", argv
[optind
]);
119 ci
->fn
= imlib_load_font(font
);
121 fprintf(stderr
, "Failed to load font '%s'\n", font
);
124 imlib_context_set_font(ci
->fn
);
125 imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT
);
131 f
= fopen("/usr/lib/X11/rgb.txt", "r");
133 fprintf(stderr
, "Failed to find rgb.txt\n");
136 while (fgets(buff
, sizeof(buff
), f
)) {
140 if (sscanf(buff
, "%d %d %d %64s", &r
, &g
, &b
, colname
) == 4 &&
141 strcasecmp(colname
, color
) == 0) {
145 /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
152 fprintf(stderr
, "Unable to find color '%s' in rgb.txt\n", color
);
156 imlib_context_set_color(ci
->r
, ci
->g
, ci
->b
, 255);
160 static Imlib_Image
get_cached_image(ContextInfo
*ci
, int width
, int height
)
164 for (cache
= ci
->cache
; cache
; cache
= cache
->next
) {
165 if (width
== cache
->width
&& height
== cache
->height
)
172 static void put_cached_image(ContextInfo
*ci
, Imlib_Image image
, int width
, int height
)
174 CachedImage
*cache
= av_mallocz(sizeof(*cache
));
176 cache
->image
= image
;
177 cache
->width
= width
;
178 cache
->height
= height
;
179 cache
->next
= ci
->cache
;
183 void Process(void *ctx
, AVPicture
*picture
, enum PixelFormat pix_fmt
, int width
, int height
, INT64 pts
)
185 ContextInfo
*ci
= (ContextInfo
*) ctx
;
190 image
= get_cached_image(ci
, width
, height
);
193 image
= imlib_create_image(width
, height
);
194 put_cached_image(ci
, image
, width
, height
);
197 imlib_context_set_image(image
);
198 data
= imlib_image_get_data();
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) {
210 imlib_image_set_has_alpha(0);
213 int wid
, hig
, h_a
, v_a
;
216 char *tbp
= ci
->text
;
217 time_t now
= time(0);
222 int fd
= open(ci
->file
, O_RDONLY
);
225 tbp
= "[File not found]";
227 int l
= read(fd
, tbuff
, sizeof(tbuff
) - 1);
239 strftime(buff
, sizeof(buff
), tbp
, localtime(&now
));
244 for (p
= buff
; p
; p
= q
) {
249 imlib_text_draw_with_return_metrics(x
, y
, p
, &wid
, &hig
, &h_a
, &v_a
);
254 if (pix_fmt
!= PIX_FMT_BGRA32
) {
255 if (img_convert(picture
, pix_fmt
,
256 &picture1
, PIX_FMT_BGRA32
, width
, height
) < 0) {
264 /* To ensure correct typing */
265 FrameHookConfigureFn ConfigureFn
= Configure
;
266 FrameHookProcessFn ProcessFn
= Process
;