1 /***************************************************************************
2  *   Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com>          *
3  *                                                                         *
4  *   Part of the Free Heroes2 Engine:                                      *
5  *   http://sourceforge.net/projects/fheroes2                              *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21  ***************************************************************************/
22 
23 #include <cstring>
24 #include <iomanip>
25 #include <iostream>
26 #include <sstream>
27 
28 #include "image_tool.h"
29 #include "serialize.h"
30 #include "system.h"
31 
main(int argc,char ** argv)32 int main( int argc, char ** argv )
33 {
34     if ( argc != 3 ) {
35         std::cout << argv[0] << " [-d] infile.til extract_to_dir" << std::endl;
36         return EXIT_SUCCESS;
37     }
38 
39     StreamFile sf;
40 
41     if ( !sf.open( argv[1], "rb" ) ) {
42         std::cout << "error open file: " << argv[1] << std::endl;
43         return EXIT_SUCCESS;
44     }
45 
46     std::string prefix( argv[2] );
47     std::string shortname( argv[1] );
48 
49     bool debugMode = false;
50     if ( shortname == "-d" ) {
51         debugMode = true;
52     }
53 
54     shortname.replace( shortname.find( "." ), 4, "" );
55     prefix = System::ConcatePath( prefix, shortname );
56 
57     if ( 0 != System::MakeDirectory( prefix ) ) {
58         std::cout << "error mkdir: " << prefix << std::endl;
59         return EXIT_SUCCESS;
60     }
61 
62     int size = sf.size();
63     int count = sf.getLE16();
64     int width = sf.getLE16();
65     int height = sf.getLE16();
66     std::vector<uint8_t> buf = sf.getRaw( width * height * count );
67     if ( debugMode ) {
68         std::cout << "Size of stream " << size << "(" << buf.size() << ")" << std::endl;
69         std::cout << "Count of images " << count << "(" << width << "," << height << ")" << std::endl;
70     }
71 
72     for ( int cur = 0; cur < count; ++cur ) {
73         uint32_t offset = width * height * cur;
74         if ( offset < buf.size() ) {
75             fheroes2::Image image( width, height );
76             memcpy( image.image(), &buf[offset], static_cast<size_t>( width * height ) );
77             std::fill( image.transform(), image.transform() + width * height, 0 );
78 
79             std::ostringstream stream;
80             stream << std::setw( 3 ) << std::setfill( '0' ) << cur;
81             std::string dstfile = System::ConcatePath( prefix, stream.str() );
82 
83 #ifndef FHEROES2_IMAGE_SUPPORT
84             dstfile += ".bmp";
85 #else
86             dstfile += ".png";
87 #endif
88             if ( debugMode ) {
89                 std::cout << "Saving " << dstfile << std::endl;
90             }
91 
92             if ( !fheroes2::Save( image, dstfile, 0 ) )
93                 std::cout << "error" << std::endl;
94         }
95     }
96 
97     sf.close();
98     std::cout << "expand to: " << prefix << std::endl;
99     return EXIT_SUCCESS;
100 }
101