images.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qdata.h"
  19. char mip_prefix[1024]; // directory to dump the textures in
  20. qboolean colormap_issued;
  21. byte colormap_palette[768];
  22. /*
  23. ==============
  24. RemapZero
  25. Replaces all 0 bytes in an image with the closest palette entry.
  26. This is because NT won't let us change index 0, so any palette
  27. animation leaves those pixels untouched.
  28. ==============
  29. */
  30. void RemapZero (byte *pixels, byte *palette, int width, int height)
  31. {
  32. int i, c;
  33. int alt_zero;
  34. int value, best;
  35. alt_zero = 0;
  36. best = 9999999;
  37. for (i=1 ; i<255 ; i++)
  38. {
  39. value = palette[i*3+0]+palette[i*3+1]+palette[i*3+2];
  40. if (value < best)
  41. {
  42. best = value;
  43. alt_zero = i;
  44. }
  45. }
  46. c = width*height;
  47. for (i=0 ; i<c ; i++)
  48. if (pixels[i] == 0)
  49. pixels[i] = alt_zero;
  50. }
  51. /*
  52. ==============
  53. Cmd_Grab
  54. $grab filename x y width height
  55. ==============
  56. */
  57. void Cmd_Grab (void)
  58. {
  59. int xl,yl,w,h,y;
  60. byte *cropped;
  61. char savename[1024];
  62. char dest[1024];
  63. GetToken (false);
  64. if (token[0] == '/' || token[0] == '\\')
  65. sprintf (savename, "%s%s.pcx", gamedir, token+1);
  66. else
  67. sprintf (savename, "%spics/%s.pcx", gamedir, token);
  68. if (g_release)
  69. {
  70. if (token[0] == '/' || token[0] == '\\')
  71. sprintf (dest, "%s.pcx", token+1);
  72. else
  73. sprintf (dest, "pics/%s.pcx", token);
  74. ReleaseFile (dest);
  75. return;
  76. }
  77. GetToken (false);
  78. xl = atoi (token);
  79. GetToken (false);
  80. yl = atoi (token);
  81. GetToken (false);
  82. w = atoi (token);
  83. GetToken (false);
  84. h = atoi (token);
  85. if (xl<0 || yl<0 || w<0 || h<0 || xl+w>byteimagewidth || yl+h>byteimageheight)
  86. Error ("GrabPic: Bad size: %i, %i, %i, %i",xl,yl,w,h);
  87. // crop it to the proper size
  88. cropped = malloc (w*h);
  89. for (y=0 ; y<h ; y++)
  90. {
  91. memcpy (cropped+y*w, byteimage+(y+yl)*byteimagewidth+xl, w);
  92. }
  93. // save off the new image
  94. printf ("saving %s\n", savename);
  95. CreatePath (savename);
  96. WritePCXfile (savename, cropped, w, h, lbmpalette);
  97. free (cropped);
  98. }
  99. /*
  100. ==============
  101. Cmd_Raw
  102. $grab filename x y width height
  103. ==============
  104. */
  105. void Cmd_Raw (void)
  106. {
  107. int xl,yl,w,h,y;
  108. byte *cropped;
  109. char savename[1024];
  110. char dest[1024];
  111. GetToken (false);
  112. sprintf (savename, "%s%s.lmp", gamedir, token);
  113. if (g_release)
  114. {
  115. sprintf (dest, "%s.lmp", token);
  116. ReleaseFile (dest);
  117. return;
  118. }
  119. GetToken (false);
  120. xl = atoi (token);
  121. GetToken (false);
  122. yl = atoi (token);
  123. GetToken (false);
  124. w = atoi (token);
  125. GetToken (false);
  126. h = atoi (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);
  129. // crop it to the proper size
  130. cropped = malloc (w*h);
  131. for (y=0 ; y<h ; y++)
  132. {
  133. memcpy (cropped+y*w, byteimage+(y+yl)*byteimagewidth+xl, w);
  134. }
  135. // save off the new image
  136. printf ("saving %s\n", savename);
  137. CreatePath (savename);
  138. SaveFile (savename, cropped, w*h);
  139. free (cropped);
  140. }
  141. /*
  142. =============================================================================
  143. COLORMAP GRABBING
  144. =============================================================================
  145. */
  146. /*
  147. ===============
  148. BestColor
  149. ===============
  150. */
  151. byte BestColor (int r, int g, int b, int start, int stop)
  152. {
  153. int i;
  154. int dr, dg, db;
  155. int bestdistortion, distortion;
  156. int bestcolor;
  157. byte *pal;
  158. //
  159. // let any color go to 0 as a last resort
  160. //
  161. bestdistortion = 256*256*4;
  162. bestcolor = 0;
  163. pal = colormap_palette + start*3;
  164. for (i=start ; i<= stop ; i++)
  165. {
  166. dr = r - (int)pal[0];
  167. dg = g - (int)pal[1];
  168. db = b - (int)pal[2];
  169. pal += 3;
  170. distortion = dr*dr + dg*dg + db*db;
  171. if (distortion < bestdistortion)
  172. {
  173. if (!distortion)
  174. return i; // perfect match
  175. bestdistortion = distortion;
  176. bestcolor = i;
  177. }
  178. }
  179. return bestcolor;
  180. }
  181. /*
  182. ==============
  183. Cmd_Colormap
  184. $colormap filename
  185. the brightes colormap is first in the table (FIXME: reverse this now?)
  186. 64 rows of 256 : lightmaps
  187. 256 rows of 256 : translucency table
  188. ==============
  189. */
  190. void Cmd_Colormap (void)
  191. {
  192. int levels, brights;
  193. int l, c;
  194. float frac, red, green, blue;
  195. float range;
  196. byte *cropped, *lump_p;
  197. char savename[1024];
  198. char dest[1024];
  199. colormap_issued = true;
  200. if (!g_release)
  201. memcpy (colormap_palette, lbmpalette, 768);
  202. if (!TokenAvailable ())
  203. { // just setting colormap_issued
  204. return;
  205. }
  206. GetToken (false);
  207. sprintf (savename, "%spics/%s.pcx", gamedir, token);
  208. if (g_release)
  209. {
  210. sprintf (dest, "pics/%s.pcx", token);
  211. ReleaseFile (dest);
  212. return;
  213. }
  214. range = 2;
  215. levels = 64;
  216. brights = 1; // ignore 255 (transparent)
  217. cropped = malloc((levels+256)*256);
  218. lump_p = cropped;
  219. // shaded levels
  220. for (l=0;l<levels;l++)
  221. {
  222. frac = range - range*(float)l/(levels-1);
  223. for (c=0 ; c<256-brights ; c++)
  224. {
  225. red = lbmpalette[c*3];
  226. green = lbmpalette[c*3+1];
  227. blue = lbmpalette[c*3+2];
  228. red = (int)(red*frac+0.5);
  229. green = (int)(green*frac+0.5);
  230. blue = (int)(blue*frac+0.5);
  231. //
  232. // note: 254 instead of 255 because 255 is the transparent color, and we
  233. // don't want anything remapping to that
  234. // don't use color 0, because NT can't remap that (or 255)
  235. //
  236. *lump_p++ = BestColor(red,green,blue, 1, 254);
  237. }
  238. // fullbrights allways stay the same
  239. for ( ; c<256 ; c++)
  240. *lump_p++ = c;
  241. }
  242. // 66% transparancy table
  243. for (l=0;l<255;l++)
  244. {
  245. for (c=0 ; c<255 ; c++)
  246. {
  247. red = lbmpalette[c*3]*0.33 + lbmpalette[l*3]*0.66;
  248. green = lbmpalette[c*3+1]*0.33 + lbmpalette[l*3+1]*0.66;
  249. blue = lbmpalette[c*3+2]*0.33 + lbmpalette[l*3+2]*0.66;
  250. *lump_p++ = BestColor(red,green,blue, 1, 254);
  251. }
  252. *lump_p++ = 255;
  253. }
  254. for (c=0 ; c<256 ; c++)
  255. *lump_p++ = 255;
  256. // save off the new image
  257. printf ("saving %s\n", savename);
  258. CreatePath (savename);
  259. WritePCXfile (savename, cropped, 256, levels+256, lbmpalette);
  260. free (cropped);
  261. }
  262. /*
  263. =============================================================================
  264. MIPTEX GRABBING
  265. =============================================================================
  266. */
  267. byte pixdata[256];
  268. int d_red, d_green, d_blue;
  269. byte palmap[32][32][32];
  270. qboolean palmap_built;
  271. /*
  272. =============
  273. FindColor
  274. =============
  275. */
  276. int FindColor (int r, int g, int b)
  277. {
  278. int bestcolor;
  279. if (r > 255)
  280. r = 255;
  281. if (r < 0)
  282. r = 0;
  283. if (g > 255)
  284. g = 255;
  285. if (g < 0)
  286. g = 0;
  287. if (b > 255)
  288. b = 255;
  289. if (b < 0)
  290. b = 0;
  291. #ifndef TABLECOLORS
  292. bestcolor = BestColor (r, g, b, 0, 254);
  293. #else
  294. bestcolor = palmap[r>>3][g>>3][b>>3];
  295. #endif
  296. return bestcolor;
  297. }
  298. void BuildPalmap (void)
  299. {
  300. #ifdef TABLECOLORS
  301. int r, g, b;
  302. int bestcolor;
  303. if (palmap_built)
  304. return;
  305. palmap_built = true;
  306. for (r=4 ; r<256 ; r+=8)
  307. {
  308. for (g=4 ; g<256 ; g+=8)
  309. {
  310. for (b=4 ; b<256 ; b+=8)
  311. {
  312. bestcolor = BestColor (r, g, b, 1, 254);
  313. palmap[r>>3][g>>3][b>>3] = bestcolor;
  314. }
  315. }
  316. }
  317. #endif
  318. if (!colormap_issued)
  319. Error ("You must issue a $colormap command first");
  320. }
  321. /*
  322. =============
  323. AveragePixels
  324. =============
  325. */
  326. byte AveragePixels (int count)
  327. {
  328. int r,g,b;
  329. int i;
  330. int vis;
  331. int pix;
  332. int bestcolor;
  333. byte *pal;
  334. int fullbright;
  335. vis = 0;
  336. r = g = b = 0;
  337. fullbright = 0;
  338. for (i=0 ; i<count ; i++)
  339. {
  340. pix = pixdata[i];
  341. r += lbmpalette[pix*3];
  342. g += lbmpalette[pix*3+1];
  343. b += lbmpalette[pix*3+2];
  344. vis++;
  345. }
  346. r /= vis;
  347. g /= vis;
  348. b /= vis;
  349. // error diffusion
  350. r += d_red;
  351. g += d_green;
  352. b += d_blue;
  353. //
  354. // find the best color
  355. //
  356. bestcolor = FindColor (r, g, b);
  357. // error diffusion
  358. pal = colormap_palette + bestcolor*3;
  359. d_red = r - (int)pal[0];
  360. d_green = g - (int)pal[1];
  361. d_blue = b - (int)pal[2];
  362. return bestcolor;
  363. }
  364. typedef enum
  365. {
  366. pt_contents,
  367. pt_flags,
  368. pt_animvalue,
  369. pt_flagvalue
  370. } parmtype_t;
  371. typedef struct
  372. {
  373. char *name;
  374. int flags;
  375. parmtype_t type;
  376. } mipparm_t;
  377. mipparm_t mipparms[] =
  378. {
  379. // utility content attributes
  380. {"water", CONTENTS_WATER, pt_contents},
  381. {"slime", CONTENTS_SLIME, pt_contents}, // mildly damaging
  382. {"lava", CONTENTS_LAVA, pt_contents}, // very damaging
  383. {"window", CONTENTS_WINDOW, pt_contents}, // solid, but doesn't eat internal textures
  384. {"mist", CONTENTS_MIST, pt_contents}, // non-solid window
  385. {"origin", CONTENTS_ORIGIN, pt_contents}, // center of rotating brushes
  386. {"playerclip", CONTENTS_PLAYERCLIP, pt_contents},
  387. {"monsterclip", CONTENTS_MONSTERCLIP, pt_contents},
  388. // utility surface attributes
  389. {"hint", SURF_HINT, pt_flags},
  390. {"skip", SURF_SKIP, pt_flags},
  391. {"light", SURF_LIGHT, pt_flagvalue}, // value is the light quantity
  392. // texture chaining
  393. {"anim", 0, pt_animvalue}, // value is the next animation
  394. // server attributes
  395. {"slick", SURF_SLICK, pt_flags},
  396. // drawing attributes
  397. {"sky", SURF_SKY, pt_flags},
  398. {"warping", SURF_WARP, pt_flags}, // only valid with 64x64 textures
  399. {"trans33", SURF_TRANS33, pt_flags}, // translucent should allso set fullbright
  400. {"trans66", SURF_TRANS66, pt_flags},
  401. {"flowing", SURF_FLOWING, pt_flags}, // flow direction towards angle 0
  402. {"nodraw", SURF_NODRAW, pt_flags}, // for clip textures and trigger textures
  403. {NULL, 0, pt_contents}
  404. };
  405. /*
  406. ==============
  407. Cmd_Mip
  408. $mip filename x y width height <OPTIONS>
  409. must be multiples of sixteen
  410. SURF_WINDOW
  411. ==============
  412. */
  413. void Cmd_Mip (void)
  414. {
  415. int x,y,xl,yl,xh,yh,w,h;
  416. byte *screen_p, *source;
  417. int linedelta;
  418. miptex_t *qtex;
  419. int miplevel, mipstep;
  420. int xx, yy, pix;
  421. int count;
  422. int flags, value, contents;
  423. mipparm_t *mp;
  424. char lumpname[64];
  425. byte *lump_p;
  426. char filename[1024];
  427. char animname[64];
  428. GetToken (false);
  429. strcpy (lumpname, token);
  430. GetToken (false);
  431. xl = atoi (token);
  432. GetToken (false);
  433. yl = atoi (token);
  434. GetToken (false);
  435. w = atoi (token);
  436. GetToken (false);
  437. h = atoi (token);
  438. if ( (w & 15) || (h & 15) )
  439. Error ("line %i: miptex sizes must be multiples of 16", scriptline);
  440. flags = 0;
  441. contents = 0;
  442. value = 0;
  443. animname[0] = 0;
  444. // get optional flags and values
  445. while (TokenAvailable ())
  446. {
  447. GetToken (false);
  448. for (mp=mipparms ; mp->name ; mp++)
  449. {
  450. if (!strcmp(mp->name, token))
  451. {
  452. switch (mp->type)
  453. {
  454. case pt_animvalue:
  455. GetToken (false); // specify the next animation frame
  456. strcpy (animname, token);
  457. break;
  458. case pt_flags:
  459. flags |= mp->flags;
  460. break;
  461. case pt_contents:
  462. contents |= mp->flags;
  463. break;
  464. case pt_flagvalue:
  465. flags |= mp->flags;
  466. GetToken (false); // specify the light value
  467. value = atoi(token);
  468. break;
  469. }
  470. break;
  471. }
  472. }
  473. if (!mp->name)
  474. Error ("line %i: unknown parm %s", scriptline, token);
  475. }
  476. sprintf (filename, "%stextures/%s/%s.wal", gamedir, mip_prefix, lumpname);
  477. if (g_release)
  478. return; // textures are only released by $maps
  479. xh = xl+w;
  480. yh = yl+h;
  481. qtex = malloc (sizeof(miptex_t) + w*h*2);
  482. memset (qtex, 0, sizeof(miptex_t));
  483. qtex->width = LittleLong(w);
  484. qtex->height = LittleLong(h);
  485. qtex->flags = LittleLong(flags);
  486. qtex->contents = LittleLong(contents);
  487. qtex->value = LittleLong(value);
  488. sprintf (qtex->name, "%s/%s", mip_prefix, lumpname);
  489. if (animname[0])
  490. sprintf (qtex->animname, "%s/%s", mip_prefix, animname);
  491. lump_p = (byte *)(&qtex->value+1);
  492. screen_p = byteimage + yl*byteimagewidth + xl;
  493. linedelta = byteimagewidth - w;
  494. source = lump_p;
  495. qtex->offsets[0] = LittleLong(lump_p - (byte *)qtex);
  496. for (y=yl ; y<yh ; y++)
  497. {
  498. for (x=xl ; x<xh ; x++)
  499. {
  500. pix = *screen_p++;
  501. if (pix == 255)
  502. pix = 1; // should never happen
  503. *lump_p++ = pix;
  504. }
  505. screen_p += linedelta;
  506. }
  507. //
  508. // subsample for greater mip levels
  509. //
  510. d_red = d_green = d_blue = 0; // no distortion yet
  511. for (miplevel = 1 ; miplevel<4 ; miplevel++)
  512. {
  513. qtex->offsets[miplevel] = LittleLong(lump_p - (byte *)qtex);
  514. mipstep = 1<<miplevel;
  515. for (y=0 ; y<h ; y+=mipstep)
  516. {
  517. for (x = 0 ; x<w ; x+= mipstep)
  518. {
  519. count = 0;
  520. for (yy=0 ; yy<mipstep ; yy++)
  521. for (xx=0 ; xx<mipstep ; xx++)
  522. {
  523. pixdata[count] = source[ (y+yy)*w + x + xx ];
  524. count++;
  525. }
  526. *lump_p++ = AveragePixels (count);
  527. }
  528. }
  529. }
  530. //
  531. // dword align the size
  532. //
  533. while ((int)lump_p&3)
  534. *lump_p++ = 0;
  535. //
  536. // write it out
  537. //
  538. printf ("writing %s\n", filename);
  539. SaveFile (filename, (byte *)qtex, lump_p - (byte *)qtex);
  540. free (qtex);
  541. }
  542. /*
  543. ===============
  544. Cmd_Mippal
  545. ===============
  546. */
  547. void Cmd_Mippal (void)
  548. {
  549. colormap_issued = true;
  550. if (g_release)
  551. return;
  552. memcpy (colormap_palette, lbmpalette, 768);
  553. BuildPalmap();
  554. }
  555. /*
  556. ===============
  557. Cmd_Mipdir
  558. ===============
  559. */
  560. void Cmd_Mipdir (void)
  561. {
  562. char filename[1024];
  563. GetToken (false);
  564. strcpy (mip_prefix, token);
  565. // create the directory if needed
  566. sprintf (filename, "%stextures", gamedir, mip_prefix);
  567. Q_mkdir (filename);
  568. sprintf (filename, "%stextures/%s", gamedir, mip_prefix);
  569. Q_mkdir (filename);
  570. }
  571. /*
  572. =============================================================================
  573. ENVIRONMENT MAP GRABBING
  574. Creates six pcx files from tga files without any palette edge seams
  575. also copies the tga files for GL rendering.
  576. =============================================================================
  577. */
  578. // 3dstudio environment map suffixes
  579. char *suf[6] = {"rt", "ft", "lf", "bk", "up", "dn"};
  580. /*
  581. =================
  582. Cmd_Environment
  583. =================
  584. */
  585. void Cmd_Environment (void)
  586. {
  587. char name[1024];
  588. int i, x, y;
  589. byte image[256*256];
  590. byte *tga;
  591. GetToken (false);
  592. if (g_release)
  593. {
  594. for (i=0 ; i<6 ; i++)
  595. {
  596. sprintf (name, "env/%s%s.pcx", token, suf[i]);
  597. ReleaseFile (name);
  598. sprintf (name, "env/%s%s.tga", token, suf[i]);
  599. ReleaseFile (name);
  600. }
  601. return;
  602. }
  603. // get the palette
  604. BuildPalmap ();
  605. sprintf (name, "%senv/", gamedir);
  606. CreatePath (name);
  607. // convert the images
  608. for (i=0 ; i<6 ; i++)
  609. {
  610. sprintf (name, "%senv/%s%s.tga", gamedir, token, suf[i]);
  611. printf ("loading %s...\n", name);
  612. LoadTGA (name, &tga, NULL, NULL);
  613. for (y=0 ; y<256 ; y++)
  614. {
  615. for (x=0 ; x<256 ; x++)
  616. {
  617. image[y*256+x] = FindColor (tga[(y*256+x)*4+0],tga[(y*256+x)*4+1],tga[(y*256+x)*4+2]);
  618. }
  619. }
  620. free (tga);
  621. sprintf (name, "%senv/%s%s.pcx", gamedir, token, suf[i]);
  622. if (FileTime (name) != -1)
  623. printf ("%s already exists, not overwriting.\n", name);
  624. else
  625. WritePCXfile (name, image, 256, 256, colormap_palette);
  626. }
  627. }