annotate libass/ass_bitmap.c @ 19998:b1cebdd78625

Do not compile with -DDEBUG by default.
author diego
date Fri, 29 Sep 2006 09:46:06 +0000
parents 27187c1ac20b
children fa122b7c71c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19846
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
1 #include <stdlib.h>
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
2 #include <string.h>
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
3 #include <math.h>
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
4 #include <assert.h>
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
5 #include <ft2build.h>
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
6 #include FT_GLYPH_H
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
7
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
8 #include "mp_msg.h"
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
9 #include "libvo/font_load.h" // for blur()
19846
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
10 #include "ass_bitmap.h"
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
11
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
12 struct ass_synth_priv_s {
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
13 int tmp_w, tmp_h;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
14 unsigned short* tmp;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
15
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
16 int g_r;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
17 int g_w;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
18
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
19 unsigned *g;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
20 unsigned *gt2;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
21 };
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
22
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
23 static const unsigned int maxcolor = 255;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
24 static const unsigned base = 256;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
25 static const double blur_radius = 1.5;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
26
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
27 static int generate_tables(ass_synth_priv_t* priv, double radius)
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
28 {
19955
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
29 double A = log(1.0/base)/(radius*radius*2);
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
30 int mx, i;
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
31 double volume_diff, volume_factor = 0;
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
32 unsigned volume;
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
33
19955
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
34 priv->g_r = ceil(radius);
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
35 priv->g_w = 2*priv->g_r+1;
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
36
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
37 if (priv->g_r) {
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
38 priv->g = malloc(priv->g_w * sizeof(unsigned));
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
39 priv->gt2 = malloc(256 * priv->g_w * sizeof(unsigned));
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
40 if (priv->g==NULL || priv->gt2==NULL) {
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
41 return -1;
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
42 }
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
43 }
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
44
19955
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
45 if (priv->g_r) {
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
46 // gaussian curve with volume = 256
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
47 for (volume_diff=10000000; volume_diff>0.0000001; volume_diff*=0.5){
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
48 volume_factor+= volume_diff;
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
49 volume=0;
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
50 for (i = 0; i<priv->g_w; ++i) {
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
51 priv->g[i] = (unsigned)(exp(A * (i-priv->g_r)*(i-priv->g_r)) * volume_factor + .5);
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
52 volume+= priv->g[i];
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
53 }
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
54 if(volume>256) volume_factor-= volume_diff;
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
55 }
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
56 volume=0;
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
57 for (i = 0; i<priv->g_w; ++i) {
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
58 priv->g[i] = (unsigned)(exp(A * (i-priv->g_r)*(i-priv->g_r)) * volume_factor + .5);
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
59 volume+= priv->g[i];
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
60 }
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
61
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
62 // gauss table:
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
63 for(mx=0;mx<priv->g_w;mx++){
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
64 for(i=0;i<256;i++){
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
65 priv->gt2[mx+i*priv->g_w] = i*priv->g[mx];
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
66 }
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
67 }
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
68 }
19955
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
69
2792de2ca069 Cosmetics. Change indentation of block of code to make it consistent with
eugeni
parents: 19941
diff changeset
70 return 0;
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
71 }
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
72
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
73 static void resize_tmp(ass_synth_priv_t* priv, int w, int h)
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
74 {
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
75 if (priv->tmp_w >= w && priv->tmp_h >= h)
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
76 return;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
77 if (priv->tmp_w == 0)
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
78 priv->tmp_w = 64;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
79 if (priv->tmp_h == 0)
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
80 priv->tmp_h = 64;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
81 while (priv->tmp_w < w) priv->tmp_w *= 2;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
82 while (priv->tmp_h < h) priv->tmp_h *= 2;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
83 if (priv->tmp)
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
84 free(priv->tmp);
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
85 priv->tmp = malloc((priv->tmp_w + 1) * priv->tmp_h * sizeof(short));
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
86 }
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
87
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
88 ass_synth_priv_t* ass_synth_init()
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
89 {
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
90 ass_synth_priv_t* priv = calloc(1, sizeof(ass_synth_priv_t));
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
91 generate_tables(priv, blur_radius);
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
92 return priv;
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
93 }
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
94
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
95 void ass_synth_done(ass_synth_priv_t* priv)
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
96 {
19962
c76d6a7332e8 Free memory in ass_synth_done().
eugeni
parents: 19956
diff changeset
97 if (priv->tmp)
c76d6a7332e8 Free memory in ass_synth_done().
eugeni
parents: 19956
diff changeset
98 free(priv->tmp);
c76d6a7332e8 Free memory in ass_synth_done().
eugeni
parents: 19956
diff changeset
99 if (priv->g)
c76d6a7332e8 Free memory in ass_synth_done().
eugeni
parents: 19956
diff changeset
100 free(priv->g);
c76d6a7332e8 Free memory in ass_synth_done().
eugeni
parents: 19956
diff changeset
101 if (priv->gt2)
c76d6a7332e8 Free memory in ass_synth_done().
eugeni
parents: 19956
diff changeset
102 free(priv->gt2);
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
103 free(priv);
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
104 }
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
105
19846
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
106 static bitmap_t* alloc_bitmap(int w, int h)
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
107 {
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
108 bitmap_t* bm;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
109 bm = calloc(1, sizeof(bitmap_t));
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
110 bm->buffer = malloc(w*h);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
111 bm->w = w;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
112 bm->h = h;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
113 bm->left = bm->top = 0;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
114 return bm;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
115 }
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
116
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
117 void ass_free_bitmap(bitmap_t* bm)
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
118 {
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
119 if (bm) {
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
120 if (bm->buffer) free(bm->buffer);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
121 free(bm);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
122 }
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
123 }
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
124
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
125 static bitmap_t* copy_bitmap(const bitmap_t* src)
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
126 {
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
127 bitmap_t* dst = alloc_bitmap(src->w, src->h);
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
128 dst->left = src->left;
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
129 dst->top = src->top;
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
130 memcpy(dst->buffer, src->buffer, src->w * src->h);
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
131 return dst;
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
132 }
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
133
19846
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
134 static bitmap_t* glyph_to_bitmap_internal(FT_Glyph glyph, int bord)
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
135 {
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
136 FT_BitmapGlyph bg;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
137 FT_Bitmap* bit;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
138 bitmap_t* bm;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
139 int w, h;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
140 unsigned char* src;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
141 unsigned char* dst;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
142 int i;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
143 int error;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
144
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
145 error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 0);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
146 if (error) {
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
147 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FT_Glyph_To_Bitmap error %d \n", error);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
148 return 0;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
149 }
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
150
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
151 bg = (FT_BitmapGlyph)glyph;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
152 bit = &(bg->bitmap);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
153 if (bit->pixel_mode != FT_PIXEL_MODE_GRAY) {
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
154 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Unsupported pixel mode: %d\n", (int)(bit->pixel_mode));
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
155 FT_Done_Glyph(glyph);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
156 return 0;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
157 }
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
158
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
159 w = bit->width;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
160 h = bit->rows;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
161 bm = alloc_bitmap(w + 2*bord, h + 2*bord);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
162 memset(bm->buffer, 0, bm->w * bm->h);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
163 bm->left = bg->left - bord;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
164 bm->top = - bg->top - bord;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
165
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
166 src = bit->buffer;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
167 dst = bm->buffer + bord + bm->w * bord;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
168 for (i = 0; i < h; ++i) {
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
169 memcpy(dst, src, w);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
170 src += bit->pitch;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
171 dst += bm->w;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
172 }
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
173
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
174 return bm;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
175 }
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
176
19966
27187c1ac20b Cosmetics: add some comments.
eugeni
parents: 19965
diff changeset
177 /**
27187c1ac20b Cosmetics: add some comments.
eugeni
parents: 19965
diff changeset
178 * \brief fix outline bitmap and generate shadow bitmap
27187c1ac20b Cosmetics: add some comments.
eugeni
parents: 19965
diff changeset
179 * Two things are done here:
27187c1ac20b Cosmetics: add some comments.
eugeni
parents: 19965
diff changeset
180 * 1. Glyph bitmap is subtracted from outline bitmap. This way looks much better in some cases.
27187c1ac20b Cosmetics: add some comments.
eugeni
parents: 19965
diff changeset
181 * 2. Shadow bitmap is created as a sum of glyph and outline bitmaps.
27187c1ac20b Cosmetics: add some comments.
eugeni
parents: 19965
diff changeset
182 */
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
183 static bitmap_t* fix_outline_and_shadow(bitmap_t* bm_g, bitmap_t* bm_o)
19856
b4cb97ef9d20 Subtract glyph bitmap from outline bitmap.
eugeni
parents: 19848
diff changeset
184 {
b4cb97ef9d20 Subtract glyph bitmap from outline bitmap.
eugeni
parents: 19848
diff changeset
185 int x, y;
19941
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
186 const int l = bm_o->left > bm_g->left ? bm_o->left : bm_g->left;
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
187 const int t = bm_o->top > bm_g->top ? bm_o->top : bm_g->top;
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
188 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;
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
189 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;
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
190
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
191 bitmap_t* bm_s = copy_bitmap(bm_o);
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
192
19941
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
193 unsigned char* g = bm_g->buffer + (t - bm_g->top) * bm_g->w + (l - bm_g->left);
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
194 unsigned char* o = bm_o->buffer + (t - bm_o->top) * bm_o->w + (l - bm_o->left);
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
195 unsigned char* s = bm_s->buffer + (t - bm_s->top) * bm_s->w + (l - bm_s->left);
19941
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
196
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
197 for (y = 0; y < b - t; ++y) {
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
198 for (x = 0; x < r - l; ++x) {
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
199 unsigned char c_g, c_o;
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
200 c_g = g[x];
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
201 c_o = o[x];
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
202 o[x] = (c_o > c_g) ? c_o - c_g : 0;
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
203 s[x] = (c_o < 0xFF - c_g) ? c_o + c_g : 0xFF;
19941
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
204 }
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
205 g += bm_g->w;
66d444e5ec8d Better fix_outline implementation. No more "fix_outline failed" messages,
eugeni
parents: 19873
diff changeset
206 o += bm_o->w;
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
207 s += bm_s->w;
19856
b4cb97ef9d20 Subtract glyph bitmap from outline bitmap.
eugeni
parents: 19848
diff changeset
208 }
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
209
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
210 assert(bm_s);
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
211 return bm_s;
19856
b4cb97ef9d20 Subtract glyph bitmap from outline bitmap.
eugeni
parents: 19848
diff changeset
212 }
b4cb97ef9d20 Subtract glyph bitmap from outline bitmap.
eugeni
parents: 19848
diff changeset
213
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
214 int glyph_to_bitmap(ass_synth_priv_t* priv, FT_Glyph glyph, FT_Glyph outline_glyph,
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
215 bitmap_t** bm_g, bitmap_t** bm_o, bitmap_t** bm_s, int be)
19846
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
216 {
19956
5f268ded7f6a Reduce size of non-blurred bitmaps.
eugeni
parents: 19955
diff changeset
217 const int bord = be ? ceil(blur_radius) : 0;
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
218
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
219 assert(bm_g && bm_o && bm_s);
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
220
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
221 *bm_g = *bm_o = *bm_s = 0;
19846
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
222
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
223 if (glyph)
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
224 *bm_g = glyph_to_bitmap_internal(glyph, bord);
19846
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
225 if (!*bm_g)
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
226 return 1;
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
227
19873
98d32b832c3a Reduce code duplication in init_render_context().
eugeni
parents: 19856
diff changeset
228 if (outline_glyph) {
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
229 *bm_o = glyph_to_bitmap_internal(outline_glyph, bord);
19846
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
230 if (!*bm_o) {
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
231 ass_free_bitmap(*bm_g);
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
232 return 1;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
233 }
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
234 }
19873
98d32b832c3a Reduce code duplication in init_render_context().
eugeni
parents: 19856
diff changeset
235 if (*bm_o)
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
236 resize_tmp(priv, (*bm_o)->w, (*bm_o)->h);
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
237 resize_tmp(priv, (*bm_g)->w, (*bm_g)->h);
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
238
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
239 if (be) {
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
240 blur((*bm_g)->buffer, priv->tmp, (*bm_g)->w, (*bm_g)->h, (*bm_g)->w, (int*)priv->gt2, priv->g_r, priv->g_w);
19873
98d32b832c3a Reduce code duplication in init_render_context().
eugeni
parents: 19856
diff changeset
241 if (*bm_o)
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
242 blur((*bm_o)->buffer, priv->tmp, (*bm_o)->w, (*bm_o)->h, (*bm_o)->w, (int*)priv->gt2, priv->g_r, priv->g_w);
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
243 }
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
244
19873
98d32b832c3a Reduce code duplication in init_render_context().
eugeni
parents: 19856
diff changeset
245 if (*bm_o)
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
246 *bm_s = fix_outline_and_shadow(*bm_g, *bm_o);
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
247 else
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
248 *bm_s = copy_bitmap(*bm_g);
19856
b4cb97ef9d20 Subtract glyph bitmap from outline bitmap.
eugeni
parents: 19848
diff changeset
249
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19962
diff changeset
250 assert(bm_s);
19846
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
251 return 0;
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
252 }
bcc792bfa431 Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
diff changeset
253