Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
5.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #if !defined( _X360 )
  9. #include <windows.h>
  10. #endif
  11. #include <stdio.h>
  12. #include "tier1/utlbuffer.h"
  13. #include <vgui/VGUI.h>
  14. #include <vgui_controls/Controls.h>
  15. #include "filesystem.h"
  16. #if defined( _X360 )
  17. #include "xbox/xbox_win32stubs.h"
  18. #endif
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. using namespace vgui;
  22. #define TYP_LUMPY 64 // 64 + grab command number
  23. typedef struct
  24. {
  25. char identification[4]; // should be WAD2 or 2DAW
  26. int numlumps;
  27. int infotableofs;
  28. } wadinfo_t;
  29. typedef struct
  30. {
  31. int filepos;
  32. int disksize;
  33. int size; // uncompressed
  34. char type;
  35. char compression;
  36. char pad1, pad2;
  37. char name[16]; // must be null terminated
  38. } lumpinfo_t;
  39. typedef struct
  40. {
  41. char name[16];
  42. unsigned width, height;
  43. unsigned offsets[4]; // four mip maps stored
  44. } miptex_t;
  45. unsigned char pixdata[256];
  46. float linearpalette[256][3];
  47. float d_red, d_green, d_blue;
  48. int colors_used;
  49. int color_used[256];
  50. float maxdistortion;
  51. unsigned char palLogo[768];
  52. /*
  53. =============
  54. AveragePixels
  55. =============
  56. */
  57. unsigned char AveragePixels (int count)
  58. {
  59. return pixdata[0];
  60. }
  61. /*
  62. ==============
  63. GrabMip
  64. filename MIP x y width height
  65. must be multiples of sixteen
  66. ==============
  67. */
  68. int GrabMip ( HANDLE hdib, unsigned char *lump_p, char *lumpname, COLORREF crf, int *width, int *height)
  69. {
  70. int i,x,y,xl,yl,xh,yh,w,h;
  71. unsigned char *screen_p, *source;
  72. miptex_t *qtex;
  73. int miplevel, mipstep;
  74. int xx, yy;
  75. int count;
  76. int byteimagewidth, byteimageheight;
  77. unsigned char *byteimage;
  78. LPBITMAPINFO lpbmi; // pointer to BITMAPINFO structure (Win3.0)
  79. /* get pointer to BITMAPINFO (Win 3.0) */
  80. lpbmi = (LPBITMAPINFO)::GlobalLock((HGLOBAL)hdib);
  81. unsigned char *lump_start = lump_p;
  82. xl = yl = 0;
  83. w = lpbmi->bmiHeader.biWidth;
  84. h = lpbmi->bmiHeader.biHeight;
  85. *width = w;
  86. *height = h;
  87. byteimage = (unsigned char *)((LPSTR)lpbmi + sizeof( BITMAPINFOHEADER ) + 256 * sizeof( RGBQUAD ) );
  88. if ( (w & 15) || (h & 15) )
  89. return 0; //Error ("line %i: miptex sizes must be multiples of 16", scriptline);
  90. xh = xl+w;
  91. yh = yl+h;
  92. qtex = (miptex_t *)lump_p;
  93. qtex->width = (unsigned)(w);
  94. qtex->height = (unsigned)(h);
  95. Q_strncpy (qtex->name, lumpname, sizeof( qtex->name) );
  96. lump_p = (unsigned char *)&qtex->offsets[4];
  97. byteimagewidth = w;
  98. byteimageheight = h;
  99. source = (unsigned char *)lump_p;
  100. qtex->offsets[0] = (unsigned)((unsigned char *)lump_p - (unsigned char *)qtex);
  101. // We're reading from a dib, so go bottom up
  102. screen_p = byteimage + (h - 1) * w;
  103. for (y=yl ; y<yh ; y++)
  104. {
  105. for (x=xl ; x<xh ; x++)
  106. *lump_p++ = *screen_p++;
  107. screen_p -= 2 * w;
  108. }
  109. // calculate gamma corrected linear palette
  110. for (i = 0; i < 256; i++)
  111. {
  112. for (int j = 0; j < 3; j++)
  113. {
  114. float f = (float)(palLogo[i*3+j] / 255.0);
  115. linearpalette[i][j] = f; //pow((double)f, 2); // assume textures are done at 2.2, we want to remap them at 1.0
  116. }
  117. }
  118. maxdistortion = 0;
  119. // assume palette full if it's a transparent texture
  120. colors_used = 256;
  121. for (i = 0; i < 256; i++)
  122. color_used[i] = 1;
  123. //
  124. // subsample for greater mip levels
  125. //
  126. for (miplevel = 1 ; miplevel<4 ; miplevel++)
  127. {
  128. d_red = d_green = d_blue = 0; // no distortion yet
  129. qtex->offsets[miplevel] = (unsigned)(lump_p - (unsigned char *)qtex);
  130. mipstep = 1<<miplevel;
  131. for (y=0 ; y<h ; y+=mipstep)
  132. {
  133. for (x = 0 ; x<w ; x+= mipstep)
  134. {
  135. count = 0;
  136. for (yy=0 ; yy<mipstep ; yy++)
  137. {
  138. for (xx=0 ; xx<mipstep ; xx++)
  139. pixdata[count++] = source[(y+yy)*w + x + xx ];
  140. }
  141. *lump_p++ = AveragePixels (count);
  142. }
  143. }
  144. }
  145. ::GlobalUnlock(lpbmi);
  146. // Write out palette in 16bit mode
  147. *(unsigned short *) lump_p = 256; // palette size
  148. lump_p += sizeof(short);
  149. memcpy(lump_p, &palLogo[0], 765);
  150. lump_p += 765;
  151. *lump_p++ = (unsigned char)(crf & 0xFF);
  152. *lump_p++ = (unsigned char)((crf >> 8) & 0xFF);
  153. *lump_p++ = (unsigned char)((crf >> 16) & 0xFF);
  154. return lump_p - lump_start;
  155. }
  156. void UpdateLogoWAD( void *phdib, int r, int g, int b )
  157. {
  158. char logoname[ 32 ];
  159. char *pszName;
  160. Q_strncpy( logoname, "LOGO", sizeof( logoname ) );
  161. pszName = &logoname[ 0 ];
  162. HANDLE hdib = (HANDLE)phdib;
  163. COLORREF crf = RGB( r, g, b );
  164. if ((!pszName) || (pszName[0] == 0) || (hdib == NULL))
  165. return;
  166. // Generate lump
  167. unsigned char *buf = (unsigned char *)_alloca( 16384 );
  168. CUtlBuffer buffer( 0, 16384 );
  169. int width, height;
  170. int length = GrabMip (hdib, buf, pszName, crf, &width, &height);
  171. if ( length == 0 )
  172. {
  173. return;
  174. }
  175. bool sizevalid = false;
  176. if ( width == height )
  177. {
  178. if ( width == 16 ||
  179. width == 32 ||
  180. width == 64 )
  181. {
  182. sizevalid = true;
  183. }
  184. }
  185. if ( !sizevalid )
  186. return;
  187. while (length & 3)
  188. length++;
  189. // Write Header
  190. wadinfo_t header;
  191. header.identification[0] = 'W';
  192. header.identification[1] = 'A';
  193. header.identification[2] = 'D';
  194. header.identification[3] = '3';
  195. header.numlumps = 1;
  196. header.infotableofs = 0;
  197. buffer.Put( &header, sizeof( wadinfo_t ) );
  198. // Fill Ino info table
  199. lumpinfo_t info;
  200. Q_memset (&info, 0, sizeof(info));
  201. Q_strncpy(info.name, pszName, sizeof( info.name ) );
  202. info.filepos = (int)sizeof(wadinfo_t);
  203. info.size = info.disksize = length;
  204. info.type = TYP_LUMPY;
  205. info.compression = 0;
  206. // Write Lump
  207. buffer.Put( buf, length );
  208. // Write info table
  209. buffer.Put( &info, sizeof( lumpinfo_t ) );
  210. int savepos = buffer.TellPut();
  211. buffer.SeekPut( CUtlBuffer::SEEK_HEAD, 0 );
  212. header.infotableofs = length + sizeof(wadinfo_t);
  213. buffer.Put( &header, sizeof( wadinfo_t ) );
  214. buffer.SeekPut( CUtlBuffer::SEEK_HEAD, savepos );
  215. // Output to file
  216. FileHandle_t file;
  217. file = g_pFullFileSystem->Open( "pldecal.wad", "wb" );
  218. if ( file != FILESYSTEM_INVALID_HANDLE )
  219. {
  220. g_pFullFileSystem->Write( buffer.Base(), buffer.TellPut(), file );
  221. g_pFullFileSystem->Close( file );
  222. }
  223. }