|
<html><head><title>dlib C++ Library - png_loader.cpp</title></head><body bgcolor='white'><pre> |
|
<font color='#009900'>// Copyright (C) 2008 Davis E. King ([email protected]), Nils Labugt |
|
</font><font color='#009900'>// License: Boost Software License See LICENSE.txt for the full license. |
|
</font><font color='#0000FF'>#ifndef</font> DLIB_PNG_LOADER_CPp_ |
|
<font color='#0000FF'>#define</font> DLIB_PNG_LOADER_CPp_ |
|
|
|
<font color='#009900'>// only do anything with this file if DLIB_PNG_SUPPORT is defined |
|
</font><font color='#0000FF'>#ifdef</font> DLIB_PNG_SUPPORT |
|
|
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../array2d.h.html'>../array2d.h</a>" |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../pixel.h.html'>../pixel.h</a>" |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../dir_nav.h.html'>../dir_nav.h</a>" |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='png_loader.h.html'>png_loader.h</a>" |
|
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>png.h<font color='#5555FF'>></font> |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../string.h.html'>../string.h</a>" |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../byte_orderer.h.html'>../byte_orderer.h</a>" |
|
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>sstream<font color='#5555FF'>></font> |
|
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>cstring<font color='#5555FF'>></font> |
|
|
|
<font color='#0000FF'>namespace</font> dlib |
|
<b>{</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>struct</font> <b><a name='LibpngData'></a>LibpngData</b> |
|
<b>{</b> |
|
png_bytep<font color='#5555FF'>*</font> row_pointers_; |
|
png_structp png_ptr_; |
|
png_infop info_ptr_; |
|
png_infop end_info_; |
|
<b>}</b>; |
|
|
|
<font color='#0000FF'>struct</font> <b><a name='PngBufferReaderState'></a>PngBufferReaderState</b> |
|
<b>{</b> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> buffer_; |
|
<font color='#0000FF'><u>size_t</u></font> buffer_size_; |
|
<font color='#0000FF'><u>size_t</u></font> current_pos_; |
|
<b>}</b>; |
|
|
|
<font color='#0000FF'>struct</font> <b><a name='FileInfo'></a>FileInfo</b> |
|
<b>{</b> |
|
<b><a name='FileInfo'></a>FileInfo</b><font face='Lucida Console'>(</font> FILE <font color='#5555FF'>*</font>fp, <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> filename <font face='Lucida Console'>)</font> : fp_<font face='Lucida Console'>(</font> fp <font face='Lucida Console'>)</font>, filename_<font face='Lucida Console'>(</font> filename <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<b>}</b> |
|
|
|
<b><a name='FileInfo'></a>FileInfo</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> buffer, <font color='#0000FF'><u>size_t</u></font> buffer_size <font face='Lucida Console'>)</font> : buffer_<font face='Lucida Console'>(</font> buffer <font face='Lucida Console'>)</font>, buffer_size_<font face='Lucida Console'>(</font> buffer_size <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<b>}</b> |
|
|
|
<font color='#009900'>// no copying this object. |
|
</font> <b><a name='FileInfo'></a>FileInfo</b><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> FileInfo<font color='#5555FF'>&</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#0000FF'>delete</font>; |
|
FileInfo<font color='#5555FF'>&</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> FileInfo<font color='#5555FF'>&</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#0000FF'>delete</font>; |
|
|
|
~<b><a name='FileInfo'></a>FileInfo</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> fp_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> nullptr <font face='Lucida Console'>)</font> <font color='#BB00BB'>fclose</font><font face='Lucida Console'>(</font> fp_ <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
FILE<font color='#5555FF'>*</font> fp_<b>{</b>nullptr<b>}</b>; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> filename_<b>{</b>nullptr<b>}</b>; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> buffer_<b>{</b>nullptr<b>}</b>; |
|
<font color='#0000FF'><u>size_t</u></font> buffer_size_<b>{</b><font color='#979000'>0</font><b>}</b>; |
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
png_loader:: |
|
<b><a name='png_loader'></a>png_loader</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> filename <font face='Lucida Console'>)</font> : height_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, width_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>read_image</font><font face='Lucida Console'>(</font> <font color='#BB00BB'>check_file</font><font face='Lucida Console'>(</font> filename <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
png_loader:: |
|
<b><a name='png_loader'></a>png_loader</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> filename <font face='Lucida Console'>)</font> : height_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, width_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>read_image</font><font face='Lucida Console'>(</font> <font color='#BB00BB'>check_file</font><font face='Lucida Console'>(</font> filename.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
png_loader:: |
|
<b><a name='png_loader'></a>png_loader</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> dlib::file<font color='#5555FF'>&</font> f <font face='Lucida Console'>)</font> : height_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, width_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>read_image</font><font face='Lucida Console'>(</font> <font color='#BB00BB'>check_file</font><font face='Lucida Console'>(</font> f.<font color='#BB00BB'>full_name</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
png_loader:: |
|
<b><a name='png_loader'></a>png_loader</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> image_buffer, <font color='#0000FF'><u>size_t</u></font> buffer_size <font face='Lucida Console'>)</font> : height_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, width_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>read_image</font><font face='Lucida Console'>(</font> std::unique_ptr<font color='#5555FF'><</font>FileInfo<font color='#5555FF'>></font><font face='Lucida Console'>(</font> <font color='#0000FF'>new</font> <font color='#BB00BB'>FileInfo</font><font face='Lucida Console'>(</font> image_buffer, buffer_size <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> png_loader::<b><a name='get_row'></a>get_row</b><font face='Lucida Console'>(</font> <font color='#0000FF'><u>unsigned</u></font> i <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>row_pointers_[i]; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
png_loader::~<b><a name='png_loader'></a>png_loader</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> ld_ <font color='#5555FF'>&</font><font color='#5555FF'>&</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>row_pointers_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font> |
|
<font color='#BB00BB'>png_destroy_read_struct</font><font face='Lucida Console'>(</font> <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>end_info_ <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>bool</u></font> png_loader::<b><a name='is_gray'></a>is_gray</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> <font face='Lucida Console'>(</font> color_type_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> PNG_COLOR_TYPE_GRAY <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>bool</u></font> png_loader::<b><a name='is_graya'></a>is_graya</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> <font face='Lucida Console'>(</font> color_type_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> PNG_COLOR_TYPE_GRAY_ALPHA <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>bool</u></font> png_loader::<b><a name='is_rgb'></a>is_rgb</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> <font face='Lucida Console'>(</font> color_type_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> PNG_COLOR_TYPE_RGB <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'><u>bool</u></font> png_loader::<b><a name='is_rgba'></a>is_rgba</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> <font face='Lucida Console'>(</font> color_type_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> PNG_COLOR_TYPE_RGB_ALPHA <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
std::unique_ptr<font color='#5555FF'><</font>FileInfo<font color='#5555FF'>></font> png_loader::<b><a name='check_file'></a>check_file</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> filename <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> filename <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: invalid filename, it is NULL</font>"<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
FILE <font color='#5555FF'>*</font>fp <font color='#5555FF'>=</font> <font color='#BB00BB'>fopen</font><font face='Lucida Console'>(</font> filename, "<font color='#CC0000'>rb</font>" <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> <font color='#5555FF'>!</font>fp <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: unable to open file </font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> filename<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>return</font> std::unique_ptr<font color='#5555FF'><</font>FileInfo<font color='#5555FF'>></font><font face='Lucida Console'>(</font> <font color='#0000FF'>new</font> <font color='#BB00BB'>FileInfo</font><font face='Lucida Console'>(</font> fp, filename <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#009900'>// Don't do anything when libpng calls us to tell us about an error. Just return to |
|
</font> <font color='#009900'>// our own code and throw an exception (at the long jump target). |
|
</font> <font color='#0000FF'><u>void</u></font> <b><a name='png_loader_user_error_fn_silent'></a>png_loader_user_error_fn_silent</b><font face='Lucida Console'>(</font>png_structp png_struct, png_const_charp <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>longjmp</font><font face='Lucida Console'>(</font><font color='#BB00BB'>png_jmpbuf</font><font face='Lucida Console'>(</font>png_struct<font face='Lucida Console'>)</font>,<font color='#979000'>1</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<font color='#0000FF'><u>void</u></font> <b><a name='png_loader_user_warning_fn_silent'></a>png_loader_user_warning_fn_silent</b><font face='Lucida Console'>(</font>png_structp , png_const_charp <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='png_buffer_reader'></a>png_buffer_reader</b><font face='Lucida Console'>(</font>png_structp png_ptr, png_bytep data, <font color='#0000FF'><u>size_t</u></font> length<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
PngBufferReaderState<font color='#5555FF'>*</font> state <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'><</font>PngBufferReaderState<font color='#5555FF'>*</font><font color='#5555FF'>></font><font face='Lucida Console'>(</font> <font color='#BB00BB'>png_get_io_ptr</font><font face='Lucida Console'>(</font> png_ptr <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> length <font color='#5555FF'>></font> <font face='Lucida Console'>(</font> state<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_size_ <font color='#5555FF'>-</font> state<font color='#5555FF'>-</font><font color='#5555FF'>></font>current_pos_ <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>png_error</font><font face='Lucida Console'>(</font>png_ptr, "<font color='#CC0000'>png_loader: read error in png_buffer_reader</font>"<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<font color='#BB00BB'>memcpy</font><font face='Lucida Console'>(</font> data, state<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_ <font color='#5555FF'>+</font> state<font color='#5555FF'>-</font><font color='#5555FF'>></font>current_pos_, length <font face='Lucida Console'>)</font>; |
|
state<font color='#5555FF'>-</font><font color='#5555FF'>></font>current_pos_ <font color='#5555FF'>+</font><font color='#5555FF'>=</font> length; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>void</u></font> png_loader::<b><a name='read_image'></a>read_image</b><font face='Lucida Console'>(</font> std::unique_ptr<font color='#5555FF'><</font>FileInfo<font color='#5555FF'>></font> file_info <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font>file_info<font face='Lucida Console'>)</font>; |
|
|
|
ld_.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> LibpngData<font face='Lucida Console'>)</font>; |
|
|
|
constexpr png_size_t png_header_size <font color='#5555FF'>=</font> <font color='#979000'>8</font>; |
|
std::string load_error_info; |
|
|
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>fp_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
png_byte sig[png_header_size]; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>fread</font><font face='Lucida Console'>(</font> sig, <font color='#979000'>1</font>, png_header_size, file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>fp_ <font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> png_header_size<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: error reading file </font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>filename_<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
load_error_info <font color='#5555FF'>=</font> std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'> in file </font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>filename_; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> <font color='#BB00BB'>png_sig_cmp</font><font face='Lucida Console'>(</font> sig, <font color='#979000'>0</font>, png_header_size <font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: format error</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> load_error_info<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<b>}</b> |
|
<font color='#0000FF'>else</font> |
|
<b>{</b> |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: invalid image buffer, it is NULL</font>"<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_size_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: invalid image buffer size, it is 0</font>"<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_size_ <font color='#5555FF'><</font> png_header_size <font color='#5555FF'>|</font><font color='#5555FF'>|</font> |
|
<font color='#BB00BB'>png_sig_cmp</font><font face='Lucida Console'>(</font> <font face='Lucida Console'>(</font>png_bytep<font face='Lucida Console'>)</font>file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_, <font color='#979000'>0</font>, png_header_size <font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: format error in image buffer</font>"<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
buffer_reader_state_.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> PngBufferReaderState<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font color='#5555FF'>=</font> <font color='#BB00BB'>png_create_read_struct</font><font face='Lucida Console'>(</font> PNG_LIBPNG_VER_STRING, NULL, <font color='#5555FF'>&</font>png_loader_user_error_fn_silent, <font color='#5555FF'>&</font>png_loader_user_warning_fn_silent <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
std::ostringstream sout; |
|
sout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>Error, unable to allocate png structure</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> std::endl; |
|
<font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> runtime_version <font color='#5555FF'>=</font> <font color='#BB00BB'>png_get_header_ver</font><font face='Lucida Console'>(</font>NULL<font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>runtime_version <font color='#5555FF'>&</font><font color='#5555FF'>&</font> std::<font color='#BB00BB'>strcmp</font><font face='Lucida Console'>(</font>PNG_LIBPNG_VER_STRING, runtime_version<font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
sout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>This is happening because you compiled against one version of libpng, but then linked to another.</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> std::endl; |
|
sout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>Compiled against libpng version: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> PNG_LIBPNG_VER_STRING <font color='#5555FF'><</font><font color='#5555FF'><</font> std::endl; |
|
sout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>Linking to this version of libpng: </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> runtime_version <font color='#5555FF'><</font><font color='#5555FF'><</font> std::endl; |
|
<b>}</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>sout.<font color='#BB00BB'>str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font color='#5555FF'>=</font> <font color='#BB00BB'>png_create_info_struct</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>png_destroy_read_struct</font><font face='Lucida Console'>(</font> <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font face='Lucida Console'>)</font>, <font face='Lucida Console'>(</font> png_infopp <font face='Lucida Console'>)</font>NULL, <font face='Lucida Console'>(</font> png_infopp <font face='Lucida Console'>)</font>NULL <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: unable to allocate png info structure</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> load_error_info<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>end_info_ <font color='#5555FF'>=</font> <font color='#BB00BB'>png_create_info_struct</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>end_info_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>png_destroy_read_struct</font><font face='Lucida Console'>(</font> <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>, <font face='Lucida Console'>(</font> png_infopp <font face='Lucida Console'>)</font>NULL <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: unable to allocate png info structure</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> load_error_info<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>setjmp</font><font face='Lucida Console'>(</font><font color='#BB00BB'>png_jmpbuf</font><font face='Lucida Console'>(</font>ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#009900'>// If we get here, we had a problem writing the file |
|
</font> <font color='#BB00BB'>png_destroy_read_struct</font><font face='Lucida Console'>(</font> <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>end_info_ <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: parse error</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> load_error_info<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#BB00BB'>png_set_palette_to_rgb</font><font face='Lucida Console'>(</font>ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_<font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>fp_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>png_init_io</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_, file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>fp_ <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<font color='#0000FF'>else</font> |
|
<b>{</b> |
|
buffer_reader_state_<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_ <font color='#5555FF'>=</font> file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_; |
|
buffer_reader_state_<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_size_ <font color='#5555FF'>=</font> file_info<font color='#5555FF'>-</font><font color='#5555FF'>></font>buffer_size_; |
|
<font color='#009900'>// skipping header |
|
</font> buffer_reader_state_<font color='#5555FF'>-</font><font color='#5555FF'>></font>current_pos_ <font color='#5555FF'>=</font> png_header_size; |
|
<font color='#BB00BB'>png_set_read_fn</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_, buffer_reader_state_.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, png_buffer_reader <font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#BB00BB'>png_set_sig_bytes</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_, png_header_size <font face='Lucida Console'>)</font>; |
|
<font color='#009900'>// flags force one byte per channel output |
|
</font> byte_orderer bo; |
|
<font color='#0000FF'><u>int</u></font> png_transforms <font color='#5555FF'>=</font> PNG_TRANSFORM_PACKING; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>bo.<font color='#BB00BB'>host_is_little_endian</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> |
|
png_transforms <font color='#5555FF'>|</font><font color='#5555FF'>=</font> PNG_TRANSFORM_SWAP_ENDIAN; |
|
<font color='#BB00BB'>png_read_png</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_, ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_, png_transforms, NULL <font face='Lucida Console'>)</font>; |
|
height_ <font color='#5555FF'>=</font> <font color='#BB00BB'>png_get_image_height</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_, ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>; |
|
width_ <font color='#5555FF'>=</font> <font color='#BB00BB'>png_get_image_width</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_, ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>; |
|
bit_depth_ <font color='#5555FF'>=</font> <font color='#BB00BB'>png_get_bit_depth</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_, ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>; |
|
color_type_ <font color='#5555FF'>=</font> <font color='#BB00BB'>png_get_color_type</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_, ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font> info_ptr_ <font face='Lucida Console'>)</font>; |
|
|
|
|
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>color_type_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> PNG_COLOR_TYPE_GRAY <font color='#5555FF'>&</font><font color='#5555FF'>&</font> |
|
color_type_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> PNG_COLOR_TYPE_RGB <font color='#5555FF'>&</font><font color='#5555FF'>&</font> |
|
color_type_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> PNG_COLOR_TYPE_RGB_ALPHA <font color='#5555FF'>&</font><font color='#5555FF'>&</font> |
|
color_type_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> PNG_COLOR_TYPE_GRAY_ALPHA<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>png_destroy_read_struct</font><font face='Lucida Console'>(</font> <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>end_info_ <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: unsupported color type</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> load_error_info<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>bit_depth_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>8</font> <font color='#5555FF'>&</font><font color='#5555FF'>&</font> bit_depth_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>16</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>png_destroy_read_struct</font><font face='Lucida Console'>(</font> <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>end_info_ <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: unsupported bit depth of </font>" <font color='#5555FF'>+</font> <font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>bit_depth_<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> load_error_info<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>row_pointers_ <font color='#5555FF'>=</font> <font color='#BB00BB'>png_get_rows</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_, ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>; |
|
|
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>row_pointers_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>png_destroy_read_struct</font><font face='Lucida Console'>(</font> <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>png_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>info_ptr_ <font face='Lucida Console'>)</font>, <font color='#5555FF'>&</font><font face='Lucida Console'>(</font> ld_<font color='#5555FF'>-</font><font color='#5555FF'>></font>end_info_ <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>png_loader: parse error</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> load_error_info<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_PNG_SUPPORT |
|
</font> |
|
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_PNG_LOADER_CPp_ |
|
</font> |
|
|
|
</pre></body></html> |