| 1 | /* Copyright (C) 2000, 2002 Free Software Foundation
|
|---|
| 2 |
|
|---|
| 3 | This file is part of GNU Classpath.
|
|---|
| 4 |
|
|---|
| 5 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 8 | any later version.
|
|---|
| 9 |
|
|---|
| 10 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License
|
|---|
| 16 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 17 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 18 | 02111-1307 USA.
|
|---|
| 19 |
|
|---|
| 20 | Linking this library statically or dynamically with other modules is
|
|---|
| 21 | making a combined work based on this library. Thus, the terms and
|
|---|
| 22 | conditions of the GNU General Public License cover the whole
|
|---|
| 23 | combination.
|
|---|
| 24 |
|
|---|
| 25 | As a special exception, the copyright holders of this library give you
|
|---|
| 26 | permission to link this library with independent modules to produce an
|
|---|
| 27 | executable, regardless of the license terms of these independent
|
|---|
| 28 | modules, and to copy and distribute the resulting executable under
|
|---|
| 29 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 30 | independent module, the terms and conditions of the license of that
|
|---|
| 31 | module. An independent module is a module which is not derived from
|
|---|
| 32 | or based on this library. If you modify this library, you may extend
|
|---|
| 33 | this exception to your version of the library, but you are not
|
|---|
| 34 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 35 | exception statement from your version. */
|
|---|
| 36 |
|
|---|
| 37 | package java.awt.image;
|
|---|
| 38 |
|
|---|
| 39 | import gnu.java.awt.BitMaskExtent;
|
|---|
| 40 | import gnu.java.awt.Buffers;
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * @author Rolf W. Rasmussen <[email protected]>
|
|---|
| 44 | */
|
|---|
| 45 | public class SinglePixelPackedSampleModel extends SampleModel
|
|---|
| 46 | {
|
|---|
| 47 | private int scanlineStride;
|
|---|
| 48 | private int[] bitMasks;
|
|---|
| 49 | private int[] bitOffsets;
|
|---|
| 50 | private int[] sampleSize;;
|
|---|
| 51 |
|
|---|
| 52 | public SinglePixelPackedSampleModel(int dataType, int w, int h,
|
|---|
| 53 | int[] bitMasks)
|
|---|
| 54 | {
|
|---|
| 55 | this(dataType, w, h, w, bitMasks);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public SinglePixelPackedSampleModel(int dataType, int w, int h,
|
|---|
| 59 | int scanlineStride, int[] bitMasks)
|
|---|
| 60 | {
|
|---|
| 61 | super(dataType, w, h, bitMasks.length);
|
|---|
| 62 |
|
|---|
| 63 | this.scanlineStride = scanlineStride;
|
|---|
| 64 | this.bitMasks = bitMasks;
|
|---|
| 65 |
|
|---|
| 66 | bitOffsets = new int[numBands];
|
|---|
| 67 | sampleSize = new int[numBands];
|
|---|
| 68 |
|
|---|
| 69 | BitMaskExtent extent = new BitMaskExtent();
|
|---|
| 70 | for (int b=0; b<numBands; b++)
|
|---|
| 71 | {
|
|---|
| 72 | extent.setMask(bitMasks[b]);
|
|---|
| 73 | sampleSize[b] = extent.bitWidth;
|
|---|
| 74 | bitOffsets[b] = extent.leastSignificantBit;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | public int getNumDataElements()
|
|---|
| 79 | {
|
|---|
| 80 | return 1;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | public SampleModel createCompatibleSampleModel(int w, int h)
|
|---|
| 84 | {
|
|---|
| 85 | /* FIXME: We can avoid recalculation of bit offsets and sample
|
|---|
| 86 | sizes here by passing these from the current instance to a
|
|---|
| 87 | special private constructor. */
|
|---|
| 88 | return new SinglePixelPackedSampleModel(dataType, w, h, bitMasks);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | public DataBuffer createDataBuffer()
|
|---|
| 92 | {
|
|---|
| 93 | // Important: use scanlineStride here, not width!
|
|---|
| 94 | int size = scanlineStride*height;
|
|---|
| 95 | return Buffers.createBuffer(getDataType(), size);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | public int[] getSampleSize()
|
|---|
| 99 | {
|
|---|
| 100 | return sampleSize;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | public int getSampleSize(int band)
|
|---|
| 104 | {
|
|---|
| 105 | return sampleSize[band];
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | public int getOffset(int x, int y)
|
|---|
| 109 | {
|
|---|
| 110 | return scanlineStride*y + x;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | public int[] getBitOffsets()
|
|---|
| 114 | {
|
|---|
| 115 | return bitOffsets;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | public int[] getBitMasks()
|
|---|
| 119 | {
|
|---|
| 120 | return bitMasks;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | public int getScanlineStride()
|
|---|
| 124 | {
|
|---|
| 125 | return scanlineStride;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | public SampleModel createSubsetSampleModel(int[] bands)
|
|---|
| 129 | {
|
|---|
| 130 | // FIXME: Is this the right way to interpret bands?
|
|---|
| 131 |
|
|---|
| 132 | int numBands = bands.length;
|
|---|
| 133 |
|
|---|
| 134 | int[] bitMasks = new int[numBands];
|
|---|
| 135 |
|
|---|
| 136 | for (int b=0; b<numBands; b++)
|
|---|
| 137 | bitMasks[b] = this.bitMasks[bands[b]];
|
|---|
| 138 |
|
|---|
| 139 | return new SinglePixelPackedSampleModel(dataType, width, height,
|
|---|
| 140 | scanlineStride, bitMasks);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | public Object getDataElements(int x, int y, Object obj,
|
|---|
| 144 | DataBuffer data)
|
|---|
| 145 | {
|
|---|
| 146 | int offset = scanlineStride*y + x + data.getOffset();
|
|---|
| 147 |
|
|---|
| 148 | return Buffers.getData(data, offset, obj,
|
|---|
| 149 | 0, // destination offset,
|
|---|
| 150 | 1 // length
|
|---|
| 151 | );
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | public int[] getPixel(int x, int y, int[] iArray, DataBuffer data)
|
|---|
| 155 | {
|
|---|
| 156 | int offset = scanlineStride*y + x;
|
|---|
| 157 | if (iArray == null) iArray = new int[numBands];
|
|---|
| 158 | int samples = data.getElem(offset);
|
|---|
| 159 |
|
|---|
| 160 | for (int b=0; b<numBands; b++)
|
|---|
| 161 | iArray[b] = (samples & bitMasks[b]) >>> bitOffsets[b];
|
|---|
| 162 |
|
|---|
| 163 | return iArray;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | public int[] getPixels(int x, int y, int w, int h, int[] iArray,
|
|---|
| 167 | DataBuffer data)
|
|---|
| 168 | {
|
|---|
| 169 | int offset = scanlineStride*y + x;
|
|---|
| 170 | if (iArray == null) iArray = new int[numBands*w*h];
|
|---|
| 171 | int outOffset = 0;
|
|---|
| 172 | for (y=0; y<h; y++)
|
|---|
| 173 | {
|
|---|
| 174 | int lineOffset = offset;
|
|---|
| 175 | for (x=0; x<w; x++)
|
|---|
| 176 | {
|
|---|
| 177 | int samples = data.getElem(lineOffset++);
|
|---|
| 178 | for (int b=0; b<numBands; b++)
|
|---|
| 179 | iArray[outOffset++] = (samples & bitMasks[b]) >>> bitOffsets[b];
|
|---|
| 180 | }
|
|---|
| 181 | offset += scanlineStride;
|
|---|
| 182 | }
|
|---|
| 183 | return iArray;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | public int getSample(int x, int y, int b, DataBuffer data)
|
|---|
| 187 | {
|
|---|
| 188 | int offset = scanlineStride*y + x;
|
|---|
| 189 | int samples = data.getElem(offset);
|
|---|
| 190 | return (samples & bitMasks[b]) >>> bitOffsets[b];
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | public void setDataElements(int x, int y, Object obj, DataBuffer data)
|
|---|
| 194 | {
|
|---|
| 195 | int offset = scanlineStride*y + x + data.getOffset();
|
|---|
| 196 |
|
|---|
| 197 | int transferType = getTransferType();
|
|---|
| 198 | if (getTransferType() != data.getDataType())
|
|---|
| 199 | {
|
|---|
| 200 | throw new IllegalArgumentException("transfer type ("+
|
|---|
| 201 | getTransferType()+"), "+
|
|---|
| 202 | "does not match data "+
|
|---|
| 203 | "buffer type (" +
|
|---|
| 204 | data.getDataType() +
|
|---|
| 205 | ").");
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | try
|
|---|
| 209 | {
|
|---|
| 210 | switch (transferType)
|
|---|
| 211 | {
|
|---|
| 212 | case DataBuffer.TYPE_BYTE:
|
|---|
| 213 | {
|
|---|
| 214 | DataBufferByte out = (DataBufferByte) data;
|
|---|
| 215 | byte[] in = (byte[]) obj;
|
|---|
| 216 | out.getData()[offset] = in[0];
|
|---|
| 217 | return;
|
|---|
| 218 | }
|
|---|
| 219 | case DataBuffer.TYPE_USHORT:
|
|---|
| 220 | {
|
|---|
| 221 | DataBufferUShort out = (DataBufferUShort) data;
|
|---|
| 222 | short[] in = (short[]) obj;
|
|---|
| 223 | out.getData()[offset] = in[0];
|
|---|
| 224 | return;
|
|---|
| 225 | }
|
|---|
| 226 | case DataBuffer.TYPE_INT:
|
|---|
| 227 | {
|
|---|
| 228 | DataBufferInt out = (DataBufferInt) data;
|
|---|
| 229 | int[] in = (int[]) obj;
|
|---|
| 230 | out.getData()[offset] = in[0];
|
|---|
| 231 | return;
|
|---|
| 232 | }
|
|---|
| 233 | // FIXME: Fill in the other possible types.
|
|---|
| 234 | default:
|
|---|
| 235 | throw new InternalError();
|
|---|
| 236 | }
|
|---|
| 237 | }
|
|---|
| 238 | catch (ArrayIndexOutOfBoundsException aioobe)
|
|---|
| 239 | {
|
|---|
| 240 | String msg = "While writing data elements" +
|
|---|
| 241 | ", x="+x+", y="+y+
|
|---|
| 242 | ", width="+width+", height="+height+
|
|---|
| 243 | ", scanlineStride="+scanlineStride+
|
|---|
| 244 | ", offset="+offset+
|
|---|
| 245 | ", data.getSize()="+data.getSize()+
|
|---|
| 246 | ", data.getOffset()="+data.getOffset()+
|
|---|
| 247 | ": " +
|
|---|
| 248 | aioobe;
|
|---|
| 249 | throw new ArrayIndexOutOfBoundsException(msg);
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | public void setPixel(int x, int y, int[] iArray, DataBuffer data)
|
|---|
| 254 | {
|
|---|
| 255 | int offset = scanlineStride*y + x;
|
|---|
| 256 |
|
|---|
| 257 | int samples = 0;
|
|---|
| 258 | for (int b=0; b<numBands; b++)
|
|---|
| 259 | samples |= (iArray[b] << bitOffsets[b]) & bitMasks[b];
|
|---|
| 260 |
|
|---|
| 261 | data.setElem(offset, samples);
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | public void setSample(int x, int y, int b, int s, DataBuffer data)
|
|---|
| 265 | {
|
|---|
| 266 | int offset = scanlineStride*y + x;
|
|---|
| 267 | int samples = data.getElem(offset);
|
|---|
| 268 | int bitMask = bitMasks[b];
|
|---|
| 269 | samples &= ~bitMask;
|
|---|
| 270 | samples |= (s << bitOffsets[b]) & bitMask;
|
|---|
| 271 | data.setElem(offset, samples);
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|