Commit 1d3ec770 authored by Manuel Günther's avatar Manuel Günther

Added PNG and TIFF

parent d123d796
......@@ -21,9 +21,7 @@
#include <bob.io.image/netpbm.h>
extern "C" {
#include "pnmio.h"
}
typedef unsigned long sample;
......
......@@ -2,12 +2,16 @@
* @file io/cxx/ImagePngFile.cc
* @date Fri Oct 12 12:08:00 2012 +0200
* @author Laurent El Shafey <laurent.el-shafey@idiap.ch>
* @author Manuel Gunther <siebenkopf@googlemail.com>
*
* @brief Implements an image format reader/writer using libpng.
*
* Copyright (C) 2011-2013 Idiap Research Institute, Martigny, Switzerland
* Copyright (c) 2016, Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs.
*/
#ifdef HAVE_LIBPNG
#include <boost/filesystem.hpp>
#include <boost/shared_array.hpp>
#include <boost/shared_ptr.hpp>
......@@ -17,7 +21,7 @@
#include <boost/algorithm/string.hpp>
#include <string>
#include <bob.io.base/File.h>
#include <bob.io.image/png.h>
extern "C" {
#include <png.h>
......@@ -462,108 +466,72 @@ static void im_save(const std::string& filename, const bob::io::base::array::int
png_destroy_write_struct(&png_ptr, &info_ptr);
}
class ImagePngFile: public bob::io::base::File {
public: //api
ImagePngFile(const char* path, char mode):
m_filename(path),
m_newfile(true) {
//checks if file exists
if (mode == 'r' && !boost::filesystem::exists(path)) {
boost::format m("file `%s' is not readable");
m % path;
throw std::runtime_error(m.str());
}
if (mode == 'r' || (mode == 'a' && boost::filesystem::exists(path))) {
{
im_peek(path, m_type);
m_length = 1;
m_newfile = false;
}
}
else {
m_length = 0;
m_newfile = true;
}
}
virtual ~ImagePngFile() { }
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
}
/**
* PNG class
*/
bob::io::image::PNGFile::PNGFile(const char* path, char mode)
: m_filename(path),
m_newfile(true)
{
//checks if file exists
if (mode == 'r' && !boost::filesystem::exists(path)) {
boost::format m("file `%s' is not readable");
m % path;
throw std::runtime_error(m.str());
}
virtual void read(bob::io::base::array::interface& buffer, size_t index) {
if (m_newfile)
throw std::runtime_error("uninitialized image file cannot be read");
if (mode == 'r' || (mode == 'a' && boost::filesystem::exists(path))) {
im_peek(path, m_type);
m_length = 1;
m_newfile = false;
} else {
m_length = 0;
m_newfile = true;
}
if (!buffer.type().is_compatible(m_type)) buffer.set(m_type);
}
if (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
void bob::io::image::PNGFile::read(bob::io::base::array::interface& buffer, size_t index) {
if (m_newfile)
throw std::runtime_error("uninitialized image file cannot be read");
if(!buffer.type().is_compatible(m_type)) buffer.set(m_type);
im_load(m_filename, buffer);
}
if (!buffer.type().is_compatible(m_type)) buffer.set(m_type);
virtual size_t append (const bob::io::base::array::interface& buffer) {
if (m_newfile) {
im_save(m_filename, buffer);
m_type = buffer.type();
m_newfile = false;
m_length = 1;
return 0;
}
if (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
throw std::runtime_error("image files only accept a single array");
}
if(!buffer.type().is_compatible(m_type)) buffer.set(m_type);
im_load(m_filename, buffer);
}
virtual void write (const bob::io::base::array::interface& buffer) {
//overwriting position 0 should always work
if (m_newfile) {
append(buffer);
return;
}
size_t bob::io::image::PNGFile::append(const bob::io::base::array::interface& buffer) {
if (m_newfile) {
im_save(m_filename, buffer);
m_type = buffer.type();
m_newfile = false;
m_length = 1;
return 0;
}
throw std::runtime_error("image files only accept a single array");
}
throw std::runtime_error("image files only accept a single array");
}
private: //representation
std::string m_filename;
bool m_newfile;
bob::io::base::array::typeinfo m_type;
size_t m_length;
void bob::io::image::PNGFile::write(const bob::io::base::array::interface& buffer) {
//overwriting position 0 should always work
if (m_newfile) {
append(buffer);
return;
}
static std::string s_codecname;
throw std::runtime_error("image files only accept a single array");
}
};
std::string ImagePngFile::s_codecname = "bob.image_png";
std::string bob::io::image::PNGFile::s_codecname = "bob.image_png";
boost::shared_ptr<bob::io::base::File> make_png_file (const char* path, char mode) {
return boost::make_shared<ImagePngFile>(path, mode);
return boost::make_shared<bob::io::image::PNGFile>(path, mode);
}
#endif // HAVE_LIBPNG
......@@ -316,7 +316,7 @@ int read_pbm_data(FILE *f, int *img_in, int img_size, int is_ascii, int img_widt
/* Decode the image contents byte-by-byte. */
for (k = 0; k < 8; k++) {
if (i >= img_size) return -1;
img_in[i++] = (lum_val >> (7-k)) & 0x1;
img_in[i++] = (lum_val >> (7-k)) & 0x1;
// fprintf(stderr, "i: %d, %d\n", i, img_in[i]);
row_position++;
if (row_position >= img_width) {
......
/*
* File : pnmio.h
* Description: Header file for pnmio.c.
* Description: Header file for pnmio.cpp.
* Author : Nikolaos Kavvadias <nikolaos.kavvadias@gmail.com>
* Copyright : (C) Nikolaos Kavvadias 2012, 2013, 2014, 2015, 2016
* Website : http://www.nkavvadias.com
......
......@@ -2,12 +2,16 @@
* @file io/cxx/ImageTiffFile.cc
* @date Fri Oct 12 12:08:00 2012 +0200
* @author Laurent El Shafey <laurent.el-shafey@idiap.ch>
* @author Manuel Gunther <siebenkopf@googlemail.com>
*
* @brief Implements an image format reader/writer using libtiff.
*
* Copyright (C) 2011-2013 Idiap Research Institute, Martigny, Switzerland
* Copyright (c) 2016, Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs.
*/
#ifdef HAVE_LIBTIFF
#include <boost/filesystem.hpp>
#include <boost/shared_array.hpp>
#include <boost/shared_ptr.hpp>
......@@ -17,7 +21,7 @@
#include <boost/algorithm/string.hpp>
#include <string>
#include <bob.io.base/File.h>
#include <bob.io.image/tiff.h>
extern "C" {
#include <tiffio.h>
......@@ -105,7 +109,7 @@ void im_load_gray(boost::shared_ptr<TIFF> in_file, bob::io::base::array::interfa
//Comment just to document
//PHOTOMETRIC_PALETTE: In this model, a color is described with a single component. The value of the component is used as an index into the red, green and blue curves in the ColorMap field to retrieve an RGB triplet that defines the color. When PhotometricInterpretation=3
// Deal with photometric interpretations
uint16 photo = PHOTOMETRIC_MINISBLACK;
if(TIFFGetField(in_file.get(), TIFFTAG_PHOTOMETRIC, &photo) == 0 || (photo != PHOTOMETRIC_MINISBLACK && photo != PHOTOMETRIC_MINISWHITE && photo != PHOTOMETRIC_PALETTE)){
......@@ -367,108 +371,71 @@ static void im_save(const std::string& filename, const bob::io::base::array::int
}
}
class ImageTiffFile: public bob::io::base::File {
public: //api
ImageTiffFile(const char* path, char mode):
m_filename(path),
m_newfile(true) {
//checks if file exists
if (mode == 'r' && !boost::filesystem::exists(path)) {
boost::format m("file '%s' is not readable");
m % path;
throw std::runtime_error(m.str());
}
if (mode == 'r' || (mode == 'a' && boost::filesystem::exists(path))) {
{
im_peek(path, m_type);
m_length = 1;
m_newfile = false;
}
}
else {
m_length = 0;
m_newfile = true;
}
}
virtual ~ImageTiffFile() { }
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
}
/**
* TIFF class
*/
virtual void read(bob::io::base::array::interface& buffer, size_t index) {
if (m_newfile)
throw std::runtime_error("uninitialized image file cannot be read");
bob::io::image::TIFFFile::TIFFFile(const char* path, char mode)
: m_filename(path),
m_newfile(true)
{
//checks if file exists
if (mode == 'r' && !boost::filesystem::exists(path)) {
boost::format m("file '%s' is not readable");
m % path;
throw std::runtime_error(m.str());
}
if (!buffer.type().is_compatible(m_type)) buffer.set(m_type);
if (mode == 'r' || (mode == 'a' && boost::filesystem::exists(path))) {
im_peek(path, m_type);
m_length = 1;
m_newfile = false;
} else {
m_length = 0;
m_newfile = true;
}
}
if (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
void bob::io::image::TIFFFile::read(bob::io::base::array::interface& buffer, size_t index) {
if (m_newfile)
throw std::runtime_error("uninitialized image file cannot be read");
if(!buffer.type().is_compatible(m_type)) buffer.set(m_type);
im_load(m_filename, buffer);
}
if (!buffer.type().is_compatible(m_type)) buffer.set(m_type);
virtual size_t append (const bob::io::base::array::interface& buffer) {
if (m_newfile) {
im_save(m_filename, buffer);
m_type = buffer.type();
m_newfile = false;
m_length = 1;
return 0;
}
if (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
throw std::runtime_error("image files only accept a single array");
}
virtual void write (const bob::io::base::array::interface& buffer) {
//overwriting position 0 should always work
if (m_newfile) {
append(buffer);
return;
}
if(!buffer.type().is_compatible(m_type)) buffer.set(m_type);
im_load(m_filename, buffer);
}
throw std::runtime_error("image files only accept a single array");
}
size_t bob::io::image::TIFFFile::append(const bob::io::base::array::interface& buffer) {
if (m_newfile) {
im_save(m_filename, buffer);
m_type = buffer.type();
m_newfile = false;
m_length = 1;
return 0;
}
private: //representation
std::string m_filename;
bool m_newfile;
bob::io::base::array::typeinfo m_type;
size_t m_length;
throw std::runtime_error("image files only accept a single array");
}
static std::string s_codecname;
void bob::io::image::TIFFFile::write(const bob::io::base::array::interface& buffer) {
//overwriting position 0 should always work
if (m_newfile) {
append(buffer);
return;
}
};
throw std::runtime_error("image files only accept a single array");
}
std::string ImageTiffFile::s_codecname = "bob.image_tiff";
std::string bob::io::image::TIFFFile::s_codecname = "bob.image_tiff";
boost::shared_ptr<bob::io::base::File> make_tiff_file (const char* path, char mode) {
return boost::make_shared<ImageTiffFile>(path, mode);
return boost::make_shared<bob::io::image::TIFFFile>(path, mode);
}
#endif // HAVE_LIBTIFF
/**
* @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>
/**
* @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;
};
template <class T, int N>
blitz::Array<T,N> read_png(const std::string& filename){
PNGFile png(filename.c_str(), 'r');
return png.read<T,N>(0);
}
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 */
/**
* @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 TIFF images using LIBTIFF
*
* 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_TIFF_H
#define BOB_IO_IMAGE_TIFF_H
#ifdef HAVE_LIBTIFF
#include <stdexcept>
#include <string>
#include <boost/shared_ptr.hpp>
#include <blitz/array.h>
#include <bob.io.base/File.h>
/**
* @brief Array submodule API of the I/O module
*/
namespace bob { namespace io { namespace image {
class TIFFFile: public bob::io::base::File {
public: //api
TIFFFile(const char* path, char mode);
virtual ~TIFFFile() { }
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;
};
template <class T, int N>
blitz::Array<T,N> read_tiff(const std::string& filename){
TIFFFile tiff(filename.c_str(), 'r');
return tiff.read<T,N>(0);
}
template <class T, int N>
void write_tiff(const blitz::Array<T,N>& image, const std::string& filename){
TIFFFile tiff(filename.c_str(), 'w');
tiff.write(image);
}
}}}
#endif // HAVE_LIBTIFF
#endif /* BOB_IO_IMAGE_TIFF_H */
......@@ -28,6 +28,8 @@
#include <bob.io.image/jpeg.h>
#include <bob.io.image/gif.h>
#include <bob.io.image/netpbm.h>
#include <bob.io.image/png.h>
#include <bob.io.image/tiff.h>
#ifdef HAVE_LIBJPEG
......@@ -118,6 +120,41 @@ BOB_TRY
throw std::runtime_error("JPEG color image IO did not succeed, check " + jpeg_color.string());
#endif
#ifdef HAVE_LIBPNG
// PNG
boost::filesystem::path png_gray(tempdir); png_gray /= std::string("gray.png");
bob::io::image::write_png(gray_image, png_gray.string());
blitz::Array<uint8_t, 2> gray_png = bob::io::image::read_png<uint8_t, 2>(png_gray.string());
if (blitz::any(blitz::abs(gray_image - gray_png) > 1))
throw std::runtime_error("JPEG gray image IO did not succeed, check " + png_gray.string());
boost::filesystem::path png_color(tempdir); png_color /= std::string("color.png");
bob::io::image::write_png(color_image, png_color.string());
blitz::Array<uint8_t, 3> color_png = bob::io::image::read_png<uint8_t, 3>(png_color.string());
if (blitz::any(blitz::abs(color_image - color_png) > 1))
throw std::runtime_error("JPEG color image IO did not succeed, check " + png_color.string());
#endif
#ifdef HAVE_LIBTIFF
// TIFF
boost::filesystem::path tiff_gray(tempdir); tiff_gray /= std::string("gray.tiff");
bob::io::image::write_tiff(gray_image, tiff_gray.string());
blitz::Array<uint8_t, 2> gray_tiff = bob::io::image::read_tiff<uint8_t, 2>(tiff_gray.string());
if (blitz::any(blitz::abs(gray_image - gray_tiff) > 1))
throw std::runtime_error("JPEG gray image IO did not succeed, check " + tiff_gray.string());
boost::filesystem::path tiff_color(tempdir); tiff_color /= std::string("color.tiff");
bob::io::image::write_tiff(color_image, tiff_color.string());
blitz::Array<uint8_t, 3> color_tiff = bob::io::image::read_tiff<uint8_t, 3>(tiff_color.string());
if (blitz::any(blitz::abs(color_image - color_tiff) > 1))
throw std::runtime_error("JPEG color image IO did not succeed, check " + tiff_color.string());
#endif
Py_RETURN_NONE;
BOB_CATCH_FUNCTION("_test_io", 0)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment