SMIL  0.9.1
DImageIO_PBM.hpp
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_PBM_H
31 #define _D_IMAGE_IO_PBM_H
32 
33 #include <fstream>
34 #include <iostream>
35 
36 #include "Core/include/private/DTypes.hpp"
37 #include "Core/include/private/DImage.hpp"
38 #include "IO/include/private/DImageIO.hpp"
39 
40 namespace smil
41 {
47  RES_T readNetPBMFileInfo(ifstream &fp, ImageFileInfo &fInfo, unsigned int &maxval);
48  RES_T readNetPBMFileInfo(const char* filename, ImageFileInfo &fInfo, unsigned int &maxval);
49 
50  template <class T> class Image;
51 
52  template <class T=void>
54  {
55  public:
57  : ImageFileHandler<T>("PGM")
58  {
59  }
60 
61  virtual RES_T getFileInfo(const char* filename, ImageFileInfo &fInfo)
62  {
63  unsigned int dum;
64  return readNetPBMFileInfo(filename, fInfo, dum);
65  }
66 
67  virtual RES_T read(const char* filename, Image<T> &image)
68  {
69  return ImageFileHandler<T>::read(filename, image);
70  }
71  virtual RES_T write(const Image<T> &image, const char* filename)
72  {
73  return ImageFileHandler<T>::write(image, filename);
74  }
75  };
76 
77  template <class T=void>
79  {
80  public:
82  : ImageFileHandler<T>("PBM")
83  {
84  }
85 
86  virtual RES_T getFileInfo(const char* filename, ImageFileInfo &fInfo)
87  {
88  unsigned int dum;
89  return readNetPBMFileInfo(filename, fInfo, dum);
90  }
91 
92  virtual RES_T read(const char* filename, Image<T> &image)
93  {
94  /* open image file */
95  ifstream fp(filename, ios_base::binary);
96 
97  if (!fp.is_open())
98  {
99  cout << "Cannot open file " << filename << endl;
100  return RES_ERR_IO;
101  }
102 
103  ImageFileInfo fInfo;
104  unsigned int dum; // no maxval in PBM format
105  ASSERT(readNetPBMFileInfo(fp, fInfo, dum)==RES_OK, RES_ERR_IO);
106  ASSERT(fInfo.colorType==ImageFileInfo::COLOR_TYPE_BINARY, "Not an binary image", RES_ERR_IO);
107 
108  size_t width = fInfo.width;
109  size_t height = fInfo.height;
110 
111  ASSERT((image.setSize(width, height)==RES_OK), RES_ERR_BAD_ALLOCATION);
112 
113  if (fInfo.fileType==ImageFileInfo::FILE_TYPE_BINARY)
114  {
115  typename ImDtTypes<T>::sliceType lines = image.getLines();
116 
117 // int nBytePerLine = width%8==0 ? width/8 : width/8+1;
118  char val;
119  int k;
120 
121  for (size_t j=0;j<height;j++)
122  {
123  typename ImDtTypes<T>::lineType pixels = lines[j];
124 
125  for (size_t i=0;i<width;i++)
126  {
127  if (i%8 == 0)
128  fp.read(&val, 1);
129 
130  k = 7 - i%8;
131  pixels[i] = ( ( val >> k )%2 )==0 ? T(0) : ImDtTypes<T>::max();
132  }
133  }
134  }
135  else
136  {
137  typename ImDtTypes<T>::lineType pixels = image.getPixels();
138 
139  int val;
140  for (size_t i=0;i<image.getPixelCount();i++)
141  {
142  fp >> val;
143  pixels[i] = val==0 ? T(0) : ImDtTypes<T>::max();
144  }
145  }
146 
147  fp.close();
148 
149  return RES_OK;
150  }
151  virtual RES_T write(const Image<T> &image, const char* filename)
152  {
153  return ImageFileHandler<T>::write(image, filename);
154  }
155  };
156 
157  template <>
158  inline RES_T PBMImageFileHandler<void>::read(const char *, Image<void> &)
159  {
160  return RES_ERR;
161  }
162 
163  template <>
164  inline RES_T PBMImageFileHandler<void>::write(const Image<void> &, const char *)
165  {
166  return RES_ERR;
167  }
168 
169  // Specializations
170 
171  TEMPL_SPEC_DECL RES_T PGMImageFileHandler<UINT8>::read(const char *filename, Image<UINT8> &image);
172  TEMPL_SPEC_DECL RES_T PGMImageFileHandler<UINT8>::write(const Image<UINT8> &image, const char *filename);
173 
174 #ifdef SMIL_WRAP_RGB
175  TEMPL_SPEC_DECL RES_T PGMImageFileHandler<RGB>::read(const char *filename, Image<RGB> &image);
176  TEMPL_SPEC_DECL RES_T PGMImageFileHandler<RGB>::write(const Image<RGB> &image, const char *filename);
177 #endif // SMIL_WRAP_RGB
178 
181 } // namespace smil
182 
183 
184 #endif // _D_IMAGE_IO_PBM_H
Definition: DImageIO_PBM.hpp:53
Definition: DColorConvert.h:38
sliceType getLines() const
Get an array containing the start offset of each line.
Definition: DImage.hpp:114
Definition: DImageIO_PBM.hpp:78
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:110
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
Definition: DImageIO.hpp:51
size_t getPixelCount() const
Get the number of pixels.
Definition: DBaseImage.h:150
Definition: DTypes.hpp:78
Main Image class.
Definition: DQVtkViewer.hpp:44
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