Commit 66beee31 authored by Manuel Günther's avatar Manuel Günther

Implemented BMP

parent aef37cd7
......@@ -17,7 +17,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 +918,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);
}
......@@ -319,13 +319,10 @@ bob::io::image::JPEGFile::JPEGFile(const char* path, char mode)
}
if (mode == 'r' || (mode == 'a' && boost::filesystem::exists(path))) {
{
im_peek(path, m_type);
m_length = 1;
m_newfile = false;
}
}
else {
im_peek(path, m_type);
m_length = 1;
m_newfile = false;
} else {
m_length = 0;
m_newfile = true;
}
......
/**
* @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_BMP_H
#define BOB_IO_IMAGE_BMP_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 BMPFile: public bob::io::base::File {
public: //api
BMPFile(const char* path, char mode);
virtual ~BMPFile() { }
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>
blitz::Array<T,3> read_bmp(const std::string& filename){
BMPFile bmp(filename.c_str(), 'r');
return bmp.read<T,3>(0);
}
template <class T>
void write_bmp(const blitz::Array<T,3>& image, const std::string& filename){
BMPFile bmp(filename.c_str(), 'w');
bmp.write(image);
}
}}}
#endif /* BOB_IO_IMAGE_BMP_H */
......@@ -17,10 +17,13 @@
#include "file.h"
#include <bob.extension/documentation.h>
#include <bob.io.image/jpeg.h>
#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <bob.io.image/bmp.h>
#include <bob.io.image/jpeg.h>
#ifdef HAVE_LIBJPEG
#include <jpeglib.h>
#endif
......@@ -50,20 +53,29 @@ BOB_TRY
for (int i = 0; i < 3; ++i)
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());
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_LIBJPEG
// JPEG
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<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());
throw std::runtime_error("JPEG 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<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());
throw std::runtime_error("JPEG color image IO did not succeed, check " + jpeg_color.string());
#endif
Py_RETURN_NONE;
......
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