33 #ifndef __INCLUDE_MIST_DRAWING__
34 #define __INCLUDE_MIST_DRAWING__
37 #ifndef __INCLUDE_MIST_H__
41 #ifndef __INCLUDE_BITMAP_H__
47 #ifdef MIST_USE_DRAW_TEXT
49 #include FT_FREETYPE_H
80 template <
class T,
class Allocator >
89 difference_type dx, dy;
90 difference_type sx, sy;
91 difference_type x, y, e, i;
92 difference_type w =
static_cast< difference_type
>( image.
width( ) );
93 difference_type h =
static_cast< difference_type
>( image.
height( ) );
95 dx = std::abs( static_cast< int >( x2 - x1 ) );
96 dy = std::abs( static_cast< int >( y2 - y1 ) );
97 sx = ( x2 - x1 >= 0 ) ? 1 : -1;
98 sy = ( y2 - y1 >= 0 ) ? 1 : -1;
104 for( i = 0 ; i <= dx ; i++ )
106 if( x >= 0 && x < w && y >= 0 && y < h )
108 image( x, y ) =
color;
123 for( i = 0 ; i <= dy ; i++ )
125 if( x >= 0 && x < w && y >= 0 && y < h )
127 image( x, y ) =
color;
153 template <
size_t BITS >
162 difference_type dx, dy;
163 difference_type sx, sy;
164 difference_type x, y, e, i;
165 difference_type w =
static_cast< difference_type
>( image.
width( ) );
166 difference_type h =
static_cast< difference_type
>( image.
height( ) );
168 dx = std::abs( static_cast< int >( x2 - x1 ) );
169 dy = std::abs( static_cast< int >( y2 - y1 ) );
170 sx = ( x2 - x1 >= 0 ) ? 1 : -1;
171 sy = ( y2 - y1 >= 0 ) ? 1 : -1;
177 for( i = 0 ; i <= dx ; i++ )
179 if( x >= 0 && x < w && y >= 0 && y < h )
181 image( x, y ) =
color;
196 for( i = 0 ; i <= dy ; i++ )
198 if( x >= 0 && x < w && y >= 0 && y < h )
200 image( x, y ) =
color;
228 template <
class T,
class Allocator >
239 difference_type dx, dy, dz;
240 difference_type sx, sy, sz;
241 difference_type x, y, z, e1, e2, i;
242 difference_type w =
static_cast< difference_type
>( image.
width( ) );
243 difference_type h =
static_cast< difference_type
>( image.
height( ) );
244 difference_type d =
static_cast< difference_type
>( image.
depth( ) );
246 dx = std::abs( static_cast< int >( x2 - x1 ) );
247 dy = std::abs( static_cast< int >( y2 - y1 ) );
248 dz = std::abs( static_cast< int >( z2 - z1 ) );
249 sx = ( x2 - x1 >= 0 ) ? 1 : -1;
250 sy = ( y2 - y1 >= 0 ) ? 1 : -1;
251 sz = ( z2 - z1 >= 0 ) ? 1 : -1;
255 if( dx >= dy && dx >= dz )
258 for( i = 0 ; i <= dx ; i++ )
260 if( x >= 0 && x < w && y >= 0 && y < h && z >= 0 && z < d )
262 image( x, y, z ) =
color;
281 else if( dy >= dx && dy >= dz )
284 for( i = 0 ; i <= dy ; i++ )
286 if( x >= 0 && x < w && y >= 0 && y < h && z >= 0 && z < d )
288 image( x, y, z ) =
color;
310 for( i = 0 ; i <= dz ; i++ )
312 if( x >= 0 && x < w && y >= 0 && y < h && z >= 0 && z < d )
314 image( x, y, z ) =
color;
347 template <
class T,
class Allocator >
356 difference_type is = x - radius;
357 difference_type ie = x + radius;
358 difference_type js = y - radius;
359 difference_type je = y + radius;
361 difference_type w =
static_cast< difference_type
>( image.
width( ) );
362 difference_type h =
static_cast< difference_type
>( image.
height( ) );
402 for( difference_type j = js ; j <= je ; j++ )
404 for( difference_type i = is ; i <= ie ; i++ )
406 image( i, j ) = value;
423 template <
class T,
class Allocator >
433 difference_type is = x - radius;
434 difference_type ie = x + radius;
435 difference_type js = y - radius;
436 difference_type je = y + radius;
437 difference_type ks = z - radius;
438 difference_type ke = z + radius;
440 difference_type w =
static_cast< difference_type
>( image.
width( ) );
441 difference_type h =
static_cast< difference_type
>( image.
height( ) );
442 difference_type d =
static_cast< difference_type
>( image.
depth( ) );
501 for( difference_type k = ks ; k <= ke ; k++ )
503 for( difference_type j = js ; j <= je ; j++ )
505 for( difference_type i = is ; i <= ie ; i++ )
507 image( i, j, k ) = value;
518 template<
typename T,
typename Allocator >
527 if( x < 0 || y < 0 || x >= static_cast< difference_type >( image.
width( ) ) || y >= static_cast< difference_type >( image.
height( ) ) )
532 image( x, y ) = value;
541 template<
typename T,
typename Allocator >
558 for( dx = 0 ; dx <= dy ; ++dx )
566 d += 10 + 4 * dx - 4 * dy--;
569 set_pixel( image, cx + dy, cy + dx, value );
570 set_pixel( image, cx + dx, cy + dy, value );
571 set_pixel( image, cx - dx, cy + dy, value );
572 set_pixel( image, cx - dy, cy + dx, value );
573 set_pixel( image, cx - dy, cy - dx, value );
574 set_pixel( image, cx - dx, cy - dy, value );
575 set_pixel( image, cx + dx, cy - dy, value );
576 set_pixel( image, cx + dy, cy - dx, value );
587 template<
typename T,
typename Allocator >
595 draw_line( image, x0, y0, x1, y0, value );
596 draw_line( image, x0, y0, x0, y1, value );
597 draw_line( image, x1, y0, x1, y1, value );
598 draw_line( image, x0, y1, x1, y1, value );
608 template<
typename T,
typename Allocator >
618 x0 = ( 0 < x0 ) ? x0 : 0;
619 y0 = ( 0 < y0 ) ? y0 : 0;
620 x1 = ( x1 < static_cast< difference_type >( image.
width() - 1 ) ) ? x1 :
static_cast< difference_type
>( image.
width() - 1 );
621 y1 = ( y1 < static_cast< difference_type >( image.
height() - 1 ) ) ? y1 :
static_cast< difference_type
>( image.
height() - 1 );
623 for( difference_type y = y0 ; y <= y1 ; ++y )
625 for( difference_type x = x0 ; x <= x1 ; ++x )
627 image( x, y ) = value;
638 template<
typename T,
typename Allocator >
645 draw_line( image, cx - size / 2, cy, cx + size / 2, cy, value );
646 draw_line( image, cx, cy - size / 2, cx, cy + size / 2, value );
651 #ifdef MIST_USE_DRAW_TEXT
671 template <
typename T,
typename Allocator >
672 inline bool draw_text( array2< T, Allocator > &img,
677 typename array2< T, Allocator >::value_type color,
682 FT_Library library = NULL;
688 error = FT_Init_FreeType( &library );
695 error = FT_New_Face( library, filename, 0, &face );
702 error = FT_Set_Pixel_Sizes( face, 0, size );
710 for (
int i = 0; i < len; i++)
712 FT_UInt glyph_index = FT_Get_Char_Index( face, (FT_ULong)str[i] );
714 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
720 error = FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL );
726 for (
int x = 0; x < face->glyph->bitmap.
width; x++)
728 for (
int y = 0; y < face->glyph->bitmap.rows; y++)
730 int xx = x + pen_x + face->glyph->bitmap_left;
731 int yy = y + pen_y - face->glyph->bitmap_top;
732 if ( ( 0 <= xx ) && ( xx < (
int)img.width() ) && ( 0 <= yy ) && ( yy < (int)img.height() ) )
735 T img_value = img(xx, yy);
736 double str_value =
static_cast< double > ( face->glyph->bitmap.buffer[ x + y * face->glyph->bitmap.
width] ) / 255.0;
737 img(xx, yy) =
static_cast< typename array2<T>::value_type
> ( str_value * color + (1.0 - str_value) * img_value );
741 pen_x += face->glyph->advance.x >> 6;
746 FT_Done_FreeType(library);
757 FT_Done_FreeType(library);
784 template <
typename T,
typename Allocator >
785 inline bool draw_text( array2< T, Allocator > &img,
790 typename array2< T, Allocator >::value_type color,
793 int len = strlen(str);
795 unsigned long *str_ulong =
new unsigned long[len];
797 for (
int i = 0; i < len; i++)
799 str_ulong[i] = (FT_ULong)str[i];
802 bool result = draw_text( img, filename, px, py, size, color, str_ulong, len );
811 #endif // MIST_USE_DRAW_TEXT
822 #endif // __INCLUDE_MIST_DRAWING__