SMIL  0.9.1
DImageIO.hxx
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_IMAGE_IO_HXX
31 #define _D_IMAGE_IO_HXX
32 
33 
34 
35 #if __cplusplus >= 201103L
36 #include <memory>
37 #define auto_ptr unique_ptr
38 #else
39 #endif // __cplusplus > 201103L
40 
41 #include "IO/include/DCommonIO.h"
42 #include "DImageIO.hpp"
43 #include "Base/include/private/DImageArith.hpp"
44 
45 #include "DImageIO_BMP.hpp"
46 #include "DImageIO_VTK.hpp"
47 #include "DImageIO_PBM.hpp"
48 
49 #ifdef USE_PNG
50 #include "DImageIO_PNG.hpp"
51 #endif
52 #ifdef USE_JPEG
53 #include "DImageIO_JPG.hpp"
54 #endif
55 #ifdef USE_TIFF
56 #include "DImageIO_TIFF.hpp"
57 #endif
58 
59 namespace smil
60 {
61 
69  template <class T>
70  ImageFileHandler<T> *getHandlerForFile(const char* filename)
71  {
72  string fileExt = getFileExtension(filename);
73 
74  if (fileExt=="BMP")
75  return new BMPImageFileHandler<T>();
76 
77  #ifdef USE_PNG
78  else if (fileExt=="PNG")
79  return new PNGImageFileHandler<T>();
80  #endif // USE_PNG
81 
82  #ifdef USE_JPEG
83  else if (fileExt=="JPG")
84  return new JPGImageFileHandler<T>();
85  #endif // USE_JPEG
86 
87  #ifdef USE_TIFF
88  else if (fileExt=="TIF")
89  return new TIFFImageFileHandler<T>();
90  #endif // USE_TIFF
91 
92  else if (fileExt=="VTK")
93  return new VTKImageFileHandler<T>();
94 
95  else if (fileExt=="PGM")
96  return new PGMImageFileHandler<T>();
97 
98  else if (fileExt=="PBM")
99  return new PBMImageFileHandler<T>();
100 
101  else
102  {
103  cout << "No reader/writer available for " << fileExt << " files." << endl;
104  return NULL;
105  }
106  }
107 
111  template <class T>
112  RES_T read(const char* filename, Image<T> &image)
113  {
114  string fileExt = getFileExtension(filename);
115  string filePrefix = (string(filename).substr(0, 7));
116 
117  RES_T res;
118 
119  if (filePrefix=="http://")
120  {
121  #ifdef USE_CURL
122  string tmpFileName = "_smilTmpIO." + fileExt;
123  if (getHttpFile(filename, tmpFileName.c_str())!=RES_OK)
124  {
125  ERR_MSG(string("Error downloading file ") + filename);
126  return RES_ERR;
127  }
128  res = read(tmpFileName.c_str(), image);
129  remove(tmpFileName.c_str());
130 
131  #else // USE_CURL
132  ERR_MSG("Error: to use this functionality you must compile SMIL with the Curl option");
133  res = RES_ERR;
134  #endif // USE_CURL
135  return res;
136  }
137  else if (filePrefix=="file://")
138  {
139  string fName = filename;
140  string buf = fName.substr(7, fName.length()-7);
141  return read(buf.c_str(), image);
142  }
143 
144  auto_ptr< ImageFileHandler<T> > fHandler(getHandlerForFile<T>(filename));
145 
146  if (fHandler.get())
147  return fHandler->read(filename, image);
148  else return RES_ERR;
149  }
150 
156  template <class T>
157  RES_T read(const vector<string> fileList, Image<T> &image)
158  {
159  size_t nFiles = fileList.size();
160  if (nFiles==0)
161  return RES_ERR;
162 
163  vector<string>::const_iterator it = fileList.begin();
164 
165  Image<T> tmpIm;
166  ASSERT((read((*it++).c_str(), tmpIm)==RES_OK));
167 
168  size_t w = tmpIm.getWidth(), h = tmpIm.getHeight();
169  ImageFreezer freezer(image);
170 
171  ASSERT((image.setSize(w, h, nFiles)==RES_OK));
172  ASSERT((fill(image, T(0))==RES_OK));
173  ASSERT((copy(tmpIm, 0, 0, 0, image, 0, 0, 0)==RES_OK));
174 
175  size_t z = 1;
176 
177  while(it!=fileList.end())
178  {
179  ASSERT((read((*it).c_str(), tmpIm)==RES_OK));
180  ASSERT((copy(tmpIm, 0, 0, 0, image, 0, 0, z)==RES_OK));
181  it++;
182  z++;
183  }
184 
185  return RES_OK;
186  }
187 
191  template <class T>
192  RES_T write(const Image<T> &image, const char *filename)
193  {
194  string fileExt = getFileExtension(filename);
195 
196  auto_ptr< ImageFileHandler<T> > fHandler(getHandlerForFile<T>(filename));
197 
198  if (fHandler.get())
199  return fHandler->write(image, filename);
200  else return RES_ERR;
201 
202  }
203 
204  template <class T>
205  RES_T write(const Image<T> &image, const vector<string> fileList)
206  {
207  UINT nFiles = fileList.size();
208  if (nFiles!=image.getDepth())
209  {
210  ERR_MSG("The fileList must contain the same number of filename as the image depth.");
211  return RES_ERR;
212  }
213 
214  size_t w = image.getWidth(), h = image.getHeight();
215  Image<T> tmpIm(w, h);
216 
217  for (size_t z=0;z<nFiles;z++)
218  {
219  ASSERT((copy(image, 0, 0, z, tmpIm)==RES_OK));
220  ASSERT((write(tmpIm, fileList[z].c_str())==RES_OK));
221  }
222 
223  return RES_OK;
224  }
225 
226  RES_T getFileInfo(const char *filename, ImageFileInfo &fInfo);
227 
228  BaseImage *createFromFile(const char *filename);
229 
232 } // namespace smil
233 
234 
235 
236 #endif // _D_IMAGE_IO_HXX
Definition: DColorConvert.h:38
RES_T copy(const Image< T1 > &imIn, size_t startX, size_t startY, size_t startZ, size_t sizeX, size_t sizeY, size_t sizeZ, Image< T2 > &imOut, size_t outStartX=0, size_t outStartY=0, size_t outStartZ=0)
Copy image.
Definition: DImageArith.hpp:114
Definition: DBaseImage.h:235
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)
Set the size of image.
Definition: DImage.hxx:319
RES_T fill(Image< T > &imOut, const T &value)
Fill an image with a given value.
Definition: DImageArith.hpp:62
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:91
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:87
Base Image class.
Definition: DBaseImage.h:50
Main Image class.
Definition: DQVtkViewer.hpp:44
size_t getDepth() const
Get image depth (Z)
Definition: DBaseImage.h:95
RES_T read(const char *filename, Image< T > &image)
Read image file.
Definition: DImageIO.hxx:112
RES_T write(const Image< T > &image, const char *filename)
Write image file.
Definition: DImageIO.hxx:192
Definition: DCommonIO.h:70