I'm loading an image using C++ and feeding the pixels to JNI via a ByteBuffer. I know the pixels are being fed just fine because if the images are square, they render perfectly fine. If they are rectangular, they get distorted. I've also saved the Image back successfully in the DLL and it works. Java unfortunately gave up on me (unless it's square-like). I cannot figure out why! What am I doing wrong?
package library;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class Frame extends JFrame {
public Frame(int Width, int Height, String FrameName, BufferedImage Buffer) {
setName(FrameName);
setSize(Width, Height);
getContentPane().add(new JLabel(new ImageIcon(Buffer)));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
All the loading:
package library;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.nio.ByteBuffer;
class SharedLibrary {
static{System.loadLibrary("TestDLL");}
private static native void GetGLBuffer(ByteBuffer Buffer);
private ByteBuffer Buffer = null;
private int ByteSize = 0, Width = 0, Height = 0, BitsPerPixel = 32;
public SharedLibrary(int ImageWidth, int ImageHeight) throws IOException {
Width = ImageWidth;
Height = ImageHeight;
ByteSize = ((Width * BitsPerPixel + 31) / 32) * 4 * Height; //Compute Image Size in Bytes.
Buffer = ByteBuffer.allocateDirect(ByteSize); //Allocate Space for the image data.
GetGLBuffer(Buffer); //Fill the buffer with Image data from the DLL.
byte[] Bytes = new byte[ByteSize];
Buffer.get(Bytes);
BufferedImage Image = new BufferedImage(Width, Height, BufferedImage.TYPE_3BYTE_BGR);
WritableRaster raster = (WritableRaster) Image.getData();
raster.setPixels(0, 0, Width, Height, ByteBufferToIntBuffer(Bytes));
Image.setData(raster);
Frame F = new Frame(Width, Height, "", Image);
}
private int[] ByteBufferToIntBuffer(byte[] Data) {
int IntBuffer[] = new int[Data.length];
for (int I = 0; I < Data.length; I++) {
IntBuffer[I] = (int)Data[I] & 0xFF;
}
return IntBuffer;
}
}
The above Image Gets drawn perfectly because it is almost square. If I resize it to a rectangle, it gets distorted. Example:
Gets distorted and looks like: