x264 version 0.83 is required.
[mplayer/glamo.git] / libass / ass_bitmap.c
blobc7c039ddd5eb2ce10c936dca3dcf1f5478ee41e0
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
235 * The glyph bitmap is subtracted from outline bitmap. This way looks much
236 * better in some cases.
238 static void fix_outline(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 unsigned char *g =
251 bm_g->buffer + (t - bm_g->top) * bm_g->w + (l - bm_g->left);
252 unsigned char *o =
253 bm_o->buffer + (t - bm_o->top) * bm_o->w + (l - bm_o->left);
255 for (y = 0; y < b - t; ++y) {
256 for (x = 0; x < r - l; ++x) {
257 unsigned char c_g, c_o;
258 c_g = g[x];
259 c_o = o[x];
260 o[x] = (c_o > c_g) ? c_o - (c_g / 2) : 0;
262 g += bm_g->w;
263 o += bm_o->w;
268 * \brief Shift a bitmap by the fraction of a pixel in x and y direction
269 * expressed in 26.6 fixed point
271 static void shift_bitmap(unsigned char *buf, int w, int h, int shift_x,
272 int shift_y)
274 int x, y, b;
276 // Shift in x direction
277 if (shift_x > 0) {
278 for (y = 0; y < h; y++) {
279 for (x = w - 1; x > 0; x--) {
280 b = (buf[x + y * w - 1] * shift_x) >> 6;
281 buf[x + y * w - 1] -= b;
282 buf[x + y * w] += b;
285 } else if (shift_x < 0) {
286 shift_x = -shift_x;
287 for (y = 0; y < h; y++) {
288 for (x = 0; x < w - 1; x++) {
289 b = (buf[x + y * w + 1] * shift_x) >> 6;
290 buf[x + y * w + 1] -= b;
291 buf[x + y * w] += b;
296 // Shift in y direction
297 if (shift_y > 0) {
298 for (x = 0; x < w; x++) {
299 for (y = h - 1; y > 0; y--) {
300 b = (buf[x + (y - 1) * w] * shift_y) >> 6;
301 buf[x + (y - 1) * w] -= b;
302 buf[x + y * w] += b;
305 } else if (shift_y < 0) {
306 shift_y = -shift_y;
307 for (x = 0; x < w; x++) {
308 for (y = 0; y < h - 1; y++) {
309 b = (buf[x + (y + 1) * w] * shift_y) >> 6;
310 buf[x + (y + 1) * w] -= b;
311 buf[x + y * w] += b;
318 * Gaussian blur. An fast pure C implementation from MPlayer.
320 static void ass_gauss_blur(unsigned char *buffer, unsigned short *tmp2,
321 int width, int height, int stride, int *m2,
322 int r, int mwidth)
325 int x, y;
327 unsigned char *s = buffer;
328 unsigned short *t = tmp2 + 1;
329 for (y = 0; y < height; y++) {
330 memset(t - 1, 0, (width + 1) * sizeof(short));
332 for (x = 0; x < r; x++) {
333 const int src = s[x];
334 if (src) {
335 register unsigned short *dstp = t + x - r;
336 int mx;
337 unsigned *m3 = (unsigned *) (m2 + src * mwidth);
338 for (mx = r - x; mx < mwidth; mx++) {
339 dstp[mx] += m3[mx];
344 for (; x < width - r; x++) {
345 const int src = s[x];
346 if (src) {
347 register unsigned short *dstp = t + x - r;
348 int mx;
349 unsigned *m3 = (unsigned *) (m2 + src * mwidth);
350 for (mx = 0; mx < mwidth; mx++) {
351 dstp[mx] += m3[mx];
356 for (; x < width; x++) {
357 const int src = s[x];
358 if (src) {
359 register unsigned short *dstp = t + x - r;
360 int mx;
361 const int x2 = r + width - x;
362 unsigned *m3 = (unsigned *) (m2 + src * mwidth);
363 for (mx = 0; mx < x2; mx++) {
364 dstp[mx] += m3[mx];
369 s += stride;
370 t += width + 1;
373 t = tmp2;
374 for (x = 0; x < width; x++) {
375 for (y = 0; y < r; y++) {
376 unsigned short *srcp = t + y * (width + 1) + 1;
377 int src = *srcp;
378 if (src) {
379 register unsigned short *dstp = srcp - 1 + width + 1;
380 const int src2 = (src + 128) >> 8;
381 unsigned *m3 = (unsigned *) (m2 + src2 * mwidth);
383 int mx;
384 *srcp = 128;
385 for (mx = r - 1; mx < mwidth; mx++) {
386 *dstp += m3[mx];
387 dstp += width + 1;
391 for (; y < height - r; y++) {
392 unsigned short *srcp = t + y * (width + 1) + 1;
393 int src = *srcp;
394 if (src) {
395 register unsigned short *dstp = srcp - 1 - r * (width + 1);
396 const int src2 = (src + 128) >> 8;
397 unsigned *m3 = (unsigned *) (m2 + src2 * mwidth);
399 int mx;
400 *srcp = 128;
401 for (mx = 0; mx < mwidth; mx++) {
402 *dstp += m3[mx];
403 dstp += width + 1;
407 for (; y < height; y++) {
408 unsigned short *srcp = t + y * (width + 1) + 1;
409 int src = *srcp;
410 if (src) {
411 const int y2 = r + height - y;
412 register unsigned short *dstp = srcp - 1 - r * (width + 1);
413 const int src2 = (src + 128) >> 8;
414 unsigned *m3 = (unsigned *) (m2 + src2 * mwidth);
416 int mx;
417 *srcp = 128;
418 for (mx = 0; mx < y2; mx++) {
419 *dstp += m3[mx];
420 dstp += width + 1;
424 t++;
427 t = tmp2;
428 s = buffer;
429 for (y = 0; y < height; y++) {
430 for (x = 0; x < width; x++) {
431 s[x] = t[x] >> 8;
433 s += stride;
434 t += width + 1;
439 * \brief Blur with [[1,2,1]. [2,4,2], [1,2,1]] kernel
440 * This blur is the same as the one employed by vsfilter.
442 static void be_blur(unsigned char *buf, int w, int h)
444 unsigned int x, y;
445 unsigned int old_sum, new_sum;
447 for (y = 0; y < h; y++) {
448 old_sum = 2 * buf[y * w];
449 for (x = 0; x < w - 1; x++) {
450 new_sum = buf[y * w + x] + buf[y * w + x + 1];
451 buf[y * w + x] = (old_sum + new_sum) >> 2;
452 old_sum = new_sum;
456 for (x = 0; x < w; x++) {
457 old_sum = 2 * buf[x];
458 for (y = 0; y < h - 1; y++) {
459 new_sum = buf[y * w + x] + buf[(y + 1) * w + x];
460 buf[y * w + x] = (old_sum + new_sum) >> 2;
461 old_sum = new_sum;
466 int glyph_to_bitmap(ASS_Library *library, ASS_SynthPriv *priv_blur,
467 FT_Glyph glyph, FT_Glyph outline_glyph,
468 Bitmap **bm_g, Bitmap **bm_o, Bitmap **bm_s,
469 int be, double blur_radius, FT_Vector shadow_offset,
470 int border_style)
472 blur_radius *= 2;
473 int bbord = be > 0 ? sqrt(2 * be) : 0;
474 int gbord = blur_radius > 0.0 ? blur_radius + 1 : 0;
475 int bord = FFMAX(bbord, gbord);
476 if (bord == 0 && (shadow_offset.x || shadow_offset.y))
477 bord = 1;
479 assert(bm_g && bm_o && bm_s);
481 *bm_g = *bm_o = *bm_s = 0;
483 if (glyph)
484 *bm_g = glyph_to_bitmap_internal(library, glyph, bord);
485 if (!*bm_g)
486 return 1;
488 if (outline_glyph) {
489 *bm_o = glyph_to_bitmap_internal(library, outline_glyph, bord);
490 if (!*bm_o) {
491 return 1;
495 // Apply box blur (multiple passes, if requested)
496 while (be--) {
497 if (*bm_o)
498 be_blur((*bm_o)->buffer, (*bm_o)->w, (*bm_o)->h);
499 else
500 be_blur((*bm_g)->buffer, (*bm_g)->w, (*bm_g)->h);
503 // Apply gaussian blur
504 if (blur_radius > 0.0) {
505 if (*bm_o)
506 resize_tmp(priv_blur, (*bm_o)->w, (*bm_o)->h);
507 else
508 resize_tmp(priv_blur, (*bm_g)->w, (*bm_g)->h);
509 generate_tables(priv_blur, blur_radius);
510 if (*bm_o)
511 ass_gauss_blur((*bm_o)->buffer, priv_blur->tmp,
512 (*bm_o)->w, (*bm_o)->h, (*bm_o)->w,
513 (int *) priv_blur->gt2, priv_blur->g_r,
514 priv_blur->g_w);
515 else
516 ass_gauss_blur((*bm_g)->buffer, priv_blur->tmp,
517 (*bm_g)->w, (*bm_g)->h, (*bm_g)->w,
518 (int *) priv_blur->gt2, priv_blur->g_r,
519 priv_blur->g_w);
522 // Create shadow and fix outline as needed
523 if (*bm_o && border_style != 3) {
524 *bm_s = copy_bitmap(*bm_o);
525 fix_outline(*bm_g, *bm_o);
526 } else if (*bm_o) {
527 *bm_s = copy_bitmap(*bm_o);
528 } else
529 *bm_s = copy_bitmap(*bm_g);
531 assert(bm_s);
533 shift_bitmap((*bm_s)->buffer, (*bm_s)->w,(*bm_s)->h,
534 shadow_offset.x, shadow_offset.y);
536 return 0;