Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sdds.cpp
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include "gldrv/sdds.h"
3 
4 #ifndef GETL16
5 #define GETL16( buf ) ( ( (unsigned short) (buf)[0] )|( (unsigned short) (buf)[1]<<8 ) )
6 #endif
7 #ifndef GETL64
8 #define GETL64( buf ) \
9  ( ( (unsigned int) (buf)[0] ) \
10  |( (unsigned long long) (buf)[1] \
11  <<8 ) \
12  |( (unsigned long long) (buf)[2] \
13  <<16 ) \
14  |( (unsigned long long) (buf)[3] \
15  <<24 ) \
16  |( (unsigned long long) (buf)[4] \
17  <<32 ) \
18  |( (unsigned long long) (buf)[5] \
19  <<40 )|( (unsigned long long) (buf)[6]<<48 )|( (unsigned long long) (buf)[7]<<56 ) )
20 #endif
21 
22 /* Software decompression for DDS files, helper functions */
23 
24 void decode_color_block( unsigned char *dst, unsigned char *src, int w, int h, int rowbytes, TEXTUREFORMAT format )
25 {
26  int i, x, y;
27  unsigned int indexes, idx;
28  unsigned char *d;
29  unsigned char colors[4][3];
30  unsigned short c0, c1;
31  c0 = GETL16( &src[0] );
32  c1 = GETL16( &src[2] );
33  colors[0][0] = ( (c0>>11)&0x1f )<<3;
34  colors[0][1] = ( (c0>>5)&0x3f )<<2;
35  colors[0][2] = ( (c0)&0x1f )<<3;
36  colors[1][0] = ( (c1>>11)&0x1f )<<3;
37  colors[1][1] = ( (c1>>5)&0x3f )<<2;
38  colors[1][2] = ( (c1)&0x1f )<<3;
39  if ( (c0 > c1) || (format == DXT5) ) {
40  for (i = 0; i < 3; ++i) {
41  colors[2][i] = (2*colors[0][i]+colors[1][i]+1)/3;
42  colors[3][i] = (2*colors[1][i]+colors[0][i]+1)/3;
43  }
44  } else {
45  for (i = 0; i < 3; ++i) {
46  colors[2][i] = (colors[0][i]+colors[1][i]+1)>>1;
47  colors[3][i] = 255;
48  }
49  }
50  src += 4;
51  for (y = 0; y < h; ++y) {
52  d = dst+(y*rowbytes);
53  indexes = src[y];
54  for (x = 0; x < w; ++x) {
55  idx = indexes&0x03;
56  d[0] = colors[idx][0];
57  d[1] = colors[idx][1];
58  d[2] = colors[idx][2];
59  if (format == DXT1 || format == DXT1RGBA)
60  d[3] = ( (c0 <= c1) && idx == 3 ) ? 0 : 255;
61  indexes >>= 2;
62  d += 4;
63  }
64  }
65 }
66 
67 void decode_dxt3_alpha( unsigned char *dst, unsigned char *src, int w, int h, int rowbytes )
68 {
69  int x, y;
70  unsigned char *d;
71  unsigned int bits;
72  for (y = 0; y < h; ++y) {
73  d = dst+(y*rowbytes);
74  bits = GETL16( &src[2*y] );
75  bits = GETL16( &src[2*y] );
76  for (x = 0; x < w; ++x) {
77  d[0] = (bits&0x0f)*17;
78  bits >>= 4;
79  d += 4;
80  }
81  }
82 }
83 
84 void decode_dxt5_alpha( unsigned char *dst, unsigned char *src, int w, int h, int bpp, int rowbytes )
85 {
86  int x, y, code;
87  unsigned char *d, a0 = src[0], a1 = src[1];
88  unsigned long long bits = GETL64( src )>>16;
89  for (y = 0; y < h; ++y) {
90  d = dst+(y*rowbytes);
91  for (x = 0; x < w; ++x) {
92  code = ( (unsigned int) bits )&0x07;
93  if (code == 0)
94  d[0] = a0;
95  else if (code == 1)
96  d[0] = a1;
97  else if (a0 > a1)
98  d[0] = ( (8-code)*a0+(code-1)*a1 )/7;
99  else if (code >= 6)
100  d[0] = (code == 6) ? 0 : 255;
101  else
102  d[0] = ( (6-code)*a0+(code-1)*a1 )/5;
103  bits >>= 3;
104  d += bpp;
105  }
106  if (w < 4)
107  bits >>= ( 3*(4-w) );
108  }
109 }
110 
111 void ddsDecompress( unsigned char* &buffer, unsigned char* &data, TEXTUREFORMAT internformat, int height, int width )
112 {
113  unsigned char *pos_out = NULL, *pos_in = NULL;
114  int bpp = 4;
115  unsigned int sx, sy;
116 
117  sx = (width < 4) ? width : 4;
118  sy = (height < 4) ? width : 4;
119  data = (unsigned char*) malloc( height*width*bpp );
120  pos_out = data;
121  pos_in = buffer;
122  for (int y = 0; y < height; y += 4)
123  for (int x = 0; x < width; x += 4) {
124  pos_out = data+(y*width+x)*bpp;
125  if (internformat == DXT3) {
126  decode_dxt3_alpha( pos_out+3, pos_in, sx, sy, width*bpp );
127  pos_in += 8;
128  } else if (internformat == DXT5) {
129  decode_dxt5_alpha( pos_out+3, pos_in, sx, sy, bpp, width*bpp );
130  pos_in += 8;
131  }
132  decode_color_block( pos_out, pos_in, sx, sy, width*bpp, internformat );
133  pos_in += 8;
134  }
135 }
136 
137 /* END of software decompression for DDS helper functions */
138