Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ ImageIO
v
routing SPI
|
+-- detect the format once
+-- detect the format
+-- discover installed backend services
+-- run lightweight capability probes
+-- resolve ownership
Expand All @@ -305,6 +305,13 @@ The routing SPI is ordered before host readers, but returns `false` whenever no
added backend owns the input. Consequently it affects a host format only when
an installed backend has positively claimed that particular input.

Readers handed out by the routing SPI are always `RoutingImageReader`
instances. Each one resolves its owner from the input passed to `setInput`,
so a reader obtained by format name, suffix, or MIME type, or reused for a
second input, is routed exactly like one obtained from
`ImageIO.getImageReaders(input)`. `RoutingImageReader.backendId()` reports
which backend owns the current input.

Backend-to-backend `IIORegistry.setOrdering()`, shared format advertisements on
every backend SPI, global priority properties, and decode-time retry chains are
not part of this design.
Expand Down Expand Up @@ -425,6 +432,13 @@ BufferedImage frame = VideoFrameExtractor.extractFrame(
VideoInfo info = VideoFrameExtractor.getInfo(Path.of("clip.mp4"));
```

The AVFoundation and FFmpeg backends return frames as displayed: the
container's rotation (the metadata phones record for portrait video) is
applied, and `VideoInfo` reports the displayed width and height, so it always
matches the extracted frame. With FFmpeg, a frame requested at a time is the
first frame at or after it, decoded forward from the preceding keyframe;
AVFoundation picks a frame within its seek tolerance of the requested time.

| Module | Platform | Native API | Containers |
|--------|----------|------------|------------|
| `imageio-native-video-apple` | macOS | AVFoundation (AVAssetImageGenerator) | MP4, MOV, M4V, 3GP |
Expand Down Expand Up @@ -469,7 +483,7 @@ ImageIO.read(file)
▼
RoutingImageReaderSpi one SPI for all still-image backends
│
├── detect format once
├── detect format
├── discover installed backend services
├── probe actual input capability
└── resolve exactly one owner
Expand Down Expand Up @@ -521,9 +535,14 @@ processing and the worst-case thumbnail surfaces used for exact Apple/WIC
render sizes. Override the limit in bytes with
`-Dimageio.native.maxIntermediateBytes=<bytes>`.

Independently of read parameters, the Apple and libvips backends refuse to
decode an image larger than 256 megapixels, which guards against corrupt or
hostile files that declare huge dimensions. Override the limit with
`-Dimageio.native.maxPixels=<pixels>`.

### EXIF orientation

Images from phones and cameras often carry an EXIF orientation tag (values 1-8) that describes how the sensor image should be rotated or flipped for correct display. Both backends apply this transform automatically during decode so the returned `BufferedImage` is always display-ready.
Images from phones and cameras often carry an EXIF orientation tag (values 1-8) that describes how the sensor image should be rotated or flipped for correct display. Every still-image backend applies this transform automatically during decode, so the returned `BufferedImage` is display-ready and the same input has the same orientation whichever backend owns it.

| EXIF value | Transform |
|------------|-----------|
Expand All @@ -540,9 +559,13 @@ Images from phones and cameras often carry an EXIF orientation tag (values 1-8)

**Windows (WIC):** Reads the EXIF orientation tag via `IWICMetadataQueryReader`, then inserts an `IWICBitmapFlipRotator` between the frame decoder and the format converter. The flip-rotator is a zero-copy coordinate remap -- it transforms pixel coordinates during `CopyPixels` rather than allocating a second buffer. For orientation 1 (no rotation), the flip-rotator is skipped entirely.

Both `getSize()` and `decode()` are orientation-aware: dimensions are swapped for orientations 5-8 (90/270 rotations), so width and height always reflect the display-oriented image.
**libvips:** Applies `vips_autorot` to full and cropped decodes; `vips_thumbnail` already rotates reduced decodes. Upright images stream with sequential access, while an image that needs a transform is opened with random access because rotation cannot be streamed.

**ImageMagick:** Calls `MagickAutoOrientImage` after reading and before any resize or crop.

Size queries and decodes are orientation-aware on every backend: dimensions are swapped for orientations 5-8 (90/270 rotations), so width and height always reflect the display-oriented image.

**Performance impact:** Negligible. Both platforms apply the transform as part of the existing decode pipeline with no extra buffer allocations or pixel copies. The macOS path creates a small options dictionary per decode call; the Windows flip-rotator is a zero-copy coordinate remap. Orientation 1 (the common case for non-phone images) skips the transform entirely.
**Performance impact:** Negligible on the platform backends. Both platforms apply the transform as part of the existing decode pipeline with no extra buffer allocations or pixel copies. The macOS path creates a small options dictionary per decode call; the Windows flip-rotator is a zero-copy coordinate remap. Orientation 1 (the common case for non-phone images) skips the transform entirely. libvips and ImageMagick rotate in memory, so a rotated image costs one extra pass there.

## Project structure

Expand Down
166 changes: 0 additions & 166 deletions ROUTING.md

This file was deleted.

1 change: 1 addition & 0 deletions example-consumer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<!-- Surefire's JUnit launcher runs this fixture on the class path. -->
<argLine>--enable-native-access=ALL-UNNAMED</argLine>
</configuration>
</plugin>
Expand Down
8 changes: 6 additions & 2 deletions example-consumer/src/main/java/demo/DecodeDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
* <p>
* Usage:
* <pre>
* java --enable-native-access=ALL-UNNAMED -jar example-consumer.jar photo.heic clip.mp4
* java --module-path lib \
* --enable-native-access=io.github.ghosthack.imageio.apple,io.github.ghosthack.imageio.video.apple \
* --module io.github.ghosthack.imageio.example/demo.DecodeDemo photo.heic clip.mp4
* </pre>
*/
public class DecodeDemo {

public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.out.println("Usage: java --enable-native-access=ALL-UNNAMED -jar example-consumer.jar <file>...");
System.out.println("Usage: java --module-path lib \\");
System.out.println(" --enable-native-access=<backend-modules> \\");
System.out.println(" --module io.github.ghosthack.imageio.example/demo.DecodeDemo <file>...");
System.out.println();
System.out.println("Supports images (HEIC, AVIF, WEBP, JP2, RAW, ...) and video poster frames (MP4, MOV, ...)");
System.out.println();
Expand Down
4 changes: 4 additions & 0 deletions example-consumer/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module io.github.ghosthack.imageio.example {
requires io.github.ghosthack.imageio;
requires io.github.ghosthack.imageio.video;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

import javax.imageio.ImageReader;
import javax.imageio.spi.ImageReaderSpi;
import java.util.Locale;

/** Apple ImageIO backend service consumed by the common routing SPI. */
public final class AppleImageBackend implements ImageDecoderBackend {

private static final boolean IS_MACOS = System.getProperty("os.name", "")
.toLowerCase(Locale.ROOT).contains("mac");

@Override
public String id() {
return "apple";
Expand All @@ -26,12 +30,12 @@ public io.github.ghosthack.imageio.common.FormatRegistry formats() {

@Override
public boolean isAvailable() {
return AppleNative.IS_MACOS;
return IS_MACOS;
}

@Override
public boolean canDecode(byte[] header, int length) {
return AppleNative.canDecode(header, length);
return IS_MACOS && AppleNative.canDecode(header, length);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.ghosthack.imageio.apple;

import io.github.ghosthack.imageio.common.ImageReadParamSupport;
import io.github.ghosthack.imageio.common.NativeLimits;
import io.github.ghosthack.panama.media.cgimage.CGImageSource;
import io.github.ghosthack.panama.media.corefoundation.CoreFoundation;

Expand Down Expand Up @@ -51,12 +52,6 @@ static String unavailableMessage(String offMacMessage) {
: offMacMessage;
}

/**
* Maximum number of pixels (width * height) we are willing to decode.
* Prevents OOM on malicious/corrupt images declaring huge dimensions.
* Default: 256 megapixels (e.g. 16384 x 16384).
*/
static final long MAX_PIXELS = Long.getLong("imageio.native.maxPixels", 256L * 1024 * 1024);

// ── Helper: read an int property from a CFDictionary ────────────────

Expand Down Expand Up @@ -156,13 +151,11 @@ private static BufferedImage decodeFromSource(
if (rawW <= 0 || rawH <= 0)
throw new javax.imageio.IIOException("Invalid image dimensions: " + rawW + "x" + rawH);

long decodedPixels = renderSize != null
? (long) renderSize.width * renderSize.height
: (long) rawW * rawH;
if (decodedPixels > MAX_PIXELS)
throw new javax.imageio.IIOException(
"Decoded image too large: " + decodedPixels
+ " pixels exceeds limit of " + MAX_PIXELS);
if (renderSize != null) {
NativeLimits.requireDecodable(renderSize.width, renderSize.height);
} else {
NativeLimits.requireDecodable(rawW, rawH);
}

int maxDim = renderSize != null
? Math.max(renderSize.width, renderSize.height)
Expand Down
Loading
Loading