Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
5.6 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: ssa8.c
  3. *
  4. * Operations on .a8 files
  5. *
  6. * Copyright (c) 1995 Microsoft Corporation
  7. *
  8. \**************************************************************************/
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "mtk.h"
  14. #include "texture.hxx"
  15. #define ESCAPE 0
  16. #define ESC_ENDLINE 0
  17. #define ESC_ENDBITMAP 1
  18. #define ESC_DELTA 2
  19. #define ESC_RANDOM 3
  20. #define RANDOM_COUNT(c) ((c)-(ESC_RANDOM-1))
  21. #define COUNT_SIZE 256
  22. #define MAXRUN (COUNT_SIZE-1)
  23. #define MAXRND (RANDOM_COUNT(COUNT_SIZE)-1)
  24. typedef unsigned char *HwMem;
  25. typedef unsigned long HwMemSize;
  26. #define HW_UNALIGNED UNALIGNED
  27. static HwMemSize HwuRld(HwMem src, HwMem dest, HwMemSize stride)
  28. {
  29. unsigned short code;
  30. unsigned char run, esc;
  31. size_t len;
  32. HwMem s;
  33. HwMem f;
  34. s = src;
  35. f = dest;
  36. for (;;)
  37. {
  38. #if 0
  39. // orig
  40. code = *((unsigned short HW_UNALIGNED *)s)++;
  41. #else
  42. // code = *( ((unsigned short *)s)++ );
  43. //mf: will this incremement OK ?
  44. code = *( ((unsigned short HW_UNALIGNED *)s ++) );
  45. #endif
  46. run = code & 0xff;
  47. esc = code >> 8;
  48. if (run == ESCAPE)
  49. {
  50. if (esc == ESC_ENDBITMAP)
  51. {
  52. break;
  53. }
  54. else if (esc == ESC_DELTA)
  55. {
  56. #if 0
  57. len = *((unsigned short HW_UNALIGNED *)s)++;
  58. #else
  59. len = *((unsigned short HW_UNALIGNED *)s++);
  60. #endif
  61. while (len-- > 0)
  62. {
  63. *f = 0;
  64. f += stride;
  65. }
  66. }
  67. else if (esc >= ESC_RANDOM)
  68. {
  69. len = RANDOM_COUNT(esc);
  70. while (len-- > 0)
  71. {
  72. *f = *s++;
  73. f += stride;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. while (run-- > 0)
  80. {
  81. *f = esc;
  82. f += stride;
  83. }
  84. }
  85. }
  86. return s-src;
  87. }
  88. static HwMemSize HwuRldTo32(HwMem src, HwMem dest, HwMemSize stride,
  89. DWORD *translate)
  90. {
  91. unsigned short code;
  92. unsigned char run, esc;
  93. size_t len;
  94. HwMem s;
  95. DWORD *f, tran;
  96. s = src;
  97. f = (DWORD *)dest;
  98. for (;;)
  99. {
  100. #if 0
  101. code = *((unsigned short HW_UNALIGNED *)s)++;
  102. #else
  103. code = *((unsigned short HW_UNALIGNED *)s++);
  104. #endif
  105. run = code & 0xff;
  106. esc = code >> 8;
  107. if (run == ESCAPE)
  108. {
  109. if (esc == ESC_ENDBITMAP)
  110. {
  111. break;
  112. }
  113. else if (esc == ESC_DELTA)
  114. {
  115. #if 0
  116. len = *((unsigned short HW_UNALIGNED *)s)++;
  117. #else
  118. len = *((unsigned short HW_UNALIGNED *)s++);
  119. #endif
  120. while (len-- > 0)
  121. {
  122. *f = translate[0];
  123. f += stride;
  124. }
  125. }
  126. else if (esc >= ESC_RANDOM)
  127. {
  128. len = RANDOM_COUNT(esc);
  129. while (len-- > 0)
  130. {
  131. *f = translate[*s++];
  132. f += stride;
  133. }
  134. }
  135. }
  136. else
  137. {
  138. tran = translate[esc];
  139. while (run-- > 0)
  140. {
  141. *f = tran;
  142. f += stride;
  143. }
  144. }
  145. }
  146. return s-src;
  147. }
  148. #define ALPHA_SIGNATURE 0xa0a1a2a3
  149. #define COMPRESS_NONE 0
  150. #define COMPRESS_RLE 1
  151. BOOL
  152. TEXTURE::A8ImageLoad(void *pvResource )
  153. {
  154. DWORD compress;
  155. DWORD size;
  156. DWORD *pal;
  157. BYTE *buf;
  158. DWORD *pdwA8;
  159. PUCHAR data;
  160. pdwA8 = (DWORD *)pvResource;
  161. // Check data signature for alpha texture format
  162. if (*pdwA8 != ALPHA_SIGNATURE)
  163. {
  164. return FALSE;
  165. }
  166. pdwA8++;
  167. width = *pdwA8++;
  168. height = *pdwA8++;
  169. // Make sure depth is 8bpp
  170. if (*pdwA8 != 8)
  171. {
  172. return FALSE;
  173. }
  174. pdwA8++;
  175. size = width*height;
  176. // Compression type
  177. compress = *pdwA8++;
  178. // Compressed data size only if compressed, not used
  179. pdwA8++;
  180. // Remember pointer to palette data
  181. pal = pdwA8;
  182. pdwA8 += 256;
  183. if (mtk_PalettedTextureEnabled())
  184. {
  185. // Allocate data for final image
  186. data = (PUCHAR) malloc(size);
  187. if (data == NULL)
  188. {
  189. return FALSE;
  190. }
  191. pal_size = 256;
  192. pal = (DWORD *) malloc(pal_size*sizeof(RGBQUAD));
  193. if (pal == NULL)
  194. {
  195. free(data);
  196. return FALSE;
  197. }
  198. memcpy(pal, pal, pal_size*sizeof(RGBQUAD));
  199. // Unpack 8bpp data into final image
  200. if (compress == COMPRESS_NONE)
  201. {
  202. memcpy(data, pdwA8, size);
  203. }
  204. else
  205. {
  206. HwuRld((HwMem)pdwA8, data, 1);
  207. }
  208. format = GL_COLOR_INDEX;
  209. components = GL_COLOR_INDEX8_EXT;
  210. }
  211. else
  212. {
  213. // Allocate data for final image
  214. data = (PUCHAR) malloc(size*sizeof(DWORD));
  215. if (data == NULL)
  216. {
  217. return FALSE;
  218. }
  219. pal_size = 0;
  220. pal = NULL;
  221. // Unpack 8bpp data into final image
  222. if (compress == COMPRESS_NONE)
  223. {
  224. DWORD i;
  225. BYTE *src;
  226. DWORD *dst;
  227. src = (BYTE *)pdwA8;
  228. dst = (DWORD *)data;
  229. for (i = 0; i < size; i++)
  230. {
  231. *dst++ = pal[*src++];
  232. }
  233. }
  234. else
  235. {
  236. HwuRldTo32((HwMem)pdwA8, data, 1, pal);
  237. }
  238. format = GL_BGRA_EXT;
  239. components = 4;
  240. }
  241. return TRUE;
  242. }