2

I am trying to convert a CGM file to PNG using below function. I am keep getting NullPointerException.

public File convertCGMtoPNG(File cgmFile, File directoryToCreatePNG, 
                            String nameSuffix)  throws IOException 
{       
    File pngFile = new File(directoryToCreatePNG.getAbsolutePath() + 
                            File.separator +                         
        cgmFile.getName().substring(0,cgmFile.getName().lastIndexOf(".cgm")) +
                    "_" + nameSuffix + ".png");         
    BufferedImage image = ImageIO.read(cgmFile);
        ImageIO.write(image, "PNG", pngFile);
        return pngFile;
}

Below is the Exception i am facing

java.lang.IllegalArgumentException: image == null! at java.desktop/javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
    at java.desktop/javax.imageio.ImageIO.getWriter(ImageIO.java:1608)
    at java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1540)

I tried to debug the code and came to know that the CGM format is not supported by ImageIO (CanDecodeInputFilter returns false for my CGM). Any idea on how to convert a CGM to PNG image.

PS: I am using java 11 for my development.

Thanks in advance

Update

Based on the replies I tried jcgm libraries(added the core and image libraries to my classpath). It passes through the "image == null!" error but now throwing below error

java.lang.IllegalArgumentException: width*height > Integer.MAX_VALUE!

at java.desktop/javax.imageio.ImageReader.getDestination(ImageReader.java:2821)
at net.sf.jcgm.imageio.plugins.cgm.CGMImageReader.read(Unknown Source)
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1468)
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1315)

Still struggling to make it work.

Krishnom
  • 1,348
  • 12
  • 39
  • Is 'image' null? Does ImageIO support cgm files? If so, you should be able to check some of the image properties, like height and width. – matt Jun 25 '19 at 16:11
  • 1
    If CGM is not supported you need another image reading routine. – Thorbjørn Ravn Andersen Jun 25 '19 at 16:14
  • yes, the image variable is null. as I have mentioned, imageIO doesn't seem to support CGM. but here is a link that says it works http://jcgm.sourceforge.net/howto.html. but when I tried it doesn't work. any idea how to achieve it? – Krishnom Jun 25 '19 at 16:15
  • 1
    You need to use the jcgm library. – matt Jun 25 '19 at 16:51
  • @matt i just tried the jcgm library (just by adding the jars in my classpath) but it throws another error as updated in question – Krishnom Jun 25 '19 at 17:15
  • @ThorbjørnRavnAndersen I am trying jcgm right now but still struggling. Do we have any other way to covert CGM to PNG that I can give a try? – Krishnom Jun 25 '19 at 17:22
  • 1
    Are you sure that your cgm file is a valid cgm file? It looks like the ImageIO.read should work. – matt Jun 25 '19 at 17:24
  • @matt Yupp look like some issue with my CGM. The code works fine with sample cgm files in jcgm library. Will check my cgm. Not sure how to get rid of that **width*height > Integer.MAX_VALUE!** error. any Idea? – Krishnom Jun 25 '19 at 18:14
  • 2
    @Krishnom The problem you are facing, is that you try to render into something too large for a standard `BufferedImage`. If JCGM has an option to render at a smaller size (preferably using `ImageReadParam.setSourceRenderSize(..)` or alternatively `ImageReadParam.setSourceSubsampling(...)`) you should be fine. Reading a part of the image, using `ImageReadParam.setSourceRegion(..)` may also work. – Harald K Jun 25 '19 at 18:54

1 Answers1

0

ImageIO.read(*...) will only load these image types GIF, PNG, JPEG, BMP, and WBMP.

Any other image type will return null without error.

reference: http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

I do realize this is not a solution to the specific original problem but it is a solution to the question asked.