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: */
017: /**
018: * @author Igor V. Stolyarov
019: * @version $Revision$
020: */package java.awt.image;
021:
022: import org.apache.harmony.awt.gl.image.DataBufferListener;
023: import org.apache.harmony.awt.internal.nls.Messages;
024:
025: public abstract class DataBuffer {
026:
027: public static final int TYPE_BYTE = 0;
028:
029: public static final int TYPE_USHORT = 1;
030:
031: public static final int TYPE_SHORT = 2;
032:
033: public static final int TYPE_INT = 3;
034:
035: public static final int TYPE_FLOAT = 4;
036:
037: public static final int TYPE_DOUBLE = 5;
038:
039: public static final int TYPE_UNDEFINED = 32;
040:
041: protected int dataType;
042:
043: protected int banks;
044:
045: protected int offset;
046:
047: protected int size;
048:
049: protected int offsets[];
050:
051: boolean dataChanged = true;
052: boolean dataTaken = false;
053: DataBufferListener listener;
054:
055: static {
056: AwtImageBackdoorAccessorImpl.init();
057: }
058:
059: protected DataBuffer(int dataType, int size, int numBanks,
060: int[] offsets) {
061: this .dataType = dataType;
062: this .size = size;
063: this .banks = numBanks;
064: this .offsets = offsets.clone();
065: this .offset = offsets[0];
066: }
067:
068: protected DataBuffer(int dataType, int size, int numBanks,
069: int offset) {
070: this .dataType = dataType;
071: this .size = size;
072: this .banks = numBanks;
073: this .offset = offset;
074: this .offsets = new int[numBanks];
075: int i = 0;
076: while (i < numBanks) {
077: offsets[i++] = offset;
078: }
079: }
080:
081: protected DataBuffer(int dataType, int size, int numBanks) {
082: this .dataType = dataType;
083: this .size = size;
084: this .banks = numBanks;
085: this .offset = 0;
086: this .offsets = new int[numBanks];
087: }
088:
089: protected DataBuffer(int dataType, int size) {
090: this .dataType = dataType;
091: this .size = size;
092: this .banks = 1;
093: this .offset = 0;
094: this .offsets = new int[1];
095: }
096:
097: public abstract void setElem(int bank, int i, int val);
098:
099: public void setElemFloat(int bank, int i, float val) {
100: setElem(bank, i, (int) val);
101: }
102:
103: public void setElemDouble(int bank, int i, double val) {
104: setElem(bank, i, (int) val);
105: }
106:
107: public void setElem(int i, int val) {
108: setElem(0, i, val);
109: }
110:
111: public abstract int getElem(int bank, int i);
112:
113: public float getElemFloat(int bank, int i) {
114: return getElem(bank, i);
115: }
116:
117: public double getElemDouble(int bank, int i) {
118: return getElem(bank, i);
119: }
120:
121: public void setElemFloat(int i, float val) {
122: setElemFloat(0, i, val);
123: }
124:
125: public void setElemDouble(int i, double val) {
126: setElemDouble(0, i, val);
127: }
128:
129: public int getElem(int i) {
130: return getElem(0, i);
131: }
132:
133: public float getElemFloat(int i) {
134: return getElem(0, i);
135: }
136:
137: public double getElemDouble(int i) {
138: return getElem(i);
139: }
140:
141: public int[] getOffsets() {
142: return offsets;
143: }
144:
145: public int getSize() {
146: return size;
147: }
148:
149: public int getOffset() {
150: return offset;
151: }
152:
153: public int getNumBanks() {
154: return banks;
155: }
156:
157: public int getDataType() {
158: return this .dataType;
159: }
160:
161: public static int getDataTypeSize(int type) {
162: switch (type) {
163:
164: case TYPE_BYTE:
165: return 8;
166:
167: case TYPE_USHORT:
168: case TYPE_SHORT:
169: return 16;
170:
171: case TYPE_INT:
172: case TYPE_FLOAT:
173: return 32;
174:
175: case TYPE_DOUBLE:
176: return 64;
177:
178: default:
179: // awt.22C=Unknown data type {0}
180: throw new IllegalArgumentException(Messages.getString(
181: "awt.22C", type)); //$NON-NLS-1$
182: }
183: }
184:
185: void notifyChanged() {
186: if (listener != null && !dataChanged) {
187: dataChanged = true;
188: listener.dataChanged();
189: }
190: }
191:
192: void notifyTaken() {
193: if (listener != null && !dataTaken) {
194: dataTaken = true;
195: listener.dataTaken();
196: }
197: }
198:
199: void releaseData() {
200: if (listener != null && dataTaken) {
201: dataTaken = false;
202: listener.dataReleased();
203: }
204: }
205:
206: void addDataBufferListener(DataBufferListener listener) {
207: this .listener = listener;
208: }
209:
210: void removeDataBufferListener() {
211: listener = null;
212: }
213:
214: void validate() {
215: dataChanged = false;
216: }
217:
218: }
|