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.

237 lines
7.9 KiB

  1. /*
  2. * (c) Copyright 1993, Silicon Graphics, Inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. */
  37. #include <windows.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include "tk.h"
  42. #define IMAGIC 0x01da
  43. #define IMAGIC_SWAP 0xda01
  44. #define SWAP_SHORT_BYTES(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
  45. #define SWAP_LONG_BYTES(x) (((((x) & 0xff) << 24) | (((x) & 0xff00) << 8)) | \
  46. ((((x) & 0xff0000) >> 8) | (((x) & 0xff000000) >> 24)))
  47. typedef struct _rawImageRec {
  48. unsigned short imagic;
  49. unsigned short type;
  50. unsigned short dim;
  51. unsigned short sizeX, sizeY, sizeZ;
  52. unsigned long min, max;
  53. unsigned long wasteBytes;
  54. char name[80];
  55. unsigned long colorMap;
  56. HANDLE file;
  57. unsigned char *tmp, *tmpR, *tmpG, *tmpB;
  58. unsigned long rleEnd;
  59. unsigned long *rowStart;
  60. long *rowSize;
  61. } rawImageRec;
  62. static rawImageRec *RawImageOpenAW(char *fileName, BOOL bUnicode)
  63. {
  64. rawImageRec *raw;
  65. unsigned long *rowStart, *rowSize, ulTmp;
  66. int x;
  67. DWORD dwBytesRead;
  68. raw = (rawImageRec *)malloc(sizeof(rawImageRec));
  69. if (raw == NULL) {
  70. MessageBoxA(NULL, "Out of memory.", "Error", MB_OK);
  71. tkQuit();
  72. }
  73. raw->file = bUnicode ? CreateFileW((LPWSTR) fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0) :
  74. CreateFileA((LPSTR) fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
  75. if (raw->file == INVALID_HANDLE_VALUE) {
  76. char ach[256];
  77. bUnicode ? wsprintf(ach, "Failed to open image file %ws.\n", fileName) :
  78. wsprintf(ach, "Failed to open image file %s.\n", fileName);
  79. MessageBoxA(NULL, ach, "Error", MB_OK);
  80. tkQuit();
  81. }
  82. ReadFile(raw->file, (LPVOID) raw, 12, &dwBytesRead, (LPOVERLAPPED) NULL);
  83. if (raw->imagic == IMAGIC_SWAP) {
  84. raw->type = SWAP_SHORT_BYTES(raw->type);
  85. raw->dim = SWAP_SHORT_BYTES(raw->dim);
  86. raw->sizeX = SWAP_SHORT_BYTES(raw->sizeX);
  87. raw->sizeY = SWAP_SHORT_BYTES(raw->sizeY);
  88. raw->sizeZ = SWAP_SHORT_BYTES(raw->sizeZ);
  89. }
  90. raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
  91. raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
  92. raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
  93. raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
  94. if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
  95. raw->tmpB == NULL) {
  96. MessageBoxA(NULL, "Out of memory.", "Error", MB_OK);
  97. tkQuit();
  98. }
  99. if ((raw->type & 0xFF00) == 0x0100) {
  100. x = raw->sizeY * raw->sizeZ * sizeof(long);
  101. raw->rowStart = (unsigned long *)malloc(x);
  102. raw->rowSize = (long *)malloc(x);
  103. if (raw->rowStart == NULL || raw->rowSize == NULL) {
  104. MessageBoxA(NULL, "Out of memory.", "Error", MB_OK);
  105. tkQuit();
  106. }
  107. raw->rleEnd = 512 + (2 * x);
  108. SetFilePointer(raw->file, 512, NULL, FILE_BEGIN);
  109. ReadFile(raw->file, (LPVOID) raw->rowStart, x, &dwBytesRead,
  110. (LPOVERLAPPED) NULL);
  111. ReadFile(raw->file, (LPVOID) raw->rowSize, x, &dwBytesRead,
  112. (LPOVERLAPPED) NULL);
  113. if (raw->imagic == IMAGIC_SWAP) {
  114. x /= sizeof(long);
  115. rowStart = raw->rowStart;
  116. rowSize = raw->rowSize;
  117. while (x--) {
  118. ulTmp = *rowStart;
  119. *rowStart++ = SWAP_LONG_BYTES(ulTmp);
  120. ulTmp = *rowSize;
  121. *rowSize++ = SWAP_LONG_BYTES(ulTmp);
  122. }
  123. }
  124. }
  125. return raw;
  126. }
  127. static void RawImageClose(rawImageRec *raw)
  128. {
  129. CloseHandle(raw->file);
  130. free(raw->tmp);
  131. free(raw->tmpR);
  132. free(raw->tmpG);
  133. free(raw->tmpB);
  134. free(raw);
  135. }
  136. static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
  137. {
  138. unsigned char *iPtr, *oPtr, pixel;
  139. int count;
  140. DWORD dwBytesRead;
  141. if ((raw->type & 0xFF00) == 0x0100) {
  142. SetFilePointer(raw->file, raw->rowStart[y+z*raw->sizeY], NULL, FILE_BEGIN);
  143. ReadFile(raw->file, (LPVOID) raw->tmp,
  144. (unsigned int)raw->rowSize[y+z*raw->sizeY], &dwBytesRead,
  145. (LPOVERLAPPED) NULL);
  146. iPtr = raw->tmp;
  147. oPtr = buf;
  148. while (1) {
  149. pixel = *iPtr++;
  150. count = (int)(pixel & 0x7F);
  151. if (!count) {
  152. return;
  153. }
  154. if (pixel & 0x80) {
  155. while (count--) {
  156. *oPtr++ = *iPtr++;
  157. }
  158. } else {
  159. pixel = *iPtr++;
  160. while (count--) {
  161. *oPtr++ = pixel;
  162. }
  163. }
  164. }
  165. } else {
  166. SetFilePointer(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
  167. NULL, FILE_BEGIN);
  168. ReadFile(raw->file, (LPVOID) buf, raw->sizeX, &dwBytesRead,
  169. (LPOVERLAPPED) NULL);
  170. }
  171. }
  172. static void RawImageGetData(rawImageRec *raw, TK_RGBImageRec *final)
  173. {
  174. unsigned char *ptr;
  175. int i, j;
  176. final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
  177. if (final->data == NULL) {
  178. MessageBoxA(NULL, "Out of memory.", "Error", MB_OK);
  179. tkQuit();
  180. }
  181. ptr = final->data;
  182. for (i = 0; i < raw->sizeY; i++) {
  183. RawImageGetRow(raw, raw->tmpR, i, 0);
  184. RawImageGetRow(raw, raw->tmpG, i, 1);
  185. RawImageGetRow(raw, raw->tmpB, i, 2);
  186. for (j = 0; j < raw->sizeX; j++) {
  187. *ptr++ = *(raw->tmpR + j);
  188. *ptr++ = *(raw->tmpG + j);
  189. *ptr++ = *(raw->tmpB + j);
  190. }
  191. }
  192. }
  193. TK_RGBImageRec *tkRGBImageLoad(char *fileName)
  194. {
  195. return tkRGBImageLoadAW(fileName, FALSE);
  196. }
  197. TK_RGBImageRec *tkRGBImageLoadAW(char *fileName, BOOL bUnicode)
  198. {
  199. rawImageRec *raw;
  200. TK_RGBImageRec *final;
  201. raw = RawImageOpenAW(fileName, bUnicode);
  202. final = (TK_RGBImageRec *)malloc(sizeof(TK_RGBImageRec));
  203. if (final == NULL) {
  204. MessageBoxA(NULL, "Out of memory.", "Error", MB_OK);
  205. tkQuit();
  206. }
  207. final->sizeX = raw->sizeX;
  208. final->sizeY = raw->sizeY;
  209. RawImageGetData(raw, final);
  210. RawImageClose(raw);
  211. return final;
  212. }