home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 36 / #36-1.iso / quake / Files / md2 / src / pcx.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-25  |  5.0 KB  |  230 lines

  1. /*****************************************************************
  2. Copyright (c) 1998 Jawed Karim <jkarim@students.uiuc.edu>
  3. All rights reserved.
  4.  
  5. This source code has been provided to you under the condition
  6. that you adhere to the following terms:
  7.  
  8. 1. YOU MAY USE THIS SOURCE CODE PRIVATELY WITHOUT RESTRICTIONS.
  9.  
  10. 2. REDISTRIBUTIONS OF MODIFICATIONS OF THIS PROGRAM IN BINARY OR
  11.    IN SOURCE CODE FORM ARE NOT PERMITTED.
  12.  
  13. 3. REDISTRIBUTIONS OF THIS SOURCE CODE ARE ONLY PERMITTED IF
  14.    THE SOURCE CODE REMAINS COMPLETELY UNCHANGED AND ALL THE
  15.    FILES WHICH WERE IN THE ORIGINAL DISTRIBUTION ARE INCLUDED.
  16.  
  17. 4. ALL SOFTWARE USING SECTIONS OF THIS SOURCE CODE MUST GIVE
  18.    EXPLICIT ACKNOWLEDGMENT TO JAWED KARIM IN THE PROGRAM
  19.    ITSELF AS WELL AS IN THE DOCUMENTATION.
  20.  
  21. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  22. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  25. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. INTERRUPTION).
  29. *****************************************************************/
  30.  
  31. #include <iostream.h>
  32. #include <windows.h>
  33. #include <commctrl.h>
  34. #include <windowsx.h>
  35. #include <gl/gl.h>
  36. #include <gl/glu.h>
  37.  
  38. #include "pcx.h"
  39.  
  40. int CImage::Read (char szfilename[])
  41. {
  42.     int w_count = 0;
  43.     int c, i;
  44.     int repeat;
  45.  
  46.     m_szCurrentFile = strdup (szfilename);
  47.  
  48.     FILE *pcx;
  49.  
  50.     if ((pcx = fopen(szfilename, "rb")) == NULL)
  51.     {
  52.         return -1;
  53.     }
  54.  
  55.     if ((c = getc(pcx)) != 10)
  56.     {
  57.         fclose(pcx);
  58.         return -1;
  59.     }
  60.  
  61.     if ((c = getc(pcx)) != 5)
  62.     {
  63.         fclose(pcx);
  64.         return -1;
  65.     }
  66.  
  67.     if ((ReadPCXDimensions (pcx)) == -1)
  68.         return -1;
  69.  
  70.     m_pixel_buffer = new unsigned char [(m_iWidth * m_iHeight)];
  71.  
  72.     fseek(pcx, 128, SEEK_SET);
  73.  
  74.     while (w_count < (m_iWidth * m_iHeight)) {
  75.  
  76.         c = getc(pcx);
  77.  
  78.         if (c > 0xbf)
  79.         {
  80.             repeat = 0x3f & c;
  81.  
  82.             c = getc(pcx);
  83.  
  84.             for (i = 0; i < repeat; i++)
  85.             {
  86.                 m_pixel_buffer[w_count++] = c;
  87.             }
  88.         }
  89.         else
  90.         {
  91.             m_pixel_buffer[w_count++] = c;
  92.         }
  93.  
  94.         fflush(stdout);
  95.     }
  96.  
  97.     m_palette_buffer = new unsigned char [768];
  98.  
  99.     fseek (pcx, -769, SEEK_END);
  100.  
  101.     if ((c = getc(pcx)) != 12)
  102.     {
  103.         fclose(pcx);
  104.         return -1;
  105.     }
  106.  
  107.     for (i = 0; i < 768; i++)
  108.     {
  109.         c = getc(pcx);
  110.         m_palette_buffer[i] = c;
  111.     }
  112.  
  113.     fclose (pcx);
  114.     return 0;
  115. }
  116.  
  117. int CImage::ReadPCXDimensions (FILE *fp)
  118. {
  119.     if (fp == NULL)
  120.         return -1;
  121.  
  122.     int sx, sy, ex, ey;
  123.  
  124.     rewind(fp);
  125.  
  126.     fgetc(fp);    fgetc(fp);    fgetc(fp);    fgetc(fp);
  127.  
  128.     sx = fgetc(fp);
  129.     sx |= fgetc(fp)<<8;
  130.  
  131.     sy = fgetc(fp);
  132.     sy |= fgetc(fp)<<8;
  133.  
  134.     ex = fgetc(fp);
  135.     ex |= fgetc(fp)<<8;
  136.  
  137.     ey = fgetc(fp);
  138.     ey |= fgetc(fp)<<8;
  139.  
  140.     m_iWidth    = ex - sx + 1;
  141.     m_iHeight    = ey - sy + 1;
  142.  
  143.     return 0;
  144. }
  145.  
  146. void CImage::Destroy (void)
  147. {
  148.     if (glTexture != NULL) {
  149.         delete [] glTexture;
  150.         glTexture = NULL;
  151.     }
  152.  
  153.     if (m_pixel_buffer != NULL)
  154.     {
  155.         delete [] m_pixel_buffer;
  156.         m_pixel_buffer = NULL;
  157.     }
  158.  
  159.     if (m_palette_buffer != NULL)
  160.     {
  161.         delete [] m_palette_buffer;
  162.         m_palette_buffer = NULL;
  163.     }
  164. }
  165.  
  166. void CImage::Image2GLTexture (void)
  167. {
  168.     GLubyte *unScaled;
  169.     int i, j;
  170.  
  171.     unScaled = new GLubyte [m_iWidth * m_iHeight * 4];
  172.  
  173.     for (j = 0; j < m_iHeight; j++) {
  174.         for (i = 0; i < m_iWidth; i++) {
  175.             unScaled[4*(j * m_iWidth + i)+0] = (GLubyte) m_palette_buffer[3*m_pixel_buffer[j*m_iWidth+i]+0];
  176.             unScaled[4*(j * m_iWidth + i)+1] = (GLubyte) m_palette_buffer[3*m_pixel_buffer[j*m_iWidth+i]+1];
  177.             unScaled[4*(j * m_iWidth + i)+2] = (GLubyte) m_palette_buffer[3*m_pixel_buffer[j*m_iWidth+i]+2];
  178.             unScaled[4*(j * m_iWidth + i)+3] = (GLubyte) 255;
  179.         }
  180.     }
  181.  
  182.     if (m_iWidth > 256)
  183.         m_iscaledWidth = 512;
  184.     else if (m_iWidth > 128)
  185.         m_iscaledWidth = 256;
  186.     else if (m_iWidth > 64)
  187.         m_iscaledWidth = 128;
  188.     else if (m_iWidth > 32)
  189.         m_iscaledWidth = 64;
  190.     else if (m_iWidth > 16)
  191.         m_iscaledWidth = 32;
  192.     else if (m_iWidth > 8)
  193.         m_iscaledWidth = 16;
  194.     else if (m_iWidth > 4)
  195.         m_iscaledWidth = 8;
  196.     else if (m_iWidth > 2)
  197.         m_iscaledWidth = 4;
  198.     else if (m_iWidth > 1)
  199.         m_iscaledWidth = 2;
  200.  
  201.     if (m_iHeight > 256)
  202.         m_iscaledHeight = 512;
  203.     else if (m_iHeight > 128)
  204.         m_iscaledHeight = 256;
  205.     else if (m_iHeight > 64)
  206.         m_iscaledHeight = 128;
  207.     else if (m_iHeight > 32)
  208.         m_iscaledHeight = 64;
  209.     else if (m_iHeight > 16)
  210.         m_iscaledHeight = 32;
  211.     else if (m_iHeight > 8)
  212.         m_iscaledHeight = 16;
  213.     else if (m_iHeight > 4)
  214.         m_iscaledHeight = 8;
  215.     else if (m_iHeight > 2)
  216.         m_iscaledHeight = 4;
  217.     else if (m_iHeight > 1)
  218.         m_iscaledHeight = 2;
  219.  
  220.     if (glTexture != NULL) {
  221.         delete [] glTexture;
  222.         glTexture = NULL;
  223.     }
  224.  
  225.     glTexture = new GLubyte [m_iscaledWidth * m_iscaledHeight * 4];
  226.  
  227.     gluScaleImage (GL_RGBA, m_iWidth, m_iHeight, GL_UNSIGNED_BYTE, unScaled, m_iscaledWidth, m_iscaledHeight, GL_UNSIGNED_BYTE, glTexture);
  228.  
  229.     delete [] unScaled;
  230. }