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

Added a generic image IO function based on filename extension (only for uint8_t)

parent 157a498d
/**
* @date Wed May 25 13:05:58 MDT 2016
* @author Manuel Gunther <siebenkopf@googlemail.com>
*
* @brief The file provides a generic interface to read any kind of images that we support
*
* 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_IMAGE_H
#define BOB_IO_IMAGE_IMAGE_H
#include <bob.io.image/bmp.h>
#include <bob.io.image/png.h>
#include <bob.io.image/gif.h>
#include <bob.io.image/jpeg.h>
#include <bob.io.image/netpbm.h>
#include <bob.io.image/tiff.h>
#include <boost/filesystem/path.hpp>
#include <boost/algorithm/string.hpp>
namespace bob { namespace io { namespace image {
inline bool is_color_image(const std::string& filename){
std::string extension = boost::filesystem::path(filename).extension().string();
boost::algorithm::to_lower(extension);
if (extension == ".bmp") return true;
#ifdef HAVE_GIFLIB
if (extension == ".gif") return true;
#endif
#ifdef HAVE_LIBPNG
if (extension == ".png") return is_color_png(filename);
#endif
#ifdef HAVE_LIBJPEG
if (extension == ".jpg" || extension == ".jpeg") return is_color_jpeg(filename);
#endif
#ifdef HAVE_LIBTIFF
if (extension == ".tif" || extension == ".tiff") return is_color_tiff(filename);
#endif
if (extension == ".pgm" || extension == ".pbm") return false;
if (extension == ".ppm") return true;
throw std::runtime_error("The filename extension '" + extension + "' is not known");
}
inline blitz::Array<uint8_t,3> read_color_image(const std::string& filename){
std::string extension = boost::filesystem::path(filename).extension().string();
boost::algorithm::to_lower(extension);
if (extension == ".bmp") return read_bmp(filename);
#ifdef HAVE_GIFLIB
if (extension == ".gif") return read_gif(filename);
#endif
#ifdef HAVE_LIBPNG
if (extension == ".png") return read_png<uint8_t,3>(filename);
#endif
#ifdef HAVE_LIBJPEG
if (extension == ".jpg" || extension == ".jpeg") return read_jpeg<3>(filename);
#endif
#ifdef HAVE_LIBTIFF
if (extension == ".tif" || extension == ".tiff") return read_tiff<uint8_t,3>(filename);
#endif
if (extension == ".ppm") return read_ppm<uint8_t>(filename);
throw std::runtime_error("The filename extension '" + extension + "' is not known or not supported for color images");
}
blitz::Array<uint8_t,2> read_gray_image(const std::string& filename){
std::string extension = boost::filesystem::path(filename).extension().string();
boost::algorithm::to_lower(extension);
#ifdef HAVE_LIBPNG
if (extension == ".png") return read_png<uint8_t,2>(filename);
#endif
#ifdef HAVE_LIBJPEG
if (extension == ".jpg" || extension == ".jpeg") return read_jpeg<2>(filename); // this will only work for T=uint8_t
#endif
#ifdef HAVE_LIBTIFF
if (extension == ".tif" || extension == ".tiff") return read_tiff<uint8_t,2>(filename);
#endif
if (extension == ".pgm") return read_pgm<uint8_t>(filename);
if (extension == ".pbm") return read_pbm<uint8_t>(filename);
throw std::runtime_error("The filename extension '" + extension + "' is not known or not supported for gray images");
}
void write_color_image(const blitz::Array<uint8_t,3>& image, const std::string& filename){
std::string extension = boost::filesystem::path(filename).extension().string();
boost::algorithm::to_lower(extension);
if (extension == ".bmp") return write_bmp(image, filename); // this will only work for T=uint8_t
#ifdef HAVE_GIFLIB
if (extension == ".gif") return write_gif(image, filename); // this will only work for T=uint8_t
#endif
#ifdef HAVE_LIBPNG
if (extension == ".png") return write_png(image, filename);
#endif
#ifdef HAVE_LIBJPEG
if (extension == ".jpg" || extension == ".jpeg") return write_jpeg(image, filename); // this will only work for T=uint8_t
#endif
#ifdef HAVE_LIBTIFF
if (extension == ".tif" || extension == ".tiff") return write_tiff(image, filename);
#endif
if (extension == ".ppm") return write_ppm(image, filename);
throw std::runtime_error("The filename extension '" + extension + "' is not known or not supported for color images");
}
void write_gray_image(const blitz::Array<uint8_t,2>& image, const std::string& filename){
std::string extension = boost::filesystem::path(filename).extension().string();
boost::algorithm::to_lower(extension);
#ifdef HAVE_LIBPNG
if (extension == ".png") return write_png(image, filename);
#endif
#ifdef HAVE_LIBJPEG
if (extension == ".jpg" || extension == ".jpeg") return write_jpeg(image, filename); // this will only work for T=uint8_t
#endif
#ifdef HAVE_LIBTIFF
if (extension == ".tif" || extension == ".tiff") return write_tiff(image, filename);
#endif
if (extension == ".pgm") return write_pgm(image, filename);
if (extension == ".pbm") return write_pbm(image, filename);
throw std::runtime_error("The filename extension '" + extension + "' is not known or not supported for gray images");
}
} } } // namespace
#endif // BOB_IO_IMAGE_IMAGE_H
......@@ -24,12 +24,7 @@
#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <bob.io.image/bmp.h>
#include <bob.io.image/jpeg.h>
#include <bob.io.image/gif.h>
#include <bob.io.image/netpbm.h>
#include <bob.io.image/png.h>
#include <bob.io.image/tiff.h>
#include <bob.io.image/image.h>
#ifdef HAVE_LIBJPEG
......@@ -63,9 +58,9 @@ BOB_TRY
// BMP; only color images are supported
boost::filesystem::path bmp(tempdir); bmp /= std::string("color.bmp");
bob::io::image::write_bmp(color_image, bmp.string());
bob::io::image::write_color_image(color_image, bmp.string());
blitz::Array<uint8_t, 3> color_bmp = bob::io::image::read_bmp(bmp.string());
blitz::Array<uint8_t, 3> color_bmp = bob::io::image::read_color_image(bmp.string());
if (blitz::any(blitz::abs(color_image - color_bmp) > 0))
throw std::runtime_error("BMP image IO did not succeed, check " + bmp.string());
......@@ -73,29 +68,29 @@ BOB_TRY
#ifdef HAVE_GIFLIB
// GIF; only color images are supported
boost::filesystem::path gif(tempdir); gif /= std::string("color.gif");
bob::io::image::write_gif(color_image, gif.string());
bob::io::image::write_color_image(color_image, gif.string());
blitz::Array<uint8_t, 3> color_gif = bob::io::image::read_gif(gif.string());
blitz::Array<uint8_t, 3> color_gif = bob::io::image::read_color_image(gif.string());
if (blitz::any(blitz::abs(color_image - color_gif) > 8)) // TODO: why is GIF not lossless?
throw std::runtime_error("GIF image IO did not succeed, check " + gif.string());
#endif
// NetPBM
boost::filesystem::path pgm(tempdir); pgm /= std::string("gray.pgm");
bob::io::image::write_pgm(gray_image, pgm.string());
bob::io::image::write_gray_image(gray_image, pgm.string());
if (bob::io::image::is_color_p_m(pgm.string()))
throw std::runtime_error("PGM image " + pgm.string() + " is not gray as expected");
blitz::Array<uint8_t, 2> gray_pgm = bob::io::image::read_pgm<uint8_t>(pgm.string());
blitz::Array<uint8_t, 2> gray_pgm = bob::io::image::read_gray_image(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_ppm(color_image, ppm.string());
bob::io::image::write_color_image(color_image, ppm.string());
if (!bob::io::image::is_color_p_m(ppm.string()))
throw std::runtime_error("PPM image " + ppm.string() + " is not color as expected");
blitz::Array<uint8_t, 3> color_ppm = bob::io::image::read_ppm<uint8_t>(ppm.string());
blitz::Array<uint8_t, 3> color_ppm = bob::io::image::read_color_image(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());
......@@ -103,20 +98,20 @@ BOB_TRY
#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());
if (bob::io::image::is_color_jpeg(jpeg_gray.string()))
bob::io::image::write_gray_image(gray_image, jpeg_gray.string());
if (bob::io::image::is_color_image(jpeg_gray.string()))
throw std::runtime_error("JPEG image " + jpeg_gray.string() + " is not gray as expected");
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_gray_image(jpeg_gray.string());
if (blitz::any(blitz::abs(gray_image - gray_jpeg) > 10))
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());
if (!bob::io::image::is_color_jpeg(jpeg_color.string()))
bob::io::image::write_color_image(color_image, jpeg_color.string());
if (!bob::io::image::is_color_image(jpeg_color.string()))
throw std::runtime_error("JPEG image " + jpeg_color.string() + " is not color as expected");
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_color_image(jpeg_color.string());
if (blitz::any(blitz::abs(color_image - color_jpeg) > 10))
throw std::runtime_error("JPEG color image IO did not succeed, check " + jpeg_color.string());
#endif
......@@ -124,20 +119,20 @@ BOB_TRY
#ifdef HAVE_LIBPNG
// PNG
boost::filesystem::path png_gray(tempdir); png_gray /= std::string("gray.png");
bob::io::image::write_png(gray_image, png_gray.string());
if (bob::io::image::is_color_png(png_gray.string()))
bob::io::image::write_gray_image(gray_image, png_gray.string());
if (bob::io::image::is_color_image(png_gray.string()))
throw std::runtime_error("PNG image " + png_gray.string() + " is not gray as expected");
blitz::Array<uint8_t, 2> gray_png = bob::io::image::read_png<uint8_t, 2>(png_gray.string());
blitz::Array<uint8_t, 2> gray_png = bob::io::image::read_gray_image(png_gray.string());
if (blitz::any(blitz::abs(gray_image - gray_png) > 1))
throw std::runtime_error("PNG gray image IO did not succeed, check " + png_gray.string());
boost::filesystem::path png_color(tempdir); png_color /= std::string("color.png");
bob::io::image::write_png(color_image, png_color.string());
bob::io::image::write_color_image(color_image, png_color.string());
if (!bob::io::image::is_color_png(png_color.string()))
throw std::runtime_error("PNG image " + png_color.string() + " is not color as expected");
blitz::Array<uint8_t, 3> color_png = bob::io::image::read_png<uint8_t, 3>(png_color.string());
blitz::Array<uint8_t, 3> color_png = bob::io::image::read_color_image(png_color.string());
if (blitz::any(blitz::abs(color_image - color_png) > 1))
throw std::runtime_error("PNG color image IO did not succeed, check " + png_color.string());
#endif
......@@ -145,25 +140,24 @@ BOB_TRY
#ifdef HAVE_LIBTIFF
// TIFF
boost::filesystem::path tiff_gray(tempdir); tiff_gray /= std::string("gray.tiff");
bob::io::image::write_tiff(gray_image, tiff_gray.string());
if (bob::io::image::is_color_tiff(tiff_gray.string()))
bob::io::image::write_gray_image(gray_image, tiff_gray.string());
if (bob::io::image::is_color_image(tiff_gray.string()))
throw std::runtime_error("TIFF image " + tiff_gray.string() + " is not gray as expected");
blitz::Array<uint8_t, 2> gray_tiff = bob::io::image::read_tiff<uint8_t, 2>(tiff_gray.string());
blitz::Array<uint8_t, 2> gray_tiff = bob::io::image::read_gray_image(tiff_gray.string());
if (blitz::any(blitz::abs(gray_image - gray_tiff) > 1))
throw std::runtime_error("TIFF gray image IO did not succeed, check " + tiff_gray.string());
boost::filesystem::path tiff_color(tempdir); tiff_color /= std::string("color.tiff");
bob::io::image::write_tiff(color_image, tiff_color.string());
if (!bob::io::image::is_color_tiff(tiff_color.string()))
bob::io::image::write_color_image(color_image, tiff_color.string());
if (!bob::io::image::is_color_image(tiff_color.string()))
throw std::runtime_error("TIFF image " + tiff_color.string() + " is not color as expected");
blitz::Array<uint8_t, 3> color_tiff = bob::io::image::read_tiff<uint8_t, 3>(tiff_color.string());
blitz::Array<uint8_t, 3> color_tiff = bob::io::image::read_color_image(tiff_color.string());
if (blitz::any(blitz::abs(color_image - color_tiff) > 1))
throw std::runtime_error("TIFF color image IO did not succeed, check " + tiff_color.string());
#endif
Py_RETURN_NONE;
BOB_CATCH_FUNCTION("_test_io", 0)
}
......
......@@ -9,6 +9,43 @@
The C++ API of ``bob.io.image`` allows users to read and write images in different file formats.
For all image tyres, there exist two functions for reading and writing image, a function to peek the color information (if applicable) and a C++ class that can be used for more detailed information about the image.
Generic functions
-----------------
These functions read and write images based on the filename extension.
Currently, only ``uint8_t`` data type is supported (because this data type is supported by all of our codes).
For other data types, please use the specialized functions as described below.
.. code-block: cpp
#include <bob.io.image/image.h>
.. cpp:function:: bool bob::io::image::is_color_image(const std::string& filename)
Returns ``true`` if the image with the given name is a color image, else ``false``.
It might raise an exception if the extension is not supported.
.. cpp:function:: blitz::Array<uint8_t,2> bob::io::image::read_gray_image(const std::string& filename)
Reads a gray image.
It might raise an exception if the extension is not supported.
.. cpp:function:: blitz::Array<uint8_t,3> bob::io::image::read_color_image(const std::string& filename)
Reads a color image.
It might raise an exception if the extension is not supported.
.. cpp:function:: void bob::io::image::write_gray_image(const blitz::Array<uint8_t,2>& image, const std::string& filename)
Writes the gray ``image``.
If the file exists, it will be overwritten.
.. cpp:function:: void bob::io::image::write_color_image(const blitz::Array<uint8_t,3>& image, const std::string& filename)
Writes the color ``image``.
If the file exists, it will be overwritten.
BMP
---
......
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