// Holds inputs/outputs for decoding/encoding images.
#include <stddef.h>
+#include <stdint.h>
+#include <type_traits>
#include <utility>
#include <vector>
-#include "lib/jxl/alpha.h"
#include "lib/jxl/base/data_parallel.h"
-#include "lib/jxl/common.h"
+#include "lib/jxl/base/status.h"
#include "lib/jxl/frame_header.h"
#include "lib/jxl/headers.h"
#include "lib/jxl/image.h"
#include "lib/jxl/image_bundle.h"
#include "lib/jxl/luminance.h"
-#include "lib/jxl/size_constraints.h"
namespace jxl {
-// Per-channel interval, used to convert between (full-range) external and
-// (bounded or unbounded) temp values. See external_image.cc for the definitions
-// of temp/external.
-struct CodecInterval {
- CodecInterval() = default;
- constexpr CodecInterval(float min, float max) : min(min), width(max - min) {}
- // Defaults for temp.
- float min = 0.0f;
- float width = 1.0f;
-};
-
-template <typename T,
- class = typename std::enable_if<std::is_unsigned<T>::value>::type>
-Status VerifyDimensions(const SizeConstraints* constraints, T xs, T ys) {
- if (!constraints) return true;
-
- if (xs == 0 || ys == 0) return JXL_FAILURE("Empty image.");
- if (xs > constraints->dec_max_xsize) return JXL_FAILURE("Image too wide.");
- if (ys > constraints->dec_max_ysize) return JXL_FAILURE("Image too tall.");
-
- const uint64_t num_pixels = static_cast<uint64_t>(xs) * ys;
- if (num_pixels > constraints->dec_max_pixels) {
- return JXL_FAILURE("Image too big.");
- }
-
- return true;
-}
-
-using CodecIntervals = std::array<CodecInterval, 4>; // RGB[A] or Y[A]
-
// Optional text/EXIF metadata.
struct Blobs {
std::vector<uint8_t> exif;
// If c_current.IsGray(), all planes must be identical.
void SetFromImage(Image3F&& color, const ColorEncoding& c_current) {
Main().SetFromImage(std::move(color), c_current);
- SetIntensityTarget(this);
+ SetIntensityTarget(&this->metadata.m);
SetSize(Main().xsize(), Main().ysize());
}
SetSize(xsize, ysize);
}
- // Calls TransformTo for each ImageBundle (preview/frames).
- Status TransformTo(const ColorEncoding& c_desired, const JxlCmsInterface& cms,
- ThreadPool* pool = nullptr) {
- if (metadata.m.have_preview) {
- JXL_RETURN_IF_ERROR(preview_frame.TransformTo(c_desired, cms, pool));
- }
- for (ImageBundle& ib : frames) {
- JXL_RETURN_IF_ERROR(ib.TransformTo(c_desired, cms, pool));
- }
- return true;
- }
- // Performs "PremultiplyAlpha" for each ImageBundle (preview/frames).
- bool PremultiplyAlpha() {
- const auto doPremultiplyAlpha = [](ImageBundle& bundle) {
- if (!bundle.HasAlpha()) return;
- if (!bundle.HasColor()) return;
- auto* color = bundle.color();
- const auto* alpha = bundle.alpha();
- JXL_CHECK(color->ysize() == alpha->ysize());
- JXL_CHECK(color->xsize() == alpha->xsize());
- for (size_t y = 0; y < color->ysize(); y++) {
- ::jxl::PremultiplyAlpha(color->PlaneRow(0, y), color->PlaneRow(1, y),
- color->PlaneRow(2, y), alpha->Row(y),
- color->xsize());
- }
- };
- ExtraChannelInfo* eci = metadata.m.Find(ExtraChannel::kAlpha);
- if (eci == nullptr || eci->alpha_associated) return false;
- if (metadata.m.have_preview) {
- doPremultiplyAlpha(preview_frame);
- }
- for (ImageBundle& ib : frames) {
- doPremultiplyAlpha(ib);
- }
- eci->alpha_associated = true;
- return true;
- }
-
- bool UnpremultiplyAlpha() {
- const auto doUnpremultiplyAlpha = [](ImageBundle& bundle) {
- if (!bundle.HasAlpha()) return;
- if (!bundle.HasColor()) return;
- auto* color = bundle.color();
- const auto* alpha = bundle.alpha();
- JXL_CHECK(color->ysize() == alpha->ysize());
- JXL_CHECK(color->xsize() == alpha->xsize());
- for (size_t y = 0; y < color->ysize(); y++) {
- ::jxl::UnpremultiplyAlpha(color->PlaneRow(0, y), color->PlaneRow(1, y),
- color->PlaneRow(2, y), alpha->Row(y),
- color->xsize());
- }
- };
- ExtraChannelInfo* eci = metadata.m.Find(ExtraChannel::kAlpha);
- if (eci == nullptr || !eci->alpha_associated) return false;
- if (metadata.m.have_preview) {
- doUnpremultiplyAlpha(preview_frame);
- }
- for (ImageBundle& ib : frames) {
- doUnpremultiplyAlpha(ib);
- }
- eci->alpha_associated = false;
- return true;
- }
-
- // -- DECODER INPUT:
-
- SizeConstraints constraints;
-
- // -- DECODER OUTPUT:
-
- // Total number of pixels decoded (may differ from #frames * xsize * ysize
- // if frames are cropped)
- uint64_t dec_pixels = 0;
-
// -- DECODER OUTPUT, ENCODER INPUT:
// Metadata stored into / retrieved from bitstreams.