LCOV - code coverage report
Current view: top level - source/graphics - MapIO.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 46 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 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/alignment.h"
      24             : #include "lib/allocators/shared_ptr.h"
      25             : #include "lib/file/file.h"
      26             : #include "lib/file/vfs/vfs.h"
      27             : #include "lib/file/vfs/vfs_path.h"
      28             : #include "lib/os_path.h"
      29             : #include "lib/posix/posix_types.h"
      30             : #include "lib/status.h"
      31             : #include "lib/tex/tex.h"
      32             : #include "maths/MathUtil.h"
      33             : #include "ps/Filesystem.h"
      34             : 
      35             : #include <algorithm>
      36             : #include <cstddef>
      37             : #include <filesystem>
      38             : #include <fcntl.h>
      39             : #include <memory>
      40             : #include <string>
      41             : #include <vector>
      42             : 
      43             : Status ParseHeightmapImage(const std::shared_ptr<std::uint8_t>& fileData, size_t fileSize,
      44             :     std::vector<std::uint16_t>& heightmap);
      45             : 
      46           0 : Status LoadHeightmapImageVfs(const VfsPath& filepath, std::vector<std::uint16_t>& heightmap)
      47             : {
      48           0 :     std::shared_ptr<std::uint8_t> fileData;
      49           0 :     size_t fileSize;
      50             : 
      51           0 :     RETURN_STATUS_IF_ERR(g_VFS->LoadFile(filepath, fileData, fileSize));
      52             : 
      53           0 :     return ParseHeightmapImage(fileData, fileSize, heightmap);
      54           0 : }
      55             : 
      56           0 : Status LoadHeightmapImageOs(const OsPath& filepath, std::vector<std::uint16_t>& heightmap)
      57             : {
      58           0 :     File file;
      59           0 :     RETURN_STATUS_IF_ERR(file.Open(OsString(filepath), O_RDONLY));
      60             : 
      61           0 :     std::error_code ec{};
      62           0 :     const size_t fileSize{static_cast<size_t>(std::filesystem::file_size(filepath.fileSystemPath(), ec))};
      63           0 :     if (ec)
      64           0 :         return StatusFromSystemError(ec);
      65             : 
      66           0 :     std::shared_ptr<std::uint8_t> fileData;
      67           0 :     RETURN_STATUS_IF_ERR(AllocateAligned(fileData, fileSize, maxSectorSize));
      68             : 
      69           0 :     Status readvalue = read(file.Descriptor(), fileData.get(), fileSize);
      70           0 :     file.Close();
      71             : 
      72           0 :     RETURN_STATUS_IF_ERR(readvalue);
      73             : 
      74           0 :     return ParseHeightmapImage(fileData, fileSize, heightmap);
      75           0 : }
      76             : 
      77           0 : Status ParseHeightmapImage(const std::shared_ptr<std::uint8_t>& fileData, size_t fileSize,
      78             :     std::vector<std::uint16_t>& heightmap)
      79             : {
      80             :     // Decode to a raw pixel format
      81           0 :     Tex tex;
      82           0 :     RETURN_STATUS_IF_ERR(tex.decode(fileData, fileSize));
      83             : 
      84             :     // Convert to uncompressed BGRA with no mipmaps.
      85             :     // Note that grayscale images don't get converted - they remain grayscale, 8-bits per pixel.
      86           0 :     RETURN_STATUS_IF_ERR(tex.transform_to((tex.m_Flags | TEX_BGR | TEX_ALPHA) & ~(TEX_DXT | TEX_MIPMAPS)));
      87             : 
      88             :     // Pick smallest side of texture; truncate if not divisible by PATCH_SIZE
      89           0 :     ssize_t tileSize = std::min(tex.m_Width, tex.m_Height);
      90           0 :     tileSize -= tileSize % PATCH_SIZE;
      91             : 
      92           0 :     std::uint8_t* mapdata = tex.get_data();
      93           0 :     ssize_t bytesPP = tex.m_Bpp / 8;
      94           0 :     ssize_t mapLineSkip = tex.m_Width * bytesPP;
      95             : 
      96             :     // Copy image data into the heightmap
      97           0 :     heightmap.resize(SQR(tileSize + 1));
      98             :     // if hoisted out of the loop as a micro-optimisation that doesn't harm readability much.
      99           0 :     if (bytesPP == 1)
     100           0 :         for (ssize_t y = 0; y < tileSize + 1; ++y)
     101           0 :             for (ssize_t x = 0; x < tileSize + 1; ++x)
     102             :             {
     103             :                 // Repeat the last pixel of the image for the last vertex of the heightmap
     104           0 :                 int offset = std::min(y, tileSize - 1) * mapLineSkip + std::min(x, tileSize - 1);
     105           0 :                 heightmap[(tileSize - y) * (tileSize + 1) + x] =
     106           0 :                     static_cast<std::uint16_t>(256) * mapdata[offset];
     107             :             }
     108           0 :     else if (bytesPP == 4)
     109           0 :         for (ssize_t y = 0; y < tileSize + 1; ++y)
     110           0 :             for (ssize_t x = 0; x < tileSize + 1; ++x)
     111             :             {
     112             :                 // Repeat the last pixel of the image for the last vertex of the heightmap
     113           0 :                 int offset = std::min(y, tileSize - 1) * mapLineSkip + std::min(x, tileSize - 1) * bytesPP;
     114           0 :                 heightmap[(tileSize - y) * (tileSize + 1) + x] =
     115           0 :                     static_cast<std::uint16_t>(256) * std::max({
     116           0 :                     mapdata[offset],
     117           0 :                     mapdata[offset + 1],
     118           0 :                     mapdata[offset + 2]});
     119             :             }
     120             : 
     121             :     return INFO::OK;
     122           0 : }

Generated by: LCOV version 1.16