summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/PNGLoader.cpp
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-03-03 23:54:07 +0200
committerAndreas Kling <kling@serenityos.org>2021-03-03 23:42:32 +0100
commitc12781a6a2dc074be249c21a31913d9b1458c1c9 (patch)
tree5bba232a939c08f4076f50800065e7ae2b7224ec /Userland/Libraries/LibGfx/PNGLoader.cpp
parent373a595c567cb467aa1f1c627dcfb4fac94dbf86 (diff)
downloadserenity-c12781a6a2dc074be249c21a31913d9b1458c1c9.zip
LibCore+LibHTTP+LibGfx: Switch to LibCompress
This commit removes the only 3rd party library (and its usages) in serenity: puff, which is used for deflate decompression. and replaces it with the existing original serenity implementation in LibCompress. :^)
Diffstat (limited to 'Userland/Libraries/LibGfx/PNGLoader.cpp')
-rw-r--r--Userland/Libraries/LibGfx/PNGLoader.cpp33
1 files changed, 9 insertions, 24 deletions
diff --git a/Userland/Libraries/LibGfx/PNGLoader.cpp b/Userland/Libraries/LibGfx/PNGLoader.cpp
index fbb4a4d8b9..6dc47cc4f6 100644
--- a/Userland/Libraries/LibGfx/PNGLoader.cpp
+++ b/Userland/Libraries/LibGfx/PNGLoader.cpp
@@ -28,7 +28,7 @@
#include <AK/Endian.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
-#include <LibCore/puff.h>
+#include <LibCompress/Gzip.h>
#include <LibGfx/PNGLoader.h>
#include <fcntl.h>
#include <math.h>
@@ -39,6 +39,7 @@
#include <unistd.h>
#ifdef __serenity__
+# include <LibCompress/Deflate.h>
# include <serenity.h>
#endif
@@ -120,8 +121,7 @@ struct PNGLoadingContext {
bool has_alpha() const { return color_type & 4 || palette_transparency_data.size() > 0; }
Vector<Scanline> scanlines;
RefPtr<Gfx::Bitmap> bitmap;
- u8* decompression_buffer { nullptr };
- size_t decompression_buffer_size { 0 };
+ ByteBuffer decompression_buffer;
Vector<u8> compressed_data;
Vector<PaletteEntry> palette_data;
Vector<u8> palette_transparency_data;
@@ -603,7 +603,7 @@ static bool decode_png_chunks(PNGLoadingContext& context)
static bool decode_png_bitmap_simple(PNGLoadingContext& context)
{
- Streamer streamer(context.decompression_buffer, context.decompression_buffer_size);
+ Streamer streamer(context.decompression_buffer.data(), context.decompression_buffer.size());
for (int y = 0; y < context.height; ++y) {
u8 filter;
@@ -749,7 +749,7 @@ static bool decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, in
static bool decode_png_adam7(PNGLoadingContext& context)
{
- Streamer streamer(context.decompression_buffer, context.decompression_buffer_size);
+ Streamer streamer(context.decompression_buffer.data(), context.decompression_buffer.size());
context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::RGBA32 : BitmapFormat::RGB32, { context.width, context.height });
if (!context.bitmap)
return false;
@@ -777,25 +777,12 @@ static bool decode_png_bitmap(PNGLoadingContext& context)
if (context.color_type == 3 && context.palette_data.is_empty())
return false; // Didn't see a PLTE chunk for a palettized image, or it was empty.
- unsigned long srclen = context.compressed_data.size() - 6;
- unsigned long destlen = 0;
- int ret = puff(nullptr, &destlen, context.compressed_data.data() + 2, &srclen);
- if (ret != 0) {
- context.state = PNGLoadingContext::State::Error;
- return false;
- }
- context.decompression_buffer_size = destlen;
-#ifdef __serenity__
- 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");
-#else
- context.decompression_buffer = (u8*)mmap(nullptr, context.decompression_buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
-#endif
-
- ret = puff(context.decompression_buffer, &destlen, context.compressed_data.data() + 2, &srclen);
- if (ret != 0) {
+ auto result = Compress::DeflateDecompressor::decompress_all(context.compressed_data.span().slice(2));
+ if (!result.has_value()) {
context.state = PNGLoadingContext::State::Error;
return false;
}
+ context.decompression_buffer = result.value();
context.compressed_data.clear();
context.scanlines.ensure_capacity(context.height);
@@ -812,9 +799,7 @@ static bool decode_png_bitmap(PNGLoadingContext& context)
VERIFY_NOT_REACHED();
}
- munmap(context.decompression_buffer, context.decompression_buffer_size);
- context.decompression_buffer = nullptr;
- context.decompression_buffer_size = 0;
+ context.decompression_buffer.clear();
context.state = PNGLoadingContext::State::BitmapDecoded;
return true;