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.

260 lines
7.1 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: ssimage.c
  3. *
  4. * Operations on .rgb 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 "tk.h"
  14. #include "sscommon.h"
  15. #define IMAGIC 0x01da
  16. #define IMAGIC_SWAP 0xda01
  17. #define SWAP_SHORT_BYTES(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
  18. #define SWAP_LONG_BYTES(x) (((((x) & 0xff) << 24) | (((x) & 0xff00) << 8)) | \
  19. ((((x) & 0xff0000) >> 8) | (((x) & 0xff000000) >> 24)))
  20. typedef struct _rawImageRec {
  21. unsigned short imagic;
  22. unsigned short type;
  23. unsigned short dim;
  24. unsigned short sizeX, sizeY, sizeZ;
  25. unsigned long min, max;
  26. unsigned long wasteBytes;
  27. char name[80];
  28. unsigned long colorMap;
  29. HANDLE file;
  30. unsigned char *tmp, *tmpR, *tmpG, *tmpB;
  31. unsigned long rleEnd;
  32. unsigned long *rowStart;
  33. long *rowSize;
  34. // !!! Hack to stick in a pointer to the resource data - shouldn't be
  35. // a problem, since rgb files always have 512 byte header
  36. unsigned char *data;
  37. } rawImageRec;
  38. static void RawImageClose(rawImageRec *raw);
  39. /**************************************************************************\
  40. *
  41. * Hacked form of tk_RGBImageLoad(), for reading a .rgb file from a resource
  42. *
  43. * Copyright (c) 1995 Microsoft Corporation
  44. *
  45. \**************************************************************************/
  46. #include <windows.h>
  47. static rawImageRec *RawImageOpen( PVOID pv )
  48. {
  49. rawImageRec *raw;
  50. unsigned long *rowStart, *rowSize, ulTmp;
  51. int x;
  52. DWORD dwBytesRead;
  53. raw = (rawImageRec *)malloc(sizeof(rawImageRec));
  54. if (raw == NULL) {
  55. return 0;
  56. }
  57. // Make a copy of the resource header, since we may be doing some byte
  58. // swapping, and resources are read-only
  59. *raw = *((rawImageRec *) pv);
  60. if (raw->imagic == IMAGIC_SWAP) {
  61. raw->type = SWAP_SHORT_BYTES(raw->type);
  62. raw->dim = SWAP_SHORT_BYTES(raw->dim);
  63. raw->sizeX = SWAP_SHORT_BYTES(raw->sizeX);
  64. raw->sizeY = SWAP_SHORT_BYTES(raw->sizeY);
  65. raw->sizeZ = SWAP_SHORT_BYTES(raw->sizeZ);
  66. }
  67. raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
  68. raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
  69. raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
  70. raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
  71. if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
  72. raw->tmpB == NULL) {
  73. RawImageClose(raw);
  74. return 0;
  75. }
  76. if ((raw->type & 0xFF00) == 0x0100) {
  77. x = raw->sizeY * raw->sizeZ * sizeof(long);
  78. raw->rowStart = (unsigned long *)malloc(x);
  79. raw->rowSize = (long *)malloc(x);
  80. if (raw->rowStart == NULL || raw->rowSize == NULL) {
  81. RawImageClose(raw);
  82. return 0;
  83. }
  84. //mf: not used (?)
  85. raw->rleEnd = 512 + (2 * x);
  86. //mf: hack to point to resource data
  87. raw->data = ((unsigned char *) pv);
  88. RtlCopyMemory( raw->rowStart, raw->data + 512, x );
  89. RtlCopyMemory( raw->rowSize, raw->data + 512 + x, x );
  90. if (raw->imagic == IMAGIC_SWAP) {
  91. x /= sizeof(long);
  92. rowStart = raw->rowStart;
  93. rowSize = (unsigned long *) raw->rowSize;
  94. while (x--) {
  95. ulTmp = *rowStart;
  96. *rowStart++ = SWAP_LONG_BYTES(ulTmp);
  97. ulTmp = *rowSize;
  98. *rowSize++ = SWAP_LONG_BYTES(ulTmp);
  99. }
  100. }
  101. }
  102. return raw;
  103. }
  104. static void RawImageClose(rawImageRec *raw)
  105. {
  106. if( !raw )
  107. return;
  108. if( raw->tmp ) free(raw->tmp);
  109. if( raw->tmpR ) free(raw->tmpR);
  110. if( raw->tmpG ) free(raw->tmpG);
  111. if( raw->tmpB ) free(raw->tmpB);
  112. free(raw);
  113. }
  114. static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
  115. {
  116. unsigned char *iPtr, *oPtr, pixel;
  117. int count;
  118. DWORD dwBytesRead;
  119. if ((raw->type & 0xFF00) == 0x0100) {
  120. RtlCopyMemory(raw->tmp, raw->data + raw->rowStart[y+z*raw->sizeY],
  121. (unsigned int)raw->rowSize[y+z*raw->sizeY] );
  122. iPtr = raw->tmp;
  123. oPtr = buf;
  124. while (1) {
  125. pixel = *iPtr++;
  126. count = (int)(pixel & 0x7F);
  127. if (!count) {
  128. return;
  129. }
  130. if (pixel & 0x80) {
  131. while (count--) {
  132. *oPtr++ = *iPtr++;
  133. }
  134. } else {
  135. pixel = *iPtr++;
  136. while (count--) {
  137. *oPtr++ = pixel;
  138. }
  139. }
  140. }
  141. } else {
  142. iPtr = raw->data + 512 + (y*raw->sizeX)+(z*raw->sizeX*raw->sizeY);
  143. RtlCopyMemory( buf, iPtr, raw->sizeX );
  144. }
  145. }
  146. static void RawImageGetData(rawImageRec *raw, TEXTURE *ptex)
  147. {
  148. unsigned char *ptr;
  149. int i, j;
  150. ptex->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
  151. if (ptex->data == NULL) {
  152. return;
  153. }
  154. ptr = ptex->data;
  155. for (i = 0; i < raw->sizeY; i++) {
  156. RawImageGetRow(raw, raw->tmpR, i, 0);
  157. RawImageGetRow(raw, raw->tmpG, i, 1);
  158. RawImageGetRow(raw, raw->tmpB, i, 2);
  159. for (j = 0; j < raw->sizeX; j++) {
  160. *ptr++ = *(raw->tmpR + j);
  161. *ptr++ = *(raw->tmpG + j);
  162. *ptr++ = *(raw->tmpB + j);
  163. }
  164. }
  165. }
  166. BOOL ss_RGBImageLoad( PVOID pv, TEXTURE *ptex )
  167. {
  168. rawImageRec *raw;
  169. if( !(raw = RawImageOpen( pv )) )
  170. return FALSE;
  171. ptex->width = raw->sizeX;
  172. ptex->height = raw->sizeY;
  173. ptex->format = GL_RGB;
  174. ptex->components = 3;
  175. ptex->pal_size = 0;
  176. ptex->pal = NULL;
  177. RawImageGetData(raw, ptex);
  178. RawImageClose(raw);
  179. return TRUE;
  180. }
  181. /******************************Public*Routine******************************\
  182. *
  183. * bVerifyRGB
  184. *
  185. * Stripped down version of tkRGBImageLoadAW that verifies that an rgb
  186. * file is valid and, if so, returns the bitmap dimensions.
  187. *
  188. * Returns:
  189. * TRUE if valid rgb file; otherwise, FALSE.
  190. *
  191. \**************************************************************************/
  192. BOOL
  193. bVerifyRGB(LPTSTR pszFileName, ISIZE *pSize )
  194. {
  195. rawImageRec *raw;
  196. DWORD dwBytesRead;
  197. BOOL bRet = FALSE;
  198. raw = (rawImageRec *)
  199. LocalAlloc( LMEM_FIXED | LMEM_ZEROINIT, sizeof(rawImageRec) );
  200. if (raw == NULL) {
  201. return FALSE;
  202. }
  203. raw->file = CreateFile((LPTSTR) pszFileName, GENERIC_READ, FILE_SHARE_READ,
  204. NULL, OPEN_EXISTING, 0, 0);
  205. if (raw->file == INVALID_HANDLE_VALUE) {
  206. goto bVerifyRGB_cleanup;
  207. }
  208. ReadFile(raw->file, (LPVOID) raw, 12, &dwBytesRead, (LPOVERLAPPED) NULL);
  209. if( raw->imagic == IMAGIC_SWAP ) {
  210. raw->sizeX = SWAP_SHORT_BYTES(raw->sizeX);
  211. raw->sizeY = SWAP_SHORT_BYTES(raw->sizeY);
  212. bRet = TRUE;
  213. } else if( raw->imagic == IMAGIC)
  214. bRet = TRUE;
  215. bVerifyRGB_cleanup:
  216. if( bRet && pSize ) {
  217. pSize->width = raw->sizeX;
  218. pSize->height = raw->sizeY;
  219. }
  220. if( raw->file != INVALID_HANDLE_VALUE )
  221. CloseHandle( raw->file );
  222. LocalFree( raw );
  223. return bRet;
  224. }