Skip to content

Commit

Permalink
Core (LV::Video): Load 8-bit indexed PNG images directly into 8-bit v…
Browse files Browse the repository at this point in the history
…ideos with palette.
kaixiong committed Jan 25, 2025
1 parent 9f20838 commit a92641d
Showing 2 changed files with 24 additions and 9 deletions.
28 changes: 23 additions & 5 deletions libvisual/libvisual/private/lv_video_png.cpp
Original file line number Diff line number Diff line change
@@ -127,10 +127,6 @@ namespace LV {
auto color_type = png_get_color_type (png_ptr, info_ptr);
auto bit_depth = png_get_bit_depth (png_ptr, info_ptr);

if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb (png_ptr);
}

if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
png_set_expand_gray_1_2_4_to_8 (png_ptr);
}
@@ -161,6 +157,9 @@ namespace LV {
VisVideoDepth depth = VISUAL_VIDEO_DEPTH_NONE;

switch (png_get_color_type (png_ptr, info_ptr)) {
case PNG_COLOR_TYPE_PALETTE:
depth = VISUAL_VIDEO_DEPTH_8BIT;
break;
case PNG_COLOR_TYPE_RGB:
depth = VISUAL_VIDEO_DEPTH_24BIT;
break;
@@ -187,11 +186,30 @@ namespace LV {

png_read_image (png_ptr, pixel_row_ptrs);

auto video = Video::wrap (pixels, true, width, height, depth);

if (depth == VISUAL_VIDEO_DEPTH_8BIT) {
png_colorp file_palette = nullptr;
int file_color_count = 0;
png_get_PLTE (png_ptr, info_ptr, &file_palette, &file_color_count);

LV::Palette palette (256);
int color_count = std::min (256, file_color_count);

for (int i = 0; i < color_count; i++) {
auto& color = palette.colors[i];
color.r = file_palette[i].red;
color.g = file_palette[i].green;
color.b = file_palette[i].blue;
}
video->set_palette (std::move (palette));
}

png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);

delete[] pixel_row_ptrs;

return Video::wrap (pixels, true, width, height, depth);
return video;
}

} // LV namespace
5 changes: 1 addition & 4 deletions libvisual/tests/video_test/video_load_test.cpp
Original file line number Diff line number Diff line change
@@ -102,11 +102,8 @@ namespace
png_image->get_width (),
png_image->get_height ())};

auto raw_image_rgb24 {LV::Video::create (raw_image->get_width (), raw_image->get_height (), VISUAL_VIDEO_DEPTH_24BIT)};
raw_image_rgb24->convert_depth (raw_image);

LV_TEST_ASSERT (bmp_image->has_same_content (raw_image));
LV_TEST_ASSERT (png_image->has_same_content (raw_image_rgb24));
LV_TEST_ASSERT (png_image->has_same_content (raw_image));
}

void test_load_rgb24 ()

0 comments on commit a92641d

Please sign in to comment.