iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TextImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_IMAGEPROC_DRAWING_DETAILS_TEXTIMPL_H
36 #define INCLUDE_NRT_IMAGEPROC_DRAWING_DETAILS_TEXTIMPL_H
37 
38 #include <algorithm>
39 #include <type_traits>
40 
41 template <class T, template <typename> class PixDest, class U, template <typename> class FGColorT,
42  class V, template <typename> class BGColorT>
43 void nrt::drawText(nrt::Image<PixDest<T> > & image, nrt::Point2D<nrt::int32> const& pt, std::string const& text,
44  FGColorT<U> const& foreground, BGColorT<U> const& background, nrt::SimpleFont const& font,
45  nrt::TextAnchor const anchor, bool transparentBG)
46 {
47  nrt::PixelPainter<T, PixDest, U, FGColorT> fgPainter(foreground);
48  nrt::PixelPainter<T, PixDest, U, BGColorT> bgPainter(background);
49 
50  int const textwidth = text.length() * font.w();
51  int const textheight = font.h();
52 
53  nrt::Point2D<int32> top_left=pt;
54  switch(anchor)
55  {
56  case nrt::TextAnchor::BOTTOM_RIGHT : top_left = Point2D<int32>(pt - Point2D<int32>(textwidth, textheight)); break;
57  case nrt::TextAnchor::BOTTOM_LEFT : top_left = Point2D<int32>(pt - Point2D<int32>(0, textheight)); break;
58  case nrt::TextAnchor::TOP_RIGHT : top_left = Point2D<int32>(pt - Point2D<int32>(textwidth, 0)); break;
59  case nrt::TextAnchor::TOP_LEFT : top_left = pt; break;
60  }
61 
62  PixDest<T> * const begin = image.begin();
63  nrt::int32 w = image.width();
64 
65  nrt::Point2D<int32> p = top_left; // copy for modif
66  const int ww = image.width(), hh = image.height();
67 
68  for (uint i = 0; i < text.length(); i ++)
69  {
70  const unsigned char *ptr = font.charptr(text[i]);
71 
72  for (int y = 0; y < int(font.h()); y ++)
73  for (int x = 0; x < int(font.w()); x ++)
74  if (p.x() + x >= 0 && p.x() + x < ww && p.y() + y >= 0 && p.y() + y < hh)
75  {
76  if (!ptr[y * font.w() + x]) fgPainter.paint(begin + p.x()+x + (p.y()+y)*w);
77  else if (!transparentBG) bgPainter.paint(begin + p.x()+x + (p.y()+y)*w);
78  }
79  p.x() += font.w();
80  }
81 }
82 
83 #endif // INCLUDE_NRT_IMAGEPROC_DRAWING_DETAILS_TEXTIMPL_H