0

Have been trying to scale down a view to different scale but i'm having some troubles. The context is inside a video player (using VLCJ).

This code is being used to scale the image but after the first frame the image stops updating with next image for some reason.

public void newFrameRecieved(int[] rgbBuffer)
{
    this.image.setRGB(0, 0, this.size.width, this.size.height, rgbBuffer, 0, this.size.width);


    after = new BufferedImage(this.getWidth(), this.getHeight(), this.image.getType());
    at = new AffineTransform();
    scalingFactorX = (double)this.getWidth()/ (double)this.image.getWidth();
    scalingFactorY = (double)this.getHeight()/ (double)this.image.getHeight();
    at.scale(scalingFactorX, scalingFactorY);
    scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
    this.image = scaleOp.filter(this.image, after);


    repaint();
}

If the middle part (all code between this.image.setRGB(......) and repaint();) is removed then it works(except the scaling part...)

Would really appreciate any help you guys can offer! The error recieved is the following:

JNA: Callback uk.co.caprica.vlcj.player.direct.DefaultDirectMediaPlayer$DisplayCallback@4516af24 threw the following exception:
java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
    at sun.awt.image.IntegerInterleavedRaster.setDataElements(IntegerInterleavedRaster.java:301)
    at java.awt.image.BufferedImage.setRGB(BufferedImage.java:1059)
    at myVlcjWrapper.VideoSurfacePanel.newFrameRecieved(VideoSurfacePanel.java:65)

I understand the problem I'm just clueless as to how to fix it! Thanks!

Payerl
  • 1,042
  • 2
  • 16
  • 33
  • how large is your image you are trying to scale? in pixels – D3181 Nov 18 '16 at 13:39
  • What does `image.setRGB` do? – bradimus Nov 18 '16 at 13:40
  • Since it's a video stream in reality I can't say I'm sertain.. But I am pretty sure it's 1920x1080! – Payerl Nov 18 '16 at 17:10
  • Simplified image.setRGB writes the buffer to the image. – Payerl Nov 18 '16 at 17:23
  • The stack trace clearly shows it's the `setRGB` invocation that throws the exception... So I think you "accidentally" fixed a mistake somewhere, in addition to commenting out the code, when it "worked". – Harald K Nov 18 '16 at 20:56
  • No I'm sertain I only commented it out! Also it works the first time data is passed through the buffer.. I was thinking if my code for some reason modified the source image with the line this.image = scaleOp.filter(....); – Payerl Nov 18 '16 at 21:12
  • 1
    I don't doubt you're certain. ;-) I'm just telling you that the stack trace says otherwise. You don't get past the first line in your method. The problem is that your `size` member variable doesn't match the dimensions of what is passed in as `rgbBuffer`. – Harald K Nov 20 '16 at 18:21

1 Answers1

0

After doing alot of error searching I ended up with this piece of working code!

public void newFrameRecieved(int[] rgbBuffer, Dimension max)
{
    before = new BufferedImage(max.width, max.height, BufferedImage.TYPE_INT_ARGB);
    before.setRGB(0, 0, max.width, max.height, rgbBuffer, 0, max.width);
    after = new BufferedImage(this.getWidth(), this.getHeight(), this.image.getType());
    at = new AffineTransform();
    scalingFactorX = (double)after.getWidth()/ (double)before.getWidth();
    scalingFactorY = (double)after.getHeight()/ (double)before.getHeight();
    at.scale(scalingFactorX, scalingFactorY);
    scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
    this.image = scaleOp.filter(before, after);

    repaint();
}

It's a minimal change but it did the trick! Thought it best to post it in case anyone ever finds their way here again. Thanks for the help guys! :)

Payerl
  • 1,042
  • 2
  • 16
  • 33