| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.util; |
| 16 | |
| 17 | import java.awt.image.BufferedImage; |
| 18 | import java.awt.image.ColorModel; |
| 19 | import java.awt.image.DataBuffer; |
| 20 | import java.awt.image.DataBufferByte; |
| 21 | import java.awt.image.Raster; |
| 22 | import java.awt.image.WritableRaster; |
| 23 | |
| 24 | public abstract class AbstractImageHolder implements ImageHolder |
| 25 | { |
| 26 | protected AbstractImageHolder(int bytesPerPixel, int[] bandOffset) |
| 27 | { |
| 28 | _bytesPerPixel = bytesPerPixel; |
| 29 | _bandOffset = bandOffset; |
| 30 | } |
| 31 | |
| 32 | abstract protected ColorModel getColorModel(); |
| 33 | |
| 34 | public BufferedImage getImage() |
| 35 | { |
| 36 | if (_content == null) |
| 37 | { |
| 38 | return null; |
| 39 | } |
| 40 | if (_image == null) |
| 41 | { |
| 42 | DataBuffer buffer = new DataBufferByte( |
| 43 | _content, _width * _height * _bytesPerPixel); |
| 44 | WritableRaster raster = Raster.createInterleavedRaster( |
| 45 | buffer, _width, _height, |
| 46 | _width * _bytesPerPixel, |
| 47 | _bytesPerPixel, |
| 48 | _bandOffset, null); |
| 49 | _image = new BufferedImage(getColorModel(), raster, false, null); |
| 50 | } |
| 51 | return _image; |
| 52 | } |
| 53 | |
| 54 | public byte[] getImageRaster() |
| 55 | { |
| 56 | return _content; |
| 57 | } |
| 58 | |
| 59 | public void setImage(BufferedImage image) |
| 60 | { |
| 61 | _image = null; |
| 62 | WritableRaster raster = image.getRaster(); |
| 63 | _height = raster.getHeight(); |
| 64 | _width = raster.getWidth(); |
| 65 | DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer(); |
| 66 | _content = buffer.getData(); |
| 67 | } |
| 68 | |
| 69 | public void setImage(int width, int height, byte[] content) |
| 70 | { |
| 71 | _image = null; |
| 72 | _height = height; |
| 73 | _width = width; |
| 74 | _content = content; |
| 75 | } |
| 76 | |
| 77 | final private int _bytesPerPixel; |
| 78 | final private int[] _bandOffset; |
| 79 | private int _width; |
| 80 | private int _height; |
| 81 | private byte[] _content; |
| 82 | private transient BufferedImage _image; |
| 83 | } |