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 File Reference
#include <stdlib.h>
#include "gldrv/sdds.h"

Go to the source code of this file.

Macros

#define GETL16(buf)   ( ( (unsigned short) (buf)[0] )|( (unsigned short) (buf)[1]<<8 ) )
 
#define GETL64(buf)
 

Functions

void decode_color_block (unsigned char *dst, unsigned char *src, int w, int h, int rowbytes, TEXTUREFORMAT format)
 
void decode_dxt3_alpha (unsigned char *dst, unsigned char *src, int w, int h, int rowbytes)
 
void decode_dxt5_alpha (unsigned char *dst, unsigned char *src, int w, int h, int bpp, int rowbytes)
 
void ddsDecompress (unsigned char *&buffer, unsigned char *&data, TEXTUREFORMAT internformat, int height, int width)
 

Macro Definition Documentation

#define GETL16 (   buf)    ( ( (unsigned short) (buf)[0] )|( (unsigned short) (buf)[1]<<8 ) )

Definition at line 5 of file sdds.cpp.

Referenced by decode_color_block(), and decode_dxt3_alpha().

#define GETL64 (   buf)
Value:
( ( (unsigned int) (buf)[0] ) \
|( (unsigned long long) (buf)[1] \
<<8 ) \
|( (unsigned long long) (buf)[2] \
<<16 ) \
|( (unsigned long long) (buf)[3] \
<<24 ) \
|( (unsigned long long) (buf)[4] \
<<32 ) \
|( (unsigned long long) (buf)[5] \
<<40 )|( (unsigned long long) (buf)[6]<<48 )|( (unsigned long long) (buf)[7]<<56 ) )

Definition at line 8 of file sdds.cpp.

Referenced by decode_dxt5_alpha().

Function Documentation

void ddsDecompress ( unsigned char *&  buffer,
unsigned char *&  data,
TEXTUREFORMAT  internformat,
int  height,
int  width 
)

Definition at line 111 of file sdds.cpp.

References buffer, decode_color_block(), decode_dxt3_alpha(), decode_dxt5_alpha(), DXT3, DXT5, height, width, x, and y.

Referenced by GFXTransferTexture(), and LoadTex().

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 }
void decode_color_block ( unsigned char *  dst,
unsigned char *  src,
int  w,
int  h,
int  rowbytes,
TEXTUREFORMAT  format 
)

Definition at line 24 of file sdds.cpp.

References colors, d, DXT1, DXT1RGBA, DXT5, GETL16, i, x, and y.

Referenced by ddsDecompress().

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 }
void decode_dxt3_alpha ( unsigned char *  dst,
unsigned char *  src,
int  w,
int  h,
int  rowbytes 
)

Definition at line 67 of file sdds.cpp.

References bits, d, GETL16, x, and y.

Referenced by ddsDecompress().

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 }
void decode_dxt5_alpha ( unsigned char *  dst,
unsigned char *  src,
int  w,
int  h,
int  bpp,
int  rowbytes 
)

Definition at line 84 of file sdds.cpp.

References a1, bits, d, GETL64, int, x, and y.

Referenced by ddsDecompress().

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 }