-2

I'm trying to compress an image using Run Length encoding and SAVE it as JPEG.

But when i try to read BufferedImage from BYteArrayInputStream the program fails, returning following at line below (BufferedImage bImage2 = ImageIO.read(bis);) cus the ImageIO.read(bis) returns null - bis is not null:

Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at javax.imageio.ImageIO.getWriter(ImageIO.java:1592)
at javax.imageio.ImageIO.write(ImageIO.java:1520)
at Compression.main(Compression.java:17)

Here is the code I'm currently using:

File input = new File("testEmily.jpeg");
    BufferedImage image = ImageIO.read(input);
    byte[] byteArray = RunLength.toByteArrayAutoClosable(image, "jpeg");
    byte[] compressedBytes2 = RunLength.compress(byteArray);
    ByteArrayInputStream bis = new ByteArrayInputStream(compressedBytes2);
    BufferedImage bImage2 = ImageIO.read(bis);
    ImageIO.write(bImage2, "jpeg", new File("compressedEmily.jpeg") );

Also this is github repo link if someone wants to reproduce - https://gitlab.com/Avivi/boniekrunlength

miyav miyav
  • 338
  • 1
  • 5
  • 18
  • 1
    Please read [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Sudhir Ojha May 06 '19 at 12:27
  • 1
    and *what* is being returned by `RunLength.compress`? Is it a valid image, recognized by `ImageIO` (correct header, encoding and such)? Documentation of [ImageIO.read](https://docs.oracle.com/en/java/javase/12/docs/api/java.desktop/javax/imageio/ImageIO.html#read(java.io.InputStream)): "*If no registered ImageReader claims to be able to read the resulting stream, null is returned.*" – user85421 May 06 '19 at 12:31
  • Yes this is valid image i was able to compress it using this example: https://examples.javacodegeeks.com/desktop-java/imageio/compress-a-jpeg-file/ but wanted to do run length encoding. The RunLenght compress you could check at github repo i linked(comment is too short for the code) but it returns byte[] – miyav miyav May 06 '19 at 12:36
  • It doesn't make the least particle of sense to run-length-encode an image and then expect to be able to read it as an image. Did you mean to run-length *decode* it? – user207421 May 06 '19 at 12:37
  • You certainly have not registered an `ImageReader` to read images compressed with that algorithm, have you? Or is it one of the recognized ones, like JPEG or PNG?As I understand it is eventually changing the header without adding a valid (recognized) one. – user85421 May 06 '19 at 12:37
  • @user207421 So you cant load run-length-encoded image like a normal image? You always have to decode it? If so i didn't know that and thanks for telling me that. – miyav miyav May 06 '19 at 12:40
  • 1
    You cannot rationally expect to be able to read *any* encoding as an image if the result of the encoding isn't an image, and the evidence here is unambiguously that it wasn't. What exactly is the point of this code anyway? You already had an image when you started, and you're trying to end up with another one. Why not just copy the bytes? And JPEG is already compresse: If you want more compression , just alter the `q` factor. You can't just invent your own encoding and expect everybody to be able to j der stand it. Life's not like that. – user207421 May 06 '19 at 12:45
  • 2 options: 1) decode the bytes and then feed `ImageIO.read` 2) write and register an `ImageReader` to read data encoded with `RunLength` - but don't expect much - JPEG is already compressed (better results expected with uncompressed images like BMP) – user85421 May 06 '19 at 12:50

1 Answers1

1

The line BufferedImage image = ImageIO.read(input); probably returned null. This could be caused by a corrupt image file or if the file is not found. Make sure, that the file is read correctly and check, if the returned value of ImageIO.read(...) is non-null.

From the documentation of ImageIO.read()

The File is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned.

Jannik
  • 1,583
  • 1
  • 14
  • 22
  • No the line below BufferedImage bImage2 = ImageIO.read(bis); returned null and bis is not null fixed the question to point out where null is happening. – miyav miyav May 06 '19 at 12:28
  • @miyavmiyav Nobody suggested that `bis` was null, and it is of course impossible. Your suggestion that `ImageIO.read()` didn't return null is ridiculous. – user207421 May 06 '19 at 12:40
  • Ok, I see. As far as I understand your code you compress the image and then create an input stream from the compressed image bytes. Then you try to decode this stream into an image which does not work, because the compressed image is no valid image file. Do you just want to save to compressed data for later or do you want to save a valid image file which can be opened with a normal image viewing program? – Jannik May 06 '19 at 12:41
  • @user207421 i dont know why are you so aggressive. I just phrased it wrongly i meant it failed due to null in line below. Also the comment sugested it returned null on line (BufferedImage image = ImageIO.read(input);) which it didnt it returned null on second ImageIO.read which is 4 lines below. – miyav miyav May 06 '19 at 12:46
  • @Jannik i wanted to save compressed data as jpeg so i can view how it look compressed kind of like in this example (https://examples.javacodegeeks.com/desktop-java/imageio/compress-a-jpeg-file/) but with run length encoding (now i dont know if its even possible using run length encdoing) – miyav miyav May 06 '19 at 12:49
  • @miyavmiyav Thats not really possible, because you don't even know, how big the run-length encoded image is. So, if your image has a uniform color, it would be very small (i.e. few pixels) and if the image varies heavily in color you would get many pixels. – Jannik May 06 '19 at 12:54
  • @miyavmiyav You stated, and I quote, 'the line below `BufferedImage bImage2 = ImageIO.read(bis)` returned null', which was wrong, as it didn't return anything: it threw an exception. This is a long way from merely 'phrasing it wrongly'. Don't misuse standard terminology, and don't be surprised when people pull you up on it. Expressing yourself clearly follows directly from thinking clearly, and you will never learn to debug your own code wihout both. – user207421 May 06 '19 at 12:56
  • @Jannik thank you thats what i needed - that i need to understand the encoding more. – miyav miyav May 06 '19 at 13:01
  • @user207421 Actually the line `BufferedImage bImage2 = ImageIO.read(bis)` returned null and the next line threw an error due to the null argument (line 17 is the line with `ImageIO.write(...)`) – Jannik May 06 '19 at 13:03
  • @user207421 Yes i know it returned null and the line beloew threw exception Jesus man you really need to chill and when i said i phrased it wrongly i mean it and i did phrase it wrongly. People like you really are the ones that make asking questions stresfull here and why new people are not enjoying asking questions whatsoever. If people knew everything they wouldn't be asking. – miyav miyav May 06 '19 at 13:05