bitmaphandler.h 11 KB

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