1 /***************************************************************************/ 2 /* */ 3 /* ftbitmap.c */ 4 /* */ 5 /* FreeType utility functions for bitmaps (body). */ 6 /* */ 7 /* Copyright 2004-2009, 2011 by */ 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9 /* */ 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. */ 15 /* */ 16 /***************************************************************************/ 17 18 19 #include <ft2build.h> 20 #include FT_BITMAP_H 21 #include FT_IMAGE_H 22 #include FT_INTERNAL_OBJECTS_H 23 24 25 static 26 const FT_Bitmap null_bitmap = { 0, 0, 0, 0, 0, 0, 0, 0 }; 27 28 29 /* documentation is in ftbitmap.h */ 30 31 FT_EXPORT_DEF( void ) FT_Bitmap_New(FT_Bitmap * abitmap)32 FT_Bitmap_New( FT_Bitmap *abitmap ) 33 { 34 *abitmap = null_bitmap; 35 } 36 37 38 /* documentation is in ftbitmap.h */ 39 40 FT_EXPORT_DEF( FT_Error ) FT_Bitmap_Copy(FT_Library library,const FT_Bitmap * source,FT_Bitmap * target)41 FT_Bitmap_Copy( FT_Library library, 42 const FT_Bitmap *source, 43 FT_Bitmap *target) 44 { 45 FT_Memory memory = library->memory; 46 FT_Error error = FT_Err_Ok; 47 FT_Int pitch = source->pitch; 48 FT_ULong size; 49 50 51 if ( source == target ) 52 return FT_Err_Ok; 53 54 if ( source->buffer == NULL ) 55 { 56 *target = *source; 57 58 return FT_Err_Ok; 59 } 60 61 if ( pitch < 0 ) 62 pitch = -pitch; 63 size = (FT_ULong)( pitch * source->rows ); 64 65 if ( target->buffer ) 66 { 67 FT_Int target_pitch = target->pitch; 68 FT_ULong target_size; 69 70 71 if ( target_pitch < 0 ) 72 target_pitch = -target_pitch; 73 target_size = (FT_ULong)( target_pitch * target->rows ); 74 75 if ( target_size != size ) 76 (void)FT_QREALLOC( target->buffer, target_size, size ); 77 } 78 else 79 (void)FT_QALLOC( target->buffer, size ); 80 81 if ( !error ) 82 { 83 unsigned char *p; 84 85 86 p = target->buffer; 87 *target = *source; 88 target->buffer = p; 89 90 FT_MEM_COPY( target->buffer, source->buffer, size ); 91 } 92 93 return error; 94 } 95 96 97 static FT_Error ft_bitmap_assure_buffer(FT_Memory memory,FT_Bitmap * bitmap,FT_UInt xpixels,FT_UInt ypixels)98 ft_bitmap_assure_buffer( FT_Memory memory, 99 FT_Bitmap* bitmap, 100 FT_UInt xpixels, 101 FT_UInt ypixels ) 102 { 103 FT_Error error; 104 int pitch; 105 int new_pitch; 106 FT_UInt bpp; 107 FT_Int i, width, height; 108 unsigned char* buffer = NULL; 109 110 111 width = bitmap->width; 112 height = bitmap->rows; 113 pitch = bitmap->pitch; 114 if ( pitch < 0 ) 115 pitch = -pitch; 116 117 switch ( bitmap->pixel_mode ) 118 { 119 case FT_PIXEL_MODE_MONO: 120 bpp = 1; 121 new_pitch = ( width + xpixels + 7 ) >> 3; 122 break; 123 case FT_PIXEL_MODE_GRAY2: 124 bpp = 2; 125 new_pitch = ( width + xpixels + 3 ) >> 2; 126 break; 127 case FT_PIXEL_MODE_GRAY4: 128 bpp = 4; 129 new_pitch = ( width + xpixels + 1 ) >> 1; 130 break; 131 case FT_PIXEL_MODE_GRAY: 132 case FT_PIXEL_MODE_LCD: 133 case FT_PIXEL_MODE_LCD_V: 134 bpp = 8; 135 new_pitch = ( width + xpixels ); 136 break; 137 default: 138 return FT_Err_Invalid_Glyph_Format; 139 } 140 141 /* if no need to allocate memory */ 142 if ( ypixels == 0 && new_pitch <= pitch ) 143 { 144 /* zero the padding */ 145 FT_Int bit_width = pitch * 8; 146 FT_Int bit_last = ( width + xpixels ) * bpp; 147 148 149 if ( bit_last < bit_width ) 150 { 151 FT_Byte* line = bitmap->buffer + ( bit_last >> 3 ); 152 FT_Byte* end = bitmap->buffer + pitch; 153 FT_Int shift = bit_last & 7; 154 FT_UInt mask = 0xFF00U >> shift; 155 FT_Int count = height; 156 157 158 for ( ; count > 0; count--, line += pitch, end += pitch ) 159 { 160 FT_Byte* write = line; 161 162 163 if ( shift > 0 ) 164 { 165 write[0] = (FT_Byte)( write[0] & mask ); 166 write++; 167 } 168 if ( write < end ) 169 FT_MEM_ZERO( write, end-write ); 170 } 171 } 172 173 return FT_Err_Ok; 174 } 175 176 if ( FT_QALLOC_MULT( buffer, new_pitch, bitmap->rows + ypixels ) ) 177 return error; 178 179 if ( bitmap->pitch > 0 ) 180 { 181 FT_Int len = ( width * bpp + 7 ) >> 3; 182 183 184 for ( i = 0; i < bitmap->rows; i++ ) 185 FT_MEM_COPY( buffer + new_pitch * ( ypixels + i ), 186 bitmap->buffer + pitch * i, len ); 187 } 188 else 189 { 190 FT_Int len = ( width * bpp + 7 ) >> 3; 191 192 193 for ( i = 0; i < bitmap->rows; i++ ) 194 FT_MEM_COPY( buffer + new_pitch * i, 195 bitmap->buffer + pitch * i, len ); 196 } 197 198 FT_FREE( bitmap->buffer ); 199 bitmap->buffer = buffer; 200 201 if ( bitmap->pitch < 0 ) 202 new_pitch = -new_pitch; 203 204 /* set pitch only, width and height are left untouched */ 205 bitmap->pitch = new_pitch; 206 207 return FT_Err_Ok; 208 } 209 210 211 /* documentation is in ftbitmap.h */ 212 213 FT_EXPORT_DEF( FT_Error ) FT_Bitmap_Embolden(FT_Library library,FT_Bitmap * bitmap,FT_Pos xStrength,FT_Pos yStrength)214 FT_Bitmap_Embolden( FT_Library library, 215 FT_Bitmap* bitmap, 216 FT_Pos xStrength, 217 FT_Pos yStrength ) 218 { 219 FT_Error error; 220 unsigned char* p; 221 FT_Int i, x, y, pitch; 222 FT_Int xstr, ystr; 223 224 225 if ( !library ) 226 return FT_Err_Invalid_Library_Handle; 227 228 if ( !bitmap || !bitmap->buffer ) 229 return FT_Err_Invalid_Argument; 230 231 if ( ( ( FT_PIX_ROUND( xStrength ) >> 6 ) > FT_INT_MAX ) || 232 ( ( FT_PIX_ROUND( yStrength ) >> 6 ) > FT_INT_MAX ) ) 233 return FT_Err_Invalid_Argument; 234 235 xstr = (FT_Int)FT_PIX_ROUND( xStrength ) >> 6; 236 ystr = (FT_Int)FT_PIX_ROUND( yStrength ) >> 6; 237 238 if ( xstr == 0 && ystr == 0 ) 239 return FT_Err_Ok; 240 else if ( xstr < 0 || ystr < 0 ) 241 return FT_Err_Invalid_Argument; 242 243 switch ( bitmap->pixel_mode ) 244 { 245 case FT_PIXEL_MODE_GRAY2: 246 case FT_PIXEL_MODE_GRAY4: 247 { 248 FT_Bitmap tmp; 249 FT_Int align; 250 251 252 if ( bitmap->pixel_mode == FT_PIXEL_MODE_GRAY2 ) 253 align = ( bitmap->width + xstr + 3 ) / 4; 254 else 255 align = ( bitmap->width + xstr + 1 ) / 2; 256 257 FT_Bitmap_New( &tmp ); 258 259 error = FT_Bitmap_Convert( library, bitmap, &tmp, align ); 260 if ( error ) 261 return error; 262 263 FT_Bitmap_Done( library, bitmap ); 264 *bitmap = tmp; 265 } 266 break; 267 268 case FT_PIXEL_MODE_MONO: 269 if ( xstr > 8 ) 270 xstr = 8; 271 break; 272 273 case FT_PIXEL_MODE_LCD: 274 xstr *= 3; 275 break; 276 277 case FT_PIXEL_MODE_LCD_V: 278 ystr *= 3; 279 break; 280 } 281 282 error = ft_bitmap_assure_buffer( library->memory, bitmap, xstr, ystr ); 283 if ( error ) 284 return error; 285 286 pitch = bitmap->pitch; 287 if ( pitch > 0 ) 288 p = bitmap->buffer + pitch * ystr; 289 else 290 { 291 pitch = -pitch; 292 p = bitmap->buffer + pitch * ( bitmap->rows - 1 ); 293 } 294 295 /* for each row */ 296 for ( y = 0; y < bitmap->rows ; y++ ) 297 { 298 /* 299 * Horizontally: 300 * 301 * From the last pixel on, make each pixel or'ed with the 302 * `xstr' pixels before it. 303 */ 304 for ( x = pitch - 1; x >= 0; x-- ) 305 { 306 unsigned char tmp; 307 308 309 tmp = p[x]; 310 for ( i = 1; i <= xstr; i++ ) 311 { 312 if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO ) 313 { 314 p[x] |= tmp >> i; 315 316 /* the maximum value of 8 for `xstr' comes from here */ 317 if ( x > 0 ) 318 p[x] |= p[x - 1] << ( 8 - i ); 319 320 #if 0 321 if ( p[x] == 0xff ) 322 break; 323 #endif 324 } 325 else 326 { 327 if ( x - i >= 0 ) 328 { 329 if ( p[x] + p[x - i] > bitmap->num_grays - 1 ) 330 { 331 p[x] = (unsigned char)(bitmap->num_grays - 1); 332 break; 333 } 334 else 335 { 336 p[x] = (unsigned char)(p[x] + p[x-i]); 337 if ( p[x] == bitmap->num_grays - 1 ) 338 break; 339 } 340 } 341 else 342 break; 343 } 344 } 345 } 346 347 /* 348 * Vertically: 349 * 350 * Make the above `ystr' rows or'ed with it. 351 */ 352 for ( x = 1; x <= ystr; x++ ) 353 { 354 unsigned char* q; 355 356 357 q = p - bitmap->pitch * x; 358 for ( i = 0; i < pitch; i++ ) 359 q[i] |= p[i]; 360 } 361 362 p += bitmap->pitch; 363 } 364 365 bitmap->width += xstr; 366 bitmap->rows += ystr; 367 368 return FT_Err_Ok; 369 } 370 371 372 /* documentation is in ftbitmap.h */ 373 374 FT_EXPORT_DEF( FT_Error ) FT_Bitmap_Convert(FT_Library library,const FT_Bitmap * source,FT_Bitmap * target,FT_Int alignment)375 FT_Bitmap_Convert( FT_Library library, 376 const FT_Bitmap *source, 377 FT_Bitmap *target, 378 FT_Int alignment ) 379 { 380 FT_Error error = FT_Err_Ok; 381 FT_Memory memory; 382 383 384 if ( !library ) 385 return FT_Err_Invalid_Library_Handle; 386 387 memory = library->memory; 388 389 switch ( source->pixel_mode ) 390 { 391 case FT_PIXEL_MODE_MONO: 392 case FT_PIXEL_MODE_GRAY: 393 case FT_PIXEL_MODE_GRAY2: 394 case FT_PIXEL_MODE_GRAY4: 395 case FT_PIXEL_MODE_LCD: 396 case FT_PIXEL_MODE_LCD_V: 397 { 398 FT_Int pad; 399 FT_Long old_size; 400 401 402 old_size = target->rows * target->pitch; 403 if ( old_size < 0 ) 404 old_size = -old_size; 405 406 target->pixel_mode = FT_PIXEL_MODE_GRAY; 407 target->rows = source->rows; 408 target->width = source->width; 409 410 pad = 0; 411 if ( alignment > 0 ) 412 { 413 pad = source->width % alignment; 414 if ( pad != 0 ) 415 pad = alignment - pad; 416 } 417 418 target->pitch = source->width + pad; 419 420 if ( target->pitch > 0 && 421 (FT_ULong)target->rows > FT_ULONG_MAX / target->pitch ) 422 return FT_Err_Invalid_Argument; 423 424 if ( target->rows * target->pitch > old_size && 425 FT_QREALLOC( target->buffer, 426 old_size, target->rows * target->pitch ) ) 427 return error; 428 } 429 break; 430 431 default: 432 error = FT_Err_Invalid_Argument; 433 } 434 435 switch ( source->pixel_mode ) 436 { 437 case FT_PIXEL_MODE_MONO: 438 { 439 FT_Byte* s = source->buffer; 440 FT_Byte* t = target->buffer; 441 FT_Int i; 442 443 444 target->num_grays = 2; 445 446 for ( i = source->rows; i > 0; i-- ) 447 { 448 FT_Byte* ss = s; 449 FT_Byte* tt = t; 450 FT_Int j; 451 452 453 /* get the full bytes */ 454 for ( j = source->width >> 3; j > 0; j-- ) 455 { 456 FT_Int val = ss[0]; /* avoid a byte->int cast on each line */ 457 458 459 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7 ); 460 tt[1] = (FT_Byte)( ( val & 0x40 ) >> 6 ); 461 tt[2] = (FT_Byte)( ( val & 0x20 ) >> 5 ); 462 tt[3] = (FT_Byte)( ( val & 0x10 ) >> 4 ); 463 tt[4] = (FT_Byte)( ( val & 0x08 ) >> 3 ); 464 tt[5] = (FT_Byte)( ( val & 0x04 ) >> 2 ); 465 tt[6] = (FT_Byte)( ( val & 0x02 ) >> 1 ); 466 tt[7] = (FT_Byte)( val & 0x01 ); 467 468 tt += 8; 469 ss += 1; 470 } 471 472 /* get remaining pixels (if any) */ 473 j = source->width & 7; 474 if ( j > 0 ) 475 { 476 FT_Int val = *ss; 477 478 479 for ( ; j > 0; j-- ) 480 { 481 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7); 482 val <<= 1; 483 tt += 1; 484 } 485 } 486 487 s += source->pitch; 488 t += target->pitch; 489 } 490 } 491 break; 492 493 494 case FT_PIXEL_MODE_GRAY: 495 case FT_PIXEL_MODE_LCD: 496 case FT_PIXEL_MODE_LCD_V: 497 { 498 FT_Int width = source->width; 499 FT_Byte* s = source->buffer; 500 FT_Byte* t = target->buffer; 501 FT_Int s_pitch = source->pitch; 502 FT_Int t_pitch = target->pitch; 503 FT_Int i; 504 505 506 target->num_grays = 256; 507 508 for ( i = source->rows; i > 0; i-- ) 509 { 510 FT_ARRAY_COPY( t, s, width ); 511 512 s += s_pitch; 513 t += t_pitch; 514 } 515 } 516 break; 517 518 519 case FT_PIXEL_MODE_GRAY2: 520 { 521 FT_Byte* s = source->buffer; 522 FT_Byte* t = target->buffer; 523 FT_Int i; 524 525 526 target->num_grays = 4; 527 528 for ( i = source->rows; i > 0; i-- ) 529 { 530 FT_Byte* ss = s; 531 FT_Byte* tt = t; 532 FT_Int j; 533 534 535 /* get the full bytes */ 536 for ( j = source->width >> 2; j > 0; j-- ) 537 { 538 FT_Int val = ss[0]; 539 540 541 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 ); 542 tt[1] = (FT_Byte)( ( val & 0x30 ) >> 4 ); 543 tt[2] = (FT_Byte)( ( val & 0x0C ) >> 2 ); 544 tt[3] = (FT_Byte)( ( val & 0x03 ) ); 545 546 ss += 1; 547 tt += 4; 548 } 549 550 j = source->width & 3; 551 if ( j > 0 ) 552 { 553 FT_Int val = ss[0]; 554 555 556 for ( ; j > 0; j-- ) 557 { 558 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 ); 559 val <<= 2; 560 tt += 1; 561 } 562 } 563 564 s += source->pitch; 565 t += target->pitch; 566 } 567 } 568 break; 569 570 571 case FT_PIXEL_MODE_GRAY4: 572 { 573 FT_Byte* s = source->buffer; 574 FT_Byte* t = target->buffer; 575 FT_Int i; 576 577 578 target->num_grays = 16; 579 580 for ( i = source->rows; i > 0; i-- ) 581 { 582 FT_Byte* ss = s; 583 FT_Byte* tt = t; 584 FT_Int j; 585 586 587 /* get the full bytes */ 588 for ( j = source->width >> 1; j > 0; j-- ) 589 { 590 FT_Int val = ss[0]; 591 592 593 tt[0] = (FT_Byte)( ( val & 0xF0 ) >> 4 ); 594 tt[1] = (FT_Byte)( ( val & 0x0F ) ); 595 596 ss += 1; 597 tt += 2; 598 } 599 600 if ( source->width & 1 ) 601 tt[0] = (FT_Byte)( ( ss[0] & 0xF0 ) >> 4 ); 602 603 s += source->pitch; 604 t += target->pitch; 605 } 606 } 607 break; 608 609 610 default: 611 ; 612 } 613 614 return error; 615 } 616 617 618 /* documentation is in ftbitmap.h */ 619 620 FT_EXPORT_DEF( FT_Error ) FT_GlyphSlot_Own_Bitmap(FT_GlyphSlot slot)621 FT_GlyphSlot_Own_Bitmap( FT_GlyphSlot slot ) 622 { 623 if ( slot && slot->format == FT_GLYPH_FORMAT_BITMAP && 624 !( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) ) 625 { 626 FT_Bitmap bitmap; 627 FT_Error error; 628 629 630 FT_Bitmap_New( &bitmap ); 631 error = FT_Bitmap_Copy( slot->library, &slot->bitmap, &bitmap ); 632 if ( error ) 633 return error; 634 635 slot->bitmap = bitmap; 636 slot->internal->flags |= FT_GLYPH_OWN_BITMAP; 637 } 638 639 return FT_Err_Ok; 640 } 641 642 643 /* documentation is in ftbitmap.h */ 644 645 FT_EXPORT_DEF( FT_Error ) FT_Bitmap_Done(FT_Library library,FT_Bitmap * bitmap)646 FT_Bitmap_Done( FT_Library library, 647 FT_Bitmap *bitmap ) 648 { 649 FT_Memory memory; 650 651 652 if ( !library ) 653 return FT_Err_Invalid_Library_Handle; 654 655 if ( !bitmap ) 656 return FT_Err_Invalid_Argument; 657 658 memory = library->memory; 659 660 FT_FREE( bitmap->buffer ); 661 *bitmap = null_bitmap; 662 663 return FT_Err_Ok; 664 } 665 666 667 /* END */ 668