001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging.formats.rgbe;
018
019import org.apache.commons.imaging.ImageFormat;
020import org.apache.commons.imaging.ImageFormats;
021import org.apache.commons.imaging.ImageInfo;
022import org.apache.commons.imaging.ImageParser;
023import org.apache.commons.imaging.ImageReadException;
024import org.apache.commons.imaging.common.ImageMetadata;
025import org.apache.commons.imaging.common.bytesource.ByteSource;
026
027import java.awt.Dimension;
028import java.awt.Point;
029import java.awt.Transparency;
030import java.awt.color.ColorSpace;
031import java.awt.image.BandedSampleModel;
032import java.awt.image.BufferedImage;
033import java.awt.image.ComponentColorModel;
034import java.awt.image.DataBuffer;
035import java.awt.image.DataBufferFloat;
036import java.awt.image.Raster;
037import java.io.IOException;
038import java.nio.ByteOrder;
039import java.util.ArrayList;
040
041/**
042 * Parser for Radiance HDR images
043 *
044 * @author <a href="mailto:peter@electrotank.com">peter royal</a>
045 */
046public class RgbeImageParser extends ImageParser<RgbeImagingParameters> {
047
048    public RgbeImageParser() {
049        setByteOrder(ByteOrder.BIG_ENDIAN);
050    }
051
052    @Override
053    public RgbeImagingParameters getDefaultParameters() {
054        return new RgbeImagingParameters();
055    }
056
057    @Override
058    public String getName() {
059        return "Radiance HDR";
060    }
061
062    @Override
063    public String getDefaultExtension() {
064        return ImageFormats.RGBE.getDefaultExtension();
065    }
066
067    @Override
068    protected String[] getAcceptedExtensions() {
069        return ImageFormats.RGBE.getExtensions();
070    }
071
072    @Override
073    protected ImageFormat[] getAcceptedTypes() {
074        return new ImageFormat[] { ImageFormats.RGBE };
075    }
076
077    @Override
078    public ImageMetadata getMetadata(final ByteSource byteSource, final RgbeImagingParameters params)
079            throws ImageReadException, IOException {
080        try (RgbeInfo info = new RgbeInfo(byteSource)) {
081            return info.getMetadata();
082        }
083    }
084
085    @Override
086    public ImageInfo getImageInfo(final ByteSource byteSource, final RgbeImagingParameters params)
087            throws ImageReadException, IOException {
088        try (RgbeInfo info = new RgbeInfo(byteSource)) {
089            return new ImageInfo(
090                    getName(),
091                    32, // todo may be 64 if double?
092                    new ArrayList<>(), ImageFormats.RGBE, getName(),
093                    info.getHeight(), "image/vnd.radiance", 1, -1, -1, -1, -1,
094                    info.getWidth(), false, false, false,
095                    ImageInfo.ColorType.RGB, ImageInfo.CompressionAlgorithm.ADAPTIVE_RLE);
096        }
097    }
098
099    @Override
100    public BufferedImage getBufferedImage(final ByteSource byteSource, final RgbeImagingParameters params)
101            throws ImageReadException, IOException {
102        try (RgbeInfo info = new RgbeInfo(byteSource)) {
103            // It is necessary to create our own BufferedImage here as the
104            // org.apache.commons.imaging.common.IBufferedImageFactory interface does
105            // not expose this complexity
106            final DataBuffer buffer = new DataBufferFloat(info.getPixelData(),
107                    info.getWidth() * info.getHeight());
108
109            return new BufferedImage(new ComponentColorModel(
110                    ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
111                    Transparency.OPAQUE, buffer.getDataType()),
112                    Raster.createWritableRaster(
113                            new BandedSampleModel(buffer.getDataType(),
114                                    info.getWidth(), info.getHeight(), 3),
115                            buffer,
116                            new Point()), false, null);
117        }
118    }
119
120    @Override
121    public Dimension getImageSize(final ByteSource byteSource, final RgbeImagingParameters params)
122            throws ImageReadException, IOException {
123        try (RgbeInfo info = new RgbeInfo(byteSource)) {
124            return new Dimension(info.getWidth(), info.getHeight());
125        }
126    }
127
128    @Override
129    public byte[] getICCProfileBytes(final ByteSource byteSource, final RgbeImagingParameters params)
130            throws ImageReadException, IOException {
131        return null;
132    }
133}