001: /*
002: * $RCSfile: ImageComponent3DRetained.java,v $
003: *
004: * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.10 $
028: * $Date: 2008/02/28 20:17:23 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.awt.image.*;
035:
036: /**
037: * This class defines a 3D array of pixels.
038: * This is used for texture images.
039: */
040:
041: class ImageComponent3DRetained extends ImageComponentRetained {
042:
043: void setDepth(int depth) {
044: this .depth = depth;
045: }
046:
047: /**
048: * Retrieves the depth of this 3D image component object.
049: * @return the format of this 3D image component object
050: */
051: int getDepth() {
052: return depth;
053: }
054:
055: /**
056: * Copies the specified BufferedImage to this 3D image component
057: * object at the specified index.
058: * @param index the image index
059: * @param images BufferedImage object containing the image.
060: * The format and size must be the same as the current format in this
061: * ImageComponent3D object. The index must not exceed the depth of this
062: * ImageComponent3D object.
063: */
064: void set(int index, BufferedImage image) {
065:
066: geomLock.getLock();
067:
068: if (byReference) {
069: // Fix to issue 488.
070: setRefImage(image, index);
071: }
072:
073: if (imageData == null) {
074: // Only do this once, on the first image
075: // Reset this flag to true, incase it was set to false due to
076: // the previous image type.
077: abgrSupported = true;
078: imageTypeIsSupported = isImageTypeSupported(image);
079: imageData = createRenderedImageDataObject(null);
080: } else {
081: if (getImageType() != evaluateImageType(image)) {
082: // TODO need to throw illegal state exception
083: }
084: }
085:
086: if (imageTypeIsSupported) {
087: copySupportedImageToImageData(image, index, imageData);
088: } else {
089: // image type is unsupported, need to create a supported local copy.
090: // TODO : borrow code from JAI to convert to right format.
091: copyUnsupportedImageToImageData(image, index, imageData);
092:
093: }
094:
095: geomLock.unLock();
096:
097: if (source.isLive()) {
098: // send a IMAGE_CHANGED message in order to
099: // notify all the users of the change
100: sendMessage(IMAGE_CHANGED, null);
101: }
102: }
103:
104: /**
105: * Copies the specified BufferedImage to this 3D image component
106: * object at the specified index.
107: * @param index the image index
108: * @param images BufferedImage object containing the image.
109: * The format and size must be the same as the current format in this
110: * ImageComponent3D object. The index must not exceed the depth of this
111: * ImageComponent3D object.
112: *
113: void set(int index, NioImageBuffer nioImage) {
114:
115: int width = nioImage.getWidth();
116: int height = nioImage.getHeight();
117:
118: if (!byReference) {
119: throw new IllegalArgumentException(J3dI18N.getString("Need_New_Message_XXXXXImageComponent2D7"));
120: }
121: if (!yUp) {
122: throw new IllegalArgumentException(J3dI18N.getString("Need_New_Message_XXXXXImageComponent2D8"));
123: }
124:
125: if (width != this.width) {
126: throw new IllegalArgumentException(J3dI18N.getString("ImageComponent3D2"));
127: }
128: if (height != this.height) {
129: throw new IllegalArgumentException(J3dI18N.getString("ImageComponent3D4"));
130: }
131:
132: geomLock.getLock();
133:
134: setImageClass(nioImage);
135:
136: // This is a byRef image.
137: setRefImage(nioImage,0);
138:
139: if(imageData == null) {
140: // Only do this once, on the first image
141: // Reset this flag to true, incase it was set to false due to
142: // the previous image type.
143: abgrSupported = true;
144:
145: imageTypeIsSupported = isImageTypeSupported(nioImage);
146:
147:
148: // TODO : Need to handle null ....
149: imageData = createNioImageBufferDataObject(null);
150: }
151: else {
152:
153: //if(getImageType() != evaluateImageType(image)) {
154: // TODO need to throw illegal state exception
155: //}
156:
157: }
158:
159: if (imageTypeIsSupported) {
160: // TODO : Need to handle this ..... case ....
161: // copySupportedImageToImageData(image, index, imageData);
162: } else {
163: // System.err.println("Image format is unsupported -- illogical case");
164: throw new AssertionError();
165: }
166:
167: geomLock.unLock();
168:
169: if (source.isLive()) {
170: // send a IMAGE_CHANGED message in order to
171: // notify all the users of the change
172: sendMessage(IMAGE_CHANGED, null);
173: }
174: }
175: */
176:
177: void set(int index, RenderedImage image) {
178:
179: int width = image.getWidth();
180: int height = image.getHeight();
181:
182: if (width != this .width) {
183: throw new IllegalArgumentException(J3dI18N
184: .getString("ImageComponent3D2"));
185: }
186: if (height != this .height) {
187: throw new IllegalArgumentException(J3dI18N
188: .getString("ImageComponent3D4"));
189: }
190:
191: if (image instanceof BufferedImage) {
192: set(index, ((BufferedImage) image));
193: } else {
194: // Create a buffered image from renderImage
195: ColorModel cm = image.getColorModel();
196: WritableRaster wRaster = image.copyData(null);
197: BufferedImage bi = new BufferedImage(cm, wRaster, cm
198: .isAlphaPremultiplied(), null);
199: set(index, bi);
200: }
201: }
202:
203: /**
204: * Retrieves a copy of the images in this ImageComponent3D object.
205: * @return a new array of new BufferedImage objects created from the
206: * images in this ImageComponent3D object
207: */
208: RenderedImage[] getRenderedImage() {
209: int i;
210: RenderedImage bi[] = new RenderedImage[depth];
211:
212: if (!byReference) {
213: for (i = 0; i < depth; i++) {
214: bi[i] = imageData.createBufferedImage(i);
215: }
216: } else {
217: for (i = 0; i < depth; i++) {
218: bi[i] = imageData.createBufferedImage(i);
219: }
220: }
221:
222: return bi;
223: }
224:
225: /**
226: * Retrieves a copy of the images in this ImageComponent3D object.
227: * @return a new array of new BufferedImage objects created from the
228: * images in this ImageComponent3D object
229: */
230: BufferedImage[] getImage() {
231: int i;
232: BufferedImage bi[] = new BufferedImage[depth];
233:
234: if (!byReference) {
235: for (i = 0; i < depth; i++) {
236: bi[i] = imageData.createBufferedImage(i);
237: }
238: } else {
239: for (i = 0; i < depth; i++) {
240: bi[i] = imageData.createBufferedImage(i);
241: if (!(bi[i] instanceof BufferedImage)) {
242: throw new IllegalStateException(J3dI18N
243: .getString("ImageComponent3DRetained0"));
244: }
245:
246: }
247: }
248: return bi;
249: }
250:
251: /**
252: * Retrieves a copy of one of the images in this ImageComponent3D object.
253: * @param index the index of the image to retrieve
254: * @return a new BufferedImage objects created from the
255: * image at the specified index in this ImageComponent3D object
256: */
257: RenderedImage getImage(int index) {
258: if (!byReference) {
259: return imageData.createBufferedImage(index);
260: }
261:
262: return (RenderedImage) getRefImage(index);
263: }
264:
265: /**
266: * Update data.
267: * x and y specifies the x & y offset of the image data in
268: * ImageComponent. It assumes that the origin is (0, 0).
269: */
270: void updateData(ImageComponent3D.Updater updater, int index, int x,
271: int y, int width, int height) {
272:
273: geomLock.getLock();
274:
275: // call the user supplied updateData method to update the data
276: updater.updateData((ImageComponent3D) source, index, x, y,
277: width, height);
278:
279: RenderedImage refImage = (RenderedImage) getRefImage(index);
280: assert (refImage != null);
281: assert (imageData != null);
282:
283: // update the internal copy of the image data if a copy has been
284: // made
285: int srcX = x + refImage.getMinX();
286: int srcY = y + refImage.getMinY();
287:
288: if (imageTypeIsSupported) {
289: if (refImage instanceof BufferedImage) {
290: copyImageLineByLine((BufferedImage) refImage, srcX,
291: srcY, x, y, index, width, height, imageData);
292: } else {
293: copySupportedImageToImageData(refImage, srcX, srcY, x,
294: y, index, width, height, imageData);
295: }
296: } else {
297: // image type is unsupported, need to create a supported local copy.
298: // TODO : Should look into borrow code from JAI to convert to right format.
299: if (refImage instanceof BufferedImage) {
300: copyUnsupportedImageToImageData(
301: (BufferedImage) refImage, srcX, srcY, x, y,
302: index, width, height, imageData);
303: } else {
304: copyUnsupportedImageToImageData(refImage, srcX, srcY,
305: x, y, index, width, height, imageData);
306: }
307: }
308:
309: geomLock.unLock();
310:
311: if (source.isLive()) {
312:
313: // send a SUBIMAGE_CHANGED message in order to
314: // notify all the users of the change
315:
316: ImageComponentUpdateInfo info;
317:
318: info = new ImageComponentUpdateInfo();
319: info.x = x;
320: info.y = y;
321: info.z = index;
322: info.width = width;
323: info.height = height;
324:
325: sendMessage(SUBIMAGE_CHANGED, info);
326: }
327: }
328:
329: void setSubImage(int index, RenderedImage image, int width,
330: int height, int srcX, int srcY, int dstX, int dstY) {
331:
332: if (!isSubImageTypeEqual(image)) {
333: throw new IllegalStateException(J3dI18N
334: .getString("ImageComponent2D6"));
335: }
336:
337: // Can't be byReference
338: assert (!byReference);
339: assert (imageData != null);
340:
341: geomLock.getLock();
342:
343: if (imageTypeIsSupported) {
344: // Either not byRef or not yUp or not both
345: // System.err.println("ImageComponen3DRetained.setSubImage() : (imageTypeSupported ) --- (1)");
346: if (image instanceof BufferedImage) {
347: copyImageLineByLine((BufferedImage) image, srcX, srcY,
348: dstX, dstY, index, width, height, imageData);
349: } else {
350: copySupportedImageToImageData(image, srcX, srcY, dstX,
351: dstY, index, width, height, imageData);
352: }
353: } else {
354: // image type is unsupported, need to create a supported local copy.
355: // TODO : Should look into borrow code from JAI to convert to right format.
356: // System.err.println("ImageComponent3DRetained.setSubImage() : (imageTypeSupported == false) --- (2)");
357: if (image instanceof BufferedImage) {
358: copyUnsupportedImageToImageData((BufferedImage) image,
359: srcX, srcY, dstX, dstY, index, width, height,
360: imageData);
361: } else {
362: copyUnsupportedImageToImageData(image, srcX, srcY,
363: dstX, dstY, index, width, height, imageData);
364: }
365: }
366:
367: geomLock.unLock();
368:
369: if (source.isLive()) {
370:
371: // send a SUBIMAGE_CHANGED message in order to
372: // notify all the users of the change
373:
374: ImageComponentUpdateInfo info;
375:
376: info = new ImageComponentUpdateInfo();
377: info.x = dstX;
378: info.y = dstY;
379: info.z = index;
380: info.width = width;
381: info.height = height;
382:
383: sendMessage(SUBIMAGE_CHANGED, info);
384: }
385: }
386: }
|