Commit 9c48b871 authored by Davis King's avatar Davis King

Made load_image() support GIF files.

parent d1b307a8
...@@ -14,9 +14,13 @@ include(use_cpp_11.cmake) ...@@ -14,9 +14,13 @@ include(use_cpp_11.cmake)
set(CPACK_PACKAGE_VERSION_MAJOR "18") set(CPACK_PACKAGE_VERSION_MAJOR "18")
set(CPACK_PACKAGE_VERSION_MINOR "18") set(CPACK_PACKAGE_VERSION_MINOR "18")
set(CPACK_PACKAGE_VERSION_PATCH "99") set(CPACK_PACKAGE_VERSION_PATCH "100")
set(VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}) set(VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH})
set(DLIB_VERSION ${VERSION} PARENT_SCOPE) # Set DLIB_VERSION in the including CMake file so they can use it to do whatever they want.
get_directory_property(has_parent PARENT_DIRECTORY)
if(has_parent)
set(DLIB_VERSION ${VERSION} PARENT_SCOPE)
endif()
set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required
# Suppress cmake warnings about changes in new versions. # Suppress cmake warnings about changes in new versions.
...@@ -68,6 +72,8 @@ if (NOT TARGET dlib) ...@@ -68,6 +72,8 @@ if (NOT TARGET dlib)
"Disable this if you don't want to use NVIDIA CUDA" ) "Disable this if you don't want to use NVIDIA CUDA" )
set (DLIB_PNG_SUPPORT_STR set (DLIB_PNG_SUPPORT_STR
"Disable this if you don't want to link against libpng" ) "Disable this if you don't want to link against libpng" )
set (DLIB_GIF_SUPPORT_STR
"Disable this if you don't want to link against libgif" )
set (DLIB_JPEG_SUPPORT_STR set (DLIB_JPEG_SUPPORT_STR
"Disable this if you don't want to link against libjpeg" ) "Disable this if you don't want to link against libjpeg" )
set (DLIB_LINK_WITH_SQLITE3_STR set (DLIB_LINK_WITH_SQLITE3_STR
...@@ -103,6 +109,7 @@ if (NOT TARGET dlib) ...@@ -103,6 +109,7 @@ if (NOT TARGET dlib)
option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} OFF) option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} OFF)
option(DLIB_USE_CUDA ${DLIB_USE_CUDA_STR} OFF) option(DLIB_USE_CUDA ${DLIB_USE_CUDA_STR} OFF)
option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} OFF) option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} OFF)
option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} OFF)
#option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} OFF) #option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} OFF)
else() else()
option(DLIB_JPEG_SUPPORT ${DLIB_JPEG_SUPPORT_STR} ON) option(DLIB_JPEG_SUPPORT ${DLIB_JPEG_SUPPORT_STR} ON)
...@@ -111,6 +118,7 @@ if (NOT TARGET dlib) ...@@ -111,6 +118,7 @@ if (NOT TARGET dlib)
option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} ON) option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} ON)
option(DLIB_USE_CUDA ${DLIB_USE_CUDA_STR} ON) option(DLIB_USE_CUDA ${DLIB_USE_CUDA_STR} ON)
option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} ON) option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} ON)
option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} ON)
#option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} ON) #option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} ON)
endif() endif()
toggle_preprocessor_switch(DLIB_JPEG_SUPPORT) toggle_preprocessor_switch(DLIB_JPEG_SUPPORT)
...@@ -118,6 +126,7 @@ if (NOT TARGET dlib) ...@@ -118,6 +126,7 @@ if (NOT TARGET dlib)
toggle_preprocessor_switch(DLIB_USE_LAPACK) toggle_preprocessor_switch(DLIB_USE_LAPACK)
toggle_preprocessor_switch(DLIB_USE_CUDA) toggle_preprocessor_switch(DLIB_USE_CUDA)
toggle_preprocessor_switch(DLIB_PNG_SUPPORT) toggle_preprocessor_switch(DLIB_PNG_SUPPORT)
toggle_preprocessor_switch(DLIB_GIF_SUPPORT)
#toggle_preprocessor_switch(DLIB_USE_FFTW) #toggle_preprocessor_switch(DLIB_USE_FFTW)
...@@ -275,6 +284,17 @@ if (NOT TARGET dlib) ...@@ -275,6 +284,17 @@ if (NOT TARGET dlib)
INCLUDE (CheckFunctionExists) INCLUDE (CheckFunctionExists)
if (DLIB_GIF_SUPPORT)
find_package(GIF QUIET)
if (GIF_FOUND)
include_directories(${GIF_INCLUDE_DIR})
set (dlib_needed_libraries ${dlib_needed_libraries} ${GIF_LIBRARY})
else()
set(DLIB_GIF_SUPPORT OFF CACHE STRING ${DLIB_GIF_SUPPORT_STR} FORCE )
toggle_preprocessor_switch(DLIB_GIF_SUPPORT)
endif()
endif()
if (DLIB_PNG_SUPPORT) if (DLIB_PNG_SUPPORT)
# try to find libpng # try to find libpng
find_package(PNG QUIET) find_package(PNG QUIET)
......
...@@ -14,10 +14,11 @@ ...@@ -14,10 +14,11 @@
//#define DLIB_NO_GUI_SUPPORT //#define DLIB_NO_GUI_SUPPORT
//#define DLIB_ENABLE_STACK_TRACE //#define DLIB_ENABLE_STACK_TRACE
// You should also consider telling dlib to link against libjpeg, libpng, fftw, CUDA, and a // You should also consider telling dlib to link against libjpeg, libpng, libgif, fftw, CUDA,
// BLAS and LAPACK library. To do this you need to uncomment the following #defines. // and a BLAS and LAPACK library. To do this you need to uncomment the following #defines.
// #define DLIB_JPEG_SUPPORT // #define DLIB_JPEG_SUPPORT
// #define DLIB_PNG_SUPPORT // #define DLIB_PNG_SUPPORT
// #define DLIB_GIF_SUPPORT
// #define DLIB_USE_FFTW // #define DLIB_USE_FFTW
// #define DLIB_USE_BLAS // #define DLIB_USE_BLAS
// #define DLIB_USE_LAPACK // #define DLIB_USE_LAPACK
......
...@@ -14,10 +14,11 @@ ...@@ -14,10 +14,11 @@
#cmakedefine DLIB_NO_GUI_SUPPORT #cmakedefine DLIB_NO_GUI_SUPPORT
#cmakedefine DLIB_ENABLE_STACK_TRACE #cmakedefine DLIB_ENABLE_STACK_TRACE
// You should also consider telling dlib to link against libjpeg, libpng, fftw, CUDA, and a // You should also consider telling dlib to link against libjpeg, libpng, libgif, fftw, CUDA,
// BLAS and LAPACK library. To do this you need to uncomment the following #defines. // and a BLAS and LAPACK library. To do this you need to uncomment the following #defines.
#cmakedefine DLIB_JPEG_SUPPORT #cmakedefine DLIB_JPEG_SUPPORT
#cmakedefine DLIB_PNG_SUPPORT #cmakedefine DLIB_PNG_SUPPORT
#cmakedefine DLIB_GIF_SUPPORT
#cmakedefine DLIB_USE_FFTW #cmakedefine DLIB_USE_FFTW
#cmakedefine DLIB_USE_BLAS #cmakedefine DLIB_USE_BLAS
#cmakedefine DLIB_USE_LAPACK #cmakedefine DLIB_USE_LAPACK
......
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
#include "image_loader.h" #include "image_loader.h"
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#ifdef DLIB_GIF_SUPPORT
#include <gif_lib.h>
#endif
namespace dlib namespace dlib
{ {
...@@ -21,6 +24,7 @@ namespace dlib ...@@ -21,6 +24,7 @@ namespace dlib
JPG, JPG,
PNG, PNG,
DNG, DNG,
GIF,
UNKNOWN UNKNOWN
}; };
...@@ -45,11 +49,15 @@ namespace dlib ...@@ -45,11 +49,15 @@ namespace dlib
return BMP; return BMP;
else if(buffer[0]=='D' && buffer[1]=='N' && buffer[2] == 'G') else if(buffer[0]=='D' && buffer[1]=='N' && buffer[2] == 'G')
return DNG; return DNG;
else if(buffer[0]=='G' && buffer[1]=='I' && buffer[2] == 'F')
return GIF;
return UNKNOWN; return UNKNOWN;
} }
}; };
// ----------------------------------------------------------------------------------------
template <typename image_type> template <typename image_type>
void load_image ( void load_image (
image_type& image, image_type& image,
...@@ -66,6 +74,54 @@ namespace dlib ...@@ -66,6 +74,54 @@ namespace dlib
#endif #endif
#ifdef DLIB_JPEG_SUPPORT #ifdef DLIB_JPEG_SUPPORT
case image_file_type::JPG: load_jpeg(image, file_name); return; case image_file_type::JPG: load_jpeg(image, file_name); return;
#endif
#ifdef DLIB_GIF_SUPPORT
case image_file_type::GIF:
{
image_view<image_type> img(image);
GifFileType* gif = DGifOpenFileName(file_name.c_str());
try
{
if (gif == 0)
throw image_load_error("Couldn't open file " + file_name);
if (DGifSlurp(gif) != GIF_OK)
throw image_load_error("Error reading from " + file_name);
if (gif->ImageCount != 1) throw image_load_error("Dlib only supports reading GIF files containing one image.");
if (gif->SColorMap == 0) throw image_load_error("Unsupported GIF format 1.");
if (gif->SavedImages == 0) throw image_load_error("Unsupported GIF format 2.");
if (gif->SavedImages->ImageDesc.Width != gif->SWidth) throw image_load_error("Unsupported GIF format 3.");
if (gif->SavedImages->ImageDesc.Height != gif->SHeight) throw image_load_error("Unsupported GIF format 4.");
if (gif->SColorMap->ColorCount != 256) throw image_load_error("Unsupported GIF format 5.");
if (gif->SColorMap->BitsPerPixel != 8) throw image_load_error("Unsupported GIF format 6.");
if (gif->SavedImages->RasterBits == 0) throw image_load_error("Unsupported GIF format 7.");
if (gif->SColorMap->Colors == 0) throw image_load_error("Unsupported GIF format 8.");
img.set_size(gif->SHeight, gif->SWidth);
unsigned char* raster = gif->SavedImages->RasterBits;
GifColorType* colormap = gif->SColorMap->Colors;
for (long r = 0; r < img.nr(); ++r)
{
for (long c = 0; c < img.nc(); ++c)
{
rgb_pixel p;
p.red = colormap[*raster].Red;
p.green = colormap[*raster].Green;
p.blue = colormap[*raster].Blue;
assign_pixel(img[r][c], p);
++raster;
}
}
DGifCloseFile(gif);
}
catch(...)
{
if (gif)
DGifCloseFile(gif);
throw;
}
return;
}
#endif #endif
default: ; default: ;
} }
...@@ -78,11 +134,11 @@ namespace dlib ...@@ -78,11 +134,11 @@ namespace dlib
"Do this by following the instructions at http://dlib.net/compile.html.\n\n"; "Do this by following the instructions at http://dlib.net/compile.html.\n\n";
#ifdef _MSC_VER #ifdef _MSC_VER
sout << "Note that you must cause DLIB_JPEG_SUPPORT to be defined for your entire project.\n"; sout << "Note that you must cause DLIB_JPEG_SUPPORT to be defined for your entire project.\n";
sout << "So don't #define it in one file, add it to the C/C++->Preprocessor->Preprocessor Definitions\n"; sout << "So don't #define it in one file. Instead, add it to the C/C++->Preprocessor->Preprocessor Definitions\n";
sout << "field in Visual Studio's Property Pages window so it takes effect for your entire application."; sout << "field in Visual Studio's Property Pages window so it takes effect for your entire application.";
#else #else
sout << "Note that you must cause DLIB_JPEG_SUPPORT to be defined for your entire project.\n"; sout << "Note that you must cause DLIB_JPEG_SUPPORT to be defined for your entire project.\n";
sout << "So don't #define it in one file, use a compiler switch like -DDLIB_JPEG_SUPPORT\n"; sout << "So don't #define it in one file. Instead, use a compiler switch like -DDLIB_JPEG_SUPPORT\n";
sout << "so it takes effect for your entire application."; sout << "so it takes effect for your entire application.";
#endif #endif
throw image_load_error(sout.str()); throw image_load_error(sout.str());
...@@ -95,11 +151,27 @@ namespace dlib ...@@ -95,11 +151,27 @@ namespace dlib
"Do this by following the instructions at http://dlib.net/compile.html.\n\n"; "Do this by following the instructions at http://dlib.net/compile.html.\n\n";
#ifdef _MSC_VER #ifdef _MSC_VER
sout << "Note that you must cause DLIB_PNG_SUPPORT to be defined for your entire project.\n"; sout << "Note that you must cause DLIB_PNG_SUPPORT to be defined for your entire project.\n";
sout << "So don't #define it in one file, add it to the C/C++->Preprocessor->Preprocessor Definitions\n"; sout << "So don't #define it in one file. Instead, add it to the C/C++->Preprocessor->Preprocessor Definitions\n";
sout << "field in Visual Studio's Property Pages window so it takes effect for your entire application.\n"; sout << "field in Visual Studio's Property Pages window so it takes effect for your entire application.\n";
#else #else
sout << "Note that you must cause DLIB_PNG_SUPPORT to be defined for your entire project.\n"; sout << "Note that you must cause DLIB_PNG_SUPPORT to be defined for your entire project.\n";
sout << "So don't #define it in one file, use a compiler switch like -DDLIB_PNG_SUPPORT\n"; sout << "So don't #define it in one file. Instead, use a compiler switch like -DDLIB_PNG_SUPPORT\n";
sout << "so it takes effect for your entire application.";
#endif
throw image_load_error(sout.str());
}
else if (im_type == image_file_type::GIF)
{
std::ostringstream sout;
sout << "Unable to load image in file " + file_name + ".\n" +
"You must #define DLIB_GIF_SUPPORT and link to libgif to read GIF files.\n\n";
#ifdef _MSC_VER
sout << "Note that you must cause DLIB_GIF_SUPPORT to be defined for your entire project.\n";
sout << "So don't #define it in one file. Instead, add it to the C/C++->Preprocessor->Preprocessor Definitions\n";
sout << "field in Visual Studio's Property Pages window so it takes effect for your entire application.\n";
#else
sout << "Note that you must cause DLIB_GIF_SUPPORT to be defined for your entire project.\n";
sout << "So don't #define it in one file. Instead, use a compiler switch like -DDLIB_GIF_SUPPORT\n";
sout << "so it takes effect for your entire application."; sout << "so it takes effect for your entire application.";
#endif #endif
throw image_load_error(sout.str()); throw image_load_error(sout.str());
...@@ -110,6 +182,8 @@ namespace dlib ...@@ -110,6 +182,8 @@ namespace dlib
} }
} }
// ----------------------------------------------------------------------------------------
} }
#endif // DLIB_LOAd_IMAGE_Hh_ #endif // DLIB_LOAd_IMAGE_Hh_
......
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
#undef DLIB_LOAd_IMAGE_ABSTRACT_ #undef DLIB_LOAd_IMAGE_ABSTRACT_
#ifdef DLIB_LOAd_IMAGE_ABSTRACT_ #ifdef DLIB_LOAd_IMAGE_ABSTRACT_
#include "load_image_abstract.h"
#include "../string.h"
#include "../image_processing/generic_image.h" #include "../image_processing/generic_image.h"
namespace dlib namespace dlib
...@@ -19,10 +17,13 @@ namespace dlib ...@@ -19,10 +17,13 @@ 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
- This function looks at the file extensions and file headers to try and figure - This function loads an image from disk, in the indicated file file_name, and
out what kind of image format is inside the given file. It then calls one of writes it to the indicated image object.
load_png(), load_jpeg(), load_bmp(), or load_dng() as appropriate and stores - It is capable of reading the PNG, JPEG, BMP, GIF, and DNG image formats. It
the resulting image into #image. is always capable of reading BMP and DNG images. However, for PNG, JPEG, and
GIF you must #define DLIB_PNG_SUPPORT, DLIB_JPEG_SUPPORT, and
DLIB_GIF_SUPPORT respectively and link your program to libpng, libjpeg, and
libgif respectively.
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