0

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;
    }
}

enter image description here The above Image Gets drawn perfectly because it is almost square. If I resize it to a rectangle, it gets distorted. Example:

enter image description here

Gets distorted and looks like: enter image description here

Brandon
  • 22,723
  • 11
  • 93
  • 186
  • 1
    Are you sure this has to do with the image being square, and not some other aspect of the dimensions? For example, I notice that your "almost square" image also has dimensions that are divisible by 8 (and one is even divisible by 16) but the image that doesn't work has dimensions that are odd (ie: are not divisible by 2). – Laurence Gonsalves Oct 07 '12 at 22:11
  • I made the image in MS Paint :S EDIT: You're right! It works when it is divisible :O Is there anyway I can pad it in Java? It works on the C++ side. Or should I pad it on the C++ side before sending it to Java? – Brandon Oct 07 '12 at 22:25
  • 1
    It's also flipped top-for-bottom, corresponding to [*Java Screen Coordinates*](http://www.leepoint.net/notes-java/GUI-lowlevel/graphics/10coord.html). – trashgod Oct 07 '12 at 23:10
  • Yes, you could add padding... You'd just need to allocate a buffer that's been padded out the right amount, and then skip ahead the right number of bytes whenever you read the end of a scan-line. Are you sure the issue is that Java expects padding, and not that your data contains unexpected padding, however? The direction of skew suggests the latter to me, but perhaps I'm getting confused by it being flipped vertically as well (as @trashgod points out). Either way, you can correct this on the Java side once you figure out exactly what's going on. – Laurence Gonsalves Oct 08 '12 at 23:05
  • @LaurenceGonsalves: I've seen padding vary among C compilers; this [approach](http://stackoverflow.com/a/9373195/230513) may be applicable in flipping vertically. – trashgod Oct 09 '12 at 00:58

0 Answers0