com.github.bloodshura.ignitium.assets.ext.image.JimiCodec Maven / Gradle / Ivy
Show all versions of ignitium-assets-ext Show documentation
package com.github.bloodshura.ignitium.assets.ext.image;
import com.github.bloodshura.ignitium.assets.image.ImageFormat;
import com.github.bloodshura.ignitium.assets.image.codec.ImageCodec;
import com.github.bloodshura.ignitium.assets.util.ImageWorker;
import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiControl;
import com.sun.jimi.core.JimiException;
import javax.annotation.Nonnull;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class JimiCodec implements ImageCodec {
static {
JimiControl.addExtension(new JimiAdditionalExtension());
}
@Nonnull
public String getJIMIMimeType(@Nonnull ImageFormat format) {
return format.getMimeTypes().first();
}
/**
* Already implemented decoders:
* BMP, CUR, GIF, ICO, JPG (Builtin), PCT, PCX, PNG, PSD, RAS (SunRaster), TGA, TIF, XBM, XPM
*
* Additional decoders:
* APF
*/
@Override
public boolean hasDecoder(@Nonnull ImageFormat format) {
return format == ImageFormat.APF || format == ImageFormat.BMP || format == ImageFormat.CUR || format == ImageFormat.GIF || format == ImageFormat.JPG || format == ImageFormat.PCT || format == ImageFormat.PCX || format == ImageFormat.PNG || format == ImageFormat.PSD || format == ImageFormat.RAS || format == ImageFormat.TGA || format == ImageFormat.TIFF || format == ImageFormat.XBM
|| format == ImageFormat.XPM;
}
/**
* Already implemented encoders:
* BMP, JPG, PCT, PCX, PNG, PSD, RAS (SunRaster), TGA, XBM, XPM
*
* Additional encoders:
* APF, CUR
*/
@Override
public boolean hasEncoder(@Nonnull ImageFormat format) {
return hasDecoder(format) && format != ImageFormat.GIF && format != ImageFormat.ICO /* JIMI has ICOEncoder, but not ICOEncoderFactory. Possibly implement it? */ && format != ImageFormat.TIFF;
}
@Nonnull
@Override
public BufferedImage read(@Nonnull InputStream input) throws IOException {
try {
return ImageWorker.asBufferedImage(Jimi.getImage(input));
} catch (Exception exception) {
throw new IOException(exception.getMessage(), exception.getCause());
}
}
@Override
public void write(@Nonnull BufferedImage image, @Nonnull ImageFormat format, @Nonnull OutputStream output) throws IOException {
try {
Jimi.putImage(getJIMIMimeType(format), image, output);
} catch (JimiException exception) {
throw new IOException("ihuuuu" + exception.getMessage(), exception.getCause());
}
}
}