Add support for bit depth with raw input and output (#2762)

* Determine input raw pixel depth from the given typed array
* Allow pixel depth to be set on raw output
This commit is contained in:
Mart
2021-08-03 15:52:54 +02:00
committed by GitHub
parent eabb671b10
commit b7add480c7
9 changed files with 123 additions and 16 deletions

View File

@@ -92,6 +92,9 @@ namespace sharp {
}
// Raw pixel input
if (HasAttr(input, "rawChannels")) {
descriptor->rawDepth = static_cast<VipsBandFormat>(
vips_enum_from_nick(nullptr, VIPS_TYPE_BAND_FORMAT,
AttrAsStr(input, "rawDepth").data()));
descriptor->rawChannels = AttrAsUint32(input, "rawChannels");
descriptor->rawWidth = AttrAsUint32(input, "rawWidth");
descriptor->rawHeight = AttrAsUint32(input, "rawHeight");
@@ -297,7 +300,7 @@ namespace sharp {
if (descriptor->rawChannels > 0) {
// Raw, uncompressed pixel data
image = VImage::new_from_memory(descriptor->buffer, descriptor->bufferLength,
descriptor->rawWidth, descriptor->rawHeight, descriptor->rawChannels, VIPS_FORMAT_UCHAR);
descriptor->rawWidth, descriptor->rawHeight, descriptor->rawChannels, descriptor->rawDepth);
if (descriptor->rawChannels < 3) {
image.get_image()->Type = VIPS_INTERPRETATION_B_W;
} else {

View File

@@ -54,6 +54,7 @@ namespace sharp {
size_t bufferLength;
bool isBuffer;
double density;
VipsBandFormat rawDepth;
int rawChannels;
int rawWidth;
int rawHeight;
@@ -78,6 +79,7 @@ namespace sharp {
bufferLength(0),
isBuffer(FALSE),
density(72.0),
rawDepth(VIPS_FORMAT_UCHAR),
rawChannels(0),
rawWidth(0),
rawHeight(0),

View File

@@ -889,9 +889,9 @@ class PipelineWorker : public Napi::AsyncWorker {
image = image[0];
baton->channels = 1;
}
if (image.format() != VIPS_FORMAT_UCHAR) {
// Cast pixels to uint8 (unsigned char)
image = image.cast(VIPS_FORMAT_UCHAR);
if (image.format() != baton->rawDepth) {
// Cast pixels to requested format
image = image.cast(baton->rawDepth);
}
// Get raw image data
baton->bufferOut = static_cast<char*>(image.write_to_memory(&baton->bufferOutLength));
@@ -1131,6 +1131,9 @@ class PipelineWorker : public Napi::AsyncWorker {
info.Set("width", static_cast<uint32_t>(width));
info.Set("height", static_cast<uint32_t>(height));
info.Set("channels", static_cast<uint32_t>(baton->channels));
if (baton->formatOut == "raw") {
info.Set("depth", vips_enum_nick(VIPS_TYPE_BAND_FORMAT, baton->rawDepth));
}
info.Set("premultiplied", baton->premultiplied);
if (baton->hasCropOffset) {
info.Set("cropOffsetLeft", static_cast<int32_t>(baton->cropOffsetLeft));
@@ -1457,6 +1460,11 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->heifSpeed = sharp::AttrAsUint32(options, "heifSpeed");
baton->heifChromaSubsampling = sharp::AttrAsStr(options, "heifChromaSubsampling");
// Raw output
baton->rawDepth = static_cast<VipsBandFormat>(
vips_enum_from_nick(nullptr, VIPS_TYPE_BAND_FORMAT,
sharp::AttrAsStr(options, "rawDepth").data()));
// Animated output
if (sharp::HasAttr(options, "pageHeight")) {
baton->pageHeight = sharp::AttrAsUint32(options, "pageHeight");

View File

@@ -169,6 +169,7 @@ struct PipelineBaton {
int heifSpeed;
std::string heifChromaSubsampling;
bool heifLossless;
VipsBandFormat rawDepth;
std::string err;
bool withMetadata;
int withMetadataOrientation;
@@ -298,6 +299,7 @@ struct PipelineBaton {
heifSpeed(5),
heifChromaSubsampling("4:4:4"),
heifLossless(false),
rawDepth(VIPS_FORMAT_UCHAR),
withMetadata(false),
withMetadataOrientation(-1),
withMetadataDensity(0.0),