hermes_palette.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. {
  2. Free Pascal port of the Hermes C library.
  3. Copyright (C) 2001-2003 Nikolay Nikolov ([email protected])
  4. Original C version by Christian Nentwich ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version
  9. with the following modification:
  10. As a special exception, the copyright holders of this library give you
  11. permission to link this library with independent modules to produce an
  12. executable, regardless of the license terms of these independent modules,and
  13. to copy and distribute the resulting executable under terms of your choice,
  14. provided that you also meet, for each linked independent module, the terms
  15. and conditions of the license of that module. An independent module is a
  16. module which is not derived from or based on this library. If you modify
  17. this library, you may extend this exception to your version of the library,
  18. but you are not obligated to do so. If you do not wish to do so, delete this
  19. exception statement from your version.
  20. This library is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. Lesser General Public License for more details.
  24. You should have received a copy of the GNU Lesser General Public
  25. License along with this library; if not, write to the Free Software
  26. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. }
  28. type
  29. PHermesLookupTable = ^THermesLookupTable;
  30. THermesLookupTable = record
  31. data: ^Uint32; { Actual lookup table }
  32. valid: Boolean; { Is this table up to date? }
  33. format: THermesFormat; { Format of lookup table }
  34. end;
  35. PHermesPalette = ^THermesPalette;
  36. THermesPalette = record
  37. data: ^Uint32; { Palette data }
  38. tables: PHermesList; { Linked list of HermesLookupTables }
  39. end;
  40. function Hermes_PaletteInstance: THermesPaletteHandle;
  41. var
  42. newinstance: PHermesPalette;
  43. begin
  44. { Create a new palette structure }
  45. New(newinstance);
  46. { Create palette data }
  47. newinstance^.data := GetMem(256*SizeOf(Uint32));
  48. { Create lookup table list }
  49. newinstance^.tables := Hermes_ListNew;
  50. Result := THermesPaletteHandle(newinstance);
  51. end;
  52. procedure HermesLookupTable_FreeCallback(q: Pointer);
  53. begin
  54. FreeMem(PHermesLookupTable(q)^.data);
  55. Dispose(PHermesLookupTable(q));
  56. end;
  57. procedure Hermes_PaletteReturn(handle: THermesPaletteHandle);
  58. begin
  59. if handle <> nil then
  60. begin
  61. { Free palette data and lookup tables }
  62. FreeMem(PHermesPalette(handle)^.data);
  63. Hermes_ListDestroy(PHermesPalette(handle)^.tables, @HermesLookupTable_FreeCallback);
  64. Dispose(PHermesPalette(handle));
  65. end;
  66. end;
  67. procedure Hermes_PaletteSet(handle: THermesPaletteHandle; palette: Pointer);
  68. var
  69. element: PHermesListElement;
  70. pal: PHermesPalette;
  71. begin
  72. { DebugMSG('Hermes_PaletteSet('+C2Str(handle)+','+C2Str(DWord(palette))+')');}
  73. if handle = nil then
  74. exit;
  75. pal := PHermesPalette(handle);
  76. element := pal^.tables^.first;
  77. { Invalidate all lookup tables }
  78. while element <> nil do
  79. begin
  80. (PHermesLookupTable(element^.data))^.valid := False;
  81. element := element^.next;
  82. end;
  83. { FillChar(palette^, 256*4, $7f);}
  84. Move(palette^, pal^.data^, 256*4);
  85. end;
  86. function Hermes_PaletteGet(handle: THermesPaletteHandle): Pointer;
  87. begin
  88. if handle = nil then
  89. begin
  90. Result := nil;
  91. exit;
  92. end;
  93. Result := PHermesPalette(handle)^.data;
  94. end;
  95. procedure Hermes_PaletteMakeLookup(lookup, palette: PUint32;
  96. format: PHermesFormat);
  97. var
  98. info: THermesGenericInfo;
  99. I: Integer;
  100. r, g, b: Uint32;
  101. begin
  102. { DebugMSG('Yo! Hermes_PaletteMakeLookup');}
  103. r := 0;
  104. g := 0;
  105. b := 0;
  106. if format^.indexed then
  107. exit;
  108. Hermes_Calculate_Generic_Info(24,16,8,32,
  109. Hermes_Topbit(format^.r),
  110. Hermes_Topbit(format^.g),
  111. Hermes_Topbit(format^.b),
  112. Hermes_Topbit(format^.a),
  113. @info);
  114. { Optimised loop if there are no left shifts }
  115. if (info.r_left = 0) and (info.g_left = 0) and (info.b_left = 0) then
  116. for I := 0 to 255 do
  117. begin
  118. r := (palette[i] shr info.r_right) and format^.r;
  119. g := (palette[i] shr info.g_right) and format^.g;
  120. b := (palette[i] shr info.b_right) and format^.b;
  121. lookup[i] := r or g or b;
  122. end
  123. else
  124. for I := 0 to 255 do
  125. begin
  126. r := ((palette[i] shr info.r_right) shl info.r_left) and format^.r;
  127. g := ((palette[i] shr info.g_right) shl info.g_left) and format^.g;
  128. b := ((palette[i] shr info.b_right) shl info.b_left) and format^.b;
  129. lookup[i] := r or g or b;
  130. end;
  131. end;
  132. function Hermes_PaletteGetTable(palette: THermesPaletteHandle; format: PHermesFormat): Pointer;
  133. var
  134. element: PHermesListElement;
  135. pal: PHermesPalette;
  136. table: PHermesLookupTable;
  137. begin
  138. if palette = nil then
  139. begin
  140. Result := nil;
  141. exit;
  142. end;
  143. pal := PHermesPalette(palette);
  144. { Go to the first table in the list }
  145. element := pal^.tables^.first;
  146. { Search for correct table using format }
  147. while element <> nil do
  148. begin
  149. table := element^.data;
  150. if Hermes_FormatEquals(@table^.format, format) then
  151. begin
  152. if table^.valid then
  153. begin
  154. Result := table^.data;
  155. exit;
  156. end;
  157. { Have to recreate the lookup table }
  158. Hermes_PaletteMakeLookup(table^.data, pal^.data, format);
  159. table^.valid := True;
  160. Result := table^.data;
  161. exit;
  162. end;
  163. element := element^.next;
  164. end;
  165. { Format not found, have to create a new table (need no handle) }
  166. New(table);
  167. table^.data := GetMem(1024);
  168. { Create lookup table }
  169. Hermes_PaletteMakeLookup(table^.data, pal^.data, format);
  170. Hermes_FormatCopy(format, @table^.format);
  171. table^.valid := True;
  172. { Create a new list element }
  173. element := Hermes_ListElementNew(0);
  174. element^.data := table;
  175. { Add to the front of the list }
  176. Hermes_ListAddFront(pal^.tables, element);
  177. { Return lookup data }
  178. Result := table^.data;
  179. end;
  180. procedure Hermes_PaletteInvalidateCache(handle: THermesPaletteHandle);
  181. var
  182. element: PHermesListElement;
  183. pal: PHermesPalette;
  184. begin
  185. if handle = nil then
  186. exit;
  187. pal := PHermesPalette(handle);
  188. element := pal^.tables^.first;
  189. { Invalidate all lookup tables }
  190. while element <> nil do
  191. begin
  192. (PHermesLookupTable(element^.data))^.valid := False;
  193. element := element^.next;
  194. end;
  195. end;