简体   繁体   English

imageIO 打开 .HDR 文件

[英]imageIO to open .HDR file

I need to open an .hdr file and work on it, but imageIO doesn't supports that format.我需要打开一个 .hdr 文件并对其进行处理,但 imageIO 不支持该格式。

The problem is that I need to keep the information loss as little as possible: 32bpc is perfect, 16 is fine and less the 16 won't work.问题是我需要尽可能少地保持信息丢失:32bpc 是完美的,16 是可以的,而 16 则不工作。

There are 3 possible solutions I came up to:我想出了 3 种可能的解决方案:

  1. Find a plugin that allow me to open .HDR file.找到一个允许我打开 .HDR 文件的插件。 I've been searching for it a lot but without luck;我一直在寻找它,但没有运气;
  2. Find a way to convert the .HDR file to a format I can find a plugin for.找到一种方法将 .HDR 文件转换为我可以找到插件的格式。 Tiff maybe?蒂夫也许? Tried this too but still no luck;也试过这个,但仍然没有运气;
  3. Reduce the dynamic range from 32bpc to 16bpc and then convert it to png.将动态范围从 32bpc 减少到 16bpc,然后将其转换为 png。 This is tricky because once I have a png file I win, but it's not that easy to cut the range without killing the image..这很棘手,因为一旦我有一个 png 文件我就赢​​了,但是在不杀死图像的情况下切割范围并不是那么容易。

What would you recommend me to do?你会推荐我做什么? Do you know a way to make one of those 3 options works?您知道一种方法可以使这 3 个选项中的一个起作用吗? Or do you have a better idea?或者你有更好的主意吗?

You can now read .HDR using ImageIO .您现在可以使用 ImageIO 读取 .HDR :-) :-)

This is a first version, so it might be a little rough around the edges, but should work for standard (default settings) Radiance RGBE .HDR files.这是第一个版本,所以它的边缘可能有点粗糙,但应该适用于标准(默认设置)Radiance RGBE .HDR文件。

The returned image will be a custom BufferedImage with a DataBufferFloat backing (ie., samples will be in 3 samples, 32-bit float interleaved RGB format).返回的图像将是带有DataBufferFloat支持的自定义BufferedImage (即,样本将采用 3 个样本,32 位float交错 RGB 格式)。

By default, a simple global tone-mapping is applied, and all RGB values will be normalized to range [0...1] (this allows anyone to just use ImageIO.read(hdrFile) and the image will look somewhat reasonable, in a very reasonable time).默认情况下,应用简单的全局色调映射,并且所有 RGB 值都将归一化到范围 [0...1](这允许任何人只使用ImageIO.read(hdrFile)并且图像看起来有些合理,在一个非常合理的时间)。

It is also possible to pass an HDRImageReadParam to the ImageReader instance with a NullToneMapper .也可以到传递HDRImageReadParamImageReader实例与NullToneMapper This is even faster, but the float values will be unnormalized, and might exceed the max value.这甚至更快,但float值将不规范化,并且可能超过最大值。 This allows you to do custom, more sophisticated tone-mapping on the image data, before converting to something more displayable.这允许您在转换为更可显示的内容之前,对图像数据进行自定义的、更复杂的色调映射。

Something like:就像是:

// Create input stream
ImageInputStream input = ImageIO.createImageInputStream(hdrFile);

try {
    // Get the reader
    Iterator<ImageReader> readers = ImageIO.getImageReaders(input);

    if (!readers.hasNext()) {
        throw new IllegalArgumentException("No reader for: " + hdrFile);
    }

    ImageReader reader = readers.next();

    try {
        reader.setInput(input);

        // Disable default tone mapping
        HDRImageReadParam param = (HDRImageReadParam) reader.getDefaultReadParam();
        param.setToneMapper(new NullToneMapper());

        // Read the image, using settings from param
        BufferedImage image = reader.read(0, param);
    }
    finally {
        // Dispose reader in finally block to avoid memory leaks
        reader.dispose();
    }
}
finally {
    // Close stream in finally block to avoid resource leaks
    input.close();
}

// Get float data
float[] rgb = ((DataBufferFloat) image.getRaster().getDataBuffer()).getData();

// TODO: Custom tone mapping on float RGB data

// Convert the image to something easily displayable
BufferedImage converted = new ColorConvertOp(null).filter(image, new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB));

// Optionally write as JPEG or other format
ImageIO.write(converted, "JPEG", new File(...));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM