Skip to content

Commit f7d7053

Browse files
committed
Core (LV::Video): Load 8-bit indexed PNG images directly into 8-bit videos with palette.
1 parent 5a3a3c8 commit f7d7053

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

libvisual/libvisual/private/lv_video_png.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ namespace LV {
109109
auto color_type = png_get_color_type (png_ptr, info_ptr);
110110
auto bit_depth = png_get_bit_depth (png_ptr, info_ptr);
111111

112-
if (color_type == PNG_COLOR_TYPE_PALETTE) {
113-
png_set_palette_to_rgb (png_ptr);
114-
}
115-
116112
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
117113
png_set_expand_gray_1_2_4_to_8 (png_ptr);
118114
}
@@ -143,6 +139,9 @@ namespace LV {
143139
VisVideoDepth depth = VISUAL_VIDEO_DEPTH_NONE;
144140

145141
switch (png_get_color_type (png_ptr, info_ptr)) {
142+
case PNG_COLOR_TYPE_PALETTE:
143+
depth = VISUAL_VIDEO_DEPTH_8BIT;
144+
break;
146145
case PNG_COLOR_TYPE_RGB:
147146
depth = VISUAL_VIDEO_DEPTH_24BIT;
148147
break;
@@ -169,11 +168,30 @@ namespace LV {
169168

170169
png_read_image (png_ptr, pixel_row_ptrs);
171170

171+
auto video = Video::wrap (pixels, true, width, height, depth);
172+
173+
if (depth == VISUAL_VIDEO_DEPTH_8BIT) {
174+
png_colorp file_palette = nullptr;
175+
int file_color_count = 0;
176+
png_get_PLTE (png_ptr, info_ptr, &file_palette, &file_color_count);
177+
178+
LV::Palette palette (256);
179+
int color_count = std::min (256, file_color_count);
180+
181+
for (int i = 0; i < color_count; i++) {
182+
auto& color = palette.colors[i];
183+
color.r = file_palette[i].red;
184+
color.g = file_palette[i].green;
185+
color.b = file_palette[i].blue;
186+
}
187+
video->set_palette (std::move (palette));
188+
}
189+
172190
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
173191

174192
delete []pixel_row_ptrs;
175193

176-
return Video::wrap (pixels, true, width, height, depth);
194+
return video;
177195
}
178196

179197
} // LV namespace

libvisual/tests/video_test/video_load_test.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,8 @@ namespace
102102
png_image->get_width (),
103103
png_image->get_height ())};
104104

105-
auto raw_image_rgb24 {LV::Video::create (raw_image->get_width (), raw_image->get_height (), VISUAL_VIDEO_DEPTH_24BIT)};
106-
raw_image_rgb24->convert_depth (raw_image);
107-
108105
LV_TEST_ASSERT (bmp_image->has_same_content (raw_image));
109-
LV_TEST_ASSERT (png_image->has_same_content (raw_image_rgb24));
106+
LV_TEST_ASSERT (png_image->has_same_content (raw_image));
110107
}
111108

112109
void test_load_rgb24 ()

0 commit comments

Comments
 (0)