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.

218 lines
6.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 <GL/gl.h>
  42. #include "stonehen.h"
  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. FILE *file;
  58. unsigned char *tmp, *tmpR, *tmpG, *tmpB;
  59. unsigned long rleEnd;
  60. unsigned long *rowStart;
  61. unsigned long *rowSize;
  62. } rawImageRec;
  63. static rawImageRec *RawImageOpen(char *fileName)
  64. {
  65. rawImageRec *raw;
  66. unsigned long *rowStart, *rowSize, ulTmp;
  67. int x;
  68. raw = (rawImageRec *)malloc(sizeof(rawImageRec));
  69. if (raw == NULL) {
  70. fprintf(stderr, "Out of memory!\n");
  71. exit(-1);
  72. }
  73. if ((raw->file = fopen(fileName, "rb")) == NULL) {
  74. perror(fileName);
  75. exit(-1);
  76. }
  77. fread(raw, 1, 12, raw->file);
  78. if (raw->imagic == IMAGIC_SWAP) {
  79. raw->type = SWAP_SHORT_BYTES(raw->type);
  80. raw->dim = SWAP_SHORT_BYTES(raw->dim);
  81. raw->sizeX = SWAP_SHORT_BYTES(raw->sizeX);
  82. raw->sizeY = SWAP_SHORT_BYTES(raw->sizeY);
  83. raw->sizeZ = SWAP_SHORT_BYTES(raw->sizeZ);
  84. }
  85. raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
  86. raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
  87. raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
  88. raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
  89. if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
  90. raw->tmpB == NULL) {
  91. fprintf(stderr, "Out of memory!\n");
  92. exit(-1);
  93. }
  94. if ((raw->type & 0xFF00) == 0x0100) {
  95. x = raw->sizeY * raw->sizeZ * sizeof(long);
  96. raw->rowStart = (unsigned long *)malloc(x);
  97. raw->rowSize = (unsigned long *)malloc(x);
  98. if (raw->rowStart == NULL || raw->rowSize == NULL) {
  99. fprintf(stderr, "Out of memory!\n");
  100. exit(-1);
  101. }
  102. raw->rleEnd = 512 + (2 * x);
  103. fseek(raw->file, 512, SEEK_SET);
  104. fread(raw->rowStart, 1, x, raw->file);
  105. fread(raw->rowSize, 1, x, raw->file);
  106. if (raw->imagic == IMAGIC_SWAP) {
  107. x /= sizeof(long);
  108. rowStart = raw->rowStart;
  109. rowSize = raw->rowSize;
  110. while (x--) {
  111. ulTmp = *rowStart;
  112. *rowStart++ = SWAP_LONG_BYTES(ulTmp);
  113. ulTmp = *rowSize;
  114. *rowSize++ = SWAP_LONG_BYTES(ulTmp);
  115. }
  116. }
  117. }
  118. return raw;
  119. }
  120. static void RawImageClose(rawImageRec *raw)
  121. {
  122. fclose(raw->file);
  123. free(raw->tmp);
  124. free(raw->tmpR);
  125. free(raw->tmpG);
  126. free(raw->tmpB);
  127. free(raw);
  128. }
  129. static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
  130. {
  131. unsigned char *iPtr, *oPtr, pixel;
  132. int count;
  133. if ((raw->type & 0xFF00) == 0x0100) {
  134. fseek(raw->file, raw->rowStart[y+z*raw->sizeY], SEEK_SET);
  135. fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY],
  136. raw->file);
  137. iPtr = raw->tmp;
  138. oPtr = buf;
  139. while (1) {
  140. pixel = *iPtr++;
  141. count = (int)(pixel & 0x7F);
  142. if (!count) {
  143. return;
  144. }
  145. if (pixel & 0x80) {
  146. while (count--) {
  147. *oPtr++ = *iPtr++;
  148. }
  149. } else {
  150. pixel = *iPtr++;
  151. while (count--) {
  152. *oPtr++ = pixel;
  153. }
  154. }
  155. }
  156. } else {
  157. fseek(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
  158. SEEK_SET);
  159. fread(buf, 1, raw->sizeX, raw->file);
  160. }
  161. }
  162. static void RawImageGetData(rawImageRec *raw, RGBImageRec *final)
  163. {
  164. unsigned char *ptr;
  165. int i, j;
  166. final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
  167. if (final->data == NULL) {
  168. fprintf(stderr, "Out of memory!\n");
  169. exit(-1);
  170. }
  171. ptr = final->data;
  172. for (i = 0; i < raw->sizeY; i++) {
  173. RawImageGetRow(raw, raw->tmpR, i, 0);
  174. RawImageGetRow(raw, raw->tmpG, i, 1);
  175. RawImageGetRow(raw, raw->tmpB, i, 2);
  176. for (j = 0; j < raw->sizeX; j++) {
  177. *ptr++ = *(raw->tmpR + j);
  178. *ptr++ = *(raw->tmpG + j);
  179. *ptr++ = *(raw->tmpB + j);
  180. }
  181. }
  182. }
  183. RGBImageRec *RGBImageLoad(char *fileName)
  184. {
  185. rawImageRec *raw;
  186. RGBImageRec *final;
  187. raw = RawImageOpen(fileName);
  188. final = (RGBImageRec *)malloc(sizeof(RGBImageRec));
  189. if (final == NULL) {
  190. fprintf(stderr, "Out of memory!\n");
  191. exit(-1);
  192. }
  193. final->sizeX = raw->sizeX;
  194. final->sizeY = raw->sizeY;
  195. RawImageGetData(raw, final);
  196. RawImageClose(raw);
  197. return final;
  198. }