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 /** 046 * Constructs a new instance with the big-endian byte order. 047 */ 048 public RgbeImageParser() { 049 // empty 050 } 051 052 @Override 053 protected String[] getAcceptedExtensions() { 054 return ImageFormats.RGBE.getExtensions(); 055 } 056 057 @Override 058 protected ImageFormat[] getAcceptedTypes() { 059 return new ImageFormat[] { ImageFormats.RGBE }; 060 } 061 062 @Override 063 public BufferedImage getBufferedImage(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException { 064 try (RgbeInfo info = new RgbeInfo(byteSource)) { 065 // It is necessary to create our own BufferedImage here as the 066 // org.apache.commons.imaging.common.IBufferedImageFactory interface does 067 // not expose this complexity 068 final DataBuffer buffer = new DataBufferFloat(info.getPixelData(), info.getWidth() * info.getHeight()); 069 070 return new BufferedImage( 071 new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, Transparency.OPAQUE, buffer.getDataType()), 072 Raster.createWritableRaster(new BandedSampleModel(buffer.getDataType(), info.getWidth(), info.getHeight(), 3), buffer, new Point()), false, 073 null); 074 } 075 } 076 077 @Override 078 public String getDefaultExtension() { 079 return ImageFormats.RGBE.getDefaultExtension(); 080 } 081 082 @Override 083 public RgbeImagingParameters getDefaultParameters() { 084 return new RgbeImagingParameters(); 085 } 086 087 @Override 088 public byte[] getIccProfileBytes(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException { 089 return null; 090 } 091 092 @Override 093 public ImageInfo getImageInfo(final ByteSource byteSource, final RgbeImagingParameters params) throws ImagingException, IOException { 094 try (RgbeInfo info = new RgbeInfo(byteSource)) { 095 return new ImageInfo(getName(), 32, // todo may be 64 if double? 096 new ArrayList<>(), ImageFormats.RGBE, getName(), info.getHeight(), "image/vnd.radiance", 1, -1, -1, -1, -1, info.getWidth(), false, false, 097 false, ImageInfo.ColorType.RGB, ImageInfo.CompressionAlgorithm.ADAPTIVE_RLE); 098 } 099 } 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}