Export equirectangular JPEGs through paint renderer
This commit is contained in:
@@ -48,6 +48,37 @@ void append_png_bytes(void* context, void* data, int size)
|
||||
bytes->insert(bytes->end(), begin, begin + size);
|
||||
}
|
||||
|
||||
void append_encoded_bytes(void* context, void* data, int size)
|
||||
{
|
||||
append_png_bytes(context, data, size);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has_jpeg_soi(std::span<const std::byte> bytes) noexcept
|
||||
{
|
||||
return bytes.size() >= 2U
|
||||
&& bytes[0] == std::byte { 0xff }
|
||||
&& bytes[1] == std::byte { 0xd8 };
|
||||
}
|
||||
|
||||
constexpr char gpano_xmp[] =
|
||||
"http://ns.adobe.com/xap/1.0/\0"
|
||||
R"(<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
|
||||
<x:xmpmeta xmlns:x="adobe:ns:meta/" xmptk="SAMSUNG 360CAM">
|
||||
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
|
||||
<rdf:Description rdf:about="" xmlns:GPano="http://ns.google.com/photos/1.0/panorama/">
|
||||
<GPano:ProjectionType>equirectangular</GPano:ProjectionType>
|
||||
<GPano:UsePanoramaViewer>True</GPano:UsePanoramaViewer>
|
||||
<GPano:CroppedAreaLeftPixels>0</GPano:CroppedAreaLeftPixels>
|
||||
<GPano:CroppedAreaTopPixels>0</GPano:CroppedAreaTopPixels>
|
||||
<GPano:PoseHeadingDegrees>0</GPano:PoseHeadingDegrees>
|
||||
<GPano:PosePitchDegrees>0</GPano:PosePitchDegrees>
|
||||
<GPano:PoseRollDegrees>0</GPano:PoseRollDegrees>
|
||||
<GPano:StitchingSoftware>PanoPainter</GPano:StitchingSoftware>
|
||||
</rdf:Description>
|
||||
</rdf:RDF>
|
||||
</x:xmpmeta>
|
||||
<?xpacket end="r"?>)";
|
||||
|
||||
}
|
||||
|
||||
pp::foundation::Result<Rgba8Image> decode_png_rgba8(std::span<const std::byte> bytes)
|
||||
@@ -106,6 +137,64 @@ pp::foundation::Result<Rgba8Image> decode_png_rgba8(std::span<const std::byte> b
|
||||
return pp::foundation::Result<Rgba8Image>::success(std::move(image));
|
||||
}
|
||||
|
||||
pp::foundation::Result<Rgba8Image> decode_jpeg_rgba8(std::span<const std::byte> bytes)
|
||||
{
|
||||
if (!has_jpeg_soi(bytes)) {
|
||||
return pp::foundation::Result<Rgba8Image>::failure(
|
||||
pp::foundation::Status::invalid_argument("JPEG signature is invalid"));
|
||||
}
|
||||
|
||||
if (bytes.size() > static_cast<std::size_t>(std::numeric_limits<int>::max())) {
|
||||
return pp::foundation::Result<Rgba8Image>::failure(
|
||||
pp::foundation::Status::out_of_range("JPEG payload is too large for the decoder"));
|
||||
}
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int source_components = 0;
|
||||
auto* decoded = stbi_load_from_memory(
|
||||
reinterpret_cast<const stbi_uc*>(bytes.data()),
|
||||
static_cast<int>(bytes.size()),
|
||||
&width,
|
||||
&height,
|
||||
&source_components,
|
||||
4);
|
||||
if (decoded == nullptr) {
|
||||
return pp::foundation::Result<Rgba8Image>::failure(
|
||||
pp::foundation::Status::invalid_argument("JPEG payload could not be decoded"));
|
||||
}
|
||||
|
||||
const auto cleanup = [decoded]() noexcept {
|
||||
stbi_image_free(decoded);
|
||||
};
|
||||
|
||||
if (width <= 0 || height <= 0
|
||||
|| static_cast<std::uint32_t>(width) > max_image_dimension
|
||||
|| static_cast<std::uint32_t>(height) > max_image_dimension) {
|
||||
cleanup();
|
||||
return pp::foundation::Result<Rgba8Image>::failure(
|
||||
pp::foundation::Status::out_of_range("decoded JPEG dimensions are outside the configured range"));
|
||||
}
|
||||
|
||||
const auto byte_count = rgba_byte_size(
|
||||
static_cast<std::uint32_t>(width),
|
||||
static_cast<std::uint32_t>(height));
|
||||
if (!byte_count) {
|
||||
cleanup();
|
||||
return pp::foundation::Result<Rgba8Image>::failure(byte_count.status());
|
||||
}
|
||||
|
||||
Rgba8Image image {
|
||||
.width = static_cast<std::uint32_t>(width),
|
||||
.height = static_cast<std::uint32_t>(height),
|
||||
.pixels = {},
|
||||
};
|
||||
image.pixels.assign(decoded, decoded + byte_count.value());
|
||||
cleanup();
|
||||
|
||||
return pp::foundation::Result<Rgba8Image>::success(std::move(image));
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::vector<std::byte>> encode_png_rgba8(
|
||||
std::uint32_t width,
|
||||
std::uint32_t height,
|
||||
@@ -156,4 +245,79 @@ pp::foundation::Result<std::vector<std::byte>> encode_png_rgba8(
|
||||
return pp::foundation::Result<std::vector<std::byte>>::success(std::move(encoded));
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::vector<std::byte>> encode_jpeg_rgba8(
|
||||
std::uint32_t width,
|
||||
std::uint32_t height,
|
||||
std::span<const std::uint8_t> pixels,
|
||||
int quality)
|
||||
{
|
||||
if (width == 0 || height == 0) {
|
||||
return pp::foundation::Result<std::vector<std::byte>>::failure(
|
||||
pp::foundation::Status::invalid_argument("JPEG dimensions must be greater than zero"));
|
||||
}
|
||||
|
||||
if (width > static_cast<std::uint32_t>(std::numeric_limits<int>::max())
|
||||
|| height > static_cast<std::uint32_t>(std::numeric_limits<int>::max())) {
|
||||
return pp::foundation::Result<std::vector<std::byte>>::failure(
|
||||
pp::foundation::Status::out_of_range("JPEG dimensions exceed encoder limits"));
|
||||
}
|
||||
|
||||
if (quality < 1 || quality > 100) {
|
||||
return pp::foundation::Result<std::vector<std::byte>>::failure(
|
||||
pp::foundation::Status::out_of_range("JPEG quality must be within 1..100"));
|
||||
}
|
||||
|
||||
const auto byte_count = rgba_byte_size(width, height);
|
||||
if (!byte_count) {
|
||||
return pp::foundation::Result<std::vector<std::byte>>::failure(byte_count.status());
|
||||
}
|
||||
|
||||
if (pixels.size() != byte_count.value()) {
|
||||
return pp::foundation::Result<std::vector<std::byte>>::failure(
|
||||
pp::foundation::Status::invalid_argument("RGBA pixel payload size does not match dimensions"));
|
||||
}
|
||||
|
||||
std::vector<std::byte> encoded;
|
||||
const auto result = stbi_write_jpg_to_func(
|
||||
append_encoded_bytes,
|
||||
&encoded,
|
||||
static_cast<int>(width),
|
||||
static_cast<int>(height),
|
||||
4,
|
||||
pixels.data(),
|
||||
quality);
|
||||
|
||||
if (result == 0 || encoded.empty()) {
|
||||
return pp::foundation::Result<std::vector<std::byte>>::failure(
|
||||
pp::foundation::Status::invalid_argument("RGBA pixels could not be encoded as JPEG"));
|
||||
}
|
||||
|
||||
return pp::foundation::Result<std::vector<std::byte>>::success(std::move(encoded));
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::vector<std::byte>> inject_gpano_xmp_into_jpeg(std::span<const std::byte> jpeg)
|
||||
{
|
||||
if (!has_jpeg_soi(jpeg)) {
|
||||
return pp::foundation::Result<std::vector<std::byte>>::failure(
|
||||
pp::foundation::Status::invalid_argument("JPEG signature is invalid"));
|
||||
}
|
||||
|
||||
constexpr auto xmp_size = sizeof(gpano_xmp);
|
||||
static_assert(xmp_size + 2U <= 0xffffU);
|
||||
const auto segment_length = static_cast<std::uint16_t>(xmp_size + 2U);
|
||||
|
||||
std::vector<std::byte> encoded;
|
||||
encoded.reserve(jpeg.size() + xmp_size + 4U);
|
||||
encoded.insert(encoded.end(), jpeg.begin(), jpeg.begin() + 2);
|
||||
encoded.push_back(std::byte { 0xff });
|
||||
encoded.push_back(std::byte { 0xe1 });
|
||||
encoded.push_back(static_cast<std::byte>((segment_length >> 8U) & 0xffU));
|
||||
encoded.push_back(static_cast<std::byte>(segment_length & 0xffU));
|
||||
const auto* xmp_begin = reinterpret_cast<const std::byte*>(gpano_xmp);
|
||||
encoded.insert(encoded.end(), xmp_begin, xmp_begin + xmp_size);
|
||||
encoded.insert(encoded.end(), jpeg.begin() + 2, jpeg.end());
|
||||
|
||||
return pp::foundation::Result<std::vector<std::byte>>::success(std::move(encoded));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,9 +18,21 @@ struct Rgba8Image {
|
||||
[[nodiscard]] pp::foundation::Result<Rgba8Image> decode_png_rgba8(
|
||||
std::span<const std::byte> bytes);
|
||||
|
||||
[[nodiscard]] pp::foundation::Result<Rgba8Image> decode_jpeg_rgba8(
|
||||
std::span<const std::byte> bytes);
|
||||
|
||||
[[nodiscard]] pp::foundation::Result<std::vector<std::byte>> encode_png_rgba8(
|
||||
std::uint32_t width,
|
||||
std::uint32_t height,
|
||||
std::span<const std::uint8_t> pixels);
|
||||
|
||||
[[nodiscard]] pp::foundation::Result<std::vector<std::byte>> encode_jpeg_rgba8(
|
||||
std::uint32_t width,
|
||||
std::uint32_t height,
|
||||
std::span<const std::uint8_t> pixels,
|
||||
int quality);
|
||||
|
||||
[[nodiscard]] pp::foundation::Result<std::vector<std::byte>> inject_gpano_xmp_into_jpeg(
|
||||
std::span<const std::byte> jpeg);
|
||||
|
||||
}
|
||||
|
||||
@@ -38,6 +38,40 @@ bool is_png_export_target(std::string_view path) noexcept
|
||||
&& (extension[3] == 'g' || extension[3] == 'G');
|
||||
}
|
||||
|
||||
bool ascii_iequals(std::string_view left, std::string_view right) noexcept
|
||||
{
|
||||
if (left.size() != right.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < left.size(); ++i) {
|
||||
auto lhs = left[i];
|
||||
if (lhs >= 'A' && lhs <= 'Z') {
|
||||
lhs = static_cast<char>(lhs - 'A' + 'a');
|
||||
}
|
||||
auto rhs = right[i];
|
||||
if (rhs >= 'A' && rhs <= 'Z') {
|
||||
rhs = static_cast<char>(rhs - 'A' + 'a');
|
||||
}
|
||||
if (lhs != rhs) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool has_extension(std::string_view path, std::string_view extension) noexcept
|
||||
{
|
||||
return path.size() >= extension.size()
|
||||
&& ascii_iequals(path.substr(path.size() - extension.size()), extension);
|
||||
}
|
||||
|
||||
bool is_jpeg_export_target(std::string_view path) noexcept
|
||||
{
|
||||
return has_extension(path, ".jpg") || has_extension(path, ".jpeg");
|
||||
}
|
||||
|
||||
struct LegacyDocumentExportSnapshotReports {
|
||||
pp::app::DocumentCanvasSnapshotResult snapshot;
|
||||
pp::paint_renderer::DocumentFrameFacePngExportResult face_pngs;
|
||||
@@ -270,32 +304,52 @@ pp::foundation::Status export_animation_frames_from_document_snapshot(
|
||||
return pp::app::execute_document_export_collection_write(target, payloads, services);
|
||||
}
|
||||
|
||||
pp::foundation::Status export_equirectangular_png_from_document_snapshot(
|
||||
pp::foundation::Status export_equirectangular_from_document_snapshot(
|
||||
App& app,
|
||||
const pp::app::DocumentExportFileTarget& target,
|
||||
const LegacyDocumentExportSnapshotReports& reports)
|
||||
{
|
||||
if (!is_png_export_target(target.path)) {
|
||||
std::span<const std::byte> bytes;
|
||||
pp::paint_renderer::DocumentFrameEquirectangularPngExportResult png_export;
|
||||
pp::paint_renderer::DocumentFrameEquirectangularJpegExportResult jpeg_export;
|
||||
|
||||
if (is_png_export_target(target.path)) {
|
||||
auto exported = pp::paint_renderer::export_document_frame_equirectangular_png(reports.face_pngs.composite);
|
||||
if (!exported) {
|
||||
return exported.status();
|
||||
}
|
||||
|
||||
png_export = std::move(exported.value());
|
||||
LOG(
|
||||
"export-equirectangular document export PNG writer: %ux%u bytes=%llu facePayloads=%zu compositedLayerFaces=%zu",
|
||||
png_export.equirectangular_extent.width,
|
||||
png_export.equirectangular_extent.height,
|
||||
static_cast<unsigned long long>(png_export.encoded_bytes),
|
||||
png_export.face_payload_count,
|
||||
png_export.composited_layer_face_count);
|
||||
bytes = std::span<const std::byte>(png_export.png.data(), png_export.png.size());
|
||||
} else if (is_jpeg_export_target(target.path)) {
|
||||
auto exported = pp::paint_renderer::export_document_frame_equirectangular_jpeg(reports.face_pngs.composite);
|
||||
if (!exported) {
|
||||
return exported.status();
|
||||
}
|
||||
|
||||
jpeg_export = std::move(exported.value());
|
||||
LOG(
|
||||
"export-equirectangular document export JPEG writer: %ux%u bytes=%llu facePayloads=%zu compositedLayerFaces=%zu xmp=%s",
|
||||
jpeg_export.equirectangular_extent.width,
|
||||
jpeg_export.equirectangular_extent.height,
|
||||
static_cast<unsigned long long>(jpeg_export.encoded_bytes),
|
||||
jpeg_export.face_payload_count,
|
||||
jpeg_export.composited_layer_face_count,
|
||||
jpeg_export.xmp_injected ? "true" : "false");
|
||||
bytes = std::span<const std::byte>(jpeg_export.jpeg.data(), jpeg_export.jpeg.size());
|
||||
} else {
|
||||
return pp::foundation::Status::invalid_argument(
|
||||
"document snapshot equirectangular export currently supports PNG targets only");
|
||||
"document snapshot equirectangular export currently supports PNG and JPEG targets only");
|
||||
}
|
||||
|
||||
auto exported = pp::paint_renderer::export_document_frame_equirectangular_png(reports.face_pngs.composite);
|
||||
if (!exported) {
|
||||
return exported.status();
|
||||
}
|
||||
|
||||
auto exported_value = std::move(exported.value());
|
||||
LOG(
|
||||
"export-equirectangular document export PNG writer: %ux%u bytes=%llu facePayloads=%zu compositedLayerFaces=%zu",
|
||||
exported_value.equirectangular_extent.width,
|
||||
exported_value.equirectangular_extent.height,
|
||||
static_cast<unsigned long long>(exported_value.encoded_bytes),
|
||||
exported_value.face_payload_count,
|
||||
exported_value.composited_layer_face_count);
|
||||
const auto write_status = write_export_binary_file(
|
||||
target.path,
|
||||
std::span<const std::byte>(exported_value.png.data(), exported_value.png.size()));
|
||||
const auto write_status = write_export_binary_file(target.path, bytes);
|
||||
if (!write_status.ok()) {
|
||||
return write_status;
|
||||
}
|
||||
@@ -320,10 +374,10 @@ public:
|
||||
{
|
||||
auto* app = &app_;
|
||||
#if !__WEB__
|
||||
if (is_png_export_target(target.path)) {
|
||||
if (is_png_export_target(target.path) || is_jpeg_export_target(target.path)) {
|
||||
const auto prepared = prepare_legacy_document_export_snapshot(app_, "export-equirectangular");
|
||||
if (prepared) {
|
||||
const auto exported = export_equirectangular_png_from_document_snapshot(app_, target, prepared.value());
|
||||
const auto exported = export_equirectangular_from_document_snapshot(app_, target, prepared.value());
|
||||
if (exported.ok()) {
|
||||
show_export_success_dialog(
|
||||
app_,
|
||||
|
||||
@@ -271,6 +271,75 @@ void append_rgba8_bytes(std::vector<std::uint8_t>& bytes, std::span<const pp::pa
|
||||
}
|
||||
}
|
||||
|
||||
struct EquirectangularProjectionResult {
|
||||
pp::renderer::Extent2D face_extent {};
|
||||
pp::renderer::Extent2D equirectangular_extent {};
|
||||
std::vector<pp::paint::Rgba> pixels;
|
||||
std::size_t face_payload_count = 0;
|
||||
std::size_t composited_layer_face_count = 0;
|
||||
};
|
||||
|
||||
pp::foundation::Result<EquirectangularProjectionResult> project_document_frame_equirectangular(
|
||||
const DocumentFrameCompositeResult& composite)
|
||||
{
|
||||
const auto face_pixel_count = expected_pixel_count(composite.extent);
|
||||
if (!face_pixel_count) {
|
||||
return pp::foundation::Result<EquirectangularProjectionResult>::failure(face_pixel_count.status());
|
||||
}
|
||||
|
||||
for (const auto& face : composite.faces) {
|
||||
if (face.extent.width != composite.extent.width || face.extent.height != composite.extent.height
|
||||
|| face.pixels.size() != face_pixel_count.value()) {
|
||||
return pp::foundation::Result<EquirectangularProjectionResult>::failure(
|
||||
pp::foundation::Status::invalid_argument("document equirectangular export requires complete cube faces"));
|
||||
}
|
||||
}
|
||||
|
||||
const auto output_width = static_cast<std::uint64_t>(composite.extent.width) * 4U;
|
||||
const auto output_height = static_cast<std::uint64_t>(composite.extent.height) * 2U;
|
||||
if (output_width > std::numeric_limits<std::uint32_t>::max()
|
||||
|| output_height > std::numeric_limits<std::uint32_t>::max()) {
|
||||
return pp::foundation::Result<EquirectangularProjectionResult>::failure(
|
||||
pp::foundation::Status::out_of_range("document equirectangular extent exceeds uint32"));
|
||||
}
|
||||
|
||||
EquirectangularProjectionResult result;
|
||||
result.face_extent = composite.extent;
|
||||
result.equirectangular_extent = pp::renderer::Extent2D {
|
||||
.width = static_cast<std::uint32_t>(output_width),
|
||||
.height = static_cast<std::uint32_t>(output_height),
|
||||
};
|
||||
result.face_payload_count = composite.face_payload_count;
|
||||
result.composited_layer_face_count = composite.composited_layer_face_count;
|
||||
|
||||
const auto output_pixel_count = expected_pixel_count(result.equirectangular_extent);
|
||||
if (!output_pixel_count) {
|
||||
return pp::foundation::Result<EquirectangularProjectionResult>::failure(output_pixel_count.status());
|
||||
}
|
||||
|
||||
constexpr auto pi = 3.14159265358979323846F;
|
||||
constexpr auto two_pi = 6.28318530717958647692F;
|
||||
result.pixels.assign(output_pixel_count.value(), pp::paint::Rgba {});
|
||||
for (std::uint32_t y = 0; y < result.equirectangular_extent.height; ++y) {
|
||||
const auto v = (static_cast<float>(y) + 0.5F) / static_cast<float>(result.equirectangular_extent.height);
|
||||
const auto angle_y = (1.0F - v) * pi;
|
||||
const auto sin_y = std::sin(angle_y);
|
||||
const auto cos_y = std::cos(angle_y);
|
||||
for (std::uint32_t x = 0; x < result.equirectangular_extent.width; ++x) {
|
||||
const auto u = (static_cast<float>(x) + 0.5F) / static_cast<float>(result.equirectangular_extent.width);
|
||||
const auto angle_x = (1.25F - u) * two_pi;
|
||||
const auto sample = panopainter_cube_face_sample(
|
||||
sin_y * std::cos(angle_x),
|
||||
cos_y,
|
||||
sin_y * std::sin(angle_x));
|
||||
result.pixels[static_cast<std::size_t>(y) * result.equirectangular_extent.width + x] =
|
||||
sample_face_nearest(composite.faces[sample.face_index], sample.s, sample.t);
|
||||
}
|
||||
}
|
||||
|
||||
return pp::foundation::Result<EquirectangularProjectionResult>::success(std::move(result));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pp::foundation::Status composite_layer(
|
||||
@@ -602,63 +671,19 @@ pp::foundation::Result<DocumentFrameFacePngExportResult> export_document_frame_f
|
||||
pp::foundation::Result<DocumentFrameEquirectangularPngExportResult>
|
||||
export_document_frame_equirectangular_png(const DocumentFrameCompositeResult& composite)
|
||||
{
|
||||
const auto face_pixel_count = expected_pixel_count(composite.extent);
|
||||
if (!face_pixel_count) {
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularPngExportResult>::failure(face_pixel_count.status());
|
||||
}
|
||||
|
||||
for (const auto& face : composite.faces) {
|
||||
if (face.extent.width != composite.extent.width || face.extent.height != composite.extent.height
|
||||
|| face.pixels.size() != face_pixel_count.value()) {
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularPngExportResult>::failure(
|
||||
pp::foundation::Status::invalid_argument("document equirectangular export requires complete cube faces"));
|
||||
}
|
||||
}
|
||||
|
||||
const auto output_width = static_cast<std::uint64_t>(composite.extent.width) * 4U;
|
||||
const auto output_height = static_cast<std::uint64_t>(composite.extent.height) * 2U;
|
||||
if (output_width > std::numeric_limits<std::uint32_t>::max()
|
||||
|| output_height > std::numeric_limits<std::uint32_t>::max()) {
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularPngExportResult>::failure(
|
||||
pp::foundation::Status::out_of_range("document equirectangular extent exceeds uint32"));
|
||||
auto projection = project_document_frame_equirectangular(composite);
|
||||
if (!projection) {
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularPngExportResult>::failure(projection.status());
|
||||
}
|
||||
|
||||
DocumentFrameEquirectangularPngExportResult result;
|
||||
result.face_extent = composite.extent;
|
||||
result.equirectangular_extent = pp::renderer::Extent2D {
|
||||
.width = static_cast<std::uint32_t>(output_width),
|
||||
.height = static_cast<std::uint32_t>(output_height),
|
||||
};
|
||||
result.face_payload_count = composite.face_payload_count;
|
||||
result.composited_layer_face_count = composite.composited_layer_face_count;
|
||||
|
||||
const auto output_pixel_count = expected_pixel_count(result.equirectangular_extent);
|
||||
if (!output_pixel_count) {
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularPngExportResult>::failure(output_pixel_count.status());
|
||||
}
|
||||
|
||||
constexpr auto pi = 3.14159265358979323846F;
|
||||
constexpr auto two_pi = 6.28318530717958647692F;
|
||||
std::vector<pp::paint::Rgba> output(output_pixel_count.value());
|
||||
for (std::uint32_t y = 0; y < result.equirectangular_extent.height; ++y) {
|
||||
const auto v = (static_cast<float>(y) + 0.5F) / static_cast<float>(result.equirectangular_extent.height);
|
||||
const auto angle_y = (1.0F - v) * pi;
|
||||
const auto sin_y = std::sin(angle_y);
|
||||
const auto cos_y = std::cos(angle_y);
|
||||
for (std::uint32_t x = 0; x < result.equirectangular_extent.width; ++x) {
|
||||
const auto u = (static_cast<float>(x) + 0.5F) / static_cast<float>(result.equirectangular_extent.width);
|
||||
const auto angle_x = (1.25F - u) * two_pi;
|
||||
const auto sample = panopainter_cube_face_sample(
|
||||
sin_y * std::cos(angle_x),
|
||||
cos_y,
|
||||
sin_y * std::sin(angle_x));
|
||||
output[static_cast<std::size_t>(y) * result.equirectangular_extent.width + x] =
|
||||
sample_face_nearest(composite.faces[sample.face_index], sample.s, sample.t);
|
||||
}
|
||||
}
|
||||
result.face_extent = projection.value().face_extent;
|
||||
result.equirectangular_extent = projection.value().equirectangular_extent;
|
||||
result.face_payload_count = projection.value().face_payload_count;
|
||||
result.composited_layer_face_count = projection.value().composited_layer_face_count;
|
||||
|
||||
std::vector<std::uint8_t> rgba8;
|
||||
append_rgba8_bytes(rgba8, output);
|
||||
append_rgba8_bytes(rgba8, projection.value().pixels);
|
||||
auto encoded = pp::assets::encode_png_rgba8(
|
||||
result.equirectangular_extent.width,
|
||||
result.equirectangular_extent.height,
|
||||
@@ -683,6 +708,53 @@ export_document_frame_equirectangular_png(DocumentFrameCompositeRequest request)
|
||||
return export_document_frame_equirectangular_png(composite.value());
|
||||
}
|
||||
|
||||
pp::foundation::Result<DocumentFrameEquirectangularJpegExportResult>
|
||||
export_document_frame_equirectangular_jpeg(const DocumentFrameCompositeResult& composite, int quality)
|
||||
{
|
||||
auto projection = project_document_frame_equirectangular(composite);
|
||||
if (!projection) {
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularJpegExportResult>::failure(projection.status());
|
||||
}
|
||||
|
||||
DocumentFrameEquirectangularJpegExportResult result;
|
||||
result.face_extent = projection.value().face_extent;
|
||||
result.equirectangular_extent = projection.value().equirectangular_extent;
|
||||
result.face_payload_count = projection.value().face_payload_count;
|
||||
result.composited_layer_face_count = projection.value().composited_layer_face_count;
|
||||
|
||||
std::vector<std::uint8_t> rgba8;
|
||||
append_rgba8_bytes(rgba8, projection.value().pixels);
|
||||
auto encoded = pp::assets::encode_jpeg_rgba8(
|
||||
result.equirectangular_extent.width,
|
||||
result.equirectangular_extent.height,
|
||||
rgba8,
|
||||
quality);
|
||||
if (!encoded) {
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularJpegExportResult>::failure(encoded.status());
|
||||
}
|
||||
|
||||
auto with_xmp = pp::assets::inject_gpano_xmp_into_jpeg(encoded.value());
|
||||
if (!with_xmp) {
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularJpegExportResult>::failure(with_xmp.status());
|
||||
}
|
||||
|
||||
result.encoded_bytes = static_cast<std::uint64_t>(with_xmp.value().size());
|
||||
result.jpeg = std::move(with_xmp.value());
|
||||
result.xmp_injected = true;
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularJpegExportResult>::success(std::move(result));
|
||||
}
|
||||
|
||||
pp::foundation::Result<DocumentFrameEquirectangularJpegExportResult>
|
||||
export_document_frame_equirectangular_jpeg(DocumentFrameCompositeRequest request, int quality)
|
||||
{
|
||||
auto composite = composite_document_frame(request);
|
||||
if (!composite) {
|
||||
return pp::foundation::Result<DocumentFrameEquirectangularJpegExportResult>::failure(composite.status());
|
||||
}
|
||||
|
||||
return export_document_frame_equirectangular_jpeg(composite.value(), quality);
|
||||
}
|
||||
|
||||
pp::foundation::Result<DocumentDepthExportRenderPlan> plan_document_depth_export_render(
|
||||
DocumentDepthExportRenderPlanRequest request) noexcept
|
||||
{
|
||||
|
||||
@@ -156,6 +156,16 @@ struct DocumentFrameEquirectangularPngExportResult {
|
||||
std::size_t composited_layer_face_count = 0;
|
||||
};
|
||||
|
||||
struct DocumentFrameEquirectangularJpegExportResult {
|
||||
pp::renderer::Extent2D face_extent {};
|
||||
pp::renderer::Extent2D equirectangular_extent {};
|
||||
std::vector<std::byte> jpeg;
|
||||
std::uint64_t encoded_bytes = 0;
|
||||
std::size_t face_payload_count = 0;
|
||||
std::size_t composited_layer_face_count = 0;
|
||||
bool xmp_injected = false;
|
||||
};
|
||||
|
||||
struct DocumentDepthExportRenderPlanRequest {
|
||||
const pp::document::CanvasDocument* document = nullptr;
|
||||
std::size_t frame_index = 0;
|
||||
@@ -255,6 +265,16 @@ export_document_frame_equirectangular_png(const DocumentFrameCompositeResult& co
|
||||
[[nodiscard]] pp::foundation::Result<DocumentFrameEquirectangularPngExportResult>
|
||||
export_document_frame_equirectangular_png(DocumentFrameCompositeRequest request);
|
||||
|
||||
[[nodiscard]] pp::foundation::Result<DocumentFrameEquirectangularJpegExportResult>
|
||||
export_document_frame_equirectangular_jpeg(
|
||||
const DocumentFrameCompositeResult& composite,
|
||||
int quality = 100);
|
||||
|
||||
[[nodiscard]] pp::foundation::Result<DocumentFrameEquirectangularJpegExportResult>
|
||||
export_document_frame_equirectangular_jpeg(
|
||||
DocumentFrameCompositeRequest request,
|
||||
int quality = 100);
|
||||
|
||||
[[nodiscard]] pp::foundation::Result<DocumentDepthExportRenderPlan> plan_document_depth_export_render(
|
||||
DocumentDepthExportRenderPlanRequest request) noexcept;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user