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.
#ifndef DLIB_LOAd_IMAGE_Hh_
#define DLIB_LOAd_IMAGE_Hh_
......@@ -8,37 +8,72 @@
#include "png_loader.h"
#include "jpeg_loader.h"
#include "image_loader.h"
#include <fstream>
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>
void load_image (
image_type& image,
const std::string& file_name
)
{
const std::string extension = tolower(right_substr(file_name,"."));
if (extension == "bmp")
load_bmp(image, file_name);
const image_file_type::type im_type = image_file_type::read_type(file_name);
switch (im_type)
{
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
else if (extension == "png")
load_png(image, file_name);
case image_file_type::PNG: load_png(image, file_name); return;
#endif
#ifdef DLIB_JPEG_SUPPORT
else if (extension == "jpeg" || extension == "jpg")
load_jpeg(image, file_name);
case image_file_type::JPG: load_jpeg(image, file_name); return;
#endif
else if (extension == "dng")
load_dng(image, file_name);
else
{
if (extension == "jpeg" || extension == "jpg")
}
if (im_type == image_file_type::JPG)
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);
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
- image_type == an image object that implements the interface defined in
dlib/image_processing/generic_image.h
ensures
- let EXT == the extension of the file given by file_name converted
to lower case (i.e. the part of the file after the '.')
- if (EXT == "png") then
- performs: load_png(image, file_name);
- 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
- This function looks at the file extensions and file headers to try and figure
out what kind of image format is inside the given file. It then calls one of
load_png(), load_jpeg(), load_bmp(), or load_dng() as appropriate and stores
the resulting image into #image.
throws
- image_load_error
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