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.

374 lines
9.0 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 <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <malloc.h>
  43. #include <fcntl.h>
  44. //#include <unistd.h>
  45. #include <math.h>
  46. #include "tk.h"
  47. // fix up math definitions
  48. #define logf log
  49. #define powf pow
  50. #define STEPCOUNT 40
  51. #define FALSE 0
  52. #define TRUE 1
  53. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  54. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  55. enum {
  56. OP_NOOP = 0,
  57. OP_STRETCH,
  58. OP_DRAWPOINT,
  59. OP_DRAWIMAGE
  60. };
  61. typedef struct _cRec {
  62. float x, y;
  63. } cRec;
  64. typedef struct _vertexRec {
  65. float x, y;
  66. float dX, dY;
  67. float tX, tY;
  68. } vertexRec;
  69. GLenum doubleBuffer, directRender;
  70. int imageSizeX, imageSizeY;
  71. char *fileName = 0;
  72. TK_RGBImageRec *image;
  73. cRec cList[50];
  74. vertexRec vList[5];
  75. int cCount, cIndex[2], cStep;
  76. GLenum op = OP_NOOP;
  77. void DrawImage(void)
  78. {
  79. glRasterPos2i(0, 0);
  80. glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
  81. image->data);
  82. tkSwapBuffers();
  83. glRasterPos2i(0, 0);
  84. glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
  85. image->data);
  86. }
  87. void DrawPoint(void)
  88. {
  89. int i;
  90. glColor3f(1.0, 0.0, 1.0);
  91. glPointSize(3.0);
  92. glBegin(GL_POINTS);
  93. for (i = 0; i < cCount; i++) {
  94. glVertex2f(cList[i].x, cList[i].y);
  95. }
  96. glEnd();
  97. tkSwapBuffers();
  98. }
  99. void InitVList(void)
  100. {
  101. vList[0].x = 0.0;
  102. vList[0].y = 0.0;
  103. vList[0].dX = 0.0;
  104. vList[0].dY = 0.0;
  105. vList[0].tX = 0.0;
  106. vList[0].tY = 0.0;
  107. vList[1].x = (float)imageSizeX;
  108. vList[1].y = 0.0;
  109. vList[1].dX = 0.0;
  110. vList[1].dY = 0.0;
  111. vList[1].tX = 1.0;
  112. vList[1].tY = 0.0;
  113. vList[2].x = (float)imageSizeX;
  114. vList[2].y = (float)imageSizeY;
  115. vList[2].dX = 0.0;
  116. vList[2].dY = 0.0;
  117. vList[2].tX = 1.0;
  118. vList[2].tY = 1.0;
  119. vList[3].x = 0.0;
  120. vList[3].y = (float)imageSizeY;
  121. vList[3].dX = 0.0;
  122. vList[3].dY = 0.0;
  123. vList[3].tX = 0.0;
  124. vList[3].tY = 1.0;
  125. vList[4].x = cList[0].x;
  126. vList[4].y = cList[0].y;
  127. vList[4].dX = (cList[1].x - cList[0].x) / STEPCOUNT;
  128. vList[4].dY = (cList[1].y - cList[0].y) / STEPCOUNT;
  129. vList[4].tX = cList[0].x / (float)imageSizeX;
  130. vList[4].tY = cList[0].y / (float)imageSizeY;
  131. }
  132. void ScaleImage(int sizeX, int sizeY)
  133. {
  134. GLubyte *buf;
  135. buf = (GLubyte *)malloc(3*sizeX*sizeY);
  136. gluScaleImage(GL_RGB, image->sizeX, image->sizeY, GL_UNSIGNED_BYTE,
  137. image->data, sizeX, sizeY, GL_UNSIGNED_BYTE, buf);
  138. free(image->data);
  139. image->data = buf;
  140. image->sizeX = sizeX;
  141. image->sizeY = sizeY;
  142. }
  143. void SetPoint(int x, int y)
  144. {
  145. cList[cCount].x = (float)x;
  146. cList[cCount].y = (float)y;
  147. cCount++;
  148. }
  149. void Stretch(void)
  150. {
  151. glBegin(GL_TRIANGLES);
  152. glTexCoord2f(vList[0].tX, vList[0].tY);
  153. glVertex2f(vList[0].x, vList[0].y);
  154. glTexCoord2f(vList[1].tX, vList[1].tY);
  155. glVertex2f(vList[1].x, vList[1].y);
  156. glTexCoord2f(vList[4].tX, vList[4].tY);
  157. glVertex2f(vList[4].x, vList[4].y);
  158. glEnd();
  159. glBegin(GL_TRIANGLES);
  160. glTexCoord2f(vList[1].tX, vList[1].tY);
  161. glVertex2f(vList[1].x, vList[1].y);
  162. glTexCoord2f(vList[2].tX, vList[2].tY);
  163. glVertex2f(vList[2].x, vList[2].y);
  164. glTexCoord2f(vList[4].tX, vList[4].tY);
  165. glVertex2f(vList[4].x, vList[4].y);
  166. glEnd();
  167. glBegin(GL_TRIANGLES);
  168. glTexCoord2f(vList[2].tX, vList[2].tY);
  169. glVertex2f(vList[2].x, vList[2].y);
  170. glTexCoord2f(vList[3].tX, vList[3].tY);
  171. glVertex2f(vList[3].x, vList[3].y);
  172. glTexCoord2f(vList[4].tX, vList[4].tY);
  173. glVertex2f(vList[4].x, vList[4].y);
  174. glEnd();
  175. glBegin(GL_TRIANGLES);
  176. glTexCoord2f(vList[3].tX, vList[3].tY);
  177. glVertex2f(vList[3].x, vList[3].y);
  178. glTexCoord2f(vList[0].tX, vList[0].tY);
  179. glVertex2f(vList[0].x, vList[0].y);
  180. glTexCoord2f(vList[4].tX, vList[4].tY);
  181. glVertex2f(vList[4].x, vList[4].y);
  182. glEnd();
  183. tkSwapBuffers();
  184. if (++cStep < STEPCOUNT) {
  185. vList[4].x += vList[4].dX;
  186. vList[4].y += vList[4].dY;
  187. } else {
  188. cIndex[0] = cIndex[1];
  189. cIndex[1] = cIndex[1] + 1;
  190. if (cIndex[1] == cCount) {
  191. cIndex[1] = 0;
  192. }
  193. vList[4].dX = (cList[cIndex[1]].x - cList[cIndex[0]].x) / STEPCOUNT;
  194. vList[4].dY = (cList[cIndex[1]].y - cList[cIndex[0]].y) / STEPCOUNT;
  195. cStep = 0;
  196. }
  197. }
  198. GLenum Key(int key, GLenum mask)
  199. {
  200. switch (key) {
  201. case TK_ESCAPE:
  202. free(image->data);
  203. tkQuit();
  204. case TK_SPACE:
  205. if (cCount > 1) {
  206. InitVList();
  207. cIndex[0] = 0;
  208. cIndex[1] = 1;
  209. cStep = 0;
  210. glEnable(GL_TEXTURE_2D);
  211. op = OP_STRETCH;
  212. }
  213. break;
  214. default:
  215. return GL_FALSE;
  216. }
  217. return GL_TRUE;
  218. }
  219. GLenum Mouse(int mouseX, int mouseY, GLenum button)
  220. {
  221. if (op == OP_STRETCH) {
  222. glDisable(GL_TEXTURE_2D);
  223. cCount = 0;
  224. op = OP_DRAWIMAGE;
  225. } else {
  226. SetPoint(mouseX, imageSizeY-mouseY);
  227. op = OP_DRAWPOINT;
  228. }
  229. return GL_TRUE;
  230. }
  231. void Animate(void)
  232. {
  233. switch (op) {
  234. case OP_STRETCH:
  235. Stretch();
  236. break;
  237. case OP_DRAWPOINT:
  238. DrawPoint();
  239. break;
  240. case OP_DRAWIMAGE:
  241. DrawImage();
  242. break;
  243. }
  244. }
  245. static GLenum Args(int argc, char **argv)
  246. {
  247. GLint i;
  248. doubleBuffer = GL_FALSE;
  249. directRender = GL_FALSE;
  250. for (i = 1; i < argc; i++) {
  251. if (strcmp(argv[i], "-sb") == 0) {
  252. doubleBuffer = GL_FALSE;
  253. } else if (strcmp(argv[i], "-db") == 0) {
  254. doubleBuffer = GL_TRUE;
  255. } else if (strcmp(argv[i], "-dr") == 0) {
  256. directRender = GL_TRUE;
  257. } else if (strcmp(argv[i], "-ir") == 0) {
  258. directRender = GL_FALSE;
  259. } else if (strcmp(argv[i], "-f") == 0) {
  260. if (i+1 >= argc || argv[i+1][0] == '-') {
  261. printf("-f (No file name).\n");
  262. return GL_FALSE;
  263. } else {
  264. fileName = argv[++i];
  265. }
  266. } else {
  267. printf("%s (Bad option).\n", argv[i]);
  268. return GL_FALSE;
  269. }
  270. }
  271. return GL_TRUE;
  272. }
  273. void main(int argc, char **argv)
  274. {
  275. GLenum type;
  276. if (Args(argc, argv) == GL_FALSE) {
  277. tkQuit();
  278. }
  279. if (fileName == 0) {
  280. printf("No image file.\n");
  281. tkQuit();
  282. }
  283. image = tkRGBImageLoad(fileName);
  284. imageSizeX = (int)powf(2.0, (float)((int)(logf(image->sizeX)/logf(2.0))));
  285. imageSizeY = (int)powf(2.0, (float)((int)(logf(image->sizeY)/logf(2.0))));
  286. tkInitPosition(0, 0, imageSizeX, imageSizeY);
  287. type = TK_RGB;
  288. type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  289. type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  290. tkInitDisplayMode(type);
  291. if (tkInitWindow("Stretch") == GL_FALSE) {
  292. tkQuit();
  293. }
  294. glViewport(0, 0, imageSizeX, imageSizeY);
  295. gluOrtho2D(0, imageSizeX, 0, imageSizeY);
  296. glClearColor(0.0, 0.0, 0.0, 0.0);
  297. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  298. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  299. ScaleImage(imageSizeX, imageSizeY);
  300. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  301. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  302. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  303. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  304. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  305. glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX, image->sizeY, 0,
  306. GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)image->data);
  307. cCount = 0;
  308. cIndex[0] = 0;
  309. cIndex[1] = 0;
  310. cStep = 0;
  311. op = OP_DRAWIMAGE;
  312. tkKeyDownFunc(Key);
  313. tkMouseDownFunc(Mouse);
  314. tkIdleFunc(Animate);
  315. tkExec();
  316. }