diff --git a/examples/dwebp.c b/examples/dwebp.c index 619331d7..4846b8a3 100644 --- a/examples/dwebp.c +++ b/examples/dwebp.c @@ -59,6 +59,7 @@ extern void* VP8GetCPUInfo; // opaque forward declaration. // Output types typedef enum { PNG = 0, + PAM, PPM, PGM, ALPHA_PLANE_ONLY // this is for experimenting only @@ -201,15 +202,22 @@ static int WritePNG(FILE* out_file, const WebPDecBuffer* const buffer) { } #endif -static int WritePPM(FILE* fout, const WebPDecBuffer* const buffer) { +static int WritePPM(FILE* fout, const WebPDecBuffer* const buffer, int alpha) { const uint32_t width = buffer->width; const uint32_t height = buffer->height; const unsigned char* const rgb = buffer->u.RGBA.rgba; const int stride = buffer->u.RGBA.stride; + const size_t bytes_per_px = alpha ? 4 : 3; uint32_t y; - fprintf(fout, "P6\n%d %d\n255\n", width, height); + + if (alpha) { + fprintf(fout, "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\n" + "TUPLTYPE RGB_ALPHA\nENDHDR\n", width, height); + } else { + fprintf(fout, "P6\n%d %d\n255\n", width, height); + } for (y = 0; y < height; ++y) { - if (fwrite(rgb + y * stride, width, 3, fout) != 3) { + if (fwrite(rgb + y * stride, width, bytes_per_px, fout) != bytes_per_px) { return 0; } } @@ -289,8 +297,10 @@ static void SaveOutput(const WebPDecBuffer* const buffer, #else ok &= WritePNG(fout, buffer); #endif + } else if (format == PAM) { + ok &= WritePPM(fout, buffer, 1); } else if (format == PPM) { - ok &= WritePPM(fout, buffer); + ok &= WritePPM(fout, buffer, 0); } else if (format == PGM) { ok &= WritePGM(fout, buffer); } else if (format == ALPHA_PLANE_ONLY) { @@ -314,7 +324,8 @@ static void Help(void) { printf("Usage: dwebp in_file [options] [-o out_file]\n\n" "Decodes the WebP image file to PNG format [Default]\n" "Use following options to convert into alternate image formats:\n" - " -ppm ......... save the raw RGB samples as color PPM\n" + " -pam ......... save the raw RGBA samples as a color PAM\n" + " -ppm ......... save the raw RGB samples as a color PPM\n" " -pgm ......... save the raw YUV samples as a grayscale PGM\n" " file with IMC4 layout.\n" " Other options are:\n" @@ -367,6 +378,8 @@ int main(int argc, const char *argv[]) { config.options.no_fancy_upsampling = 1; } else if (!strcmp(argv[c], "-nofilter")) { config.options.bypass_filtering = 1; + } else if (!strcmp(argv[c], "-pam")) { + format = PAM; } else if (!strcmp(argv[c], "-ppm")) { format = PPM; } else if (!strcmp(argv[c], "-version")) { @@ -434,6 +447,9 @@ int main(int argc, const char *argv[]) { output_buffer->colorspace = bitstream->has_alpha ? MODE_RGBA : MODE_RGB; #endif break; + case PAM: + output_buffer->colorspace = MODE_RGBA; + break; case PPM: output_buffer->colorspace = MODE_RGB; // drops alpha for PPM break; diff --git a/man/dwebp.1 b/man/dwebp.1 index 637297f6..1e8b6c56 100644 --- a/man/dwebp.1 +++ b/man/dwebp.1 @@ -1,5 +1,5 @@ .\" Hey, EMACS: -*- nroff -*- -.TH DWEBP 1 "January 24, 2012" +.TH DWEBP 1 "July 20, 2012" .SH NAME dwebp \- decompress a WebP file to an image file .SH SYNOPSIS @@ -11,7 +11,7 @@ This manual page documents the .B dwebp command. .PP -\fBdwebp\fP decompresses WebP files into PNG, PPM or PGM images. +\fBdwebp\fP decompresses WebP files into PNG, PAM, PPM or PGM images. .SH OPTIONS The basic options are: .TP @@ -24,8 +24,11 @@ Print the version number (as major.minor.revision) and exit. .B \-o string Specify the name of the output file (as PNG format by default). .TP +.B \-pam +Change the output format to PAM (retains alpha). +.TP .B \-ppm -Change the output format to PPM. +Change the output format to PPM (discards alpha). .TP .B \-pgm Change the output format to PGM. The output consist of luma/chroma