Debian Codegraph @

src/tools/til2img.cpp

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2022 - 2024 *
4 * *
5 * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
6 * Copyright (C) 2009 by Andrey Afletdinov <[email protected]> *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24 #include <algorithm>
25 #include <cstdint>
26 #include <cstdlib>
27 #include <filesystem>
28 #include <iomanip>
29 #include <iostream>
30 #include <sstream>
31 #include <string>
32 #include <system_error>
33 #include <vector>
34
35 #include "image.h"
36 #include "image_palette.h"
37 #include "image_tool.h"
38 #include "serialize.h"
39 #include "system.h"
40
41 namespace
42 {
43 constexpr size_t validPaletteSize = 768;
44 constexpr uint8_t spriteBackground = 0;
45 }
46
47 int main( int argc, char ** argv )
48 {
49 if ( argc < 4 ) {
50 const std::string toolName = System::GetFileName( argv[0] );
51
52 std::cerr << toolName << " extracts sprites in BMP or PNG format (if supported) from the specified TIL file(s) using the specified palette." << std::endl
53 << "Syntax: " << toolName << " dst_dir palette_file.pal input_file.til ..." << std::endl;
54 return EXIT_FAILURE;
55 }
56
57 const char * dstDir = argv[1];
58 const char * paletteFileName = argv[2];
59
60 {
61 StreamFile paletteStream;
62 if ( !paletteStream.open( paletteFileName, "rb" ) ) {
63 std::cerr << "Cannot open file " << paletteFileName << std::endl;
64 return EXIT_FAILURE;
65 }
66
67 const std::vector<uint8_t> palette = paletteStream.getRaw( 0 );
68 if ( palette.size() != validPaletteSize ) {
69 std::cerr << "Invalid palette size of " << palette.size() << " instead of " << validPaletteSize << std::endl;
70 return EXIT_FAILURE;
71 }
72
73 fheroes2::setGamePalette( palette );
74 }
75
76 std::vector<std::string> inputFileNames;
77 for ( int i = 3; i < argc; ++i ) {
78 if ( System::isShellLevelGlobbingSupported() ) {
79 inputFileNames.emplace_back( argv[i] );
80 }
81 else {
82 System::globFiles( argv[i], inputFileNames );
83 }
84 }
85
86 uint32_t spritesExtracted = 0;
87
88 for ( const std::string & inputFileName : inputFileNames ) {
89 std::cout << "Processing " << inputFileName << "..." << std::endl;
90
91 StreamFile inputStream;
92 if ( !inputStream.open( inputFileName, "rb" ) ) {
93 std::cerr << "Cannot open file " << inputFileName << std::endl;
94 // A non-existent or inaccessible file is not considered a fatal error
95 continue;
96 }
97
98 const std::filesystem::path prefixPath = std::filesystem::path( dstDir ) / std::filesystem::path( inputFileName ).stem();
99
100 std::error_code ec;
101
102 // Using the non-throwing overloads
103 if ( !std::filesystem::exists( prefixPath, ec ) && !std::filesystem::create_directories( prefixPath, ec ) ) {
104 std::cerr << "Cannot create directory " << prefixPath << std::endl;
105 return EXIT_FAILURE;
106 }
107
108 const uint16_t spritesCount = inputStream.getLE16();
109 if ( spritesCount == 0 ) {
110 std::cerr << inputFileName << ": no sprites found" << std::endl;
111 return EXIT_FAILURE;
112 }
113
114 const uint16_t spriteWidth = inputStream.getLE16();
115 const uint16_t spriteHeight = inputStream.getLE16();
116
117 const size_t spriteSize = static_cast<size_t>( spriteWidth ) * spriteHeight;
118 if ( spriteSize == 0 ) {
119 std::cerr << inputFileName << ": invalid sprite size " << spriteWidth << "x" << spriteHeight << std::endl;
120 return EXIT_FAILURE;
121 }
122
123 const std::vector<uint8_t> buf = inputStream.getRaw( spriteSize * spritesCount );
124 if ( buf.size() != spriteSize * spritesCount ) {
125 std::cerr << inputFileName << ": failed to extract sprites" << std::endl;
126 return EXIT_FAILURE;
127 }
128
129 std::vector<fheroes2::Image> sprites;
130 sprites.reserve( spritesCount );
131
132 fheroes2::decodeTILImages( buf.data(), spritesCount, spriteWidth, spriteHeight, sprites );
133 if ( sprites.size() != spritesCount ) {
134 std::cerr << inputFileName << ": failed to extract sprites" << std::endl;
135 return EXIT_FAILURE;
136 }
137
138 for ( size_t spriteIdx = 0; spriteIdx < sprites.size(); ++spriteIdx ) {
139 std::ostringstream spriteIdxStream;
140 spriteIdxStream << std::setw( 3 ) << std::setfill( '0' ) << spriteIdx;
141
142 const std::string spriteIdxStr = spriteIdxStream.str();
143 std::string outputFileName = ( prefixPath / spriteIdxStr ).string();
144
145 if ( fheroes2::isPNGFormatSupported() ) {
146 outputFileName += ".png";
147 }
148 else {
149 outputFileName += ".bmp";
150 }
151
152 if ( !fheroes2::Save( sprites[spriteIdx], outputFileName, spriteBackground ) ) {
153 std::cerr << inputFileName << ": error saving sprite " << spriteIdx << std::endl;
154 return EXIT_FAILURE;
155 }
156
157 ++spritesExtracted;
158 }
159 }
160
161 std::cout << "Total extracted sprites: " << spritesExtracted << std::endl;
162
163 return EXIT_SUCCESS;
164 }