    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.nio.ByteOrder;
    031import java.util.ArrayList;
    032import java.util.Map;
    033
    034import org.apache.commons.imaging.ImageFormat;
    035import org.apache.commons.imaging.ImageFormats;
    036import org.apache.commons.imaging.ImageInfo;
    037import org.apache.commons.imaging.ImageParser;
    038import org.apache.commons.imaging.ImageReadException;
    039import org.apache.commons.imaging.common.ImageMetadata;
    040import org.apache.commons.imaging.common.bytesource.ByteSource;
    041
    042/**
    043 * Parser for Radiance HDR images
    044 *
    045 * @author <a href="mailto:peter@electrotank.com">peter royal</a>
    046 */
    047public class RgbeImageParser extends ImageParser {
    048
    049    public RgbeImageParser() {
    050        setByteOrder(ByteOrder.BIG_ENDIAN);
    051    }
    052
    053    @Override
    054    public String getName() {
    055        return "Radiance HDR";
    056    }
    057
    058    @Override
    059    public String getDefaultExtension() {
    060        return ".hdr";
    061    }
    062
    063    @Override
    064    protected String[] getAcceptedExtensions() {
    065        return new String[] { ".hdr", ".pic" };
    066    }
    067
    068    @Override
    069    protected ImageFormat[] getAcceptedTypes() {
    070        return new ImageFormat[] { ImageFormats.RGBE };
    071    }
    072
    073    @Override
    074    public ImageMetadata getMetadata(final ByteSource byteSource, final Map<String, Object> params)
    075            throws ImageReadException, IOException {
    076        try (RgbeInfo info = new RgbeInfo(byteSource)) {
    077            final ImageMetadata ret = info.getMetadata();
    078            return ret;
    079        }
    080    }
    081
    082    @Override
    083    public ImageInfo getImageInfo(final ByteSource byteSource, final Map<String, Object> params)
    084            throws ImageReadException, IOException {
    085        try (RgbeInfo info = new RgbeInfo(byteSource)) {
    086            return new ImageInfo(
    087                    getName(),
    088                    32, // todo may be 64 if double?
    089                    new ArrayList<String>(), ImageFormats.RGBE, getName(),
    090                    info.getHeight(), "image/vnd.radiance", 1, -1, -1, -1, -1,
    091                    info.getWidth(), false, false, false,
    092                    ImageInfo.ColorType.RGB, ImageInfo.CompressionAlgorithm.ADAPTIVE_RLE);
    093        }
    094    }
    095
    096    @Override
    097    public BufferedImage getBufferedImage(final ByteSource byteSource, final Map<String, Object> params)
    098            throws ImageReadException, IOException {
    099        try (RgbeInfo info = new RgbeInfo(byteSource)) {
    100            // It is necessary to create our own BufferedImage here as the
    101            // org.apache.commons.imaging.common.IBufferedImageFactory interface does
    102            // not expose this complexity
    103            final DataBuffer buffer = new DataBufferFloat(info.getPixelData(),
    104                    info.getWidth() * info.getHeight());
    105
    106            final BufferedImage ret = new BufferedImage(new ComponentColorModel(
    107                    ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
    108                    Transparency.OPAQUE, buffer.getDataType()),
    109                    Raster.createWritableRaster(
    110                            new BandedSampleModel(buffer.getDataType(),
    111                                    info.getWidth(), info.getHeight(), 3),
    112                            buffer,
    113                            new Point()), false, null);
    114            return ret;
    115        }
    116    }
    117
    118    @Override
    119    public Dimension getImageSize(final ByteSource byteSource, final Map<String, Object> params)
    120            throws ImageReadException, IOException {
    121        try (RgbeInfo info = new RgbeInfo(byteSource)) {
    122            final Dimension ret = new Dimension(info.getWidth(), info.getHeight());
    123            return ret;
    124        }
    125    }
    126
    127    @Override
    128    public byte[] getICCProfileBytes(final ByteSource byteSource, final Map<String, Object> params)
    129            throws ImageReadException, IOException {
    130        return null;
    131    }
    132}