Commit d61dcc30 authored by Manuel Günther's avatar Manuel Günther
Browse files

Moved code into bob_io_image.so library to keep headers C++11-free

parent 554e4edd
Pipeline #3622 passed with stages
in 48 minutes and 32 seconds
/**
* @date Thu Sep 8 12:05:08 MDT 2016
* @author Manuel Gunther <siebenkopf@googlemail.com>
*
* @brief Implements an generic image functionalities.
*
* Copyright (c) 2016, Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs.
*/
#include <bob.io.image/image.h>
namespace bob { namespace io { namespace image {
const std::string& get_correct_image_extension(const std::string& image_name){
// global map of known magic numbers
// these numbers are based on: http://stackoverflow.com/questions/26350342/determining-the-extension-type-of-an-image-file-using-binary/26350431#26350431
static std::map<std::string, std::vector<std::vector<std::uint8_t>>> known_magic_numbers;
// initialize list, if not done yet
if (known_magic_numbers.empty()){
// BMP
known_magic_numbers[".bmp"].push_back({ 0x42, 0x4D });
// NetPBM, see https://en.wikipedia.org/wiki/Netpbm_format
known_magic_numbers[".pbm"].push_back({ 0x50, 0x31 });// P1
known_magic_numbers[".pbm"].push_back({ 0x50, 0x34 });// P4
known_magic_numbers[".pgm"].push_back({ 0x50, 0x32 });// P2
known_magic_numbers[".pgm"].push_back({ 0x50, 0x35 });// P5
known_magic_numbers[".ppm"].push_back({ 0x50, 0x33 });// P3
known_magic_numbers[".ppm"].push_back({ 0x50, 0x36 });// P6
// TODO: what about P7?
#ifdef HAVE_GIFLIB
// GIF
known_magic_numbers[".gif"].push_back({ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 });
known_magic_numbers[".gif"].push_back({ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 });
#endif // HAVE_GIFLIB
#ifdef HAVE_LIBPNG
// PNG
known_magic_numbers[".png"].push_back({ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A });
#endif // HAVE_LIB_PNG
#ifdef HAVE_LIBJPEG
// JPEG
known_magic_numbers[".jpg"].push_back({ 0xFF, 0xD8, 0xFF });
known_magic_numbers[".jpg"].push_back({ 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20 });
#endif // HAVE_LIBJPEG
#ifdef HAVE_LIBTIFF
// TIFF
known_magic_numbers[".tiff"].push_back({ 0x0C, 0xED });
known_magic_numbers[".tiff"].push_back({ 0x49, 0x20, 0x49 });
known_magic_numbers[".tiff"].push_back({ 0x49, 0x49, 0x2A, 0x00 });
known_magic_numbers[".tiff"].push_back({ 0x4D, 0x4D, 0x00, 0x2A });
known_magic_numbers[".tiff"].push_back({ 0x4D, 0x4D, 0x00, 0x2B });
#endif // HAVE_LIBTIFF
}
// read first 8 bytes from file
static uint8_t image_bytes[8];
std::ifstream f(image_name);
if (!f) throw std::runtime_error("The given image '" + image_name + "' could not be opened for reading");
f.read(reinterpret_cast<char*>(image_bytes), 8);
// iterate over all extensions
for (auto eit = known_magic_numbers.begin(); eit != known_magic_numbers.end(); ++eit){
// iterate over all magic bytes
for (auto mit = eit->second.begin(); mit != eit->second.end(); ++mit){
// check magic number
if (std::equal(mit->begin(), mit->end(), image_bytes))
return eit->first;
}
}
throw std::runtime_error("The given image '" + image_name + "' does not contain an image of a known type");
}
bool is_color_image(const std::string& filename, std::string extension){
if (extension.empty())
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");
}
} } } // namespaces
......@@ -9,7 +9,7 @@
#define BOB_IO_IMAGE_CONFIG_H
/* Macros that define versions and important names */
#define BOB_IO_IMAGE_API_VERSION 0x0200
#define BOB_IO_IMAGE_API_VERSION 0x0201
#ifdef BOB_IMPORT_VERSION
......
......@@ -34,89 +34,9 @@
namespace bob { namespace io { namespace image {
inline const std::string& get_correct_image_extension(const std::string& image_name){
// global map of known magic numbers
// these numbers are based on: http://stackoverflow.com/questions/26350342/determining-the-extension-type-of-an-image-file-using-binary/26350431#26350431
static std::map<std::string, std::vector<std::vector<std::uint8_t>>> known_magic_numbers;
// initialize list, if not done yet
if (known_magic_numbers.empty()){
// BMP
known_magic_numbers[".bmp"].push_back({ 0x42, 0x4D });
// NetPBM, see https://en.wikipedia.org/wiki/Netpbm_format
known_magic_numbers[".pbm"].push_back({ 0x50, 0x31 });// P1
known_magic_numbers[".pbm"].push_back({ 0x50, 0x34 });// P4
known_magic_numbers[".pgm"].push_back({ 0x50, 0x32 });// P2
known_magic_numbers[".pgm"].push_back({ 0x50, 0x35 });// P5
known_magic_numbers[".ppm"].push_back({ 0x50, 0x33 });// P3
known_magic_numbers[".ppm"].push_back({ 0x50, 0x36 });// P6
// TODO: what about P7?
const std::string& get_correct_image_extension(const std::string& image_name);
#ifdef HAVE_GIFLIB
// GIF
known_magic_numbers[".gif"].push_back({ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 });
known_magic_numbers[".gif"].push_back({ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 });
#endif // HAVE_GIFLIB
#ifdef HAVE_LIBPNG
// PNG
known_magic_numbers[".png"].push_back({ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A });
#endif // HAVE_LIB_PNG
#ifdef HAVE_LIBJPEG
// JPEG
known_magic_numbers[".jpg"].push_back({ 0xFF, 0xD8, 0xFF });
known_magic_numbers[".jpg"].push_back({ 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20 });
#endif // HAVE_LIBJPEG
#ifdef HAVE_LIBTIFF
// TIFF
known_magic_numbers[".tiff"].push_back({ 0x0C, 0xED });
known_magic_numbers[".tiff"].push_back({ 0x49, 0x20, 0x49 });
known_magic_numbers[".tiff"].push_back({ 0x49, 0x49, 0x2A, 0x00 });
known_magic_numbers[".tiff"].push_back({ 0x4D, 0x4D, 0x00, 0x2A });
known_magic_numbers[".tiff"].push_back({ 0x4D, 0x4D, 0x00, 0x2B });
#endif // HAVE_LIBTIFF
}
// read first 8 bytes from file
static uint8_t image_bytes[8];
std::ifstream f(image_name);
if (!f) throw std::runtime_error("The given image '" + image_name + "' could not be opened for reading");
f.read(reinterpret_cast<char*>(image_bytes), 8);
// iterate over all extensions
for (auto eit = known_magic_numbers.begin(); eit != known_magic_numbers.end(); ++eit){
// iterate over all magic bytes
for (auto mit = eit->second.begin(); mit != eit->second.end(); ++mit){
// check magic number
if (std::equal(mit->begin(), mit->end(), image_bytes))
return eit->first;
}
}
throw std::runtime_error("The given image '" + image_name + "' does not contain an image of a known type");
}
inline bool is_color_image(const std::string& filename, std::string extension=""){
if (extension.empty())
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");
}
bool is_color_image(const std::string& filename, std::string extension="");
inline blitz::Array<uint8_t,3> read_color_image(const std::string& filename, std::string extension=""){
if (extension.empty())
......
......@@ -88,7 +88,7 @@ namespace bob { namespace io { namespace image {
static std::string s_codecname;
};
bool is_color_jpeg(const std::string& filename){
inline bool is_color_jpeg(const std::string& filename){
JPEGFile jpeg(filename.c_str(), 'r');
return jpeg.type().nd == 3;
}
......
......@@ -123,7 +123,7 @@ namespace bob { namespace io { namespace image {
}
bool is_color_p_m(const std::string& filename){
inline bool is_color_p_m(const std::string& filename){
NetPBMFile p_m(filename.c_str(), 'r');
return p_m.type().nd == 3;
}
......
......@@ -88,7 +88,7 @@ namespace bob { namespace io { namespace image {
static std::string s_codecname;
};
bool is_color_png(const std::string& filename){
inline bool is_color_png(const std::string& filename){
PNGFile png(filename.c_str(), 'r');
return png.type().nd == 3;
}
......
......@@ -88,7 +88,7 @@ namespace bob { namespace io { namespace image {
static std::string s_codecname;
};
bool is_color_tiff(const std::string& filename){
inline bool is_color_tiff(const std::string& filename){
TIFFFile tiff(filename.c_str(), 'r');
return tiff.type().nd == 3;
}
......
......@@ -397,6 +397,7 @@ setup(
"bob/io/image/cpp/bmp.cpp",
"bob/io/image/cpp/pnmio.cpp",
"bob/io/image/cpp/netpbm.cpp",
"bob/io/image/cpp/image.cpp",
],
packages = packages,
boost_modules = boost_modules,
......
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