Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DrawTextWx.cpp
1 #include <wx/wx.h>
2 
3 #include <Base/Common/BIASpragma.hh>
4 #include <Gui/DrawTextWx.hh>
5 #include <Base/Image/Image.hh>
6 #include <Base/Image/ColourRGB.hh>
7 #include <Gui/StringConv.hh>
8 
9 
10 using namespace BIAS;
11 using namespace std;
12 
13 template <class StorageType>
15 GetTextExtend(const std::string &text,
16  unsigned &width, unsigned &height,
17  const unsigned point_size)
18 {
19  wxBitmap bmp(10, 10);
20  wxMemoryDC dc(bmp);
21  if (!dc.IsOk()){
22  BIASERR("DrawTextWx::Text(): wxMemoryDC is not OK\n");
23  return DTWX_WX_ERROR;
24  }
25  wxFont font = dc.GetFont();
26  font.SetPointSize(point_size);
27  dc.SetFont(font);
28 
29  int res = DTWX_OK;
30  wxString str = AsciiToWx(text);
31 #ifdef WIN32
32  wxCoord x,y;
33  dc.GetTextExtent(str,&x,&y);
34  width = x;
35  height =y;
36 #else
37  wxSize size = dc.GetTextExtent(str);
38  width = size.GetWidth();
39  height = size.GetHeight();
40 #endif
41  return res;
42 }
43 
44 
45 template <class StorageType>
47 Text(Image<StorageType> &im, const std::string &text,
48  const unsigned &pos_x, const unsigned &pos_y,
49  const ColourRGB<StorageType> &foreground_color,
50  const ColourRGB<StorageType> &background_color,
51  const unsigned point_size,
52  const enum EVerticalAlign vertical_align,
53  const enum EHorizontalAlign horizontal_align)
54 {
55  if (im.GetChannelCount()!=3){
56  BIASERR("DrawTextWx::Text(): Cannot draw text in single channel image.\n");
57  return DTWX_INVALID_ARGUMENT;
58  }
60  BIASERR("DrawTextWx::Text(): Works only with RGB images.\n");
61  return DTWX_INVALID_ARGUMENT;
62  }
63 
64  const bool static_data = true;
65  wxImage wximg(im.GetWidth(), im.GetHeight(),
66  im.GetImageData(), static_data);
67  if (!wximg.IsOk()){
68  BIASERR("DrawTextWx::Text(): wxImage is not OK\n");
69  return DTWX_WX_ERROR;
70  }
71  // the following line core dumps in console applications, because wx is
72  // not competely initialized. It is absolutly necessary to derive from
73  // the class wxApp and implement teh function OnInit() to use this class
74  wxBitmap bmp(wximg);
75  if (!bmp.IsOk()){
76  BIASERR("DrawTextWx::Text(): wxBitmap is not OK\n");
77  return DTWX_WX_ERROR;
78  }
79  wxMemoryDC dc(bmp);
80  if (!dc.IsOk()){
81  BIASERR("DrawTextWx::Text(): wxMemoryDC is not OK\n");
82  return DTWX_WX_ERROR;
83  }
84 
85  dc.SetTextForeground(wxColor(foreground_color[0],
86  foreground_color[1],
87  foreground_color[2]));
88  dc.SetTextBackground(wxColour(background_color[0],
89  background_color[1],
90  background_color[2]));
91 
92  wxFont font = dc.GetFont();
93  font.SetPointSize(point_size);
94  dc.SetFont(font);
95 
96  int res = DTWX_OK;
97  wxString str = AsciiToWx(text);
98 #ifdef WIN32
99  wxCoord w,h;
100  dc.GetTextExtent(str,&w,&h);
101  wxSize size(w,h);
102 #else
103  wxSize size = dc.GetTextExtent(str);
104 #endif
105  int px = 0, py = 0;
106  switch (vertical_align){
107  case V_ALIGN_TOP: py = (int)pos_y; break;
108  case V_ALIGN_CENTER: py = (int)pos_y - size.GetHeight()/2; break;
109  case V_ALIGN_BOTTOM: py = (int)pos_y - size.GetHeight(); break;
110  default: cerr << "unkown vertical alignment: "<<vertical_align<<endl;
111  return DTWX_INVALID_ARGUMENT; break;
112  }
113  if (py<0 || py+size.GetHeight()>=(int)im.GetHeight()){
114  res = DTWX_OUT_OF_IMAGE;
115  }
116  switch (horizontal_align){
117  case H_ALIGN_LEFT: px = (int)pos_x; break;
118  case H_ALIGN_CENTER: px = (int)pos_x - size.GetWidth()/2; break;
119  case H_ALIGN_RIGHT: px = (int)pos_x - size.GetWidth(); break;
120  default: cerr << "unkown horizontal alignment: "<<horizontal_align<<endl;
121  return DTWX_INVALID_ARGUMENT; break;
122  }
123  if (px<0 || px+size.GetWidth()>=(int)im.GetWidth()){
124  res = DTWX_OUT_OF_IMAGE;
125  }
126  dc.DrawText(str, px, py);
127 
128  dc.SelectObject(wxNullBitmap);
129 
130  wxImage dst_img = bmp.ConvertToImage();
131 
132  const unsigned img_size = im.GetSizeByte();
133  memcpy(im.GetImageData(), dst_img.GetData(), img_size);
134 
135  return res;
136 }
137 
138 
139 ///////////////////////////////////
140 // instantiation
141 ///////////////////////////////////
142 
143 #define INST(type) template class BIASGui_EXPORT DrawTextWx<type>
144 namespace BIAS{
145 // create instances
146 INST(unsigned char);
147 // INST(float);
148 // #ifdef BUILD_IMAGE_INT
149 // INST(int);
150 // #endif
151 // #ifdef BUILD_IMAGE_CHAR
152 // INST(char);
153 // #endif
154 // #ifdef BUILD_IMAGE_SHORT
155 // INST(short);
156 // #endif
157 // #if defined(BUILD_IMAGE_USHORT)
158 // INST(unsigned short);
159 // #endif
160 // #ifdef BUILD_IMAGE_DOUBLE
161 // INST(double);
162 // #endif
163 // #ifdef BUILD_IMAGE_UINT
164 // INST(unsigned int);
165 // #endif
166 }
167 #undef INST
static int GetTextExtend(const std::string &text, unsigned &width, unsigned &height, const unsigned point_size=8)
returns size of the bounding box of the text
Definition: DrawTextWx.cpp:15
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
static int Text(Image< StorageType > &im, const std::string &text, const unsigned &pos_x, const unsigned &pos_y, const ColourRGB< StorageType > &foreground_color=COLOR_WHITE, const ColourRGB< StorageType > &background_color=COLOR_BLACK, const unsigned point_size=8, const enum EVerticalAlign vertical_align=V_ALIGN_TOP, const enum EHorizontalAlign horizontal_align=H_ALIGN_LEFT)
write text in an image !!! Can only be used in wx-GUI applications which implement wxApp !!! ...
Definition: DrawTextWx.cpp:47
unsigned int GetSizeByte() const
returns the nr.
Definition: ImageBase.hh:352
interface class used to ease handover in function calls
Definition: ColourRGB.hh:34
unsigned int GetWidth() const
Definition: ImageBase.hh:312
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
unsigned int GetHeight() const
Definition: ImageBase.hh:319
INST(unsigned char)
The image template class for specific storage types.
Definition: Image.hh:78
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407