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