summaryrefslogtreecommitdiff
path: root/utils/texpow2/texpow2.c
blob: baf8a85fd53f6d464769f7d30d6baf2629fca17b (plain) (blame)
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
/*
==================================
TEXPOW2 by Iikka Keranen 2001

Loads TGA files and scales them
up to the closest power of two.
Overwrites the originals, so be
careful and make backups.
==================================
*/

#define FLIST_LEN 2000

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>   
#include <math.h>

#include "texpow2.h"

/*
======= PROTOTYPES FOR FUNCS =======
*/

long countval(char *str);

/*
======= MAIN =======
*/

// Glob vars...
long width, height;

void main(int argc, char *argv[])
{
  unsigned char *animname=NULL;
  unsigned char odname[256];
	unsigned char ofname[256];
  unsigned char *fname[FLIST_LEN];
  long flags=0, contents=0, value=0;
  long x;
  unsigned char ignpal=0;
  short opn=0, numsrc=0;
	t_i_image *in;
	t_i_image *out;

  printf("TEXPOW2 by Iikka Ker�nen 2001\n\n");
  if (argc<2)
	{
    printf("Usage: TEXPOW2 <source> [source2] [source3] ... [options]\n");
    printf("Options:\n");
    printf("-o output dir -- output directory (by default, replaces original)\n\n");
    return;
  }

  for (x=1;x<argc;x++)
  {
    if (!stricmp(argv[x]+strlen(argv[x])-4,".tga"))
    {
      fname[numsrc]=argv[x];
      numsrc++;
    }
    if (x<argc-1)
    {
      if (!strcmp(argv[x],"-o"))
      {
        strcpy(odname, argv[x+1]);
        opn=1;
      }
    }
  }

  if (numsrc==1)
    printf("%d texture to be converted...\n", numsrc);
  else
    printf("%d textures to be converted...\n", numsrc);

  for (x=0;x<numsrc;x++)
  {
    printf("%s ... ",fname[x]);
    if (!opn)  strcpy(ofname, fname[x]);
		else sprintf(ofname, "%s/%s", odname, fname[x]);

		in = i_load_tga(fname[x]);
		if (in)
		{
			out = powerof2(in);
			if (out)
			{
				i_save_tga(out, ofname);
				printf("Saved %s\n", ofname);
			}
			else
			{
				printf("Error!\n");
			}
		}
  }
}

/*
========
SCALE
========
*/

t_i_image *powerof2(t_i_image *img1)
{
	int32 x,y,w,h;
	int32 dx, dy, tx, ty;
	uint32 c1, c2, c3, c4;

	t_i_image *img;

	w = 1; h = 1;
	while (w < img1->w)
		w = w * 2;
	while (h < img1->h)
		h = h * 2;

	if (w < 2 || h < 2)
		return NULL;

	img=new_image(w, h);
	if (!img)
		return NULL;

	dx = ((img1->w) << 16) / (w);
	dy = ((img1->h) << 16) / (h);
	ty = 0;
	for (y = 0; y < h; y++)
	{
		tx = 0;
		for (x = 0; x < w; x++)
		{
			c1 = i_getpixel(img1, (tx>>16), (ty>>16));
			c2 = i_getpixel(img1, (tx>>16)+1, (ty>>16));
			c3 = i_getpixel(img1, (tx>>16), (ty>>16)+1);
			c4 = i_getpixel(img1, (tx>>16)+1, (ty>>16)+1);

			c1 = i_pixel_alphamix(c1, c2, (tx & 0xffff)>>8);
			c2 = i_pixel_alphamix(c3, c4, (tx & 0xffff)>>8);
			c1 = i_pixel_alphamix(c1, c2, (ty & 0xffff)>>8);

			i_putpixel(img, x, y, c1);

			tx += dx;
		}
		ty += dy;
	}

	return img;
}

/*
========
IMAGE
========
*/

