Index: ftstring.c =================================================================== RCS file: /cvs/freetype/ft2demos/src/ftstring.c,v retrieving revision 1.10 diff -c -r1.10 ftstring.c *** ftstring.c 2002/04/28 02:48:45 1.10 --- ftstring.c 2002/06/08 06:53:46 *************** *** 55,60 **** --- 55,61 ---- static int antialias = 1; /* is anti-aliasing active ? */ static int use_sbits = 0; /* do we use embedded bitmaps ? */ static int kerning = 1; + static int use_gamma = 1; static int res = 72; /* default resolution in dpi */ *************** *** 162,167 **** --- 163,213 ---- static TGlyph glyphs[ MAX_GLYPHS ]; static int num_glyphs; + /*************************************************************************/ + /* */ + /* Gamma correction. */ + /* */ + /* */ + static double gamma_value = 2.0f; + static FT_Byte gamma_ramp[256]; + + static void + init_gamma( void ) + { + int i; + double gamma_inv = 1.0f / gamma_value; + + for (i = 0; i < 256; i++) + gamma_ramp[i] = (FT_Byte)( pow((double)i / 256.0f, gamma_inv) * 256.0f ); + } + + static void + apply_gamma( grBitmap* bmp ) + { + int i, j; + char* buffer = bmp->buffer; + + for (i = 0; i < bmp->rows; i++) + { + for (j = 0; j < bmp->width; j++) + /* bitmap color is the inverse of coverage values, hence the '255-x' */ + buffer[j] = 255 - gamma_ramp[255 - (FT_Byte)buffer[j]]; + + buffer += bmp->pitch; + } + } + + static void + draw_gamma_ramp( void ) + { + int i, x, y; + + x = (bit.width - 256) / 2; + y = (bit.rows + 256) / 2; + for (i = 0; i < 256; i++, x++) + bit.buffer[bit.pitch*(y - gamma_ramp[i]) + x] = 80; + } + /*************************************************************************/ /* */ *************** *** 384,389 **** --- 430,437 ---- case ft_pixel_mode_grays: bit3.mode = gr_pixel_mode_gray; bit3.grays = source->num_grays; + if (use_gamma) + apply_gamma(&bit3); break; default: *************** *** 484,489 **** --- 532,538 ---- grWriteln(" a : toggle anti-aliasing" ); grWriteln(" h : toggle outline hinting" ); grWriteln(" k : toggle kerning" ); + grWriteln(" g : toggle gamma correction" ); grLn(); grWriteln(" Up : increase pointsize by 1 unit" ); grWriteln(" Down : decrease pointsize by 1 unit" ); *************** *** 495,500 **** --- 544,552 ---- grWriteln(" F7 : big rotate counter-clockwise"); grWriteln(" F8 : big rotate clockwise"); grLn(); + grWriteln(" F9 : decrease gamma by 0.1" ); + grWriteln(" F10 : increase gamma by 0.1" ); + grLn(); grWriteln("press any key to exit this help screen"); grRefreshSurface( surface ); *************** *** 550,555 **** --- 602,614 ---- : (char *)"glyph hinting is now ignored"; break; + case grKEY( 'g' ): + use_gamma = !use_gamma; + new_header = use_gamma + ? (char *)"gamma correction is now on" + : (char *)"gamma correction is now off"; + return 1; + case grKeyF1: case grKEY( '?' ): Help(); *************** *** 571,576 **** --- 630,638 ---- case grKeyRight: i = 1; goto Do_Rotate; case grKeyF7: i = -10; goto Do_Rotate; case grKeyF8: i = 10; goto Do_Rotate; + + case grKeyF9 : if (gamma_value > 0.1f) gamma_value -= 0.1f; goto Do_Gamma_Set; + case grKeyF10: gamma_value += 0.1f; goto Do_Gamma_Set; default: ; } *************** *** 580,585 **** --- 642,657 ---- Rotation = ( Rotation + i ) & 127; return 1; + Do_Gamma_Set: + { + static char header_buffer[64]; + + sprintf(header_buffer, "gamma is %.1f", gamma_value); + new_header = header_buffer; + init_gamma(); + return 1; + } + Do_Scale: ptsize += i; if ( ptsize < 1 ) ptsize = 1; *************** *** 731,736 **** --- 803,810 ---- goto Display_Font; Success: + init_gamma(); + /* prepare the text to be rendered */ prepare_text( (unsigned char*)Text ); *************** *** 773,778 **** --- 847,854 ---- { reset_transform(); layout_glyphs(); + if (use_gamma) + draw_gamma_ramp(); render_string( bit.width/2, bit.rows/2 ); }