Commit 97d6125f authored by Davis King's avatar Davis King

Made the PNG loader able to load in grayscale images with an alpha channel.

parent f2a52a47
......@@ -73,6 +73,13 @@ namespace dlib
return ( color_type_ == PNG_COLOR_TYPE_GRAY );
}
// ----------------------------------------------------------------------------------------
bool png_loader::is_graya() const
{
return ( color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA );
}
// ----------------------------------------------------------------------------------------
bool png_loader::is_rgb() const
......@@ -169,7 +176,8 @@ namespace dlib
if (color_type_ != PNG_COLOR_TYPE_GRAY &&
color_type_ != PNG_COLOR_TYPE_RGB &&
color_type_ != PNG_COLOR_TYPE_RGB_ALPHA )
color_type_ != PNG_COLOR_TYPE_RGB_ALPHA &&
color_type_ != PNG_COLOR_TYPE_GRAY_ALPHA)
{
fclose( fp );
png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), &( ld_->end_info_ ) );
......
......@@ -23,6 +23,7 @@ namespace dlib
~png_loader();
bool is_gray() const;
bool is_graya() const;
bool is_rgb() const;
bool is_rgba() const;
......@@ -41,6 +42,7 @@ namespace dlib
COMPILE_TIME_ASSERT(sizeof(T) == 0);
#endif
typedef typename T::type pixel_type;
t.set_size( height_, width_ );
......@@ -68,6 +70,52 @@ namespace dlib
}
}
}
else if (is_graya() && bit_depth_ == 8)
{
for ( unsigned n = 0; n < height_;n++ )
{
const unsigned char* v = get_row( n );
for ( unsigned m = 0; m < width_; m++ )
{
unsigned char p = v[m*2];
if (!pixel_traits<pixel_type>::has_alpha)
{
assign_pixel( t[n][m], p );
}
else
{
unsigned char pa = v[m*2+1];
rgb_alpha_pixel pix;
assign_pixel(pix, p);
assign_pixel(pix.alpha, pa);
assign_pixel(t[n][m], pix);
}
}
}
}
else if (is_graya() && 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*2];
if (!pixel_traits<pixel_type>::has_alpha)
{
assign_pixel( t[n][m], p );
}
else
{
dlib::uint16 pa = v[m*2+1];
rgb_alpha_pixel pix;
assign_pixel(pix, p);
assign_pixel(pix.alpha, pa);
assign_pixel(t[n][m], pix);
}
}
}
}
else if (is_rgb() && bit_depth_ == 8)
{
for ( unsigned n = 0; n < height_;n++ )
......
......@@ -75,7 +75,17 @@ namespace dlib
) const;
/*!
ensures
- if (this object contains a grayscale image) then
- if (this object contains a grayscale image without an alpha channel) then
- returns true
- else
- returns false
!*/
bool is_graya(
) const;
/*!
ensures
- if (this object contains a grayscale image with an alpha channel) then
- returns true
- else
- returns false
......
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