Increase control over output metadata (#3856)

Add withX and keepX functions to take advantage of
libvips 8.15.0 new 'keep' metadata feature.
This commit is contained in:
Lovell Fuller
2023-11-22 09:03:57 +00:00
committed by GitHub
parent 3f7313d031
commit e78200cc84
13 changed files with 694 additions and 179 deletions

View File

@@ -531,7 +531,33 @@ namespace sharp {
Does this image have an embedded profile?
*/
bool HasProfile(VImage image) {
return (image.get_typeof(VIPS_META_ICC_NAME) != 0) ? TRUE : FALSE;
return image.get_typeof(VIPS_META_ICC_NAME) == VIPS_TYPE_BLOB;
}
/*
Get copy of embedded profile.
*/
std::pair<char*, size_t> GetProfile(VImage image) {
std::pair<char*, size_t> icc(nullptr, 0);
if (HasProfile(image)) {
size_t length;
const void *data = image.get_blob(VIPS_META_ICC_NAME, &length);
icc.first = static_cast<char*>(g_malloc(length));
icc.second = length;
memcpy(icc.first, data, length);
}
return icc;
}
/*
Set embedded profile.
*/
VImage SetProfile(VImage image, std::pair<char*, size_t> icc) {
if (icc.first != nullptr) {
image = image.copy();
image.set(VIPS_META_ICC_NAME, reinterpret_cast<VipsCallbackFn>(vips_area_free_cb), icc.first, icc.second);
}
return image;
}
/*
@@ -542,6 +568,27 @@ namespace sharp {
return image.has_alpha();
}
static void* RemoveExifCallback(VipsImage *image, char const *field, GValue *value, void *data) {
std::vector<std::string> *fieldNames = static_cast<std::vector<std::string> *>(data);
std::string fieldName(field);
if (fieldName.substr(0, 8) == ("exif-ifd")) {
fieldNames->push_back(fieldName);
}
return nullptr;
}
/*
Remove all EXIF-related image fields.
*/
VImage RemoveExif(VImage image) {
std::vector<std::string> fieldNames;
vips_image_map(image.get_image(), static_cast<VipsImageMapFn>(RemoveExifCallback), &fieldNames);
for (const auto& f : fieldNames) {
image.remove(f.data());
}
return image;
}
/*
Get EXIF Orientation of image, if any.
*/

View File

@@ -222,12 +222,27 @@ namespace sharp {
*/
bool HasProfile(VImage image);
/*
Get copy of embedded profile.
*/
std::pair<char*, size_t> GetProfile(VImage image);
/*
Set embedded profile.
*/
VImage SetProfile(VImage image, std::pair<char*, size_t> icc);
/*
Does this image have an alpha channel?
Uses colour space interpretation with number of channels to guess this.
*/
bool HasAlpha(VImage image);
/*
Remove all EXIF-related image fields.
*/
VImage RemoveExif(VImage image);
/*
Get EXIF Orientation of image, if any.
*/

View File

@@ -315,6 +315,11 @@ class PipelineWorker : public Napi::AsyncWorker {
}
// Ensure we're using a device-independent colour space
std::pair<char*, size_t> inputProfile(nullptr, 0);
if ((baton->keepMetadata & VIPS_FOREIGN_KEEP_ICC) && baton->withIccProfile.empty()) {
// Cache input profile for use with output
inputProfile = sharp::GetProfile(image);
}
char const *processingProfile = image.interpretation() == VIPS_INTERPRETATION_RGB16 ? "p3" : "srgb";
if (
sharp::HasProfile(image) &&
@@ -758,7 +763,8 @@ class PipelineWorker : public Napi::AsyncWorker {
// Convert colourspace, pass the current known interpretation so libvips doesn't have to guess
image = image.colourspace(baton->colourspace, VImage::option()->set("source_space", image.interpretation()));
// Transform colours from embedded profile to output profile
if (baton->withMetadata && sharp::HasProfile(image) && baton->withMetadataIcc.empty()) {
if ((baton->keepMetadata & VIPS_FOREIGN_KEEP_ICC) &&
baton->withIccProfile.empty() && sharp::HasProfile(image)) {
image = image.icc_transform("srgb", VImage::option()
->set("embedded", TRUE)
->set("depth", sharp::Is16Bit(image.interpretation()) ? 16 : 8)
@@ -787,27 +793,30 @@ class PipelineWorker : public Napi::AsyncWorker {
}
// Apply output ICC profile
if (baton->withMetadata) {
image = image.icc_transform(
baton->withMetadataIcc.empty() ? "srgb" : const_cast<char*>(baton->withMetadataIcc.data()),
VImage::option()
->set("input_profile", processingProfile)
->set("embedded", TRUE)
->set("depth", sharp::Is16Bit(image.interpretation()) ? 16 : 8)
->set("intent", VIPS_INTENT_PERCEPTUAL));
if (!baton->withIccProfile.empty()) {
image = image.icc_transform(const_cast<char*>(baton->withIccProfile.data()), VImage::option()
->set("input_profile", processingProfile)
->set("embedded", TRUE)
->set("depth", sharp::Is16Bit(image.interpretation()) ? 16 : 8)
->set("intent", VIPS_INTENT_PERCEPTUAL));
} else if (baton->keepMetadata & VIPS_FOREIGN_KEEP_ICC) {
image = sharp::SetProfile(image, inputProfile);
}
// Override EXIF Orientation tag
if (baton->withMetadata && baton->withMetadataOrientation != -1) {
if (baton->withMetadataOrientation != -1) {
image = sharp::SetExifOrientation(image, baton->withMetadataOrientation);
}
// Override pixel density
if (baton->withMetadataDensity > 0) {
image = sharp::SetDensity(image, baton->withMetadataDensity);
}
// Metadata key/value pairs, e.g. EXIF
if (!baton->withMetadataStrs.empty()) {
// EXIF key/value pairs
if (baton->keepMetadata & VIPS_FOREIGN_KEEP_EXIF) {
image = image.copy();
for (const auto& s : baton->withMetadataStrs) {
if (!baton->withExifMerge) {
image = sharp::RemoveExif(image);
}
for (const auto& s : baton->withExif) {
image.set(s.first.data(), s.second.data());
}
}
@@ -828,7 +837,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write JPEG to buffer
sharp::AssertImageTypeDimensions(image, sharp::ImageType::JPEG);
VipsArea *area = reinterpret_cast<VipsArea*>(image.jpegsave_buffer(VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("Q", baton->jpegQuality)
->set("interlace", baton->jpegProgressive)
->set("subsample_mode", baton->jpegChromaSubsampling == "4:4:4"
@@ -870,7 +879,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write PNG to buffer
sharp::AssertImageTypeDimensions(image, sharp::ImageType::PNG);
VipsArea *area = reinterpret_cast<VipsArea*>(image.pngsave_buffer(VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("interlace", baton->pngProgressive)
->set("compression", baton->pngCompressionLevel)
->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE)
@@ -889,7 +898,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write WEBP to buffer
sharp::AssertImageTypeDimensions(image, sharp::ImageType::WEBP);
VipsArea *area = reinterpret_cast<VipsArea*>(image.webpsave_buffer(VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("Q", baton->webpQuality)
->set("lossless", baton->webpLossless)
->set("near_lossless", baton->webpNearLossless)
@@ -909,7 +918,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write GIF to buffer
sharp::AssertImageTypeDimensions(image, sharp::ImageType::GIF);
VipsArea *area = reinterpret_cast<VipsArea*>(image.gifsave_buffer(VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("bitdepth", baton->gifBitdepth)
->set("effort", baton->gifEffort)
->set("reuse", baton->gifReuse)
@@ -934,7 +943,7 @@ class PipelineWorker : public Napi::AsyncWorker {
image = image.cast(VIPS_FORMAT_FLOAT);
}
VipsArea *area = reinterpret_cast<VipsArea*>(image.tiffsave_buffer(VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("Q", baton->tiffQuality)
->set("bitdepth", baton->tiffBitdepth)
->set("compression", baton->tiffCompression)
@@ -958,7 +967,7 @@ class PipelineWorker : public Napi::AsyncWorker {
sharp::AssertImageTypeDimensions(image, sharp::ImageType::HEIF);
image = sharp::RemoveAnimationProperties(image).cast(VIPS_FORMAT_UCHAR);
VipsArea *area = reinterpret_cast<VipsArea*>(image.heifsave_buffer(VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("Q", baton->heifQuality)
->set("compression", baton->heifCompression)
->set("effort", baton->heifEffort)
@@ -990,7 +999,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write JXL to buffer
image = sharp::RemoveAnimationProperties(image);
VipsArea *area = reinterpret_cast<VipsArea*>(image.jxlsave_buffer(VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("distance", baton->jxlDistance)
->set("tier", baton->jxlDecodingTier)
->set("effort", baton->jxlEffort)
@@ -1051,7 +1060,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write JPEG to file
sharp::AssertImageTypeDimensions(image, sharp::ImageType::JPEG);
image.jpegsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("Q", baton->jpegQuality)
->set("interlace", baton->jpegProgressive)
->set("subsample_mode", baton->jpegChromaSubsampling == "4:4:4"
@@ -1081,7 +1090,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write PNG to file
sharp::AssertImageTypeDimensions(image, sharp::ImageType::PNG);
image.pngsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("interlace", baton->pngProgressive)
->set("compression", baton->pngCompressionLevel)
->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE)
@@ -1096,7 +1105,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write WEBP to file
sharp::AssertImageTypeDimensions(image, sharp::ImageType::WEBP);
image.webpsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("Q", baton->webpQuality)
->set("lossless", baton->webpLossless)
->set("near_lossless", baton->webpNearLossless)
@@ -1112,7 +1121,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write GIF to file
sharp::AssertImageTypeDimensions(image, sharp::ImageType::GIF);
image.gifsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("bitdepth", baton->gifBitdepth)
->set("effort", baton->gifEffort)
->set("reuse", baton->gifReuse)
@@ -1131,7 +1140,7 @@ class PipelineWorker : public Napi::AsyncWorker {
image = image.cast(VIPS_FORMAT_FLOAT);
}
image.tiffsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("Q", baton->tiffQuality)
->set("bitdepth", baton->tiffBitdepth)
->set("compression", baton->tiffCompression)
@@ -1151,7 +1160,7 @@ class PipelineWorker : public Napi::AsyncWorker {
sharp::AssertImageTypeDimensions(image, sharp::ImageType::HEIF);
image = sharp::RemoveAnimationProperties(image).cast(VIPS_FORMAT_UCHAR);
image.heifsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("Q", baton->heifQuality)
->set("compression", baton->heifCompression)
->set("effort", baton->heifEffort)
@@ -1165,7 +1174,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Write JXL to file
image = sharp::RemoveAnimationProperties(image);
image.jxlsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("distance", baton->jxlDistance)
->set("tier", baton->jxlDecodingTier)
->set("effort", baton->jxlEffort)
@@ -1187,7 +1196,7 @@ class PipelineWorker : public Napi::AsyncWorker {
(willMatchInput && inputImageType == sharp::ImageType::VIPS)) {
// Write V to file
image.vipssave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata));
->set("keep", baton->keepMetadata));
baton->formatOut = "v";
} else {
// Unsupported output format
@@ -1401,7 +1410,7 @@ class PipelineWorker : public Napi::AsyncWorker {
suffix = AssembleSuffixString(extname, options);
}
vips::VOption *options = VImage::option()
->set("strip", !baton->withMetadata)
->set("keep", baton->keepMetadata)
->set("tile_size", baton->tileSize)
->set("overlap", baton->tileOverlap)
->set("container", baton->tileContainer)
@@ -1593,18 +1602,19 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
// Output
baton->formatOut = sharp::AttrAsStr(options, "formatOut");
baton->fileOut = sharp::AttrAsStr(options, "fileOut");
baton->withMetadata = sharp::AttrAsBool(options, "withMetadata");
baton->keepMetadata = sharp::AttrAsUint32(options, "keepMetadata");
baton->withMetadataOrientation = sharp::AttrAsUint32(options, "withMetadataOrientation");
baton->withMetadataDensity = sharp::AttrAsDouble(options, "withMetadataDensity");
baton->withMetadataIcc = sharp::AttrAsStr(options, "withMetadataIcc");
Napi::Object mdStrs = options.Get("withMetadataStrs").As<Napi::Object>();
Napi::Array mdStrKeys = mdStrs.GetPropertyNames();
for (unsigned int i = 0; i < mdStrKeys.Length(); i++) {
std::string k = sharp::AttrAsStr(mdStrKeys, i);
if (mdStrs.HasOwnProperty(k)) {
baton->withMetadataStrs.insert(std::make_pair(k, sharp::AttrAsStr(mdStrs, k)));
baton->withIccProfile = sharp::AttrAsStr(options, "withIccProfile");
Napi::Object withExif = options.Get("withExif").As<Napi::Object>();
Napi::Array withExifKeys = withExif.GetPropertyNames();
for (unsigned int i = 0; i < withExifKeys.Length(); i++) {
std::string k = sharp::AttrAsStr(withExifKeys, i);
if (withExif.HasOwnProperty(k)) {
baton->withExif.insert(std::make_pair(k, sharp::AttrAsStr(withExif, k)));
}
}
baton->withExifMerge = sharp::AttrAsBool(options, "withExifMerge");
baton->timeoutSeconds = sharp::AttrAsUint32(options, "timeoutSeconds");
// Format-specific
baton->jpegQuality = sharp::AttrAsUint32(options, "jpegQuality");

View File

@@ -187,11 +187,12 @@ struct PipelineBaton {
bool jxlLossless;
VipsBandFormat rawDepth;
std::string err;
bool withMetadata;
int keepMetadata;
int withMetadataOrientation;
double withMetadataDensity;
std::string withMetadataIcc;
std::unordered_map<std::string, std::string> withMetadataStrs;
std::string withIccProfile;
std::unordered_map<std::string, std::string> withExif;
bool withExifMerge;
int timeoutSeconds;
std::unique_ptr<double[]> convKernel;
int convKernelWidth;
@@ -353,9 +354,10 @@ struct PipelineBaton {
jxlEffort(7),
jxlLossless(false),
rawDepth(VIPS_FORMAT_UCHAR),
withMetadata(false),
keepMetadata(0),
withMetadataOrientation(-1),
withMetadataDensity(0.0),
withExifMerge(true),
timeoutSeconds(0),
convKernelWidth(0),
convKernelHeight(0),