2006-10-01 17:09:04 +00:00
|
|
|
// -*- c-basic-offset: 8; indent-tabs-mode: t -*-
|
|
|
|
// vim:ts=8:sw=8:noet:ai:
|
|
|
|
/*
|
2008-05-13 08:02:23 +00:00
|
|
|
* Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
|
|
|
|
*
|
2008-05-14 17:21:58 +00:00
|
|
|
* This file is part of libass.
|
2008-05-13 08:02:23 +00:00
|
|
|
*
|
2008-05-14 17:21:58 +00:00
|
|
|
* libass is free software; you can redistribute it and/or modify
|
2008-05-13 08:02:23 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2008-05-14 17:21:58 +00:00
|
|
|
* libass is distributed in the hope that it will be useful,
|
2008-05-13 08:02:23 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2008-05-14 17:21:58 +00:00
|
|
|
* with libass; if not, write to the Free Software Foundation, Inc.,
|
2008-05-13 08:02:23 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2006-10-01 17:09:04 +00:00
|
|
|
|
2006-09-16 13:08:17 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_GLYPH_H
|
|
|
|
|
2006-11-19 00:41:00 +00:00
|
|
|
#include "mputils.h"
|
2006-09-16 13:08:17 +00:00
|
|
|
#include "ass_bitmap.h"
|
|
|
|
|
2006-09-16 13:32:46 +00:00
|
|
|
struct ass_synth_priv_s {
|
|
|
|
int tmp_w, tmp_h;
|
|
|
|
unsigned short* tmp;
|
|
|
|
|
|
|
|
int g_r;
|
|
|
|
int g_w;
|
|
|
|
|
|
|
|
unsigned *g;
|
|
|
|
unsigned *gt2;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned int maxcolor = 255;
|
|
|
|
static const unsigned base = 256;
|
|
|
|
static const double blur_radius = 1.5;
|
|
|
|
|
|
|
|
static int generate_tables(ass_synth_priv_t* priv, double radius)
|
|
|
|
{
|
2006-09-23 17:54:24 +00:00
|
|
|
double A = log(1.0/base)/(radius*radius*2);
|
|
|
|
int mx, i;
|
|
|
|
double volume_diff, volume_factor = 0;
|
|
|
|
unsigned volume;
|
2006-09-16 13:32:46 +00:00
|
|
|
|
2006-09-23 17:54:24 +00:00
|
|
|
priv->g_r = ceil(radius);
|
|
|
|
priv->g_w = 2*priv->g_r+1;
|
2006-09-16 13:32:46 +00:00
|
|
|
|
2006-09-23 17:54:24 +00:00
|
|
|
if (priv->g_r) {
|
|
|
|
priv->g = malloc(priv->g_w * sizeof(unsigned));
|
|
|
|
priv->gt2 = malloc(256 * priv->g_w * sizeof(unsigned));
|
|
|
|
if (priv->g==NULL || priv->gt2==NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2006-09-16 13:32:46 +00:00
|
|
|
}
|
|
|
|
|
2006-09-23 17:54:24 +00:00
|
|
|
if (priv->g_r) {
|
|
|
|
// gaussian curve with volume = 256
|
|
|
|
for (volume_diff=10000000; volume_diff>0.0000001; volume_diff*=0.5){
|
|
|
|
volume_factor+= volume_diff;
|
|
|
|
volume=0;
|
|
|
|
for (i = 0; i<priv->g_w; ++i) {
|
|
|
|
priv->g[i] = (unsigned)(exp(A * (i-priv->g_r)*(i-priv->g_r)) * volume_factor + .5);
|
|
|
|
volume+= priv->g[i];
|
|
|
|
}
|
|
|
|
if(volume>256) volume_factor-= volume_diff;
|
|
|
|
}
|
|
|
|
volume=0;
|
|
|
|
for (i = 0; i<priv->g_w; ++i) {
|
|
|
|
priv->g[i] = (unsigned)(exp(A * (i-priv->g_r)*(i-priv->g_r)) * volume_factor + .5);
|
|
|
|
volume+= priv->g[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// gauss table:
|
|
|
|
for(mx=0;mx<priv->g_w;mx++){
|
|
|
|
for(i=0;i<256;i++){
|
|
|
|
priv->gt2[mx+i*priv->g_w] = i*priv->g[mx];
|
|
|
|
}
|
|
|
|
}
|
2006-09-16 13:32:46 +00:00
|
|
|
}
|
2006-09-23 17:54:24 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-09-16 13:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void resize_tmp(ass_synth_priv_t* priv, int w, int h)
|
|
|
|
{
|
|
|
|
if (priv->tmp_w >= w && priv->tmp_h >= h)
|
|
|
|
return;
|
|
|
|
if (priv->tmp_w == 0)
|
|
|
|
priv->tmp_w = 64;
|
|
|
|
if (priv->tmp_h == 0)
|
|
|
|
priv->tmp_h = 64;
|
|
|
|
while (priv->tmp_w < w) priv->tmp_w *= 2;
|
|
|
|
while (priv->tmp_h < h) priv->tmp_h *= 2;
|
|
|
|
if (priv->tmp)
|
|
|
|
free(priv->tmp);
|
|
|
|
priv->tmp = malloc((priv->tmp_w + 1) * priv->tmp_h * sizeof(short));
|
|
|
|
}
|
|
|
|
|
2007-04-01 21:59:54 +00:00
|
|
|
ass_synth_priv_t* ass_synth_init(void)
|
2006-09-16 13:32:46 +00:00
|
|
|
{
|
|
|
|
ass_synth_priv_t* priv = calloc(1, sizeof(ass_synth_priv_t));
|
|
|
|
generate_tables(priv, blur_radius);
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ass_synth_done(ass_synth_priv_t* priv)
|
|
|
|
{
|
2006-09-24 15:50:31 +00:00
|
|
|
if (priv->tmp)
|
|
|
|
free(priv->tmp);
|
|
|
|
if (priv->g)
|
|
|
|
free(priv->g);
|
|
|
|
if (priv->gt2)
|
|
|
|
free(priv->gt2);
|
2006-09-16 13:32:46 +00:00
|
|
|
free(priv);
|
|
|
|
}
|
|
|
|
|
2006-09-16 13:08:17 +00:00
|
|
|
static bitmap_t* alloc_bitmap(int w, int h)
|
|
|
|
{
|
|
|
|
bitmap_t* bm;
|
|
|
|
bm = calloc(1, sizeof(bitmap_t));
|
|
|
|
bm->buffer = malloc(w*h);
|
|
|
|
bm->w = w;
|
|
|
|
bm->h = h;
|
|
|
|
bm->left = bm->top = 0;
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ass_free_bitmap(bitmap_t* bm)
|
|
|
|
{
|
|
|
|
if (bm) {
|
|
|
|
if (bm->buffer) free(bm->buffer);
|
|
|
|
free(bm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-24 16:04:37 +00:00
|
|
|
static bitmap_t* copy_bitmap(const bitmap_t* src)
|
|
|
|
{
|
|
|
|
bitmap_t* dst = alloc_bitmap(src->w, src->h);
|
|
|
|
dst->left = src->left;
|
|
|
|
dst->top = src->top;
|
|
|
|
memcpy(dst->buffer, src->buffer, src->w * src->h);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2008-02-22 19:31:53 +00:00
|
|
|
static int check_glyph_area(FT_Glyph glyph)
|
|
|
|
{
|
|
|
|
FT_BBox bbox;
|
|
|
|
long long dx, dy;
|
|
|
|
FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox);
|
|
|
|
dx = bbox.xMax - bbox.xMin;
|
|
|
|
dy = bbox.yMax - bbox.yMin;
|
|
|
|
if (dx * dy > 8000000) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_GlyphBBoxTooLarge, (int)dx, (int)dy);
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-16 13:08:17 +00:00
|
|
|
static bitmap_t* glyph_to_bitmap_internal(FT_Glyph glyph, int bord)
|
|
|
|
{
|
|
|
|
FT_BitmapGlyph bg;
|
|
|
|
FT_Bitmap* bit;
|
|
|
|
bitmap_t* bm;
|
|
|
|
int w, h;
|
|
|
|
unsigned char* src;
|
|
|
|
unsigned char* dst;
|
|
|
|
int i;
|
|
|
|
int error;
|
|
|
|
|
2008-02-22 19:31:53 +00:00
|
|
|
if (check_glyph_area(glyph))
|
|
|
|
return 0;
|
2006-09-16 13:08:17 +00:00
|
|
|
error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 0);
|
|
|
|
if (error) {
|
2006-11-19 22:35:17 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FT_Glyph_To_BitmapError, error);
|
2006-09-16 13:08:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bg = (FT_BitmapGlyph)glyph;
|
|
|
|
bit = &(bg->bitmap);
|
|
|
|
if (bit->pixel_mode != FT_PIXEL_MODE_GRAY) {
|
2006-11-19 22:35:17 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UnsupportedPixelMode, (int)(bit->pixel_mode));
|
2006-09-16 13:08:17 +00:00
|
|
|
FT_Done_Glyph(glyph);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
w = bit->width;
|
|
|
|
h = bit->rows;
|
|
|
|
bm = alloc_bitmap(w + 2*bord, h + 2*bord);
|
|
|
|
memset(bm->buffer, 0, bm->w * bm->h);
|
|
|
|
bm->left = bg->left - bord;
|
|
|
|
bm->top = - bg->top - bord;
|
|
|
|
|
|
|
|
src = bit->buffer;
|
|
|
|
dst = bm->buffer + bord + bm->w * bord;
|
|
|
|
for (i = 0; i < h; ++i) {
|
|
|
|
memcpy(dst, src, w);
|
|
|
|
src += bit->pitch;
|
|
|
|
dst += bm->w;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2006-09-24 16:15:47 +00:00
|
|
|
/**
|
|
|
|
* \brief fix outline bitmap and generate shadow bitmap
|
|
|
|
* Two things are done here:
|
|
|
|
* 1. Glyph bitmap is subtracted from outline bitmap. This way looks much better in some cases.
|
|
|
|
* 2. Shadow bitmap is created as a sum of glyph and outline bitmaps.
|
|
|
|
*/
|
2006-09-24 16:04:37 +00:00
|
|
|
static bitmap_t* fix_outline_and_shadow(bitmap_t* bm_g, bitmap_t* bm_o)
|
2006-09-16 19:01:56 +00:00
|
|
|
{
|
|
|
|
int x, y;
|
2006-09-23 00:15:00 +00:00
|
|
|
const int l = bm_o->left > bm_g->left ? bm_o->left : bm_g->left;
|
|
|
|
const int t = bm_o->top > bm_g->top ? bm_o->top : bm_g->top;
|
|
|
|
const int r = bm_o->left + bm_o->w < bm_g->left + bm_g->w ? bm_o->left + bm_o->w : bm_g->left + bm_g->w;
|
|
|
|
const int b = bm_o->top + bm_o->h < bm_g->top + bm_g->h ? bm_o->top + bm_o->h : bm_g->top + bm_g->h;
|
2006-09-24 16:04:37 +00:00
|
|
|
|
|
|
|
bitmap_t* bm_s = copy_bitmap(bm_o);
|
|
|
|
|
2006-09-23 00:15:00 +00:00
|
|
|
unsigned char* g = bm_g->buffer + (t - bm_g->top) * bm_g->w + (l - bm_g->left);
|
|
|
|
unsigned char* o = bm_o->buffer + (t - bm_o->top) * bm_o->w + (l - bm_o->left);
|
2006-09-24 16:04:37 +00:00
|
|
|
unsigned char* s = bm_s->buffer + (t - bm_s->top) * bm_s->w + (l - bm_s->left);
|
2006-09-23 00:15:00 +00:00
|
|
|
|
|
|
|
for (y = 0; y < b - t; ++y) {
|
|
|
|
for (x = 0; x < r - l; ++x) {
|
2006-09-16 19:01:56 +00:00
|
|
|
unsigned char c_g, c_o;
|
2006-09-23 00:15:00 +00:00
|
|
|
c_g = g[x];
|
|
|
|
c_o = o[x];
|
2006-11-13 16:35:15 +00:00
|
|
|
o[x] = (c_o > c_g) ? c_o : 0;
|
2006-09-24 16:04:37 +00:00
|
|
|
s[x] = (c_o < 0xFF - c_g) ? c_o + c_g : 0xFF;
|
2006-09-16 19:01:56 +00:00
|
|
|
}
|
2006-09-23 00:15:00 +00:00
|
|
|
g += bm_g->w;
|
|
|
|
o += bm_o->w;
|
2006-09-24 16:04:37 +00:00
|
|
|
s += bm_s->w;
|
2006-09-23 00:15:00 +00:00
|
|
|
}
|
2006-09-24 16:04:37 +00:00
|
|
|
|
|
|
|
assert(bm_s);
|
|
|
|
return bm_s;
|
2006-09-16 19:01:56 +00:00
|
|
|
}
|
|
|
|
|
2006-09-24 16:04:37 +00:00
|
|
|
int glyph_to_bitmap(ass_synth_priv_t* priv, FT_Glyph glyph, FT_Glyph outline_glyph,
|
|
|
|
bitmap_t** bm_g, bitmap_t** bm_o, bitmap_t** bm_s, int be)
|
2006-09-16 13:08:17 +00:00
|
|
|
{
|
2006-09-23 18:35:19 +00:00
|
|
|
const int bord = be ? ceil(blur_radius) : 0;
|
2006-09-16 13:32:46 +00:00
|
|
|
|
2006-09-24 16:04:37 +00:00
|
|
|
assert(bm_g && bm_o && bm_s);
|
|
|
|
|
|
|
|
*bm_g = *bm_o = *bm_s = 0;
|
2006-09-16 13:08:17 +00:00
|
|
|
|
|
|
|
if (glyph)
|
2006-09-16 13:32:46 +00:00
|
|
|
*bm_g = glyph_to_bitmap_internal(glyph, bord);
|
2006-09-16 13:08:17 +00:00
|
|
|
if (!*bm_g)
|
|
|
|
return 1;
|
2006-09-24 16:04:37 +00:00
|
|
|
|
2006-09-17 15:32:01 +00:00
|
|
|
if (outline_glyph) {
|
2006-09-16 13:32:46 +00:00
|
|
|
*bm_o = glyph_to_bitmap_internal(outline_glyph, bord);
|
2006-09-16 13:08:17 +00:00
|
|
|
if (!*bm_o) {
|
|
|
|
ass_free_bitmap(*bm_g);
|
|
|
|
return 1;
|
|
|
|
}
|
2006-09-24 16:04:37 +00:00
|
|
|
}
|
2006-09-17 15:32:01 +00:00
|
|
|
if (*bm_o)
|
2006-09-16 13:32:46 +00:00
|
|
|
resize_tmp(priv, (*bm_o)->w, (*bm_o)->h);
|
|
|
|
resize_tmp(priv, (*bm_g)->w, (*bm_g)->h);
|
|
|
|
|
|
|
|
if (be) {
|
2006-09-17 15:32:01 +00:00
|
|
|
if (*bm_o)
|
2006-09-16 13:32:46 +00:00
|
|
|
blur((*bm_o)->buffer, priv->tmp, (*bm_o)->w, (*bm_o)->h, (*bm_o)->w, (int*)priv->gt2, priv->g_r, priv->g_w);
|
2008-08-07 22:20:58 +00:00
|
|
|
else
|
|
|
|
blur((*bm_g)->buffer, priv->tmp, (*bm_g)->w, (*bm_g)->h, (*bm_g)->w, (int*)priv->gt2, priv->g_r, priv->g_w);
|
2006-09-16 13:32:46 +00:00
|
|
|
}
|
|
|
|
|
2006-09-17 15:32:01 +00:00
|
|
|
if (*bm_o)
|
2006-09-24 16:04:37 +00:00
|
|
|
*bm_s = fix_outline_and_shadow(*bm_g, *bm_o);
|
|
|
|
else
|
|
|
|
*bm_s = copy_bitmap(*bm_g);
|
2006-09-16 19:01:56 +00:00
|
|
|
|
2006-09-24 16:04:37 +00:00
|
|
|
assert(bm_s);
|
2006-09-16 13:08:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|