png.h 4.25 KB
Newer Older
Manuel Günther's avatar
Manuel Günther committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * @date Wed May 11 12:39:37 MDT 2016
 * @author Manuel Gunther <siebenkopf@googlemail.com>
 *
 * @brief The file provides an easy C++ interface to read and write PNG images using LIBPNG
 *
 * Copyright (c) 2016, Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs.
 * All rights reserved.

 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef BOB_IO_IMAGE_PNG_H
#define BOB_IO_IMAGE_PNG_H

#ifdef HAVE_LIBPNG

#include <stdexcept>
#include <string>

#include <boost/shared_ptr.hpp>
#include <blitz/array.h>

#include <bob.io.base/File.h>
33
#include <bob.core/array_convert.h>
Manuel Günther's avatar
Manuel Günther committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91


/**
 * @brief Array submodule API of the I/O module
 */
namespace bob { namespace io { namespace image {

  class PNGFile: public bob::io::base::File {

    public: //api


      PNGFile(const char* path, char mode);

      virtual ~PNGFile() { }

      virtual const char* filename() const {
        return m_filename.c_str();
      }

      virtual const bob::io::base::array::typeinfo& type_all() const {
        return m_type;
      }

      virtual const bob::io::base::array::typeinfo& type() const {
        return m_type;
      }

      virtual size_t size() const {
        return m_length;
      }

      virtual const char* name() const {
        return s_codecname.c_str();
      }

      virtual void read_all(bob::io::base::array::interface& buffer) {
        read(buffer, 0); ///we only have 1 image in an image file anyways
      }

      virtual void read(bob::io::base::array::interface& buffer, size_t index);

      virtual size_t append (const bob::io::base::array::interface& buffer);

      virtual void write (const bob::io::base::array::interface& buffer);

      using bob::io::base::File::write;
      using bob::io::base::File::read;

    private: //representation
      std::string m_filename;
      bool m_newfile;
      bob::io::base::array::typeinfo m_type;
      size_t m_length;

      static std::string s_codecname;
  };

92
  inline bool is_color_png(const std::string& filename){
93
94
95
96
    PNGFile png(filename.c_str(), 'r');
    return png.type().nd == 3;
  }

Manuel Günther's avatar
Manuel Günther committed
97
98
99
  template <class T, int N>
  blitz::Array<T,N> read_png(const std::string& filename){
    PNGFile png(filename.c_str(), 'r');
100
101
102
103
104
105
106
107
108
109
110
111
    switch (png.type().dtype){
      case bob::io::base::array::t_uint8:{
        blitz::Array<uint8_t, N> image(png.read<uint8_t, N>(0));
        return bob::core::array::convert<T>(image);
      }
      case bob::io::base::array::t_uint16:{
        blitz::Array<uint16_t, N> image(png.read<uint16_t, N>(0));
        return bob::core::array::convert<T>(image);
      }
      default:
        throw std::runtime_error("The png image has a weired data type");
    }
Manuel Günther's avatar
Manuel Günther committed
112
113
114
115
116
117
118
119
120
121
122
123
124
  }

  template <class T, int N>
  void write_png(const blitz::Array<T,N>& image, const std::string& filename){
    PNGFile png(filename.c_str(), 'w');
    png.write(image);
  }

}}}

#endif // HAVE_LIBPNG

#endif /* BOB_IO_IMAGE_PNG_H */