Skip to content

Commit 64f08ba

Browse files
committed
LibGfx: Give BilevelImage::create_from_bitmap() a fast path
If the input image is black and white, shortcircuit all the fancy processing. Takes Tests/LibGfx/test-inputs/jbig2/json/compile.sh from 0.23s to 0.20s on my machine. (When running the tests serially, it reduced the time for that from 1.72s to 1.32s on my machine.) (I also tried putting a decoding cache in jbig2-from-image.cpp, but this approach here is more general, simpler, and just as fast.)
1 parent 33f1c58 commit 64f08ba

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Userland/Libraries/LibGfx/ImageFormats/BilevelImage.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ static constexpr auto bayer_matrix_8x8 = make_bayer_matrix<3>();
246246

247247
ErrorOr<NonnullRefPtr<BilevelImage>> BilevelImage::create_from_bitmap(Gfx::Bitmap const& bitmap, DitheringAlgorithm dithering_algorithm)
248248
{
249+
bool input_is_bilevel = true;
250+
for (auto pixel : bitmap) {
251+
if (pixel != 0xFF000000 && pixel != 0xFFFFFFFF) {
252+
input_is_bilevel = false;
253+
break;
254+
}
255+
}
256+
if (input_is_bilevel) {
257+
auto bilevel_image = TRY(BilevelImage::create(bitmap.width(), bitmap.height()));
258+
for (int y = 0; y < bitmap.height(); ++y) {
259+
for (int x = 0; x < bitmap.width(); ++x) {
260+
auto color = bitmap.get_pixel(x, y);
261+
bilevel_image->set_bit(x, y, color == Color::Black ? 1 : 0);
262+
}
263+
}
264+
return bilevel_image;
265+
}
266+
249267
auto gray_bitmap = TRY(ByteBuffer::create_uninitialized(bitmap.width() * bitmap.height()));
250268
for (int y = 0, i = 0; y < bitmap.height(); ++y) {
251269
for (int x = 0; x < bitmap.width(); ++x, ++i) {

0 commit comments

Comments
 (0)