| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2023 - 2024 * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include <cstdint> |
| 22 | #include <cstdlib> |
| 23 | #include <iostream> |
| 24 | #include <set> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "image.h" |
| 29 | #include "image_palette.h" |
| 30 | #include "image_tool.h" |
| 31 | #include "serialize.h" |
| 32 | #include "system.h" |
| 33 | |
| 34 | namespace |
| 35 | { |
| 36 | constexpr size_t validPaletteSize = 768; |
| 37 | } |
| 38 | |
| 39 | int main( int argc, char ** argv ) |
| 40 | { |
| 41 | if ( argc != 3 ) { |
| 42 | const std::string toolName = System::GetFileName( argv[0] ); |
| 43 | |
| 44 | std::cerr << toolName << " generates an image with colors based on a provided palette file." << std::endl |
| 45 | << "Syntax: " << toolName << " palette_file.pal output.bmp|output.png" << std::endl; |
| 46 | return EXIT_FAILURE; |
| 47 | } |
| 48 | |
| 49 | const char * paletteFileName = argv[1]; |
| 50 | const char * outputFileName = argv[2]; |
| 51 | |
| 52 | { |
| 53 | StreamFile paletteStream; |
| 54 | if ( !paletteStream.open( paletteFileName, "rb" ) ) { |
| 55 | std::cerr << "Cannot open file " << paletteFileName << std::endl; |
| 56 | return EXIT_FAILURE; |
| 57 | } |
| 58 | |
| 59 | const std::vector<uint8_t> palette = paletteStream.getRaw( 0 ); |
| 60 | if ( palette.size() != validPaletteSize ) { |
| 61 | std::cerr << "Invalid palette size of " << palette.size() << " instead of " << validPaletteSize << std::endl; |
| 62 | return EXIT_FAILURE; |
| 63 | } |
| 64 | |
| 65 | fheroes2::setGamePalette( palette ); |
| 66 | } |
| 67 | |
| 68 | fheroes2::Image image( 256, 256 ); |
| 69 | image.reset(); |
| 70 | // We do not need to care about the transform layer. |
| 71 | image._disableTransformLayer(); |
| 72 | |
| 73 | // These color indexes are from PAL::GetCyclingPalette() method. |
| 74 | const std::set<uint8_t> cyclingColors{ 214, 215, 216, 217, 218, 219, 220, 221, 231, 232, 233, 234, 235, 238, 239, 240, 241 }; |
| 75 | |
| 76 | for ( uint8_t y = 0; y < 16; ++y ) { |
| 77 | for ( uint8_t x = 0; x < 16; ++x ) { |
| 78 | fheroes2::Fill( image, x * 16, y * 16, 16, 16, x + y * 16 ); |
| 79 | |
| 80 | if ( cyclingColors.count( x + y * 16 ) > 0 ) { |
| 81 | fheroes2::Fill( image, x * 16, y * 16, 4, 4, 0 ); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if ( !fheroes2::Save( image, outputFileName ) ) { |
| 87 | std::cerr << "Error writing to file " << outputFileName << std::endl; |
| 88 | return EXIT_FAILURE; |
| 89 | } |
| 90 | |
| 91 | return EXIT_SUCCESS; |
| 92 | } |