X-Git-Url: http://git.draconx.ca/gitweb/liblbx.git/blobdiff_plain/5f678b5604af19ce003930897e77fd78d7ab5c72..bd5f6b55320b6889827908762197642b484ea28d:/src/pnm.c diff --git a/src/pnm.c b/src/pnm.c index 5359ac2..43108f8 100644 --- a/src/pnm.c +++ b/src/pnm.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -311,6 +312,68 @@ err: return -1; } +static void pam_format_rgba(unsigned char *rowbuf, const unsigned char *pixels, + unsigned char *mask, unsigned width, + unsigned long offset, struct lbx_colour *palette) +{ + unsigned x; + + for (x = 0; x < width; x++) { + struct lbx_colour val = palette[pixels[offset+x]]; + unsigned vis = 1 & ( mask[(offset+x)/CHAR_BIT] + >> ((offset+x)%CHAR_BIT) ); + + if (sizeof val == 4) { + memcpy(rowbuf+4ul*x, &val, 4); + } else { + rowbuf[4ul*x+0] = val.red; + rowbuf[4ul*x+1] = val.green; + rowbuf[4ul*x+2] = val.blue; + } + rowbuf[4ul*x+3] = -vis & 0x3f; + } +} + +static void pam_format_rgb(unsigned char *rowbuf, const unsigned char *pixels, + unsigned width, unsigned long offset, + struct lbx_colour *palette) +{ + unsigned x; + + for (x = 0; x < width; x++) { + struct lbx_colour val = palette[pixels[offset+x]]; + + if (sizeof val == 4) { + memcpy(rowbuf+3ul*x, &val, 4); + } else { + rowbuf[3ul*x+0] = val.red; + rowbuf[3ul*x+1] = val.green; + rowbuf[3ul*x+2] = val.blue; + } + } +} + +static void pam_format_ga(unsigned char *rowbuf, + const unsigned char *pixels, const unsigned char *mask, + unsigned width, unsigned long offset) +{ + unsigned x; + + for (x = 0; x < width; x++) { + unsigned vis = 1 & ( mask[(offset+x)/CHAR_BIT] + >> ((offset+x)%CHAR_BIT) ); + + rowbuf[2ul*x+0] = pixels[offset+x]; + rowbuf[2ul*x+1] = -vis & 0xff; + } +} + +static void pam_format_g(unsigned char *rowbuf, const unsigned char *pixels, + unsigned width, unsigned long offset) +{ + memcpy(rowbuf, pixels+offset, width); +} + /* * Output filter for Netpbm's PAM format. This format combines the features * of all the other Netpbm formats, supporting RGB and grayscale images with @@ -322,59 +385,61 @@ int img_output_pam(FILE *f, const char *filename, struct lbx_colour *palette) { bool masked = img_is_masked(mask, width, height); - unsigned x, y; - size_t depth; - - if (fprintf_ascii(f, "P7\nWIDTH %u\nHEIGHT %u\n", width, height) < 0) - goto err; - - if (palette) { - depth = masked ? 4 : 3; - - if (fprintf_ascii(f, "DEPTH %zu\nMAXVAL 63\n" - "TUPLTYPE RGB%s\nENDHDR\n", - depth, masked ? "_ALPHA" : "") < 0) - goto err; - - for (x = y = 0; y < height; ++x < width || (x = 0, y++)) { - unsigned long offset = (unsigned long) y * width + x; - struct lbx_colour *c = &palette[pixels[offset]]; - unsigned char buf[4]; - bool vis; - - vis = mask[offset/CHAR_BIT] & (1u << offset%CHAR_BIT); - buf[0] = c->red; - buf[1] = c->green; - buf[2] = c->blue; - buf[3] = vis ? 63 : 0; + unsigned char *rowbuf; + unsigned long offset; + unsigned y, depth; + int rc; + + depth = (palette ? 3 : 1) + masked; + if (width >= (size_t)-1 / depth) + goto alloc_err; + + rowbuf = malloc(width * depth + 1); + if (!rowbuf) { +alloc_err: + tool_err(-1, "failed to allocate memory"); + return -1; + } - if (fwrite(buf, 1, depth, f) < depth) - goto err; + rc = fprintf_ascii(f, "P7\nWIDTH %u\nHEIGHT %u\nDEPTH %u\n", + width, height, depth); + if (rc < 0) + goto write_err; + + rc = fprintf_ascii(f, "MAXVAL %d\nTUPLTYPE %s", + palette ? 63 : 255, + palette ? "RGB" : "GRAYSCALE"); + if (rc < 0) + goto write_err; + + rc = fprintf_ascii(f, "_ALPHA\nENDHDR\n" + 6*!masked); + if (rc < 0) + goto write_err; + + for (offset = y = 0; y < height; y++, offset += width) { + switch (depth) { + case 4: + pam_format_rgba(rowbuf, pixels, mask, width, offset, palette); + break; + case 3: + pam_format_rgb(rowbuf, pixels, width, offset, palette); + break; + case 2: + pam_format_ga(rowbuf, pixels, mask, width, offset); + break; + case 1: + pam_format_g(rowbuf, pixels, width, offset); + break; } - } else { - depth = masked ? 2 : 1; - - if (fprintf_ascii(f, "DEPTH %zu\nMAXVAL 255\n" - "TUPLTYPE GRAYSCALE%s\nENDHDR\n", - depth, masked ? "_ALPHA" : "") < 0) - goto err; - for (x = y = 0; y < height; ++x < width || (x = 0, y++)) { - unsigned long offset = (unsigned long) y * width + x; - unsigned char buf[2]; - bool vis; - - vis = mask[offset/CHAR_BIT] & (1u << offset%CHAR_BIT); - buf[0] = pixels[offset]; - buf[1] = vis ? 255 : 0; - - if (fwrite(buf, 1, depth, f) < depth) - goto err; - } + if (fwrite(rowbuf, depth, width, f) < width) + goto write_err; } + free(rowbuf); return 0; -err: +write_err: tool_err(0, "error writing %s", filename); + free(rowbuf); return -1; }