| 2161 | } |
| 2162 | |
| 2163 | dtStatus dtDecompressTileCacheLayer(dtTileCacheAlloc* alloc, dtTileCacheCompressor* comp, |
| 2164 | unsigned char* compressed, const int compressedSize, |
| 2165 | dtTileCacheLayer** layerOut) |
| 2166 | { |
| 2167 | dtAssert(alloc); |
| 2168 | dtAssert(comp); |
| 2169 | |
| 2170 | if (!layerOut) |
| 2171 | return DT_FAILURE | DT_INVALID_PARAM; |
| 2172 | if (!compressed) |
| 2173 | return DT_FAILURE | DT_INVALID_PARAM; |
| 2174 | |
| 2175 | *layerOut = 0; |
| 2176 | |
| 2177 | dtTileCacheLayerHeader* compressedHeader = (dtTileCacheLayerHeader*)compressed; |
| 2178 | if (compressedHeader->magic != DT_TILECACHE_MAGIC) |
| 2179 | return DT_FAILURE | DT_WRONG_MAGIC; |
| 2180 | if (compressedHeader->version != DT_TILECACHE_VERSION) |
| 2181 | return DT_FAILURE | DT_WRONG_VERSION; |
| 2182 | |
| 2183 | const int layerSize = dtAlign4(sizeof(dtTileCacheLayer)); |
| 2184 | const int headerSize = dtAlign4(sizeof(dtTileCacheLayerHeader)); |
| 2185 | const int gridSize = (int)compressedHeader->width * (int)compressedHeader->height; |
| 2186 | const int bufferSize = layerSize + headerSize + gridSize*4; |
| 2187 | |
| 2188 | unsigned char* buffer = (unsigned char*)alloc->alloc(bufferSize); |
| 2189 | if (!buffer) |
| 2190 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 2191 | memset(buffer, 0, bufferSize); |
| 2192 | |
| 2193 | dtTileCacheLayer* layer = (dtTileCacheLayer*)buffer; |
| 2194 | dtTileCacheLayerHeader* header = (dtTileCacheLayerHeader*)(buffer + layerSize); |
| 2195 | unsigned char* grids = buffer + layerSize + headerSize; |
| 2196 | const int gridsSize = bufferSize - (layerSize + headerSize); |
| 2197 | |
| 2198 | // Copy header |
| 2199 | memcpy(header, compressedHeader, headerSize); |
| 2200 | // Decompress grid. |
| 2201 | int size = 0; |
| 2202 | dtStatus status = comp->decompress(compressed+headerSize, compressedSize-headerSize, |
| 2203 | grids, gridsSize, &size); |
| 2204 | if (dtStatusFailed(status)) |
| 2205 | { |
| 2206 | alloc->free(buffer); |
| 2207 | return status; |
| 2208 | } |
| 2209 | |
| 2210 | layer->header = header; |
| 2211 | layer->heights = grids; |
| 2212 | layer->areas = grids + gridSize; |
| 2213 | layer->cons = grids + gridSize*2; |
| 2214 | layer->regs = grids + gridSize*3; |
| 2215 | |
| 2216 | *layerOut = layer; |
| 2217 | |
| 2218 | return DT_SUCCESS; |
| 2219 | } |
| 2220 |
no test coverage detected