Commit d123d796 authored by Manuel Günther's avatar Manuel Günther

Implemented NetPBM

parent 980d1f74
......@@ -2,10 +2,12 @@
* @file io/cxx/ImageBmpFile.cc
* @date Wed Nov 28 15:36: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 for BMP files.
*
* 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.
*/
#include <boost/filesystem.hpp>
......
......@@ -2,10 +2,12 @@
* @file io/cxx/ImageGifFile.cc
* @date Fri Nov 23 16:53: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 giflib.
*
* 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_GIFLIB
......
......@@ -2,11 +2,13 @@
* @file io/cxx/ImageJpegFile.cc
* @date Wed Oct 10 16:38: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 libjpeg.
* This codec is only able to work with 3D input/output.
*
* 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_LIBJPEG
......
......@@ -2,11 +2,13 @@
* @file io/cxx/ImageNetpbmFile.cc
* @date Tue Oct 9 18:13: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 libnetpbm.
* This codec is only able to work with 2D and 3D input.
*
* 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.
*/
#include <boost/filesystem.hpp>
......@@ -17,7 +19,7 @@
#include <boost/algorithm/string.hpp>
#include <string>
#include <bob.io.base/File.h>
#include <bob.io.image/netpbm.h>
extern "C" {
#include "pnmio.h"
......@@ -492,110 +494,69 @@ static void im_save (const std::string& filename, const bob::io::base::array::in
}
/**
* NetPBM class
*/
class ImageNetpbmFile: public bob::io::base::File {
public: //api
ImageNetpbmFile(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 ~ImageNetpbmFile() { }
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) {
if (m_newfile)
throw std::runtime_error("uninitialized image file cannot be read");
if (!buffer.type().is_compatible(m_type)) buffer.set(m_type);
bob::io::image::NetPBMFile::NetPBMFile(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 (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
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);
im_load(m_filename, buffer);
}
void bob::io::image::NetPBMFile::read(bob::io::base::array::interface& buffer, size_t index) {
if (m_newfile)
throw std::runtime_error("uninitialized image file cannot be read");
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 (!buffer.type().is_compatible(m_type)) buffer.set(m_type);
throw std::runtime_error("image files only accept a single array");
}
if (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
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::NetPBMFile::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::NetPBMFile::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 ImageNetpbmFile::s_codecname = "bob.image_netpbm";
std::string bob::io::image::NetPBMFile::s_codecname = "bob.image_netpbm";
boost::shared_ptr<bob::io::base::File> make_netpbm_file (const char* path, char mode) {
return boost::make_shared<ImageNetpbmFile>(path, mode);
return boost::make_shared<bob::io::image::NetPBMFile>(path, mode);
}
......@@ -2,9 +2,9 @@
* @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 JPEG images
* @brief The file provides an easy C++ interface to read and write BMP images using our own codec
*
* Copyright (c) 2016 , Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs.
* 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:
......
......@@ -2,9 +2,9 @@
* @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 JPEG images
* @brief The file provides an easy C++ interface to read and write GIF images using GIFLIB
*
* Copyright (c) 2016 , Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs.
* 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:
......
......@@ -2,9 +2,9 @@
* @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 JPEG images
* @brief The file provides an easy C++ interface to read and write JPEG images using LIBJPEG
*
* Copyright (c) 2016 , Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs.
* 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:
......
/**
* @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 images using the NetPBM interface
*
* 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_NETPBM_H
#define BOB_IO_IMAGE_NETPBM_H
#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 NetPBMFile: public bob::io::base::File {
public: //api
NetPBMFile(const char* path, char mode);
virtual ~NetPBMFile() { }
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_p_m(const std::string& filename){
NetPBMFile p_m(filename.c_str(), 'r');
return p_m.read<T,N>(0);
}
template <class T, int N>
void write_p_m(const blitz::Array<T,N>& image, const std::string& filename){
NetPBMFile p_m(filename.c_str(), 'w');
p_m.write(image);
}
}}}
#endif /* BOB_IO_IMAGE_NETPBM_H */
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @author Manuel Gunther <siebenkopf@googlemail.com>
* @date Fri 16 May 12:33:38 2014 CEST
*
* @brief Pythonic bindings
*
* 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 NO_IMPORT_ARRAY
......@@ -23,6 +27,7 @@
#include <bob.io.image/bmp.h>
#include <bob.io.image/jpeg.h>
#include <bob.io.image/gif.h>
#include <bob.io.image/netpbm.h>
#ifdef HAVE_LIBJPEG
......@@ -55,12 +60,12 @@ BOB_TRY
color_image(i, blitz::Range::all(), blitz::Range::all()) = gray_image(blitz::Range::all(), blitz::Range::all());
// BMP; only color images are supported
boost::filesystem::path bmp_color(tempdir); bmp_color /= std::string("color.bmp");
bob::io::image::write_bmp(color_image, bmp_color.string());
blitz::Array<uint8_t, 3> color_bmp = bob::io::image::read_bmp<uint8_t>(bmp_color.string());
boost::filesystem::path bmp(tempdir); bmp /= std::string("color.bmp");
bob::io::image::write_bmp(color_image, bmp.string());
blitz::Array<uint8_t, 3> color_bmp = bob::io::image::read_bmp<uint8_t>(bmp.string());
if (blitz::any(blitz::abs(color_image - color_bmp) > 0))
throw std::runtime_error("BMP color image IO did not succeed, check " + bmp_color.string());
throw std::runtime_error("BMP image IO did not succeed, check " + bmp.string());
#ifdef HAVE_LIBGIF
......@@ -69,17 +74,33 @@ BOB_TRY
bob::io::image::write_gif(gray_image, gif_gray.string());
blitz::Array<uint8_t, 2> gray_gif = bob::io::image::read_gif<uint8_t, 2>(gif_gray.string());
if (blitz::any(blitz::abs(gray_image - gray_gif) > 10))
if (blitz::any(blitz::abs(gray_image - gray_gif) > 0))
throw std::runtime_error("GIF gray image IO did not succeed, check " + gif_gray.string());
boost::filesystem::path gif_color(tempdir); gif_color /= std::string("color.gif");
bob::io::image::write_gif(color_image, gif_color.string());
blitz::Array<uint8_t, 3> color_gif = bob::io::image::read_gif<uint8_t, 3>(gif_color.string());
if (blitz::any(blitz::abs(color_image - color_gif) > 10))
if (blitz::any(blitz::abs(color_image - color_gif) > 0))
throw std::runtime_error("GIF color image IO did not succeed, check " + gif_color.string());
#endif
// NetPBM
boost::filesystem::path pgm(tempdir); pgm /= std::string("gray.pgm");
bob::io::image::write_p_m(gray_image, pgm.string());
blitz::Array<uint8_t, 2> gray_pgm = bob::io::image::read_p_m<uint8_t, 2>(pgm.string());
if (blitz::any(blitz::abs(gray_image - gray_pgm) > 0))
throw std::runtime_error("PGM image IO did not succeed, check " + pgm.string());
boost::filesystem::path ppm(tempdir); ppm /= std::string("color.ppm");
bob::io::image::write_p_m(color_image, ppm.string());
blitz::Array<uint8_t, 3> color_ppm = bob::io::image::read_p_m<uint8_t, 3>(ppm.string());
if (blitz::any(blitz::abs(color_image - color_ppm) > 0))
throw std::runtime_error("PPM image IO did not succeed, check " + ppm.string());
#ifdef HAVE_LIBJPEG
// JPEG
boost::filesystem::path jpeg_gray(tempdir); jpeg_gray /= std::string("gray.jpg");
......
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