1 /* Copyright (C) 2000, 2002, 2003 Free Software Foundation
3 This file is part of GNU Classpath.
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)
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.
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
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
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. */
37 package java
.awt
.image
;
39 import gnu
.java
.awt
.BitMaskExtent
;
40 import gnu
.java
.awt
.Buffers
;
43 * @author Rolf W. Rasmussen <rolfwr@ii.uib.no>
45 public class SinglePixelPackedSampleModel
extends SampleModel
47 private int scanlineStride
;
48 private int[] bitMasks
;
49 private int[] bitOffsets
;
50 private int[] sampleSize
;
52 public SinglePixelPackedSampleModel(int dataType
, int w
, int h
,
55 this(dataType
, w
, h
, w
, bitMasks
);
58 public SinglePixelPackedSampleModel(int dataType
, int w
, int h
,
59 int scanlineStride
, int[] bitMasks
)
61 super(dataType
, w
, h
, bitMasks
.length
);
63 this.scanlineStride
= scanlineStride
;
64 this.bitMasks
= bitMasks
;
66 bitOffsets
= new int[numBands
];
67 sampleSize
= new int[numBands
];
69 BitMaskExtent extent
= new BitMaskExtent();
70 for (int b
=0; b
<numBands
; b
++)
72 extent
.setMask(bitMasks
[b
]);
73 sampleSize
[b
] = extent
.bitWidth
;
74 bitOffsets
[b
] = extent
.leastSignificantBit
;
78 public int getNumDataElements()
83 public SampleModel
createCompatibleSampleModel(int w
, int h
)
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
);
93 * Creates a DataBuffer for holding pixel data in the format and
94 * layout described by this SampleModel. The returned buffer will
95 * consist of one single bank.
97 public DataBuffer
createDataBuffer()
101 // We can save (scanlineStride - width) pixels at the very end of
102 // the buffer. The Sun reference implementation (J2SE 1.3.1 and
103 // 1.4.1_01) seems to do this; tested with Mauve test code.
104 size
= scanlineStride
* (height
- 1) + width
;
106 return Buffers
.createBuffer(getDataType(), size
);
110 public int[] getSampleSize()
115 public int getSampleSize(int band
)
117 return sampleSize
[band
];
120 public int getOffset(int x
, int y
)
122 return scanlineStride
*y
+ x
;
125 public int[] getBitOffsets()
130 public int[] getBitMasks()
135 public int getScanlineStride()
137 return scanlineStride
;
140 public SampleModel
createSubsetSampleModel(int[] bands
)
142 // FIXME: Is this the right way to interpret bands?
144 int numBands
= bands
.length
;
146 int[] bitMasks
= new int[numBands
];
148 for (int b
=0; b
<numBands
; b
++)
149 bitMasks
[b
] = this.bitMasks
[bands
[b
]];
151 return new SinglePixelPackedSampleModel(dataType
, width
, height
,
152 scanlineStride
, bitMasks
);
155 public Object
getDataElements(int x
, int y
, Object obj
,
158 int offset
= scanlineStride
*y
+ x
+ data
.getOffset();
160 return Buffers
.getData(data
, offset
, obj
,
161 0, // destination offset,
166 public int[] getPixel(int x
, int y
, int[] iArray
, DataBuffer data
)
168 int offset
= scanlineStride
*y
+ x
;
169 if (iArray
== null) iArray
= new int[numBands
];
170 int samples
= data
.getElem(offset
);
172 for (int b
=0; b
<numBands
; b
++)
173 iArray
[b
] = (samples
& bitMasks
[b
]) >>> bitOffsets
[b
];
178 public int[] getPixels(int x
, int y
, int w
, int h
, int[] iArray
,
181 int offset
= scanlineStride
*y
+ x
;
182 if (iArray
== null) iArray
= new int[numBands
*w
*h
];
186 int lineOffset
= offset
;
189 int samples
= data
.getElem(lineOffset
++);
190 for (int b
=0; b
<numBands
; b
++)
191 iArray
[outOffset
++] = (samples
& bitMasks
[b
]) >>> bitOffsets
[b
];
193 offset
+= scanlineStride
;
198 public int getSample(int x
, int y
, int b
, DataBuffer data
)
200 int offset
= scanlineStride
*y
+ x
;
201 int samples
= data
.getElem(offset
);
202 return (samples
& bitMasks
[b
]) >>> bitOffsets
[b
];
205 public void setDataElements(int x
, int y
, Object obj
, DataBuffer data
)
207 int offset
= scanlineStride
*y
+ x
+ data
.getOffset();
209 int transferType
= getTransferType();
210 if (getTransferType() != data
.getDataType())
212 throw new IllegalArgumentException("transfer type ("+
213 getTransferType()+"), "+
214 "does not match data "+
222 switch (transferType
)
224 case DataBuffer
.TYPE_BYTE
:
226 DataBufferByte out
= (DataBufferByte
) data
;
227 byte[] in
= (byte[]) obj
;
228 out
.getData()[offset
] = in
[0];
231 case DataBuffer
.TYPE_USHORT
:
233 DataBufferUShort out
= (DataBufferUShort
) data
;
234 short[] in
= (short[]) obj
;
235 out
.getData()[offset
] = in
[0];
238 case DataBuffer
.TYPE_INT
:
240 DataBufferInt out
= (DataBufferInt
) data
;
241 int[] in
= (int[]) obj
;
242 out
.getData()[offset
] = in
[0];
245 // FIXME: Fill in the other possible types.
247 throw new InternalError();
250 catch (ArrayIndexOutOfBoundsException aioobe
)
252 String msg
= "While writing data elements" +
254 ", width="+width
+", height="+height
+
255 ", scanlineStride="+scanlineStride
+
257 ", data.getSize()="+data
.getSize()+
258 ", data.getOffset()="+data
.getOffset()+
261 throw new ArrayIndexOutOfBoundsException(msg
);
265 public void setPixel(int x
, int y
, int[] iArray
, DataBuffer data
)
267 int offset
= scanlineStride
*y
+ x
;
270 for (int b
=0; b
<numBands
; b
++)
271 samples
|= (iArray
[b
] << bitOffsets
[b
]) & bitMasks
[b
];
273 data
.setElem(offset
, samples
);
276 public void setSample(int x
, int y
, int b
, int s
, DataBuffer data
)
278 int offset
= scanlineStride
*y
+ x
;
279 int samples
= data
.getElem(offset
);
280 int bitMask
= bitMasks
[b
];
282 samples
|= (s
<< bitOffsets
[b
]) & bitMask
;
283 data
.setElem(offset
, samples
);