ROOT  6.06/09
Reference Guide
TImage.cxx
Go to the documentation of this file.
1 // @(#)root/graf:$Id$
2 // Author: Fons Rademakers 15/10/2001
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TImage.h"
13 #include "TROOT.h"
14 #include "TPluginManager.h"
15 #include "TApplication.h"
16 #include "TSystem.h"
17 
19 
20 /** \class TImage
21 \ingroup BasicGraphics
22 
23 An abstract interface to image processing library.
24 
25 It allows for the reading and writing of images in different formats, several
26 image manipulations (scaling, tiling, merging, etc.) and displaying in pads.
27 
28 The concrete implementation of this class is done by the TASImage class. The
29 methods are documented in that class.
30 End_Html
31 */
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// Create an image.
35 /// Use ReadImage() or SetImage() to initialize the image.
36 
37 TImage *TImage::Create()
38 {
39  static TPluginHandler *h = 0;
40 
41  if (!h) {
42  h = gROOT->GetPluginManager()->FindHandler("TImage");
43  if (!h) return 0;
44  if (h->LoadPlugin() == -1) {
45  h = 0; // try to reload plugin next time
46  return 0;
47  }
48  }
49  TImage *img = (TImage *) h->ExecPlugin(0);
50  if (img) img->SetName("dummy_image");
51 
52  return img;
53 }
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Return the image type for the extension specified in filename.
57 /// Case of the extension is ignored. E.g. for a filename "myimg.GIF",
58 /// kGif is returned.
59 /// kAnimGif is returned if the file extension is ".anim.gif".
60 
62 {
63  if (!filename) return kUnknown;
64 
65  TString sFilename(filename);
66  if (sFilename.EndsWith(".xpm.gz", TString::kIgnoreCase))
67  return kGZCompressedXpm;
68  else if (sFilename.EndsWith(".xpm.z", TString::kIgnoreCase))
69  return kZCompressedXpm;
70  else if (sFilename.EndsWith(".png", TString::kIgnoreCase))
71  return kPng;
72  else if (sFilename.EndsWith(".jpeg", TString::kIgnoreCase))
73  return kJpeg;
74  else if (sFilename.EndsWith(".jpg", TString::kIgnoreCase))
75  return kJpeg;
76  else if (sFilename.EndsWith(".xcf", TString::kIgnoreCase))
77  return kXcf;
78  else if (sFilename.EndsWith(".ppm", TString::kIgnoreCase))
79  return kPpm;
80  else if (sFilename.EndsWith(".pnm", TString::kIgnoreCase))
81  return kPnm;
82  else if (sFilename.EndsWith(".bmp", TString::kIgnoreCase))
83  return kBmp;
84  else if (sFilename.EndsWith(".ico", TString::kIgnoreCase))
85  return kIco;
86  else if (sFilename.EndsWith(".cur", TString::kIgnoreCase))
87  return kCur;
88  else if (sFilename.EndsWith(".gif", TString::kIgnoreCase))
89  return kGif;
90  else if (sFilename.EndsWith(".tiff", TString::kIgnoreCase))
91  return kTiff;
92  else if (sFilename.EndsWith(".tif", TString::kIgnoreCase))
93  return kTiff;
94  else if (sFilename.EndsWith(".xbm", TString::kIgnoreCase))
95  return kXbm;
96  else if (sFilename.EndsWith(".fits", TString::kIgnoreCase))
97  return kFits;
98  else if (sFilename.EndsWith(".tga", TString::kIgnoreCase))
99  return kTga;
100  else if (sFilename.EndsWith(".xml", TString::kIgnoreCase))
101  return kXml;
102  else if (sFilename.EndsWith(".anim.gif", TString::kIgnoreCase))
103  return kAnimGif;
104 
105  return kUnknown;
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Open a specified image file.
110 
112 {
113  TImage *img = Create();
114  char *fullname = gSystem->ExpandPathName(file);
115 
116  if (img)
117  img->ReadImage(fullname, type);
118 
119  delete [] fullname;
120 
121  return img;
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// Open an image with the specified data in a Double_t array.
126 
127 TImage *TImage::Open(const char *name, const Double_t *imageData, UInt_t width,
128  UInt_t height, TImagePalette *palette)
129 {
130  TImage *img = Create();
131 
132  if (img) {
133  img->SetImage(imageData, width, height, palette);
134  img->SetName(name);
135  }
136  return img;
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Open an image with the specified data in a TArrayD.
141 
142 TImage *TImage::Open(const char *name, const TArrayD &imageData, UInt_t width,
143  TImagePalette *palette)
144 {
145  TImage *img = Create();
146 
147  if (img) {
148  img->SetImage(imageData, width, palette);
149  img->SetName(name);
150  }
151  return img;
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Open an image with the specified data in a TVectorD.
156 
157 TImage *TImage::Open(const char *name, const TVectorD &imageData, UInt_t width,
158  TImagePalette *palette)
159 {
160  TImage *img = Create();
161 
162  if (img) {
163  img->SetImage(imageData, width, palette);
164  img->SetName(name);
165  }
166  return img;
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Create image from XPM data array.
171 
172 TImage *TImage::Open(char **data)
173 {
174  TImage *img = Create();
175 
176  if (img) {
177  img->SetImageBuffer(data, TImage::kXpm);
178  img->SetName("XPM_image");
179  }
180  return img;
181 }
182 
183 
184 TImage operator+(const TImage &i1, const TImage &i2) { TImage ret(i1); ret.Append(&i2, "+"); return ret; }
185 TImage operator/(const TImage &i1, const TImage &i2) { TImage ret(i1); ret.Append(&i2, "/"); return ret; }
virtual void Append(const TImage *, const char *="+", const char *="#00000000")
Definition: TImage.h:191
EImageFileTypes
Definition: TImage.h:52
TImage operator+(const TImage &i1, const TImage &i2)
Definition: TImage.cxx:184
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
static const char * filename()
#define gROOT
Definition: TROOT.h:340
Int_t LoadPlugin()
Load the plugin library for this handler.
Basic string class.
Definition: TString.h:137
An abstract interface to image processing library.
Definition: TImage.h:45
Long_t ExecPlugin(int nargs, const T &...params)
virtual void SetImage(const Double_t *, UInt_t, UInt_t, TImagePalette *=0)
Definition: TImage.h:132
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2220
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void ReadImage(const char *, EImageFileTypes=TImage::kUnknown)
Definition: TImage.h:130
static TImage * Create()
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
int type
Definition: TGX11.cxx:120
A class to define a conversion from pixel values to pixel color.
Definition: TAttImage.h:83
Array of doubles (64 bits per element).
Definition: TArrayD.h:29
#define name(a, b)
Definition: linkTestLib0.cpp:5
static EImageFileTypes GetImageFileTypeFromFilename(const char *opt)
Return the image type for the extension specified in filename.
Definition: TImage.cxx:61
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition: TImage.cxx:111
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1243
virtual Bool_t SetImageBuffer(char **, EImageFileTypes=TImage::kPng)
Definition: TImage.h:258
TImage operator/(const TImage &i1, const TImage &i2)
Definition: TImage.cxx:185