1 /***************************************************************************/
5 /* FreeType API for color filtering of subpixel bitmap glyphs (body). */
7 /* Copyright 2006, 2008-2010, 2013 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
22 #include FT_LCD_FILTER_H
24 #include FT_INTERNAL_OBJECTS_H
27 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
29 /* define USE_LEGACY to implement the legacy filter */
32 /* FIR filter used by the default and light filters */
34 _ft_lcd_filter_fir( FT_Bitmap* bitmap,
38 FT_Byte* weights = library->lcd_weights;
39 FT_UInt width = (FT_UInt)bitmap->width;
40 FT_UInt height = (FT_UInt)bitmap->rows;
43 /* horizontal in-place FIR filter */
44 if ( mode == FT_RENDER_MODE_LCD && width >= 4 )
46 FT_Byte* line = bitmap->buffer;
49 for ( ; height > 0; height--, line += bitmap->pitch )
56 fir[0] = weights[2] * val1;
57 fir[1] = weights[3] * val1;
58 fir[2] = weights[4] * val1;
63 fir[0] += weights[1] * val1;
64 fir[1] += weights[2] * val1;
65 fir[2] += weights[3] * val1;
66 fir[3] += weights[4] * val1;
68 for ( xx = 2; xx < width; xx++ )
74 pix = fir[0] + weights[0] * val;
75 fir[0] = fir[1] + weights[1] * val;
76 fir[1] = fir[2] + weights[2] * val;
77 fir[2] = fir[3] + weights[3] * val;
78 fir[3] = weights[4] * val;
82 line[xx - 2] = (FT_Byte)pix;
91 line[xx - 2] = (FT_Byte)pix;
95 line[xx - 1] = (FT_Byte)pix;
100 /* vertical in-place FIR filter */
101 else if ( mode == FT_RENDER_MODE_LCD_V && height >= 4 )
103 FT_Byte* column = bitmap->buffer;
104 FT_Int pitch = bitmap->pitch;
107 for ( ; width > 0; width--, column++ )
109 FT_Byte* col = column;
115 fir[0] = weights[2] * val1;
116 fir[1] = weights[3] * val1;
117 fir[2] = weights[4] * val1;
123 fir[0] += weights[1] * val1;
124 fir[1] += weights[2] * val1;
125 fir[2] += weights[3] * val1;
126 fir[3] += weights[4] * val1;
129 for ( yy = 2; yy < height; yy++ )
135 pix = fir[0] + weights[0] * val;
136 fir[0] = fir[1] + weights[1] * val;
137 fir[1] = fir[2] + weights[2] * val;
138 fir[2] = fir[3] + weights[3] * val;
139 fir[3] = weights[4] * val;
142 pix |= -( pix >> 8 );
143 col[-2 * pitch] = (FT_Byte)pix;
152 pix |= -( pix >> 8 );
153 col[-2 * pitch] = (FT_Byte)pix;
156 pix |= -( pix >> 8 );
157 col[-pitch] = (FT_Byte)pix;
166 /* intra-pixel filter used by the legacy filter */
168 _ft_lcd_filter_legacy( FT_Bitmap* bitmap,
172 FT_UInt width = (FT_UInt)bitmap->width;
173 FT_UInt height = (FT_UInt)bitmap->rows;
174 FT_Int pitch = bitmap->pitch;
176 static const int filters[3][3] =
178 { 65538 * 9/13, 65538 * 1/6, 65538 * 1/13 },
179 { 65538 * 3/13, 65538 * 4/6, 65538 * 3/13 },
180 { 65538 * 1/13, 65538 * 1/6, 65538 * 9/13 }
183 FT_UNUSED( library );
186 /* horizontal in-place intra-pixel filter */
187 if ( mode == FT_RENDER_MODE_LCD && width >= 3 )
189 FT_Byte* line = bitmap->buffer;
192 for ( ; height > 0; height--, line += pitch )
197 for ( xx = 0; xx < width; xx += 3 )
206 r += filters[0][0] * p;
207 g += filters[0][1] * p;
208 b += filters[0][2] * p;
211 r += filters[1][0] * p;
212 g += filters[1][1] * p;
213 b += filters[1][2] * p;
216 r += filters[2][0] * p;
217 g += filters[2][1] * p;
218 b += filters[2][2] * p;
220 line[xx] = (FT_Byte)( r / 65536 );
221 line[xx + 1] = (FT_Byte)( g / 65536 );
222 line[xx + 2] = (FT_Byte)( b / 65536 );
226 else if ( mode == FT_RENDER_MODE_LCD_V && height >= 3 )
228 FT_Byte* column = bitmap->buffer;
231 for ( ; width > 0; width--, column++ )
233 FT_Byte* col = column;
234 FT_Byte* col_end = col + height * pitch;
237 for ( ; col < col_end; col += 3 * pitch )
246 r += filters[0][0] * p;
247 g += filters[0][1] * p;
248 b += filters[0][2] * p;
251 r += filters[1][0] * p;
252 g += filters[1][1] * p;
253 b += filters[1][2] * p;
256 r += filters[2][0] * p;
257 g += filters[2][1] * p;
258 b += filters[2][2] * p;
260 col[0] = (FT_Byte)( r / 65536 );
261 col[pitch] = (FT_Byte)( g / 65536 );
262 col[2 * pitch] = (FT_Byte)( b / 65536 );
268 #endif /* USE_LEGACY */
271 FT_EXPORT_DEF( FT_Error )
272 FT_Library_SetLcdFilterWeights( FT_Library library,
273 unsigned char *weights )
275 if ( !library || !weights )
276 return FT_THROW( Invalid_Argument );
278 ft_memcpy( library->lcd_weights, weights, 5 );
284 FT_EXPORT_DEF( FT_Error )
285 FT_Library_SetLcdFilter( FT_Library library,
286 FT_LcdFilter filter )
288 static const FT_Byte light_filter[5] =
289 { 0x00, 0x55, 0x56, 0x55, 0x00 };
290 /* the values here sum up to a value larger than 256, */
291 /* providing a cheap gamma correction */
292 static const FT_Byte default_filter[5] =
293 { 0x10, 0x40, 0x70, 0x40, 0x10 };
297 return FT_THROW( Invalid_Argument );
301 case FT_LCD_FILTER_NONE:
302 library->lcd_filter_func = NULL;
303 library->lcd_extra = 0;
306 case FT_LCD_FILTER_DEFAULT:
307 #if defined( FT_FORCE_LEGACY_LCD_FILTER )
309 library->lcd_filter_func = _ft_lcd_filter_legacy;
310 library->lcd_extra = 0;
312 #elif defined( FT_FORCE_LIGHT_LCD_FILTER )
314 ft_memcpy( library->lcd_weights, light_filter, 5 );
315 library->lcd_filter_func = _ft_lcd_filter_fir;
316 library->lcd_extra = 2;
320 ft_memcpy( library->lcd_weights, default_filter, 5 );
321 library->lcd_filter_func = _ft_lcd_filter_fir;
322 library->lcd_extra = 2;
328 case FT_LCD_FILTER_LIGHT:
329 ft_memcpy( library->lcd_weights, light_filter, 5 );
330 library->lcd_filter_func = _ft_lcd_filter_fir;
331 library->lcd_extra = 2;
336 case FT_LCD_FILTER_LEGACY:
337 library->lcd_filter_func = _ft_lcd_filter_legacy;
338 library->lcd_extra = 0;
344 return FT_THROW( Invalid_Argument );
347 library->lcd_filter = filter;
352 #else /* !FT_CONFIG_OPTION_SUBPIXEL_RENDERING */
354 FT_EXPORT_DEF( FT_Error )
355 FT_Library_SetLcdFilterWeights( FT_Library library,
356 unsigned char *weights )
358 FT_UNUSED( library );
359 FT_UNUSED( weights );
361 return FT_THROW( Unimplemented_Feature );
365 FT_EXPORT_DEF( FT_Error )
366 FT_Library_SetLcdFilter( FT_Library library,
367 FT_LcdFilter filter )
369 FT_UNUSED( library );
372 return FT_THROW( Unimplemented_Feature );
375 #endif /* !FT_CONFIG_OPTION_SUBPIXEL_RENDERING */