INTERPAL.CPP 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/INTERPAL.CPP 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : INTERPAL.CPP *
  26. * *
  27. * Programmer : Steve Tall *
  28. * *
  29. * Start Date : December 7th 1995 *
  30. * *
  31. *---------------------------------------------------------------------------------------------*
  32. * Overview: *
  33. * This module contains functions to allow use of old 320x200 animations on a 640x400 screen *
  34. * *
  35. * Functions: *
  36. * Read_Interpolation_Palette -- reads an interpolation palette table from disk *
  37. * Write_Interpolation_Palette -- writes an interpolation palette to disk *
  38. * Create_Palette_Interpolation_Table -- build the palette interpolation table *
  39. * Increase_Palette_Luminance -- increase the contrast of a palette *
  40. * Interpolate_2X_Scale -- Stretch a 320x200 graphic buffer into 640x400 *
  41. * *
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43. #include "function.h"
  44. BOOL InterpolationPaletteChanged = FALSE;
  45. extern "C" {
  46. extern void __cdecl Asm_Interpolate (unsigned char* src_ptr ,
  47. unsigned char* dest_ptr ,
  48. int lines ,
  49. int src_width ,
  50. int dest_width);
  51. extern void __cdecl Asm_Interpolate_Line_Double (unsigned char* src_ptr ,
  52. unsigned char* dest_ptr ,
  53. int lines ,
  54. int src_width ,
  55. int dest_width);
  56. extern void __cdecl Asm_Interpolate_Line_Interpolate (unsigned char* src_ptr ,
  57. unsigned char* dest_ptr ,
  58. int lines ,
  59. int src_width ,
  60. int dest_width);
  61. }
  62. #define SIZE_OF_PALETTE 256
  63. extern "C"{
  64. unsigned char PaletteInterpolationTable[SIZE_OF_PALETTE][SIZE_OF_PALETTE];
  65. unsigned char * InterpolationPalette;
  66. }
  67. /***********************************************************************************************
  68. * Read_Interpolation_Palette -- reads an interpolation palette table from disk *
  69. * *
  70. * *
  71. * *
  72. * INPUT: name of palette file *
  73. * *
  74. * OUTPUT: Nothing *
  75. * *
  76. * WARNINGS: None *
  77. * *
  78. * HISTORY: *
  79. * 12/12/95 12:15PM ST : Created *
  80. *=============================================================================================*/
  81. void Read_Interpolation_Palette (char const * palette_file_name)
  82. {
  83. CCFileClass palette_file(palette_file_name);
  84. if (palette_file.Is_Available()) {
  85. palette_file.Open(READ);
  86. palette_file.Read(&PaletteInterpolationTable[0][0], 256*256);
  87. palette_file.Close();
  88. InterpolationPaletteChanged = FALSE;
  89. }
  90. }
  91. /***********************************************************************************************
  92. * Write_Interpolation_Palette -- writes an interpolation palette table to disk *
  93. * *
  94. * *
  95. * *
  96. * INPUT: name of palette file *
  97. * *
  98. * OUTPUT: Nothing *
  99. * *
  100. * WARNINGS: None *
  101. * *
  102. * HISTORY: *
  103. * 12/12/95 12:15PM ST : Created *
  104. *=============================================================================================*/
  105. void Write_Interpolation_Palette (char const * palette_file_name)
  106. {
  107. CCFileClass palette_file(palette_file_name);
  108. if (!palette_file.Is_Available()) {
  109. palette_file.Open(WRITE);
  110. palette_file.Write(&PaletteInterpolationTable[0][0], 256*256);
  111. palette_file.Close();
  112. }
  113. }
  114. /***************************************************************************
  115. * CREATE_PALETTE_INTERPOLATION_TABLE *
  116. * *
  117. * INPUT: *
  118. * *
  119. * OUTPUT: *
  120. * *
  121. * WARNINGS: *
  122. * *
  123. * HISTORY: *
  124. * 12/06/1995 MG : Created. *
  125. *=========================================================================*/
  126. void Create_Palette_Interpolation_Table( void )
  127. {
  128. // Asm_Create_Palette_Interpolation_Table();
  129. #if (1)
  130. int i;
  131. int j;
  132. int p;
  133. unsigned char * first_palette_ptr;
  134. unsigned char * second_palette_ptr;
  135. unsigned char * match_pal_ptr;
  136. int first_r;
  137. int first_g;
  138. int first_b;
  139. int second_r;
  140. int second_g;
  141. int second_b;
  142. int diff_r;
  143. int diff_g;
  144. int diff_b;
  145. int dest_r;
  146. int dest_g;
  147. int dest_b;
  148. int distance;
  149. int closest_distance;
  150. int index_of_closest_color;
  151. //
  152. // Create an interpolation table for the current palette.
  153. //
  154. first_palette_ptr = (unsigned char *) InterpolationPalette;
  155. for ( i = 0; i < SIZE_OF_PALETTE; i ++ ) {
  156. //
  157. // Get the first palette entry's RGB.
  158. //
  159. first_r = *first_palette_ptr;
  160. first_palette_ptr ++;
  161. first_g = *first_palette_ptr;
  162. first_palette_ptr ++;
  163. first_b = *first_palette_ptr;
  164. first_palette_ptr ++;
  165. second_palette_ptr = (unsigned char *) InterpolationPalette;
  166. for ( j = 0; j < SIZE_OF_PALETTE; j ++ ) {
  167. //
  168. // Get the second palette entry's RGB.
  169. //
  170. second_r = *second_palette_ptr;
  171. second_palette_ptr ++;
  172. second_g = *second_palette_ptr;
  173. second_palette_ptr ++;
  174. second_b = *second_palette_ptr;
  175. second_palette_ptr ++;
  176. //
  177. // Now calculate the RGB halfway between the first and second colors.
  178. //
  179. dest_r = ( first_r + second_r ) >> 1;
  180. dest_g = ( first_g + second_g ) >> 1;
  181. dest_b = ( first_b + second_b ) >> 1;
  182. //
  183. // Now find the color in the palette that most closely matches the interpolated color.
  184. //
  185. index_of_closest_color = 0;
  186. // closest_distance = (256 * 256) * 3;
  187. closest_distance = 500000;
  188. match_pal_ptr = (unsigned char *) InterpolationPalette;
  189. for ( p = 0; p < SIZE_OF_PALETTE; p ++ ) {
  190. diff_r = ( ((int) (*match_pal_ptr)) - dest_r );
  191. match_pal_ptr ++;
  192. diff_g = ( ((int) (*match_pal_ptr)) - dest_g );
  193. match_pal_ptr ++;
  194. diff_b = ( ((int) (*match_pal_ptr)) - dest_b );
  195. match_pal_ptr ++;
  196. distance = ( diff_r * diff_r ) + ( diff_g * diff_g ) + ( diff_b * diff_b );
  197. if ( distance < closest_distance ) {
  198. closest_distance = distance;
  199. index_of_closest_color = p;
  200. }
  201. }
  202. PaletteInterpolationTable[ i ][ j ] = (unsigned char) index_of_closest_color;
  203. }
  204. }
  205. #endif
  206. InterpolationPaletteChanged = FALSE;
  207. return;
  208. }
  209. /***********************************************************************************************
  210. * Increase_Palette_Luminance -- increase contrast of colours in a palette *
  211. * *
  212. * *
  213. * *
  214. * INPUT: ptr to palette *
  215. * percentage increase of red *
  216. * percentage increase of green *
  217. * percentage increase of blue *
  218. * cap value for colours *
  219. * *
  220. * *
  221. * OUTPUT: Nothing *
  222. * *
  223. * WARNINGS: None *
  224. * *
  225. * HISTORY: *
  226. * 12/12/95 12:16PM ST : Created *
  227. *=============================================================================================*/
  228. void Increase_Palette_Luminance (unsigned char * palette , int red_percentage , int green_percentage , int blue_percentage , int cap)
  229. {
  230. unsigned int red;
  231. unsigned int green;
  232. unsigned int blue;
  233. for (int i=0 ; i<SIZE_OF_PALETTE*3 ; i+=3) {
  234. red = (unsigned)*(palette+i);
  235. green = (unsigned)*(palette+i+1);
  236. blue = (unsigned)*(palette+i+2);
  237. red += red*red_percentage/100;
  238. green += green*green_percentage/100;
  239. blue += blue*blue_percentage/100;
  240. red = MIN (cap, red);
  241. green = MIN (cap, green);
  242. blue = MIN (cap, blue);
  243. *(palette+i) =(unsigned char)red;
  244. *(palette+i+1) =(unsigned char)green;
  245. *(palette+i+2) =(unsigned char)blue;
  246. }
  247. }
  248. int CopyType =0;
  249. #ifdef WIN32
  250. /***************************************************************************
  251. * INTERPOLATE_2X_SCALE *
  252. * *
  253. * INPUT: *
  254. * *
  255. * OUTPUT: *
  256. * *
  257. * WARNINGS: *
  258. * *
  259. * HISTORY: *
  260. * 12/06/1995 MG : Created. *
  261. *=========================================================================*/
  262. void Interpolate_2X_Scale( GraphicBufferClass * source, GraphicViewPortClass * dest , char const * palette_file_name)
  263. {
  264. unsigned char * src_ptr;
  265. unsigned char * dest_ptr;
  266. unsigned char * last_dest_ptr;
  267. unsigned char * end_of_source;
  268. int src_width;
  269. int dest_width;
  270. // int width_counter;
  271. BOOL source_locked = FALSE;
  272. BOOL dest_locked = FALSE;
  273. /*
  274. **If a palette table exists on disk then read it in otherwise create it
  275. */
  276. if (InterpolationPaletteChanged) {
  277. if (palette_file_name) {
  278. Read_Interpolation_Palette(palette_file_name);
  279. }
  280. if (InterpolationPaletteChanged) {
  281. Create_Palette_Interpolation_Table();
  282. }
  283. }
  284. /*
  285. ** Write the palette table to disk so we don't have to create it again next time
  286. */
  287. if (palette_file_name) {
  288. Write_Interpolation_Palette(palette_file_name);
  289. }
  290. if ( dest == &SeenBuff ) Hide_Mouse();
  291. Wait_Blit();
  292. /*
  293. ** Lock video surfaces if required
  294. */
  295. if (source->Get_IsDirectDraw()) {
  296. if (!source->Lock()) {
  297. if (dest == &SeenBuff) Show_Mouse();
  298. return;
  299. }
  300. source_locked = TRUE;
  301. }
  302. if (dest->Get_IsDirectDraw()) {
  303. if (!dest->Lock()) {
  304. if (source_locked) {
  305. source->Unlock();
  306. }
  307. if (dest == &SeenBuff) Show_Mouse();
  308. return;
  309. }
  310. dest_locked = TRUE;
  311. }
  312. //
  313. // Get pointers to the source and destination buffers.
  314. //
  315. src_ptr = (unsigned char *) source->Get_Offset();
  316. dest_ptr = (unsigned char *) dest->Get_Offset();
  317. end_of_source = src_ptr + ( source->Get_Width() * source->Get_Height() );
  318. //
  319. // Get width of source and dest buffers.
  320. //
  321. src_width = source->Get_Width();
  322. dest_width = 2*(dest->Get_Width() + dest->Get_XAdd() + dest->Get_Pitch());
  323. last_dest_ptr = dest_ptr;
  324. /*
  325. ** Call the appropriate assembly language copy routine
  326. */
  327. #if (1)
  328. switch (CopyType) {
  329. case 0:
  330. Asm_Interpolate ( src_ptr , dest_ptr , source->Get_Height() , src_width , dest_width);
  331. break;
  332. case 1:
  333. Asm_Interpolate_Line_Double( src_ptr , dest_ptr , source->Get_Height() , src_width , dest_width);
  334. break;
  335. case 2:
  336. Asm_Interpolate_Line_Interpolate( src_ptr , dest_ptr , source->Get_Height() , src_width , dest_width);
  337. break;
  338. }
  339. #endif
  340. #if (0)
  341. //
  342. // Copy over the first pixel (upper left).
  343. //
  344. *dest_ptr = *src_ptr;
  345. src_ptr ++;
  346. dest_ptr ++;
  347. //
  348. // Scale copy.
  349. //
  350. width_counter = 0;
  351. while ( src_ptr < end_of_source ) {
  352. //
  353. // Blend this pixel with the one to the left and place this new color in the dest buffer.
  354. //
  355. *dest_ptr = PaletteInterpolationTable[ (*src_ptr) ][ (*( src_ptr - 1 )) ];
  356. dest_ptr ++;
  357. //
  358. // Now place the source pixel into the dest buffer.
  359. //
  360. *dest_ptr = *src_ptr;
  361. src_ptr ++;
  362. dest_ptr ++;
  363. width_counter ++;
  364. if ( width_counter == src_width ) {
  365. width_counter = 0;
  366. last_dest_ptr += dest_width;
  367. dest_ptr = last_dest_ptr;
  368. }
  369. }
  370. #endif
  371. if (source_locked) source->Unlock();
  372. if (dest_locked) dest->Unlock();
  373. if (dest == &SeenBuff) Show_Mouse();
  374. //BG long *longptr = (long *)&PaletteInterpolationTable[0][0];
  375. //BG Mono_Printf("Clock cycles: %08x\n",*longptr);
  376. }
  377. #endif