Commit 980d1f74 authored by Manuel Günther's avatar Manuel Günther

Implemented GIF

parent 66beee31
......@@ -8,6 +8,8 @@
* Copyright (C) 2011-2013 Idiap Research Institute, Martigny, Switzerland
*/
#ifdef HAVE_GIFLIB
#include <boost/filesystem.hpp>
#include <boost/shared_array.hpp>
#include <boost/shared_ptr.hpp>
......@@ -17,7 +19,7 @@
#include <boost/algorithm/string.hpp>
#include <string>
#include <bob.io.base/File.h>
#include <bob.io.image/gif.h>
extern "C" {
#include <gif_lib.h>
......@@ -564,109 +566,70 @@ static void im_save(const std::string& filename, const bob::io::base::array::int
}
class ImageGifFile: public bob::io::base::File {
public: //api
ImageGifFile(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 ~ImageGifFile() { }
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);
if (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
/**
* GIF class
*/
bob::io::image::GIFFile::GIFFile(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);
im_load(m_filename, buffer);
}
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 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;
}
void bob::io::image::GIFFile::read(bob::io::base::array::interface& buffer, size_t index) {
if (m_newfile)
throw std::runtime_error("uninitialized image file cannot be read");
throw std::runtime_error("image files only accept a single array");
}
if (!buffer.type().is_compatible(m_type)) buffer.set(m_type);
virtual void write (const bob::io::base::array::interface& buffer) {
//overwriting position 0 should always work
if (m_newfile) {
append(buffer);
return;
}
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);
}
private: //representation
std::string m_filename;
bool m_newfile;
bob::io::base::array::typeinfo m_type;
size_t m_length;
size_t bob::io::image::GIFFile::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;
}
static std::string s_codecname;
throw std::runtime_error("image files only accept a single array");
}
};
void bob::io::image::GIFFile::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 ImageGifFile::s_codecname = "bob.image_gif";
std::string bob::io::image::GIFFile::s_codecname = "bob.image_gif";
boost::shared_ptr<bob::io::base::File> make_gif_file (const char* path, char mode) {
return boost::make_shared<ImageGifFile>(path, mode);
return boost::make_shared<bob::io::image::GIFFile>(path, mode);
}
#endif // HAVE_GIFLIB
/**
* @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
*
* 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_GIF_H
#define BOB_IO_IMAGE_GIF_H
#ifdef HAVE_GIFLIB
#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 GIFFile: public bob::io::base::File {
public: //api
GIFFile(const char* path, char mode);
virtual ~GIFFile() { }
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_gif(const std::string& filename){
GIFFile gif(filename.c_str(), 'r');
return gif.read<T,N>(0);
}
template <class T, int N>
void write_gif(const blitz::Array<T,N>& image, const std::string& filename){
GIFFile gif(filename.c_str(), 'w');
gif.write(image);
}
}}}
#endif // HAVE_GIFLIB
#endif /* BOB_IO_IMAGE_GIF_H */
......@@ -22,6 +22,7 @@
#include <bob.io.image/bmp.h>
#include <bob.io.image/jpeg.h>
#include <bob.io.image/gif.h>
#ifdef HAVE_LIBJPEG
......@@ -61,6 +62,24 @@ BOB_TRY
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());
#ifdef HAVE_LIBGIF
// GIF
boost::filesystem::path gif_gray(tempdir); gif_gray /= std::string("gray.gif");
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))
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))
throw std::runtime_error("GIF color image IO did not succeed, check " + gif_color.string());
#endif
#ifdef HAVE_LIBJPEG
// JPEG
boost::filesystem::path jpeg_gray(tempdir); jpeg_gray /= std::string("gray.jpg");
......@@ -151,9 +170,11 @@ static PyObject* create_module (void) {
}
#endif
#ifdef HAVE_GIFLIB
if (!PyBobIoCodec_Register(".gif", "GIF (giflib)", &make_gif_file)) {
PyErr_Print();
}
#endif // HAVE_GIFLIB
if (!PyBobIoCodec_Register(".pbm", "PBM, indexed (libnetpbm)",
&make_netpbm_file)) {
......
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