freetype2/src/base/ftlcdfil.c

393 lines
10 KiB
C
Raw Normal View History

2006-09-29 23:31:53 +02:00
/***************************************************************************/
/* */
/* ftlcdfil.c */
/* */
/* FreeType API for color filtering of subpixel bitmap glyphs (body). */
/* */
2017-01-04 20:16:34 +01:00
/* Copyright 2006-2017 by */
2006-09-29 23:31:53 +02:00
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
/* */
/* This file is part of the FreeType project, and may only be used, */
/* modified, and distributed under the terms of the FreeType project */
/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
/* this file you indicate that you have read the license and */
/* understand and accept it fully. */
/* */
/***************************************************************************/
2006-09-27 10:48:38 +02:00
#include <ft2build.h>
#include FT_INTERNAL_DEBUG_H
2006-09-27 10:48:38 +02:00
#include FT_LCD_FILTER_H
#include FT_IMAGE_H
#include FT_INTERNAL_OBJECTS_H
2006-09-29 23:31:53 +02:00
2006-09-27 10:48:38 +02:00
#ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
/* define USE_LEGACY to implement the legacy filter */
#define USE_LEGACY
/* FIR filter used by the default and light filters */
FT_BASE( void )
ft_lcd_filter_fir( FT_Bitmap* bitmap,
FT_Render_Mode mode,
FT_LcdFiveTapFilter weights )
2006-09-27 10:48:38 +02:00
{
FT_UInt width = (FT_UInt)bitmap->width;
FT_UInt height = (FT_UInt)bitmap->rows;
2006-09-29 23:31:53 +02:00
2006-09-27 10:48:38 +02:00
/* horizontal in-place FIR filter */
if ( mode == FT_RENDER_MODE_LCD && width >= 4 )
{
2006-09-29 23:31:53 +02:00
FT_Byte* line = bitmap->buffer;
2006-09-27 10:48:38 +02:00
/* take care of bitmap flow */
if ( bitmap->pitch < 0 )
line -= bitmap->pitch * (FT_Int)( bitmap->rows - 1 );
/* `fir' and `pix' must be at least 32 bit wide, since the sum of */
/* the values in `weights' can exceed 0xFF */
2006-09-27 10:48:38 +02:00
for ( ; height > 0; height--, line += bitmap->pitch )
{
FT_UInt fir[4]; /* below, `pix' is used as the 5th element */
2006-09-29 23:31:53 +02:00
FT_UInt val1, xx;
2006-09-27 10:48:38 +02:00
val1 = line[0];
2006-09-29 23:31:53 +02:00
fir[0] = weights[2] * val1;
fir[1] = weights[3] * val1;
fir[2] = weights[4] * val1;
2006-09-27 10:48:38 +02:00
fir[3] = 0;
val1 = line[1];
2006-09-29 23:31:53 +02:00
fir[0] += weights[1] * val1;
fir[1] += weights[2] * val1;
fir[2] += weights[3] * val1;
fir[3] += weights[4] * val1;
2006-09-27 10:48:38 +02:00
for ( xx = 2; xx < width; xx++ )
{
FT_UInt val, pix;
2006-09-29 23:31:53 +02:00
2006-09-27 10:48:38 +02:00
val = line[xx];
2006-09-29 23:31:53 +02:00
pix = fir[0] + weights[0] * val;
fir[0] = fir[1] + weights[1] * val;
fir[1] = fir[2] + weights[2] * val;
fir[2] = fir[3] + weights[3] * val;
fir[3] = weights[4] * val;
pix >>= 8;
pix |= (FT_UInt)-(FT_Int)( pix >> 8 );
2006-09-29 23:31:53 +02:00
line[xx - 2] = (FT_Byte)pix;
2006-09-27 10:48:38 +02:00
}
{
FT_UInt pix;
2006-09-29 23:31:53 +02:00
pix = fir[0] >> 8;
pix |= (FT_UInt)-(FT_Int)( pix >> 8 );
2006-09-29 23:31:53 +02:00
line[xx - 2] = (FT_Byte)pix;
pix = fir[1] >> 8;
pix |= (FT_UInt)-(FT_Int)( pix >> 8 );
2006-09-29 23:31:53 +02:00
line[xx - 1] = (FT_Byte)pix;
2006-09-27 10:48:38 +02:00
}
}
}
2006-09-29 23:31:53 +02:00
2006-09-27 10:48:38 +02:00
/* vertical in-place FIR filter */
else if ( mode == FT_RENDER_MODE_LCD_V && height >= 4 )
{
2006-09-29 23:31:53 +02:00
FT_Byte* column = bitmap->buffer;
FT_Int pitch = bitmap->pitch;
2006-09-27 10:48:38 +02:00
/* take care of bitmap flow */
if ( bitmap->pitch < 0 )
column -= bitmap->pitch * (FT_Int)( bitmap->rows - 1 );
2006-09-27 10:48:38 +02:00
for ( ; width > 0; width--, column++ )
{
FT_Byte* col = column;
FT_UInt fir[4]; /* below, `pix' is used as the 5th element */
2006-09-27 10:48:38 +02:00
FT_UInt val1, yy;
2006-09-29 23:31:53 +02:00
2006-09-27 10:48:38 +02:00
val1 = col[0];
2006-09-29 23:31:53 +02:00
fir[0] = weights[2] * val1;
fir[1] = weights[3] * val1;
fir[2] = weights[4] * val1;
2006-09-27 10:48:38 +02:00
fir[3] = 0;
col += pitch;
val1 = col[0];
2006-09-29 23:31:53 +02:00
fir[0] += weights[1] * val1;
fir[1] += weights[2] * val1;
fir[2] += weights[3] * val1;
fir[3] += weights[4] * val1;
2006-09-27 10:48:38 +02:00
col += pitch;
for ( yy = 2; yy < height; yy++ )
{
FT_UInt val, pix;
2006-09-29 23:31:53 +02:00
2006-09-27 10:48:38 +02:00
val = col[0];
2006-09-29 23:31:53 +02:00
pix = fir[0] + weights[0] * val;
fir[0] = fir[1] + weights[1] * val;
fir[1] = fir[2] + weights[2] * val;
fir[2] = fir[3] + weights[3] * val;
fir[3] = weights[4] * val;
pix >>= 8;
pix |= (FT_UInt)-(FT_Int)( pix >> 8 );
2006-09-29 23:31:53 +02:00
col[-2 * pitch] = (FT_Byte)pix;
col += pitch;
2006-09-27 10:48:38 +02:00
}
{
FT_UInt pix;
2006-09-29 23:31:53 +02:00
pix = fir[0] >> 8;
pix |= (FT_UInt)-(FT_Int)( pix >> 8 );
2006-09-29 23:31:53 +02:00
col[-2 * pitch] = (FT_Byte)pix;
2006-09-27 10:48:38 +02:00
pix = fir[1] >> 8;
pix |= (FT_UInt)-(FT_Int)( pix >> 8 );
2006-09-27 10:48:38 +02:00
col[-pitch] = (FT_Byte)pix;
}
}
}
}
#ifdef USE_LEGACY
2006-11-14 11:37:10 +01:00
/* intra-pixel filter used by the legacy filter */
static void
_ft_lcd_filter_legacy( FT_Bitmap* bitmap,
FT_Render_Mode mode,
FT_Byte* weights )
{
2006-11-14 11:37:10 +01:00
FT_UInt width = (FT_UInt)bitmap->width;
FT_UInt height = (FT_UInt)bitmap->rows;
FT_Int pitch = bitmap->pitch;
static const unsigned int filters[3][3] =
{
2006-11-14 11:37:10 +01:00
{ 65538 * 9/13, 65538 * 1/6, 65538 * 1/13 },
{ 65538 * 3/13, 65538 * 4/6, 65538 * 3/13 },
{ 65538 * 1/13, 65538 * 1/6, 65538 * 9/13 }
};
FT_UNUSED( weights );
2006-11-14 11:37:10 +01:00
/* horizontal in-place intra-pixel filter */
if ( mode == FT_RENDER_MODE_LCD && width >= 3 )
{
FT_Byte* line = bitmap->buffer;
/* take care of bitmap flow */
if ( bitmap->pitch < 0 )
line -= bitmap->pitch * (FT_Int)( bitmap->rows - 1 );
for ( ; height > 0; height--, line += pitch )
{
FT_UInt xx;
2006-11-14 11:37:10 +01:00
for ( xx = 0; xx < width; xx += 3 )
{
FT_UInt r = 0;
FT_UInt g = 0;
FT_UInt b = 0;
FT_UInt p;
2006-11-14 11:37:10 +01:00
p = line[xx];
2006-11-14 11:37:10 +01:00
r += filters[0][0] * p;
g += filters[0][1] * p;
b += filters[0][2] * p;
p = line[xx + 1];
r += filters[1][0] * p;
g += filters[1][1] * p;
b += filters[1][2] * p;
p = line[xx + 2];
r += filters[2][0] * p;
g += filters[2][1] * p;
b += filters[2][2] * p;
line[xx] = (FT_Byte)( r / 65536 );
line[xx + 1] = (FT_Byte)( g / 65536 );
line[xx + 2] = (FT_Byte)( b / 65536 );
}
}
}
else if ( mode == FT_RENDER_MODE_LCD_V && height >= 3 )
{
2006-11-14 11:37:10 +01:00
FT_Byte* column = bitmap->buffer;
/* take care of bitmap flow */
if ( bitmap->pitch < 0 )
column -= bitmap->pitch * (FT_Int)( bitmap->rows - 1 );
for ( ; width > 0; width--, column++ )
{
2006-11-14 11:37:10 +01:00
FT_Byte* col = column;
FT_Byte* col_end = col + (FT_Int)height * pitch;
2006-11-14 11:37:10 +01:00
2006-11-14 11:37:10 +01:00
for ( ; col < col_end; col += 3 * pitch )
{
FT_UInt r = 0;
FT_UInt g = 0;
FT_UInt b = 0;
FT_UInt p;
2006-11-14 11:37:10 +01:00
p = col[0];
2006-11-14 11:37:10 +01:00
r += filters[0][0] * p;
g += filters[0][1] * p;
b += filters[0][2] * p;
p = col[pitch];
r += filters[1][0] * p;
g += filters[1][1] * p;
b += filters[1][2] * p;
p = col[pitch * 2];
r += filters[2][0] * p;
g += filters[2][1] * p;
b += filters[2][2] * p;
col[0] = (FT_Byte)( r / 65536 );
col[pitch] = (FT_Byte)( g / 65536 );
col[2 * pitch] = (FT_Byte)( b / 65536 );
}
}
}
}
2006-11-14 11:37:10 +01:00
#endif /* USE_LEGACY */
2006-11-14 11:37:10 +01:00
FT_EXPORT_DEF( FT_Error )
FT_Library_SetLcdFilterWeights( FT_Library library,
unsigned char *weights )
{
if ( !library )
return FT_THROW( Invalid_Library_Handle );
if ( !weights )
return FT_THROW( Invalid_Argument );
ft_memcpy( library->lcd_weights, weights, FT_LCD_FILTER_FIVE_TAPS );
library->lcd_filter_func = ft_lcd_filter_fir;
return FT_Err_Ok;
}
FT_EXPORT_DEF( FT_Error )
FT_Library_SetLcdFilter( FT_Library library,
FT_LcdFilter filter )
2006-09-27 10:48:38 +02:00
{
static const FT_LcdFiveTapFilter default_weights =
{ 0x08, 0x4d, 0x56, 0x4d, 0x08 };
static const FT_LcdFiveTapFilter light_weights =
{ 0x00, 0x55, 0x56, 0x55, 0x00 };
2006-11-14 11:37:10 +01:00
2006-09-27 10:48:38 +02:00
if ( !library )
return FT_THROW( Invalid_Library_Handle );
2006-09-27 10:48:38 +02:00
switch ( filter )
2006-09-27 10:48:38 +02:00
{
2006-11-14 11:37:10 +01:00
case FT_LCD_FILTER_NONE:
library->lcd_filter_func = NULL;
break;
case FT_LCD_FILTER_DEFAULT:
ft_memcpy( library->lcd_weights,
default_weights,
FT_LCD_FILTER_FIVE_TAPS );
library->lcd_filter_func = ft_lcd_filter_fir;
2006-11-14 11:37:10 +01:00
break;
case FT_LCD_FILTER_LIGHT:
ft_memcpy( library->lcd_weights,
light_weights,
FT_LCD_FILTER_FIVE_TAPS );
library->lcd_filter_func = ft_lcd_filter_fir;
2006-11-14 11:37:10 +01:00
break;
#ifdef USE_LEGACY
2006-11-14 11:37:10 +01:00
case FT_LCD_FILTER_LEGACY:
case FT_LCD_FILTER_LEGACY1:
2006-11-14 11:37:10 +01:00
library->lcd_filter_func = _ft_lcd_filter_legacy;
break;
#endif
2006-11-14 11:37:10 +01:00
default:
return FT_THROW( Invalid_Argument );
2006-09-27 10:48:38 +02:00
}
library->lcd_filter = filter;
return FT_Err_Ok;
2006-09-27 10:48:38 +02:00
}
2006-09-29 23:31:53 +02:00
#else /* !FT_CONFIG_OPTION_SUBPIXEL_RENDERING */
2006-09-27 10:48:38 +02:00
FT_BASE( void )
ft_lcd_filter_fir( FT_Bitmap* bitmap,
FT_Render_Mode mode,
FT_LcdFiveTapFilter weights )
{
FT_UNUSED( bitmap );
FT_UNUSED( mode );
FT_UNUSED( weights );
}
FT_EXPORT_DEF( FT_Error )
FT_Library_SetLcdFilterWeights( FT_Library library,
unsigned char *weights )
{
FT_UNUSED( library );
FT_UNUSED( weights );
return FT_THROW( Unimplemented_Feature );
}
FT_EXPORT_DEF( FT_Error )
FT_Library_SetLcdFilter( FT_Library library,
FT_LcdFilter filter )
2006-09-27 10:48:38 +02:00
{
2006-09-29 23:31:53 +02:00
FT_UNUSED( library );
FT_UNUSED( filter );
2006-09-27 10:48:38 +02:00
return FT_THROW( Unimplemented_Feature );
2006-09-27 10:48:38 +02:00
}
2006-09-29 23:31:53 +02:00
#endif /* !FT_CONFIG_OPTION_SUBPIXEL_RENDERING */
2006-09-27 10:48:38 +02:00
2006-09-29 23:31:53 +02:00
/* END */