de.digitalcollections.turbojpeg.Info Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of imageio-turbojpeg Show documentation
Show all versions of imageio-turbojpeg Show documentation
ImageIO plugin for reading and writing JPEG images via libjpeg-turbo/turbojpeg.
Requires the libjpeg-turbo and turbojpeg shared native libraries to be installed on the system.
package de.digitalcollections.turbojpeg;
import de.digitalcollections.turbojpeg.lib.structs.tjscalingfactor;
import java.awt.Dimension;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Info {
private int width;
private int height;
private int subsampling;
List availableSizes;
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getSubsampling() {
return subsampling;
}
public List getAvailableSizes() {
return availableSizes;
}
private static int getScaled(int dim, int num, int denom) {
return (dim * num + denom - 1) / denom;
}
public Info(int width, int height, int subsampling, tjscalingfactor[] factors) {
this.width = width;
this.height = height;
this.subsampling = subsampling;
this.availableSizes = Arrays.stream(factors)
.sorted(Comparator.comparing(f -> -getScaled(width, f.num.get(), f.denom.get())))
.map(f -> new Dimension(getScaled(width, f.num.get(), f.denom.get()),
getScaled(height, f.num.get(), f.denom.get())))
.filter(d -> d.width <= width && d.height <= height && d.width >= 8 && d.height >= 8)
.distinct()
.collect(Collectors.toList());
}
public Dimension getMCUSize() {
switch (subsampling) {
case 1: // 4:2:2
return new Dimension(16, 8);
case 2:
return new Dimension(16, 16);
case 4:
return new Dimension(8, 16);
default:
return new Dimension(8, 8);
}
}
}