SMIL  1.0.4
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''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _D_IMAGE_IO_HXX
31 #define _D_IMAGE_IO_HXX
32 
33 #if __cplusplus >= 201103L
34 #include <memory>
35 #define auto_ptr unique_ptr
36 #else
37 #endif // __cplusplus > 201103L
38 
39 #include "IO/include/DCommonIO.h"
40 #include "DImageIO.hpp"
41 #include "Base/include/private/DImageArith.hpp"
42 
43 #include "DImageIO_BMP.hpp"
44 #include "DImageIO_VTK.hpp"
45 #include "DImageIO_PBM.hpp"
46 
47 #ifdef USE_PNG
48 #include "DImageIO_PNG.hpp"
49 #endif
50 #ifdef USE_JPEG
51 #include "DImageIO_JPG.hpp"
52 #endif
53 #ifdef USE_TIFF
54 #include "DImageIO_TIFF.hpp"
55 #endif
56 
57 namespace smil
58 {
62  /* *@{*/
63 
65  template <class T>
66  ImageFileHandler<T> *getHandlerForFile(const char *filename)
67  {
68  string fileExt = getFileExtension(filename);
69 
70  if (fileExt == "BMP")
71  return new BMPImageFileHandler<T>();
72 
73 #ifdef USE_PNG
74  else if (fileExt == "PNG")
75  return new PNGImageFileHandler<T>();
76 #endif // USE_PNG
77 
78 #ifdef USE_JPEG
79  else if (fileExt == "JPG" || fileExt == "JPEG")
80  return new JPGImageFileHandler<T>();
81 #endif // USE_JPEG
82 
83 #ifdef USE_TIFF
84  else if (fileExt == "TIF" || fileExt == "TIFF")
85  return new TIFFImageFileHandler<T>();
86 #endif // USE_TIFF
87 
88  else if (fileExt == "VTK")
89  return new VTKImageFileHandler<T>();
90 
91  else if (fileExt == "PGM")
92  return new PGMImageFileHandler<T>();
93 
94  else if (fileExt == "PBM")
95  return new PBMImageFileHandler<T>();
96 
97  else {
98  cout << "No reader/writer available for " << fileExt << " files." << endl;
99  return NULL;
100  }
101  }
104  /*
105  * Read image file
106  */
107  template <class T> RES_T read(const char *filename, Image<T> &image)
108  {
109  RES_T res;
110 
111  string fileExt = getFileExtension(filename);
112  string filePrefix = (string(filename).substr(0, 7));
113  string prefix = string(filename);
114 
115  if (prefix.find("http://") == 0 || prefix.find("https://") == 0) {
116 #ifdef USE_CURL
117  string tmpFileName = "_smilTmpIO." + fileExt;
118  if (getHttpFile(filename, tmpFileName.c_str()) != RES_OK) {
119  ERR_MSG(string("Error downloading file ") + filename);
120  return RES_ERR;
121  }
122  res = read(tmpFileName.c_str(), image);
123  remove(tmpFileName.c_str());
124 
125 #else // USE_CURL
126  ERR_MSG("Error: to use this functionality you must compile SMIL with the "
127  "Curl option");
128  res = RES_ERR;
129 #endif // USE_CURL
130  return res;
131  }
132 
133  if (prefix.find("file://") == 0) {
134  string fName = filename;
135  string buf = fName.substr(7, fName.length() - 7);
136  return read(buf.c_str(), image);
137  }
138 
139  auto_ptr<ImageFileHandler<T>> fHandler(getHandlerForFile<T>(filename));
140 
141  if (fHandler.get())
142  return fHandler->read(filename, image);
143  else
144  return RES_ERR;
145  }
146 
147  /*
148  * Read a stack of 2D images and convert then into a 3D image
149  *
150  */
151  template <class T> RES_T read(const vector<string> fileList, Image<T> &image)
152  {
153  size_t nFiles = fileList.size();
154  if (nFiles == 0)
155  return RES_ERR;
156 
157  vector<string>::const_iterator it = fileList.begin();
158 
159  Image<T> tmpIm;
160  ASSERT((read((*it++).c_str(), tmpIm) == RES_OK));
161 
162  size_t w = tmpIm.getWidth(), h = tmpIm.getHeight();
163  ImageFreezer freezer(image);
164 
165  ASSERT((image.setSize(w, h, nFiles) == RES_OK));
166  ASSERT((fill(image, T(0)) == RES_OK));
167  ASSERT((copy(tmpIm, 0, 0, 0, image, 0, 0, 0) == RES_OK));
168 
169  size_t z = 1;
170 
171  while (it != fileList.end()) {
172  ASSERT((read((*it).c_str(), tmpIm) == RES_OK));
173  ASSERT((copy(tmpIm, 0, 0, 0, image, 0, 0, z) == RES_OK));
174  it++;
175  z++;
176  }
177 
178  return RES_OK;
179  }
180 
181  /*
182  * Write image file
183  */
184  template <class T> RES_T write(const Image<T> &image, const char *filename)
185  {
186  string fileExt = getFileExtension(filename);
187 
188  auto_ptr<ImageFileHandler<T>> fHandler(getHandlerForFile<T>(filename));
189 
190  if (fHandler.get())
191  return fHandler->write(image, filename);
192  else
193  return RES_ERR;
194  }
195 
196  /*
197  * Write 3D image into a stack of 2D image files
198  */
199  template <class T>
200  RES_T write(const Image<T> &image, const vector<string> fileList)
201  {
202  UINT nFiles = fileList.size();
203  if (nFiles != image.getDepth()) {
204  ERR_MSG("The fileList must contain the same number of filename as the "
205  "image depth.");
206  return RES_ERR;
207  }
208 
209  size_t w = image.getWidth(), h = image.getHeight();
210  Image<T> tmpIm(w, h);
211 
212  for (size_t z = 0; z < nFiles; z++) {
213  ASSERT((copy(image, 0, 0, z, tmpIm) == RES_OK));
214  ASSERT((write(tmpIm, fileList[z].c_str()) == RES_OK));
215  }
216 
217  return RES_OK;
218  }
219 
220  /*
221  * Get information about file
222  *
223  */
224  RES_T getFileInfo(const char *filename, ImageFileInfo &fInfo);
225 
226 
227  /*
228  * createFromFile
229  *
230  */
231  BaseImage *createFromFile(const char *filename);
232 
233  /* *@}*/
234 
235 } // namespace smil
236 
237 #endif // _D_IMAGE_IO_HXX
size_t getDepth() const
Get image depth (Z)
Definition: DBaseImage.h:90
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:80
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:85
Definition: DBaseImage.h:386
Main Image class.
Definition: DImage.hpp:57
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)
Set the size of image.
RES_T fill(Image< T > &imOut, const T &value)
fill() - Fill an image with a given value.
Definition: DImageArith.hpp:1456
RES_T write(const Image< T > &image, const char *filename)
Write image into file.
Definition: DImageIO.hxx:184
RES_T read(const char *filename, Image< T > &image)
Read image file.
Definition: DImageIO.hxx:107
RES_T getFileInfo(const char *filename, ImageFileInfo &fInfo)
Get information about an image file.
Definition: DImageIO.cpp:51
RES_T getHttpFile(const char *url, const char *outfilename)
Get a file from an URL.
Definition: DCommonIO.cpp:136
BaseImage * createFromFile(const char *filename)
createFromFile TBD
Definition: DImageIO.cpp:62
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() - Copy image (or a zone) into an output image
Definition: DImageTransform.hpp:84