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.

253 lines
8.4 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 static
  43. #define IMAGIC 0x01da
  44. #define IMAGIC_SWAP 0xda01
  45. #define SWAP_SHORT_BYTES(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
  46. #define SWAP_LONG_BYTES(x) (((((x) & 0xff) << 24) | (((x) & 0xff00) << 8)) | \
  47. ((((x) & 0xff0000) >> 8) | (((x) & 0xff000000) >> 24)))
  48. typedef struct _rawImageRec {
  49. unsigned short imagic;
  50. unsigned short type;
  51. unsigned short dim;
  52. unsigned short sizeX, sizeY, sizeZ;
  53. unsigned long min, max;
  54. unsigned long wasteBytes;
  55. char name[80];
  56. unsigned long colorMap;
  57. HANDLE file;
  58. unsigned char *tmp, *tmpR, *tmpG, *tmpB;
  59. unsigned long rleEnd;
  60. unsigned long *rowStart;
  61. long *rowSize;
  62. } rawImageRec;
  63. static void RawImageClose(rawImageRec *raw);
  64. static rawImageRec *RawImageOpenAW(char *fileName, BOOL bUnicode)
  65. {
  66. rawImageRec *raw;
  67. unsigned long *rowStart, *rowSize, ulTmp;
  68. int x;
  69. DWORD dwBytesRead;
  70. raw = (rawImageRec *)malloc(sizeof(rawImageRec));
  71. if (raw == NULL) {
  72. MESSAGEBOX(GetFocus(), "Out of memory.", "Error", MB_OK);
  73. return NULL;
  74. }
  75. raw->file = bUnicode ? CreateFileW((LPWSTR) fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0) :
  76. CreateFileA((LPSTR) fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
  77. if (raw->file == INVALID_HANDLE_VALUE) {
  78. char ach[256];
  79. bUnicode ? wsprintf(ach, "Failed to open image file %ws.\n", fileName) :
  80. wsprintf(ach, "Failed to open image file %s.\n", fileName);
  81. MESSAGEBOX(GetFocus(), ach, "Error", MB_OK);
  82. free( raw );
  83. return NULL;
  84. }
  85. ReadFile(raw->file, (LPVOID) raw, 12, &dwBytesRead, (LPOVERLAPPED) NULL);
  86. if (raw->imagic == IMAGIC_SWAP) {
  87. raw->type = SWAP_SHORT_BYTES(raw->type);
  88. raw->dim = SWAP_SHORT_BYTES(raw->dim);
  89. raw->sizeX = SWAP_SHORT_BYTES(raw->sizeX);
  90. raw->sizeY = SWAP_SHORT_BYTES(raw->sizeY);
  91. raw->sizeZ = SWAP_SHORT_BYTES(raw->sizeZ);
  92. } else if (raw->imagic != IMAGIC) {
  93. // magic number is absent - conclude file is invalid (?)
  94. MESSAGEBOX(GetFocus(), "Invalid rgb file.", "Error", MB_OK);
  95. RawImageClose( raw );
  96. return NULL;
  97. }
  98. raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
  99. raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
  100. raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
  101. raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
  102. if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
  103. raw->tmpB == NULL) {
  104. MESSAGEBOX(GetFocus(), "Out of memory.", "Error", MB_OK);
  105. RawImageClose( raw );
  106. return NULL;
  107. }
  108. if ((raw->type & 0xFF00) == 0x0100) {
  109. x = raw->sizeY * raw->sizeZ * sizeof(long);
  110. raw->rowStart = (unsigned long *)malloc(x);
  111. raw->rowSize = (long *)malloc(x);
  112. if (raw->rowStart == NULL || raw->rowSize == NULL) {
  113. MESSAGEBOX(GetFocus(), "Out of memory.", "Error", MB_OK);
  114. RawImageClose( raw );
  115. return NULL;
  116. }
  117. raw->rleEnd = 512 + (2 * x);
  118. SetFilePointer(raw->file, 512, NULL, FILE_BEGIN);
  119. ReadFile(raw->file, (LPVOID) raw->rowStart, x, &dwBytesRead,
  120. (LPOVERLAPPED) NULL);
  121. ReadFile(raw->file, (LPVOID) raw->rowSize, x, &dwBytesRead,
  122. (LPOVERLAPPED) NULL);
  123. if (raw->imagic == IMAGIC_SWAP) {
  124. x /= sizeof(long);
  125. rowStart = raw->rowStart;
  126. rowSize = raw->rowSize;
  127. while (x--) {
  128. ulTmp = *rowStart;
  129. *rowStart++ = SWAP_LONG_BYTES(ulTmp);
  130. ulTmp = *rowSize;
  131. *rowSize++ = SWAP_LONG_BYTES(ulTmp);
  132. }
  133. }
  134. }
  135. return raw;
  136. }
  137. static void RawImageClose(rawImageRec *raw)
  138. {
  139. if( !raw )
  140. return;
  141. CloseHandle(raw->file);
  142. if( raw->tmp ) free(raw->tmp);
  143. if( raw->tmpR ) free(raw->tmpR);
  144. if( raw->tmpG ) free(raw->tmpG);
  145. if( raw->tmpB ) free(raw->tmpB);
  146. free(raw);
  147. }
  148. static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
  149. {
  150. unsigned char *iPtr, *oPtr, pixel;
  151. int count;
  152. DWORD dwBytesRead;
  153. if ((raw->type & 0xFF00) == 0x0100) {
  154. SetFilePointer(raw->file, raw->rowStart[y+z*raw->sizeY], NULL, FILE_BEGIN);
  155. ReadFile(raw->file, (LPVOID) raw->tmp,
  156. (unsigned int)raw->rowSize[y+z*raw->sizeY], &dwBytesRead,
  157. (LPOVERLAPPED) NULL);
  158. iPtr = raw->tmp;
  159. oPtr = buf;
  160. while (1) {
  161. pixel = *iPtr++;
  162. count = (int)(pixel & 0x7F);
  163. if (!count) {
  164. return;
  165. }
  166. if (pixel & 0x80) {
  167. while (count--) {
  168. *oPtr++ = *iPtr++;
  169. }
  170. } else {
  171. pixel = *iPtr++;
  172. while (count--) {
  173. *oPtr++ = pixel;
  174. }
  175. }
  176. }
  177. } else {
  178. SetFilePointer(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
  179. NULL, FILE_BEGIN);
  180. ReadFile(raw->file, (LPVOID) buf, raw->sizeX, &dwBytesRead,
  181. (LPOVERLAPPED) NULL);
  182. }
  183. }
  184. static void RawImageGetData(rawImageRec *raw, TK_RGBImageRec *final)
  185. {
  186. unsigned char *ptr;
  187. int i, j;
  188. final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
  189. if (final->data == NULL) {
  190. MESSAGEBOX(GetFocus(), "Out of memory.", "Error", MB_OK);
  191. return;
  192. }
  193. ptr = final->data;
  194. for (i = 0; i < raw->sizeY; i++) {
  195. RawImageGetRow(raw, raw->tmpR, i, 0);
  196. RawImageGetRow(raw, raw->tmpG, i, 1);
  197. RawImageGetRow(raw, raw->tmpB, i, 2);
  198. for (j = 0; j < raw->sizeX; j++) {
  199. *ptr++ = *(raw->tmpR + j);
  200. *ptr++ = *(raw->tmpG + j);
  201. *ptr++ = *(raw->tmpB + j);
  202. }
  203. }
  204. }
  205. TK_RGBImageRec *tkRGBImageLoad(char *fileName)
  206. {
  207. return tkRGBImageLoadAW(fileName, FALSE);
  208. }
  209. TK_RGBImageRec *tkRGBImageLoadAW(char *fileName, BOOL bUnicode)
  210. {
  211. rawImageRec *raw;
  212. TK_RGBImageRec *final;
  213. if( !(raw = RawImageOpenAW(fileName, bUnicode)) )
  214. return NULL;
  215. final = (TK_RGBImageRec *)malloc(sizeof(TK_RGBImageRec));
  216. if (final == NULL) {
  217. MESSAGEBOX(GetFocus(), "Out of memory.", "Error", MB_OK);
  218. RawImageClose(raw);
  219. return NULL;
  220. }
  221. final->sizeX = raw->sizeX;
  222. final->sizeY = raw->sizeY;
  223. RawImageGetData(raw, final);
  224. RawImageClose(raw);
  225. return final;
  226. }