SMIL  1.0.4
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''
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_RAW_HPP
31 #define _D_IMAGE_IO_RAW_HPP
32 
33 #include <cstdio>
34 #include <cstdlib>
35 #include <string>
36 
37 #include "Core/include/private/DImage.hpp"
38 
39 using namespace std;
40 
41 namespace smil
42 {
70  template <class T>
71  RES_T readRAW(const char *filename, size_t width, size_t height, size_t depth,
72  Image<T> &image)
73  {
74  FILE *fp = NULL;
75 
76  /* open image file */
77  SMIL_OPEN(fp, filename, "rb");
78 
79  ASSERT(fp, "Error: couldn't open file", RES_ERR_IO);
80 
81  image.setSize(width, height, depth);
82  // image->allocate();
83 
84  size_t ret =
85  fread(image.getVoidPointer(), sizeof(T), image.getPixelCount(), fp);
86  if (ret == 0) {
87  fprintf(stderr, "error reading \"%s\"!\n", filename);
88  return RES_ERR;
89  }
90 
91  fclose(fp);
92 
93  image.modified();
94 
95  return RES_OK;
96  }
97 
109  template <class T> RES_T writeRAW(Image<T> &image, const char *filename)
110  {
111  FILE *fp = NULL;
112 
113  /* open image file */
114  SMIL_OPEN(fp, filename, "wb");
115 
116  ASSERT(fp, "Error: couldn't open file", RES_ERR_IO);
117 
118  fwrite(image.getVoidPointer(), sizeof(T), image.getPixelCount(), fp);
119 
120  fclose(fp);
121 
122  return RES_OK;
123  }
124 
127 } // namespace smil
128 
129 #endif // _D_IMAGE_IO_RAW_H
size_t getPixelCount() const
Get the number of pixels.
Definition: DBaseImage.h:160
Main Image class.
Definition: DImage.hpp:57
virtual void * getVoidPointer(void)
Get pixels as a void pointer.
Definition: DImage.hpp:291
virtual void modified()
Trigger modified event (allows to force display update)
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)
Set the size of image.
RES_T writeRAW(Image< T > &image, const char *filename)
Save an image in a RAW format.
Definition: DImageIO_RAW.hpp:109
RES_T readRAW(const char *filename, size_t width, size_t height, size_t depth, Image< T > &image)
Get an image saved in a RAW format.
Definition: DImageIO_RAW.hpp:71