mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 12:05:00 +00:00
LibGfx+LibPDF: Create filter_type() for converting u8 to FilterType
...and use it in LibPDF. No behavior change.
This commit is contained in:
parent
7e4fe8e610
commit
588d6fab22
3 changed files with 23 additions and 17 deletions
|
@ -698,18 +698,18 @@ static ErrorOr<void> decode_png_bitmap_simple(PNGLoadingContext& context, ByteBu
|
||||||
Streamer streamer(decompression_buffer.data(), decompression_buffer.size());
|
Streamer streamer(decompression_buffer.data(), decompression_buffer.size());
|
||||||
|
|
||||||
for (int y = 0; y < context.height; ++y) {
|
for (int y = 0; y < context.height; ++y) {
|
||||||
PNG::FilterType filter;
|
u8 filter_byte;
|
||||||
if (!streamer.read(filter)) {
|
if (!streamer.read(filter_byte)) {
|
||||||
context.state = PNGLoadingContext::State::Error;
|
context.state = PNGLoadingContext::State::Error;
|
||||||
return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
|
return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (to_underlying(filter) > 4) {
|
if (filter_byte > 4) {
|
||||||
context.state = PNGLoadingContext::State::Error;
|
context.state = PNGLoadingContext::State::Error;
|
||||||
return Error::from_string_literal("PNGImageDecoderPlugin: Invalid PNG filter");
|
return Error::from_string_literal("PNGImageDecoderPlugin: Invalid PNG filter");
|
||||||
}
|
}
|
||||||
|
|
||||||
context.scanlines.append({ filter });
|
context.scanlines.append({ MUST(PNG::filter_type(filter_byte)) });
|
||||||
auto& scanline_buffer = context.scanlines.last().data;
|
auto& scanline_buffer = context.scanlines.last().data;
|
||||||
auto row_size = context.compute_row_size_for_width(context.width);
|
auto row_size = context.compute_row_size_for_width(context.width);
|
||||||
if (row_size.has_overflow())
|
if (row_size.has_overflow())
|
||||||
|
@ -784,18 +784,18 @@ static ErrorOr<void> decode_adam7_pass(PNGLoadingContext& context, Streamer& str
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
for (int y = 0; y < subimage_context.height; ++y) {
|
for (int y = 0; y < subimage_context.height; ++y) {
|
||||||
PNG::FilterType filter;
|
u8 filter_byte;
|
||||||
if (!streamer.read(filter)) {
|
if (!streamer.read(filter_byte)) {
|
||||||
context.state = PNGLoadingContext::State::Error;
|
context.state = PNGLoadingContext::State::Error;
|
||||||
return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
|
return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (to_underlying(filter) > 4) {
|
if (filter_byte > 4) {
|
||||||
context.state = PNGLoadingContext::State::Error;
|
context.state = PNGLoadingContext::State::Error;
|
||||||
return Error::from_string_literal("PNGImageDecoderPlugin: Invalid PNG filter");
|
return Error::from_string_literal("PNGImageDecoderPlugin: Invalid PNG filter");
|
||||||
}
|
}
|
||||||
|
|
||||||
subimage_context.scanlines.append({ filter });
|
subimage_context.scanlines.append({ MUST(PNG::filter_type(filter_byte)) });
|
||||||
auto& scanline_buffer = subimage_context.scanlines.last().data;
|
auto& scanline_buffer = subimage_context.scanlines.last().data;
|
||||||
|
|
||||||
auto row_size = context.compute_row_size_for_width(subimage_context.width);
|
auto row_size = context.compute_row_size_for_width(subimage_context.width);
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Array.h>
|
#include <AK/Array.h>
|
||||||
|
#include <AK/Error.h>
|
||||||
#include <AK/SIMD.h>
|
#include <AK/SIMD.h>
|
||||||
|
|
||||||
namespace Gfx::PNG {
|
namespace Gfx::PNG {
|
||||||
|
@ -32,6 +33,13 @@ enum class FilterType : u8 {
|
||||||
Paeth,
|
Paeth,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline ErrorOr<FilterType> filter_type(u8 byte)
|
||||||
|
{
|
||||||
|
if (byte <= 4)
|
||||||
|
return static_cast<FilterType>(byte);
|
||||||
|
return Error::from_string_literal("PNGImageDecoderPlugin: Invalid PNG filter");
|
||||||
|
}
|
||||||
|
|
||||||
// https://www.w3.org/TR/PNG/#9Filter-type-4-Paeth
|
// https://www.w3.org/TR/PNG/#9Filter-type-4-Paeth
|
||||||
ALWAYS_INLINE u8 paeth_predictor(u8 a, u8 b, u8 c)
|
ALWAYS_INLINE u8 paeth_predictor(u8 a, u8 b, u8 c)
|
||||||
{
|
{
|
||||||
|
|
|
@ -162,19 +162,19 @@ PDFErrorOr<ByteBuffer> Filter::decode_png_prediction(Bytes bytes, int bytes_per_
|
||||||
for (int row_index = 0; row_index < number_of_rows; ++row_index) {
|
for (int row_index = 0; row_index < number_of_rows; ++row_index) {
|
||||||
auto row = bytes.data() + row_index * bytes_per_row;
|
auto row = bytes.data() + row_index * bytes_per_row;
|
||||||
|
|
||||||
u8 algorithm_tag = row[0];
|
auto filter = TRY(Gfx::PNG::filter_type(row[0]));
|
||||||
switch (algorithm_tag) {
|
switch (filter) {
|
||||||
case 0:
|
case Gfx::PNG::FilterType::None:
|
||||||
break;
|
break;
|
||||||
case 1:
|
case Gfx::PNG::FilterType::Sub:
|
||||||
for (int i = 2; i < bytes_per_row; ++i)
|
for (int i = 2; i < bytes_per_row; ++i)
|
||||||
row[i] += row[i - 1];
|
row[i] += row[i - 1];
|
||||||
break;
|
break;
|
||||||
case 2:
|
case Gfx::PNG::FilterType::Up:
|
||||||
for (int i = 1; i < bytes_per_row; ++i)
|
for (int i = 1; i < bytes_per_row; ++i)
|
||||||
row[i] += previous_row[i];
|
row[i] += previous_row[i];
|
||||||
break;
|
break;
|
||||||
case 3:
|
case Gfx::PNG::FilterType::Average:
|
||||||
for (int i = 1; i < bytes_per_row; ++i) {
|
for (int i = 1; i < bytes_per_row; ++i) {
|
||||||
u8 left = 0;
|
u8 left = 0;
|
||||||
if (i > 1)
|
if (i > 1)
|
||||||
|
@ -183,7 +183,7 @@ PDFErrorOr<ByteBuffer> Filter::decode_png_prediction(Bytes bytes, int bytes_per_
|
||||||
row[i] += (left + above) / 2;
|
row[i] += (left + above) / 2;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 4:
|
case Gfx::PNG::FilterType::Paeth:
|
||||||
for (int i = 1; i < bytes_per_row; ++i) {
|
for (int i = 1; i < bytes_per_row; ++i) {
|
||||||
u8 left = 0;
|
u8 left = 0;
|
||||||
u8 upper_left = 0;
|
u8 upper_left = 0;
|
||||||
|
@ -195,8 +195,6 @@ PDFErrorOr<ByteBuffer> Filter::decode_png_prediction(Bytes bytes, int bytes_per_
|
||||||
row[i] += Gfx::PNG::paeth_predictor(left, above, upper_left);
|
row[i] += Gfx::PNG::paeth_predictor(left, above, upper_left);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
return AK::Error::from_string_literal("Unknown PNG algorithm tag");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
previous_row = row;
|
previous_row = row;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue