1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21// LOVE
22#include "ASTCHandler.h"
23#include "common/int.h"
24#include "common/Exception.h"
25
26namespace love
27{
28namespace image
29{
30namespace magpie
31{
32
33namespace
34{
35
36static const uint32 ASTC_IDENTIFIER = 0x5CA1AB13;
37
38#pragma pack(push, 1)
39struct ASTCHeader
40{
41 uint8 identifier[4];
42 uint8 blockdimX;
43 uint8 blockdimY;
44 uint8 blockdimZ;
45 uint8 sizeX[3];
46 uint8 sizeY[3];
47 uint8 sizeZ[3];
48};
49#pragma pack(pop)
50
51static PixelFormat convertFormat(uint32 blockX, uint32 blockY, uint32 blockZ)
52{
53 if (blockZ > 1)
54 return PIXELFORMAT_UNKNOWN;
55
56 if (blockX == 4 && blockY == 4)
57 return PIXELFORMAT_ASTC_4x4;
58 else if (blockX == 5 && blockY == 4)
59 return PIXELFORMAT_ASTC_5x4;
60 else if (blockX == 5 && blockY == 5)
61 return PIXELFORMAT_ASTC_5x5;
62 else if (blockX == 6 && blockY == 5)
63 return PIXELFORMAT_ASTC_6x5;
64 else if (blockX == 6 && blockY == 6)
65 return PIXELFORMAT_ASTC_6x6;
66 else if (blockX == 8 && blockY == 5)
67 return PIXELFORMAT_ASTC_8x5;
68 else if (blockX == 8 && blockY == 6)
69 return PIXELFORMAT_ASTC_8x6;
70 else if (blockX == 8 && blockY == 8)
71 return PIXELFORMAT_ASTC_8x8;
72 else if (blockX == 10 && blockY == 5)
73 return PIXELFORMAT_ASTC_10x5;
74 else if (blockX == 10 && blockY == 6)
75 return PIXELFORMAT_ASTC_10x6;
76 else if (blockX == 10 && blockY == 8)
77 return PIXELFORMAT_ASTC_10x8;
78 else if (blockX == 10 && blockY == 10)
79 return PIXELFORMAT_ASTC_10x10;
80 else if (blockX == 12 && blockY == 10)
81 return PIXELFORMAT_ASTC_12x10;
82 else if (blockX == 12 && blockY == 12)
83 return PIXELFORMAT_ASTC_12x12;
84
85 return PIXELFORMAT_UNKNOWN;
86}
87
88} // Anonymous namespace.
89
90bool ASTCHandler::canParseCompressed(Data *data)
91{
92 if (data->getSize() <= sizeof(ASTCHeader))
93 return false;
94
95 const ASTCHeader *header = (const ASTCHeader *) data->getData();
96
97 uint32 identifier = (uint32) header->identifier[0]
98 + ((uint32) header->identifier[1] << 8)
99 + ((uint32) header->identifier[2] << 16)
100 + ((uint32) header->identifier[3] << 24);
101
102 if (identifier != ASTC_IDENTIFIER)
103 return false;
104
105 return true;
106}
107
108StrongRef<CompressedMemory> ASTCHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
109{
110 if (!canParseCompressed(filedata))
111 throw love::Exception("Could not decode compressed data (not an .astc file?)");
112
113 ASTCHeader header = *(const ASTCHeader *) filedata->getData();
114
115 PixelFormat cformat = convertFormat(header.blockdimX, header.blockdimY, header.blockdimZ);
116
117 if (cformat == PIXELFORMAT_UNKNOWN)
118 throw love::Exception("Could not parse .astc file: unsupported ASTC format %dx%dx%d.", header.blockdimX, header.blockdimY, header.blockdimZ);
119
120 uint32 sizeX = header.sizeX[0] + (header.sizeX[1] << 8) + (header.sizeX[2] << 16);
121 uint32 sizeY = header.sizeY[0] + (header.sizeY[1] << 8) + (header.sizeY[2] << 16);
122 uint32 sizeZ = header.sizeZ[0] + (header.sizeZ[1] << 8) + (header.sizeZ[2] << 16);
123
124 uint32 blocksX = (sizeX + header.blockdimX - 1) / header.blockdimX;
125 uint32 blocksY = (sizeY + header.blockdimY - 1) / header.blockdimY;
126 uint32 blocksZ = (sizeZ + header.blockdimZ - 1) / header.blockdimZ;
127
128 size_t totalsize = blocksX * blocksY * blocksZ * 16;
129
130 if (totalsize + sizeof(header) > filedata->getSize())
131 throw love::Exception("Could not parse .astc file: file is too small.");
132
133 StrongRef<CompressedMemory> memory(new CompressedMemory(totalsize), Acquire::NORETAIN);
134
135 // .astc files only store a single mipmap level.
136 memcpy(memory->data, (uint8 *) filedata->getData() + sizeof(ASTCHeader), totalsize);
137
138 images.emplace_back(new CompressedSlice(cformat, sizeX, sizeY, memory, 0, totalsize), Acquire::NORETAIN);
139
140 format = cformat;
141 sRGB = false;
142
143 return memory;
144}
145
146} // magpie
147} // image
148} // love
149
150