Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
18 ClassImp(TImage);
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 */
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 /// Create an image.
34 /// Use ReadImage() or SetImage() to initialize the image.
35 
36 TImage *TImage::Create()
37 {
38  static TPluginHandler *h = 0;
39 
40  if (!h) {
41  h = gROOT->GetPluginManager()->FindHandler("TImage");
42  if (!h) return 0;
43  if (h->LoadPlugin() == -1) {
44  h = 0; // try to reload plugin next time
45  return 0;
46  }
47  }
48  TImage *img = (TImage *) h->ExecPlugin(0);
49  if (img) img->SetName("dummy_image");
50 
51  return img;
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Return the image type for the extension specified in filename.
56 /// Case of the extension is ignored. E.g. for a filename "myimg.GIF",
57 /// kGif is returned.
58 /// kAnimGif is returned if the file extension is ".anim.gif".
59 
60 TImage::EImageFileTypes TImage::GetImageFileTypeFromFilename(const char* filename)
61 {
62  if (!filename) return kUnknown;
63 
64  TString sFilename(filename);
65  if (sFilename.EndsWith(".xpm.gz", TString::kIgnoreCase))
66  return kGZCompressedXpm;
67  else if (sFilename.EndsWith(".xpm.z", TString::kIgnoreCase))
68  return kZCompressedXpm;
69  else if (sFilename.EndsWith(".png", TString::kIgnoreCase))
70  return kPng;
71  else if (sFilename.EndsWith(".jpeg", TString::kIgnoreCase))
72  return kJpeg;
73  else if (sFilename.EndsWith(".jpg", TString::kIgnoreCase))
74  return kJpeg;
75  else if (sFilename.EndsWith(".xcf", TString::kIgnoreCase))
76  return kXcf;
77  else if (sFilename.EndsWith(".ppm", TString::kIgnoreCase))
78  return kPpm;
79  else if (sFilename.EndsWith(".pnm", TString::kIgnoreCase))
80  return kPnm;
81  else if (sFilename.EndsWith(".bmp", TString::kIgnoreCase))
82  return kBmp;
83  else if (sFilename.EndsWith(".ico", TString::kIgnoreCase))
84  return kIco;
85  else if (sFilename.EndsWith(".cur", TString::kIgnoreCase))
86  return kCur;
87  else if (sFilename.EndsWith(".gif", TString::kIgnoreCase))
88  return kGif;
89  else if (sFilename.EndsWith(".tiff", TString::kIgnoreCase))
90  return kTiff;
91  else if (sFilename.EndsWith(".tif", TString::kIgnoreCase))
92  return kTiff;
93  else if (sFilename.EndsWith(".xbm", TString::kIgnoreCase))
94  return kXbm;
95  else if (sFilename.EndsWith(".fits", TString::kIgnoreCase))
96  return kFits;
97  else if (sFilename.EndsWith(".tga", TString::kIgnoreCase))
98  return kTga;
99  else if (sFilename.EndsWith(".xml", TString::kIgnoreCase))
100  return kXml;
101  else if (sFilename.EndsWith(".anim.gif", TString::kIgnoreCase))
102  return kAnimGif;
103 
104  return kUnknown;
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// List this image with its attributes.
109 
110 void TImage::ls(Option_t *) const
111 {
112  TROOT::IndentLevel();
113  printf("TImage: \"%s\"\n", GetName() );
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// Open a specified image file.
118 
119 TImage *TImage::Open(const char *file, EImageFileTypes type)
120 {
121  TImage *img = Create();
122  char *fullname = gSystem->ExpandPathName(file);
123 
124  if (img)
125  img->ReadImage(fullname, type);
126 
127  delete [] fullname;
128 
129  return img;
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Open an image with the specified data in a Double_t array.
134 
135 TImage *TImage::Open(const char *name, const Double_t *imageData, UInt_t width,
136  UInt_t height, TImagePalette *palette)
137 {
138  TImage *img = Create();
139 
140  if (img) {
141  img->SetImage(imageData, width, height, palette);
142  img->SetName(name);
143  }
144  return img;
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// Open an image with the specified data in a TArrayD.
149 
150 TImage *TImage::Open(const char *name, const TArrayD &imageData, UInt_t width,
151  TImagePalette *palette)
152 {
153  TImage *img = Create();
154 
155  if (img) {
156  img->SetImage(imageData, width, palette);
157  img->SetName(name);
158  }
159  return img;
160 }
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// Open an image with the specified data in a TVectorD.
164 
165 TImage *TImage::Open(const char *name, const TVectorD &imageData, UInt_t width,
166  TImagePalette *palette)
167 {
168  TImage *img = Create();
169 
170  if (img) {
171  img->SetImage(imageData, width, palette);
172  img->SetName(name);
173  }
174  return img;
175 }
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// Create image from XPM data array.
179 
180 TImage *TImage::Open(char **data)
181 {
182  TImage *img = Create();
183 
184  if (img) {
185  img->SetImageBuffer(data, TImage::kXpm);
186  img->SetName("XPM_image");
187  }
188  return img;
189 }
190 
191 
192 TImage operator+(const TImage &i1, const TImage &i2) { TImage ret(i1); ret.Append(&i2, "+"); return ret; }
193 TImage operator/(const TImage &i1, const TImage &i2) { TImage ret(i1); ret.Append(&i2, "/"); return ret; }