@@ -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
0 commit comments