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

Provided JPEGFile class and fast accessor functions (read_jpeg and write_jpeg)

parent 77b22e07
......@@ -2,7 +2,7 @@
* @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
* @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.
......@@ -18,8 +18,10 @@
* 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_IO_H
#define BOB_IO_IMAGE_IO_H
#ifndef BOB_IO_IMAGE_JPEG_H
#define BOB_IO_IMAGE_JPEG_H
#ifdef HAVE_LIBJPEG
#include <stdexcept>
#include <string>
......@@ -27,19 +29,79 @@
#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 {
#ifdef HAVE_LIBJPEG
template <int N>
blitz::Array<uint8_t,N> read_jpeg(const std::string& filename);
class JPEGFile: public bob::io::base::File {
public: //api
JPEGFile(const char* path, char mode);
virtual ~JPEGFile() { }
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;
}
template <int N>
void write_jpeg(const blitz::Array<uint8_t,N>& image, const std::string& filename);
#endif
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_jpeg(const std::string& filename){
JPEGFile jpeg(filename.c_str(), 'r');
return jpeg.read<T,N>(0);
}
template <class T, int N>
void write_jpeg(const blitz::Array<T,N>& image, const std::string& filename){
JPEGFile jpeg(filename.c_str(), 'w');
jpeg.write(image);
}
}}}
#endif /* BOB_IO_IMAGE_IO_H */
#endif // HAVE_LIBJPEG
#endif /* BOB_IO_IMAGE_JPEG_H */
......@@ -20,8 +20,7 @@
#include <boost/algorithm/string.hpp>
#include <string>
#include <bob.io.base/File.h>
#include <bob.io.image/io.h>
#include <bob.io.image/jpeg.h>
#include <jpeglib.h>
......@@ -304,130 +303,78 @@ 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);
}
template <int N>
blitz::Array<uint8_t,N> bob::io::image::read_jpeg(const std::string& filename){
ImageJpegFile jpeg(filename.c_str(), 'r');
return dynamic_cast<bob::io::base::File&>(jpeg).read<uint8_t,N>(0);
return boost::make_shared<bob::io::image::JPEGFile>(path, mode);
}
template <int N>
void bob::io::image::write_jpeg(const blitz::Array<uint8_t,N>& image, const std::string& filename){
ImageJpegFile jpeg(filename.c_str(), 'w');
dynamic_cast<bob::io::base::File&>(jpeg).write(image);
}
// instantiate
template blitz::Array<uint8_t, 2> bob::io::image::read_jpeg(const std::string&);
template blitz::Array<uint8_t, 3> bob::io::image::read_jpeg(const std::string&);
template void bob::io::image::write_jpeg(const blitz::Array<uint8_t, 2>&, const std::string&);
template void bob::io::image::write_jpeg(const blitz::Array<uint8_t, 3>&, const std::string&);
#endif // HAVE_LIBJPEG
......@@ -17,7 +17,7 @@
#include "file.h"
#include <bob.extension/documentation.h>
#include <bob.io.image/io.h>
#include <bob.io.image/jpeg.h>
#include <boost/format.hpp>
#include <boost/filesystem.hpp>
......@@ -53,14 +53,14 @@ BOB_TRY
#ifdef HAVE_LIBJPEG
boost::filesystem::path jpeg_gray(tempdir); jpeg_gray /= std::string("gray.jpg");
bob::io::image::write_jpeg(gray_image, jpeg_gray.string());
blitz::Array<uint8_t, 2> gray_jpeg = bob::io::image::read_jpeg<2>(jpeg_gray.string());
blitz::Array<uint8_t, 2> gray_jpeg = bob::io::image::read_jpeg<uint8_t, 2>(jpeg_gray.string());
if (blitz::any(blitz::abs(gray_image - gray_jpeg) > 10))
throw std::runtime_error("Gray image IO did not succeed, check " + jpeg_gray.string());
boost::filesystem::path jpeg_color(tempdir); jpeg_color /= std::string("color.jpg");
bob::io::image::write_jpeg(color_image, jpeg_color.string());
blitz::Array<uint8_t, 3> color_jpeg = bob::io::image::read_jpeg<3>(jpeg_color.string());
blitz::Array<uint8_t, 3> color_jpeg = bob::io::image::read_jpeg<uint8_t, 3>(jpeg_color.string());
if (blitz::any(blitz::abs(color_image - color_jpeg) > 10))
throw std::runtime_error("Color image IO did not succeed, check " + jpeg_color.string());
......
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