|
| 1 | +/* |
| 2 | + * This file is part of javavp8decoder. |
| 3 | + * |
| 4 | + * javavp8decoder is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * javavp8decoder is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with javavp8decoder. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package net.sf.javavp8decoder.imageio; |
| 19 | + |
| 20 | +import java.awt.color.ColorSpace; |
| 21 | +import java.awt.image.BufferedImage; |
| 22 | +import java.awt.image.DataBuffer; |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Iterator; |
| 26 | + |
| 27 | +import javax.imageio.IIOException; |
| 28 | +import javax.imageio.ImageReadParam; |
| 29 | +import javax.imageio.ImageReader; |
| 30 | +import javax.imageio.ImageTypeSpecifier; |
| 31 | +import javax.imageio.event.IIOReadProgressListener; |
| 32 | +import javax.imageio.metadata.IIOMetadata; |
| 33 | +import javax.imageio.spi.ImageReaderSpi; |
| 34 | +import javax.imageio.stream.ImageInputStream; |
| 35 | + |
| 36 | +import net.sf.javavp8decoder.vp8Decoder.VP8Frame; |
| 37 | + |
| 38 | + |
| 39 | +public class WebPImageReader extends ImageReader implements IIOReadProgressListener { |
| 40 | + |
| 41 | + // Constants enumerating the values of colorType |
| 42 | + static final int COLOR_TYPE_GRAY = 0; |
| 43 | + |
| 44 | + static final int COLOR_TYPE_RGB = 1; |
| 45 | + |
| 46 | + int colorType; |
| 47 | + |
| 48 | + private VP8Frame decoder; |
| 49 | + |
| 50 | + boolean gotHeader = false; |
| 51 | + |
| 52 | + WebPMetadata metadata = null; // class defined below |
| 53 | + |
| 54 | + ImageInputStream stream = null; |
| 55 | + |
| 56 | + int width, height; |
| 57 | + |
| 58 | + public WebPImageReader(ImageReaderSpi originatingProvider) { |
| 59 | + super(originatingProvider); |
| 60 | + } |
| 61 | + |
| 62 | + private void _setInput(Object input) { |
| 63 | + if (input == null) { |
| 64 | + this.stream = null; |
| 65 | + return; |
| 66 | + } |
| 67 | + if (input instanceof ImageInputStream) { |
| 68 | + this.stream = (ImageInputStream) input; |
| 69 | + } else { |
| 70 | + throw new IllegalArgumentException("bad input"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void checkIndex(int imageIndex) { |
| 75 | + if (imageIndex != 0) { |
| 76 | + throw new IndexOutOfBoundsException("bad index"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public int getHeight(int imageIndex) throws IIOException { |
| 81 | + checkIndex(imageIndex); |
| 82 | + readHeader(); |
| 83 | + return height; |
| 84 | + } |
| 85 | + |
| 86 | + public IIOMetadata getImageMetadata(int imageIndex) throws IIOException { |
| 87 | + if (imageIndex != 0) { |
| 88 | + throw new IndexOutOfBoundsException("imageIndex != 0!"); |
| 89 | + } |
| 90 | + readMetadata(); |
| 91 | + return metadata; |
| 92 | + } |
| 93 | + |
| 94 | + public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) throws IIOException { |
| 95 | + checkIndex(imageIndex); |
| 96 | + readHeader(); |
| 97 | + |
| 98 | + ImageTypeSpecifier imageType = null; |
| 99 | + int datatype = DataBuffer.TYPE_BYTE; |
| 100 | + java.util.List<ImageTypeSpecifier> l = new ArrayList<>(); |
| 101 | + |
| 102 | + ColorSpace rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
| 103 | + int[] bandOffsets = new int[3]; |
| 104 | + bandOffsets[0] = 0; |
| 105 | + bandOffsets[1] = 1; |
| 106 | + bandOffsets[2] = 2; |
| 107 | + imageType = ImageTypeSpecifier.createInterleaved(rgb, bandOffsets, datatype, false, false); |
| 108 | + |
| 109 | + l.add(imageType); |
| 110 | + return l.iterator(); |
| 111 | + } |
| 112 | + |
| 113 | + public int getNumImages(boolean allowSearch) throws IIOException { |
| 114 | + return 1; // format can only encode a single image |
| 115 | + } |
| 116 | + |
| 117 | + public IIOMetadata getStreamMetadata() throws IIOException { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + public int getWidth(int imageIndex) throws IIOException { |
| 122 | + checkIndex(imageIndex); // must throw an exception if != 0 |
| 123 | + readHeader(); |
| 124 | + return width; |
| 125 | + } |
| 126 | + |
| 127 | + public void imageComplete(ImageReader source) { |
| 128 | + } |
| 129 | + |
| 130 | + public void imageProgress(ImageReader source, float percentageDone) { |
| 131 | + processImageProgress(percentageDone); |
| 132 | + } |
| 133 | + |
| 134 | + public void imageStarted(ImageReader source, int imageIndex) { |
| 135 | + } |
| 136 | + |
| 137 | + public BufferedImage read(int imageIndex, ImageReadParam param) throws IIOException { |
| 138 | + super.processImageStarted(0); |
| 139 | + readMetadata(); // Stream is positioned at start of image data |
| 140 | + // Get values from the ImageReadParam, if any |
| 141 | + if (param != null) { |
| 142 | + } |
| 143 | + // Get the specified detination image or create a new one |
| 144 | + BufferedImage dst = getDestination(param, getImageTypes(0), width, height); |
| 145 | + decoder.useBufferedImage(dst); |
| 146 | + // decoder.getBufferedImage(); |
| 147 | + |
| 148 | + super.processImageComplete(); |
| 149 | + return dst; |
| 150 | + } |
| 151 | + |
| 152 | + public void readAborted(ImageReader source) { |
| 153 | + } |
| 154 | + |
| 155 | + public void readHeader() throws IIOException { |
| 156 | + if (stream == null) { |
| 157 | + throw new IllegalStateException("No input stream"); |
| 158 | + } |
| 159 | + try { |
| 160 | + byte[] signature = new byte[4]; |
| 161 | + try { |
| 162 | + stream.readFully(signature); |
| 163 | + } catch (IOException e) { |
| 164 | + throw new IIOException("Error reading RIFF signature", e); |
| 165 | + } |
| 166 | + if (signature[0] != (byte) 'R' || signature[1] != (byte) 'I' || signature[2] != (byte) 'F' |
| 167 | + || signature[3] != (byte) 'F') { // etc. |
| 168 | + throw new IIOException("Bad RIFF signature!"); |
| 169 | + } |
| 170 | + try { |
| 171 | + stream.read(); |
| 172 | + stream.read(); |
| 173 | + stream.read(); |
| 174 | + stream.read(); |
| 175 | + } catch (IOException e) { |
| 176 | + throw new IIOException("Error reading frame size 1", e); |
| 177 | + } |
| 178 | + try { |
| 179 | + stream.readFully(signature); |
| 180 | + } catch (IOException e) { |
| 181 | + throw new IIOException("Error reading WEBP signature", e); |
| 182 | + } |
| 183 | + if (signature[0] != (byte) 'W' || signature[1] != (byte) 'E' || signature[2] != (byte) 'B' |
| 184 | + || signature[3] != (byte) 'P') { // etc. |
| 185 | + throw new IIOException("Bad WEBP signature!"); |
| 186 | + } |
| 187 | + try { |
| 188 | + stream.readFully(signature); |
| 189 | + } catch (IOException e) { |
| 190 | + throw new IIOException("Error reading VP8 signature", e); |
| 191 | + } |
| 192 | + if (signature[0] != (byte) 'V' || signature[1] != (byte) 'P' || signature[2] != (byte) '8') { |
| 193 | + throw new IIOException("Bad WEBP signature!"); |
| 194 | + } |
| 195 | + try { |
| 196 | + stream.read(); |
| 197 | + stream.read(); |
| 198 | + stream.read(); |
| 199 | + stream.read(); |
| 200 | + } catch (IOException e) { |
| 201 | + throw new IIOException("Error reading frame size 1", e); |
| 202 | + } |
| 203 | + try { |
| 204 | + if (decoder == null) { |
| 205 | + decoder = new VP8Frame(stream); |
| 206 | + decoder.addIIOReadProgressListener(this); |
| 207 | + } else { |
| 208 | + decoder.setFrame(stream); |
| 209 | + } |
| 210 | + decoder.decodeFrame(false); |
| 211 | + } catch (IOException e) { |
| 212 | + e.printStackTrace(); |
| 213 | + } |
| 214 | + } catch (IOException e) { |
| 215 | + e.printStackTrace(); |
| 216 | + } |
| 217 | + this.width = decoder.getWidth(); |
| 218 | + this.height = decoder.getHeight(); |
| 219 | + } |
| 220 | + |
| 221 | + public void readMetadata() throws IIOException { |
| 222 | + if (metadata != null) { |
| 223 | + return; |
| 224 | + } |
| 225 | + readHeader(); |
| 226 | + this.metadata = new WebPMetadata(); |
| 227 | + } |
| 228 | + |
| 229 | + public void sequenceComplete(ImageReader source) { |
| 230 | + } |
| 231 | + |
| 232 | + public void sequenceStarted(ImageReader source, int minIndex) { |
| 233 | + } |
| 234 | + |
| 235 | + public void setInput(Object input) { |
| 236 | + super.setInput(input); |
| 237 | + _setInput(input); |
| 238 | + } |
| 239 | + |
| 240 | + public void setInput(Object input, boolean isStreamable) { |
| 241 | + super.setInput(input, isStreamable); |
| 242 | + _setInput(input); |
| 243 | + |
| 244 | + } |
| 245 | + |
| 246 | + public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) { |
| 247 | + super.setInput(input, seekForwardOnly, ignoreMetadata); |
| 248 | + _setInput(input); |
| 249 | + } |
| 250 | + |
| 251 | + public void thumbnailComplete(ImageReader source) { |
| 252 | + } |
| 253 | + |
| 254 | + public void thumbnailProgress(ImageReader source, float percentageDone) { |
| 255 | + } |
| 256 | + |
| 257 | + public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) { |
| 258 | + } |
| 259 | +} |
0 commit comments