template <typename NumeratorType> float FractionToFloat(NumeratorType numerator, uint32_t denominator) { // First cast to double and not float because uint32_t->float conversion can // cause precision loss. returnstatic_cast<double>(numerator) / denominator;
}
// CrabbyAvif needs a contiguous data buffer.
sk_sp<SkData> data = nullptr; if (stream->getMemoryBase()) { // It is safe to make without copy because we'll hold onto the stream.
data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength());
} else {
data = SkCopyStreamToData(stream.get()); // If we are forced to copy the stream to a data, we can go ahead and // delete the stream.
stream.reset(nullptr);
} return SkCrabbyAvifCodec::MakeFromData(std::move(stream), std::move(data), result, gainmapOnly);
}
// Ignore XMP and Exif to ensure that avifDecoderParse() isn't waiting for // some tiny Exif payload hiding at the end of a file.
avifDecoder->ignoreXMP = crabbyavif::CRABBY_AVIF_TRUE;
avifDecoder->ignoreExif = crabbyavif::CRABBY_AVIF_TRUE;
// Disable strict mode. This allows some AVIF files in the wild that are // technically invalid according to the specification because they were // created with older tools but can be decoded and rendered without any // issues.
avifDecoder->strictFlags = crabbyavif::AVIF_STRICT_DISABLED;
// TODO(vigneshv): Enable threading based on number of CPU cores available.
avifDecoder->maxThreads = 1;
if (gainmapOnly) {
avifDecoder->imageContentToDecode = crabbyavif::AVIF_IMAGE_CONTENT_GAIN_MAP;
}
crabbyavif::avifResult res =
crabbyavif::avifDecoderSetIOMemory(avifDecoder.get(), data->bytes(), data->size()); if (res != crabbyavif::AVIF_RESULT_OK) {
*result = SkCodec::kInternalError; return nullptr;
}
res = crabbyavif::avifDecoderParse(avifDecoder.get()); if (res != crabbyavif::AVIF_RESULT_OK) {
*result = SkCodec::kInvalidInput; return nullptr;
}
std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr; // TODO(vigneshv): Get ICC Profile from the avif decoder.
crabbyavif::avifResult result =
crabbyavif::avifDecoderNthImage(fAvifDecoder.get(), options.fFrameIndex); if (result != crabbyavif::AVIF_RESULT_OK) { return kInvalidInput;
} if (fGainmapOnly && !fAvifDecoder->image->gainMap) { return kInvalidInput;
}
crabbyavif::avifImage* image =
fGainmapOnly ? fAvifDecoder->image->gainMap->image : fAvifDecoder->image; if (this->dimensions() != dstInfo.dimensions()) {
result = crabbyavif::avifImageScale(
image, dstInfo.width(), dstInfo.height(), &fAvifDecoder->diag); if (result != crabbyavif::AVIF_RESULT_OK) { return kInvalidInput;
}
}
using AvifImagePtr =
std::unique_ptr<crabbyavif::avifImage, decltype(&crabbyavif::crabby_avifImageDestroy)>; // cropped_image is a view into the underlying image. It can be safely deleted once the pixels // are converted into RGB (or when it goes out of scope in one of the error paths).
AvifImagePtr cropped_image{nullptr, crabbyavif::crabby_avifImageDestroy}; if (image->transformFlags & crabbyavif::AVIF_TRANSFORM_CLAP) {
crabbyavif::avifCropRect rect; if (crabbyavif::crabby_avifCropRectConvertCleanApertureBox(
&rect, &image->clap, image->width, image->height, image->yuvFormat, nullptr)) {
cropped_image.reset(crabbyavif::crabby_avifImageCreateEmpty());
result = crabbyavif::crabby_avifImageSetViewRect(cropped_image.get(), image, &rect); if (result != crabbyavif::AVIF_RESULT_OK) { return kInvalidInput;
}
image = cropped_image.get();
}
}
switch (dstInfo.colorType()) { case kRGBA_8888_SkColorType:
rgbImage.depth = 8; break; case kRGBA_F16_SkColorType:
rgbImage.depth = 16;
rgbImage.isFloat = crabbyavif::CRABBY_AVIF_TRUE; break; case kRGBA_1010102_SkColorType:
rgbImage.depth = 10;
rgbImage.format = crabbyavif::AVIF_RGB_FORMAT_RGBA1010102; break; default: // TODO(vigneshv): Check if more color types need to be supported. // Currently android supports at least RGB565 and BGRA8888 which is // not supported here. return kUnimplemented;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.