cwebp: add PNG metadata extraction

the values of XMP/EXIF/ICC are currently unused.

Change-Id: I39d78b9a179f2d36c9c8ea12776bfdf6d8d18663
This commit is contained in:
James Zern 2012-12-04 19:00:30 -08:00
parent 6bf208748c
commit dba64d91bb
3 changed files with 184 additions and 9 deletions

View File

@ -327,7 +327,7 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic,
// If no size specified, try to decode it as PNG/JPEG (as appropriate). // If no size specified, try to decode it as PNG/JPEG (as appropriate).
const InputFileFormat format = GetImageType(in_file); const InputFileFormat format = GetImageType(in_file);
if (format == PNG_) { if (format == PNG_) {
ok = ReadPNG(in_file, pic, keep_alpha); ok = ReadPNG(in_file, pic, keep_alpha, metadata);
} else if (format == JPEG_) { } else if (format == JPEG_) {
ok = ReadJPEG(in_file, pic); ok = ReadJPEG(in_file, pic);
} else if (format == TIFF_) { } else if (format == TIFF_) {

View File

@ -21,15 +21,173 @@
#include <stdlib.h> #include <stdlib.h>
#include "webp/encode.h" #include "webp/encode.h"
#include "./metadata.h"
static void PNGAPI error_function(png_structp png, png_const_charp dummy) { static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
(void)dummy; // remove variable-unused warning (void)dummy; // remove variable-unused warning
longjmp(png_jmpbuf(png), 1); longjmp(png_jmpbuf(png), 1);
} }
int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) { // Converts the NULL terminated 'hexstring' which contains 2-byte character
// representations of hex values to raw data.
// 'hexstring' may contain values consisting of [A-F][a-f][0-9] in pairs,
// e.g., 7af2..., separated by any number of newlines.
// 'expected_length' is the anticipated processed size.
// On success the raw buffer is returned with its length equivalent to
// 'expected_length'. NULL is returned if the processed length is less than
// 'expected_length' or any character aside from those above is encountered.
// The returned buffer must be freed by the caller.
static uint8_t* HexStringToBytes(const char* hexstring,
size_t expected_length) {
const char* src = hexstring;
size_t actual_length = 0;
uint8_t* const raw_data = (uint8_t*)malloc(expected_length);
uint8_t* dst;
if (raw_data == NULL) return NULL;
for (dst = raw_data; actual_length < expected_length && *src != '\0'; ++src) {
char* end;
char val[3];
if (*src == '\n') continue;
val[0] = *src++;
val[1] = *src;
val[2] = '\0';
*dst++ = (uint8_t)strtol(val, &end, 16);
if (end != val + 2) break;
++actual_length;
}
if (actual_length != expected_length) {
free(raw_data);
return NULL;
}
return raw_data;
}
static int ProcessRawProfile(const char* profile, size_t profile_len,
MetadataPayload* const payload) {
const char* src = profile;
char* end;
int expected_length;
if (profile == NULL || profile_len == 0) return 0;
// ImageMagick formats 'raw profiles' as
// '\n<name>\n<length>(%8lu)\n<hex payload>\n'.
if (*src != '\n') {
fprintf(stderr, "Malformed raw profile, expected '\\n' got '\\x%.2X'\n",
*src);
return 0;
}
++src;
// skip the profile name and extract the length.
while (*src != '\0' && *src++ != '\n') {}
expected_length = (int)strtol(src, &end, 10);
if (*end != '\n') {
fprintf(stderr, "Malformed raw profile, expected '\\n' got '\\x%.2X'\n",
*end);
return 0;
}
++end;
// 'end' now points to the profile payload.
payload->bytes = HexStringToBytes(end, expected_length);
if (payload->bytes == NULL) return 0;
payload->size = expected_length;
return 1;
}
static const struct {
const char* name;
int (*process)(const char* profile, size_t profile_len,
MetadataPayload* const payload);
size_t storage_offset;
} kPNGMetadataMap[] = {
// http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/PNG.html#TextualData
// See also: ExifTool on CPAN.
{ "Raw profile type exif", ProcessRawProfile, METADATA_OFFSET(exif) },
{ "Raw profile type xmp", ProcessRawProfile, METADATA_OFFSET(xmp) },
// XMP Specification Part 3, Section 3 #PNG
{ "XML:com.adobe.xmp", MetadataCopy, METADATA_OFFSET(xmp) },
{ NULL, NULL, 0 },
};
// Looks for metadata at both the beginning and end of the PNG file, giving
// preference to the head.
// Returns true on success. The caller must use MetadataFree() on 'metadata' in
// all cases.
static int ExtractMetadataFromPNG(png_structp png,
png_infop const head_info,
png_infop const end_info,
Metadata* const metadata) {
int p;
for (p = 0; p < 2; ++p) {
png_infop const info = (p == 0) ? head_info : end_info;
png_textp text = NULL;
const int num = png_get_text(png, info, &text, NULL);
int i;
// Look for EXIF / XMP metadata.
for (i = 0; i < num; ++i, ++text) {
int j;
for (j = 0; kPNGMetadataMap[j].name != NULL; ++j) {
if (!strcmp(text->key, kPNGMetadataMap[j].name)) {
MetadataPayload* const payload =
(MetadataPayload*)((uint8_t*)metadata +
kPNGMetadataMap[j].storage_offset);
png_size_t text_length;
switch (text->compression) {
#ifdef PNG_iTXt_SUPPORTED
case PNG_ITXT_COMPRESSION_NONE:
case PNG_ITXT_COMPRESSION_zTXt:
text_length = text->itxt_length;
break;
#endif
default:
case PNG_TEXT_COMPRESSION_NONE:
case PNG_TEXT_COMPRESSION_zTXt:
text_length = text->text_length;
break;
}
if (payload->bytes != NULL) {
fprintf(stderr, "Ignoring additional '%s'\n", text->key);
} else if (!kPNGMetadataMap[j].process(text->text, text_length,
payload)) {
fprintf(stderr, "Failed to process: '%s'\n", text->key);
return 0;
}
break;
}
}
}
// Look for an ICC profile.
{
png_charp name;
int comp_type;
#if ((PNG_LIBPNG_VER_MAJOR << 8) | PNG_LIBPNG_VER_MINOR << 0) < \
((1 << 8) | (5 << 0))
png_charp profile;
#else // >= libpng 1.5.0
png_bytep profile;
#endif
png_uint_32 len;
if (png_get_iCCP(png, info,
&name, &comp_type, &profile, &len) == PNG_INFO_iCCP) {
if (!MetadataCopy((const char*)profile, len, &metadata->iccp)) return 0;
}
}
}
return 1;
}
int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha,
Metadata* const metadata) {
png_structp png; png_structp png;
png_infop info; png_infop info = NULL;
png_infop end_info = NULL;
int color_type, bit_depth, interlaced; int color_type, bit_depth, interlaced;
int has_alpha; int has_alpha;
int num_passes; int num_passes;
@ -47,13 +205,16 @@ int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
png_set_error_fn(png, 0, error_function, NULL); png_set_error_fn(png, 0, error_function, NULL);
if (setjmp(png_jmpbuf(png))) { if (setjmp(png_jmpbuf(png))) {
Error: Error:
png_destroy_read_struct(&png, NULL, NULL); MetadataFree(metadata);
png_destroy_read_struct(&png, &info, &end_info);
free(rgb); free(rgb);
goto End; goto End;
} }
info = png_create_info_struct(png); info = png_create_info_struct(png);
if (info == NULL) goto Error; if (info == NULL) goto Error;
end_info = png_create_info_struct(png);
if (end_info == NULL) goto Error;
png_init_io(png, in_file); png_init_io(png, in_file);
png_read_info(png, info); png_read_info(png, info);
@ -94,8 +255,15 @@ int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
png_read_rows(png, &row, NULL, 1); png_read_rows(png, &row, NULL, 1);
} }
} }
png_read_end(png, info); png_read_end(png, end_info);
png_destroy_read_struct(&png, &info, NULL);
if (metadata != NULL &&
!ExtractMetadataFromPNG(png, info, end_info, metadata)) {
fprintf(stderr, "Error extracting PNG metadata!\n");
goto Error;
}
png_destroy_read_struct(&png, &info, &end_info);
pic->width = width; pic->width = width;
pic->height = height; pic->height = height;
@ -103,7 +271,10 @@ int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
: WebPPictureImportRGB(pic, rgb, stride); : WebPPictureImportRGB(pic, rgb, stride);
free(rgb); free(rgb);
if (ok && has_alpha && keep_alpha == 2) { if (!ok) {
goto Error;
}
if (has_alpha && keep_alpha == 2) {
WebPCleanupTransparentArea(pic); WebPCleanupTransparentArea(pic);
} }
@ -111,10 +282,12 @@ int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
return ok; return ok;
} }
#else // !WEBP_HAVE_PNG #else // !WEBP_HAVE_PNG
int ReadPNG(FILE* in_file, struct WebPPicture* const pic, int keep_alpha) { int ReadPNG(FILE* in_file, struct WebPPicture* const pic, int keep_alpha,
struct Metadata* const metadata) {
(void)in_file; (void)in_file;
(void)pic; (void)pic;
(void)keep_alpha; (void)keep_alpha;
(void)metadata;
fprintf(stderr, "PNG support not compiled. Please install the libpng " fprintf(stderr, "PNG support not compiled. Please install the libpng "
"development package before building.\n"); "development package before building.\n");
return 0; return 0;

View File

@ -16,13 +16,15 @@
extern "C" { extern "C" {
#endif #endif
struct Metadata;
struct WebPPicture; struct WebPPicture;
// Reads a PNG from 'in_file', returning the decoded output in 'pic'. // Reads a PNG from 'in_file', returning the decoded output in 'pic'.
// If 'keep_alpha' is true and the PNG has an alpha channel, the output is RGBA // If 'keep_alpha' is true and the PNG has an alpha channel, the output is RGBA
// otherwise it will be RGB. // otherwise it will be RGB.
// Returns true on success. // Returns true on success.
int ReadPNG(FILE* in_file, struct WebPPicture* const pic, int keep_alpha); int ReadPNG(FILE* in_file, struct WebPPicture* const pic, int keep_alpha,
struct Metadata* const metadata);
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
} // extern "C" } // extern "C"