aboutsummaryrefslogtreecommitdiffstats
path: root/test/jake2/qcommon/TestTGA.java
blob: 32d45cd50e2a611b9ae511f003e82a7ece2aa411 (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
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
/*
 * TestTGA.java
 * Copyright (C) 2003
 *
 * $Id: TestTGA.java,v 1.1 2004-07-08 20:24:31 hzi Exp $
 */
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
package jake2.qcommon;

import jake2.game.Cmd;
import jake2.imageio.ImageFrame;

import java.awt.Dimension;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.logging.*;

import javax.imageio.stream.MemoryCacheImageInputStream;



/**
 * TestTGA
 * 
 * @author cwei
 */
public class TestTGA {

	public static void main(String[] args) {
		System.out.println("*** Start TGA test ***\n");

		init();

		FS.InitFilesystem();
	
		// search for pack_t
		FS.searchpath_t search;
		Collection filenames = new TreeSet();
		for (search = FS.fs_searchpaths; search != null; search = search.next) {
			// is the element a pak file?
			if (search.pack != null) {
				// add all the pak file names
				filenames.addAll(search.pack.files.keySet());
			}
		}
	
		ImageFrame frame = new ImageFrame(null);
		frame.setVisible(true);
		frame.setLocation(50, 50);
		frame.setSize(800, 800);

		byte[] buffer = null;
		Dimension dim = new Dimension();	
		BufferedImage image = null;

		int[] pixel = new int[512 * 512];

		 
		for (Iterator it = filenames.iterator(); it.hasNext();) {

			String filename = it.next().toString();
			if (!filename.endsWith(".tga")) continue;

			System.out.println(filename);
			buffer = LoadTGA(filename, dim);

			if (buffer != null) {
				try {

					int w = dim.width;
					int h = dim.height;
					int size = w * h;
		
					int r, g, b, a;
					
					for (int i = 0; i < size; i++)
					{
						r = buffer[4* i + 0] & 0xFF;
						g = buffer[4* i + 1] & 0xFF;
						b = buffer[4* i + 2] & 0xFF;
						a = buffer[4* i +3] & 0xFF;
	
						pixel[i] = (a << 24) | (r << 16) | (g << 8) | (b << 0); 
					}
		
					image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
					image.setRGB(0,  0, w, h, pixel, 0, w);
					
					AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(3, 3), AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
					BufferedImage tmp = op.filter(image, null);

					frame.showImage(tmp);
					frame.setTitle(filename);

					Thread.sleep(500);

				} catch (InterruptedException e) {
				}
			}
		}
		frame.dispose();
	
		System.gc();
		Runtime rt = Runtime.getRuntime();
		System.out.println(
			"\nJVM total memory: " + rt.totalMemory() / 1024 + " Kbytes");

		System.out.println("\n*** TGA test is succeeded :-) ***");
	}

	static void init() {
		// init the global LogManager with the logging.properties file
		try {
			LogManager.getLogManager().readConfiguration(
				TestFS.class.getResourceAsStream("/jake2/logging.properties"));
		} catch (SecurityException secEx) {
			secEx.printStackTrace();
		} catch (IOException ioEx) {
			System.err.println(
				"FATAL Error: can't load /jake2/logging.properties (classpath)");
			ioEx.printStackTrace();
		}
	}
	
	/*
	=============
	LoadTGA
	=============
	*/
	static byte[] LoadTGA(String name, Dimension dim) {
		int columns, rows, numPixels;
		int pixbuf; // index into pic
		int row, column;
		byte[] raw;
		ByteBuffer buf_p;
		int length;
		qfiles.tga_t targa_header;
		byte[] pic = null;

		//
		// load the file
		//
		raw = FS.LoadFile(name);
		
		if (raw == null)
		{
			System.out.println("Bad tga file "+ name +'\n');
			return null;
		}
		
		targa_header = new qfiles.tga_t(raw);
		
		if (targa_header.image_type != 2 && targa_header.image_type != 10) 
			System.out.println("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
		
		if (targa_header.colormap_type != 0 || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24))
			System.out.println("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
		
		columns = targa_header.width;
		rows = targa_header.height;
		numPixels = columns * rows;
		
		if (dim != null) {
			dim.width = columns;
			dim.height = rows;
		}
		
		pic = new byte[numPixels * 4]; // targa_rgba;
		
		if (targa_header.id_length != 0)
			targa_header.data.position(targa_header.id_length);  // skip TARGA image comment
		
		buf_p = targa_header.data;
			
		byte red,green,blue,alphabyte;
		red = green = blue = alphabyte = 0;
		int packetHeader, packetSize, j;
		
		if (targa_header.image_type==2) {  // Uncompressed, RGB images
			for(row=rows-1; row>=0; row--) {
				
				pixbuf = row * columns * 4;
				
				for(column=0; column<columns; column++) {
					switch (targa_header.pixel_size) {
						case 24:
									
							blue = buf_p.get();
							green = buf_p.get();
							red = buf_p.get();
							pic[pixbuf++] = red;
							pic[pixbuf++] = green;
							pic[pixbuf++] = blue;
							pic[pixbuf++] = (byte)255;
							break;
						case 32:
							blue = buf_p.get();
							green = buf_p.get();
							red = buf_p.get();
							alphabyte = buf_p.get();
							pic[pixbuf++] = red;
							pic[pixbuf++] = green;
							pic[pixbuf++] = blue;
							pic[pixbuf++] = alphabyte;
							break;
					}
				}
			}
		}
		else if (targa_header.image_type==10) {   // Runlength encoded RGB images
			for(row=rows-1; row>=0; row--) {
				
				pixbuf = row * columns * 4;
				try {

					for(column=0; column<columns; ) {
					
						packetHeader= buf_p.get() & 0xFF;
						packetSize = 1 + (packetHeader & 0x7f);
					
						if ((packetHeader & 0x80) != 0) {        // run-length packet
							switch (targa_header.pixel_size) {
								case 24:
									blue = buf_p.get();
									green = buf_p.get();
									red = buf_p.get();
									alphabyte = (byte)255;
									break;
								case 32:
									blue = buf_p.get();
									green = buf_p.get();
									red = buf_p.get();
									alphabyte = buf_p.get();
									break;
							}
				
							for(j=0;j<packetSize;j++) {
								pic[pixbuf++]=red;
								pic[pixbuf++]=green;
								pic[pixbuf++]=blue;
								pic[pixbuf++]=alphabyte;
								column++;
								if (column==columns) { // run spans across rows
									column=0;
									if (row>0)
										row--;
									else
										// goto label breakOut;
										throw new longjmpException();
			
									pixbuf = row * columns * 4;
								}
							}
						}
						else { // non run-length packet
							for(j=0;j<packetSize;j++) {
								switch (targa_header.pixel_size) {
									case 24:
										blue = buf_p.get();
										green = buf_p.get();
										red = buf_p.get();
										pic[pixbuf++] = red;
										pic[pixbuf++] = green;
										pic[pixbuf++] = blue;
										pic[pixbuf++] = (byte)255;
										break;
									case 32:
										blue = buf_p.get();
										green = buf_p.get();
										red = buf_p.get();
										alphabyte = buf_p.get();
										pic[pixbuf++] = red;
										pic[pixbuf++] = green;
										pic[pixbuf++] = blue;
										pic[pixbuf++] = alphabyte;
										break;
								}
								column++;
								if (column==columns) { // pixel packet run spans across rows
									column=0;
									if (row>0)
										row--;
									else
										// goto label breakOut;
										throw new longjmpException();
			
									pixbuf = row * columns * 4;
								}						
							}
						}
					}
				} catch (longjmpException e){
					// label breakOut:
				}
			}
		}
		return pic;
	}

}