FADING.ASM 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. ;***************************************************************************
  19. ;** 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 **
  20. ;***************************************************************************
  21. ;* *
  22. ;* Project Name : Westwood Library *
  23. ;* *
  24. ;* File Name : FADING.ASM *
  25. ;* *
  26. ;* Programmer : Joe L. Bostic *
  27. ;* *
  28. ;* Start Date : August 20, 1993 *
  29. ;* *
  30. ;* Last Update : August 20, 1993 [JLB] *
  31. ;* *
  32. ;*-------------------------------------------------------------------------*
  33. ;* Functions: *
  34. ;* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *
  35. IDEAL
  36. P386
  37. MODEL USE32 FLAT
  38. GLOBAL C Build_Fading_Table :NEAR
  39. CODESEG
  40. ;***********************************************************
  41. ; BUILD_FADING_TABLE
  42. ;
  43. ; void *Build_Fading_Table(void *palette, void *dest, long int color, long int frac);
  44. ;
  45. ; This routine will create the fading effect table used to coerce colors
  46. ; from toward a common value. This table is used when Fading_Effect is
  47. ; active.
  48. ;
  49. ; Bounds Checking: None
  50. ;*
  51. PROC Build_Fading_Table C near
  52. USES ebx, ecx, edi, esi
  53. ARG palette:DWORD
  54. ARG dest:DWORD
  55. ARG color:DWORD
  56. ARG frac:DWORD
  57. LOCAL matchvalue:DWORD ; Last recorded match value.
  58. LOCAL targetred:BYTE ; Target gun red.
  59. LOCAL targetgreen:BYTE ; Target gun green.
  60. LOCAL targetblue:BYTE ; Target gun blue.
  61. LOCAL idealred:BYTE
  62. LOCAL idealgreen:BYTE
  63. LOCAL idealblue:BYTE
  64. LOCAL matchcolor:BYTE ; Tentative match color.
  65. cld
  66. ; If the source palette is NULL, then just return with current fading table pointer.
  67. cmp [palette],0
  68. je ??fini
  69. cmp [dest],0
  70. je ??fini
  71. ; Fractions above 255 become 255.
  72. mov eax,[frac]
  73. cmp eax,0100h
  74. jb short ??ok
  75. mov [frac],0FFh
  76. ??ok:
  77. ; Record the target gun values.
  78. mov esi,[palette]
  79. mov ebx,[color]
  80. add esi,ebx
  81. add esi,ebx
  82. add esi,ebx
  83. lodsb
  84. mov [targetred],al
  85. lodsb
  86. mov [targetgreen],al
  87. lodsb
  88. mov [targetblue],al
  89. ; Main loop.
  90. xor ebx,ebx ; Remap table index.
  91. ; Transparent black never gets remapped.
  92. mov edi,[dest]
  93. mov [edi],bl
  94. inc edi
  95. ; EBX = source palette logical number (1..255).
  96. ; EDI = running pointer into dest remap table.
  97. ??mainloop:
  98. inc ebx
  99. mov esi,[palette]
  100. add esi,ebx
  101. add esi,ebx
  102. add esi,ebx
  103. mov edx,[frac]
  104. shr edx,1
  105. ; new = orig - ((orig-target) * fraction);
  106. lodsb ; orig
  107. mov dh,al ; preserve it for later.
  108. sub al,[targetred] ; al = (orig-target)
  109. imul dl ; ax = (orig-target)*fraction
  110. shl ax,1
  111. sub dh,ah ; dh = orig - ((orig-target) * fraction)
  112. mov [idealred],dh ; preserve ideal color gun value.
  113. lodsb ; orig
  114. mov dh,al ; preserve it for later.
  115. sub al,[targetgreen] ; al = (orig-target)
  116. imul dl ; ax = (orig-target)*fraction
  117. shl ax,1
  118. sub dh,ah ; dh = orig - ((orig-target) * fraction)
  119. mov [idealgreen],dh ; preserve ideal color gun value.
  120. lodsb ; orig
  121. mov dh,al ; preserve it for later.
  122. sub al,[targetblue] ; al = (orig-target)
  123. imul dl ; ax = (orig-target)*fraction
  124. shl ax,1
  125. sub dh,ah ; dh = orig - ((orig-target) * fraction)
  126. mov [idealblue],dh ; preserve ideal color gun value.
  127. ; Sweep through the entire existing palette to find the closest
  128. ; matching color. Never matches with color 0.
  129. mov eax,[color]
  130. mov [matchcolor],al ; Default color (self).
  131. mov [matchvalue],-1 ; Ridiculous match value init.
  132. mov ecx,255
  133. mov esi,[palette] ; Pointer to original palette.
  134. add esi,3
  135. ; BH = color index.
  136. mov bh,1
  137. ??innerloop:
  138. ; Recursion through the fading table won't work if a color is allowed
  139. ; to remap to itself. Prevent this from occuring.
  140. add esi,3
  141. cmp bh,bl
  142. je short ??notclose
  143. sub esi,3
  144. xor edx,edx ; Comparison value starts null.
  145. mov eax,edx
  146. ; Build the comparison value based on the sum of the differences of the color
  147. ; guns squared.
  148. lodsb
  149. sub al,[idealred]
  150. mov ah,al
  151. imul ah
  152. add edx,eax
  153. lodsb
  154. sub al,[idealgreen]
  155. mov ah,al
  156. imul ah
  157. add edx,eax
  158. lodsb
  159. sub al,[idealblue]
  160. mov ah,al
  161. imul ah
  162. add edx,eax
  163. jz short ??perfect ; If perfect match found then quit early.
  164. cmp edx,[matchvalue]
  165. ja short ??notclose
  166. mov [matchvalue],edx ; Record new possible color.
  167. mov [matchcolor],bh
  168. ??notclose:
  169. inc bh ; Checking color index.
  170. loop ??innerloop
  171. mov bh,[matchcolor]
  172. ??perfect:
  173. mov [matchcolor],bh
  174. xor bh,bh ; Make BX valid main index again.
  175. ; When the loop exits, we have found the closest match.
  176. mov al,[matchcolor]
  177. stosb
  178. cmp ebx,255
  179. jne ??mainloop
  180. ??fini:
  181. mov eax,[dest]
  182. ret
  183. ENDP Build_Fading_Table
  184. END