Handle illegal \a tags like VSFilter
[libass.git] / libass / ass_bitmap.c
blob1ae2d7c3e7a23c16ecd34fe1d42022843ed3e6e1
1 /*
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
6 * libass is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * libass is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with libass; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include <assert.h>
25 #include <ft2build.h>
26 #include FT_GLYPH_H
28 #include "ass_utils.h"
29 #include "ass_bitmap.h"
31 struct ass_synth_priv {
32 int tmp_w, tmp_h;
33 unsigned short *tmp;
35 int g_r;
36 int g_w;
38 unsigned *g;
39 unsigned *gt2;
41 double radius;
44 static const unsigned int maxcolor = 255;
45 static const unsigned base = 256;
47 static int generate_tables(ASS_SynthPriv *priv, double radius)
49 double A = log(1.0 / base) / (radius * radius * 2);
50 int mx, i;
51 double volume_diff, volume_factor = 0;
52 unsigned volume;
54 if (priv->radius == radius)
55 return 0;
56 else
57 priv->radius = radius;
59 priv->g_r = ceil(radius);
60 priv->g_w = 2 * priv->g_r + 1;
62 if (priv->g_r) {
63 priv->g = realloc(priv->g, priv->g_w * sizeof(unsigned));
64 priv->gt2 = realloc(priv->gt2, 256 * priv->g_w * sizeof(unsigned));
65 if (priv->g == NULL || priv->gt2 == NULL) {
66 return -1;
70 if (priv->g_r) {
71 // gaussian curve with volume = 256
72 for (volume_diff = 10000000; volume_diff > 0.0000001;
73 volume_diff *= 0.5) {
74 volume_factor += volume_diff;
75 volume = 0;
76 for (i = 0; i < priv->g_w; ++i) {
77 priv->g[i] =
78 (unsigned) (exp(A * (i - priv->g_r) * (i - priv->g_r)) *
79 volume_factor + .5);
80 volume += priv->g[i];
82 if (volume > 256)
83 volume_factor -= volume_diff;
85 volume = 0;
86 for (i = 0; i < priv->g_w; ++i) {
87 priv->g[i] =
88 (unsigned) (exp(A * (i - priv->g_r) * (i - priv->g_r)) *
89 volume_factor + .5);
90 volume += priv->g[i];
93 // gauss table:
94 for (mx = 0; mx < priv->g_w; mx++) {
95 for (i = 0; i < 256; i++) {
96 priv->gt2[mx + i * priv->g_w] = i * priv->g[mx];
101 return 0;
104 static void resize_tmp(ASS_SynthPriv *priv, int w, int h)
106 if (priv->tmp_w >= w && priv->tmp_h >= h)
107 return;
108 if (priv->tmp_w == 0)
109 priv->tmp_w = 64;
110 if (priv->tmp_h == 0)
111 priv->tmp_h = 64;
112 while (priv->tmp_w < w)
113 priv->tmp_w *= 2;
114 while (priv->tmp_h < h)
115 priv->tmp_h *= 2;
116 if (priv->tmp)
117 free(priv->tmp);
118 priv->tmp = malloc((priv->tmp_w + 1) * priv->tmp_h * sizeof(short));
121 ASS_SynthPriv *ass_synth_init(double radius)
123 ASS_SynthPriv *priv = calloc(1, sizeof(ASS_SynthPriv));
124 generate_tables(priv, radius);
125 return priv;
128 void ass_synth_done(ASS_SynthPriv *priv)
130 if (priv->tmp)
131 free(priv->tmp);
132 if (priv->g)
133 free(priv->g);
134 if (priv->gt2)
135 free(priv->gt2);
136 free(priv);
139 static Bitmap *alloc_bitmap(int w, int h)
141 Bitmap *bm;
142 bm = calloc(1, sizeof(Bitmap));
143 bm->buffer = malloc(w * h);
144 bm->w = w;
145 bm->h = h;
146 bm->left = bm->top = 0;
147 return bm;
150 void ass_free_bitmap(Bitmap *bm)
152 if (bm) {
153 if (bm->buffer)
154 free(bm->buffer);
155 free(bm);
159 static Bitmap *copy_bitmap(const Bitmap *src)
161 Bitmap *dst = alloc_bitmap(src->w, src->h);
162 dst->left = src->left;
163 dst->top = src->top;
164 memcpy(dst->buffer, src->buffer, src->w * src->h);
165 return dst;
168 static int check_glyph_area(ASS_Library *library, FT_Glyph glyph)
170 FT_BBox bbox;
171 long long dx, dy;
172 FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox);
173 dx = bbox.xMax - bbox.xMin;
174 dy = bbox.yMax - bbox.yMin;
175 if (dx * dy > 8000000) {
176 ass_msg(library, MSGL_WARN, "Glyph bounding box too large: %dx%dpx",
177 (int) dx, (int) dy);
178 return 1;
179 } else
180 return 0;
183 static Bitmap *glyph_to_bitmap_internal(ASS_Library *library,
184 FT_Glyph glyph, int bord)
186 FT_BitmapGlyph bg;
187 FT_Bitmap *bit;
188 Bitmap *bm;
189 int w, h;
190 unsigned char *src;
191 unsigned char *dst;
192 int i;
193 int error;
195 if (check_glyph_area(library, glyph))
196 return 0;
197 error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 0);
198 if (error) {
199 ass_msg(library, MSGL_WARN, "FT_Glyph_To_Bitmap error %d",
200 error);
201 return 0;
204 bg = (FT_BitmapGlyph) glyph;
205 bit = &(bg->bitmap);
206 if (bit->pixel_mode != FT_PIXEL_MODE_GRAY) {
207 ass_msg(library, MSGL_WARN, "Unsupported pixel mode: %d",
208 (int) (bit->pixel_mode));
209 FT_Done_Glyph(glyph);
210 return 0;
213 w = bit->width;
214 h = bit->rows;
215 bm = alloc_bitmap(w + 2 * bord, h + 2 * bord);
216 memset(bm->buffer, 0, bm->w * bm->h);
217 bm->left = bg->left - bord;
218 bm->top = -bg->top - bord;
220 src = bit->buffer;
221 dst = bm->buffer + bord + bm->w * bord;
222 for (i = 0; i < h; ++i) {
223 memcpy(dst, src, w);
224 src += bit->pitch;
225 dst += bm->w;
228 FT_Done_Glyph(glyph);
229 return bm;
233 * \brief fix outline bitmap and generate shadow bitmap
234 * Two things are done here:
235 * 1. Glyph bitmap is subtracted from outline bitmap. This way looks much better in some cases.
236 * 2. Shadow bitmap is created as a sum of glyph and outline bitmaps.
238 static Bitmap *fix_outline_and_shadow(Bitmap *bm_g, Bitmap *bm_o)
240 int x, y;
241 const int l = bm_o->left > bm_g->left ? bm_o->left : bm_g->left;
242 const int t = bm_o->top > bm_g->top ? bm_o->top : bm_g->top;
243 const int r =
244 bm_o->left + bm_o->w <
245 bm_g->left + bm_g->w ? bm_o->left + bm_o->w : bm_g->left + bm_g->w;
246 const int b =
247 bm_o->top + bm_o->h <
248 bm_g->top + bm_g->h ? bm_o->top + bm_o->h : bm_g->top + bm_g->h;
250 Bitmap *bm_s = copy_bitmap(bm_o);
252 unsigned char *g =
253 bm_g->buffer + (t - bm_g->top) * bm_g->w + (l - bm_g->left);
254 unsigned char *o =
255 bm_o->buffer + (t - bm_o->top) * bm_o->w + (l - bm_o->left);
256 unsigned char *s =
257 bm_s->buffer + (t - bm_s->top) * bm_s->w + (l - bm_s->left);
259 for (y = 0; y < b - t; ++y) {
260 for (x = 0; x < r - l; ++x) {
261 unsigned char c_g, c_o;
262 c_g = g[x];
263 c_o = o[x];
264 o[x] = (c_o > c_g) ? c_o - (c_g / 2) : 0;
265 s[x] = (c_o < 0xFF - c_g) ? c_o + c_g : 0xFF;
267 g += bm_g->w;
268 o += bm_o->w;
269 s += bm_s->w;
272 assert(bm_s);
273 return bm_s;
277 * \brief Shift a bitmap by the fraction of a pixel in x and y direction
278 * expressed in 26.6 fixed point
280 static void shift_bitmap(unsigned char *buf, int w, int h, int shift_x,
281 int shift_y)
283 int x, y, b;
285 // Shift in x direction
286 if (shift_x > 0) {
287 for (y = 0; y < h; y++) {
288 for (x = w - 1; x > 0; x--) {
289 b = (buf[x + y * w - 1] * shift_x) >> 6;
290 buf[x + y * w - 1] -= b;
291 buf[x + y * w] += b;
294 } else if (shift_x < 0) {
295 shift_x = -shift_x;
296 for (y = 0; y < h; y++) {
297 for (x = 0; x < w - 1; x++) {
298 b = (buf[x + y * w + 1] * shift_x) >> 6;
299 buf[x + y * w + 1] -= b;
300 buf[x + y * w] += b;
305 // Shift in y direction
306 if (shift_y > 0) {
307 for (x = 0; x < w; x++) {
308 for (y = h - 1; y > 0; y--) {
309 b = (buf[x + (y - 1) * w] * shift_y) >> 6;
310 buf[x + (y - 1) * w] -= b;
311 buf[x + y * w] += b;
314 } else if (shift_y < 0) {
315 shift_y = -shift_y;
316 for (x = 0; x < w; x++) {
317 for (y = 0; y < h - 1; y++) {
318 b = (buf[x + (y + 1) * w] * shift_y) >> 6;
319 buf[x + (y + 1) * w] -= b;
320 buf[x + y * w] += b;
327 * Gaussian blur. An fast pure C implementation from MPlayer.
329 static void ass_gauss_blur(unsigned char *buffer, unsigned short *tmp2,
330 int width, int height, int stride, int *m2,
331 int r, int mwidth)
334 int x, y;
336 unsigned char *s = buffer;
337 unsigned short *t = tmp2 + 1;
338 for (y = 0; y < height; y++) {
339 memset(t - 1, 0, (width + 1) * sizeof(short));
341 for (x = 0; x < r; x++) {
342 const int src = s[x];
343 if (src) {
344 register unsigned short *dstp = t + x - r;
345 int mx;
346 unsigned *m3 = (unsigned *) (m2 + src * mwidth);
347 for (mx = r - x; mx < mwidth; mx++) {
348 dstp[mx] += m3[mx];
353 for (; x < width - r; x++) {
354 const int src = s[x];
355 if (src) {
356 register unsigned short *dstp = t + x - r;
357 int mx;
358 unsigned *m3 = (unsigned *) (m2 + src * mwidth);
359 for (mx = 0; mx < mwidth; mx++) {
360 dstp[mx] += m3[mx];
365 for (; x < width; x++) {
366 const int src = s[x];
367 if (src) {
368 register unsigned short *dstp = t + x - r;
369 int mx;
370 const int x2 = r + width - x;
371 unsigned *m3 = (unsigned *) (m2 + src * mwidth);
372 for (mx = 0; mx < x2; mx++) {
373 dstp[mx] += m3[mx];
378 s += stride;
379 t += width + 1;
382 t = tmp2;
383 for (x = 0; x < width; x++) {
384 for (y = 0; y < r; y++) {
385 unsigned short *srcp = t + y * (width + 1) + 1;
386 int src = *srcp;
387 if (src) {
388 register unsigned short *dstp = srcp - 1 + width + 1;
389 const int src2 = (src + 128) >> 8;
390 unsigned *m3 = (unsigned *) (m2 + src2 * mwidth);
392 int mx;
393 *srcp = 128;
394 for (mx = r - 1; mx < mwidth; mx++) {
395 *dstp += m3[mx];
396 dstp += width + 1;
400 for (; y < height - r; y++) {
401 unsigned short *srcp = t + y * (width + 1) + 1;
402 int src = *srcp;
403 if (src) {
404 register unsigned short *dstp = srcp - 1 - r * (width + 1);
405 const int src2 = (src + 128) >> 8;
406 unsigned *m3 = (unsigned *) (m2 + src2 * mwidth);
408 int mx;
409 *srcp = 128;
410 for (mx = 0; mx < mwidth; mx++) {
411 *dstp += m3[mx];
412 dstp += width + 1;
416 for (; y < height; y++) {
417 unsigned short *srcp = t + y * (width + 1) + 1;
418 int src = *srcp;
419 if (src) {
420 const int y2 = r + height - y;
421 register unsigned short *dstp = srcp - 1 - r * (width + 1);
422 const int src2 = (src + 128) >> 8;
423 unsigned *m3 = (unsigned *) (m2 + src2 * mwidth);
425 int mx;
426 *srcp = 128;
427 for (mx = 0; mx < y2; mx++) {
428 *dstp += m3[mx];
429 dstp += width + 1;
433 t++;
436 t = tmp2;
437 s = buffer;
438 for (y = 0; y < height; y++) {
439 for (x = 0; x < width; x++) {
440 s[x] = t[x] >> 8;
442 s += stride;
443 t += width + 1;
448 * \brief Blur with [[1,2,1]. [2,4,2], [1,2,1]] kernel
449 * This blur is the same as the one employed by vsfilter.
451 static void be_blur(unsigned char *buf, int w, int h)
453 unsigned int x, y;
454 unsigned int old_sum, new_sum;
456 for (y = 0; y < h; y++) {
457 old_sum = 2 * buf[y * w];
458 for (x = 0; x < w - 1; x++) {
459 new_sum = buf[y * w + x] + buf[y * w + x + 1];
460 buf[y * w + x] = (old_sum + new_sum) >> 2;
461 old_sum = new_sum;
465 for (x = 0; x < w; x++) {
466 old_sum = 2 * buf[x];
467 for (y = 0; y < h - 1; y++) {
468 new_sum = buf[y * w + x] + buf[(y + 1) * w + x];
469 buf[y * w + x] = (old_sum + new_sum) >> 2;
470 old_sum = new_sum;
475 int glyph_to_bitmap(ASS_Library *library, ASS_SynthPriv *priv_blur,
476 FT_Glyph glyph, FT_Glyph outline_glyph,
477 Bitmap **bm_g, Bitmap **bm_o, Bitmap **bm_s,
478 int be, double blur_radius, FT_Vector shadow_offset,
479 int border_style)
481 blur_radius *= 2;
482 int bbord = be > 0 ? sqrt(2 * be) : 0;
483 int gbord = blur_radius > 0.0 ? blur_radius + 1 : 0;
484 int bord = FFMAX(bbord, gbord);
485 if (bord == 0 && (shadow_offset.x || shadow_offset.y))
486 bord = 1;
488 assert(bm_g && bm_o && bm_s);
490 *bm_g = *bm_o = *bm_s = 0;
492 if (glyph)
493 *bm_g = glyph_to_bitmap_internal(library, glyph, bord);
494 if (!*bm_g)
495 return 1;
497 if (outline_glyph) {
498 *bm_o = glyph_to_bitmap_internal(library, outline_glyph, bord);
499 if (!*bm_o) {
500 return 1;
504 // Apply box blur (multiple passes, if requested)
505 while (be--) {
506 if (*bm_o)
507 be_blur((*bm_o)->buffer, (*bm_o)->w, (*bm_o)->h);
508 else
509 be_blur((*bm_g)->buffer, (*bm_g)->w, (*bm_g)->h);
512 // Apply gaussian blur
513 if (blur_radius > 0.0) {
514 if (*bm_o)
515 resize_tmp(priv_blur, (*bm_o)->w, (*bm_o)->h);
516 else
517 resize_tmp(priv_blur, (*bm_g)->w, (*bm_g)->h);
518 generate_tables(priv_blur, blur_radius);
519 if (*bm_o)
520 ass_gauss_blur((*bm_o)->buffer, priv_blur->tmp,
521 (*bm_o)->w, (*bm_o)->h, (*bm_o)->w,
522 (int *) priv_blur->gt2, priv_blur->g_r,
523 priv_blur->g_w);
524 else
525 ass_gauss_blur((*bm_g)->buffer, priv_blur->tmp,
526 (*bm_g)->w, (*bm_g)->h, (*bm_g)->w,
527 (int *) priv_blur->gt2, priv_blur->g_r,
528 priv_blur->g_w);
531 if (*bm_o && border_style == 3)
532 *bm_s = copy_bitmap(*bm_o);
533 else if (*bm_o)
534 *bm_s = fix_outline_and_shadow(*bm_g, *bm_o);
535 else
536 *bm_s = copy_bitmap(*bm_g);
538 shift_bitmap((*bm_s)->buffer, (*bm_s)->w,(*bm_s)->h,
539 shadow_offset.x, shadow_offset.y);
541 assert(bm_s);
542 return 0;