TDME2  1.9.200
BC7TextureReader.cpp
Go to the documentation of this file.
2 
3 #include <array>
4 #include <string>
5 
6 #include <tdme/tdme.h>
8 
9 #include <ext/bc7enc_rdo/bc7decomp.h>
10 
11 using std::array;
12 using std::string;
13 
15 
17 
18 bool BC7TextureReader::read(int width, int height, int bytesPerPixel, const vector<uint8_t>& bc7Data, ByteBuffer& textureByteBuffer) {
19  //
20  auto xBlocks = static_cast<int>(Math::ceil(width / 4.0f));
21  auto yBlocks = static_cast<int>(Math::ceil(height / 4.0f));
22 
23  //
24  auto textureByteBufferBuffer = textureByteBuffer.getBuffer();
25 
26  //
27  for (auto yBlock = 0; yBlock < yBlocks; yBlock++) {
28  for (auto xBlock = 0; xBlock < xBlocks; xBlock++) {
29  //
30  array<bc7decomp::color_rgba, 4 * 4> rgbaBlockPixels { 0 };
31  bc7decomp::unpack_bc7(&bc7Data.data()[yBlock * xBlocks * 16 + xBlock * 16], rgbaBlockPixels.data());
32  //
33  auto blockPixelIdx = 0;
34  auto xBlockOffset = xBlock * 4;
35  auto yBlockOffset = yBlock * 4;
36  for (int y = 0; y < 4; y++) {
37  for (int x = 0; x < 4; x++) {
38  auto offset = (yBlockOffset + y) * width * bytesPerPixel + (xBlockOffset + x) * bytesPerPixel;
39  auto red = rgbaBlockPixels[blockPixelIdx].r;
40  auto green = rgbaBlockPixels[blockPixelIdx].g;
41  auto blue = rgbaBlockPixels[blockPixelIdx].b;
42  auto alpha = rgbaBlockPixels[blockPixelIdx].a;
43  if (offset < textureByteBuffer.getCapacity()) {
44  textureByteBufferBuffer[offset + 0] = red;
45  textureByteBufferBuffer[offset + 1] = green;
46  textureByteBufferBuffer[offset + 2] = blue;
47  if (bytesPerPixel == 4) textureByteBufferBuffer[offset + 3] = alpha;
48  }
49  blockPixelIdx++;
50  }
51  }
52  }
53  }
54  //
55  return false;
56 }
57 
const uint8_t * getBuffer() const
Definition: Buffer.h:150
virtual int64_t getCapacity() const
Definition: Buffer.h:82
Byte buffer class.
Definition: ByteBuffer.h:27