1

So currently I'm using C++ code to dump screenshots to the disk. I read them in using Java in order to crop them and then I write them back to the disk so C++ can load them back in and apply image algorithms (OpenCV).

After writing the modified image back from Java, I however get the following error in C++:

libpng error: PNG unsigned integer out of range

My cropping code in Java is as follows:

private void cropImage(Path imageFilePath) throws IOException
{
    File imageFile = imageFilePath.toFile();
    BufferedImage bufferedImage = ImageIO.read(imageFile);
    BufferedImage subImage = bufferedImage.getSubimage(x, y, width, height);
    BufferedImage copiedImage = new BufferedImage(subImage.getWidth(), subImage.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics graphics = copiedImage.createGraphics();
    graphics.drawImage(subImage, 0, 0, null);
    ImageIO.write(copiedImage, getExtension(imageFile.getName()), imageFile);
}

Surprisingly, if I view the cropped PNG it looks fine in the Windows Photo app but will not work in libpng. Without the cropping step, there's no libpng issue. Is there anything wrong with my PNG cropping code?

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • 3
    Why hop through all those hoops and not crop the image in OpenCV, then proceed from the cropped image? – RealSkeptic Feb 25 '20 at 14:10
  • As it works in other applications, perhaps the problem is your C++ code that uses libPNG? Possibly related: https://stackoverflow.com/q/22564718/1428606 – Harald K Feb 25 '20 at 15:02
  • @RealSkeptic: When I use `OpenCV` image processing in C++ instead I don't run into this error :) – BullyWiiPlaza Mar 11 '20 at 17:59

0 Answers0