MyTexture.cpp
上传用户:sz25923981
上传日期:2022-06-28
资源大小:3615k
文件大小:6k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // MyTexture.cpp: implementation of the CMyTexture class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "MyTerrain1.h"
  6. #include "MyTexture.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CMyTexture::CMyTexture()
  16. {
  17. }
  18. CMyTexture::~CMyTexture()
  19. {
  20. }
  21. void CMyTexture::bwtorgba(unsigned char *b,unsigned char *l,int n)
  22. {
  23.     while(n--)
  24. {
  25. l[0] = *b;
  26. l[1] = *b;
  27. l[2] = *b;
  28. l[3] = 0xff;
  29. l += 4; b++;
  30.     }
  31. }
  32. void CMyTexture::latorgba(unsigned char *b, unsigned char *a,unsigned char *l,int n) 
  33. {
  34.     while(n--) 
  35. {
  36. l[0] = *b;
  37. l[1] = *b;
  38. l[2] = *b;
  39. l[3] = *a;
  40. l += 4; b++; a++;
  41.     }
  42. }
  43. void CMyTexture::rgbtorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *l,int n) 
  44. {
  45.     while(n--) 
  46. {
  47. l[0] = r[0];
  48. l[1] = g[0];
  49. l[2] = b[0];
  50. l[3] = 0xff;
  51. l += 4; r++; g++; b++;
  52.     }
  53. }
  54. void CMyTexture::rgbatorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *a,unsigned char *l,int n) 
  55. {
  56.     while(n--) 
  57. {
  58. l[0] = r[0];
  59. l[1] = g[0];
  60. l[2] = b[0];
  61. l[3] = a[0];
  62.         l += 4; r++; g++; b++; a++;
  63.     }
  64. }
  65. void CMyTexture::ConvertShort(unsigned short *array, long length) 
  66. {
  67.     unsigned b1, b2;
  68.     unsigned char *ptr;
  69.     ptr = (unsigned char *)array;
  70.     while (length--) 
  71. {
  72. b1 = *ptr++;
  73. b2 = *ptr++;
  74. *array++ = (b1 << 8) | (b2);
  75.     }
  76. }
  77. void CMyTexture::ConvertLong(unsigned *array, long length) 
  78. {
  79.     unsigned b1, b2, b3, b4;
  80.     unsigned char *ptr;
  81.     ptr = (unsigned char *)array;
  82.     while (length--) {
  83. b1 = *ptr++;
  84. b2 = *ptr++;
  85. b3 = *ptr++;
  86. b4 = *ptr++;
  87. *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
  88.     }
  89. }
  90. ImageRec * CMyTexture::ImageOpen(const char *fileName)
  91. {
  92.     union {
  93. int testWord;
  94. char testByte[4];
  95.     } endianTest;
  96.     ImageRec *image;
  97.     int swapFlag;
  98.     int x;
  99.     endianTest.testWord = 1;
  100.     if (endianTest.testByte[0] == 1) {
  101. swapFlag = 1;
  102.     } else {
  103. swapFlag = 0;
  104.     }
  105.     image = (ImageRec *)malloc(sizeof(ImageRec));
  106.     if (image == NULL) {
  107. fprintf(stderr, "Out of memory!n");
  108. exit(1);
  109.     }
  110.     if ((image->file = fopen(fileName, "rb")) == NULL) {
  111. perror(fileName);
  112. exit(1);
  113.     }
  114.     fread(image, 1, 12, image->file);
  115.     if (swapFlag) {
  116. ConvertShort(&image->imagic, 6);
  117.     }
  118.     image->tmp = (unsigned char *)malloc(image->xsize*256);
  119.     image->tmpR = (unsigned char *)malloc(image->xsize*256);
  120.     image->tmpG = (unsigned char *)malloc(image->xsize*256);
  121.     image->tmpB = (unsigned char *)malloc(image->xsize*256);
  122.     if (image->tmp == NULL || image->tmpR == NULL || image->tmpG == NULL ||
  123. image->tmpB == NULL) {
  124. fprintf(stderr, "Out of memory!n");
  125. exit(1);
  126.     }
  127.     if ((image->type & 0xFF00) == 0x0100) {
  128. x = image->ysize * image->zsize * sizeof(unsigned);
  129. image->rowStart = (unsigned *)malloc(x);
  130. image->rowSize = (int *)malloc(x);
  131. if (image->rowStart == NULL || image->rowSize == NULL) {
  132.     fprintf(stderr, "Out of memory!n");
  133.     exit(1);
  134. }
  135. image->rleEnd = 512 + (2 * x);
  136. fseek(image->file, 512, SEEK_SET);
  137. fread(image->rowStart, 1, x, image->file);
  138. fread(image->rowSize, 1, x, image->file);
  139. if (swapFlag) {
  140.     ConvertLong(image->rowStart, x/(int)sizeof(unsigned));
  141.     ConvertLong((unsigned *)image->rowSize, x/(int)sizeof(int));
  142. }
  143.     } else {
  144. image->rowStart = NULL;
  145. image->rowSize = NULL;
  146.     }
  147.     return image;
  148. }
  149. void CMyTexture::ImageClose(ImageRec *image) 
  150. {
  151.     fclose(image->file);
  152.     free(image->tmp);
  153.     free(image->tmpR);
  154.     free(image->tmpG);
  155.     free(image->tmpB);
  156.     free(image->rowSize);
  157.     free(image->rowStart);
  158.     free(image);
  159. }
  160. void CMyTexture::ImageGetRow(ImageRec *image, unsigned char *buf, int y, int z) 
  161. {
  162.     unsigned char *iPtr, *oPtr, pixel;
  163.     int count;
  164.     if ((image->type & 0xFF00) == 0x0100) {
  165. fseek(image->file, (long)image->rowStart[y+z*image->ysize], SEEK_SET);
  166. fread(image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],
  167.       image->file);
  168. iPtr = image->tmp;
  169. oPtr = buf;
  170. for (;;) {
  171.     pixel = *iPtr++;
  172.     count = (int)(pixel & 0x7F);
  173.     if (!count) {
  174. return;
  175.     }
  176.     if (pixel & 0x80) {
  177. while (count--) {
  178.     *oPtr++ = *iPtr++;
  179. }
  180.     } else {
  181. pixel = *iPtr++;
  182. while (count--) {
  183.     *oPtr++ = pixel;
  184. }
  185.     }
  186. }
  187.     } else {
  188. fseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
  189.       SEEK_SET);
  190. fread(buf, 1, image->xsize, image->file);
  191.     }
  192. }
  193. unsigned * CMyTexture::read_texture(char *name, int *width, int *height, int *components) 
  194. {
  195.     unsigned *base, *lptr;
  196.     unsigned char *rbuf, *gbuf, *bbuf, *abuf;
  197.     ImageRec *image;
  198.     int y;
  199.     image = ImageOpen(name);
  200.     
  201.     if(!image)
  202. return NULL;
  203.     (*width)=image->xsize;
  204.     (*height)=image->ysize;
  205.     (*components)=image->zsize;
  206.     base = (unsigned *)malloc(image->xsize*image->ysize*sizeof(unsigned));
  207.     rbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
  208.     gbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
  209.     bbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
  210.     abuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
  211.     if(!base || !rbuf || !gbuf || !bbuf)
  212.       return NULL;
  213.     lptr = base;
  214.     for(y=0; y<image->ysize; y++) {
  215. if(image->zsize>=4) {
  216.     ImageGetRow(image,rbuf,y,0);
  217.     ImageGetRow(image,gbuf,y,1);
  218.     ImageGetRow(image,bbuf,y,2);
  219.     ImageGetRow(image,abuf,y,3);
  220.     rgbatorgba(rbuf,gbuf,bbuf,abuf,(unsigned char *)lptr,image->xsize);
  221.     lptr += image->xsize;
  222. } else if(image->zsize==3) {
  223.     ImageGetRow(image,rbuf,y,0);
  224.     ImageGetRow(image,gbuf,y,1);
  225.     ImageGetRow(image,bbuf,y,2);
  226.     rgbtorgba(rbuf,gbuf,bbuf,(unsigned char *)lptr,image->xsize);
  227.     lptr += image->xsize;
  228. } else if(image->zsize==2) {
  229.     ImageGetRow(image,rbuf,y,0);
  230.     ImageGetRow(image,abuf,y,1);
  231.     latorgba(rbuf,abuf,(unsigned char *)lptr,image->xsize);
  232.     lptr += image->xsize;
  233. } else {
  234.     ImageGetRow(image,rbuf,y,0);
  235.     bwtorgba(rbuf,(unsigned char *)lptr,image->xsize);
  236.     lptr += image->xsize;
  237. }
  238.     }
  239.     ImageClose(image);
  240.     free(rbuf);
  241.     free(gbuf);
  242.     free(bbuf);
  243.     free(abuf);
  244.     return (unsigned *) base;
  245. }