Imagine++
IO.h
1 // ===========================================================================
2 // Imagine++ Libraries
3 // Copyright (C) Imagine
4 // For detailed information: http://imagine.enpc.fr/software
5 // ===========================================================================
6 
7 namespace Imagine {
10 
11 
12 
23  AlphaColor* aC = I.data();
24  setMaskFromColor(aC,I.width(), I.height(), col);
25  }
26 
27  // Files
28 
37  inline bool load(Image<byte>& I, std::string name) {
38  int W,H;
39  byte* G;
40  if (!loadGreyImage(name,G,W,H))
41  return false;
42  I = Image<byte>(G,W,H,true);
43  return true;
44  }
53  inline bool save(const Image<byte>& I, std::string name) {
54  return saveGreyImage(name,(byte *)I.data(),I.width(),I.height());
55  }
64  inline bool load(Image<Color>& I, std::string name) {
65  int W,H;
66  Color* G;
67  if (!loadColorImage(name,G,W,H))
68  return false;
69  I = Image<Color>(G,W,H,true);
70  return true;
71  }
82  inline bool load(Image<AlphaColor>& I, std::string name) {
83  int W,H;
84  AlphaColor* aC;
85  if(!loadAlphaColorImage(name,aC,W,H))
86  return false;
87  I = Image<AlphaColor>(aC,W,H,true);
88  return true;
89  }
90 
99  inline bool load(Image<byte>& IR, Image<byte>& IG, Image<byte>& IB, std::string name) {
100  int W,H;
101  byte *R,*G,*B;
102  if (!loadColorImage(name,R,G,B,W,H))
103  return false;
104  IR = Image<byte>(R,W,H,true);
105  IG = Image<byte>(G,W,H,true);
106  IB = Image<byte>(B,W,H,true);
107  return true;
108  }
119  inline bool save(const Image<Color>& I, std::string name,int quality=85) {
120  return saveColorImage(name,I.data(),I.width(),I.height(),quality);
121  }
131  inline bool save(Image<AlphaColor>& I, std::string name, int quality=85) {
132  return saveAlphaColorImage(name, I.data(), I.width(), I.height(), quality);
133  }
134 
135 
145  inline bool save(const Image<byte>& IR, const Image<byte>& IG, const Image<byte>& IB, std::string name,int quality=85) {
146  assert(IR.sizes() == IG.sizes() && IR.sizes() == IB.sizes());
147  return saveColorImage(name,(byte *)IR.data(),(byte *)IG.data(),(byte *)IB.data(),IR.width(),IR.height(),quality);
148  }
149 
150  //==================== DISPLAY ===============
151 
161  inline void display(const Image<byte>& I, int x=0, int y=0,bool xorMode=false,double fact=1.) {
162  putGreyImage(x,y,(byte *)I.data(),I.width(),I.height(),xorMode,fact);
163  }
165  inline void display(const Image<byte>& I,IntPoint2 p,bool xorMode=false,double fact=1.) { display(I,p.x(),p.y(),xorMode,fact); }
175  inline void display(const Image<Color>& I, int x=0, int y=0,bool xorMode=false,double fact=1.) {
176  putColorImage(x,y,(Color *)I.data(),I.width(),I.height(),xorMode,fact);
177  }
187  inline void display(const Image<AlphaColor>& I, int x = 0, int y = 0, bool xorMode = false, double fact=1.) {
188  putAlphaColorImage(x,y,(AlphaColor *)I.data(), I.width(), I.height(), xorMode, fact);
189  }
190 
192  inline void display(const Image<Color>& I,IntPoint2 p,bool xorMode=false,double fact=1.) { display(I,p.x(),p.y(),xorMode,fact); }
202  inline void display(const Image<byte>& IR, const Image<byte>& IG, const Image<byte>& IB, int x=0, int y=0,bool xorMode=false,double fact=1.) {
203  assert( IR.sizes() == IG.sizes() && IR.sizes() == IB.sizes());
204  putColorImage(x,y,(byte *)IR.data(),(byte *)IG.data(),(byte *)IB.data(),IR.width(),IR.height(),xorMode,fact);
205  }
207  inline void display(const Image<byte>& IR, const Image<byte>& IG, const Image<byte>& IB,IntPoint2 p,bool xorMode=false,double fact=1.) { display(IR,IG,IB,p.x(),p.y(),xorMode,fact); }
217  template <typename T, int dim>
218  Image<T> cut2D(const Image<T,dim>& I,const Coords<dim>& cut,int d1,int d2)
219  {
220  Coords<2> sz=Coords<2>(I.size(d1),I.size(d2));
221  Image<T> C(sz);
222  Coords<dim> p=cut;
223  for (CoordsIterator<2> r=C.coordsBegin();r!=C.coordsEnd();++r) {
224  p[d1]=(*r)[0];
225  p[d2]=(*r)[1];
226  C(*r)=I(p);
227  }
228  return C;
229  }
230 
231 #ifndef DOXYGEN_SHOULD_SKIP_THIS
232  // Finds the color of from a rainbow palette from m to M (BLUE to RED)
233  // Private usage...
234  template <typename T> Color rainbowColor(T x, T m, T M) {
235  const T r=T(.25);
236  x=(x-m)/(M-m);
237  x=std::min(T(1.),std::max(T(0.),x));
238  if (x<r)
239  return Color(0,byte(255*x/r),255);
240  x-=r;
241  if (x<r)
242  return Color(0,255,byte(255*(1-x/r)));
243  x-=r;
244  if (x<r)
245  return Color(byte(255*x/r),255,0);
246  x-=r;
247  return Color(255,byte(255*(1-x/r)),0);
248  }
249 #endif
250 
258  template <typename T, int dim>
259  Image<Color,dim> rainbow(const Image<T,dim>& I, T m, T M)
260  {
261  Image<Color,dim> c(I.sizes());
262  typename Image<T,dim>::const_iterator it1 = I.begin();
263  typename Image<Color,dim>::iterator it2 = c.begin();
264  for ( ; it1 != I.end() ; ++it1, ++it2) {
265  *it2 = rainbowColor(*it1,m,M);
266  }
267  return c;
268  }
275  template <typename T, int dim>
277  {
278  std::pair<T,T> r=range(I);
279  return rainbow(I,r.first,r.second);
280  }
288  template <typename T, int dim>
289  Image<byte,dim> grey(const Image<T,dim>& I, T m, T M)
290  {
291  Image<byte,dim> c(I.sizes());
292  typename Image<T,dim>::const_iterator it1 = I.begin();
293  typename Image<byte,dim>::iterator it2 = c.begin();
294  for ( ; it1 != I.end() ; ++it1, ++it2) {
295  T v=std::min(M,std::max(m,*it1));
296  *it2 = byte((v-m)*255/(M-m));
297  }
298  return c;
299  }
306  template <typename T, int dim>
308  {
309  std::pair<T,T> r=range(I);
310  if (r.first==r.second)
311  r.second=r.second+T(1);
312  return grey(I,r.first,r.second);
313  }
321  template <typename T, int dim>
322  Image<Color,dim> color(const Image<RGB<T>,dim>& I,const RGB<T>& m,const RGB<T>& M)
323  {
324  Image<Color,dim> C(I.sizes());
325  typename Image<RGB<T>,dim>::const_iterator it1 = I.begin();
326  typename Image<Color,dim>::iterator it2 = C.begin();
327  for ( ; it1 != I.end() ; ++it1, ++it2) {
328  RGB<T> v=pmin(M,pmax(m,*it1));
329  *it2 = Color(div((v-m)*255,(M-m)));
330  };
331  return C;
332  }
339  template <typename T, int dim>
341  {
342  std::pair<RGB<T>, RGB<T> > r=prange(I);
343  return color(I,r.first,r.second);
344  }
345 
346 
348 }
349 
350 #include "Analyze.h"
bool saveColorImage(const std::string &name, const Color *cols, int w, int h, int quality=85)
Save color image.
Base::const_iterator const_iterator
Const iterator type.
Definition: Image.h:36
iterator begin()
Begin iterator.
Definition: FArray.h:202
bool loadColorImage(const std::string &name, Color *&cols, int &w, int &h)
Load color image.
RGB< byte > Color
RGB<byte> alias.
Definition: Color.h:281
void display(const Image< byte > &I, int x=0, int y=0, bool xorMode=false, double fact=1.)
Display grey image.
Definition: IO.h:161
void createMaskFromColor(Image< AlphaColor > &I, AlphaColor col)
Create a transparency mask from a specified color-key.
Definition: IO.h:22
Iterator on Coordinates.
Definition: Coords.h:83
bool loadGreyImage(const std::string &name, byte *&g, int &w, int &h)
Load grey image.
bool saveAlphaColorImage(const std::string &name, Imagine::AlphaColor *cols, int w, int h, int quality=85)
Save color image with alpha channel.
const T & x() const
Read alias.
Definition: FVector.h:104
void putAlphaColorImage(int x, int y, Imagine::AlphaColor *cols, int w, int h, bool xorMode=false, double fact=1.)
Display color bitmap with alpha channel.
RED GREEN BLUE Alpha color.
Definition: Color.h:138
Image< Color, dim > rainbow(const Image< T, dim > &I, T m, T M)
Rainbow representation.
Definition: IO.h:259
int size(int i) const
ith size.
Definition: MultiArray.h:225
Coords< dim > sizes() const
Sizes.
Definition: MultiArray.h:211
void setMaskFromColor(byte *&r, byte *&g, byte *&b, byte *&a, int w, int h, const Color &col)
Create a transparency mask from a specified color-key.
bool save(const Image< byte > &I, std::string name)
Save grey image.
Definition: IO.h:53
Image.
Definition: Image.h:24
void putColorImage(int x, int y, const Color *cols, int w, int h, bool xorMode=false, double fact=1.)
Display color bitmap.
T * data()
Data pointer (read/write).
Definition: Array.h:217
Image< byte, dim > grey(const Image< T, dim > &I, T m, T M)
Grey level representation.
Definition: IO.h:289
bool saveGreyImage(const std::string &name, const byte *g, int w, int h, int quality=85)
Save grey image.
void putGreyImage(int x, int y, const byte *g, int w, int h, bool xorMode=false, double fact=1.)
Display grey bitmap.
bool loadAlphaColorImage(const std::string &name, AlphaColor *&acols, int &w, int &h)
Load color image with alpha channel.
int height() const
Size alias 1.
Definition: MultiArray.h:239
bool load(Image< byte > &I, std::string name)
Load grey image.
Definition: IO.h:37
Image< T > cut2D(const Image< T, dim > &I, const Coords< dim > &cut, int d1, int d2)
2D cut.
Definition: IO.h:218
const T & y() const
Read alias.
Definition: FVector.h:118
iterator begin()
Begin iterator.
Definition: Array.h:231
Imagine++ namespace.
Definition: Array.h:7
Image< Color, dim > color(const Image< RGB< T >, dim > &I, const RGB< T > &m, const RGB< T > &M)
Color representation.
Definition: IO.h:322
RED GREEN BLUE color.
Definition: Color.h:26
iterator end()
End iterator.
Definition: Array.h:245
int width() const
Size alias 0.
Definition: MultiArray.h:232