2 Copyright (C) 1999-2006 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
49 if (token[0] == '/' || token[0] == '\\')
50 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);
59 sprintf (dest, "pics/%s.pcx", token);
74 if (xl<0 || yl<0 || w<0 || h<0 || xl+w>byteimagewidth || yl+h>byteimageheight)
75 Error ("GrabPic: Bad size: %i, %i, %i, %i",xl,yl,w,h);
77 // crop it to the proper size
78 cropped = malloc (w*h);
81 memcpy (cropped+y*w, byteimage+(y+yl)*byteimagewidth+xl, w);
84 // save off the new image
85 printf ("saving %s\n", savename);
86 CreatePath (savename);
87 WritePCXfile (savename, cropped, w, h, lbmpalette);
96 $grab filename x y width height
108 sprintf (savename, "%s%s.lmp", writedir, token);
112 sprintf (dest, "%s.lmp", token);
126 if (xl<0 || yl<0 || w<0 || h<0 || xl+w>byteimagewidth || yl+h>byteimageheight)
127 Error ("GrabPic: Bad size: %i, %i, %i, %i",xl,yl,w,h);
129 // crop it to the proper size
130 cropped = malloc (w*h);
131 for (y=0 ; y<h ; y++)
133 memcpy (cropped+y*w, byteimage+(y+yl)*byteimagewidth+xl, w);
136 // save off the new image
137 printf ("saving %s\n", savename);
138 CreatePath (savename);
140 SaveFile (savename, cropped, w*h);
146 =============================================================================
150 =============================================================================
158 byte BestColor (int r, int g, int b, int start, int stop)
162 int bestdistortion, distortion;
167 // let any color go to 0 as a last resort
169 bestdistortion = 256*256*4;
172 pal = colormap_palette + start*3;
173 for (i=start ; i<= stop ; i++)
175 dr = r - (int)pal[0];
176 dg = g - (int)pal[1];
177 db = b - (int)pal[2];
179 distortion = dr*dr + dg*dg + db*db;
180 if (distortion < bestdistortion)
183 return i; // perfect match
185 bestdistortion = distortion;
200 the brightes colormap is first in the table (FIXME: reverse this now?)
202 64 rows of 256 : lightmaps
203 256 rows of 256 : translucency table
206 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);
220 if (!TokenAvailable ())
221 { // just setting colormap_issued
226 sprintf (savename, "%spics/%s.pcx", writedir, token);
230 sprintf (dest, "pics/%s.pcx", token);
237 brights = 1; // ignore 255 (transparent)
239 cropped = malloc((levels+256)*256);
243 for (l=0;l<levels;l++)
245 frac = range - range*(float)l/(levels-1);
246 for (c=0 ; c<256-brights ; c++)
248 red = lbmpalette[c*3];
249 green = lbmpalette[c*3+1];
250 blue = lbmpalette[c*3+2];
252 red = (int)(red*frac+0.5);
253 green = (int)(green*frac+0.5);
254 blue = (int)(blue*frac+0.5);
257 // note: 254 instead of 255 because 255 is the transparent color, and we
258 // don't want anything remapping to that
259 // don't use color 0, because NT can't remap that (or 255)
261 *lump_p++ = BestColor(red,green,blue, 1, 254);
264 // fullbrights allways stay the same
269 // 66% transparancy table
272 for (c=0 ; c<255 ; c++)
274 red = lbmpalette[c*3]*0.33 + lbmpalette[l*3]*0.66;
275 green = lbmpalette[c*3+1]*0.33 + lbmpalette[l*3+1]*0.66;
276 blue = lbmpalette[c*3+2]*0.33 + lbmpalette[l*3+2]*0.66;
278 *lump_p++ = BestColor(red,green,blue, 1, 254);
282 for (c=0 ; c<256 ; c++)
285 // save off the new image
286 printf ("saving %s\n", savename);
287 CreatePath (savename);
288 WritePCXfile (savename, cropped, 256, levels+256, lbmpalette);
294 =============================================================================
298 =============================================================================
303 int d_red, d_green, d_blue;
305 byte palmap[32][32][32];
306 qboolean palmap_built;
313 int FindColor (int r, int g, int b)
330 bestcolor = BestColor (r, g, b, 0, 254);
332 bestcolor = palmap[r>>3][g>>3][b>>3];
339 void BuildPalmap (void)
347 palmap_built = qtrue;
349 for (r=4 ; r<256 ; r+=8)
351 for (g=4 ; g<256 ; g+=8)
353 for (b=4 ; b<256 ; b+=8)
355 bestcolor = BestColor (r, g, b, 1, 254);
356 palmap[r>>3][g>>3][b>>3] = bestcolor;
362 if (!colormap_issued)
363 Error ("You must issue a $colormap command first");
372 byte AveragePixels (int count)
385 for (i=0 ; i<count ; i++)
389 r += lbmpalette[pix*3];
390 g += lbmpalette[pix*3+1];
391 b += lbmpalette[pix*3+2];
405 // find the best color
407 bestcolor = FindColor (r, g, b);
410 pal = colormap_palette + bestcolor*3;
411 d_red = r - (int)pal[0];
412 d_green = g - (int)pal[1];
413 d_blue = b - (int)pal[2];
421 =============================================================================
423 ENVIRONMENT MAP GRABBING
425 Creates six pcx files from tga files without any palette edge seams
426 also copies the tga files for GL rendering.
427 =============================================================================
430 // 3dstudio environment map suffixes
431 char *suf[6] = {"rt", "ft", "lf", "bk", "up", "dn"};
438 void Cmd_Environment (void)
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);
483 WritePCXfile (name, image, 256, 256, colormap_palette);