aboutsummaryrefslogtreecommitdiffstats
path: root/gl4java/utils/textures/PngTextureLoader.java
blob: 6cc9bb247c5a010bd0ec5067dcce392f967cd8de (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
package gl4java.utils.textures;

import gl4java.*;

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

import com.sixlegs.image.png.*;

/**
 * This is Class implements the PNG texture-loader
 * while using the png-library in package
 * "com.sixlegs.image.png" !
 *
 * @see IOTextureLoader
 * @see TextureLoader
 */   
public class PngTextureLoader
extends IOTextureLoader
{
	protected boolean grayToAlpha = false;

        public PngTextureLoader(GLFunc gl, GLUFunc glu)
        {
                super(gl, glu);
        }

        /**
          * Set to 'true' to load grayscale images as alpha images
          * (in other words, GL_ALPHA instead of GL_LUMINANCE)
          */        
        public void setGrayToAlpha(boolean b)
        {
           this.grayToAlpha = b;
        }

        protected boolean readTexture(InputStream is)
        {
          try {
            int len;
            PngImage png = new PngImage(is);
            
            imageWidth = png.getWidth();
            imageHeight = png.getHeight();

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

            PixelGrabber pp=new PixelGrabber(png,
                                             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;
            }

            // bitDepth beachten
            switch(png.getColorType())
            {
                case PngImage.COLOR_TYPE_GRAY:
                    glFormat= grayToAlpha ? GL_ALPHA : GL_LUMINANCE;
                    break;
                case PngImage.COLOR_TYPE_GRAY_ALPHA:
                    glFormat=GL_LUMINANCE_ALPHA;
                    break;
                case PngImage.COLOR_TYPE_RGB:
                    glFormat=GL_RGB;
                    break;
                case PngImage.COLOR_TYPE_RGB_ALPHA:
                    glFormat=GL_RGBA;
                    break;
                case PngImage.COLOR_TYPE_PALETTE:
                	// todo: support COLOR_TYPE_INDEXED?
                    glFormat=GL_RGB;
                    break;
                default:
                    error=true;
                    System.err.println("unsupported png color type: "+
			     png.getColorType());
                    return false;
            };
           
            int ncmpts = getComponents();
            pixel=new byte[imageWidth * imageHeight * ncmpts];
            
            byte alpha=0;
            byte red=0;
            byte green=0;
            byte blue=0;
            int offset=0;
            int aPixel;
            for(int y=imageHeight-1; y>=0; y--)
            {
              for(int x=0;x<imageWidth;x++)
              {
                aPixel = iPixels[y*imageWidth + x];

                switch (glFormat)
                {
                   case GL_RGBA:
                      pixel[offset] = (byte)(aPixel>>16); 
                      pixel[offset+1] = (byte)(aPixel>>8);
                      pixel[offset+2] = (byte)(aPixel>>0);
                      pixel[offset+3] = (byte)(aPixel>>24);
                      offset += 4;
                      break;
                   case GL_RGB:
                      pixel[offset] = (byte)(aPixel>>16);
                      pixel[offset+1] = (byte)(aPixel>>8);
                      pixel[offset+2] = (byte)(aPixel>>0);
                      offset += 3;
                      break;
                   case GL_LUMINANCE:
                   case GL_COLOR_INDEX:
                      pixel[offset] = (byte)(aPixel);
                      offset += 1;
                      break;
                   case GL_LUMINANCE_ALPHA: // todo: untested
                      pixel[offset] = (byte)(aPixel);
                      pixel[offset+1] = (byte)(aPixel>>24);
                      offset += 2;
                      break;
                }
                
              }
            }

            setTextureSize();
            return true;

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

}