windowscodecs: Add support for palette image formats to PNG encoder.

Signed-off-by: Vincent Povirk <vincent@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Dmitry Timoshkov 2017-09-15 11:12:08 -05:00 committed by Alexandre Julliard
parent be1f61e275
commit 14cca6ee65
2 changed files with 40 additions and 0 deletions

View File

@ -1,5 +1,6 @@
/* /*
* Copyright 2009 Vincent Povirk for CodeWeavers * Copyright 2009 Vincent Povirk for CodeWeavers
* Copyright 2016 Dmitry Timoshkov
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -325,8 +326,10 @@ MAKE_FUNCPTR(png_set_gray_to_rgb);
MAKE_FUNCPTR(png_set_interlace_handling); MAKE_FUNCPTR(png_set_interlace_handling);
MAKE_FUNCPTR(png_set_IHDR); MAKE_FUNCPTR(png_set_IHDR);
MAKE_FUNCPTR(png_set_pHYs); MAKE_FUNCPTR(png_set_pHYs);
MAKE_FUNCPTR(png_set_PLTE);
MAKE_FUNCPTR(png_set_read_fn); MAKE_FUNCPTR(png_set_read_fn);
MAKE_FUNCPTR(png_set_strip_16); MAKE_FUNCPTR(png_set_strip_16);
MAKE_FUNCPTR(png_set_tRNS);
MAKE_FUNCPTR(png_set_tRNS_to_alpha); MAKE_FUNCPTR(png_set_tRNS_to_alpha);
MAKE_FUNCPTR(png_set_write_fn); MAKE_FUNCPTR(png_set_write_fn);
MAKE_FUNCPTR(png_read_end); MAKE_FUNCPTR(png_read_end);
@ -389,8 +392,10 @@ static void *load_libpng(void)
LOAD_FUNCPTR(png_set_interlace_handling); LOAD_FUNCPTR(png_set_interlace_handling);
LOAD_FUNCPTR(png_set_IHDR); LOAD_FUNCPTR(png_set_IHDR);
LOAD_FUNCPTR(png_set_pHYs); LOAD_FUNCPTR(png_set_pHYs);
LOAD_FUNCPTR(png_set_PLTE);
LOAD_FUNCPTR(png_set_read_fn); LOAD_FUNCPTR(png_set_read_fn);
LOAD_FUNCPTR(png_set_strip_16); LOAD_FUNCPTR(png_set_strip_16);
LOAD_FUNCPTR(png_set_tRNS);
LOAD_FUNCPTR(png_set_tRNS_to_alpha); LOAD_FUNCPTR(png_set_tRNS_to_alpha);
LOAD_FUNCPTR(png_set_write_fn); LOAD_FUNCPTR(png_set_write_fn);
LOAD_FUNCPTR(png_read_end); LOAD_FUNCPTR(png_read_end);
@ -1349,6 +1354,10 @@ static const struct png_pixelformat formats[] = {
{&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1}, {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
{&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0}, {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
{&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0}, {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
{&GUID_WICPixelFormat1bppIndexed, 1, 1, PNG_COLOR_TYPE_PALETTE, 0, 0},
{&GUID_WICPixelFormat2bppIndexed, 2, 2, PNG_COLOR_TYPE_PALETTE, 0, 0},
{&GUID_WICPixelFormat4bppIndexed, 4, 4, PNG_COLOR_TYPE_PALETTE, 0, 0},
{&GUID_WICPixelFormat8bppIndexed, 8, 8, PNG_COLOR_TYPE_PALETTE, 0, 0},
{NULL}, {NULL},
}; };
@ -1651,6 +1660,33 @@ static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
(This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER); (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
} }
if (This->format->color_type == PNG_COLOR_TYPE_PALETTE && This->colors)
{
png_color png_palette[256];
png_byte trans[256];
UINT i, num_trans = 0, colors;
/* Newer libpng versions don't accept larger palettes than the declared
* bit depth, so we need to generate the palette of the correct length.
*/
colors = min(This->colors, 1 << This->format->bit_depth);
for (i = 0; i < colors; i++)
{
png_palette[i].red = (This->palette[i] >> 16) & 0xff;
png_palette[i].green = (This->palette[i] >> 8) & 0xff;
png_palette[i].blue = This->palette[i] & 0xff;
trans[i] = (This->palette[i] >> 24) & 0xff;
if (trans[i] != 0xff)
num_trans = i+1;
}
ppng_set_PLTE(This->png_ptr, This->info_ptr, png_palette, colors);
if (num_trans)
ppng_set_tRNS(This->png_ptr, This->info_ptr, trans, num_trans, NULL);
}
ppng_write_info(This->png_ptr, This->info_ptr); ppng_write_info(This->png_ptr, This->info_ptr);
if (This->format->remove_filler) if (This->format->remove_filler)

View File

@ -1359,6 +1359,10 @@ static GUID const * const png_encode_formats[] = {
&GUID_WICPixelFormat32bppBGRA, &GUID_WICPixelFormat32bppBGRA,
&GUID_WICPixelFormat48bppRGB, &GUID_WICPixelFormat48bppRGB,
&GUID_WICPixelFormat64bppRGBA, &GUID_WICPixelFormat64bppRGBA,
&GUID_WICPixelFormat1bppIndexed,
&GUID_WICPixelFormat2bppIndexed,
&GUID_WICPixelFormat4bppIndexed,
&GUID_WICPixelFormat8bppIndexed,
NULL NULL
}; };