Leaked source code of windows server 2003
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.

264 lines
7.3 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: texrgb.cxx.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 "mtk.h"
  14. #include "texture.hxx"
  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. PUCHAR RawImageGetData(rawImageRec *raw )
  147. {
  148. unsigned char *ptr;
  149. int i, j;
  150. PUCHAR pData;
  151. pData = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
  152. if (pData == NULL) {
  153. return 0;
  154. }
  155. ptr = pData;
  156. for (i = 0; i < raw->sizeY; i++) {
  157. RawImageGetRow(raw, raw->tmpR, i, 0);
  158. RawImageGetRow(raw, raw->tmpG, i, 1);
  159. RawImageGetRow(raw, raw->tmpB, i, 2);
  160. for (j = 0; j < raw->sizeX; j++) {
  161. *ptr++ = *(raw->tmpR + j);
  162. *ptr++ = *(raw->tmpG + j);
  163. *ptr++ = *(raw->tmpB + j);
  164. }
  165. }
  166. return pData;
  167. }
  168. BOOL
  169. TEXTURE::RGBImageLoad( PVOID pv )
  170. {
  171. rawImageRec *raw;
  172. if( !(raw = RawImageOpen( pv )) )
  173. return FALSE;
  174. width = raw->sizeX;
  175. height = raw->sizeY;
  176. format = GL_RGB;
  177. components = 3;
  178. pal_size = 0;
  179. pal = NULL;
  180. data = RawImageGetData( raw );
  181. RawImageClose(raw);
  182. return TRUE;
  183. }
  184. /******************************Public*Routine******************************\
  185. *
  186. * bVerifyRGB
  187. *
  188. * Stripped down version of tkRGBImageLoadAW that verifies that an rgb
  189. * file is valid and, if so, returns the bitmap dimensions.
  190. *
  191. * Returns:
  192. * TRUE if valid rgb file; otherwise, FALSE.
  193. *
  194. \**************************************************************************/
  195. BOOL
  196. bVerifyRGB(LPTSTR pszFileName, ISIZE *pSize )
  197. {
  198. rawImageRec *raw;
  199. DWORD dwBytesRead;
  200. BOOL bRet = FALSE;
  201. raw = (rawImageRec *)
  202. LocalAlloc( LMEM_FIXED | LMEM_ZEROINIT, sizeof(rawImageRec) );
  203. if (raw == NULL) {
  204. return FALSE;
  205. }
  206. raw->file = CreateFile((LPTSTR) pszFileName, GENERIC_READ, FILE_SHARE_READ,
  207. NULL, OPEN_EXISTING, 0, 0);
  208. if (raw->file == INVALID_HANDLE_VALUE) {
  209. goto bVerifyRGB_cleanup;
  210. }
  211. ReadFile(raw->file, (LPVOID) raw, 12, &dwBytesRead, (LPOVERLAPPED) NULL);
  212. if( raw->imagic == IMAGIC_SWAP ) {
  213. raw->sizeX = SWAP_SHORT_BYTES(raw->sizeX);
  214. raw->sizeY = SWAP_SHORT_BYTES(raw->sizeY);
  215. bRet = TRUE;
  216. } else if( raw->imagic == IMAGIC)
  217. bRet = TRUE;
  218. bVerifyRGB_cleanup:
  219. if( bRet && pSize ) {
  220. pSize->width = raw->sizeX;
  221. pSize->height = raw->sizeY;
  222. }
  223. if( raw->file != INVALID_HANDLE_VALUE )
  224. CloseHandle( raw->file );
  225. LocalFree( raw );
  226. return bRet;
  227. }