t_i_image *new_image(int32 w, int32 h)
{
	t_i_image *img;

	img=malloc(sizeof(t_i_image));
	if (!img)
	{
		return NULL;
	}

	img->w=w;
	img->h=h;

	img->data=calloc(w*h, sizeof(uint32));
	if (!img->data)
	{
		free(img);
		return NULL;
	}

	img->data32 = (int32 *) img->data;

	return img;
}

void del_image(t_i_image *img)
{
	if (!img)
		return;

	if (img->data)
		free(img->data);

	free(img);
}

/*
=============
TGA SAVE/LOAD
=============
*/

t_i_image *i_load_tga(char *fname)
{
	uint8 id_len, pal_type, img_type;
	uint16 f_color, pal_colors;
	uint8 pal_size;
	uint16 left, top, img_w, img_h;
	uint8 bpp, des_bits;

	t_i_image *image;

	uint8 *buffer;
	uint8 *line;

	int32 x,y,po;
	uint8 die=0;

	FILE *img;

	img=fopen(fname, "rb");
	if (!img)
		return NULL;

	// load header
	id_len=fgetc(img);
	pal_type=fgetc(img);
	img_type=fgetc(img);
	f_color=fgetc(img);
	f_color+=fgetc(img)<<8;
	pal_colors=fgetc(img);
	pal_colors+=fgetc(img)<<8;
	pal_size=fgetc(img);
	left=fgetc(img);
	left+=fgetc(img)<<8;
	top=fgetc(img);
	top+=fgetc(img)<<8;
	img_w=fgetc(img);
	img_w+=fgetc(img)<<8;
	img_h=fgetc(img);
	img_h+=fgetc(img)<<8;
	bpp=fgetc(img);
	des_bits=fgetc(img);

	// check for unsupported features
	if (id_len!=0 || pal_colors!=0 || (img_type!=2 && img_type!=3))
		die=1;

	if (img_type==3 && bpp!=8)
		die=1;

	if (img_type==2 && bpp!=24 && bpp!=32)
		die=1;

	if (die)
	{
		fclose(img);	
		return NULL;
	}


	// allocate buffer for the image
	image=new_image(img_w, img_h);
	if (!image)
		return NULL;
	buffer=image->data;

	// allocate temp buffer to store each line as they're read from the file
	line=malloc(img_w*(bpp>>3));
	if (!line)
	{
		del_image(image);
		fclose(img);	
		return NULL;
	}	

	image->data32=(uint32 *)image->data;

	// actually read the image data from file
	for (y=0;y<img_h;y++)
	{
		// read a line into memory
		fread(line, 1, img_w*(bpp>>3), img);

		// convert into 32bit truecolor
		if (des_bits & 0x20)
			po=y*img_w*4;
		else
			po=(img_h-y-1)*img_w*4;
		for (x=0;x<img_w;x++)
		{
			switch(bpp)
			{
				case 8:
					buffer[po++]=line[x];
					buffer[po++]=line[x];
					buffer[po++]=line[x];
					buffer[po++]=0;
					break;
				case 24:
					buffer[po++]=line[x*3];
					buffer[po++]=line[x*3+1];
					buffer[po++]=line[x*3+2];
					buffer[po++]=0;
					break;
				case 32:
					buffer[po++]=line[x*4];
					buffer[po++]=line[x*4+1];
					buffer[po++]=line[x*4+2];
					buffer[po++]=line[x*4+3];
					break;
			}
		}
	}

	free(line);

	return image;
}

