Skip to content

Commit

Permalink
GRAPHICS: Add ImageDecoder::isTransparent()
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Feb 7, 2014
1 parent e980fa7 commit 69bffbd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/graphics/images/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,49 @@ bool ImageDecoder::hasAlpha() const {
return hasAlpha(_format);
}

bool ImageDecoder::isTransparent() const {
if (_mipMaps.empty())
return false;

const MipMap &mipMap = *_mipMaps[0];

const int pixels = mipMap.width * mipMap.height;
const byte *data = mipMap.data;

switch (_format) {
case kPixelFormatR8G8B8A8:
for (int i = 0; i < pixels; i++, data += 4)
if (data[3] != 0xFF)
return true;
break;

case kPixelFormatB8G8R8A8:
for (int i = 0; i < pixels; i++, data += 4)
if (data[3] != 0xFF)
return true;
break;

case kPixelFormatA1R5G5B5:
for (int i = 0; i < pixels; i++, data += 2)
if (!(data[0] & 0x80))
return true;
break;

case kPixelFormatDXT3:
return isTransparentDXT3(mipMap.data, mipMap.size);
break;

case kPixelFormatDXT5:
return isTransparentDXT5(mipMap.data, mipMap.size);
break;

default:
break;
}

return false;
}

PixelFormat ImageDecoder::getFormat() const {
return _format;
}
Expand Down
5 changes: 4 additions & 1 deletion src/graphics/images/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ class ImageDecoder {
/** Is the image data compressed? */
bool isCompressed() const;

/** Does the image data have alpha? .*/
/** Does the image data have an alpha channel? */
bool hasAlpha() const;

/** Does the image have transparent alpha values? */
bool isTransparent() const;

/** Return the image data's format. */
PixelFormat getFormat() const;

Expand Down
30 changes: 30 additions & 0 deletions src/graphics/images/s3tc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,34 @@ void decompressDXT5(byte *dest, Common::SeekableReadStream &src, uint32 width, u
}
}

bool isTransparentDXT3(const byte *data, uint32 size) {
for ( ; size > 15; size -= 16, data += 16)
if ((data[0] & data[1] & data[2] & data[3] & data[4] & data[5] & data[6] & data[7]) != 0xFF)
return true;

return false;
}

bool isTransparentDXT5(const byte *data, uint32 size) {
for ( ; size > 15; size -= 16, data += 16) {
const byte a0 = data[0];
const byte a1 = data[1];

uint64 abl = READ_LE_UINT32(data + 2) | ((uint64)READ_LE_UINT16(data + 6) << 32);

for (int i = 0; i < 16; i++, abl >>= 3) {
const uint8 a = abl & 7;

const bool o1 = ((a == 0) && (a0 == 0xFF)) || ((a == 1) && (a1 == 0xFF));
const bool o2 = (a0 == a1) && (a0 == 0xFF) && (a != 6);
const bool o3 = (a0 < a1) && (a == 7);

if (!(o1 || o2 || o3))
return true;
}
}

return false;
}

} // End of namespace Graphics
3 changes: 3 additions & 0 deletions src/graphics/images/s3tc.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ void decompressDXT1(byte *dest, Common::SeekableReadStream &src, uint32 width, u
void decompressDXT3(byte *dest, Common::SeekableReadStream &src, uint32 width, uint32 height, uint32 pitch);
void decompressDXT5(byte *dest, Common::SeekableReadStream &src, uint32 width, uint32 height, uint32 pitch);

bool isTransparentDXT3(const byte *data, uint32 size);
bool isTransparentDXT5(const byte *data, uint32 size);

} // End of namespace Graphics

#endif // GRAPHICS_IMAGES_S3TC_H

0 comments on commit 69bffbd

Please sign in to comment.