Commit 878ded28 authored by Davis King's avatar Davis King

Incorporated Changjiang Yang's patch that makes load_image() use the file

headers to decide how to load images instead of using their file extensions.
This makes load_image() work even when the files have the incorrect file
extensions.
parent 5a4f2823
// Copyright (C) 2011 Davis E. King (davis@dlib.net), Nils Labugt // Copyright (C) 2011 Davis E. King (davis@dlib.net), Nils Labugt, Changjiang Yang (yangcha@leidos.com)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_LOAd_IMAGE_Hh_ #ifndef DLIB_LOAd_IMAGE_Hh_
#define DLIB_LOAd_IMAGE_Hh_ #define DLIB_LOAd_IMAGE_Hh_
...@@ -8,37 +8,72 @@ ...@@ -8,37 +8,72 @@
#include "png_loader.h" #include "png_loader.h"
#include "jpeg_loader.h" #include "jpeg_loader.h"
#include "image_loader.h" #include "image_loader.h"
#include <fstream>
namespace dlib namespace dlib
{ {
namespace image_file_type
{
enum type
{
BMP,
JPG,
PNG,
DNG,
UNKNOWN
};
type read_type(const std::string& fileName)
{
std::ifstream file(fileName, std::ios::in|std::ios::binary);
if (!file)
throw image_load_error("Unable to open file: " + fileName);
char buffer[9];
file.read((char*)buffer, 8);
buffer[8] = 0;
// Determine the true image type using link:
// http://en.wikipedia.org/wiki/List_of_file_signatures
if (strcmp(buffer, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") == 0)
return PNG;
else if(buffer[0]=='\xff' && buffer[1]=='\xd8' && buffer[2]=='\xff')
return JPG;
else if(buffer[0]=='B' && buffer[1]=='M')
return BMP;
else if(buffer[0]=='D' && buffer[1]=='N' && buffer[2] == 'G')
return DNG;
return UNKNOWN;
}
};
template <typename image_type> template <typename image_type>
void load_image ( void load_image (
image_type& image, image_type& image,
const std::string& file_name const std::string& file_name
) )
{ {
const std::string extension = tolower(right_substr(file_name,".")); const image_file_type::type im_type = image_file_type::read_type(file_name);
if (extension == "bmp") switch (im_type)
load_bmp(image, file_name); {
case image_file_type::BMP: load_bmp(image, file_name); return;
case image_file_type::DNG: load_dng(image, file_name); return;
#ifdef DLIB_PNG_SUPPORT #ifdef DLIB_PNG_SUPPORT
else if (extension == "png") case image_file_type::PNG: load_png(image, file_name); return;
load_png(image, file_name);
#endif #endif
#ifdef DLIB_JPEG_SUPPORT #ifdef DLIB_JPEG_SUPPORT
else if (extension == "jpeg" || extension == "jpg") case image_file_type::JPG: load_jpeg(image, file_name); return;
load_jpeg(image, file_name);
#endif #endif
else if (extension == "dng") }
load_dng(image, file_name);
else if (im_type == image_file_type::JPG)
{
if (extension == "jpeg" || extension == "jpg")
throw image_load_error("DLIB_JPEG_SUPPORT not #defined: Unable to load image in file " + file_name); throw image_load_error("DLIB_JPEG_SUPPORT not #defined: Unable to load image in file " + file_name);
else if (extension == "png") else if (im_type == image_file_type::PNG)
throw image_load_error("DLIB_PNG_SUPPORT not #defined: Unable to load image in file " + file_name); throw image_load_error("DLIB_PNG_SUPPORT not #defined: Unable to load image in file " + file_name);
else else
throw image_load_error("Unknown file extension: Unable to load image in file " + file_name); throw image_load_error("Unknown image file format: Unable to load image in file " + file_name);
}
} }
} }
......
...@@ -19,18 +19,10 @@ namespace dlib ...@@ -19,18 +19,10 @@ namespace dlib
- image_type == an image object that implements the interface defined in - image_type == an image object that implements the interface defined in
dlib/image_processing/generic_image.h dlib/image_processing/generic_image.h
ensures ensures
- let EXT == the extension of the file given by file_name converted - This function looks at the file extensions and file headers to try and figure
to lower case (i.e. the part of the file after the '.') out what kind of image format is inside the given file. It then calls one of
- if (EXT == "png") then load_png(), load_jpeg(), load_bmp(), or load_dng() as appropriate and stores
- performs: load_png(image, file_name); the resulting image into #image.
- else if (EXT == "jpg" || EXT == "jpeg") then
- performs: load_jpeg(image, file_name);
- else if (EXT == "bmp") then
- performs: load_bmp(image, file_name);
- else if (EXT == "dng") then
- performs: load_dng(image, file_name);
- else
- throws image_load_error
throws throws
- image_load_error - image_load_error
This exception is thrown if there is some error that prevents This exception is thrown if there is some error that prevents
......
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