|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#include "GdiPlusFlat.h" |
| 7 | + |
| 8 | +static int status_counter = 0; |
| 9 | + |
| 10 | +#define CHECK_STATUS(x) do { if (status != Ok) { printf ("status[%d] == %d!\n", status_counter++, status); if(x) { exit(-1); } } else { printf ("status[%d] == Ok\n", status_counter++); } } while (0) |
| 11 | +#define CHECK_ASSERT(x) do { if (!(x)) { printf ("check %s at %s:%d failed\n", #x, __FILE__, __LINE__); exit(-1); } else { printf("check %s at %s:%d passed\n", #x, __FILE__, __LINE__); } } while (0) |
| 12 | + |
| 13 | +CLSID png_clsid = { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } }; |
| 14 | + |
| 15 | +int |
| 16 | +main (int argc, char **argv) |
| 17 | +{ |
| 18 | + GpImage *img; |
| 19 | + gunichar2 *unis; |
| 20 | + GpBitmap *bitmap; |
| 21 | + GpStatus status; |
| 22 | + int original_palette_size; |
| 23 | + int reloaded_palette_size; |
| 24 | + ColorPalette *original_palette; |
| 25 | + ColorPalette *reloaded_palette; |
| 26 | + GdiplusStartupInput gdiplusStartupInput; |
| 27 | + ULONG_PTR gdiplusToken; |
| 28 | + |
| 29 | + GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); |
| 30 | + |
| 31 | + // PNG resave should preserve the palette transparency. Let's test it |
| 32 | + // by loading a PNG file and its palette, then resaving it and loading |
| 33 | + // it again for comparison. |
| 34 | + unis = g_utf8_to_utf16 ("test-trns.png", -1, NULL, NULL, NULL); |
| 35 | + status = GdipLoadImageFromFile (unis, &img); |
| 36 | + CHECK_STATUS(1); |
| 37 | + g_free (unis); |
| 38 | + |
| 39 | + status = GdipGetImagePaletteSize (img, &original_palette_size); |
| 40 | + CHECK_STATUS(1); |
| 41 | + CHECK_ASSERT(original_palette_size > 0); |
| 42 | + original_palette = malloc (original_palette_size); |
| 43 | + GdipGetImagePalette (img, original_palette, original_palette_size); |
| 44 | + CHECK_STATUS(1); |
| 45 | + |
| 46 | + unis = g_utf8_to_utf16 ("test-trns-resave.png", -1, NULL, NULL, NULL); |
| 47 | + status = GdipSaveImageToFile (img, unis, &png_clsid, NULL); |
| 48 | + CHECK_STATUS(1); |
| 49 | + GdipDisposeImage (img); |
| 50 | + status = GdipLoadImageFromFile (unis, &img); |
| 51 | + CHECK_STATUS(1); |
| 52 | + g_free (unis); |
| 53 | + |
| 54 | + status = GdipGetImagePaletteSize (img, &reloaded_palette_size); |
| 55 | + CHECK_STATUS(1); |
| 56 | + CHECK_ASSERT(reloaded_palette_size > 0); |
| 57 | + CHECK_ASSERT(reloaded_palette_size == original_palette_size); |
| 58 | + reloaded_palette = malloc (reloaded_palette_size); |
| 59 | + GdipGetImagePalette (img, reloaded_palette, reloaded_palette_size); |
| 60 | + CHECK_STATUS(1); |
| 61 | + |
| 62 | + CHECK_ASSERT(memcmp (original_palette, reloaded_palette, original_palette_size) == 0); |
| 63 | + |
| 64 | + GdipDisposeImage (img); |
| 65 | + img = NULL; |
| 66 | + unlink ("test-trns-resave.png"); |
| 67 | + free (original_palette); |
| 68 | + free (reloaded_palette); |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
0 commit comments