Counter Strike : Global Offensive Source Code
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.

295 lines
6.3 KiB

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