-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebpCodec.java
More file actions
58 lines (49 loc) · 2.1 KB
/
Copy pathWebpCodec.java
File metadata and controls
58 lines (49 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package me.tamkungz.codecmedia.internal.image.webp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import javax.imageio.ImageIO;
import me.tamkungz.codecmedia.CodecMediaException;
/**
* WebP image decode/encode bridge backed by {@link ImageIO}.
* <p>
* Note: standard JDK runtimes do not always include a WebP reader/writer SPI.
* A compatible plugin must be present in the runtime for decode/encode to work.
*/
public final class WebpCodec {
private WebpCodec() {
}
public static BufferedImage decode(Path input) throws CodecMediaException {
try {
BufferedImage image = ImageIO.read(input.toFile());
if (image == null) {
throw new CodecMediaException("No WEBP reader available in ImageIO runtime");
}
validateDecodedImage(image, input);
return image;
} catch (IOException e) {
throw new CodecMediaException("Failed to decode WebP: " + input, e);
}
}
public static void encode(BufferedImage image, Path output) throws CodecMediaException {
try {
boolean written = ImageIO.write(image, "webp", output.toFile());
if (!written) {
throw new CodecMediaException("No WEBP writer available in ImageIO runtime");
}
} catch (IOException e) {
throw new CodecMediaException("Failed to encode WebP: " + output, e);
}
}
private static void validateDecodedImage(BufferedImage image, Path input) throws CodecMediaException {
if (image.getWidth() <= 0 || image.getHeight() <= 0) {
throw new CodecMediaException("Decoded WebP has invalid dimensions: " + input);
}
if (image.getColorModel() == null || image.getColorModel().getPixelSize() <= 0) {
throw new CodecMediaException("Decoded WebP has invalid bit depth: " + input);
}
if (image.getRaster() == null || image.getRaster().getNumBands() <= 0) {
throw new CodecMediaException("Decoded WebP has invalid pixel channels: " + input);
}
}
}