LibGfx: Decode paletted and grayscale images with 1/2/4 bit depth

When dealing with png data that has less than 8 bits per pixel, round
up to the next byte when allocating per row buffers and streamers. This
fixes decoding odd sized PNGs with less than 8 bits per pixel.

Also added a test page with some odd sized palleted PNGs.
This commit is contained in:
LepkoQQ 2020-05-01 13:24:23 +02:00 committed by Andreas Kling
parent e37065cc8b
commit c7f0de14b5
Notes: sideshowbarker 2024-07-19 07:07:33 +09:00
3 changed files with 95 additions and 32 deletions

View file

@ -0,0 +1,30 @@
<HTML>
<HEAD>
<TITLE>PngSuite - Odd sizes / PNG-files</TITLE>
</HEAD>
<BODY BGCOLOR="#ede">
<!-- Modified version of http://schaik.com/pngsuite/pngsuite_siz_png.html -->
<BR><IMG SRC="http://schaik.com/pngsuite/s01n3p01.png" WIDTH="1" HEIGHT="1"> --- s01n3p01 - 1x1 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s02n3p01.png" WIDTH="2" HEIGHT="2"> --- s02n3p01 - 2x2 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s03n3p01.png" WIDTH="3" HEIGHT="3"> --- s03n3p01 - 3x3 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s04n3p01.png" WIDTH="4" HEIGHT="4"> --- s04n3p01 - 4x4 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s05n3p02.png" WIDTH="5" HEIGHT="5"> --- s05n3p02 - 5x5 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s06n3p02.png" WIDTH="6" HEIGHT="6"> --- s06n3p02 - 6x6 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s07n3p02.png" WIDTH="7" HEIGHT="7"> --- s07n3p02 - 7x7 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s08n3p02.png" WIDTH="8" HEIGHT="8"> --- s08n3p02 - 8x8 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s09n3p02.png" WIDTH="9" HEIGHT="9"> --- s09n3p02 - 9x9 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s32n3p04.png" WIDTH="32" HEIGHT="32"> --- s32n3p04 - 32x32 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s33n3p04.png" WIDTH="33" HEIGHT="33"> --- s33n3p04 - 33x33 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s34n3p04.png" WIDTH="34" HEIGHT="34"> --- s34n3p04 - 34x34 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s35n3p04.png" WIDTH="35" HEIGHT="35"> --- s35n3p04 - 35x35 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s36n3p04.png" WIDTH="36" HEIGHT="36"> --- s36n3p04 - 36x36 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s37n3p04.png" WIDTH="37" HEIGHT="37"> --- s37n3p04 - 37x37 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s38n3p04.png" WIDTH="38" HEIGHT="38"> --- s38n3p04 - 38x38 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s39n3p04.png" WIDTH="39" HEIGHT="39"> --- s39n3p04 - 39x39 paletted file, no interlacing
<BR><IMG SRC="http://schaik.com/pngsuite/s40n3p04.png" WIDTH="40" HEIGHT="40"> --- s40n3p04 - 40x40 paletted file, no interlacing
</BODY>
</HTML>

View file

@ -28,6 +28,7 @@ span#ua {
<p>Your user agent is: <b><span id="ua"></span></b></p>
<p>Some small test pages:</p>
<ul>
<li><a href="pngsuite_siz_png.html">pngsuite odd sizes test</a></li>
<li><a href="pngsuite_bas_png.html">pngsuite basic formats test</a></li>
<li><a href="canvas-path.html">canvas path house!</a></li>
<li><a href="img-canvas.html">canvas drawImage() test</a></li>

View file

