4

I have a program which rescales images using the mortennobel image library for the rescale filter/algorithm. I'm using the Java standard library ImageIO to read the file into a BufferedImage object. However the trouble I'm having is that the ImageIO library only accepts standard forms of images and so if the ImageIO.read() function throws an IOException I want to catch it and convert the image into a standard form such as a JPEG.

Just to clarify, the problem I'm having is that some of the images are not of a standard JPEG format i.e. FF D8 FF E0 JFIF, instead they have been produced by a digital camera or edited in photoshop and the format is FF D8 FF E1 Exif. It's possible to change this format by loading the image into a program like paint and saving again as JPEG however its not ideal for my situation as I'd like it to be automated.

Convert from: FF D8 FF E1 Exif to FF D8 FF E0 JFIF

trooper
  • 4,444
  • 5
  • 32
  • 32
Alexei Blue
  • 1,762
  • 3
  • 21
  • 36

1 Answers1

3

The first field after FFE0 marker is the length field. It's 16 bits long big endian and includes the length of the length field but not the FFE0 marker. Just copy the FFE0 marker and the associated data from an existing jpeg and fit it in between FFD8 and FFE1.

According to the JFIF standard, the JFIF header should follow immediately after SOI (FFD8). Every jpeg without a JFIF header is essentially broken as there's no way of knowing what color coding is used. Of course there's the Adobe jpeg's but they don't really count. If there's a JFIF header, you know that the image is coded in YCbCr. If there's no JFIF header, you could maybe assume it's YCbCr, but you can't know for sure.

You can read more about the JFIF standard here: http://www.w3.org/Graphics/JPEG/jfif3.pdf

onemasse
  • 6,514
  • 8
  • 32
  • 37
  • Hey thanks for your answer, is the value always the same for this marker? Any chance you could post it if it is? Once I know what it is I'm guessing I just read the file in as a normal data file until I reach the 16th bit, insert the marker and continue with the rest of the file? :) Sorry for the late reply, been busy with some other projects for a little while. – Alexei Blue Nov 04 '11 at 17:52
  • 1
    Just copied from a file the values are "ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00". The contents of this marker is almost always the same. If you want to know what it means, you should read the document I linked to, it's only nine pages. – onemasse Nov 04 '11 at 20:00
  • It took me a while to figure out how to copy those additional tags in. The image still works and loads but the ImageIO library still doesn't accept it. Looking at the hex values the photo was taken and then edited in Photoshop which is what caused it to be in this Exif format. Any other ideas on how I could get the library to accept it? Cheers in advance – Alexei Blue Nov 11 '11 at 13:53
  • 1
    OK, then we come back to the topic of Adobe that I mentioned in my answer. It might be that Photoshop saves the JPEG in a non JFIF format. You need to save your edited file as an "internet" file, otherwise Photoshop might save it as an ARGB, CMYK or YCCB file. – onemasse Nov 11 '11 at 14:07
  • That's were the problem is, I have no control over the image creation, they're just passed to me. I re-saved the image in paint as JPEG which performed the same change you gave in your answer and also what looks to be a change on the colour model, by doing this ImageIO accepted the file. I don't suppose you know any Java code that performs this "save as" action and in turn converts it to the format I am after? – Alexei Blue Nov 11 '11 at 15:51
  • I tried posting on the Adobe forums to see if they code help me convert it from their Exif format into a regular format but no look yet. However the answer above would work given the file was in the right colour map as you suggested. :) Cheers onemasse – Alexei Blue Nov 29 '11 at 12:52
  • Alexei Blue, did you get to handle this problem? – Ramon Chiara Jan 05 '15 at 14:42