Skip to content

Commit a92641d

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

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
@@ -127,10 +127,6 @@ namespace LV {
127127
auto color_type = png_get_color_type (png_ptr, info_ptr);
128128
auto bit_depth = png_get_bit_depth (png_ptr, info_ptr);
129129

130-
if (color_type == PNG_COLOR_TYPE_PALETTE) {
131-
png_set_palette_to_rgb (png_ptr);
132-
}
133-
134130
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
135131
png_set_expand_gray_1_2_4_to_8 (png_ptr);
136132
}
@@ -161,6 +157,9 @@ namespace LV {
161157
VisVideoDepth depth = VISUAL_VIDEO_DEPTH_NONE;
162158

163159
switch (png_get_color_type (png_ptr, info_ptr)) {
160+
case PNG_COLOR_TYPE_PALETTE:
161+
depth = VISUAL_VIDEO_DEPTH_8BIT;
162+
break;
164163
case PNG_COLOR_TYPE_RGB:
165164
depth = VISUAL_VIDEO_DEPTH_24BIT;
166165
break;
@@ -187,11 +186,30 @@ namespace LV {
187186

188187
png_read_image (png_ptr, pixel_row_ptrs);
189188

189+
auto video = Video::wrap (pixels, true, width, height, depth);
190+
191+
if (depth == VISUAL_VIDEO_DEPTH_8BIT) {
192+
png_colorp file_palette = nullptr;
193+
int file_color_count = 0;
194+
png_get_PLTE (png_ptr, info_ptr, &file_palette, &file_color_count);
195+
196+
LV::Palette palette (256);
197+
int color_count = std::min (256, file_color_count);
198+
199+
for (int i = 0; i < color_count; i++) {
200+
auto& color = palette.colors[i];
201+
color.r = file_palette[i].red;
202+
color.g = file_palette[i].green;
203+
color.b = file_palette[i].blue;
204+
}
205+
video->set_palette (std::move (palette));
206+
}
207+
190208
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
191209

192210
delete[] pixel_row_ptrs;
193211

194-
return Video::wrap (pixels, true, width, height, depth);
212+
return video;
195213
}
196214

197215
} // 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)