1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
/*
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package javax.media.j3d;
/**
* Abstract class that is used to define 2D or 3D ImageComponent
* classes used in a Java 3D scene graph. This is used for texture
* images, background images and raster components of Shape3D nodes.
*
* <p>
* Image data may be passed to this ImageComponent object in
* one of two ways: by copying the image data into this object or by
* accessing the image data by reference.
*
* <p>
* <ul>
* <li>
* <b>By Copying:</b>
* By default, the set and get image methods copy the image
* data into or out of this ImageComponent object. This is
* appropriate for many applications, since the application may reuse
* the RenderedImage object after copying it to the ImageComponent.
* </li>
* <li><b>By Reference:</b>
* A new feature in Java 3D version 1.2 allows image data to
* be accessed by reference, directly from the RenderedImage object.
* To use this feature, you need to construct an ImageComponent object
* with the <code>byReference</code> flag set to <code>true</code>.
* In this mode, a reference to the input data is saved, but the data
* itself is not necessarily copied (although it may be, depending on
* the value of the <code>yUp</code> flag, the format of the
* ImageComponent, and the format of the RenderedImage). Image data
* referenced by an ImageComponent object can only be modified via
* the updateData method.
* Applications must exercise care not to violate this rule. If any
* referenced RenderedImage is modified outside the updateData method
* after it has been passed
* to an ImageComponent object, the results are undefined.
* Another restriction in by-reference mode is that if the specified
* RenderedImage is not an instance of BufferedImage, then
* this ImageComponent cannot be used for readRaster or
* off-screen rendering operations, since these operations modify
* the ImageComponent data.
* </li>
* </ul>
*
* <p>
* An image component object also specifies whether the orientation of
* its image data is "y-up" or "y-down" (the default). Y-up mode
* causes images to be interpreted as having their origin at the lower
* left (rather than the default upper left) of a texture or raster
* image with successive scan lines moving up. This is more
* consistent with texture mapping data onto a surface, and maps
* directly into the the way textures are used in OpenGL and other 3D
* APIs. Setting the <code>yUp</code> flag to true in conjunction
* with setting the <code>byReference</code> flag to true makes it
* possible for Java 3D to avoid copying the texture map in some
* cases.
*
* <p>
* Note that all color fields are treated as unsigned values, even though
* Java does not directly support unsigned variables. This means, for
* example, that an ImageComponent using a format of FORMAT_RGB5 can
* represent red, green, and blue values between 0 and 31, while an
* ImageComponent using a format of FORMAT_RGB8 can represent color
* values between 0 and 255. Even when byte values are used to create a
* RenderedImage with 8-bit color components, the resulting colors
* (bytes) are interpreted as if they were unsigned.
* Values greater than 127 can be assigned to a byte variable using a
* type cast. For example:
* <ul>byteVariable = (byte) intValue; // intValue can be > 127</ul>
* If intValue is greater than 127, then byteVariable will be negative. The
* correct value will be extracted when it is used (by masking off the upper
* bits).
*/
public abstract class ImageComponent extends NodeComponent {
//
// Pixel format values
//
/**
* Specifies that each pixel contains 3 8-bit channels: one each
* for red, green, blue. Same as FORMAT_RGB8.
*/
public static final int FORMAT_RGB = 1;
/**
* Specifies that each pixel contains 4 8-bit channels: one each
* for red, green, blue, alpha. Same as FORMAT_RGBA8.
*/
public static final int FORMAT_RGBA = 2;
/**
* Specifies that each pixel contains 3 8-bit channels: one each
* for red, green, blue. Same as FORMAT_RGB.
*/
public static final int FORMAT_RGB8 = FORMAT_RGB;
/**
* Specifies that each pixel contains 4 8-bit channels: one each
* for red, green, blue, alpha. Same as FORMAT_RGBA.
*/
public static final int FORMAT_RGBA8 = FORMAT_RGBA;
/**
* Specifies that each pixel contains 3 5-bit channels: one each
* for red, green, blue.
*/
public static final int FORMAT_RGB5 = 3;
/**
* Specifies that each pixel contains 3 5-bit channels: one each
* for red, green, blue and 1 1-bit channel for alpha.
*/
public static final int FORMAT_RGB5_A1 = 4;
/**
* Specifies that each pixel contains 3 4-bit channels: one each
* for red, green, blue.
*/
public static final int FORMAT_RGB4 = 5;
/**
* Specifies that each pixel contains 4 4-bit channels: one each
* for red, green, blue, alpha.
*/
public static final int FORMAT_RGBA4 = 6;
/**
* Specifies that each pixel contains 2 4-bit channels: one each
* for luminance and alpha.
*/
public static final int FORMAT_LUM4_ALPHA4 = 7;
/**
* Specifies that each pixel contains 2 8-bit channels: one each
* for luminance and alpha.
*/
public static final int FORMAT_LUM8_ALPHA8 = 8;
/**
* Specifies that each pixel contains 2 3-bit channels: one each
* for red, green, and 1 2-bit channel for blue.
*/
public static final int FORMAT_R3_G3_B2 = 9;
/**
* Specifies that each pixel contains 1 8-bit channel: it can be
* used for only luminance or only alpha or only intensity.
*/
public static final int FORMAT_CHANNEL8 = 10;
// Internal variable for checking validity of formats
// Change this if any more formats are added or removed
static final int FORMAT_TOTAL = 10;
/**
* Used to specify the class of the image being wrapped.
*
* @since Java 3D 1.5
*/
public enum ImageClass {
/**
* Indicates that this ImageComponent object wraps a BufferedImage
* object. This is the default state. Note that the image class will
* be BUFFERED_IMAGE following a call to set(RenderedImage image)
* if we are in by-copy mode, or if the image is an instance of
* BufferedImage.
*/
BUFFERED_IMAGE,
/**
* Indicates that this ImageComponent object wraps a RenderedImage
* object that is <i>not</i> a BufferedImage. Note that the image class
* of an ImageComponent following a call to set(RenderedImage image)
* will be RENDERED_IMAGE, if and only if the image is not an instance
* of BufferedImage and the ImageComponent is in by-reference mode.
*/
RENDERED_IMAGE,
/**
* Indicates that this ImageComponent object wraps an NioImageBuffer
* object. Note that an ImageComponent in this state must not be used
* as the off-screen buffer of a Canvas3D nor as the target of a
* readRaster operation.
*/
NIO_IMAGE_BUFFER,
}
/**
* Specifies that this ImageComponent object allows reading its
* size component information (width, height, and depth).
*/
public static final int
ALLOW_SIZE_READ = CapabilityBits.IMAGE_COMPONENT_ALLOW_SIZE_READ;
/**
* Specifies that this ImageComponent object allows reading its
* format component information.
*/
public static final int
ALLOW_FORMAT_READ = CapabilityBits.IMAGE_COMPONENT_ALLOW_FORMAT_READ;
/**
* Specifies that this ImageComponent object allows reading its
* image component information.
*/
public static final int
ALLOW_IMAGE_READ = CapabilityBits.IMAGE_COMPONENT_ALLOW_IMAGE_READ;
/**
* Specifies that this ImageComponent object allows writing its
* image component information.
*
* @since Java 3D 1.3
*/
public static final int
ALLOW_IMAGE_WRITE = CapabilityBits.IMAGE_COMPONENT_ALLOW_IMAGE_WRITE;
// Array for setting default read capabilities
private static final int[] readCapabilities = {
ALLOW_SIZE_READ,
ALLOW_IMAGE_READ,
ALLOW_FORMAT_READ
};
/**
* Not a public constructor, for internal use
*/
ImageComponent() {
// set default read capabilities
setDefaultReadCapabilities(readCapabilities);
}
/**
* Constructs an image component object using the specified format, width,
* and height. Default values are used for all other parameters. The
* default values are as follows:
* <ul>
* byReference : false<br>
* yUp : false<br>
* </ul>
*
* @param format the image component format, one of: FORMAT_RGB,
* FORMAT_RGBA etc.
* @param width the number of columns of pixels in this image component
* object
* @param height the number of rows of pixels in this image component
* object
* @exception IllegalArgumentException if format is invalid, or if
* width or height are not positive.
*/
public ImageComponent(int format, int width, int height) {
// set default read capabilities
setDefaultReadCapabilities(readCapabilities);
((ImageComponentRetained)this.retained).processParams(format, width, height, 1);
}
/**
* Constructs an image component object using the specified format, width,
* height, byReference flag, and yUp flag.
*
* @param format the image component format, one of: FORMAT_RGB,
* FORMAT_RGBA etc.
* @param width the number of columns of pixels in this image component
* object
* @param height the number of rows of pixels in this image component
* object
* @param byReference a flag that indicates whether the data is copied
* into this image component object or is accessed by reference.
* @param yUp a flag that indicates the y-orientation of this image
* component. If yUp is set to true, the origin of the image is
* the lower left; otherwise, the origin of the image is the upper
* left.
* @exception IllegalArgumentException if format is invalid, or if
* width or height are not positive.
*
* @since Java 3D 1.2
*/
public ImageComponent(int format,
int width,
int height,
boolean byReference,
boolean yUp) {
// set default read capabilities
setDefaultReadCapabilities(readCapabilities);
((ImageComponentRetained)this.retained).setYUp(yUp);
((ImageComponentRetained)this.retained).setByReference(byReference);
((ImageComponentRetained)this.retained).processParams(format, width, height, 1);
}
/**
* Retrieves the width of this image component object.
* @return the width of this image component object
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public int getWidth() {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_SIZE_READ))
throw new CapabilityNotSetException(J3dI18N.getString("ImageComponent0"));
return ((ImageComponentRetained)this.retained).getWidth();
}
/**
* Retrieves the height of this image component object.
* @return the height of this image component object
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public int getHeight() {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_SIZE_READ))
throw new CapabilityNotSetException(J3dI18N.getString("ImageComponent1"));
return ((ImageComponentRetained)this.retained).getHeight();
}
/**
* Retrieves the format of this image component object.
* @return the format of this image component object
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public int getFormat() {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_FORMAT_READ))
throw new CapabilityNotSetException(J3dI18N.getString("ImageComponent2"));
return ((ImageComponentRetained)this.retained).getFormat();
}
/**
* Retrieves the data access mode for this ImageComponent object.
*
* @return <code>true</code> if the data access mode for this
* ImageComponent object is by-reference;
* <code>false</code> if the data access mode is by-copying.
*
* @since Java 3D 1.2
*/
public boolean isByReference() {
return ((ImageComponentRetained)this.retained).isByReference();
}
/**
* Sets the y-orientation of this ImageComponent object to
* y-up or y-down.
*
* @param yUp a flag that indicates the y-orientation of this image
* component. If yUp is set to true, the origin of the image is
* the lower left; otherwise, the origin of the image is the upper
* left.
*
* @exception RestrictedAccessException if the method is called
* when this object is part of live or compiled scene graph.
*
* @exception IllegalStateException if the image class of this object
* is ImageClass.NIO_IMAGE_BUFFER.
*
* @deprecated as of Java 3D 1.5, the yUp flag should only be set at object
* construction time.
*
* @since Java 3D 1.2
*/
public void setYUp(boolean yUp) {
checkForLiveOrCompiled();
// check for illegal image class
if (((ImageComponentRetained)this.retained).getImageClass() == ImageClass.NIO_IMAGE_BUFFER) {
throw new IllegalStateException("ImageComponent4");
}
((ImageComponentRetained)this.retained).setYUp(yUp);
}
/**
* Retrieves the y-orientation for this ImageComponent object.
*
* @return <code>true</code> if the y-orientation of this
* ImageComponent object is y-up; <code>false</code> if the
* y-orientation of this ImageComponent object is y-down.
*
* @since Java 3D 1.2
*/
public boolean isYUp() {
return ((ImageComponentRetained)this.retained).isYUp();
}
/**
* Retrieves the image class of this ImageComponent object.
*
* @return the image class of this ImageComponent,
* one of: ImageClass.BUFFERED_IMAGE,
* ImageClass.RENDERED_IMAGE, or ImageClass.NIO_IMAGE_BUFFER.
*
* @since Java 3D 1.5
*/
public ImageClass getImageClass() {
return ((ImageComponentRetained)this.retained).getImageClass();
}
}
|