+
+ if (!ismasked(mask, width, height)) {
+ /*
+ * This case is easy; we can just feed the palette and pixel
+ * data to libpng and let it do its magic.
+ */
+
+ png_color png_palette[256];
+ for (unsigned i = 0; i < 256; i++) {
+ png_palette[i].red = palette[i].red;
+ png_palette[i].green = palette[i].green;
+ png_palette[i].blue = palette[i].blue;
+ }
+
+ png_set_IHDR(png, info, width, height, 8,
+ PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT,
+ PNG_FILTER_TYPE_DEFAULT);
+
+ png_set_PLTE(png, info, png_palette, 256);
+ png_set_rows(png, info, framedata);
+ png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
+ } else {
+ /*
+ * Unfortunately, LBX doesn't translate nicely to PNG here.
+ * LBX has a 256 colour palette _plus_ transparency.
+ * We'll form an RGBA PNG to deal with this.
+ */
+
+ png_set_IHDR(png, info, width, height, 8,
+ PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT,
+ PNG_FILTER_TYPE_DEFAULT);
+
+ png_write_info(png, info);
+
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ row[4*x+0] = palette[framedata[y][x]].red;
+ row[4*x+1] = palette[framedata[y][x]].green;
+ row[4*x+2] = palette[framedata[y][x]].blue;
+ row[4*x+3] = (mask[y][x]) ? -1 : 0;
+ }
+
+ png_write_row(png, row);
+ }
+
+ png_write_end(png, NULL);
+ }