Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1850 (JmeSystem.writeImageFile() throw java.nio.BufferUnderflowE… #1851

Merged
merged 1 commit into from
Sep 20, 2022
Merged
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
28 changes: 22 additions & 6 deletions jme3-desktop/src/main/java/com/jme3/system/JmeDesktopSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@
import com.jme3.audio.openal.ALC;
import com.jme3.audio.openal.EFX;
import com.jme3.system.JmeContext.Type;
import com.jme3.texture.Image;
import com.jme3.texture.image.ColorSpace;
import com.jme3.util.Screenshots;
import jme3tools.converters.ImageToAwt;

import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
Expand Down Expand Up @@ -80,30 +84,42 @@ private static BufferedImage verticalFlip(BufferedImage original) {
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -original.getHeight());
AffineTransformOp transformOp = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage awtImage = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_BGR);
BufferedImage awtImage = new BufferedImage(original.getWidth(), original.getHeight(), original.getType());
Graphics2D g2d = awtImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_SPEED);
g2d.drawImage(original, transformOp, 0, 0);
g2d.dispose();
return awtImage;
}


private static BufferedImage ensureOpaque(BufferedImage original) {
if (original.getTransparency() == BufferedImage.OPAQUE)
return original;
int w = original.getWidth();
int h = original.getHeight();
int[] pixels = new int[w * h];
original.getRGB(0, 0, w, h, pixels, 0, w);
BufferedImage opaqueImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
opaqueImage.setRGB(0, 0, w, h, pixels, 0, w);
return opaqueImage;
}

@Override
public void writeImageFile(OutputStream outStream, String format, ByteBuffer imageData, int width, int height) throws IOException {
BufferedImage awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Screenshots.convertScreenShot2(imageData.asIntBuffer(), awtImage);
BufferedImage awtImage = ImageToAwt.convert(new Image(Image.Format.RGBA8, width, height, imageData, ColorSpace.Linear), false, true, 0);
awtImage = verticalFlip(awtImage);

ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();

if (format.equals("jpg")) {
awtImage = ensureOpaque(awtImage);

JPEGImageWriteParam jpegParam = (JPEGImageWriteParam) writeParam;
jpegParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParam.setCompressionQuality(0.95f);
}

awtImage = verticalFlip(awtImage);

ImageOutputStream imgOut = new MemoryCacheImageOutputStream(outStream);
writer.setOutput(imgOut);
Expand Down