Export equirectangular JPEGs through paint renderer
This commit is contained in:
@@ -5,10 +5,14 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
using pp::assets::decode_png_rgba8;
|
||||
using pp::assets::decode_jpeg_rgba8;
|
||||
using pp::assets::encode_jpeg_rgba8;
|
||||
using pp::assets::encode_png_rgba8;
|
||||
using pp::assets::inject_gpano_xmp_into_jpeg;
|
||||
using pp::foundation::StatusCode;
|
||||
|
||||
namespace {
|
||||
@@ -75,6 +79,66 @@ void encodes_rgba8_pixels_to_decodable_png(pp::tests::Harness& h)
|
||||
PP_EXPECT(h, decoded.value().pixels == pixels);
|
||||
}
|
||||
|
||||
void encodes_rgba8_pixels_to_decodable_jpeg(pp::tests::Harness& h)
|
||||
{
|
||||
const std::vector<std::uint8_t> pixels {
|
||||
255, 0, 0, 255,
|
||||
0, 255, 0, 255,
|
||||
0, 0, 255, 255,
|
||||
255, 255, 255, 255,
|
||||
};
|
||||
|
||||
const auto encoded = encode_jpeg_rgba8(2, 2, pixels, 95);
|
||||
|
||||
PP_EXPECT(h, encoded.ok());
|
||||
PP_EXPECT(h, encoded.value().size() > 4U);
|
||||
PP_EXPECT(h, encoded.value()[0] == std::byte { 0xff });
|
||||
PP_EXPECT(h, encoded.value()[1] == std::byte { 0xd8 });
|
||||
const auto decoded = decode_jpeg_rgba8(encoded.value());
|
||||
PP_EXPECT(h, decoded.ok());
|
||||
PP_EXPECT(h, decoded.value().width == 2U);
|
||||
PP_EXPECT(h, decoded.value().height == 2U);
|
||||
PP_EXPECT(h, decoded.value().pixels.size() == pixels.size());
|
||||
}
|
||||
|
||||
void injects_gpano_xmp_into_jpeg(pp::tests::Harness& h)
|
||||
{
|
||||
const std::vector<std::uint8_t> pixels {
|
||||
10, 20, 30, 255,
|
||||
40, 50, 60, 255,
|
||||
};
|
||||
const auto encoded = encode_jpeg_rgba8(2, 1, pixels, 90);
|
||||
PP_EXPECT(h, encoded);
|
||||
if (!encoded) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto with_xmp = inject_gpano_xmp_into_jpeg(encoded.value());
|
||||
PP_EXPECT(h, with_xmp);
|
||||
if (!with_xmp) {
|
||||
return;
|
||||
}
|
||||
|
||||
PP_EXPECT(h, with_xmp.value().size() > encoded.value().size());
|
||||
PP_EXPECT(h, with_xmp.value()[0] == std::byte { 0xff });
|
||||
PP_EXPECT(h, with_xmp.value()[1] == std::byte { 0xd8 });
|
||||
PP_EXPECT(h, with_xmp.value()[2] == std::byte { 0xff });
|
||||
PP_EXPECT(h, with_xmp.value()[3] == std::byte { 0xe1 });
|
||||
|
||||
const std::string_view text(
|
||||
reinterpret_cast<const char*>(with_xmp.value().data()),
|
||||
with_xmp.value().size());
|
||||
PP_EXPECT(h, text.find("GPano:ProjectionType") != std::string_view::npos);
|
||||
PP_EXPECT(h, text.find("equirectangular") != std::string_view::npos);
|
||||
|
||||
const auto decoded = decode_jpeg_rgba8(with_xmp.value());
|
||||
PP_EXPECT(h, decoded);
|
||||
if (decoded) {
|
||||
PP_EXPECT(h, decoded.value().width == 2U);
|
||||
PP_EXPECT(h, decoded.value().height == 1U);
|
||||
}
|
||||
}
|
||||
|
||||
void rejects_invalid_png_encode_inputs(pp::tests::Harness& h)
|
||||
{
|
||||
const std::vector<std::uint8_t> pixels { 0, 0, 0, 0 };
|
||||
@@ -88,6 +152,29 @@ void rejects_invalid_png_encode_inputs(pp::tests::Harness& h)
|
||||
PP_EXPECT(h, wrong_payload_size.status().code == StatusCode::invalid_argument);
|
||||
}
|
||||
|
||||
void rejects_invalid_jpeg_inputs(pp::tests::Harness& h)
|
||||
{
|
||||
const std::vector<std::uint8_t> pixels { 0, 0, 0, 255 };
|
||||
const std::byte corrupt[] { std::byte { 0x00 }, std::byte { 0x01 } };
|
||||
|
||||
const auto no_size = encode_jpeg_rgba8(0, 1, pixels, 90);
|
||||
const auto bad_quality = encode_jpeg_rgba8(1, 1, pixels, 0);
|
||||
const auto wrong_payload_size = encode_jpeg_rgba8(2, 1, pixels, 90);
|
||||
const auto corrupt_decode = decode_jpeg_rgba8(corrupt);
|
||||
const auto corrupt_xmp = inject_gpano_xmp_into_jpeg(corrupt);
|
||||
|
||||
PP_EXPECT(h, !no_size.ok());
|
||||
PP_EXPECT(h, no_size.status().code == StatusCode::invalid_argument);
|
||||
PP_EXPECT(h, !bad_quality.ok());
|
||||
PP_EXPECT(h, bad_quality.status().code == StatusCode::out_of_range);
|
||||
PP_EXPECT(h, !wrong_payload_size.ok());
|
||||
PP_EXPECT(h, wrong_payload_size.status().code == StatusCode::invalid_argument);
|
||||
PP_EXPECT(h, !corrupt_decode.ok());
|
||||
PP_EXPECT(h, corrupt_decode.status().code == StatusCode::invalid_argument);
|
||||
PP_EXPECT(h, !corrupt_xmp.ok());
|
||||
PP_EXPECT(h, corrupt_xmp.status().code == StatusCode::invalid_argument);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
@@ -96,6 +183,9 @@ int main()
|
||||
harness.run("decodes_png_to_rgba8_pixels", decodes_png_to_rgba8_pixels);
|
||||
harness.run("rejects_corrupt_png_payload", rejects_corrupt_png_payload);
|
||||
harness.run("encodes_rgba8_pixels_to_decodable_png", encodes_rgba8_pixels_to_decodable_png);
|
||||
harness.run("encodes_rgba8_pixels_to_decodable_jpeg", encodes_rgba8_pixels_to_decodable_jpeg);
|
||||
harness.run("injects_gpano_xmp_into_jpeg", injects_gpano_xmp_into_jpeg);
|
||||
harness.run("rejects_invalid_png_encode_inputs", rejects_invalid_png_encode_inputs);
|
||||
harness.run("rejects_invalid_jpeg_inputs", rejects_invalid_jpeg_inputs);
|
||||
return harness.finish();
|
||||
}
|
||||
|
||||
@@ -903,6 +903,71 @@ void exports_document_frame_as_equirectangular_png(pp::tests::Harness& h)
|
||||
PP_EXPECT(h, decoded.value().pixels[bottom + 2U] == 255U);
|
||||
}
|
||||
|
||||
void exports_document_frame_as_equirectangular_jpeg_with_xmp(pp::tests::Harness& h)
|
||||
{
|
||||
const AnimationFrame root_frames[] {
|
||||
{ .duration_ms = 100, .face_pixels = {} },
|
||||
};
|
||||
const AnimationFrame layer_frames[] {
|
||||
{
|
||||
.duration_ms = 100,
|
||||
.face_pixels = solid_cube_faces(1, 4, 25, 125, 225, 255),
|
||||
},
|
||||
};
|
||||
const DocumentLayerConfig layers[] {
|
||||
{
|
||||
.name = "Paint",
|
||||
.frames = std::span<const AnimationFrame>(layer_frames, 1),
|
||||
},
|
||||
};
|
||||
const auto document = CanvasDocument::create_from_snapshot(DocumentSnapshotConfig {
|
||||
.width = 1,
|
||||
.height = 4,
|
||||
.layers = std::span<const DocumentLayerConfig>(layers, 1),
|
||||
.frames = std::span<const AnimationFrame>(root_frames, 1),
|
||||
.selection_masks = {},
|
||||
});
|
||||
PP_EXPECT(h, document);
|
||||
if (!document) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto exported = pp::paint_renderer::export_document_frame_equirectangular_jpeg(
|
||||
DocumentFrameCompositeRequest {
|
||||
.document = &document.value(),
|
||||
.frame_index = 0,
|
||||
},
|
||||
90);
|
||||
|
||||
PP_EXPECT(h, exported);
|
||||
if (!exported) {
|
||||
return;
|
||||
}
|
||||
|
||||
PP_EXPECT(h, exported.value().face_extent.width == 1U);
|
||||
PP_EXPECT(h, exported.value().face_extent.height == 4U);
|
||||
PP_EXPECT(h, exported.value().equirectangular_extent.width == 4U);
|
||||
PP_EXPECT(h, exported.value().equirectangular_extent.height == 8U);
|
||||
PP_EXPECT(h, exported.value().face_payload_count == pp::document::cube_face_count);
|
||||
PP_EXPECT(h, exported.value().encoded_bytes == exported.value().jpeg.size());
|
||||
PP_EXPECT(h, exported.value().xmp_injected);
|
||||
|
||||
const std::string_view text(
|
||||
reinterpret_cast<const char*>(exported.value().jpeg.data()),
|
||||
exported.value().jpeg.size());
|
||||
PP_EXPECT(h, text.find("GPano:ProjectionType") != std::string_view::npos);
|
||||
PP_EXPECT(h, text.find("equirectangular") != std::string_view::npos);
|
||||
|
||||
const auto decoded = pp::assets::decode_jpeg_rgba8(exported.value().jpeg);
|
||||
PP_EXPECT(h, decoded);
|
||||
if (!decoded) {
|
||||
return;
|
||||
}
|
||||
|
||||
PP_EXPECT(h, decoded.value().width == 4U);
|
||||
PP_EXPECT(h, decoded.value().height == 8U);
|
||||
}
|
||||
|
||||
void exports_document_layers_as_equirectangular_pngs(pp::tests::Harness& h)
|
||||
{
|
||||
const AnimationFrame root_frames[] {
|
||||
@@ -1121,6 +1186,8 @@ void document_frame_upload_rejects_invalid_requests(pp::tests::Harness& h)
|
||||
DocumentFrameCompositeRequest {});
|
||||
const auto no_document_equirect = pp::paint_renderer::export_document_frame_equirectangular_png(
|
||||
DocumentFrameCompositeRequest {});
|
||||
const auto no_document_jpeg = pp::paint_renderer::export_document_frame_equirectangular_jpeg(
|
||||
DocumentFrameCompositeRequest {});
|
||||
const auto no_document_layers = pp::paint_renderer::export_document_layers_equirectangular_pngs(
|
||||
pp::paint_renderer::DocumentLayerEquirectangularPngExportRequest {});
|
||||
const auto no_document_frames = pp::paint_renderer::export_document_animation_frames_equirectangular_pngs(
|
||||
@@ -1165,6 +1232,12 @@ void document_frame_upload_rejects_invalid_requests(pp::tests::Harness& h)
|
||||
.frame_index = 0,
|
||||
.output_extent = Extent2D {},
|
||||
});
|
||||
const auto bad_jpeg_quality = pp::paint_renderer::export_document_frame_equirectangular_jpeg(
|
||||
DocumentFrameCompositeRequest {
|
||||
.document = &document.value(),
|
||||
.frame_index = 0,
|
||||
},
|
||||
0);
|
||||
|
||||
PP_EXPECT(h, !no_document.ok());
|
||||
PP_EXPECT(h, no_document.status().code == StatusCode::invalid_argument);
|
||||
@@ -1172,6 +1245,8 @@ void document_frame_upload_rejects_invalid_requests(pp::tests::Harness& h)
|
||||
PP_EXPECT(h, no_document_readiness.status().code == StatusCode::invalid_argument);
|
||||
PP_EXPECT(h, !no_document_equirect.ok());
|
||||
PP_EXPECT(h, no_document_equirect.status().code == StatusCode::invalid_argument);
|
||||
PP_EXPECT(h, !no_document_jpeg.ok());
|
||||
PP_EXPECT(h, no_document_jpeg.status().code == StatusCode::invalid_argument);
|
||||
PP_EXPECT(h, !no_document_layers.ok());
|
||||
PP_EXPECT(h, no_document_layers.status().code == StatusCode::invalid_argument);
|
||||
PP_EXPECT(h, !no_document_frames.ok());
|
||||
@@ -1186,6 +1261,8 @@ void document_frame_upload_rejects_invalid_requests(pp::tests::Harness& h)
|
||||
PP_EXPECT(h, bad_frame_depth.status().code == StatusCode::out_of_range);
|
||||
PP_EXPECT(h, !bad_extent_depth.ok());
|
||||
PP_EXPECT(h, bad_extent_depth.status().code == StatusCode::invalid_argument);
|
||||
PP_EXPECT(h, !bad_jpeg_quality.ok());
|
||||
PP_EXPECT(h, bad_jpeg_quality.status().code == StatusCode::out_of_range);
|
||||
PP_EXPECT(h, device.commands().empty());
|
||||
}
|
||||
|
||||
@@ -1538,6 +1615,9 @@ int main()
|
||||
harness.run("exports_document_frame_faces_as_pngs", exports_document_frame_faces_as_pngs);
|
||||
harness.run("prepares_document_frame_export_readiness_report", prepares_document_frame_export_readiness_report);
|
||||
harness.run("exports_document_frame_as_equirectangular_png", exports_document_frame_as_equirectangular_png);
|
||||
harness.run(
|
||||
"exports_document_frame_as_equirectangular_jpeg_with_xmp",
|
||||
exports_document_frame_as_equirectangular_jpeg_with_xmp);
|
||||
harness.run("exports_document_layers_as_equirectangular_pngs", exports_document_layers_as_equirectangular_pngs);
|
||||
harness.run(
|
||||
"exports_document_animation_frames_as_equirectangular_pngs",
|
||||
|
||||
Reference in New Issue
Block a user