1 /**
2 * Copyright (c) 2006-2016 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
25 namespace love
26 {
27 namespace image
28 {
29 namespace magpie
30 {
31
32 namespace
33 {
34
35 static const uint32 ASTC_IDENTIFIER = 0x5CA1AB13;
36
37 #pragma pack(push, 1)
38 struct ASTCHeader
39 {
40 uint8 identifier[4];
41 uint8 blockdimX;
42 uint8 blockdimY;
43 uint8 blockdimZ;
44 uint8 sizeX[3];
45 uint8 sizeY[3];
46 uint8 sizeZ[3];
47 };
48 #pragma pack(pop)
49
convertFormat(uint32 blockX,uint32 blockY,uint32 blockZ)50 static CompressedImageData::Format convertFormat(uint32 blockX, uint32 blockY, uint32 blockZ)
51 {
52 if (blockZ > 1)
53 return CompressedImageData::FORMAT_UNKNOWN;
54
55 if (blockX == 4 && blockY == 4)
56 return CompressedImageData::FORMAT_ASTC_4x4;
57 else if (blockX == 5 && blockY == 4)
58 return CompressedImageData::FORMAT_ASTC_5x4;
59 else if (blockX == 5 && blockY == 5)
60 return CompressedImageData::FORMAT_ASTC_5x5;
61 else if (blockX == 6 && blockY == 5)
62 return CompressedImageData::FORMAT_ASTC_6x5;
63 else if (blockX == 6 && blockY == 6)
64 return CompressedImageData::FORMAT_ASTC_6x6;
65 else if (blockX == 8 && blockY == 5)
66 return CompressedImageData::FORMAT_ASTC_8x5;
67 else if (blockX == 8 && blockY == 6)
68 return CompressedImageData::FORMAT_ASTC_8x6;
69 else if (blockX == 8 && blockY == 8)
70 return CompressedImageData::FORMAT_ASTC_8x8;
71 else if (blockX == 10 && blockY == 5)
72 return CompressedImageData::FORMAT_ASTC_10x5;
73 else if (blockX == 10 && blockY == 6)
74 return CompressedImageData::FORMAT_ASTC_10x6;
75 else if (blockX == 10 && blockY == 8)
76 return CompressedImageData::FORMAT_ASTC_10x8;
77 else if (blockX == 10 && blockY == 10)
78 return CompressedImageData::FORMAT_ASTC_10x10;
79 else if (blockX == 12 && blockY == 10)
80 return CompressedImageData::FORMAT_ASTC_12x10;
81 else if (blockX == 12 && blockY == 12)
82 return CompressedImageData::FORMAT_ASTC_12x12;
83
84 return CompressedImageData::FORMAT_UNKNOWN;
85 }
86
87 } // Anonymous namespace.
88
canParse(const filesystem::FileData * data)89 bool ASTCHandler::canParse(const filesystem::FileData *data)
90 {
91 if (data->getSize() <= sizeof(ASTCHeader))
92 return false;
93
94 const ASTCHeader *header = (const ASTCHeader *) data->getData();
95
96 uint32 identifier = (uint32) header->identifier[0]
97 + ((uint32) header->identifier[1] << 8)
98 + ((uint32) header->identifier[2] << 16)
99 + ((uint32) header->identifier[3] << 24);
100
101 if (identifier != ASTC_IDENTIFIER)
102 return false;
103
104 return true;
105 }
106
parse(filesystem::FileData * filedata,std::vector<CompressedImageData::SubImage> & images,size_t & dataSize,CompressedImageData::Format & format,bool & sRGB)107 uint8 *ASTCHandler::parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB)
108 {
109 if (!canParse(filedata))
110 throw love::Exception("Could not decode compressed data (not an .astc file?)");
111
112 ASTCHeader header = *(const ASTCHeader *) filedata->getData();
113
114 CompressedImageData::Format cformat = convertFormat(header.blockdimX, header.blockdimY, header.blockdimZ);
115
116 if (cformat == CompressedImageData::FORMAT_UNKNOWN)
117 throw love::Exception("Could not parse .astc file: unsupported ASTC format %dx%dx%d.", header.blockdimX, header.blockdimY, header.blockdimZ);
118
119 uint32 sizeX = header.sizeX[0] + (header.sizeX[1] << 8) + (header.sizeX[2] << 16);
120 uint32 sizeY = header.sizeY[0] + (header.sizeY[1] << 8) + (header.sizeY[2] << 16);
121 uint32 sizeZ = header.sizeZ[0] + (header.sizeZ[1] << 8) + (header.sizeZ[2] << 16);
122
123 uint32 blocksX = (sizeX + header.blockdimX - 1) / header.blockdimX;
124 uint32 blocksY = (sizeY + header.blockdimY - 1) / header.blockdimY;
125 uint32 blocksZ = (sizeZ + header.blockdimZ - 1) / header.blockdimZ;
126
127 size_t totalsize = blocksX * blocksY * blocksZ * 16;
128
129 if (totalsize + sizeof(header) > filedata->getSize())
130 throw love::Exception("Could not parse .astc file: file is too small.");
131
132 uint8 *data = nullptr;
133
134 try
135 {
136 data = new uint8[totalsize];
137 }
138 catch (std::bad_alloc &)
139 {
140 throw love::Exception("Out of memory.");
141 }
142
143 // .astc files only store a single mipmap level.
144 memcpy(data, (uint8 *) filedata->getData() + sizeof(ASTCHeader), totalsize);
145
146 CompressedImageData::SubImage mip;
147
148 mip.width = sizeX;
149 mip.height = sizeY;
150
151 mip.size = totalsize;
152 mip.data = data;
153
154 images.push_back(mip);
155
156 dataSize = totalsize;
157 format = cformat;
158 sRGB = false;
159
160 return data;
161 }
162
163 } // magpie
164 } // image
165 } // love
166
167