View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.rgbe;
18  
19  import java.awt.Dimension;
20  import java.awt.Point;
21  import java.awt.Transparency;
22  import java.awt.color.ColorSpace;
23  import java.awt.image.BandedSampleModel;
24  import java.awt.image.BufferedImage;
25  import java.awt.image.ComponentColorModel;
26  import java.awt.image.DataBuffer;
27  import java.awt.image.DataBufferFloat;
28  import java.awt.image.Raster;
29  import java.io.IOException;
30  import java.util.ArrayList;
31  
32  import org.apache.commons.imaging.AbstractImageParser;
33  import org.apache.commons.imaging.ImageFormat;
34  import org.apache.commons.imaging.ImageFormats;
35  import org.apache.commons.imaging.ImageInfo;
36  import org.apache.commons.imaging.ImagingException;
37  import org.apache.commons.imaging.bytesource.ByteSource;
38  import org.apache.commons.imaging.common.ImageMetadata;
39  
40  /**
41   * Parser for Radiance HDR images
42   */
43  public class RgbeImageParser extends AbstractImageParser<RgbeImagingParameters> {
44  
45      /**
46       * Constructs a new instance with the big-endian byte order.
47       */
48      public RgbeImageParser() {
49          // empty
50      }
51  
52      @Override
53      protected String[] getAcceptedExtensions() {
54          return ImageFormats.RGBE.getExtensions();
55      }
56  
57      @Override
58      protected ImageFormat[] getAcceptedTypes() {
59          return new ImageFormat[] { ImageFormats.RGBE };
60      }
61  
62      @Override
63      public BufferedImage getBufferedImage(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
64          try (RgbeInfo info = new RgbeInfo(byteSource)) {
65              // It is necessary to create our own BufferedImage here as the
66              // org.apache.commons.imaging.common.IBufferedImageFactory interface does
67              // not expose this complexity
68              final DataBuffer buffer = new DataBufferFloat(info.getPixelData(), info.getWidth() * info.getHeight());
69  
70              return new BufferedImage(
71                      new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, Transparency.OPAQUE, buffer.getDataType()),
72                      Raster.createWritableRaster(new BandedSampleModel(buffer.getDataType(), info.getWidth(), info.getHeight(), 3), buffer, new Point()), false,
73                      null);
74          }
75      }
76  
77      @Override
78      public String getDefaultExtension() {
79          return ImageFormats.RGBE.getDefaultExtension();
80      }
81  
82      @Override
83      public RgbeImagingParameters getDefaultParameters() {
84          return new RgbeImagingParameters();
85      }
86  
87      @Override
88      public byte[] getIccProfileBytes(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
89          return null;
90      }
91  
92      @Override
93      public ImageInfo getImageInfo(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
94          try (RgbeInfo info = new RgbeInfo(byteSource)) {
95              return new ImageInfo(getName(), 32, // todo may be 64 if double?
96                      new ArrayList<>(), ImageFormats.RGBE, getName(), info.getHeight(), "image/vnd.radiance", 1, -1, -1, -1, -1, info.getWidth(), false, false,
97                      false, ImageInfo.ColorType.RGB, ImageInfo.CompressionAlgorithm.ADAPTIVE_RLE);
98          }
99      }
100 
101     @Override
102     public Dimension getImageSize(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
103         try (RgbeInfo info = new RgbeInfo(byteSource)) {
104             return new Dimension(info.getWidth(), info.getHeight());
105         }
106     }
107 
108     @Override
109     public ImageMetadata getMetadata(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
110         try (RgbeInfo info = new RgbeInfo(byteSource)) {
111             return info.getMetadata();
112         }
113     }
114 
115     @Override
116     public String getName() {
117         return "Radiance HDR";
118     }
119 }