Commit 6f4c65a6 authored by Davis King's avatar Davis King

updated png_loader so it can load 16bit data.

parent b81db5eb
......@@ -11,6 +11,7 @@
#include "../dir_nav.h"
#include "png_loader.h"
#include <png.h>
#include "../string.h"
namespace dlib
{
......@@ -154,12 +155,14 @@ namespace dlib
png_init_io( ld_->png_ptr_, fp );
png_set_sig_bytes( ld_->png_ptr_, 8 );
// flags force one byte per channel output
png_read_png( ld_->png_ptr_, ld_->info_ptr_, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING, NULL );
png_read_png( ld_->png_ptr_, ld_->info_ptr_, PNG_TRANSFORM_PACKING, NULL );
height_ = png_get_image_height( ld_->png_ptr_, ld_->info_ptr_ );
width_ = png_get_image_width( ld_->png_ptr_, ld_->info_ptr_ );
bit_depth_ = png_get_bit_depth( ld_->png_ptr_, ld_->info_ptr_ );
color_type_ = png_get_color_type( ld_->png_ptr_, ld_-> info_ptr_ );
std::cout << "bit_depth_: "<< bit_depth_ << std::endl;
if (color_type_ != PNG_COLOR_TYPE_GRAY &&
color_type_ != PNG_COLOR_TYPE_RGB &&
color_type_ != PNG_COLOR_TYPE_RGB_ALPHA )
......@@ -169,6 +172,13 @@ namespace dlib
throw image_load_error(std::string("png_loader: unsupported color type in file ") + filename);
}
if (bit_depth_ != 8 && bit_depth_ != 16)
{
fclose( fp );
png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), &( ld_->end_info_ ) );
throw image_load_error("png_loader: unsupported bit depth of " + cast_to_string(bit_depth_) + " in file " + std::string(filename));
}
ld_->row_pointers_ = png_get_rows( ld_->png_ptr_, ld_->info_ptr_ );
fclose( fp );
......
......@@ -26,6 +26,8 @@ namespace dlib
bool is_rgb() const;
bool is_rgba() const;
unsigned int bit_depth () const { return bit_depth_; }
template<typename T>
void get_image( T& t) const
{
......@@ -42,7 +44,7 @@ namespace dlib
t.set_size( height_, width_ );
if (is_gray())
if (is_gray() && bit_depth_ == 8)
{
for ( unsigned n = 0; n < height_;n++ )
{
......@@ -54,7 +56,19 @@ namespace dlib
}
}
}
else if (is_rgb())
else if (is_gray() && bit_depth_ == 16)
{
for ( unsigned n = 0; n < height_;n++ )
{
const uint16* v = (uint16*)get_row( n );
for ( unsigned m = 0; m < width_;m++ )
{
dlib::uint16 p = v[m];
assign_pixel( t[n][m], p );
}
}
}
else if (is_rgb() && bit_depth_ == 8)
{
for ( unsigned n = 0; n < height_;n++ )
{
......@@ -69,7 +83,22 @@ namespace dlib
}
}
}
else if (is_rgba())
else if (is_rgb() && bit_depth_ == 16)
{
for ( unsigned n = 0; n < height_;n++ )
{
const uint16* v = (uint16*)get_row( n );
for ( unsigned m = 0; m < width_;m++ )
{
rgb_pixel p;
p.red = static_cast<uint8>(v[m*3]);
p.green = static_cast<uint8>(v[m*3+1]);
p.blue = static_cast<uint8>(v[m*3+2]);
assign_pixel( t[n][m], p );
}
}
}
else if (is_rgba() && bit_depth_ == 8)
{
for ( unsigned n = 0; n < height_;n++ )
{
......@@ -85,6 +114,22 @@ namespace dlib
}
}
}
else if (is_rgba() && bit_depth_ == 16)
{
for ( unsigned n = 0; n < height_;n++ )
{
const uint16* v = (uint16*)get_row( n );
for ( unsigned m = 0; m < width_;m++ )
{
rgb_alpha_pixel p;
p.red = static_cast<uint8>(v[m*4]);
p.green = static_cast<uint8>(v[m*4+1]);
p.blue = static_cast<uint8>(v[m*4+2]);
p.alpha = static_cast<uint8>(v[m*4+3]);
assign_pixel( t[n][m], p );
}
}
}
}
private:
......
......@@ -101,6 +101,14 @@ namespace dlib
- returns false
!*/
unsigned int bit_depth (
) const;
/*!
ensures
- returns the number of bits per channel in the image contained by this
object. The possible values are 8 or 16.
!*/
template<
typename image_type
>
......
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