Skip to content

Commit

Permalink
Scrimage should output helper when image loading fails (#269)
Browse files Browse the repository at this point in the history
* Fix PNG image compression PNG image compression

* Added .exe to windows paths

* Scrimage should output helper when image loading fails #251
  • Loading branch information
sksamuel authored Jun 18, 2023
1 parent 69ba242 commit 664419e
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sksamuel.scrimage;

import com.sksamuel.scrimage.format.Format;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -14,7 +16,12 @@ public ImageParseException() {
}

public ImageParseException(List<Throwable> errors) {
super("Image parsing failed. Tried the following ImageReader implementations:\n" + errors.stream().map(Throwable::getMessage).collect(Collectors.joining("\n")));
super("Image parsing failed for unknown image format. Tried the following ImageReader implementations:\n" + errors.stream().map(Throwable::getMessage).collect(Collectors.joining("\n")));
this.errors = errors;
}

public ImageParseException(List<Throwable> errors, Format format) {
super("Image parsing failed for " + format + ". If the format is `webp` ensure you have a webp reader on your classpath, such as the `scrimage-webp` module. Tried the following ImageReader implementations:\n" + errors.stream().map(Throwable::getMessage).collect(Collectors.joining("\n")));
this.errors = errors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public ImmutableImage read(byte[] bytes, Rectangle rectangle) throws IOException
else
throw new IOException("No javax.imageio.ImageReader supported this image format; tried " + attempts.size() + " readers; errors=" + attempts);
}

@Override
public String toString() {
return "com.sksamuel.scrimage.nio.ImageIOReader (delegates to the JDK javax.imageio readers)";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.sksamuel.scrimage.ImageParseException;
import com.sksamuel.scrimage.ImmutableImage;
import com.sksamuel.scrimage.format.Format;
import com.sksamuel.scrimage.format.FormatDetector;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -37,15 +40,16 @@ public static ImmutableImage read(ImageSource source, Rectangle rectangle, Class
/**
* Attempts to read an image from the given source, using the supplied image readers.
*
* @param source the image source
* @param source the image source
* @param rectangle an optional subset of the image to read.
* @param readers the readers that should be used to attempt to load this image.
* @param readers the readers that should be used to attempt to load this image.
*/
public static ImmutableImage read(ImageSource source, Rectangle rectangle, List<ImageReader> readers) throws IOException {
List<Throwable> errors = new ArrayList<>();
byte[] bytes = source.read();
for (ImageReader reader : readers) {
try {
ImmutableImage image = reader.read(source.read(), rectangle);
ImmutableImage image = reader.read(bytes, rectangle);
if (image == null) {
errors.add(new IOException(reader + " failed"));
} else {
Expand All @@ -55,9 +59,10 @@ public static ImmutableImage read(ImageSource source, Rectangle rectangle, List<
errors.add(new IOException(reader.toString() + " failed due to " + e.getMessage(), e));
}
}
if (errors.isEmpty())
throw new ImageParseException();
else
Format format = FormatDetector.detect(bytes).orElse(null);
if (format == null)
throw new ImageParseException(errors);
else
throw new ImageParseException(errors, format);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public List<ImmutableImage> readAll(byte[] bytes) throws IOException {
}
return images;
}

@Override
public String toString() {
return "com.sksamuel.scrimage.nio.OpenGifReader (delegates to at.dhyan.open_imaging)";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,9 @@ private boolean isPng(byte[] bytes) {
System.arraycopy(bytes, 0, actual, 0, 8);
return Arrays.equals(expected, actual);
}

@Override
public String toString() {
return "com.sksamuel.scrimage.nio.PngReader (delegates to ar.com.hjg.pngj)";
}
}
6 changes: 3 additions & 3 deletions scrimage-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ dependencies {
implementation(project(":scrimage-core"))
testImplementation(kotlin("stdlib"))
testImplementation(kotlin("stdlib-jdk8"))
testImplementation("io.kotest:kotest-framework-datatest:5.5.4")
testImplementation("io.kotest:kotest-runner-junit5:5.5.4")
testImplementation("io.kotest:kotest-assertions-core:5.5.4")
testImplementation("io.kotest:kotest-framework-datatest:5.5.5")
testImplementation("io.kotest:kotest-runner-junit5:5.5.5")
testImplementation("io.kotest:kotest-assertions-core:5.5.5")
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package com.sksamuel.scrimage.core

import com.sksamuel.scrimage.format.Format
import com.sksamuel.scrimage.format.FormatDetector
import com.sksamuel.scrimage.nio.ImmutableImageLoader
import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain

class FormatDetectorTest : WordSpec({

Expand All @@ -22,4 +25,12 @@ class FormatDetectorTest : WordSpec({
}
}

"image loader" should {
"output webp warning" {
shouldThrowAny {
ImmutableImageLoader.create().fromResource("/spacedock.webp")
}.message shouldContain "Image parsing failed for WEBP"
}
}

})
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MultipleImageLoaderExceptionErrorTest : FunSpec() {
val e = shouldThrowAny {
ImmutableImageLoader.create().load(ImageSource.of(bytes))
}
e.message shouldContain """Image parsing failed. Tried the following ImageReader implementations"""
e.message shouldContain """Image parsing failed for unknown image format. Tried the following ImageReader implementations"""
e.message shouldContain """com.sksamuel.scrimage.nio.ImageIOReader"""
e.message shouldContain """com.sksamuel.scrimage.nio.PngReader"""
e.message shouldContain """com.sksamuel.scrimage.nio.OpenGifReader"""
Expand Down
Binary file added scrimage-tests/src/test/resources/spacedock.webp
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ protected static String[] getBinaryPaths(String binaryName) {
if (SystemUtils.IS_OS_WINDOWS) {
return new String[]{
"/webp_binaries/" + binaryName,
"/webp_binaries/" + binaryName + ".exe",
// typo from previous versions must be left in
"/webp_binaries/window/" + binaryName,
"/webp_binaries/window/" + binaryName + ".exe",
"/webp_binaries/windows/" + binaryName,
"/webp_binaries/windows/" + binaryName + ".exe",
"/dist_webp_binaries/libwebp-1.3.0-windows-x64/bin/" + binaryName,
"/dist_webp_binaries/libwebp-1.3.0-windows-x64/bin/" + binaryName + ".exe",
};
} else if (SystemUtils.IS_OS_MAC) {
return new String[]{
Expand Down

0 comments on commit 664419e

Please sign in to comment.