开发者

How to write a TIFF from a 2D array of floats in Java?

开发者 https://www.devze.com 2023-03-08 16:10 出处:网络
In a Java program I have a开发者_JS百科 1024 x 1024 array of floats.How can I write a TIFF file corresponding to the image represented by this array?

In a Java program I have a开发者_JS百科 1024 x 1024 array of floats. How can I write a TIFF file corresponding to the image represented by this array?

Clarifications:

  1. I am asking for a code snippet illustrating how to write a TIFF corresponding to the array of floats.
  2. I'm looking for a grayscale image.
  3. I know how to convert the 1024 x 1024 array of floats into any other 1024 x 1024 array of numerical values; e.g. if the method you have in mind requires, say, 1024 x 1024 floats in the range [0, 1.0), no problem, I know how to convert my data so that this constraint holds.

Thanks!

kjo


The problem that you will have is that, while it is possible to have floating point values for pixel data in TIFF, this is not part of the baseline specification. TIFF is a mushy enough spec to allow floating point samples, but not to standardize their semantic meaning. For example, I had a customer who had floating point samples generated by a Java app (using ImageJ, I believe) and expected us to read them correctly. ImageJ had put in a badly serialized hashtable into one of the description strings so I had to give them code that would work for that sample file but probably for no others. Don't be that Java app. And if you're going to use ImageJ to write floating point TIFFs, normalize your data between 0 and 1, because then I can guarantee that at least my tools will read it correctly without depending on semantic meaning.

While the baseline spec says that 16 bit per channel samples aren't part of the baseline, they are more likely to be be recognized by current TIFF consumers. So you might be happier in the long run writing grayscale with 16-bit samples in the range 0..65535, if you're hell-bent on writing TIFF.

If you think that you're going to write a non-compliant TIFF, just write your own file format and publish the spec and the reading and writing code. If you shoe-horn it into TIFF, you are creating a new format anyway and you will break most TIFF consuming applications as a side-effect. Which is better for the ecosystem?

Remember, when you write a bad TIFF, an angel gets set on fire.


AFAIU JAI can write TIFF files.


The canonical standard for handling TIFF images is the libtiff library, which is written in C. It is possible to call native C code from Java.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号