1 /* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
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"
35 Status
ParseHeightmapImage(const std::shared_ptr
<u8
>& fileData
, size_t fileSize
, std::vector
<u16
>& heightmap
);
37 Status
LoadHeightmapImageVfs(const VfsPath
& filepath
, std::vector
<u16
>& heightmap
)
39 std::shared_ptr
<u8
> fileData
;
42 RETURN_STATUS_IF_ERR(g_VFS
->LoadFile(filepath
, fileData
, fileSize
));
44 return ParseHeightmapImage(fileData
, fileSize
, heightmap
);
47 Status
LoadHeightmapImageOs(const OsPath
& filepath
, std::vector
<u16
>& heightmap
)
50 RETURN_STATUS_IF_ERR(file
.Open(OsString(filepath
), O_RDONLY
));
52 size_t fileSize
= lseek(file
.Descriptor(), 0, SEEK_END
);
53 lseek(file
.Descriptor(), 0, SEEK_SET
);
55 std::shared_ptr
<u8
> fileData
;
56 RETURN_STATUS_IF_ERR(AllocateAligned(fileData
, fileSize
, maxSectorSize
));
58 Status readvalue
= read(file
.Descriptor(), fileData
.get(), fileSize
);
61 RETURN_STATUS_IF_ERR(readvalue
);
63 return ParseHeightmapImage(fileData
, fileSize
, heightmap
);
66 Status
ParseHeightmapImage(const std::shared_ptr
<u8
>& fileData
, size_t fileSize
, std::vector
<u16
>& heightmap
)
68 // Decode to a raw pixel format
70 RETURN_STATUS_IF_ERR(tex
.decode(fileData
, fileSize
));
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 RETURN_STATUS_IF_ERR(tex
.transform_to((tex
.m_Flags
| TEX_BGR
| TEX_ALPHA
) & ~(TEX_DXT
| TEX_MIPMAPS
)));
76 // Pick smallest side of texture; truncate if not divisible by PATCH_SIZE
77 ssize_t tileSize
= std::min(tex
.m_Width
, tex
.m_Height
);
78 tileSize
-= tileSize
% PATCH_SIZE
;
80 u8
* mapdata
= tex
.get_data();
81 ssize_t bytesPP
= tex
.m_Bpp
/ 8;
82 ssize_t mapLineSkip
= tex
.m_Width
* bytesPP
;
84 // Copy image data into the heightmap
85 heightmap
.resize(SQR(tileSize
+ 1));
86 // if hoisted out of the loop as a micro-optimisation that doesn't harm readability much.
88 for (ssize_t y
= 0; y
< tileSize
+ 1; ++y
)
89 for (ssize_t x
= 0; x
< tileSize
+ 1; ++x
)
91 // Repeat the last pixel of the image for the last vertex of the heightmap
92 int offset
= std::min(y
, tileSize
- 1) * mapLineSkip
+ std::min(x
, tileSize
- 1);
93 heightmap
[(tileSize
- y
) * (tileSize
+ 1) + x
] = static_cast<u16
>(256) * mapdata
[offset
];
95 else if (bytesPP
== 4)
96 for (ssize_t y
= 0; y
< tileSize
+ 1; ++y
)
97 for (ssize_t x
= 0; x
< tileSize
+ 1; ++x
)
99 // Repeat the last pixel of the image for the last vertex of the heightmap
100 int offset
= std::min(y
, tileSize
- 1) * mapLineSkip
+ std::min(x
, tileSize
- 1) * bytesPP
;
101 heightmap
[(tileSize
- y
) * (tileSize
+ 1) + x
] = static_cast<u16
>(256) * std::max({
104 mapdata
[offset
+ 2]});