1 /*
2  * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3  *
4  * This file is part of libass.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef LIBASS_BITMAP_H
20 #define LIBASS_BITMAP_H
21 
22 #include <stdbool.h>
23 #include <ft2build.h>
24 #include FT_GLYPH_H
25 
26 #include "ass.h"
27 
28 
29 struct segment;
30 typedef void (*FillSolidTileFunc)(uint8_t *buf, ptrdiff_t stride, int set);
31 typedef void (*FillHalfplaneTileFunc)(uint8_t *buf, ptrdiff_t stride,
32                                       int32_t a, int32_t b, int64_t c, int32_t scale);
33 typedef void (*FillGenericTileFunc)(uint8_t *buf, ptrdiff_t stride,
34                                     const struct segment *line, size_t n_lines,
35                                     int winding);
36 
37 typedef void (*BitmapBlendFunc)(uint8_t *dst, intptr_t dst_stride,
38                                 uint8_t *src, intptr_t src_stride,
39                                 intptr_t height, intptr_t width);
40 typedef void (*BitmapMulFunc)(uint8_t *dst, intptr_t dst_stride,
41                               uint8_t *src1, intptr_t src1_stride,
42                               uint8_t *src2, intptr_t src2_stride,
43                               intptr_t width, intptr_t height);
44 
45 typedef void (*BeBlurFunc)(uint8_t *buf, intptr_t w, intptr_t h,
46                            intptr_t stride, uint16_t *tmp);
47 
48 // intermediate bitmaps represented as sets of verical stripes of int16_t[alignment / 2]
49 typedef void (*Convert8to16Func)(int16_t *dst, const uint8_t *src, ptrdiff_t src_stride,
50                                  uintptr_t width, uintptr_t height);
51 typedef void (*Convert16to8Func)(uint8_t *dst, ptrdiff_t dst_stride, const int16_t *src,
52                                  uintptr_t width, uintptr_t height);
53 typedef void (*FilterFunc)(int16_t *dst, const int16_t *src,
54                            uintptr_t src_width, uintptr_t src_height);
55 typedef void (*ParamFilterFunc)(int16_t *dst, const int16_t *src,
56                                 uintptr_t src_width, uintptr_t src_height,
57                                 const int16_t *param);
58 
59 #define C_ALIGN_ORDER 5
60 
61 typedef struct {
62     int align_order;  // log2(alignment)
63 
64     // rasterizer functions
65 #if CONFIG_RASTERIZER
66     int tile_order;  // log2(tile_size)
67     FillSolidTileFunc fill_solid;
68     FillHalfplaneTileFunc fill_halfplane;
69     FillGenericTileFunc fill_generic;
70 #endif
71 
72     // blend functions
73     BitmapBlendFunc add_bitmaps, sub_bitmaps;
74     BitmapMulFunc mul_bitmaps;
75 
76     // be blur function
77     BeBlurFunc be_blur;
78 
79     // gaussian blur functions
80     Convert8to16Func stripe_unpack;
81     Convert16to8Func stripe_pack;
82     FilterFunc shrink_horz, shrink_vert;
83     FilterFunc expand_horz, expand_vert;
84     FilterFunc pre_blur_horz[3], pre_blur_vert[3];
85     ParamFilterFunc main_blur_horz[3], main_blur_vert[3];
86 } BitmapEngine;
87 
88 extern const BitmapEngine ass_bitmap_engine_c;
89 extern const BitmapEngine ass_bitmap_engine_sse2;
90 extern const BitmapEngine ass_bitmap_engine_avx2;
91 
92 
93 typedef struct {
94     size_t n_contours, max_contours;
95     size_t *contours;
96     size_t n_points, max_points;
97     FT_Vector *points;
98     char *tags;
99 } ASS_Outline;
100 
101 #define EFFICIENT_CONTOUR_COUNT 8
102 
103 typedef struct {
104     int left, top;
105     int w, h;                   // width, height
106     int stride;
107     unsigned char *buffer;      // h * stride buffer
108 } Bitmap;
109 
110 Bitmap *alloc_bitmap(const BitmapEngine *engine, int w, int h);
111 bool realloc_bitmap(const BitmapEngine *engine, Bitmap *bm, int w, int h);
112 Bitmap *copy_bitmap(const BitmapEngine *engine, const Bitmap *src);
113 void ass_free_bitmap(Bitmap *bm);
114 
115 Bitmap *outline_to_bitmap(ASS_Renderer *render_priv,
116                           ASS_Outline *outline, int bord);
117 
118 void ass_synth_blur(const BitmapEngine *engine, int opaque_box, int be,
119                     double blur_radius, Bitmap *bm_g, Bitmap *bm_o);
120 
121 /**
122  * \brief perform glyph rendering
123  * \param outline original glyph
124  * \param border "border" glyph, produced from outline by FreeType's glyph stroker
125  * \param bm_g out: pointer to the bitmap of original glyph is returned here
126  * \param bm_o out: pointer to the bitmap of border glyph is returned here
127  */
128 int outline_to_bitmap2(ASS_Renderer *render_priv,
129                        ASS_Outline *outline, ASS_Outline *border,
130                        Bitmap **bm_g, Bitmap **bm_o);
131 
132 int be_padding(int be);
133 void be_blur_pre(uint8_t *buf, intptr_t w,
134                  intptr_t h, intptr_t stride);
135 void be_blur_post(uint8_t *buf, intptr_t w,
136                   intptr_t h, intptr_t stride);
137 bool ass_gaussian_blur(const BitmapEngine *engine, Bitmap *bm, double r2);
138 void shift_bitmap(Bitmap *bm, int shift_x, int shift_y);
139 void fix_outline(Bitmap *bm_g, Bitmap *bm_o);
140 
141 #endif                          /* LIBASS_BITMAP_H */
142