@ -30,6 +30,7 @@
#include <AK/NetworkOrdered.h>
#include <LibCore/puff.h>
#include <LibGfx/PNGLoader.h>
#include <LibM/math.h>
#include <fcntl.h>
#include <serenity.h>
#include <stdio.h>
@ -120,7 +121,7 @@ struct PNGLoadingContext {
u8 compression_method { 0 };
u8 filter_method { 0 };
u8 interlace_method { 0 };
u8 bytes_per_pixel { 0 };
u8 channels { 0 };
bool has_seen_zlib_header { false };
bool has_alpha() const { return color_type & 4 || palette_transparency_data.size() > 0; }
Vector<Scanline> scanlines;
@ -339,6 +340,21 @@ NEVER_INLINE FLATTEN static void unfilter(PNGLoadingContext& context)
pixel.a = 0xff;
}
}
} else if (context.bit_depth == 1 || context.bit_depth == 2 || context.bit_depth == 4) {
auto pixels_per_byte = 8 / context.bit_depth;
auto mask = (1 << context.bit_depth) - 1;
for (int y = 0; y < context.height; ++y) {
auto* gray_values = (u8*)context.scanlines[y].data.data();
for (int i = 0; i < context.width; ++i) {
auto bit_offset = (8 - context.bit_depth) - (context.bit_depth * (i % pixels_per_byte));
auto value = (gray_values[i / pixels_per_byte] >> bit_offset) & mask;
auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
pixel.r = value * (0xff / pow(context.bit_depth, 2));
pixel.g = value * (0xff / pow(context.bit_depth, 2));
pixel.b = value * (0xff / pow(context.bit_depth, 2));
pixel.a = 0xff;
}
}
} else {
ASSERT_NOT_REACHED();
}
@ -418,19 +434,42 @@ NEVER_INLINE FLATTEN static void unfilter(PNGLoadingContext& context)
}
break;
case 3:
for (int y = 0; y < context.height; ++y) {
auto* palette_index = (u8*)context.scanlines[y].data.data();
for (int i = 0; i < context.width; ++i) {
auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
auto& color = context.palette_data.at((int)palette_index[i]);
auto transparency = context.palette_transparency_data.size() >= palette_index[i] + 1u
? context.palette_transparency_data.data()[palette_index[i]]
: 0xff;
pixel.r = color.r;
pixel.g = color.g;
pixel.b = color.b;
pixel.a = transparency;
if (context.bit_depth == 8) {
for (int y = 0; y < context.height; ++y) {
auto* palette_index = (u8*)context.scanlines[y].data.data();
for (int i = 0; i < context.width; ++i) {
auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
auto& color = context.palette_data.at((int)palette_index[i]);
auto transparency = context.palette_transparency_data.size() >= palette_index[i] + 1u
? context.palette_transparency_data.data()[palette_index[i]]
: 0xff;
pixel.r = color.r;
pixel.g = color.g;
pixel.b = color.b;
pixel.a = transparency;
}
}
} else if (context.bit_depth == 1 || context.bit_depth == 2 || context.bit_depth == 4) {
auto pixels_per_byte = 8 / context.bit_depth;
auto mask = (1 << context.bit_depth) - 1;
for (int y = 0; y < context.height; ++y) {
auto* palette_indexes = (u8*)context.scanlines[y].data.data();
for (int i = 0; i < context.width; ++i) {
auto bit_offset = (8 - context.bit_depth) - (context.bit_depth * (i % pixels_per_byte));
auto palette_index = (palette_indexes[i / pixels_per_byte] >> bit_offset) & mask;
auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
auto& color = context.palette_data.at(palette_index);
auto transparency = context.palette_transparency_data.size() >= palette_index + 1u
? context.palette_transparency_data.data()[palette_index]
: 0xff;
pixel.r = color.r;
pixel.g = color.g;
pixel.b = color.b;
pixel.a = transparency;
}
}
} else {
ASSERT_NOT_REACHED();
}
break;
default:
@ -586,7 +625,8 @@ static bool decode_png_bitmap(PNGLoadingContext& context)
context.scanlines.append({ filter });
auto& scanline_buffer = context.scanlines.last().data;
if (!streamer.wrap_bytes(scanline_buffer, context.width * context.bytes_per_pixel)) {
auto row_size = ((context.width * context.channels * context.bit_depth) + 7) / 8;
if (!streamer.wrap_bytes(scanline_buffer, row_size)) {
context.state = PNGLoadingContext::State::Error;
return false;
}
@ -648,36 +688,28 @@ static bool process_IHDR(const ByteBuffer& data, PNGLoadingContext& context, boo
switch (context.color_type) {
case 0: // Each pixel is a grayscale sample.
// FIXME: Implement support for 1/2/4 bit grayscale based images.
if (ihdr.bit_depth != 8 && ihdr.bit_depth != 16) {
dbgprintf("PNGLoader::process_IHDR: Unsupported grayscale format (%d bpp).\n", context.bit_depth);
return false;
}
context.bytes_per_pixel = ihdr.bit_depth / 8;
context.channels = 1;
break;
case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
context.bytes_per_pixel = 2 * ihdr.bit_depth / 8;
context.channels = 2;
break;
case 2:
context.bytes_per_pixel = 3 * (ihdr.bit_depth / 8);
case 2: // Each pixel is an RGB sample
context.channels = 3;
break;
case 3: // Each pixel is a palette index; a PLTE chunk must appear.
// FIXME: Implement support for 1/2/4 bit palette based images.
if (ihdr.bit_depth != 8) {
dbgprintf("PNGLoader::process_IHDR: Unsupported index-based format (%d bpp).\n", context.bit_depth);
return false;
}
context.bytes_per_pixel = 1;
context.channels = 1;
break;
case 6:
context.bytes_per_pixel = 4 * (ihdr.bit_depth / 8);
case 6: // Each pixel is an RGB sample, followed by an alpha sample.
context.channels = 4;
break;
default:
ASSERT_NOT_REACHED();
}
if (!decode_size_only) {
context.decompression_buffer_size = (context.width * context.height * context.bytes_per_pixel + context.height);
// Calculate number of bytes per row (+1 for filter)
auto row_size = ((context.width * context.channels * context.bit_depth) + 7) / 8 + 1;
context.decompression_buffer_size = row_size * context.height;
context.decompression_buffer = (u8*)mmap_with_name(nullptr, context.decompression_buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "PNG decompression buffer");
}
return true;