Skip to content

Commit

Permalink
Add more metadata methods to DDSHeader
Browse files Browse the repository at this point in the history
New methods getCompressionFormat() and getRGBBits(), which are used by the
debug operator<< but not in any calling code so far. Also removed some unused
byte-swapping functions in ddslib.cpp, and simplified the naming of the ones
which are used.
  • Loading branch information
Matthew Mott committed Jul 8, 2020
1 parent 59126f9 commit 55d8e4f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 58 deletions.
77 changes: 19 additions & 58 deletions radiant/image/ddslib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,81 +59,42 @@ floatSwapUnion;

#ifdef __BIG_ENDIAN__

int DDSBigLong( int src ) {
return src;
}
short DDSBigShort( short src ) {
return src;
}
float DDSBigFloat( float src ) {
return src;
}

int DDSLittleLong( int src ) {
int DDSLong( int src ) {
return ((src & 0xFF000000) >> 24) |
((src & 0x00FF0000) >> 8) |
((src & 0x0000FF00) << 8) |
((src & 0x000000FF) << 24);
}

short DDSLittleShort( short src ) {
short DDSShort( short src ) {
return ((src & 0xFF00) >> 8) |
((src & 0x00FF) << 8);
}

float DDSLittleFloat( float src ) {
floatSwapUnion in,out;
in.f = src;
out.c[ 0 ] = in.c[ 3 ];
out.c[ 1 ] = in.c[ 2 ];
out.c[ 2 ] = in.c[ 1 ];
out.c[ 3 ] = in.c[ 0 ];
return out.f;
}

#else /*__BIG_ENDIAN__*/

int DDSLittleLong( int src ) {
return src;
}
short DDSLittleShort( short src ) {
int DDSLong( int src ) {
return src;
}
float DDSLittleFloat( float src ) {
short DDSShort( short src ) {
return src;
}

int DDSBigLong( int src ) {
return ((src & 0xFF000000) >> 24) |
((src & 0x00FF0000) >> 8) |
((src & 0x0000FF00) << 8) |
((src & 0x000000FF) << 24);
}

short DDSBigShort( short src ) {
return ((src & 0xFF00) >> 8) |
((src & 0x00FF) << 8);
}

float DDSBigFloat( float src ) {
floatSwapUnion in,out;
in.f = src;
out.c[ 0 ] = in.c[ 3 ];
out.c[ 1 ] = in.c[ 2 ];
out.c[ 2 ] = in.c[ 1 ];
out.c[ 3 ] = in.c[ 0 ];
return out.f;
}

#endif /*__BIG_ENDIAN__*/

std::ostream& operator<< (std::ostream& os, const DDSHeader& h)
{
os << "DDSHeader { " << (h.isValid() ? "VALID" : "INVALID")
<< " | " << h.width << "x" << h.height
<< " | " << (h.isCompressed() ? "Compressed" : "Uncompressed")
<< " | " << h.mipMapCount << " mipmaps"
<< " | " << h.width << "x" << h.height;

if (h.isCompressed())
os << " | " << h.getCompressionFormat();
else
os << " | " << h.getRGBBits() << " bit RGB";

os << " | " << h.mipMapCount << " mipmaps"
<< " }";

return os;
}

Expand Down Expand Up @@ -189,14 +150,14 @@ int DDSGetInfo(const DDSHeader* header, int *width, int *height, ddsPF_t *pf ) {
/* test dds header */
if( *((int*) header->magic) != *((int*) "DDS ") )
return -1;
if( DDSLittleLong( header->size ) != 124 )
if( DDSLong( header->size ) != 124 )
return -1;

/* extract width and height */
if( width != NULL )
*width = DDSLittleLong( header->width );
*width = DDSLong( header->width );
if( height != NULL )
*height = DDSLittleLong( header->height );
*height = DDSLong( header->height );

/* get pixel format */
DDSDecodePixelFormat( header, pf );
Expand All @@ -215,7 +176,7 @@ static void DDSGetColorBlockColors( ddsColorBlock_t *block, ddsColor_t colors[ 4


/* color 0 */
word = DDSLittleShort( block->colors[ 0 ] );
word = DDSShort( block->colors[ 0 ] );
colors[ 0 ].a = 0xff;

/* extract rgb bits */
Expand All @@ -232,7 +193,7 @@ static void DDSGetColorBlockColors( ddsColorBlock_t *block, ddsColor_t colors[ 4
colors[ 0 ].r |= (colors[ 0 ].r >> 5);

/* same for color 1 */
word = DDSLittleShort( block->colors[ 1 ] );
word = DDSShort( block->colors[ 1 ] );
colors[ 1 ].a = 0xff;

/* extract rgb bits */
Expand Down Expand Up @@ -345,7 +306,7 @@ static void DDSDecodeAlphaExplicit( unsigned int *pixel, ddsAlphaBlockExplicit_t

/* walk rows */
for( row = 0; row < 4; row++, pixel += (width - 4) ) {
word = DDSLittleShort( alphaBlock->row[ row ] );
word = DDSShort( alphaBlock->row[ row ] );

/* walk pixels */
for( pix = 0; pix < 4; pix++ ) {
Expand Down
23 changes: 23 additions & 0 deletions radiant/image/ddslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,29 @@ struct DDSHeader
{
return pixelFormat.flags & DDPF_FOURCC;
}

/// Return the FOURCC compression format, if any
std::string getCompressionFormat() const
{
if (isCompressed())
// Return FOURCC as a string
return std::string(
reinterpret_cast<const char*>(&pixelFormat.fourCC[0]),
4
);
else
// Uncompressed, return nothing
return "";
}

/// Return number of RGB bits (uncompressed) or 0 (compressed)
int getRGBBits() const
{
if (!isCompressed() && (pixelFormat.flags & DDPF_RGB))
return pixelFormat.rgbBitCount;
else
return 0;
}
};

// Debug output for DDSHeader
Expand Down

0 comments on commit 55d8e4f

Please sign in to comment.