Commit 157a498d authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira

Merge pull request #11 from bioidiap/C++interface

C++interface
parents 8c2089e9 687fc278
# import Libraries of other lib packages
import bob.io.base
# import our own Library
import bob.extension
bob.extension.load_bob_library('bob.io.image', __file__)
from . import _library
from . import version
from .version import module as __version__
import os
def get_config():
"""Returns a string containing the configuration information.
"""
import bob.extension
return bob.extension.get_config(__name__, version.externals)
def get_include_directories():
"""get_include_directories() -> includes
Returns a list of include directories for dependent libraries, such as libjpeg, libtiff, ...
This function is automatically used by :py:func:`bob.extension.get_bob_libraries` to retrieve the non-standard include directories that are required to use the C bindings of this library in dependent classes.
You shouldn't normally need to call this function by hand.
**Returns:**
``includes`` : [str]
The list of non-standard include directories required to use the C bindings of this class.
For now, only the directory for the HDF5 headers are returned.
"""
# try to use pkg_config first
from bob.extension.utils import find_header, uniq_paths
from bob.extension import pkgconfig
import logging
logger = logging.getLogger("bob.io.image")
directories = []
for name, header in (('libjpeg', 'jpeglib.h'), ('libtiff', 'tiff.h'), ('giflib', 'gif_lib.h')):
# locate pkg-config on our own
candidates = find_header(header)
if not candidates:
logger.warn("could not find %s's `%s' - have you installed %s on this machine?" % (name, header, name))
directories.append(os.path.dirname(candidates[0]))
for name in ("libpng",):
try:
pkg = pkgconfig(name)
directories.extend(pkg.include_directories())
except:
pass
return uniq_paths(directories)
def get_macros():
"""get_macros() -> macros
Returns a list of preprocessor macros, such as ``(HAVE_LIBJPEG, 1)``.
This function is automatically used by :py:func:`bob.extension.get_bob_libraries` to retrieve the prerpocessor definitions that are required to use the C bindings of this library in dependent classes.
You shouldn't normally need to call this function by hand.
**Returns:**
``macros`` : [str]
The list of preprocessor macros required to use the C bindings of this class.
"""
# try to use pkg_config first
from bob.extension.utils import find_header, uniq_paths
from bob.extension import pkgconfig
macros = []
for define, header in (('HAVE_LIBJPEG', 'jpeglib.h'), ('HAVE_LIBTIFF', 'tiff.h'), ('HAVE_GIFLIB', 'gif_lib.h')):
# locate pkg-config on our own
candidates = find_header(header)
if candidates:
macros.append((define, '1'))
for define, name in (("HAVE_LIBPNG", "libpng"),):
try:
pkg = pkgconfig(name)
macros.append((define, '1'))
except:
pass
return macros
# gets sphinx autodoc done right - don't remove it
__all__ = [_ for _ in dir() if not _.startswith('_')]
......@@ -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>
......@@ -17,7 +19,7 @@
#include <boost/algorithm/string.hpp>
#include <string>
#include <bob.io.base/File.h>
#include <bob.io.image/bmp.h>
// The following documentation is mostly coming from wikipedia:
// http://en.wikipedia.org/wiki/BMP_file_format
......@@ -918,120 +920,81 @@ static void im_save(const std::string& filename, const bob::io::base::array::int
im_save_color(array, out_file.get());
}
else {
boost::format m("the image in file `%s' has a number of dimensions for which this bmp codec has no support for: %s");
boost::format m("the image in file `%s' has a number of dimensions for which this bmp codec has no support for");
m % info.str();
throw std::runtime_error(m.str());
}
}
else {
boost::format m("the image in file `%s' has a data type this bmp codec has no support for: %s");
boost::format m("the image in file `%s' has a data type this bmp codec has no support for");
m % info.str();
throw std::runtime_error(m.str());
}
}
class ImageBmpFile: public bob::io::base::File {
public: //api
ImageBmpFile(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 ~ImageBmpFile() { }
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
}
/**
* BMP class
*/
bob::io::image::BMPFile::BMPFile(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);
void bob::io::image::BMPFile::read(bob::io::base::array::interface& buffer, size_t index) {
if (m_newfile)
throw std::runtime_error("uninitialized image file cannot be read");
if (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
if (!buffer.type().is_compatible(m_type)) buffer.set(m_type);
if(!buffer.type().is_compatible(m_type)) buffer.set(m_type);
im_load(m_filename, buffer);
}
if (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
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);
im_load(m_filename, buffer);
}
throw std::runtime_error("image files only accept a single array");
}
size_t bob::io::image::BMPFile::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;
}
virtual void 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");
}
throw std::runtime_error("image files only accept a single array");
}
void bob::io::image::BMPFile::write(const bob::io::base::array::interface& buffer) {
//overwriting position 0 should always work
if (m_newfile) {
append(buffer);
return;
}
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;
};
std::string ImageBmpFile::s_codecname = "bob.image_bmp";
std::string bob::io::image::BMPFile::s_codecname = "bob.image_bmp";
boost::shared_ptr<bob::io::base::File> make_bmp_file (const char* path, char mode) {
return boost::make_shared<ImageBmpFile>(path, mode);
return boost::make_shared<bob::io::image::BMPFile>(path, mode);
}
......@@ -2,12 +2,16 @@
* @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
#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/gif.h>
extern "C" {
#include <gif_lib.h>
......@@ -564,109 +568,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
......@@ -2,13 +2,17 @@
* @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
#include <boost/filesystem.hpp>
#include <boost/shared_array.hpp>
#include <boost/shared_ptr.hpp>
......@@ -18,7 +22,7 @@
#include <boost/algorithm/string.hpp>
#include <string>
#include <bob.io.base/File.h>
#include <bob.io.image/jpeg.h>
#include <jpeglib.h>
......@@ -301,108 +305,75 @@ static void im_save (const std::string& filename, const bob::io::base::array::in
}
class ImageJpegFile: public bob::io::base::File {
public: //api
ImageJpegFile(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 ~ImageJpegFile() { }
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;
}
/**
* JPEG class
*/
virtual size_t size() const {
return m_length;
}
bob::io::image::JPEGFile::JPEGFile(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 const char* name() const {
return s_codecname.c_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 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");
void bob::io::image::JPEGFile::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 (!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");
if (index != 0)
throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");
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;
}
// load jpeg
im_load(m_filename, buffer);
}
throw std::runtime_error("image files only accept a single array");
}
size_t bob::io::image::JPEGFile::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;
}
virtual void 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");
}
throw std::runtime_error("image files only accept a single array");
}
void bob::io::image::JPEGFile::write(const bob::io::base::array::interface& buffer) {
//overwriting position 0 should always work
if (m_newfile) {
append(buffer);
return;
}
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;
};
std::string ImageJpegFile::s_codecname = "bob.image_jpeg";
std::string bob::io::image::JPEGFile::s_codecname = "bob.image_jpeg";
boost::shared_ptr<bob::io::base::File> make_jpeg_file (const char* path, char mode) {
return boost::make_shared<ImageJpegFile>(path, mode);
return boost::make_shared<bob::io::image::JPEGFile>(path, mode);
}
#endif // 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,11 +19,9 @@
#include <boost/algorithm/string.hpp>
#include <string>
#include <bob.io.base/File.h>
#include <bob.io.image/netpbm.h>
extern "C" {
#include "pnmio.h"
}
typedef unsigned long sample;
......@@ -408,7 +408,7 @@ static void im_save_color(const bob::io::base::array::interface& b, struct pam *
img_data[c+0] = element_r[y*info.shape[2] + x];
img_data[c+1] = element_g[y*info.shape[2] + x];
img_data[c+2] = element_b[y*info.shape[2] + x];
c = c + 3;
c += 3;
}
}
pnm_writepam(out_pam, img_data);
......@@ -492,110 +492,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),