aboutsummaryrefslogtreecommitdiffstats
path: root/gl4java/utils/textures/AWTTextureLoader.java
blob: 2df31455cd25b746b754bddbf729a52a7a1ce262 (plain)
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
package gl4java.utils.textures;

import gl4java.*;

import java.awt.*;
import java.awt.image.*;
// import java.awt.Color.*;
// import java.awt.color.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;

/**
 * This Class implements the universal
 * texture-loader using the AWT's standard interface !
 *
 * The number of image-types depends
 * on the JDK version you use !
 *
 * @see TextureLoader
 */   
public class AWTTextureLoader
extends TextureLoader
{
	Component comp;

	public AWTTextureLoader(Component comp, GLFunc gl, GLUFunc glu)
	{
		super(gl, glu);
		this.comp=comp;
	}

	public boolean readTexture(String fname)
	{
	    Image img = comp.getToolkit().getImage(fname);
	    return readTexture(comp, img);
	}

	public boolean readTexture(URL base, String uri)
	{
	    try {
		    URL url = new URL (base, uri);
	    	    Image img = comp.getToolkit().getImage(url);
	    	    return readTexture(comp, img);
	    } catch (Exception ex) {
		System.out.println("AWTTextureLoader.readTexture <"+
			base+" / "+uri+"> failed !\n"+ex);
	    }
	    return false;
	}

	private boolean readTexture(Component comp, Image img)
	{
	  try {
            try {
                MediaTracker tracker = new MediaTracker(comp);
                tracker.addImage(img, 0);
                tracker.waitForID(0);
            }
            catch ( Exception e ) {}

            imageWidth = img.getWidth(comp);
            imageHeight = img.getHeight(comp);
	    /* This is Java2 only code :-(
            BufferedImage image = 
	    	new BufferedImage(imageWidth, imageHeight, 
		                  BufferedImage.TYPE_INT_RGB);

            Graphics g = image.createGraphics();
            g.drawImage(img,0,0,comp);

            imageWidth = image.getWidth();
	    imageHeight = image.getHeight();
	    */

            // Read entire PNG image (doesn't throw exceptions)
            int[] iPixels = new int[imageWidth * imageHeight];

            PixelGrabber pp=new PixelGrabber(img,
                                             0,0,
                                             imageWidth, imageHeight,
                                             iPixels,
                                             0,
                                             imageWidth);
            try 
            {
                pp.grabPixels();
            } 
            catch (InterruptedException e) 
            {
                System.err.println("interrupted waiting for pixel!");
		error=true;
                return false;
            }
            if ((pp.getStatus() & ImageObserver.ABORT) != 0) 
            {
                System.err.println("image fetch aborted or errored");
		error=true;
                return false;
            }

	    /* This is Java2 only code :-(
            int imagetype = image.getType();
            switch(imagetype)
            {
                case BufferedImage.TYPE_INT_RGB:
                    glFormat=GL_RGB;
                    break;
                case BufferedImage.TYPE_INT_ARGB:
                case BufferedImage.TYPE_INT_ARGB_PRE:
                    glFormat=GL_RGBA;
                    break;
                default:
		    error=true;
                    System.err.println("unsupported format: "+imagetype);
                    return false;
            };
	    */
	    //
	    // we are guessing the RGB type,
	    // because fetching the true type
	    // is Java2 only code :-(
	    //
	    glFormat=GL_RGB;

	    setTextureSize();
            pixel=new byte[imageWidth * imageHeight * getComponents()];
            
            int offset=0;
	    int aPixel;
	    int y_desc;
            for(y_desc=imageHeight-1; y_desc>=0; y_desc--)
	    {
              for(int x=0;x<imageWidth;x++)
              {
	        aPixel = iPixels[y_desc*imageWidth + x];

		// red
                pixel[offset++]=
			new Integer( (aPixel  >> 16) & 0xff ).byteValue();

		// green 
                pixel[offset++]=
			new Integer( (aPixel  >>  8) & 0xff ).byteValue();

		// blue 
                pixel[offset++]=
			new Integer( (aPixel       ) & 0xff ).byteValue();

		// alpha
                if(glFormat==GL_RGBA)
                    pixel[offset++]=
			new Integer( (aPixel  >> 24) & 0xff ).byteValue();
              }
            }

	    return true;

	   } catch (Exception e) {
	        System.out.println("An exception occured, while loading a AWTTexture");
	        System.out.println(e);
	        error=true;
	   }
	   return false;
	}

}