/ftype2.cpp

https://github.com/tnzk/shineshockd · C++ · 79 lines · 63 code · 16 blank · 0 comment · 2 complexity · c1ce6df3f07b5b5cf7c3aa21c1e66ee0 MD5 · raw file

  1. #include <string.h>
  2. #include <iostream>
  3. #include <ft2build.h>
  4. #include <opencv/cv.h>
  5. #include <opencv/highgui.h>
  6. #include FT_FREETYPE_H
  7. #include "utf8decoder.h"
  8. using namespace std;
  9. using namespace cv;
  10. int main( int argc, char** argv){
  11. FT_Library library;
  12. FT_Face face;
  13. FT_GlyphSlot slot;
  14. FT_UInt glyph_index;
  15. FT_Error error;
  16. char text[] = "a日b本語テスト彳";
  17. char filename[] = "test.bmp";
  18. int pen_x, pen_y, n;
  19. int num_chars = (int)strlen( text);
  20. unsigned char fcr = 0;
  21. unsigned char fcg = 0;
  22. unsigned char fcb = 0;
  23. FT_Init_FreeType( &library );
  24. FT_New_Face( library, "mplus-1c-heavy.ttf", 0, &face );
  25. slot = face->glyph;
  26. FT_Set_Char_Size( face, 0, 16 * 64, 300, 300);
  27. int cpos_x = 50;
  28. int cpos_y = 60;
  29. int cpos_w = 640;
  30. int cpos_h = 50;
  31. IplImage* img = cvLoadImage("image.bmp", CV_LOAD_IMAGE_COLOR);
  32. IplImage* dst = cvCloneImage(img);
  33. Utf8Decoder u8d(text, num_chars);
  34. pen_x = 300;
  35. pen_y = 200;
  36. for ( n = 0;n < u8d.length(); n++ ){
  37. int i;
  38. FT_Bitmap bitmap;
  39. long int unicode_index = u8d.get(n);
  40. FT_Load_Char( face, unicode_index, FT_LOAD_RENDER);
  41. bitmap = slot->bitmap;
  42. int offset_y = cpos_h - bitmap.rows + cpos_y;
  43. for( i = 0; i < bitmap.rows * bitmap.width; i++){
  44. int x = ( i % bitmap.width) + cpos_x;
  45. int y = offset_y + ( i / bitmap.width);
  46. unsigned char* src_r = (unsigned char*)(dst->imageData + dst->widthStep * y + x * 3);
  47. unsigned char* src_g = (unsigned char*)(dst->imageData + dst->widthStep * y + x * 3 + 1);
  48. unsigned char* src_b = (unsigned char*)(dst->imageData + dst->widthStep * y + x * 3 + 2);
  49. double gph_a = (double)bitmap.buffer[i] / 255.0;
  50. double src_a = 1.0 - gph_a;
  51. int tmp;
  52. tmp = fcr * gph_a + *src_r * src_a;
  53. *src_r = (tmp > 255) ? 255 : tmp;
  54. tmp = fcg * gph_a + *src_g * src_a;
  55. *src_g = (tmp > 255) ? 255 : tmp;
  56. tmp = fcb * gph_a + *src_b * src_a;
  57. *src_b = (tmp > 255) ? 255 : tmp;
  58. }
  59. cpos_x += bitmap.width + 5;
  60. pen_x += slot->advance.x >> 6;
  61. }
  62. cvSaveImage( "ftyperesult.png", dst);
  63. }