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 java.awt.Dimension;
020import java.awt.Point;
021import java.awt.Transparency;
022import java.awt.color.ColorSpace;
023import java.awt.image.BandedSampleModel;
024import java.awt.image.BufferedImage;
025import java.awt.image.ComponentColorModel;
026import java.awt.image.DataBuffer;
027import java.awt.image.DataBufferFloat;
028import java.awt.image.Raster;
029import java.io.IOException;
030import java.util.ArrayList;
031
032import org.apache.commons.imaging.AbstractImageParser;
033import org.apache.commons.imaging.ImageFormat;
034import org.apache.commons.imaging.ImageFormats;
035import org.apache.commons.imaging.ImageInfo;
036import org.apache.commons.imaging.ImagingException;
037import org.apache.commons.imaging.bytesource.ByteSource;
038import org.apache.commons.imaging.common.ImageMetadata;
039
040/**
041 * Parser for Radiance HDR images
042 */
043public class RgbeImageParser extends AbstractImageParser<RgbeImagingParameters> {
044
045    @Override
046    protected String[] getAcceptedExtensions() {
047        return ImageFormats.RGBE.getExtensions();
048    }
049
050    @Override
051    protected ImageFormat[] getAcceptedTypes() {
052        return new ImageFormat[] { ImageFormats.RGBE };
053    }
054
055    @Override
056    public BufferedImage getBufferedImage(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
057        try (RgbeInfo info = new RgbeInfo(byteSource)) {
058            // It is necessary to create our own BufferedImage here as the
059            // org.apache.commons.imaging.common.IBufferedImageFactory interface does
060            // not expose this complexity
061            final DataBuffer buffer = new DataBufferFloat(info.getPixelData(), info.getWidth() * info.getHeight());
062
063            return new BufferedImage(
064                    new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, Transparency.OPAQUE, buffer.getDataType()),
065                    Raster.createWritableRaster(new BandedSampleModel(buffer.getDataType(), info.getWidth(), info.getHeight(), 3), buffer, new Point()), false,
066                    null);
067        }
068    }
069
070    @Override
071    public String getDefaultExtension() {
072        return ImageFormats.RGBE.getDefaultExtension();
073    }
074
075    @Override
076    public RgbeImagingParameters getDefaultParameters() {
077        return new RgbeImagingParameters();
078    }
079
080    @Override
081    public byte[] getIccProfileBytes(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
082        return null;
083    }
084
085    @Override
086    public ImageInfo getImageInfo(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
087        try (RgbeInfo info = new RgbeInfo(byteSource)) {
088            return new ImageInfo(getName(), 32, // todo may be 64 if double?
089                    new ArrayList<>(), ImageFormats.RGBE, getName(), info.getHeight(), "image/vnd.radiance", 1, -1, -1, -1, -1, info.getWidth(), false, false,
090                    false, ImageInfo.ColorType.RGB, ImageInfo.CompressionAlgorithm.ADAPTIVE_RLE);
091        }
092    }
093
094    @Override
095    public Dimension getImageSize(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
096        try (RgbeInfo info = new RgbeInfo(byteSource)) {
097            return new Dimension(info.getWidth(), info.getHeight());
098        }
099    }
100
101    @Override
102    public ImageMetadata getMetadata(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException {
103        try (RgbeInfo info = new RgbeInfo(byteSource)) {
104            return info.getMetadata();
105        }
106    }
107
108    @Override
109    public String getName() {
110        return "Radiance HDR";
111    }
112}