Line data Source code
1 : /* Copyright (C) 2021 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "MapIO.h"
21 :
22 : #include "graphics/Patch.h"
23 : #include "lib/allocators/shared_ptr.h"
24 : #include "lib/file/file.h"
25 : #include "lib/file/vfs/vfs_path.h"
26 : #include "lib/os_path.h"
27 : #include "lib/status.h"
28 : #include "lib/tex/tex.h"
29 : #include "maths/MathUtil.h"
30 : #include "ps/Filesystem.h"
31 :
32 : #include <algorithm>
33 : #include <vector>
34 :
35 : Status ParseHeightmapImage(const std::shared_ptr<u8>& fileData, size_t fileSize, std::vector<u16>& heightmap);
36 :
37 0 : Status LoadHeightmapImageVfs(const VfsPath& filepath, std::vector<u16>& heightmap)
38 : {
39 0 : std::shared_ptr<u8> fileData;
40 : size_t fileSize;
41 :
42 0 : RETURN_STATUS_IF_ERR(g_VFS->LoadFile(filepath, fileData, fileSize));
43 :
44 0 : return ParseHeightmapImage(fileData, fileSize, heightmap);
45 : }
46 :
47 0 : Status LoadHeightmapImageOs(const OsPath& filepath, std::vector<u16>& heightmap)
48 : {
49 0 : File file;
50 0 : RETURN_STATUS_IF_ERR(file.Open(OsString(filepath), O_RDONLY));
51 :
52 0 : size_t fileSize = lseek(file.Descriptor(), 0, SEEK_END);
53 0 : lseek(file.Descriptor(), 0, SEEK_SET);
54 :
55 0 : std::shared_ptr<u8> fileData;
56 0 : RETURN_STATUS_IF_ERR(AllocateAligned(fileData, fileSize, maxSectorSize));
57 :
58 0 : Status readvalue = read(file.Descriptor(), fileData.get(), fileSize);
59 0 : file.Close();
60 :
61 0 : RETURN_STATUS_IF_ERR(readvalue);
62 :
63 0 : return ParseHeightmapImage(fileData, fileSize, heightmap);
64 : }
65 :
66 0 : Status ParseHeightmapImage(const std::shared_ptr<u8>& fileData, size_t fileSize, std::vector<u16>& heightmap)
67 : {
68 : // Decode to a raw pixel format
69 0 : Tex tex;
70 0 : RETURN_STATUS_IF_ERR(tex.decode(fileData, fileSize));
71 :
72 : // Convert to uncompressed BGRA with no mipmaps.
73 : // Note that grayscale images don't get converted - they remain grayscale, 8-bits per pixel.
74 0 : RETURN_STATUS_IF_ERR(tex.transform_to((tex.m_Flags | TEX_BGR | TEX_ALPHA) & ~(TEX_DXT | TEX_MIPMAPS)));
75 :
76 : // Pick smallest side of texture; truncate if not divisible by PATCH_SIZE
77 0 : ssize_t tileSize = std::min(tex.m_Width, tex.m_Height);
78 0 : tileSize -= tileSize % PATCH_SIZE;
79 :
80 0 : u8* mapdata = tex.get_data();
81 0 : ssize_t bytesPP = tex.m_Bpp / 8;
82 0 : ssize_t mapLineSkip = tex.m_Width * bytesPP;
83 :
84 : // Copy image data into the heightmap
85 0 : heightmap.resize(SQR(tileSize + 1));
86 : // if hoisted out of the loop as a micro-optimisation that doesn't harm readability much.
87 0 : if (bytesPP == 1)
88 0 : for (ssize_t y = 0; y < tileSize + 1; ++y)
89 0 : for (ssize_t x = 0; x < tileSize + 1; ++x)
90 : {
91 : // Repeat the last pixel of the image for the last vertex of the heightmap
92 0 : int offset = std::min(y, tileSize - 1) * mapLineSkip + std::min(x, tileSize - 1);
93 0 : heightmap[(tileSize - y) * (tileSize + 1) + x] = static_cast<u16>(256) * mapdata[offset];
94 : }
95 0 : else if (bytesPP == 4)
96 0 : for (ssize_t y = 0; y < tileSize + 1; ++y)
97 0 : for (ssize_t x = 0; x < tileSize + 1; ++x)
98 : {
99 : // Repeat the last pixel of the image for the last vertex of the heightmap
100 0 : int offset = std::min(y, tileSize - 1) * mapLineSkip + std::min(x, tileSize - 1) * bytesPP;
101 0 : heightmap[(tileSize - y) * (tileSize + 1) + x] = static_cast<u16>(256) * std::max({
102 0 : mapdata[offset],
103 0 : mapdata[offset + 1],
104 0 : mapdata[offset + 2]});
105 : }
106 :
107 0 : return INFO::OK;
108 3 : }
|