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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
|
/*
* Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
* ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
* DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use
* in the design, construction, operation or maintenance of any nuclear
* facility.
*
* Sun gratefully acknowledges that this software was originally authored
* and developed by Kenneth Bradley Russell and Christopher John Kline.
*/
package com.sun.opengl.utils;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.nio.*;
import java.util.*;
import javax.imageio.*;
import javax.media.opengl.*;
/** <P> Provides input and output facilities for both loading OpenGL
textures from disk and streams as well as writing textures already
in memory back to disk. </P>
<P> The TextureIO class supports an arbitrary number of plug-in
TextureProviders which know how to produce TextureData objects
from files, InputStreams and URLs. The TextureData class
represents the raw data of the texture before it has been
converted to an OpenGL texture object. The Texture class
represents the OpenGL texture object and provides easy facilities
for using the texture. </P>
<P> There are several built-in TextureProviders supplied with the
TextureIO implementation. The most basic provider uses the
platform's Image I/O facilities to read in a BufferedImage and
convert it to a texture. This is the baseline provider and is
registered so that it is the last one consulted. All others are
asked first to open a given file. </P>
<P> There are three other providers registered by default as of
the time of this writing. One handles SGI RGB (".sgi", ".rgb")
images from both files and streams. One handles DirectDraw Surface
(".dds") images read from files, though can not read these images
from streams. One handles Targa (".tga") images read from both
files and streams. These providers are executed in an arbitrary
order. Some of these providers require the file's suffix to either
be specified via the newTextureData methods or for the file to be
named with the appropriate suffix. In general a file suffix should
be provided to the newTexture and newTextureData methods if at all
possible. </P>
<P> Note that additional TextureProviders, if reading images from
InputStreams, must use the mark()/reset() methods on InputStream
when probing for e.g. magic numbers at the head of the file to
make sure not to disturb the state of the InputStream for
downstream TextureProviders. </P>
*/
public class TextureIO {
/** Constant which can be used as a file suffix to indicate a
DirectDraw Surface file. */
public static final String DDS = "dds";
/** Constant which can be used as a file suffix to indicate an SGI
RGB file. */
public static final String SGI = "sgi";
/** Constant which can be used as a file suffix to indicate an SGI
RGB file. */
public static final String SGI_RGB = "rgb";
/** Constant which can be used as a file suffix to indicate a GIF
file. */
public static final String GIF = "gif";
/** Constant which can be used as a file suffix to indicate a JPEG
file. */
public static final String JPG = "jpg";
/** Constant which can be used as a file suffix to indicate a PNG
file. */
public static final String PNG = "png";
/** Constant which can be used as a file suffix to indicate a Targa
file. */
public static final String TGA = "tga";
/** Constant which can be used as a file suffix to indicate a TIFF
file. */
public static final String TIFF = "tiff";
//----------------------------------------------------------------------
// methods that *do not* require a current context
// These methods assume RGB or RGBA textures.
// Some texture providers may not recognize the file format unless
// the fileSuffix is specified, so it is strongly recommended to
// specify it wherever it is known.
// Some texture providers may also only support one kind of input,
// i.e., reading from a file as opposed to a stream.
/**
* Creates a TextureData representing the specified mipmap level of
* a texture from the given file. Does no OpenGL work.
*
* @param file the file from which to read the texture data
* @param mipmapLevel the mipmap level this data represents (FIXME:
* not currently used, needs to be rethought)
* @param fileSuffix the suffix of the file name to be used as a
* hint of the file format to the underlying
* texture provider, or null if none and should be
* auto-detected (some texture providers do not
* support this)
* @return the texture data from the file, or null if none of the
* registered texture providers could read the file
* @throws IOException if an error occurred while reading the file
*/
public static TextureData newTextureData(File file,
int mipmapLevel,
String fileSuffix) throws IOException {
return newTextureDataImpl(file, mipmapLevel, 0, 0, fileSuffix);
}
/**
* Creates a TextureData representing the specified mipmap level of
* a texture from the given stream. Does no OpenGL work.
*
* @param stream the stream from which to read the texture data
* @param mipmapLevel the mipmap level this data represents (FIXME:
* not currently used, needs to be rethought)
* @param fileSuffix the suffix of the file name to be used as a
* hint of the file format to the underlying
* texture provider, or null if none and should be
* auto-detected (some texture providers do not
* support this)
* @return the texture data from the stream, or null if none of the
* registered texture providers could read the stream
* @throws IOException if an error occurred while reading the stream
*/
public static TextureData newTextureData(InputStream stream,
int mipmapLevel,
String fileSuffix) throws IOException {
return newTextureDataImpl(stream, mipmapLevel, 0, 0, fileSuffix);
}
/**
* Creates a TextureData representing the specified mipmap level of
* a texture from the given URL. Does no OpenGL work.
*
* @param url the URL from which to read the texture data
* @param mipmapLevel the mipmap level this data represents (FIXME:
* not currently used, needs to be rethought)
* @param fileSuffix the suffix of the file name to be used as a
* hint of the file format to the underlying
* texture provider, or null if none and should be
* auto-detected (some texture providers do not
* support this)
* @return the texture data from the URL, or null if none of the
* registered texture providers could read the URL
* @throws IOException if an error occurred while reading the URL
*/
public static TextureData newTextureData(URL url,
int mipmapLevel,
String fileSuffix) throws IOException {
return newTextureDataImpl(url, mipmapLevel, 0, 0, fileSuffix);
}
/**
* Creates a TextureData representing the specified mipmap level of
* a texture from the given BufferedImage. Does no OpenGL work.
*
* @param image the BufferedImage containing the texture data
* @param mipmapLevel the mipmap level this data represents (FIXME:
* not currently used, needs to be rethought)
* @return the texture data from the image
*/
public static TextureData newTextureData(BufferedImage image,
int mipmapLevel) {
return newTextureDataImpl(image, mipmapLevel, 0, 0);
}
//----------------------------------------------------------------------
// These methods make no assumption about the OpenGL internal format
// or pixel format of the texture; they must be specified by the
// user. It is not allowed to supply 0 (indicating no preference)
// for either the internalFormat or the pixelFormat;
// IllegalArgumentException will be thrown in this case.
/**
* Creates a TextureData representing the specified mipmap level of
* a texture from the given file, using the specified OpenGL
* internal format and pixel format for the texture which will
* eventually result. The internalFormat and pixelFormat must be
* specified and may not be zero; to use default values, use the
* variant of this method which does not take these arguments. Does
* no OpenGL work.
*
* @param file the file from which to read the texture data
* @param mipmapLevel the mipmap level this data represents (FIXME:
* not currently used, needs to be rethought)
* @param internalFormat the OpenGL internal format of the texture
* which will eventually result from the TextureData
* @param pixelFormat the OpenGL pixel format of the texture
* which will eventually result from the TextureData
* @param fileSuffix the suffix of the file name to be used as a
* hint of the file format to the underlying
* texture provider, or null if none and should be
* auto-detected (some texture providers do not
* support this)
* @return the texture data from the file, or null if none of the
* registered texture providers could read the file
* @throws IllegalArgumentException if either internalFormat or
* pixelFormat was 0
* @throws IOException if an error occurred while reading the file
*/
public static TextureData newTextureData(File file,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException, IllegalArgumentException {
if ((internalFormat == 0) || (pixelFormat == 0)) {
throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero");
}
return newTextureDataImpl(file, mipmapLevel, internalFormat, pixelFormat, fileSuffix);
}
/**
* Creates a TextureData representing the specified mipmap level of
* a texture from the given stream, using the specified OpenGL
* internal format and pixel format for the texture which will
* eventually result. The internalFormat and pixelFormat must be
* specified and may not be zero; to use default values, use the
* variant of this method which does not take these arguments. Does
* no OpenGL work.
*
* @param stream the stream from which to read the texture data
* @param mipmapLevel the mipmap level this data represents (FIXME:
* not currently used, needs to be rethought)
* @param internalFormat the OpenGL internal format of the texture
* which will eventually result from the TextureData
* @param pixelFormat the OpenGL pixel format of the texture
* which will eventually result from the TextureData
* @param fileSuffix the suffix of the file name to be used as a
* hint of the file format to the underlying
* texture provider, or null if none and should be
* auto-detected (some texture providers do not
* support this)
* @return the texture data from the stream, or null if none of the
* registered texture providers could read the stream
* @throws IllegalArgumentException if either internalFormat or
* pixelFormat was 0
* @throws IOException if an error occurred while reading the stream
*/
public static TextureData newTextureData(InputStream stream,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException, IllegalArgumentException {
if ((internalFormat == 0) || (pixelFormat == 0)) {
throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero");
}
return newTextureDataImpl(stream, mipmapLevel, internalFormat, pixelFormat, fileSuffix);
}
/**
* Creates a TextureData representing the specified mipmap level of
* a texture from the given URL, using the specified OpenGL
* internal format and pixel format for the texture which will
* eventually result. The internalFormat and pixelFormat must be
* specified and may not be zero; to use default values, use the
* variant of this method which does not take these arguments. Does
* no OpenGL work.
*
* @param url the URL from which to read the texture data
* @param mipmapLevel the mipmap level this data represents (FIXME:
* not currently used, needs to be rethought)
* @param internalFormat the OpenGL internal format of the texture
* which will eventually result from the TextureData
* @param pixelFormat the OpenGL pixel format of the texture
* which will eventually result from the TextureData
* @param fileSuffix the suffix of the file name to be used as a
* hint of the file format to the underlying
* texture provider, or null if none and should be
* auto-detected (some texture providers do not
* support this)
* @return the texture data from the URL, or null if none of the
* registered texture providers could read the URL
* @throws IllegalArgumentException if either internalFormat or
* pixelFormat was 0
* @throws IOException if an error occurred while reading the URL
*/
public static TextureData newTextureData(URL url,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException, IllegalArgumentException {
if ((internalFormat == 0) || (pixelFormat == 0)) {
throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero");
}
return newTextureDataImpl(url, mipmapLevel, internalFormat, pixelFormat, fileSuffix);
}
/**
* Creates a TextureData representing the specified mipmap level of
* a texture from the given BufferedImage, using the specified
* OpenGL internal format and pixel format for the texture which
* will eventually result. The internalFormat and pixelFormat must
* be specified and may not be zero; to use default values, use the
* variant of this method which does not take these arguments. Does
* no OpenGL work.
*
* @param image the BufferedImage containing the texture data
* @param mipmapLevel the mipmap level this data represents (FIXME:
* not currently used, needs to be rethought)
* @param internalFormat the OpenGL internal format of the texture
* which will eventually result from the TextureData
* @param pixelFormat the OpenGL pixel format of the texture
* which will eventually result from the TextureData
* @return the texture data from the image
* @throws IllegalArgumentException if either internalFormat or
* pixelFormat was 0
*/
public static TextureData newTextureData(BufferedImage image,
int mipmapLevel,
int internalFormat,
int pixelFormat) throws IllegalArgumentException {
if ((internalFormat == 0) || (pixelFormat == 0)) {
throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero");
}
return newTextureDataImpl(image, mipmapLevel, internalFormat, pixelFormat);
}
//----------------------------------------------------------------------
// methods that *do* require a current context
//
/**
* Creates an OpenGL texture object from the specified TextureData
* using the current OpenGL context.
*
* @param data the texture data to turn into an OpenGL texture
* @throws GLException if no OpenGL context is current or if an
* OpenGL error occurred
*/
public static Texture newTexture(TextureData data) throws GLException {
if (data == null) {
return null;
}
return new Texture(data);
}
/**
* Creates an OpenGL texture object from the specified file using
* the current OpenGL context.
*
* @param file the file from which to read the texture data
* @throws IOException if an error occurred while reading the file
* @throws GLException if no OpenGL context is current or if an
* OpenGL error occurred
*/
public static Texture newTexture(File file) throws IOException, GLException {
TextureData data = newTextureData(file, 0, getFileSuffix(file));
Texture texture = newTexture(data);
data.flush();
return texture;
}
/**
* Creates an OpenGL texture object from the specified stream using
* the current OpenGL context.
*
* @param stream the stream from which to read the texture data
* @throws IOException if an error occurred while reading the stream
* @throws GLException if no OpenGL context is current or if an
* OpenGL error occurred
*/
public static Texture newTexture(InputStream stream) throws IOException, GLException {
TextureData data = newTextureData(stream, 0, null);
Texture texture = newTexture(data);
data.flush();
return texture;
}
/**
* Creates an OpenGL texture object from the specified URL using
* the current OpenGL context.
*
* @param url the URL from which to read the texture data
* @throws IOException if an error occurred while reading the URL
* @throws GLException if no OpenGL context is current or if an
* OpenGL error occurred
*/
public static Texture newTexture(URL url) throws IOException, GLException {
TextureData data = newTextureData(url, 0, null);
Texture texture = newTexture(data);
data.flush();
return texture;
}
/**
* Creates an OpenGL texture object from the specified BufferedImage
* using the current OpenGL context.
*
* @param image the BufferedImage from which to read the texture data
* @throws GLException if no OpenGL context is current or if an
* OpenGL error occurred
*/
public static Texture newTexture(BufferedImage image) throws GLException {
TextureData data = newTextureData(image, 0);
Texture texture = newTexture(data);
data.flush();
return texture;
}
// FIXME: add texture writing capabilities
// public void writeTextureToFile(Texture texture, File file, boolean saveUncompressed) throws IOException, GLException;
//----------------------------------------------------------------------
// SPI support
//
/** Adds a TextureProvider to support reading of a new file
format. */
public static void addTextureProvider(TextureProvider provider) {
// Must always add at the front so the ImageIO provider is last,
// so we don't accidentally use it instead of a user's possibly
// more optimal provider
textureProviders.add(0, provider);
}
//----------------------------------------------------------------------
// Internals only below this point
//
private static List/*<TextureProvider>*/ textureProviders = new ArrayList/*<TextureProvider>*/();
static {
// ImageIO provider, the fall-back, must be the first one added
addTextureProvider(new IIOTextureProvider());
// Other special-case providers
addTextureProvider(new DDSTextureProvider());
addTextureProvider(new SGITextureProvider());
addTextureProvider(new TGATextureProvider());
}
// Implementation methods
private static TextureData newTextureDataImpl(File file,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
for (Iterator iter = textureProviders.iterator(); iter.hasNext(); ) {
TextureProvider provider = (TextureProvider) iter.next();
TextureData data = provider.newTextureData(file,
mipmapLevel,
internalFormat,
pixelFormat,
fileSuffix);
if (data != null) {
return data;
}
}
return null;
}
private static TextureData newTextureDataImpl(InputStream stream,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
for (Iterator iter = textureProviders.iterator(); iter.hasNext(); ) {
TextureProvider provider = (TextureProvider) iter.next();
TextureData data = provider.newTextureData(stream,
mipmapLevel,
internalFormat,
pixelFormat,
fileSuffix);
if (data != null) {
return data;
}
}
return null;
}
private static TextureData newTextureDataImpl(URL url,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
for (Iterator iter = textureProviders.iterator(); iter.hasNext(); ) {
TextureProvider provider = (TextureProvider) iter.next();
TextureData data = provider.newTextureData(url,
mipmapLevel,
internalFormat,
pixelFormat,
fileSuffix);
if (data != null) {
return data;
}
}
return null;
}
private static TextureData newTextureDataImpl(BufferedImage image,
int mipmapLevel,
int internalFormat,
int pixelFormat) {
return new TextureData(mipmapLevel, internalFormat, pixelFormat, image);
}
//----------------------------------------------------------------------
// Base provider - used last
static class IIOTextureProvider implements TextureProvider {
public TextureData newTextureData(File file,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
BufferedImage img = ImageIO.read(file);
if (img == null) {
return null;
}
return new TextureData(mipmapLevel, internalFormat, pixelFormat, img);
}
public TextureData newTextureData(InputStream stream,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
BufferedImage img = ImageIO.read(stream);
if (img == null) {
return null;
}
return new TextureData(mipmapLevel, internalFormat, pixelFormat, img);
}
public TextureData newTextureData(URL url,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
InputStream stream = url.openStream();
try {
return newTextureData(stream, mipmapLevel, internalFormat, pixelFormat, fileSuffix);
} finally {
stream.close();
}
}
}
//----------------------------------------------------------------------
// DDS provider -- supports files only for now
static class DDSTextureProvider implements TextureProvider {
public TextureData newTextureData(File file,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
if (DDS.equals(fileSuffix) ||
DDS.equals(getFileSuffix(file))) {
final DDSReader reader = new DDSReader();
reader.loadFile(file);
// FIXME: handle case where all mipmaps are requested -- this
// will require API changes
DDSReader.ImageInfo info = reader.getMipMap(mipmapLevel);
if (pixelFormat == 0) {
switch (reader.getPixelFormat()) {
case DDSReader.D3DFMT_R8G8B8:
pixelFormat = GL.GL_RGB;
break;
default:
pixelFormat = GL.GL_RGBA;
break;
}
}
if (info.isCompressed()) {
switch (info.getCompressionFormat()) {
case DDSReader.D3DFMT_DXT1:
internalFormat = GL.GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
break;
case DDSReader.D3DFMT_DXT3:
internalFormat = GL.GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
break;
case DDSReader.D3DFMT_DXT5:
internalFormat = GL.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
break;
default:
throw new RuntimeException("Unsupported DDS compression format \"" +
DDSReader.getCompressionFormatName(info.getCompressionFormat()) + "\"");
}
}
if (internalFormat == 0) {
switch (reader.getPixelFormat()) {
case DDSReader.D3DFMT_R8G8B8:
pixelFormat = GL.GL_RGB;
break;
default:
pixelFormat = GL.GL_RGBA;
break;
}
}
TextureData.Flusher flusher = new TextureData.Flusher() {
public void flush() {
reader.close();
}
};
TextureData data = new TextureData(mipmapLevel,
internalFormat,
info.getWidth(),
info.getHeight(),
0,
pixelFormat,
GL.GL_UNSIGNED_BYTE,
info.isCompressed(),
true,
info.getData(),
flusher);
return data;
}
return null;
}
public TextureData newTextureData(InputStream stream,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
return null;
}
public TextureData newTextureData(URL url,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
return null;
}
}
//----------------------------------------------------------------------
// Base class for SGI RGB and TGA image providers
static abstract class StreamBasedTextureProvider implements TextureProvider {
public TextureData newTextureData(File file,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
InputStream inStream = new BufferedInputStream(new FileInputStream(file));
try {
// The SGIImage and TGAImage implementations use InputStreams
// anyway so there isn't much point in having a separate code
// path for files
return newTextureData(inStream,
mipmapLevel,
internalFormat,
pixelFormat,
((fileSuffix != null) ? fileSuffix : getFileSuffix(file)));
} finally {
inStream.close();
}
}
public TextureData newTextureData(URL url,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
InputStream stream = url.openStream();
try {
return newTextureData(stream, mipmapLevel, internalFormat, pixelFormat, fileSuffix);
} finally {
stream.close();
}
}
}
//----------------------------------------------------------------------
// SGI RGB image provider
static class SGITextureProvider extends StreamBasedTextureProvider {
public TextureData newTextureData(InputStream stream,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
if (SGI.equals(fileSuffix) ||
SGI_RGB.equals(fileSuffix) ||
SGIImage.isSGIImage(stream)) {
SGIImage image = SGIImage.read(stream);
if (pixelFormat == 0) {
pixelFormat = image.getFormat();
}
if (internalFormat == 0) {
internalFormat = image.getFormat();
}
return new TextureData(mipmapLevel,
internalFormat,
image.getWidth(),
image.getHeight(),
0,
pixelFormat,
GL.GL_UNSIGNED_BYTE,
false,
false,
ByteBuffer.wrap(image.getData()),
null);
}
return null;
}
}
//----------------------------------------------------------------------
// TGA (Targa) image provider
static class TGATextureProvider extends StreamBasedTextureProvider {
public TextureData newTextureData(InputStream stream,
int mipmapLevel,
int internalFormat,
int pixelFormat,
String fileSuffix) throws IOException {
if (TGA.equals(fileSuffix)) {
TGAImage image = TGAImage.read(stream);
if (pixelFormat == 0) {
pixelFormat = image.getGLFormat();
}
if (internalFormat == 0) {
internalFormat = GL.GL_RGBA8;
}
return new TextureData(mipmapLevel,
internalFormat,
image.getWidth(),
image.getHeight(),
0,
pixelFormat,
GL.GL_UNSIGNED_BYTE,
false,
false,
ByteBuffer.wrap(image.getData()),
null);
}
return null;
}
}
//----------------------------------------------------------------------
// Helper function for above TextureProviders
private static String getFileSuffix(File file) {
String name = file.getName().toLowerCase();
int lastDot = name.lastIndexOf('.');
if (lastDot < 0) {
return null;
}
return name.substring(lastDot + 1);
}
}
|