Debian Codegraph @

src/tools/icn2img.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 <fstream>
29 #include <iomanip>
30 #include <iostream>
31 #include <sstream>
32 #include <string>
33 #include <system_error>
34 #include <vector>
35
36 #include "agg_file.h"
37 #include "image.h"
38 #include "image_palette.h"
39 #include "image_tool.h"
40 #include "serialize.h"
41 #include "system.h"
42
43 namespace
44 {
45 constexpr size_t validPaletteSize = 768;
46 constexpr uint8_t spriteBackground = 23;
47 }
48
49 int main( int argc, char ** argv )
50 {
51 if ( argc < 4 ) {
52 const std::string toolName = System::GetFileName( argv[0] );
53
54 std::cerr << toolName << " extracts sprites in BMP or PNG format (if supported) and their offsets from the specified ICN file(s) using the specified palette."
55 << std::endl
56 << "Syntax: " << toolName << " dst_dir palette_file.pal input_file.icn ..." << std::endl;
57 return EXIT_FAILURE;
58 }
59
60 const char * dstDir = argv[1];
61 const char * paletteFileName = argv[2];
62
63 {
64 StreamFile paletteStream;
65 if ( !paletteStream.open( paletteFileName, "rb" ) ) {
66 std::cerr << "Cannot open file " << paletteFileName << std::endl;
67 return EXIT_FAILURE;
68 }
69
70 const std::vector<uint8_t> palette = paletteStream.getRaw( 0 );
71 if ( palette.size() != validPaletteSize ) {
72 std::cerr << "Invalid palette size of " << palette.size() << " instead of " << validPaletteSize << std::endl;
73 return EXIT_FAILURE;
74 }
75
76 fheroes2::setGamePalette( palette );
77 }
78
79 std::vector<std::string> inputFileNames;
80 for ( int i = 3; i < argc; ++i ) {
81 if ( System::isShellLevelGlobbingSupported() ) {
82 inputFileNames.emplace_back( argv[i] );
83 }
84 else {
85 System::globFiles( argv[i], inputFileNames );
86 }
87 }
88
89 uint32_t spritesExtracted = 0;
90 uint32_t spritesFailed = 0;
91
92 for ( const std::string & inputFileName : inputFileNames ) {
93 std::cout << "Processing " << inputFileName << "..." << std::endl;
94
95 StreamFile inputStream;
96 if ( !inputStream.open( inputFileName, "rb" ) ) {
97 std::cerr << "Cannot open file " << inputFileName << std::endl;
98 // A non-existent or inaccessible file is not considered a fatal error
99 continue;
100 }
101
102 const std::filesystem::path prefixPath = std::filesystem::path( dstDir ) / std::filesystem::path( inputFileName ).stem();
103
104 std::error_code ec;
105
106 // Using the non-throwing overloads
107 if ( !std::filesystem::exists( prefixPath, ec ) && !std::filesystem::create_directories( prefixPath, ec ) ) {
108 std::cerr << "Cannot create directory " << prefixPath << std::endl;
109 return EXIT_FAILURE;
110 }
111
112 const std::filesystem::path offsetFilePath = prefixPath / "offsets.txt";
113
114 std::ofstream offsetStream( offsetFilePath, std::ios_base::trunc );
115 if ( !offsetStream ) {
116 std::cerr << "Cannot create file " << offsetFilePath << std::endl;
117 return EXIT_FAILURE;
118 }
119
120 const uint16_t spritesCount = inputStream.getLE16();
121 const uint32_t totalSize = inputStream.getLE32();
122
123 const size_t beginPos = inputStream.tell();
124
125 std::vector<fheroes2::ICNHeader> headers( spritesCount );
126 for ( fheroes2::ICNHeader & header : headers ) {
127 inputStream >> header;
128 }
129
130 for ( uint16_t spriteIdx = 0; spriteIdx < spritesCount; ++spriteIdx ) {
131 const fheroes2::ICNHeader & header = headers[spriteIdx];
132
133 inputStream.seek( beginPos + header.offsetData );
134
135 const uint32_t dataSize = ( spriteIdx + 1 < spritesCount ? headers[spriteIdx + 1].offsetData - header.offsetData : totalSize - header.offsetData );
136 if ( dataSize == 0 ) {
137 ++spritesFailed;
138
139 std::cerr << inputFileName << ": sprite " << spriteIdx << " is empty" << std::endl;
140 continue;
141 }
142
143 const std::vector<uint8_t> buf = inputStream.getRaw( dataSize );
144 if ( buf.size() != dataSize ) {
145 ++spritesFailed;
146
147 std::cerr << inputFileName << ": invalid size of sprite " << spriteIdx << ": expected " << dataSize << ", got " << buf.size() << std::endl;
148 continue;
149 }
150
151 const fheroes2::Sprite sprite = fheroes2::decodeICNSprite( buf.data(), buf.data() + dataSize, header );
152
153 std::ostringstream spriteIdxStream;
154 spriteIdxStream << std::setw( 3 ) << std::setfill( '0' ) << spriteIdx;
155
156 const std::string spriteIdxStr = spriteIdxStream.str();
157 std::string outputFileName = ( prefixPath / spriteIdxStr ).string();
158
159 if ( fheroes2::isPNGFormatSupported() ) {
160 outputFileName += ".png";
161 }
162 else {
163 outputFileName += ".bmp";
164 }
165
166 offsetStream << spriteIdxStr << " [" << header.offsetX << ", " << header.offsetY << "]";
167 if ( header.animationFrames > 0 ) {
168 if ( header.animationFrames != 32 ) {
169 offsetStream << ", animation frames count = " << std::to_string( header.animationFrames );
170 }
171 else {
172 offsetStream << ", monochromatic image";
173 }
174 }
175 offsetStream << std::endl;
176
177 if ( !offsetStream ) {
178 std::cerr << "Error writing to file " << offsetFilePath << std::endl;
179 return EXIT_FAILURE;
180 }
181
182 if ( !fheroes2::Save( sprite, outputFileName, spriteBackground ) ) {
183 ++spritesFailed;
184
185 std::cerr << inputFileName << ": error saving sprite " << spriteIdx << std::endl;
186 continue;
187 }
188
189 ++spritesExtracted;
190 }
191 }
192
193 std::cout << "Total extracted sprites: " << spritesExtracted << ", failed sprites: " << spritesFailed << std::endl;
194
195 return ( spritesFailed == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE;
196 }