3 byte *byteimage, *lbmpalette;
\r
4 int byteimagewidth, byteimageheight;
\r
7 char mip_prefix[1024]; // directory to dump the textures in
\r
9 qboolean colormap_issued;
\r
10 byte colormap_palette[768];
\r
16 $grab filename x y width height
\r
19 void Cmd_Grab (void)
\r
23 char savename[1024];
\r
28 if (token[0] == '/' || token[0] == '\\')
\r
29 sprintf (savename, "%s%s.pcx", writedir, token+1);
\r
31 sprintf (savename, "%spics/%s.pcx", writedir, token);
\r
35 if (token[0] == '/' || token[0] == '\\')
\r
36 sprintf (dest, "%s.pcx", token+1);
\r
38 sprintf (dest, "pics/%s.pcx", token);
\r
53 if (xl<0 || yl<0 || w<0 || h<0 || xl+w>byteimagewidth || yl+h>byteimageheight)
\r
54 Error ("GrabPic: Bad size: %i, %i, %i, %i",xl,yl,w,h);
\r
56 // crop it to the proper size
\r
57 cropped = malloc (w*h);
\r
58 for (y=0 ; y<h ; y++)
\r
60 memcpy (cropped+y*w, byteimage+(y+yl)*byteimagewidth+xl, w);
\r
63 // save off the new image
\r
64 printf ("saving %s\n", savename);
\r
65 CreatePath (savename);
\r
66 WritePCXfile (savename, cropped, w, h, lbmpalette);
\r
75 $grab filename x y width height
\r
82 char savename[1024];
\r
87 sprintf (savename, "%s%s.lmp", writedir, token);
\r
91 sprintf (dest, "%s.lmp", token);
\r
105 if (xl<0 || yl<0 || w<0 || h<0 || xl+w>byteimagewidth || yl+h>byteimageheight)
\r
106 Error ("GrabPic: Bad size: %i, %i, %i, %i",xl,yl,w,h);
\r
108 // crop it to the proper size
\r
109 cropped = malloc (w*h);
\r
110 for (y=0 ; y<h ; y++)
\r
112 memcpy (cropped+y*w, byteimage+(y+yl)*byteimagewidth+xl, w);
\r
115 // save off the new image
\r
116 printf ("saving %s\n", savename);
\r
117 CreatePath (savename);
\r
119 SaveFile (savename, cropped, w*h);
\r
125 =============================================================================
\r
129 =============================================================================
\r
137 byte BestColor (int r, int g, int b, int start, int stop)
\r
141 int bestdistortion, distortion;
\r
146 // let any color go to 0 as a last resort
\r
148 bestdistortion = 256*256*4;
\r
151 pal = colormap_palette + start*3;
\r
152 for (i=start ; i<= stop ; i++)
\r
154 dr = r - (int)pal[0];
\r
155 dg = g - (int)pal[1];
\r
156 db = b - (int)pal[2];
\r
158 distortion = dr*dr + dg*dg + db*db;
\r
159 if (distortion < bestdistortion)
\r
162 return i; // perfect match
\r
164 bestdistortion = distortion;
\r
179 the brightes colormap is first in the table (FIXME: reverse this now?)
\r
181 64 rows of 256 : lightmaps
\r
182 256 rows of 256 : translucency table
\r
185 void Cmd_Colormap (void)
\r
187 int levels, brights;
\r
189 float frac, red, green, blue;
\r
191 byte *cropped, *lump_p;
\r
192 char savename[1024];
\r
195 colormap_issued = qtrue;
\r
197 memcpy (colormap_palette, lbmpalette, 768);
\r
199 if (!TokenAvailable ())
\r
200 { // just setting colormap_issued
\r
205 sprintf (savename, "%spics/%s.pcx", writedir, token);
\r
209 sprintf (dest, "pics/%s.pcx", token);
\r
210 ReleaseFile (dest);
\r
216 brights = 1; // ignore 255 (transparent)
\r
218 cropped = malloc((levels+256)*256);
\r
222 for (l=0;l<levels;l++)
\r
224 frac = range - range*(float)l/(levels-1);
\r
225 for (c=0 ; c<256-brights ; c++)
\r
227 red = lbmpalette[c*3];
\r
228 green = lbmpalette[c*3+1];
\r
229 blue = lbmpalette[c*3+2];
\r
231 red = (int)(red*frac+0.5);
\r
232 green = (int)(green*frac+0.5);
\r
233 blue = (int)(blue*frac+0.5);
\r
236 // note: 254 instead of 255 because 255 is the transparent color, and we
\r
237 // don't want anything remapping to that
\r
238 // don't use color 0, because NT can't remap that (or 255)
\r
240 *lump_p++ = BestColor(red,green,blue, 1, 254);
\r
243 // fullbrights allways stay the same
\r
244 for ( ; c<256 ; c++)
\r
248 // 66% transparancy table
\r
249 for (l=0;l<255;l++)
\r
251 for (c=0 ; c<255 ; c++)
\r
253 red = lbmpalette[c*3]*0.33 + lbmpalette[l*3]*0.66;
\r
254 green = lbmpalette[c*3+1]*0.33 + lbmpalette[l*3+1]*0.66;
\r
255 blue = lbmpalette[c*3+2]*0.33 + lbmpalette[l*3+2]*0.66;
\r
257 *lump_p++ = BestColor(red,green,blue, 1, 254);
\r
261 for (c=0 ; c<256 ; c++)
\r
264 // save off the new image
\r
265 printf ("saving %s\n", savename);
\r
266 CreatePath (savename);
\r
267 WritePCXfile (savename, cropped, 256, levels+256, lbmpalette);
\r
273 =============================================================================
\r
277 =============================================================================
\r
282 int d_red, d_green, d_blue;
\r
284 byte palmap[32][32][32];
\r
285 qboolean palmap_built;
\r
292 int FindColor (int r, int g, int b)
\r
308 #ifndef TABLECOLORS
\r
309 bestcolor = BestColor (r, g, b, 0, 254);
\r
311 bestcolor = palmap[r>>3][g>>3][b>>3];
\r
318 void BuildPalmap (void)
\r
326 palmap_built = qtrue;
\r
328 for (r=4 ; r<256 ; r+=8)
\r
330 for (g=4 ; g<256 ; g+=8)
\r
332 for (b=4 ; b<256 ; b+=8)
\r
334 bestcolor = BestColor (r, g, b, 1, 254);
\r
335 palmap[r>>3][g>>3][b>>3] = bestcolor;
\r
341 if (!colormap_issued)
\r
342 Error ("You must issue a $colormap command first");
\r
351 byte AveragePixels (int count)
\r
364 for (i=0 ; i<count ; i++)
\r
368 r += lbmpalette[pix*3];
\r
369 g += lbmpalette[pix*3+1];
\r
370 b += lbmpalette[pix*3+2];
\r
384 // find the best color
\r
386 bestcolor = FindColor (r, g, b);
\r
389 pal = colormap_palette + bestcolor*3;
\r
390 d_red = r - (int)pal[0];
\r
391 d_green = g - (int)pal[1];
\r
392 d_blue = b - (int)pal[2];
\r
400 =============================================================================
\r
402 ENVIRONMENT MAP GRABBING
\r
404 Creates six pcx files from tga files without any palette edge seams
\r
405 also copies the tga files for GL rendering.
\r
406 =============================================================================
\r
409 // 3dstudio environment map suffixes
\r
410 char *suf[6] = {"rt", "ft", "lf", "bk", "up", "dn"};
\r
417 void Cmd_Environment (void)
\r
421 byte image[256*256];
\r
428 for (i=0 ; i<6 ; i++)
\r
430 sprintf (name, "env/%s%s.pcx", token, suf[i]);
\r
431 ReleaseFile (name);
\r
432 sprintf (name, "env/%s%s.tga", token, suf[i]);
\r
433 ReleaseFile (name);
\r
440 sprintf (name, "%senv/", gamedir);
\r
443 // convert the images
\r
444 for (i=0 ; i<6 ; i++)
\r
446 sprintf (name, "%senv/%s%s.tga", gamedir, token, suf[i]);
\r
447 printf ("loading %s...\n", name);
\r
448 LoadTGA (name, &tga, NULL, NULL);
\r
450 for (y=0 ; y<256 ; y++)
\r
452 for (x=0 ; x<256 ; x++)
\r
454 image[y*256+x] = FindColor (tga[(y*256+x)*4+0],tga[(y*256+x)*4+1],tga[(y*256+x)*4+2]);
\r
458 sprintf (name, "%senv/%s%s.pcx", writedir, token, suf[i]);
\r
459 if (FileTime (name) != -1)
\r
460 printf ("%s already exists, not overwriting.\n", name);
\r
462 WritePCXfile (name, image, 256, 256, colormap_palette);
\r