mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-12 22:14:29 +02:00
EXPERIMENTAL: add support for alpha channel
This is a (minor) bitstream change: if the 'color_space' bit is set to '1' (which is normally an undefined/invalid behaviour), we add extra data at the end of partition #0 (so-called 'extensions') Namely, we add the size of the extension data as 3 bytes (little-endian), followed by a set of bits telling which extensions we're incorporating. The data then _preceeds_ this trailing tags. This is all experimental, and you'll need to have '#define WEBP_EXPERIMENTAL_FEATURES' in webp/types.h to enable this code (at your own risk! :)) Still, this hack produces almost-valid WebP file for decoders that don't check this color_space bit. In particular, previous 'dwebp' (and for instance Chrome) will recognize this files and decode them, but without the alpha of course. Other decoder will just see random extra stuff at the end of partition #0. To experiment with the alpha-channel, you need to compile on Unix platform and use PNGs for input/output. If 'alpha.png' is a source with alpha channel, then you can try (on Unix): cwebp alpha.png -o alpha.webp dwebp alpha.webp -o test.png cwebp now has a '-noalpha' flag to ignore any alpha information from the source, if present. More hacking and experimenting welcome! Change-Id: I3c7b1fd8411c9e7a9f77690e898479ad85c52f3e
This commit is contained in:
@ -4,8 +4,8 @@ bin_PROGRAMS = dwebp cwebp
|
||||
|
||||
dwebp_SOURCES = dwebp.c stopwatch.h
|
||||
dwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES) $(JPEG_INCLUDES)
|
||||
dwebp_LDADD = ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS)
|
||||
dwebp_LDADD = ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS) -lz
|
||||
|
||||
cwebp_SOURCES = cwebp.c stopwatch.h
|
||||
cwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES) $(JPEG_INCLUDES)
|
||||
cwebp_LDADD = ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS)
|
||||
cwebp_LDADD = ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS) -lz
|
||||
|
@ -162,7 +162,8 @@ static HRESULT ReadPictureWithWIC(const char* filename,
|
||||
return hr;
|
||||
}
|
||||
|
||||
static int ReadPicture(const char* const filename, WebPPicture* const pic) {
|
||||
static int ReadPicture(const char* const filename, WebPPicture* const pic,
|
||||
int keep_alpha) {
|
||||
int ok;
|
||||
if (pic->width != 0 && pic->height != 0) {
|
||||
// If image size is specified, infer it as YUV format.
|
||||
@ -282,10 +283,11 @@ static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
|
||||
longjmp(png_jmpbuf(png), 1);
|
||||
}
|
||||
|
||||
static int ReadPNG(FILE* in_file, WebPPicture* const pic) {
|
||||
static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
|
||||
png_structp png;
|
||||
png_infop info;
|
||||
int color_type, bit_depth, interlaced;
|
||||
int has_alpha;
|
||||
int num_passes;
|
||||
int p;
|
||||
int ok = 0;
|
||||
@ -327,12 +329,16 @@ static int ReadPNG(FILE* in_file, WebPPicture* const pic) {
|
||||
if (png_get_valid(png, info, PNG_INFO_tRNS)) {
|
||||
png_set_tRNS_to_alpha(png);
|
||||
}
|
||||
has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
|
||||
|
||||
if (!keep_alpha) {
|
||||
png_set_strip_alpha(png);
|
||||
has_alpha = 0;
|
||||
}
|
||||
|
||||
// TODO(skal): Strip Alpha for now (till Alpha is supported).
|
||||
png_set_strip_alpha(png);
|
||||
num_passes = png_set_interlace_handling(png);
|
||||
png_read_update_info(png, info);
|
||||
stride = 3 * width * sizeof(*rgb);
|
||||
stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
|
||||
rgb = (uint8_t*)malloc(stride * height);
|
||||
if (rgb == NULL) goto Error;
|
||||
for (p = 0; p < num_passes; ++p) {
|
||||
@ -346,14 +352,15 @@ static int ReadPNG(FILE* in_file, WebPPicture* const pic) {
|
||||
|
||||
pic->width = width;
|
||||
pic->height = height;
|
||||
ok = WebPPictureImportRGB(pic, rgb, stride);
|
||||
ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
|
||||
: WebPPictureImportRGB(pic, rgb, stride);
|
||||
free(rgb);
|
||||
|
||||
End:
|
||||
return ok;
|
||||
}
|
||||
#else
|
||||
static int ReadPNG(FILE* in_file, WebPPicture* const pic) {
|
||||
static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
|
||||
printf("PNG support not compiled. Please install the libpng development "
|
||||
"package before building.\n");
|
||||
return 0;
|
||||
@ -385,7 +392,8 @@ static InputFileFormat GetImageType(FILE* in_file) {
|
||||
return format;
|
||||
}
|
||||
|
||||
static int ReadPicture(const char* const filename, WebPPicture* const pic) {
|
||||
static int ReadPicture(const char* const filename, WebPPicture* const pic,
|
||||
int keep_alpha) {
|
||||
int ok = 0;
|
||||
FILE* in_file = fopen(filename, "rb");
|
||||
if (in_file == NULL) {
|
||||
@ -397,7 +405,7 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic) {
|
||||
// If no size specified, try to decode it as PNG/JPEG (as appropriate).
|
||||
const InputFileFormat format = GetImageType(in_file);
|
||||
if (format == PNG) {
|
||||
ok = ReadPNG(in_file, pic);
|
||||
ok = ReadPNG(in_file, pic, keep_alpha);
|
||||
} else if (format == JPEG) {
|
||||
ok = ReadJPEG(in_file, pic);
|
||||
}
|
||||
@ -475,6 +483,10 @@ static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
|
||||
100.f * stats->header_bytes[0] / stats->coded_size,
|
||||
stats->header_bytes[1],
|
||||
100.f * stats->header_bytes[1] / stats->coded_size);
|
||||
if (stats->alpha_data_size) {
|
||||
fprintf(stderr, " transparency: %6d\n",
|
||||
stats->alpha_data_size);
|
||||
}
|
||||
fprintf(stderr, " Residuals bytes "
|
||||
"|segment 1|segment 2|segment 3"
|
||||
"|segment 4| total\n");
|
||||
@ -590,6 +602,8 @@ static void HelpLong(void) {
|
||||
printf(" -sharpness <int> ....... "
|
||||
"filter sharpness (0:most .. 7:least sharp)\n");
|
||||
printf(" -strong ................ use strong filter instead of simple.\n");
|
||||
printf(" -alpha_comp <int> ...... set the transparency-compression\n");
|
||||
printf(" -noalpha ............... discard any transparency information.\n");
|
||||
printf(" -pass <int> ............ analysis pass number (1..10)\n");
|
||||
printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
|
||||
printf(" -map <int> ............. print map of extra info.\n");
|
||||
@ -618,12 +632,15 @@ int main(int argc, const char *argv[]) {
|
||||
int c;
|
||||
int short_output = 0;
|
||||
int quiet = 0;
|
||||
int keep_alpha = 0;
|
||||
int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
|
||||
WebPPicture picture;
|
||||
WebPConfig config;
|
||||
WebPAuxStats stats;
|
||||
Stopwatch stop_watch;
|
||||
|
||||
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
||||
keep_alpha = 1;
|
||||
#endif
|
||||
if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
|
||||
fprintf(stderr, "Error! Version mismatch!\n");
|
||||
goto Error;
|
||||
@ -675,6 +692,10 @@ int main(int argc, const char *argv[]) {
|
||||
config.preprocessing = strtol(argv[++c], NULL, 0);
|
||||
} else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
|
||||
config.segments = strtol(argv[++c], NULL, 0);
|
||||
} else if (!strcmp(argv[c], "-alpha_comp") && c < argc - 1) {
|
||||
config.alpha_compression = strtol(argv[++c], NULL, 0);
|
||||
} else if (!strcmp(argv[c], "-noalpha")) {
|
||||
keep_alpha = 0;
|
||||
} else if (!strcmp(argv[c], "-map") && c < argc - 1) {
|
||||
picture.extra_info_type = strtol(argv[++c], NULL, 0);
|
||||
} else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
|
||||
@ -734,7 +755,7 @@ int main(int argc, const char *argv[]) {
|
||||
// Read the input
|
||||
if (verbose)
|
||||
StopwatchReadAndReset(&stop_watch);
|
||||
if (!ReadPicture(in_file, &picture)) {
|
||||
if (!ReadPicture(in_file, &picture, keep_alpha)) {
|
||||
fprintf(stderr, "Error! Cannot read input picture\n");
|
||||
goto Error;
|
||||
}
|
||||
|
@ -109,7 +109,8 @@ static HRESULT WriteUsingWIC(const char* out_file_name, REFGUID container_guid,
|
||||
}
|
||||
|
||||
static int WritePNG(const char* out_file_name, unsigned char* rgb, int stride,
|
||||
uint32_t width, uint32_t height) {
|
||||
uint32_t width, uint32_t height, int has_alpha) {
|
||||
assert(!has_alpha); // TODO(mikolaj)
|
||||
return SUCCEEDED(WriteUsingWIC(out_file_name,
|
||||
MAKE_REFGUID(GUID_ContainerFormatPng), rgb, stride, width,
|
||||
height));
|
||||
@ -122,7 +123,7 @@ static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
|
||||
}
|
||||
|
||||
static int WritePNG(FILE* out_file, unsigned char* rgb, int stride,
|
||||
png_uint_32 width, png_uint_32 height) {
|
||||
png_uint_32 width, png_uint_32 height, int has_alpha) {
|
||||
png_structp png;
|
||||
png_infop info;
|
||||
png_uint_32 y;
|
||||
@ -142,7 +143,8 @@ static int WritePNG(FILE* out_file, unsigned char* rgb, int stride,
|
||||
return 0;
|
||||
}
|
||||
png_init_io(png, out_file);
|
||||
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
|
||||
png_set_IHDR(png, info, width, height, 8,
|
||||
has_alpha ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB,
|
||||
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
|
||||
PNG_FILTER_TYPE_DEFAULT);
|
||||
png_write_info(png, info);
|
||||
@ -159,7 +161,7 @@ static int WritePNG(FILE* out_file, unsigned char* rgb, int stride,
|
||||
typedef uint32_t png_uint_32;
|
||||
|
||||
static int WritePNG(FILE* out_file, unsigned char* rgb, int stride,
|
||||
png_uint_32 width, png_uint_32 height) {
|
||||
png_uint_32 width, png_uint_32 height, int has_alpha) {
|
||||
printf("PNG support not compiled. Please install the libpng development "
|
||||
"package before building.\n");
|
||||
printf("You can run with -ppm flag to decode in PPM format.\n");
|
||||
@ -167,12 +169,28 @@ static int WritePNG(FILE* out_file, unsigned char* rgb, int stride,
|
||||
}
|
||||
#endif
|
||||
|
||||
static int WritePPM(FILE* fout, unsigned char* rgb,
|
||||
static int WritePPM(FILE* fout, const unsigned char* rgb,
|
||||
uint32_t width, uint32_t height) {
|
||||
fprintf(fout, "P6\n%d %d\n255\n", width, height);
|
||||
return (fwrite(rgb, width * height, 3, fout) == 3);
|
||||
}
|
||||
|
||||
static int WriteAlphaPlane(FILE* fout, const unsigned char* rgba,
|
||||
uint32_t width, uint32_t height) {
|
||||
uint32_t y;
|
||||
fprintf(fout, "P5\n%d %d\n255\n", width, height);
|
||||
for (y = 0; y < height; ++y) {
|
||||
const unsigned char* line = rgba + y * (width * 4);
|
||||
uint32_t x;
|
||||
for (x = 0; x < width; ++x) {
|
||||
if (fputc(line[4 * x + 3], fout) == EOF) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int WritePGM(FILE* fout,
|
||||
unsigned char* y_plane, unsigned char *u, unsigned char* v,
|
||||
int y_stride, int uv_stride,
|
||||
@ -202,6 +220,7 @@ typedef enum {
|
||||
PNG = 0,
|
||||
PPM,
|
||||
PGM,
|
||||
ALPHA_PLANE_ONLY // this is for experimenting only
|
||||
} OutputFileFormat;
|
||||
|
||||
static void Help(void) {
|
||||
@ -222,6 +241,7 @@ int main(int argc, const char *argv[]) {
|
||||
const char *out_file = NULL;
|
||||
|
||||
int width, height, stride, uv_stride;
|
||||
int has_alpha = 0;
|
||||
uint8_t* out = NULL, *u = NULL, *v = NULL;
|
||||
OutputFileFormat format = PNG;
|
||||
Stopwatch stop_watch;
|
||||
@ -232,6 +252,8 @@ int main(int argc, const char *argv[]) {
|
||||
return 0;
|
||||
} else if (!strcmp(argv[c], "-o") && c < argc - 1) {
|
||||
out_file = argv[++c];
|
||||
} else if (!strcmp(argv[c], "-alpha")) {
|
||||
format = ALPHA_PLANE_ONLY;
|
||||
} else if (!strcmp(argv[c], "-ppm")) {
|
||||
format = PPM;
|
||||
} else if (!strcmp(argv[c], "-version")) {
|
||||
@ -284,8 +306,12 @@ int main(int argc, const char *argv[]) {
|
||||
case PNG:
|
||||
#ifdef _WIN32
|
||||
out = WebPDecodeBGR((const uint8_t*)data, data_size, &width, &height);
|
||||
stride = 3 * width;
|
||||
has_alpha = 0;
|
||||
#else
|
||||
out = WebPDecodeRGB((const uint8_t*)data, data_size, &width, &height);
|
||||
out = WebPDecodeRGBA((const uint8_t*)data, data_size, &width, &height);
|
||||
stride = 4 * width;
|
||||
has_alpha = 1;
|
||||
#endif
|
||||
break;
|
||||
case PPM:
|
||||
@ -295,6 +321,9 @@ int main(int argc, const char *argv[]) {
|
||||
out = WebPDecodeYUV((const uint8_t*)data, data_size, &width, &height,
|
||||
&u, &v, &stride, &uv_stride);
|
||||
break;
|
||||
case ALPHA_PLANE_ONLY:
|
||||
out = WebPDecodeRGBA((const uint8_t*)data, data_size, &width, &height);
|
||||
break;
|
||||
default:
|
||||
free(data);
|
||||
return -1;
|
||||
@ -331,14 +360,16 @@ int main(int argc, const char *argv[]) {
|
||||
int ok = 1;
|
||||
if (format == PNG) {
|
||||
#ifdef _WIN32
|
||||
ok &= WritePNG(out_file, out, 3 * width, width, height);
|
||||
ok &= WritePNG(out_file, out, stride, width, height, has_alpha);
|
||||
#else
|
||||
ok &= WritePNG(fout, out, 3 * width, width, height);
|
||||
ok &= WritePNG(fout, out, stride, width, height, has_alpha);
|
||||
#endif
|
||||
} else if (format == PPM) {
|
||||
ok &= WritePPM(fout, out, width, height);
|
||||
} else if (format == PGM) {
|
||||
ok &= WritePGM(fout, out, u, v, stride, uv_stride, width, height);
|
||||
} else if (format == ALPHA_PLANE_ONLY) {
|
||||
ok &= WriteAlphaPlane(fout, out, width, height);
|
||||
}
|
||||
if (fout)
|
||||
fclose(fout);
|
||||
|
Reference in New Issue
Block a user