Make the JPEG cropper drop broken metadata
Alpine 3.24 Success
Alpine 3.24 Flatpak Success
Arch Linux AUR Success

An iPhone camera photo includes a bunch of stuff
that libjpeg-turbo nukes.
This commit is contained in:
p
2026-10-29 04:25:16 +01:00
parent 2687cee745
commit f712abfda4
4 changed files with 68 additions and 17 deletions
+3 -1
View File
@@ -60,7 +60,9 @@ Cropper::load_working(const vector<uint8_t> &data)
return false;
// libjpeg-turbo transforms keep nothing past the primary image's EOI,
// such as gain maps or depth maps.
// such as gain maps or depth maps. We also remove the index of these
// images, thus we remember the count from the time of opening the file.
grid.mpf_images = max(grid.mpf_images, this->grid_.mpf_images);
if (grid.mpf_images)
set_message(dawn::format_message(
P_("Saving drops %u embedded image",
+6 -4
View File
@@ -475,10 +475,12 @@ struct JpegGrid {
};
bool jpeg_grid(std::span<const uint8_t> data, JpegGrid *out, Error *error);
/// Turns stored pixels, then crops in the rotated image's MCU grid. Zero width
/// or height means to the edge. Rotations that cannot preserve every block
/// fail. All markers are copied unchanged, including Exif orientation and
/// dimensions. Returns empty on failure.
/// Rotates stored pixels, then crops in the rotated image's MCU grid.
/// Zero width or height means to the edge. Rotations that cannot preserve
/// every block fail. Markers are copied unchanged, including Exif orientation
/// and dimensions, except those invalidated by the result: the Multi-Picture
/// Format index, ISO 21496-1 and Apple gain map segments. Any Exif thumbnail
/// is unlinked. Returns empty on failure.
std::vector<uint8_t> jpeg_transform(std::span<const uint8_t> data,
Orientation op, uint32_t x, uint32_t y, uint32_t w, uint32_t h,
Error *error);
+46 -6
View File
@@ -168,6 +168,9 @@ enum JpegMarker : uint8_t {
APP5,
APP6,
APP7,
APP8,
APP9,
APP10,
};
struct JpegMetadata {
@@ -227,6 +230,9 @@ next_jpeg_segment(
return false;
}
// ISO 21496-1 metadata, as Ultra HDR 1.1 stores it in JPEG.
static constexpr char kIsoNs[] = "urn:iso:std:iso:ts:21496:-1";
static void
parse_jpeg_metadata(span<const uint8_t> data, JpegMetadata *meta)
{
@@ -267,11 +273,9 @@ parse_jpeg_metadata(span<const uint8_t> data, JpegMetadata *meta)
!memcmp(payload.data(), xmp_ns, sizeof xmp_ns) && meta->xmp.empty())
meta->xmp.assign(payload.begin() + sizeof xmp_ns, payload.end());
// ISO 21496-1 metadata, as Ultra HDR 1.1 stores it in JPEG.
static constexpr char iso_ns[] = "urn:iso:std:iso:ts:21496:-1";
if (marker == APP2 && payload.size() >= sizeof iso_ns &&
!memcmp(payload.data(), iso_ns, sizeof iso_ns) && meta->iso.empty())
meta->iso.assign(payload.begin() + sizeof iso_ns, payload.end());
if (marker == APP2 && payload.size() >= sizeof kIsoNs &&
!memcmp(payload.data(), kIsoNs, sizeof kIsoNs) && meta->iso.empty())
meta->iso.assign(payload.begin() + sizeof kIsoNs, payload.end());
}
if (!icc_done)
@@ -766,6 +770,42 @@ jpeg_grid(span<const uint8_t> data, JpegGrid *out, Error *error)
return ok;
}
// TurboJPEG can only filter markers by class. Some markers describe data not
// kept by a transform: images after the EOI of the primary image,
// or the Exif thumbnail, which the transform neither rotates nor crops.
static vector<uint8_t>
drop_stale_markers(unsigned char *data, size_t length)
{
vector<uint8_t> result;
const uint8_t *kept = data, *end = data + length;
uint8_t marker = 0;
span<const uint8_t> payload, rest(data, length);
while (next_jpeg_segment(rest, &marker, &payload)) {
// End the IFD chain after IFD0, so that IFD1 has no link.
tiffer T = {};
if (marker == APP1 && payload.size() >= 6 &&
!memcmp(payload.data(), "Exif\0", 5) &&
tiffer_init(&T, payload.data() + 6, payload.size() - 6) &&
tiffer_next_ifd(&T) &&
size_t(T.end - T.p) >= T.remaining_fields * 12u + 4)
memset(data + (T.p - data) + T.remaining_fields * 12, 0, 4);
// The index of the images, ISO 21496-1 for the gain map,
// and the gain curve of Apple.
if ((marker == APP2 && payload.size() >= 4 &&
!memcmp(payload.data(), "MPF\0", 4)) ||
(marker == APP2 && payload.size() >= sizeof kIsoNs &&
!memcmp(payload.data(), kIsoNs, sizeof kIsoNs)) ||
(marker == APP10 && payload.size() >= 5 &&
!memcmp(payload.data(), "AROT\0", 5))) {
result.insert(result.end(), kept, payload.data() - 4);
kept = payload.data() + payload.size();
}
}
result.insert(result.end(), kept, end);
return result;
}
vector<uint8_t>
jpeg_transform(span<const uint8_t> data, Orientation op, uint32_t x, uint32_t y,
uint32_t w, uint32_t h, Error *error)
@@ -811,7 +851,7 @@ jpeg_transform(span<const uint8_t> data, Orientation op, uint32_t x, uint32_t y,
if (error)
*error = {Error::Code::Open, tj3GetErrorStr(handle)};
} else
result.assign(output, output + length);
result = drop_stale_markers(output, length);
tj3Free(output);
if (handle)
tj3Destroy(handle);
+13 -6
View File
@@ -128,15 +128,16 @@ static void
test_metadata()
{
// A little-endian Exif orientation IFD, kept byte-for-byte through
// a rotation.
const uint8_t exif[] = {0xff, 0xe1, 0, 34, 'E', 'x', 'i', 'f', 0, 0, 'I',
'I', 42, 0, 8, 0, 0, 0, 1, 0, 0x12, 1, 3, 0, 1, 0, 0, 0, 7, 0, 0, 0, 0,
0, 0, 0};
// a rotation, except for the link to its thumbnail IFD1.
uint8_t exif[] = {0xff, 0xe1, 0, 52, 'E', 'x', 'i', 'f', 0, 0, 'I', 'I',
42, 0, 8, 0, 0, 0, 1, 0, 0x12, 1, 3, 0, 1, 0, 0, 0, 7, 0, 0, 0, 26, 0,
0, 0, 1, 0, 3, 1, 3, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0};
auto data = fixture("quads420.jpg");
data.insert(data.begin() + 2, begin(exif), end(exif));
dawn::Error error;
auto output = dawn::jpeg_transform(
data, dawn::Orientation::Rotate90, 0, 0, 0, 0, &error);
exif[32] = 0;
CHECK(search(output.begin(), output.end(), begin(exif), end(exif)) !=
output.end());
auto image = decode(output);
@@ -153,11 +154,11 @@ test_metadata()
CHECK(decode(output));
}
// The transform keeps the MPF index, but not the images past EOI.
// The transform keeps neither the images past EOI, nor what describes them.
static void
test_mpf()
{
auto data = fixture("gainmap.jpg");
auto data = fixture("gainmap-iso.jpg");
dawn::Error error;
dawn::JpegGrid grid;
CHECK(dawn::jpeg_grid(data, &grid, &error));
@@ -165,6 +166,12 @@ test_mpf()
auto output = dawn::jpeg_transform(
data, dawn::Orientation::Rotate0, 16, 16, 32, 16, &error);
CHECK(dawn::jpeg_grid(output, &grid, &error));
CHECK(!grid.mpf_images);
for (string_view id : {"MPF", "urn:iso:std:iso:ts:21496:-1"})
CHECK(search(output.begin(), output.end(), id.begin(), id.end()) ==
output.end());
dawn::OpenContext ctx;
ctx.gain_maps = true;
auto cropped = dawn::open_from_data(output, ctx, &error);