2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 byte *byteimage, *lbmpalette;
25 int byteimagewidth, byteimageheight;
28 char mip_prefix[1024]; // directory to dump the textures in
30 qboolean colormap_issued;
31 byte colormap_palette[768];
37 $grab filename x y width height
40 void Cmd_Grab( void ){
48 if ( token[0] == '/' || token[0] == '\\' ) {
49 sprintf( savename, "%s%s.pcx", writedir, token + 1 );
52 sprintf( savename, "%spics/%s.pcx", writedir, token );
56 if ( token[0] == '/' || token[0] == '\\' ) {
57 sprintf( dest, "%s.pcx", token + 1 );
60 sprintf( dest, "pics/%s.pcx", token );
76 if ( xl < 0 || yl < 0 || w < 0 || h < 0 || xl + w > byteimagewidth || yl + h > byteimageheight ) {
77 Error( "GrabPic: Bad size: %i, %i, %i, %i",xl,yl,w,h );
80 // crop it to the proper size
81 cropped = malloc( w * h );
82 for ( y = 0 ; y < h ; y++ )
84 memcpy( cropped + y * w, byteimage + ( y + yl ) * byteimagewidth + xl, w );
87 // save off the new image
88 printf( "saving %s\n", savename );
89 CreatePath( savename );
90 WritePCXfile( savename, cropped, w, h, lbmpalette );
99 $grab filename x y width height
102 void Cmd_Raw( void ){
110 sprintf( savename, "%s%s.lmp", writedir, token );
113 sprintf( dest, "%s.lmp", token );
127 if ( xl < 0 || yl < 0 || w < 0 || h < 0 || xl + w > byteimagewidth || yl + h > byteimageheight ) {
128 Error( "GrabPic: Bad size: %i, %i, %i, %i",xl,yl,w,h );
131 // crop it to the proper size
132 cropped = malloc( w * h );
133 for ( y = 0 ; y < h ; y++ )
135 memcpy( cropped + y * w, byteimage + ( y + yl ) * byteimagewidth + xl, w );
138 // save off the new image
139 printf( "saving %s\n", savename );
140 CreatePath( savename );
142 SaveFile( savename, cropped, w * h );
148 =============================================================================
152 =============================================================================
160 byte BestColor( int r, int g, int b, int start, int stop ){
163 int bestdistortion, distortion;
168 // let any color go to 0 as a last resort
170 bestdistortion = 256 * 256 * 4;
173 pal = colormap_palette + start * 3;
174 for ( i = start ; i <= stop ; i++ )
176 dr = r - (int)pal[0];
177 dg = g - (int)pal[1];
178 db = b - (int)pal[2];
180 distortion = dr * dr + dg * dg + db * db;
181 if ( distortion < bestdistortion ) {
183 return i; // perfect match
186 bestdistortion = distortion;
201 the brightes colormap is first in the table (FIXME: reverse this now?)
203 64 rows of 256 : lightmaps
204 256 rows of 256 : translucency table
207 void Cmd_Colormap( void ){
210 float frac, red, green, blue;
212 byte *cropped, *lump_p;
216 colormap_issued = qtrue;
218 memcpy( colormap_palette, lbmpalette, 768 );
221 if ( !TokenAvailable() ) { // just setting colormap_issued
226 sprintf( savename, "%spics/%s.pcx", writedir, token );
229 sprintf( dest, "pics/%s.pcx", token );
236 brights = 1; // ignore 255 (transparent)
238 cropped = malloc( ( levels + 256 ) * 256 );
242 for ( l = 0; l < levels; l++ )
244 frac = range - range * (float)l / ( levels - 1 );
245 for ( c = 0 ; c < 256 - brights ; c++ )
247 red = lbmpalette[c * 3];
248 green = lbmpalette[c * 3 + 1];
249 blue = lbmpalette[c * 3 + 2];
251 red = (int)( red * frac + 0.5 );
252 green = (int)( green * frac + 0.5 );
253 blue = (int)( blue * frac + 0.5 );
256 // note: 254 instead of 255 because 255 is the transparent color, and we
257 // don't want anything remapping to that
258 // don't use color 0, because NT can't remap that (or 255)
260 *lump_p++ = BestColor( red,green,blue, 1, 254 );
263 // fullbrights allways stay the same
264 for ( ; c < 256 ; c++ )
268 // 66% transparancy table
269 for ( l = 0; l < 255; l++ )
271 for ( c = 0 ; c < 255 ; c++ )
273 red = lbmpalette[c * 3] * 0.33 + lbmpalette[l * 3] * 0.66;
274 green = lbmpalette[c * 3 + 1] * 0.33 + lbmpalette[l * 3 + 1] * 0.66;
275 blue = lbmpalette[c * 3 + 2] * 0.33 + lbmpalette[l * 3 + 2] * 0.66;
277 *lump_p++ = BestColor( red,green,blue, 1, 254 );
281 for ( c = 0 ; c < 256 ; c++ )
284 // save off the new image
285 printf( "saving %s\n", savename );
286 CreatePath( savename );
287 WritePCXfile( savename, cropped, 256, levels + 256, lbmpalette );
293 =============================================================================
297 =============================================================================
302 int d_red, d_green, d_blue;
304 byte palmap[32][32][32];
305 qboolean palmap_built;
312 int FindColor( int r, int g, int b ){
334 bestcolor = BestColor( r, g, b, 0, 254 );
336 bestcolor = palmap[r >> 3][g >> 3][b >> 3];
343 void BuildPalmap( void ){
348 if ( palmap_built ) {
351 palmap_built = qtrue;
353 for ( r = 4 ; r < 256 ; r += 8 )
355 for ( g = 4 ; g < 256 ; g += 8 )
357 for ( b = 4 ; b < 256 ; b += 8 )
359 bestcolor = BestColor( r, g, b, 1, 254 );
360 palmap[r >> 3][g >> 3][b >> 3] = bestcolor;
366 if ( !colormap_issued ) {
367 Error( "You must issue a $colormap command first" );
377 byte AveragePixels( int count ){
387 for ( i = 0 ; i < count ; i++ )
391 r += lbmpalette[pix * 3];
392 g += lbmpalette[pix * 3 + 1];
393 b += lbmpalette[pix * 3 + 2];
407 // find the best color
409 bestcolor = FindColor( r, g, b );
412 pal = colormap_palette + bestcolor * 3;
413 d_red = r - (int)pal[0];
414 d_green = g - (int)pal[1];
415 d_blue = b - (int)pal[2];
423 =============================================================================
425 ENVIRONMENT MAP GRABBING
427 Creates six pcx files from tga files without any palette edge seams
428 also copies the tga files for GL rendering.
429 =============================================================================
432 // 3dstudio environment map suffixes
433 char *suf[6] = {"rt", "ft", "lf", "bk", "up", "dn"};
440 void Cmd_Environment( void ){
443 byte image[256 * 256];
449 for ( i = 0 ; i < 6 ; i++ )
451 sprintf( name, "env/%s%s.pcx", token, suf[i] );
453 sprintf( name, "env/%s%s.tga", token, suf[i] );
461 sprintf( name, "%senv/", gamedir );
464 // convert the images
465 for ( i = 0 ; i < 6 ; i++ )
467 sprintf( name, "%senv/%s%s.tga", gamedir, token, suf[i] );
468 printf( "loading %s...\n", name );
469 LoadTGA( name, &tga, NULL, NULL );
471 for ( y = 0 ; y < 256 ; y++ )
473 for ( x = 0 ; x < 256 ; x++ )
475 image[y * 256 + x] = FindColor( tga[( y * 256 + x ) * 4 + 0],tga[( y * 256 + x ) * 4 + 1],tga[( y * 256 + x ) * 4 + 2] );
479 sprintf( name, "%senv/%s%s.pcx", writedir, token, suf[i] );
480 if ( FileTime( name ) != -1 ) {
481 printf( "%s already exists, not overwriting.\n", name );
484 WritePCXfile( name, image, 256, 256, colormap_palette );