SMIL  0.9.1
DImageIO_RAW.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_RAW_HPP
31 #define _D_IMAGE_IO_RAW_HPP
32 
33 
34 #include <cstdio>
35 #include <cstdlib>
36 #include <string>
37 
38 #include "Core/include/private/DImage.hpp"
39 
40 using namespace std;
41 
42 namespace smil
43 {
52  template <class T>
53  RES_T readRAW(const char *filename, size_t width, size_t height, size_t depth, Image<T> &image)
54  {
55  FILE *fp = NULL;
56 
57  /* open image file */
58  SMIL_OPEN(fp, filename, "rb");
59 
60  ASSERT(fp, "Error: couldn't open file", RES_ERR_IO);
61 
62 
63  image.setSize(width, height, depth);
64  // image->allocate();
65 
66  size_t ret = fread(image.getVoidPointer(), sizeof(T), image.getPixelCount(), fp);
67  if (ret==0)
68  {
69  fprintf (stderr, "error reading \"%s\"!\n", filename);
70  return RES_ERR;
71  }
72 
73  fclose (fp);
74 
75  image.modified();
76 
77  return RES_OK;
78  }
79 
83  template <class T>
84  RES_T writeRAW(Image<T> &image, const char *filename)
85  {
86  FILE *fp = NULL;
87 
88  /* open image file */
89  SMIL_OPEN(fp, filename, "wb");
90 
91  ASSERT(fp, "Error: couldn't open file", RES_ERR_IO);
92 
93  fwrite(image.getVoidPointer(), sizeof(T), image.getPixelCount(), fp);
94 
95  fclose (fp);
96 
97  return RES_OK;
98  }
99 
102 } // namespace smil
103 
104 
105 #endif // _D_IMAGE_IO_RAW_H
Definition: DColorConvert.h:38
RES_T readRAW(const char *filename, size_t width, size_t height, size_t depth, Image< T > &image)
RAW file read.
Definition: DImageIO_RAW.hpp:53
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
virtual void * getVoidPointer(void)
Get pixels as a void pointer.
Definition: DImage.hpp:266
virtual void modified()
Trigger modified event (allows to force display update)
Definition: DImage.hxx:223
size_t getPixelCount() const
Get the number of pixels.
Definition: DBaseImage.h:150
Main Image class.
Definition: DQVtkViewer.hpp:44
RES_T writeRAW(Image< T > &image, const char *filename)
RAW file write.
Definition: DImageIO_RAW.hpp:84