home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / graphic / csg_rt / tex.c < prev    next >
C/C++ Source or Header  |  1993-01-12  |  4KB  |  189 lines

  1. /*
  2.  
  3. TEX.C  Handle 3D texture map data
  4.  
  5. */
  6.  
  7. /*...sincludes:0:*/
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <stdlib.h>
  11. #include <stddef.h>
  12. #include <stdarg.h>
  13. #include <string.h>
  14. #include <memory.h>
  15. #include <malloc.h>
  16. #ifdef AIX
  17. #include <unistd.h>
  18. #else
  19. #include <io.h>
  20. #endif
  21. #include <fcntl.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include "standard.h"
  25.  
  26. #ifndef O_BINARY
  27. #define    O_BINARY 0
  28. #endif
  29. /*...e*/
  30.  
  31. typedef struct { byte b, g, r, dummy; } PAL;
  32.  
  33. typedef struct
  34.     {
  35.     dword w, h, d, bpv, stride;
  36.     PAL pals [0x100];
  37.     byte *data;
  38.     int ref_count;
  39.     } TEX;
  40.  
  41. #define    TEX_MAGIC    ((dword) 0x1a584554)
  42.  
  43. /*...sread_tex:0:*/
  44. /*...sread_dword:0:*/
  45. static dword read_dword(int fd)
  46.     {
  47.     byte b [4];
  48.  
  49.     read(fd, b, 4);
  50.     return (  (dword) b [0]        +
  51.          ((dword) b [1] <<  8) +
  52.          ((dword) b [2] << 16) +
  53.          ((dword) b [3] << 24) );
  54.     }
  55. /*...e*/
  56.  
  57. TEX *read_tex(char *fn)
  58.     {
  59.     TEX *tex;
  60.     int fd, i;
  61.     dword bytes;
  62.  
  63.     if ( (fd = open(fn, O_RDONLY | O_BINARY)) == -1 )
  64.         return ( NULL );
  65.  
  66.     if ( (tex = malloc(sizeof(TEX))) == NULL )
  67.         { close(fd); return ( NULL ); }
  68.  
  69.     if ( read_dword(fd) != TEX_MAGIC )
  70.         { free(tex); close(fd); return ( NULL ); }
  71.  
  72.     tex -> w   = read_dword(fd);
  73.     tex -> h   = read_dword(fd);
  74.     tex -> d   = read_dword(fd);
  75.     tex -> bpv = read_dword(fd);
  76.  
  77.     if ( tex -> bpv != 1 && tex -> bpv != 4 && tex -> bpv != 8 && tex -> bpv != 24 )
  78.         { free(tex); close(fd); return ( NULL ); }
  79.  
  80.     for ( i = 0; i < ((1 << tex -> bpv) & 0x1ff); i++ )
  81.         if ( read(fd, (char *) &(tex -> pals [i]), sizeof(PAL)) != sizeof(PAL) )
  82.             { free(tex); close(fd); return ( NULL ); }
  83.  
  84.     tex -> stride = ((tex -> w * tex -> bpv + 31)/32)*4;
  85.     bytes = tex -> stride * tex -> h * tex -> d;
  86.  
  87.     if ( (tex -> data = malloc((size_t) bytes)) == NULL )
  88.         { free(tex); close(fd); return ( NULL ); }
  89.  
  90.     if ( read(fd, tex -> data, (unsigned int) bytes) != (int) bytes )
  91.         { free(tex -> data); free(tex); close(fd); return ( NULL ); }
  92.  
  93.     close(fd);
  94.  
  95.     tex -> ref_count = 1;
  96.  
  97.     return ( tex );
  98.     }
  99. /*...e*/
  100. /*...scopy_tex:0:*/
  101. TEX *copy_tex(TEX *tex)
  102.     {
  103.     (tex -> ref_count)++;
  104.     return ( tex );
  105.     }
  106. /*...e*/
  107. /*...sdestroy_tex:0:*/
  108. void destroy_tex(TEX *tex)
  109.     {
  110.     if ( --(tex -> ref_count) == 0 )
  111.         {
  112.         free(tex -> data);
  113.         free(tex);
  114.         }
  115.     }
  116. /*...e*/
  117.  
  118. /*...sget_voxel_tex:0:*/
  119. void get_voxel_tex(
  120.     TEX *tex,
  121.     int x, int y, int z,
  122.     byte *r, byte *g, byte *b
  123.     )
  124.     {
  125.     byte inx, *data = tex -> data + (z * tex -> h + y) * tex -> stride;
  126.  
  127.     switch ( tex -> bpv )
  128.         {
  129. /*...s1:16:*/
  130. case 1:
  131.     inx = data [x >> 3];
  132.     inx >>= ( 7 - (x & 7) );
  133.     inx &= 1;    
  134.     inx ^= 1;            /* B/W reverse palette fix */
  135.     *b = tex -> pals [inx].b;
  136.     *g = tex -> pals [inx].g;
  137.     *r = tex -> pals [inx].r;
  138.     break;
  139. /*...e*/
  140. /*...s4:16:*/
  141. case 4:
  142.     inx = data [x >> 1];
  143.     if ( x & 1 )
  144.         inx &= 0x0f;
  145.     else
  146.         inx >>= 4;
  147.     *b = tex -> pals [inx].b;
  148.     *g = tex -> pals [inx].g;
  149.     *r = tex -> pals [inx].r;
  150.     break;
  151. /*...e*/
  152. /*...s8:16:*/
  153. case 8:
  154.     inx = data [x];
  155.     *b = tex -> pals [inx].b;
  156.     *g = tex -> pals [inx].g;
  157.     *r = tex -> pals [inx].r;
  158.     break;
  159. /*...e*/
  160. /*...s24:16:*/
  161. case 24:
  162.     data += (x * 3);
  163.     *b = *data++;
  164.     *g = *data++;
  165.     *r = *data;
  166.     break;
  167. /*...e*/
  168.         }
  169.     }
  170. /*...e*/
  171. /*...swidth_tex:0:*/
  172. int width_tex(TEX *tex)
  173.     {
  174.     return ( (int) tex -> w );
  175.     }
  176. /*...e*/
  177. /*...sheight_tex:0:*/
  178. int height_tex(TEX *tex)
  179.     {
  180.     return ( (int) tex -> h );
  181.     }
  182. /*...e*/
  183. /*...sdepth_tex:0:*/
  184. int depth_tex(TEX *tex)
  185.     {
  186.     return ( (int) tex -> d );
  187.     }
  188. /*...e*/
  189.