void i_save_tga(t_i_image *image, char *fname)
{
	uint16 img_w=image->w, img_h=image->h;
	uint8 *buffer;
	int32 y,x, po;
	FILE *img;

	img=fopen(fname, "wb");
	if (!img)
		return;

	// save header
	fputc(0, img);    // id_len
	fputc(0, img);    // pal_type
	fputc(2, img);    // img_type
  fputc(0, img); fputc(0, img);   // f_color
  fputc(0, img); fputc(0, img);   // pal_colors
  fputc(0, img); 									// pal_size
  fputc(0, img); fputc(0, img);   // left
  fputc(0, img); fputc(0, img);   // top
  fputc(img_w&255, img); fputc(img_w>>8, img);   // width
  fputc(img_h&255, img); fputc(img_h>>8, img);   // height
	fputc(24, img); 	// bpp
	fputc(0, img);    // des_bits
	
	buffer=image->data;

	// save the image data to file
	for (y=0;y<img_h;y++)
	{
		po=(img_h-y-1)*img_w*4;
		for (x = 0; x < img_w; x++)
		{
			fputc(buffer[po+x*4], img);
			fputc(buffer[po+x*4+1], img);
			fputc(buffer[po+x*4+2], img);
		}
		//fwrite(buffer+po, 1, img_w*4, img);
	}
}

/*
=========
PIXEL
=========
*/

uint32 i_rgb_to_32(uint32 r, uint32 g, uint32 b, uint32 a)
{
	uint32 co;

	co=(a<<24)+(r<<16)+(g<<8)+b;

	return co;
}

void i_putpixel(t_i_image *img, int32 x, int32 y, uint32 co)
{
	img->data32[(y%img->h)*img->w+(x%img->w)]=co;
}

void i_putpixel_rgba(t_i_image *img, int32 x, int32 y, int32 r, int32 g, int32 b, int32 a)
{
	uint32 co;

	co=i_rgb_to_32(r,g,b,a); //(a<<24)+(r<<16)+(g<<8)+b;

	img->data32[(y%img->h)*img->w+(x%img->w)]=co;
}

uint32 i_getpixel(t_i_image *img, int32 x, int32 y)
{
	uint32 co;

	co=img->data32[(y%img->h)*img->w+(x%img->w)];

	return co;
}

int32 i_getpixel_ch(t_i_image *img, int32 x, int32 y, int32 ch)
{
	uint32 co;

	co=img->data32[(y%img->h)*img->w+(x%img->w)];

	// ch:  0:red 1:green 2:blue 3:alpha

	switch (ch)
	{
		case 0: // red
			co=(co>>16)&255;
			break;
		case 1: // green
			co=(co>>8)&255;
			break;
		case 2: // blue
			co=co&255;
			break;
		case 3: // alpha
			co=(co>>24)&255;
			break;
		default: ;
	}

	return co;
}

uint32 i_pixel_alphamix(uint32 c1, uint32 c2, uint32 p)
{
	uint32 co; 

	co=i_pixel_add(i_pixel_multiply_n(c1,256-p), i_pixel_multiply_n(c2,p));

	return co;
}

uint32 i_pixel_multiply_n(uint32 c1, uint32 n)
{
	uint32 co,r,g,b,a;

	r=(((c1>>16)&255)*n)>>8;
	g=(((c1>>8)&255) *n)>>8;
	b=(((c1>>0)&255) *n)>>8;
	a=(((c1>>24)&255)*n)>>8;

	co=i_rgb_to_32(r,g,b,a);

	return co;
}

uint32 i_pixel_add(uint32 co1, uint32 co2)
{
	uint32 co,r,g,b,a;

	r=MIN(255, MAX(0, ((co1>>16)&255)+((co2>>16)&255)));
	g=MIN(255, MAX(0, ((co1>>8 )&255)+((co2>>8 )&255)));
	b=MIN(255, MAX(0, ((co1>>0 )&255)+((co2>>0 )&255)));
	a=MIN(255, MAX(0, ((co1>>24)&255)+((co2>>24)&255)));

	co=i_rgb_to_32(r,g,b,a);

	return co;
}

/*
========
MISC
========
*/

long countval(char *str)
{
  long val=0,n,l;

	l=strlen(str);
	for (n=0;n<l;n++)
		if (str[n]>47 && str[n]<58)
			val=val*10+str[n]-48;

	return val;
}