int currentframe;
int currentx, currenty;
unsigned char **framedata;
+ unsigned char **mask;
uint32_t offsets[];
};
size_t rc;
assert(img->framedata);
+ assert(img->mask);
if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
type = letohs(type); img->foff += sizeof type;
return -1;
}
+ memset(&img->mask[img->currenty][img->currentx], 1, count);
+
pos = &img->framedata[img->currenty][img->currentx];
rc = fread(pos, 1, count, img->f);
img->currentx += rc;
return -1;
}
+static unsigned char **allocframebuffer(size_t width, size_t height)
+{
+ unsigned char **new, *tmp;
+ size_t i;
+
+ tmp = calloc(height, width);
+ if (!tmp) {
+ lbx_errno = -errno;
+ return NULL;
+ }
+
+ new = malloc(height * sizeof *new);
+ if (!new) {
+ lbx_errno = -errno;
+ free(tmp);
+ return NULL;
+ }
+
+ for (i = 0; i < height; i++) {
+ new[i] = tmp + i * width;
+ }
+
+ return new;
+}
+
unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
{
if (frame >= img->frames || frame < 0) {
}
if (!img->framedata) {
- unsigned char *tmp;
- int i;
-
- tmp = malloc(img->width * img->height);
- if (!tmp) {
- lbx_errno = -errno;
+ img->framedata = allocframebuffer(img->width, img->height);
+ if (!img->framedata)
return NULL;
- }
+ }
- img->framedata = malloc(img->height * sizeof *img->framedata);
- if (!img->framedata) {
- lbx_errno = -errno;
- free(tmp);
+ if (!img->mask) {
+ img->mask = allocframebuffer(img->width, img->height);
+ if (!img->mask)
return NULL;
- }
-
- for (i = 0; i < img->height; i++) {
- img->framedata[i] = tmp + i * img->width;
- }
}
/* Start over if we are backtracking. */
if (img->flags & FLAG_OVERWRITE) {
/* Clear the slate. */
memset(img->framedata[0], 0, img->width * img->height);
+ memset(img->mask[0], 0, img->width * img->height);
} else {
/* We must have previous frame decoded to continue. */
if (frame > img->currentframe + 1) {
}
}
+unsigned char **lbximg_getmask(struct lbx_image *img)
+{
+ return img->mask;
+}
+
void lbximg_close(struct lbx_image *img)
{
if (!img) return;
free(img->framedata);
}
+ if (img->mask) {
+ free(img->mask[0]);
+ free(img->mask);
+ }
+
if (img->f) {
fclose(img->f);
}
return 0;
}
-int outpng(unsigned int frameno, unsigned char **framedata,
- unsigned int width, unsigned int height,
+static int ismasked(unsigned char **mask, unsigned width, unsigned height)
+{
+ unsigned y, x;
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ if (mask[y][x] == 0) return 1;
+ }
+ }
+
+ return 0;
+}
+
+int outpng(unsigned int frameno,
+ unsigned char **framedata, unsigned char **mask,
+ unsigned int width, unsigned int height,
struct lbx_colour palette[static 256])
{
char name[strlen(outname) + sizeof ".65535.png"];
+ unsigned char *row;
+ unsigned int x, y;
FILE *of;
png_structp png;
assert(frameno < 65536);
snprintf(name, sizeof name, "%s.%03d.png", outname, frameno);
-
+
+ row = malloc(4 * width);
+ if (!row) {
+ errmsg("failed to allocate row buffer: %s\n", strerror(errno));
+ return -1;
+ }
+
of = fopen(name, "wb");
if (!of) {
errmsg("failed to open %s: %s.\n", name, strerror(errno));
+ free(row);
return -1;
}
}
if (setjmp(png_jmpbuf(png))) {
+ free(row);
png_destroy_write_struct(&png, &info);
goto err;
}
-
- png_set_IHDR(png, info, width, height, 8,
- usepalette ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_GRAY,
- PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
- PNG_FILTER_TYPE_DEFAULT);
+ png_init_io(png, of);
- if (usepalette)
- png_set_PLTE(png, info, (png_colorp)palette, 256);
- png_set_rows(png, info, framedata);
+ 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_init_io(png, of);
- png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
+ 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_colorp)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);
+ }
png_destroy_write_struct(&png, &info);
fclose(of);
printf("wrote %s\n", name);
return 0;
+
err:
fclose(of);
remove(name);
+ free(row);
return -1;
}
+static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
+{
+ LBX_IMG *overimg = lbximg_fopen(f);
+ struct lbx_imginfo info;
+
+ if (!overimg) {
+ errmsg("failed to open override image: %s\n", lbx_strerror());
+ return -1;
+ }
+ lbximg_getinfo(overimg, &info);
+
+ if (!info.haspalette) {
+ errmsg("override image has no palette.\n", 0);
+ lbximg_close(overimg);
+ return -1;
+ }
+
+ if (lbximg_getpalette(overimg, palette) == -1) {
+ errmsg("error reading override palette: %s\n", lbx_strerror());
+ lbximg_close(overimg);
+ return -1;
+ }
+
+ lbximg_close(overimg);
+ return 0;
+}
+
static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
- FILE *palf, struct lbx_colour palette[static 256])
+ FILE *palf, FILE *override,
+ struct lbx_colour palette[static 256])
{
int i;
- for (i = 0; i < 256; i++)
- palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
-
- if (palf) {
- if (lbximg_loadpalette(palf, palette) == -1) {
- errmsg("error reading palette: %s\n", lbx_strerror());
- return -1;
+ /* In no-palette mode, use palette indices for colour. */
+ if (!usepalette) {
+ for (i = 0; i < 256; i++) {
+ palette[i] = (struct lbx_colour){i,i,i};
}
- } else if (!info->haspalette) {
+
+ return 0;
+ }
+
+ /* For sanity. */
+ if (!palf && !info->haspalette && !override) {
errmsg("no palette available.\n", 0);
return -1;
}
- if (lbximg_getpalette(img, palette) == -1) {
+ /* Default the palette to a wonderful pink. */
+ for (i = 0; i < 256; i++) {
+ palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
+ }
+
+ /* Read the external palette, if any. */
+ if (palf && lbximg_loadpalette(palf, palette) == -1) {
+ errmsg("error reading external palette: %s\n", lbx_strerror());
+ return -1;
+ }
+
+ /* Read the embedded palette, if any. */
+ if (info->haspalette && lbximg_getpalette(img, palette) == -1) {
errmsg("error reading embedded palette: %s\n", lbx_strerror());
return -1;
}
+ /* Read the override palette, if any. */
+ if (override && loadoverride(override, palette) == -1) {
+ return -1;
+ }
+
return 0;
}
}
}
- if (usepalette && loadpalette(img, &info, palf, palette) == -1) {
+ if (loadpalette(img, &info, palf, override, palette) == -1) {
goto err;
}
- if (override) {
- LBX_IMG *overimg = lbximg_fopen(override);
- struct lbx_imginfo info;
-
- if (!overimg) {
- errmsg("failed to open override image: %s\n",
- lbx_strerror());
- goto err;
- }
- lbximg_getinfo(overimg, &info);
-
- if (!info.haspalette) {
- errmsg("override image has no palette.\n", 0);
- lbximg_close(overimg);
- goto err;
- }
-
- if (lbximg_getpalette(overimg, palette) == -1) {
- errmsg("error reading override palette: %s\n",
- lbx_strerror());
- lbximg_close(overimg);
- goto err;
- }
-
- lbximg_close(overimg);
- }
-
/* Extract the images, in order. */
for (i = 0; i < info.nframes; i++) {
- unsigned char **framedata;
+ unsigned char **data;
+ unsigned char **mask;
if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
continue;
- framedata = lbximg_getframe(img, i);
- if (!framedata) {
+ data = lbximg_getframe(img, i);
+ if (!data) {
errmsg("error in frame %u: %s\n", i, lbx_strerror());
continue;
}
- if (!outpng(i, framedata, info.width, info.height, palette)) {
+ mask = lbximg_getmask(img);
+
+ if (!outpng(i, data, mask, info.width, info.height, palette)) {
extracted = 1;
}
}
errmsg("no frames extracted.\n", 0);
goto err;
}
-
+
free(framebits);
return EXIT_SUCCESS;
err: