INTERPAL.CPP 17 KB

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