Skip to content
Snippets Groups Projects
Commit 12fef283 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

Merge branch '25-gcc421' into 'master'

Fixes gcc-4.2.1+libstdc++ compatibility (closes #25)

Closes #25

See merge request !23
parents 08a9570b c757f1b7
Branches
Tags
1 merge request!23Fixes gcc-4.2.1+libstdc++ compatibility (closes #25)
Pipeline #
...@@ -7,57 +7,59 @@ ...@@ -7,57 +7,59 @@
* Copyright (c) 2016, Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs. * Copyright (c) 2016, Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs.
*/ */
#include <stdint.h>
#include <boost/assign/list_of.hpp>
#include <bob.io.image/image.h> #include <bob.io.image/image.h>
namespace bob { namespace io { namespace image { namespace bob { namespace io { namespace image {
static std::map<std::string, std::vector<std::vector<std::uint8_t>>> _initialize_magic_numbers(){ static std::map<std::string, std::vector<std::vector<uint8_t>>> _initialize_magic_numbers(){
// these numbers are based on: http://stackoverflow.com/questions/26350342/determining-the-extension-type-of-an-image-file-using-binary/26350431#26350431 // these numbers are based on: http://stackoverflow.com/questions/26350342/determining-the-extension-type-of-an-image-file-using-binary/26350431#26350431
std::map<std::string, std::vector<std::vector<std::uint8_t>>> magic_numbers; std::map<std::string, std::vector<std::vector<uint8_t>>> magic_numbers;
// BMP // BMP
magic_numbers[".bmp"].push_back({ 0x42, 0x4D }); magic_numbers[".bmp"].push_back(boost::assign::list_of(0x42)(0x4D));
// NetPBM, see https://en.wikipedia.org/wiki/Netpbm_format // NetPBM, see https://en.wikipedia.org/wiki/Netpbm_format
magic_numbers[".pbm"].push_back({ 0x50, 0x31 });// P1 magic_numbers[".pbm"].push_back(boost::assign::list_of(0x50)(0x31));// P1
magic_numbers[".pbm"].push_back({ 0x50, 0x34 });// P4 magic_numbers[".pbm"].push_back(boost::assign::list_of(0x50)(0x34));// P4
magic_numbers[".pgm"].push_back({ 0x50, 0x32 });// P2 magic_numbers[".pgm"].push_back(boost::assign::list_of(0x50)(0x32));// P2
magic_numbers[".pgm"].push_back({ 0x50, 0x35 });// P5 magic_numbers[".pgm"].push_back(boost::assign::list_of(0x50)(0x35));// P5
magic_numbers[".ppm"].push_back({ 0x50, 0x33 });// P3 magic_numbers[".ppm"].push_back(boost::assign::list_of(0x50)(0x33));// P3
magic_numbers[".ppm"].push_back({ 0x50, 0x36 });// P6 magic_numbers[".ppm"].push_back(boost::assign::list_of(0x50)(0x36));// P6
// TODO: what about P7? // TODO: what about P7?
#ifdef HAVE_GIFLIB #ifdef HAVE_GIFLIB
// GIF // GIF
magic_numbers[".gif"].push_back({ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }); magic_numbers[".gif"].push_back(boost::assign::list_of(0x47)(0x49)(0x46)(0x38)(0x37)(0x61));
magic_numbers[".gif"].push_back({ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }); magic_numbers[".gif"].push_back(boost::assign::list_of(0x47)(0x49)(0x46)(0x38)(0x39)(0x61));
#endif // HAVE_GIFLIB #endif // HAVE_GIFLIB
#ifdef HAVE_LIBPNG #ifdef HAVE_LIBPNG
// PNG // PNG
magic_numbers[".png"].push_back({ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }); magic_numbers[".png"].push_back(boost::assign::list_of(0x89)(0x50)(0x4E)(0x47)(0x0D)(0x0A)(0x1A)(0x0A));
#endif // HAVE_LIB_PNG #endif // HAVE_LIB_PNG
#ifdef HAVE_LIBJPEG #ifdef HAVE_LIBJPEG
// JPEG // JPEG
magic_numbers[".jpg"].push_back({ 0xFF, 0xD8, 0xFF }); magic_numbers[".jpg"].push_back(boost::assign::list_of(0xFF)(0xD8)(0xFF));
magic_numbers[".jpg"].push_back({ 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20 }); magic_numbers[".jpg"].push_back(boost::assign::list_of(0x00)(0x00)(0x00)(0x0C)(0x6A)(0x50)(0x20)(0x20));
#endif // HAVE_LIBJPEG #endif // HAVE_LIBJPEG
#ifdef HAVE_LIBTIFF #ifdef HAVE_LIBTIFF
// TIFF // TIFF
magic_numbers[".tiff"].push_back({ 0x0C, 0xED }); magic_numbers[".tiff"].push_back(boost::assign::list_of(0x0C)(0xED));
magic_numbers[".tiff"].push_back({ 0x49, 0x20, 0x49 }); magic_numbers[".tiff"].push_back(boost::assign::list_of(0x49)(0x20)(0x49));
magic_numbers[".tiff"].push_back({ 0x49, 0x49, 0x2A, 0x00 }); magic_numbers[".tiff"].push_back(boost::assign::list_of(0x49)(0x49)(0x2A)(0x00));
magic_numbers[".tiff"].push_back({ 0x4D, 0x4D, 0x00, 0x2A }); magic_numbers[".tiff"].push_back(boost::assign::list_of(0x4D)(0x4D)(0x00)(0x2A));
magic_numbers[".tiff"].push_back({ 0x4D, 0x4D, 0x00, 0x2B }); magic_numbers[".tiff"].push_back(boost::assign::list_of(0x4D)(0x4D)(0x00)(0x2B));
#endif // HAVE_LIBTIFF #endif // HAVE_LIBTIFF
return magic_numbers; return magic_numbers;
} }
// global static thread-safe map of known magic numbers // global static thread-safe map of known magic numbers
static std::map<std::string, std::vector<std::vector<std::uint8_t>>> known_magic_numbers = _initialize_magic_numbers(); static std::map<std::string, std::vector<std::vector<uint8_t>>> known_magic_numbers = _initialize_magic_numbers();
const std::string& get_correct_image_extension(const std::string& image_name){ const std::string& get_correct_image_extension(const std::string& image_name){
// read first 8 bytes from file // read first 8 bytes from file
uint8_t image_bytes[8]; uint8_t image_bytes[8];
std::ifstream f(image_name); std::ifstream f(image_name.c_str());
if (!f) throw std::runtime_error("The given image '" + image_name + "' could not be opened for reading"); 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); f.read(reinterpret_cast<char*>(image_bytes), 8);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment