texturethumbnail.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "texturethumbnail.h"
  19. #include "hashtemplate.h"
  20. #include "missingtexture.h"
  21. #include "targa.h"
  22. #include "ww3dformat.h"
  23. #include "ddsfile.h"
  24. #include "textureloader.h"
  25. #include "bitmaphandler.h"
  26. #include "ffactory.h"
  27. static HashTemplateClass<StringClass,ThumbnailClass*> thumbnail_hash;
  28. static bool _ThumbHashModified;
  29. static unsigned char* _ThumbnailMemory;
  30. static const char *THUMBNAIL_FILENAME = "thumbnails.dat";
  31. ThumbnailClass::ThumbnailClass(const char* name, unsigned char* bitmap, unsigned w, unsigned h, bool allocated)
  32. :
  33. Name(name),
  34. Bitmap(bitmap),
  35. Allocated(allocated),
  36. Width(w),
  37. Height(h)
  38. {
  39. thumbnail_hash.Insert(Name,this);
  40. }
  41. // ----------------------------------------------------------------------------
  42. //
  43. // Load texture and generate mipmap levels if requested. The function tries
  44. // to create texture that matches targa format. If suitable format is not
  45. // available, it selects closest matching format and performs color space
  46. // conversion.
  47. //
  48. // ----------------------------------------------------------------------------
  49. ThumbnailClass::ThumbnailClass(const StringClass& filename)
  50. :
  51. Bitmap(0),
  52. Name(filename),
  53. Allocated(false),
  54. Width(0),
  55. Height(0)
  56. {
  57. unsigned reduction_factor=3;
  58. // First, try loading image from a DDS file
  59. DDSFileClass dds_file(filename,reduction_factor);
  60. if (dds_file.Is_Available() && dds_file.Load()) {
  61. Width=dds_file.Get_Width(0);
  62. Height=dds_file.Get_Height(0);
  63. Bitmap=W3DNEWARRAY unsigned char[Width*Height*4];
  64. Allocated=true;
  65. dds_file.Copy_Level_To_Surface(
  66. 0, // Level
  67. WW3D_FORMAT_A8R8G8B8,
  68. Width,
  69. Height,
  70. Bitmap,
  71. Width*4);
  72. }
  73. // If DDS file can't be used try loading from TGA
  74. else {
  75. // Make sure the file can be opened. If not, return missing texture.
  76. Targa targa;
  77. if (TARGA_ERROR_HANDLER(targa.Open(filename,TGA_READMODE),filename)) return;
  78. // DX8 uses image upside down compared to TGA
  79. targa.Header.ImageDescriptor ^= TGAIDF_YORIGIN;
  80. WW3DFormat src_format,dest_format;
  81. unsigned src_bpp=0;
  82. Get_WW3D_Format(dest_format,src_format,src_bpp,targa);
  83. // Destination size will be the next power of two square from the larger width and height...
  84. Width=targa.Header.Width>>reduction_factor;
  85. Height=targa.Header.Height>>reduction_factor;
  86. TextureLoader::Validate_Texture_Size(Width,Height);
  87. unsigned src_width=targa.Header.Width;
  88. unsigned src_height=targa.Header.Height;
  89. // NOTE: We load the palette but we do not yet support paletted textures!
  90. char palette[256*4];
  91. targa.SetPalette(palette);
  92. if (TARGA_ERROR_HANDLER(targa.Load(filename, TGAF_IMAGE, false),filename)) return;
  93. unsigned char* src_surface=(unsigned char*)targa.GetImage();
  94. Bitmap=W3DNEWARRAY unsigned char[Width*Height*4];
  95. Allocated=true;
  96. dest_format=WW3D_FORMAT_A8R8G8B8;
  97. BitmapHandlerClass::Copy_Image(
  98. Bitmap,
  99. Width,
  100. Height,
  101. Width*4,
  102. WW3D_FORMAT_A8R8G8B8,
  103. src_surface,
  104. src_width,
  105. src_height,
  106. src_width*src_bpp,
  107. src_format,
  108. (unsigned char*)targa.GetPalette(),
  109. targa.Header.CMapDepth>>3,
  110. false);
  111. }
  112. _ThumbHashModified=true;
  113. thumbnail_hash.Insert(Name,this);
  114. }
  115. ThumbnailClass::~ThumbnailClass()
  116. {
  117. if (Allocated) delete[] Bitmap;
  118. thumbnail_hash.Remove(Name);
  119. }
  120. ThumbnailClass* ThumbnailClass::Peek_Instance(const StringClass& name)
  121. {
  122. return thumbnail_hash.Get(name);
  123. }
  124. void ThumbnailClass::Init()
  125. {
  126. WWASSERT(!_ThumbnailMemory);
  127. // If the thumbnail hash table file is available, init hash table
  128. #if 0 // don't do thumbnail file.
  129. file_auto_ptr thumb_file(_TheFileFactory, THUMBNAIL_FILENAME);
  130. if (thumb_file->Is_Available()) {
  131. thumb_file->Open(FileClass::READ);
  132. char tmp[4];
  133. thumb_file->Read(tmp,4);
  134. if (tmp[0]=='T' && tmp[1]=='M' && tmp[2]=='B' && tmp[3]=='1') {
  135. int total_thumb_count;
  136. int total_header_length;
  137. int total_data_length;
  138. thumb_file->Read(&total_thumb_count,sizeof(int));
  139. thumb_file->Read(&total_header_length,sizeof(int));
  140. thumb_file->Read(&total_data_length,sizeof(int));
  141. if (total_thumb_count) {
  142. WWASSERT(total_data_length && total_header_length);
  143. _ThumbnailMemory=W3DNEWARRAY unsigned char[total_data_length];
  144. // Load thumbs
  145. for (int i=0;i<total_thumb_count;++i) {
  146. char name[256];
  147. int offset;
  148. int width;
  149. int height;
  150. unsigned long date_time;
  151. int name_len;
  152. thumb_file->Read(&offset,sizeof(int));
  153. thumb_file->Read(&width,sizeof(int));
  154. thumb_file->Read(&height,sizeof(int));
  155. thumb_file->Read(&date_time,sizeof(unsigned long));
  156. thumb_file->Read(&name_len,sizeof(int));
  157. WWASSERT(name_len<255);
  158. thumb_file->Read(name,name_len);
  159. name[name_len]='\0';
  160. // Make sure the file is available and the timestamp matches
  161. file_auto_ptr myfile(_TheFileFactory,name);
  162. if (myfile->Is_Available()) {
  163. myfile->Open(FileClass::READ);
  164. if (date_time==myfile->Get_Date_Time()) {
  165. W3DNEW ThumbnailClass(
  166. name,
  167. _ThumbnailMemory+offset-total_header_length,
  168. width,
  169. height,
  170. false);
  171. }
  172. myfile->Close();
  173. }
  174. }
  175. thumb_file->Read(_ThumbnailMemory,total_data_length);
  176. }
  177. }
  178. thumb_file->Close();
  179. }
  180. #endif
  181. }
  182. void ThumbnailClass::Deinit()
  183. {
  184. // If the thumbnail hash table was modified, save it to disk
  185. HashTemplateIterator<StringClass,ThumbnailClass*> ite(thumbnail_hash);
  186. if (_ThumbHashModified) {
  187. #if 0 // don't write thumbnails. jba.
  188. int total_header_length=0;
  189. int total_data_length=0;
  190. int total_thumb_count=0;
  191. total_header_length+=4; // header 'TMB1'
  192. total_header_length+=4; // thumb count
  193. total_header_length+=4; // header size
  194. total_header_length+=4; // data length
  195. for (ite.First();!ite.Is_Done();ite.Next()) {
  196. total_header_length+=4; // int bitmap offset
  197. total_header_length+=4; // int bitmap width
  198. total_header_length+=4; // int bitmap height
  199. total_header_length+=4; // unsigned long date_time
  200. total_header_length+=4; // int name string length
  201. ThumbnailClass* thumb=ite.Peek_Value();
  202. total_header_length+=strlen(thumb->Get_Name());
  203. total_data_length+=thumb->Get_Width()*thumb->Get_Height()*4;
  204. total_thumb_count++;
  205. }
  206. int offset=total_header_length;
  207. file_auto_ptr thumb_file(_TheWritingFileFactory, THUMBNAIL_FILENAME);
  208. if (thumb_file->Is_Available()) {
  209. thumb_file->Delete();
  210. }
  211. thumb_file->Create();
  212. thumb_file->Open(FileClass::WRITE);
  213. char* header="TMB1";
  214. thumb_file->Write(header,4);
  215. thumb_file->Write(&total_thumb_count,sizeof(int));
  216. thumb_file->Write(&total_header_length,sizeof(int));
  217. thumb_file->Write(&total_data_length,sizeof(int));
  218. // Save names and offsets
  219. for (ite.First();!ite.Is_Done();ite.Next()) {
  220. ThumbnailClass* thumb=ite.Peek_Value();
  221. const char* name=thumb->Get_Name();
  222. int name_len=strlen(name);
  223. int width=thumb->Get_Width();
  224. int height=thumb->Get_Height();
  225. unsigned long date_time=0;
  226. file_auto_ptr myfile(_TheFileFactory,name);
  227. if (myfile->Is_Available()) {
  228. myfile->Open(FileClass::READ);
  229. date_time=myfile->Get_Date_Time();
  230. myfile->Close();
  231. }
  232. thumb_file->Write(&offset,sizeof(int));
  233. thumb_file->Write(&width,sizeof(int));
  234. thumb_file->Write(&height,sizeof(int));
  235. thumb_file->Write(&date_time,sizeof(unsigned long));
  236. thumb_file->Write(&name_len,sizeof(int));
  237. thumb_file->Write(name,name_len);
  238. offset+=width*height*4;
  239. }
  240. // Save bitmaps
  241. offset=total_header_length;
  242. for (ite.First();!ite.Is_Done();ite.Next()) {
  243. ThumbnailClass* thumb=ite.Peek_Value();
  244. int width=thumb->Get_Width();
  245. int height=thumb->Get_Height();
  246. thumb_file->Write(thumb->Peek_Bitmap(),width*height*4);
  247. }
  248. thumb_file->Close();
  249. #endif
  250. }
  251. ite.First();
  252. while (!ite.Is_Done()) {
  253. ThumbnailClass* thumb=ite.Peek_Value();
  254. delete thumb;
  255. ite.First();
  256. }
  257. delete [] _ThumbnailMemory;
  258. _ThumbnailMemory=NULL;
  259. }