SMIL  1.0.4
DAAImageViewer.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _D_AA_IMAGE_VIEWER_HPP
31 #define _D_AA_IMAGE_VIEWER_HPP
32 
33 #include "Gui/include/private/DImageViewer.hpp"
34 #include "Core/include/DTypes.h"
35 #include "Base/include/private/DImageTransform.hpp"
36 
37 #include <aalib.h>
38 
39 
40 namespace smil
41 {
42  template <class T> class Image;
43 
48 
54  template <class T>
55  class AaImageViewer : public ImageViewer<T>
56  {
57  public:
58  typedef ImageViewer<T> parentClass;
59  AaImageViewer();
61  ~AaImageViewer();
62  virtual void hide();
63  virtual void show();
64  virtual bool isVisible();
65  virtual void setName(const char* _name);
66  virtual void clearOverlay() { }
67 
68  protected:
69  aa_context *context;
70  int createContext();
71  virtual void drawImage();
72  };
73 
74 
75  template <class T>
77  {
78  context = NULL;
79  }
80 
81  template <class T>
82  AaImageViewer<T>::AaImageViewer(Image<T> *im)
83  : ImageViewer<T>(im)
84  {
85  context = NULL;
86  }
87 
88  template <class T>
89  int AaImageViewer<T>::createContext()
90  {
91  context = aa_autoinit(&aa_defparams);
92  if(context == NULL)
93  {
94  fprintf(stderr,"Cannot initialize AA-lib. Sorry.\n");
95  return -1;
96  }
97  return 0;
98  }
99 
100  template <class T>
101  AaImageViewer<T>::~AaImageViewer()
102  {
103  hide();
104  }
105 
106 
107  template <class T>
108  void AaImageViewer<T>::show()
109  {
110  drawImage();
111  }
112 
113  template <class T>
114  void AaImageViewer<T>::hide()
115  {
116  if (context)
117  aa_close(context);
118  context = NULL;
119  }
120 
121  template <class T>
122  void AaImageViewer<T>::drawImage()
123  {
124  if (!context)
125  createContext();
126 
127  aa_resize(context);
128 
129  double imW = this->image->getWidth();
130  double imH = this->image->getHeight();
131  double imR = imW / imH;
132 
133  double scrW = aa_imgwidth(context);
134  double scrH = aa_imgheight(context);
135  double scrR = scrW / scrH;
136 
137  size_t w, h;
138  // find dimensions to fit screen
139  if (scrR > imR)
140  {
141  w = imW * scrH / imH;
142  h = scrH;
143  }
144  else
145  {
146  w = scrW;
147  h = imH * scrW / imW;
148  }
149  w *= aa_imgheight(context) / aa_scrheight(context);
150 
151  Image<T> tmpIm(w, h);
152  resize(*this->image, w, h, tmpIm);
153 
154  unsigned char *data = aa_image(context);
155  typename Image<T>::lineType pixels = tmpIm.getPixels();
156  double coeff = double(numeric_limits<UINT8>::max()) / double(numeric_limits<T>::max());
157 
158  for (int j=0;j<scrH;j++)
159  for (int i=0;i<scrW;i++,data++)
160  {
161  if (i<w && j<h)
162  *data = (UINT8)(coeff * double(*pixels++));
163  else *data = 0;
164  }
165 
166  aa_render(context, &aa_defrenderparams, 0, 0, aa_scrwidth (context), aa_scrheight (context));
167  aa_flush(context);
168  }
169 
170  template <class T>
171  bool AaImageViewer<T>::isVisible()
172  {
173  return context!=NULL;
174  }
175 
176  template <class T>
177  void AaImageViewer<T>::setName(const char* _name)
178  {
179  }
180 
183 } // namespace smil
184 
185 
186 #endif // _D_AA_IMAGE_VIEWER_HPP
AA (Ascii Art) image viewer.
Definition: DAAImageViewer.hpp:56
Main Image class.
Definition: DImage.hpp:57
Base image viewer.
Definition: DImageViewer.hpp:52
RES_T resize(Image< T > &imIn, size_t sx, size_t sy, size_t sz, Image< T > &imOut, string method="trilinear")
resize() - 3D image resize
Definition: DImageTransform.hpp:1180