bitmaphandler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. ** Command & Conquer Renegade(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. #if defined(_MSC_VER)
  19. #pragma once
  20. #endif
  21. #ifndef BITMAPHANDLER_H
  22. #define BITMAPHANDLER_H
  23. #include "always.h"
  24. #include "ww3dformat.h"
  25. void Bitmap_Assert(bool condition);
  26. class BitmapHandlerClass
  27. {
  28. public:
  29. // Read pixel at given address
  30. WWINLINE static void Read_B8G8R8A8(
  31. unsigned char* argb,
  32. const unsigned char* src_ptr,
  33. WW3DFormat src_format,
  34. const unsigned char* palette,
  35. unsigned palette_bpp);
  36. // Read pixel at given address
  37. WWINLINE static void Read_B8G8R8A8(
  38. unsigned& argb,
  39. const unsigned char* src_ptr,
  40. WW3DFormat src_format,
  41. const unsigned char* palette,
  42. unsigned palette_bpp);
  43. // Read pixel from surface
  44. WWINLINE static void Read_B8G8R8A8(
  45. unsigned& argb,
  46. const unsigned char* src_ptr,
  47. WW3DFormat src_format,
  48. int x,
  49. int y,
  50. int width,
  51. int height,
  52. const unsigned char* palette,
  53. unsigned palette_bpp);
  54. WWINLINE static void Write_B8G8R8A8(
  55. unsigned char* dest_ptr,
  56. WW3DFormat dest_format,
  57. const unsigned char* argb);
  58. WWINLINE static void Write_B8G8R8A8(
  59. unsigned char* dest_ptr,
  60. WW3DFormat dest_format,
  61. const unsigned& argb);
  62. WWINLINE static void Copy_Pixel(
  63. unsigned char* dest_ptr,
  64. WW3DFormat dest_format,
  65. const unsigned char* src_ptr,
  66. WW3DFormat src_format,
  67. const unsigned char* palette,
  68. unsigned palette_bpp);
  69. WWINLINE static unsigned Combine_A8R8G8B8(
  70. unsigned bgra1,
  71. unsigned bgra2,
  72. unsigned bgra3,
  73. unsigned bgra4);
  74. static void Create_Mipmap_B8G8R8A8(
  75. unsigned char* dest_surface,
  76. unsigned dest_surface_pitch,
  77. unsigned char* src_surface,
  78. unsigned src_surface_pitch,
  79. unsigned width,
  80. unsigned height);
  81. static void Copy_Image_Generate_Mipmap(
  82. unsigned width,
  83. unsigned height,
  84. unsigned char* dest_surface,
  85. unsigned dest_pitch,
  86. WW3DFormat dest_format,
  87. unsigned char* src_surface,
  88. unsigned src_pitch,
  89. WW3DFormat src_format,
  90. unsigned char* mip_surface,
  91. unsigned mip_pitch);
  92. static void Copy_Image(
  93. unsigned char* dest_surface,
  94. unsigned dest_surface_width,
  95. unsigned dest_surface_height,
  96. unsigned dest_surface_pitch,
  97. WW3DFormat dest_surface_format,
  98. unsigned char* src_surface,
  99. unsigned src_surface_width,
  100. unsigned src_surface_height,
  101. unsigned src_surface_pitch,
  102. WW3DFormat src_surface_format,
  103. const unsigned char* src_palette,
  104. unsigned src_palette_bpp,
  105. bool generate_mip_level);
  106. };
  107. // ----------------------------------------------------------------------------
  108. //
  109. // Read color value of given type in BGRA (D3D) byte order. Regarless
  110. // of the source format the color value is converted to 32-bit format.
  111. //
  112. // ----------------------------------------------------------------------------
  113. WWINLINE void BitmapHandlerClass::Read_B8G8R8A8(
  114. unsigned char* argb,
  115. const unsigned char* src_ptr,
  116. WW3DFormat src_format,
  117. const unsigned char* palette,
  118. unsigned palette_bpp)
  119. {
  120. switch (src_format) {
  121. case WW3D_FORMAT_A8R8G8B8:
  122. case WW3D_FORMAT_X8R8G8B8:
  123. *(unsigned*)argb=*(unsigned*)src_ptr;
  124. break;
  125. case WW3D_FORMAT_R8G8B8:
  126. *argb++=src_ptr[0];
  127. *argb++=src_ptr[1];
  128. *argb++=src_ptr[2];
  129. *argb++=0xff;
  130. break;
  131. case WW3D_FORMAT_A4R4G4B4:
  132. {
  133. unsigned short tmp;
  134. tmp=*(unsigned short*)src_ptr;
  135. *argb++=((tmp&0x000f)<<4);
  136. *argb++=((tmp&0x00f0));
  137. *argb++=((tmp&0x0f00)>>4);
  138. *argb++=((tmp&0xf000)>>8);
  139. }
  140. break;
  141. case WW3D_FORMAT_A1R5G5B5:
  142. {
  143. unsigned short tmp;
  144. tmp=*(unsigned short*)src_ptr;
  145. argb[3]=tmp&0x8000 ? 0xff : 0x0;
  146. argb[2]=(tmp>>7)&0xf8;
  147. argb[1]=(tmp>>2)&0xf8;
  148. argb[0]=(tmp<<3)&0xf8;
  149. }
  150. break;
  151. case WW3D_FORMAT_R5G6B5:
  152. {
  153. unsigned short tmp;
  154. tmp=*(unsigned short*)src_ptr;
  155. argb[3]=0xff;
  156. argb[2]=(tmp>>8)&0xf8;
  157. argb[1]=(tmp>>3)&0xfc;
  158. argb[0]=(tmp<<3)&0xf8;
  159. }
  160. break;
  161. case WW3D_FORMAT_R3G3B2:
  162. {
  163. unsigned char tmp=*src_ptr;
  164. argb[3]=0xff;
  165. argb[2]=tmp&0xe0;
  166. argb[1]=(tmp<<3)&0xe0;
  167. argb[0]=(tmp<<6)&0xc0;
  168. }
  169. break;
  170. case WW3D_FORMAT_L8:
  171. {
  172. unsigned char tmp=*src_ptr++;
  173. *argb++=tmp;
  174. *argb++=tmp;
  175. *argb++=tmp;
  176. *argb++=0xff;
  177. }
  178. break;
  179. case WW3D_FORMAT_A8:
  180. {
  181. *argb++=0;
  182. *argb++=0;
  183. *argb++=0;
  184. *argb++=*src_ptr++;
  185. }
  186. break;
  187. case WW3D_FORMAT_P8:
  188. {
  189. unsigned char index=*src_ptr++;
  190. switch (palette_bpp) {
  191. case 4:
  192. *argb++=palette[palette_bpp*index+3];
  193. *argb++=palette[palette_bpp*index+2];
  194. *argb++=palette[palette_bpp*index+1];
  195. *argb++=palette[palette_bpp*index+0];
  196. break;
  197. case 3:
  198. *argb++=palette[palette_bpp*index+2];
  199. *argb++=palette[palette_bpp*index+1];
  200. *argb++=palette[palette_bpp*index+0];
  201. *argb++=0xff;
  202. break;
  203. case 2:
  204. case 1:
  205. default:
  206. Bitmap_Assert(0);
  207. break;
  208. }
  209. }
  210. break;
  211. case WW3D_FORMAT_DXT1:
  212. case WW3D_FORMAT_DXT2:
  213. case WW3D_FORMAT_DXT3:
  214. case WW3D_FORMAT_DXT4:
  215. case WW3D_FORMAT_DXT5:
  216. default: Bitmap_Assert(0); break;
  217. }
  218. }
  219. WWINLINE void BitmapHandlerClass::Read_B8G8R8A8(
  220. unsigned& argb,
  221. const unsigned char* src_ptr,
  222. WW3DFormat src_format,
  223. const unsigned char* palette,
  224. unsigned palette_bpp)
  225. {
  226. Read_B8G8R8A8((unsigned char*)&argb,src_ptr,src_format,palette,palette_bpp);
  227. }
  228. // Read pixel from surface
  229. WWINLINE void BitmapHandlerClass::Read_B8G8R8A8(
  230. unsigned& argb,
  231. const unsigned char* src_ptr,
  232. WW3DFormat src_format,
  233. int x,
  234. int y,
  235. int width,
  236. int height,
  237. const unsigned char* palette,
  238. unsigned palette_bpp)
  239. {
  240. if (x<0 || y<0 || x>=width || y>=height) {
  241. argb=0;
  242. return;
  243. }
  244. unsigned bpp=Get_Bytes_Per_Pixel(src_format);
  245. Read_B8G8R8A8(
  246. argb,
  247. src_ptr+bpp*x+width*bpp*y,
  248. src_format,
  249. palette,
  250. palette_bpp);
  251. }
  252. // ----------------------------------------------------------------------------
  253. //
  254. // Write color value of given type in BGRA (D3D) byte order. The source value
  255. // is always 32 bit and it is converted to defined destination format.
  256. //
  257. // ----------------------------------------------------------------------------
  258. WWINLINE void BitmapHandlerClass::Write_B8G8R8A8(
  259. unsigned char* dest_ptr,
  260. WW3DFormat dest_format,
  261. const unsigned char* argb)
  262. {
  263. switch (dest_format) {
  264. case WW3D_FORMAT_A8R8G8B8:
  265. case WW3D_FORMAT_X8R8G8B8:
  266. *(unsigned*)dest_ptr=*(unsigned*)argb;
  267. break;
  268. case WW3D_FORMAT_R8G8B8:
  269. *dest_ptr++=*argb++;
  270. *dest_ptr++=*argb++;
  271. *dest_ptr++=*argb++;
  272. break;
  273. case WW3D_FORMAT_A4R4G4B4:
  274. {
  275. unsigned short tmp;
  276. tmp=((argb[3])&0xf0)<<8;
  277. tmp|=((argb[2])&0xf0)<<4;
  278. tmp|=((argb[1])&0xf0);
  279. tmp|=((argb[0])&0xf0)>>4;
  280. *(unsigned short*)dest_ptr=tmp;
  281. }
  282. break;
  283. case WW3D_FORMAT_A1R5G5B5:
  284. {
  285. unsigned short tmp;
  286. tmp=argb[3] ? 0x8000 : 0x0;
  287. tmp|=((argb[2])&0xf8)<<7;
  288. tmp|=((argb[1])&0xf8)<<2;
  289. tmp|=((argb[0])&0xf8)>>3;
  290. *(unsigned short*)dest_ptr=tmp;
  291. }
  292. break;
  293. case WW3D_FORMAT_R5G6B5:
  294. {
  295. unsigned short tmp;
  296. tmp=((argb[2])&0xf8)<<8;
  297. tmp|=((argb[1])&0xfc)<<3;
  298. tmp|=((argb[0])&0xf8)>>3;
  299. *(unsigned short*)dest_ptr=tmp;
  300. }
  301. break;
  302. case WW3D_FORMAT_R3G3B2:
  303. {
  304. unsigned char tmp;
  305. tmp=((argb[2])&0xe0);
  306. tmp|=((argb[1])&0xe0)>>3;
  307. tmp|=((argb[0])&0xc0)>>6;
  308. *(unsigned short*)dest_ptr=tmp;
  309. }
  310. break;
  311. case WW3D_FORMAT_L8:
  312. {
  313. // CIE Req. 709: Y709 = 0.2125R + 0.7154G + 0.0721B
  314. unsigned char tmp = (unsigned char) ( (
  315. ((unsigned int)argb[0] * (unsigned int)0x1275) + // 0.0721B
  316. ((unsigned int)argb[1] * (unsigned int)0xB725) + // 0.7154G (rounded up so FF, FF, FF becomes FF)
  317. ((unsigned int)argb[2] * (unsigned int)0x3666) // 0.2125R
  318. ) >> 16);
  319. *dest_ptr++=tmp;
  320. }
  321. break;
  322. case WW3D_FORMAT_A8:
  323. {
  324. *dest_ptr++=*argb++;
  325. }
  326. break;
  327. case WW3D_FORMAT_DXT1:
  328. case WW3D_FORMAT_DXT2:
  329. case WW3D_FORMAT_DXT3:
  330. case WW3D_FORMAT_DXT4:
  331. case WW3D_FORMAT_DXT5:
  332. case WW3D_FORMAT_P8: // Paletted destination not supported
  333. default: Bitmap_Assert(0); break;
  334. }
  335. }
  336. WWINLINE void BitmapHandlerClass::Write_B8G8R8A8(
  337. unsigned char* dest_ptr,
  338. WW3DFormat dest_format,
  339. const unsigned& argb)
  340. {
  341. Write_B8G8R8A8(dest_ptr,dest_format,(unsigned char*)&argb);
  342. }
  343. // ----------------------------------------------------------------------------
  344. //
  345. // Copy pixel. Perform color space conversion if needed. The source and
  346. // destination are always D3D-style BGRA.
  347. //
  348. // ----------------------------------------------------------------------------
  349. WWINLINE void BitmapHandlerClass::Copy_Pixel(
  350. unsigned char* dest_ptr,
  351. WW3DFormat dest_format,
  352. const unsigned char* src_ptr,
  353. WW3DFormat src_format,
  354. const unsigned char* palette,
  355. unsigned palette_bpp)
  356. {
  357. // Color space conversion needed?
  358. if (dest_format==src_format) {
  359. switch (dest_format) {
  360. case WW3D_FORMAT_A8R8G8B8:
  361. case WW3D_FORMAT_X8R8G8B8:
  362. *(unsigned*)dest_ptr=*(unsigned*)src_ptr;
  363. break;
  364. case WW3D_FORMAT_R8G8B8:
  365. *dest_ptr++=src_ptr[0];
  366. *dest_ptr++=src_ptr[1];
  367. *dest_ptr++=src_ptr[2];
  368. break;
  369. case WW3D_FORMAT_A4R4G4B4:
  370. {
  371. unsigned short tmp=*(unsigned short*)src_ptr;
  372. *(unsigned short*)dest_ptr=((tmp&0x000f)<<12)|((tmp&0x00f0)<<4)|((tmp&0x0f00)>>4)|((tmp&0xf000)>>12);
  373. }
  374. break;
  375. case WW3D_FORMAT_A1R5G5B5:
  376. {
  377. unsigned short tmp=*(unsigned short*)src_ptr;
  378. *(unsigned short*)dest_ptr=((tmp&0x001f)<<11)|((tmp&0x03e0)<<1)|((tmp&0x7c00)>>9)|((tmp&0x8000)>>15);
  379. }
  380. break;
  381. case WW3D_FORMAT_R5G6B5:
  382. {
  383. unsigned short tmp=*(unsigned short*)src_ptr;
  384. *(unsigned short*)dest_ptr=((tmp&0x001f)<<11)|(tmp&0x07e0)|((tmp&0xf800)>>11);
  385. }
  386. break;
  387. case WW3D_FORMAT_R3G3B2:
  388. case WW3D_FORMAT_L8:
  389. case WW3D_FORMAT_A8: *dest_ptr++=*src_ptr++;
  390. break;
  391. case WW3D_FORMAT_P8: // Paletted destinations not supported
  392. default: Bitmap_Assert(0); break;
  393. }
  394. }
  395. else {
  396. unsigned b8g8r8a8;
  397. Read_B8G8R8A8(b8g8r8a8,src_ptr,src_format,palette,palette_bpp);
  398. Write_B8G8R8A8(dest_ptr,dest_format,b8g8r8a8);
  399. }
  400. }
  401. WWINLINE unsigned BitmapHandlerClass::Combine_A8R8G8B8(
  402. unsigned bgra1,
  403. unsigned bgra2,
  404. unsigned bgra3,
  405. unsigned bgra4)
  406. {
  407. bgra1&=0xfcfcfcfc;
  408. bgra2&=0xfcfcfcfc;
  409. bgra3&=0xfcfcfcfc;
  410. bgra4&=0xfcfcfcfc;
  411. bgra1>>=2;
  412. bgra2>>=2;
  413. bgra3>>=2;
  414. bgra4>>=2;
  415. bgra1+=bgra2;
  416. bgra3+=bgra4;
  417. bgra1+=bgra3;
  418. return bgra1;
  419. }
  420. #endif