| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "q3data.h" |
| 23 |
|
| 24 |
byte *byteimage, *lbmpalette; |
| 25 |
int byteimagewidth, byteimageheight; |
| 26 |
|
| 27 |
|
| 28 |
char mip_prefix[1024]; |
| 29 |
|
| 30 |
qboolean colormap_issued; |
| 31 |
byte colormap_palette[768]; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
void Cmd_Grab( void ){ |
| 41 |
int xl,yl,w,h,y; |
| 42 |
byte *cropped; |
| 43 |
char savename[1024]; |
| 44 |
char dest[1024]; |
| 45 |
|
| 46 |
GetToken( qfalse ); |
| 47 |
|
| 48 |
if ( token[0] == '/' || token[0] == '\\' ) { |
| 49 |
sprintf( savename, "%s%s.pcx", writedir, token + 1 ); |
| 50 |
} |
| 51 |
else{ |
| 52 |
sprintf( savename, "%spics/%s.pcx", writedir, token ); |
| 53 |
} |
| 54 |
|
| 55 |
if ( g_release ) { |
| 56 |
if ( token[0] == '/' || token[0] == '\\' ) { |
| 57 |
sprintf( dest, "%s.pcx", token + 1 ); |
| 58 |
} |
| 59 |
else{ |
| 60 |
sprintf( dest, "pics/%s.pcx", token ); |
| 61 |
} |
| 62 |
|
| 63 |
ReleaseFile( dest ); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
GetToken( qfalse ); |
| 68 |
xl = atoi( token ); |
| 69 |
GetToken( qfalse ); |
| 70 |
yl = atoi( token ); |
| 71 |
GetToken( qfalse ); |
| 72 |
w = atoi( token ); |
| 73 |
GetToken( qfalse ); |
| 74 |
h = atoi( token ); |
| 75 |
|
| 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 ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
cropped = malloc( w * h ); |
| 82 |
for ( y = 0 ; y < h ; y++ ) |
| 83 |
{ |
| 84 |
memcpy( cropped + y * w, byteimage + ( y + yl ) * byteimagewidth + xl, w ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
printf( "saving %s\n", savename ); |
| 89 |
CreatePath( savename ); |
| 90 |
WritePCXfile( savename, cropped, w, h, lbmpalette ); |
| 91 |
|
| 92 |
free( cropped ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
void Cmd_Raw( void ){ |
| 103 |
int xl,yl,w,h,y; |
| 104 |
byte *cropped; |
| 105 |
char savename[1024]; |
| 106 |
char dest[1024]; |
| 107 |
|
| 108 |
GetToken( qfalse ); |
| 109 |
|
| 110 |
sprintf( savename, "%s%s.lmp", writedir, token ); |
| 111 |
|
| 112 |
if ( g_release ) { |
| 113 |
sprintf( dest, "%s.lmp", token ); |
| 114 |
ReleaseFile( dest ); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
GetToken( qfalse ); |
| 119 |
xl = atoi( token ); |
| 120 |
GetToken( qfalse ); |
| 121 |
yl = atoi( token ); |
| 122 |
GetToken( qfalse ); |
| 123 |
w = atoi( token ); |
| 124 |
GetToken( qfalse ); |
| 125 |
h = atoi( token ); |
| 126 |
|
| 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 ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
cropped = malloc( w * h ); |
| 133 |
for ( y = 0 ; y < h ; y++ ) |
| 134 |
{ |
| 135 |
memcpy( cropped + y * w, byteimage + ( y + yl ) * byteimagewidth + xl, w ); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
printf( "saving %s\n", savename ); |
| 140 |
CreatePath( savename ); |
| 141 |
|
| 142 |
SaveFile( savename, cropped, w * h ); |
| 143 |
|
| 144 |
free( cropped ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
byte BestColor( int r, int g, int b, int start, int stop ){ |
| 161 |
int i; |
| 162 |
int dr, dg, db; |
| 163 |
int bestdistortion, distortion; |
| 164 |
int bestcolor; |
| 165 |
byte *pal; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
bestdistortion = 256 * 256 * 4; |
| 171 |
bestcolor = 0; |
| 172 |
|
| 173 |
pal = colormap_palette + start * 3; |
| 174 |
for ( i = start ; i <= stop ; i++ ) |
| 175 |
{ |
| 176 |
dr = r - (int)pal[0]; |
| 177 |
dg = g - (int)pal[1]; |
| 178 |
db = b - (int)pal[2]; |
| 179 |
pal += 3; |
| 180 |
distortion = dr * dr + dg * dg + db * db; |
| 181 |
if ( distortion < bestdistortion ) { |
| 182 |
if ( !distortion ) { |
| 183 |
return i; |
| 184 |
|
| 185 |
} |
| 186 |
bestdistortion = distortion; |
| 187 |
bestcolor = i; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return bestcolor; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
void Cmd_Colormap( void ){ |
| 208 |
int levels, brights; |
| 209 |
int l, c; |
| 210 |
float frac, red, green, blue; |
| 211 |
float range; |
| 212 |
byte *cropped, *lump_p; |
| 213 |
char savename[1024]; |
| 214 |
char dest[1024]; |
| 215 |
|
| 216 |
colormap_issued = qtrue; |
| 217 |
if ( !g_release ) { |
| 218 |
memcpy( colormap_palette, lbmpalette, 768 ); |
| 219 |
} |
| 220 |
|
| 221 |
if ( !TokenAvailable() ) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
GetToken( qfalse ); |
| 226 |
sprintf( savename, "%spics/%s.pcx", writedir, token ); |
| 227 |
|
| 228 |
if ( g_release ) { |
| 229 |
sprintf( dest, "pics/%s.pcx", token ); |
| 230 |
ReleaseFile( dest ); |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
range = 2; |
| 235 |
levels = 64; |
| 236 |
brights = 1; |
| 237 |
|
| 238 |
cropped = malloc( ( levels + 256 ) * 256 ); |
| 239 |
lump_p = cropped; |
| 240 |
|
| 241 |
|
| 242 |
for ( l = 0; l < levels; l++ ) |
| 243 |
{ |
| 244 |
frac = range - range * (float)l / ( levels - 1 ); |
| 245 |
for ( c = 0 ; c < 256 - brights ; c++ ) |
| 246 |
{ |
| 247 |
red = lbmpalette[c * 3]; |
| 248 |
green = lbmpalette[c * 3 + 1]; |
| 249 |
blue = lbmpalette[c * 3 + 2]; |
| 250 |
|
| 251 |
red = (int)( red * frac + 0.5 ); |
| 252 |
green = (int)( green * frac + 0.5 ); |
| 253 |
blue = (int)( blue * frac + 0.5 ); |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
*lump_p++ = BestColor( red,green,blue, 1, 254 ); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
for ( ; c < 256 ; c++ ) |
| 265 |
*lump_p++ = c; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
for ( l = 0; l < 255; l++ ) |
| 270 |
{ |
| 271 |
for ( c = 0 ; c < 255 ; c++ ) |
| 272 |
{ |
| 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; |
| 276 |
|
| 277 |
*lump_p++ = BestColor( red,green,blue, 1, 254 ); |
| 278 |
} |
| 279 |
*lump_p++ = 255; |
| 280 |
} |
| 281 |
for ( c = 0 ; c < 256 ; c++ ) |
| 282 |
*lump_p++ = 255; |
| 283 |
|
| 284 |
|
| 285 |
printf( "saving %s\n", savename ); |
| 286 |
CreatePath( savename ); |
| 287 |
WritePCXfile( savename, cropped, 256, levels + 256, lbmpalette ); |
| 288 |
|
| 289 |
free( cropped ); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
byte pixdata[256]; |
| 301 |
|
| 302 |
int d_red, d_green, d_blue; |
| 303 |
|
| 304 |
byte palmap[32][32][32]; |
| 305 |
qboolean palmap_built; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
int FindColor( int r, int g, int b ){ |
| 313 |
int bestcolor; |
| 314 |
|
| 315 |
if ( r > 255 ) { |
| 316 |
r = 255; |
| 317 |
} |
| 318 |
if ( r < 0 ) { |
| 319 |
r = 0; |
| 320 |
} |
| 321 |
if ( g > 255 ) { |
| 322 |
g = 255; |
| 323 |
} |
| 324 |
if ( g < 0 ) { |
| 325 |
g = 0; |
| 326 |
} |
| 327 |
if ( b > 255 ) { |
| 328 |
b = 255; |
| 329 |
} |
| 330 |
if ( b < 0 ) { |
| 331 |
b = 0; |
| 332 |
} |
| 333 |
#ifndef TABLECOLORS |
| 334 |
bestcolor = BestColor( r, g, b, 0, 254 ); |
| 335 |
#else |
| 336 |
bestcolor = palmap[r >> 3][g >> 3][b >> 3]; |
| 337 |
#endif |
| 338 |
|
| 339 |
return bestcolor; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
void BuildPalmap( void ){ |
| 344 |
#ifdef TABLECOLORS |
| 345 |
int r, g, b; |
| 346 |
int bestcolor; |
| 347 |
|
| 348 |
if ( palmap_built ) { |
| 349 |
return; |
| 350 |
} |
| 351 |
palmap_built = qtrue; |
| 352 |
|
| 353 |
for ( r = 4 ; r < 256 ; r += 8 ) |
| 354 |
{ |
| 355 |
for ( g = 4 ; g < 256 ; g += 8 ) |
| 356 |
{ |
| 357 |
for ( b = 4 ; b < 256 ; b += 8 ) |
| 358 |
{ |
| 359 |
bestcolor = BestColor( r, g, b, 1, 254 ); |
| 360 |
palmap[r >> 3][g >> 3][b >> 3] = bestcolor; |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
#endif |
| 365 |
|
| 366 |
if ( !colormap_issued ) { |
| 367 |
Error( "You must issue a $colormap command first" ); |
| 368 |
} |
| 369 |
|
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
byte AveragePixels( int count ){ |
| 378 |
int r,g,b; |
| 379 |
int i; |
| 380 |
int vis; |
| 381 |
int pix; |
| 382 |
int bestcolor; |
| 383 |
byte *pal; |
| 384 |
|
| 385 |
vis = 0; |
| 386 |
r = g = b = 0; |
| 387 |
for ( i = 0 ; i < count ; i++ ) |
| 388 |
{ |
| 389 |
pix = pixdata[i]; |
| 390 |
|
| 391 |
r += lbmpalette[pix * 3]; |
| 392 |
g += lbmpalette[pix * 3 + 1]; |
| 393 |
b += lbmpalette[pix * 3 + 2]; |
| 394 |
vis++; |
| 395 |
} |
| 396 |
|
| 397 |
r /= vis; |
| 398 |
g /= vis; |
| 399 |
b /= vis; |
| 400 |
|
| 401 |
|
| 402 |
r += d_red; |
| 403 |
g += d_green; |
| 404 |
b += d_blue; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
bestcolor = FindColor( r, g, b ); |
| 410 |
|
| 411 |
|
| 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]; |
| 416 |
|
| 417 |
return bestcolor; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
char *suf[6] = {"rt", "ft", "lf", "bk", "up", "dn"}; |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
void Cmd_Environment( void ){ |
| 441 |
char name[1024]; |
| 442 |
int i, x, y; |
| 443 |
byte image[256 * 256]; |
| 444 |
byte *tga; |
| 445 |
|
| 446 |
GetToken( qfalse ); |
| 447 |
|
| 448 |
if ( g_release ) { |
| 449 |
for ( i = 0 ; i < 6 ; i++ ) |
| 450 |
{ |
| 451 |
sprintf( name, "env/%s%s.pcx", token, suf[i] ); |
| 452 |
ReleaseFile( name ); |
| 453 |
sprintf( name, "env/%s%s.tga", token, suf[i] ); |
| 454 |
ReleaseFile( name ); |
| 455 |
} |
| 456 |
return; |
| 457 |
} |
| 458 |
|
| 459 |
BuildPalmap(); |
| 460 |
|
| 461 |
sprintf( name, "%senv/", gamedir ); |
| 462 |
CreatePath( name ); |
| 463 |
|
| 464 |
|
| 465 |
for ( i = 0 ; i < 6 ; i++ ) |
| 466 |
{ |
| 467 |
sprintf( name, "%senv/%s%s.tga", gamedir, token, suf[i] ); |
| 468 |
printf( "loading %s...\n", name ); |
| 469 |
LoadTGA( name, &tga, NULL, NULL ); |
| 470 |
|
| 471 |
for ( y = 0 ; y < 256 ; y++ ) |
| 472 |
{ |
| 473 |
for ( x = 0 ; x < 256 ; x++ ) |
| 474 |
{ |
| 475 |
image[y * 256 + x] = FindColor( tga[( y * 256 + x ) * 4 + 0],tga[( y * 256 + x ) * 4 + 1],tga[( y * 256 + x ) * 4 + 2] ); |
| 476 |
} |
| 477 |
} |
| 478 |
free( tga ); |
| 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 ); |
| 482 |
} |
| 483 |
else{ |
| 484 |
WritePCXfile( name, image, 256, 256, colormap_palette ); |
| 485 |
} |
| 486 |
} |
| 487 |
} |