Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Removed out commented code for the Boost library & fixed line tab issues.
[xestiaab/.git] / source / tools / bitmapcode.cpp
1 //-------------------------------------------------------------------
2 // bitmapcode: Helper application which coverts PNG files into
3 // C++ code outputting binary code.
4 //
5 // This application also is a simple test if the wxWidgets header 
6 // files and library files are there.
7 //
8 // This file is licenced under the GNU General Public License
9 // version 3 only. (GPLv3 only).
10 //-------------------------------------------------------------------
12 #include <iostream>
13 #include <fstream>
14 #include <iomanip>
15 #include <ios>
16 #include <wx/wx.h>
17 #include <wx/dir.h>
18 #include <wx/tokenzr.h>
19 #include <string.h>
21 using namespace std;
23 void CreateCPPFile(wxString dirfilename, wxString PNGFilename, int* counter)
24 {
25 //-------------------------------------------------------------------
26 // CreateCPPFile: Create a CPP file from the filename given.
27 //
28 // dirfilename  wxString of the directory name.
29 // filename     wxString of the filename.
30 // counter      Pointer to a integer counter.
31 //-------------------------------------------------------------------
32         
33         char pngfile_char;
34         int pageseek = 0;
35         wxString outname;
36         wxString CPPFilename;
37         CPPFilename = PNGFilename;
38         CPPFilename.RemoveLast(4);
39         CPPFilename.Append(wxT(".cpp"));
40     
41         wxString DirFilenameOnly;
42         wxString CPPFilenameOnly;
44 #if defined(__HAIKU__)
46 #elif defined(__WIN32__)
48         wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("\\"));
49         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
51 #else
53         wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("/"));
54         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
56 #endif
58         while (CPPFilenameTokens.HasMoreTokens()){
60                 CPPFilenameOnly = CPPFilenameTokens.GetNextToken();
62         }
64         while (DirFilenameTokens.HasMoreTokens()){
66                 DirFilenameOnly = DirFilenameTokens.GetNextToken();
68         }
70         // Setup the PNG file reading and cpp file.
71     
72         fstream cppfile, pngfile;
74 #if defined(__WIN32__)
76         pngfile.open(PNGFilename.wc_str(), ios::in | ios::binary );
77         cppfile.open(CPPFilename.wc_str(), ios::out | ios::trunc );     
79 # else
81         pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
82         cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc );
84 #endif
86         outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
87         outname.Append(wxT("_"));
88         outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
89         outname.MakeUpper();
90         outname.RemoveLast(4);
92         // Setup the inclusion guard.
93     
94         cppfile << "#include <iostream>" << endl << endl;    
95         cppfile << "#ifndef " << outname.c_str() << "_CPP" << endl;
96         cppfile << "#define " << outname.c_str() << "_CPP" << endl << endl;
97     
98         outname.Clear();
99         outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
100         outname.Append(wxT("_"));
101         outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
102         outname.RemoveLast(4);
103         outname.Append(wxT("_png"));
104         outname.MakeLower();
105     
106         // Convert the PNG file into an unsigned char array.
107     
108         cppfile << "static unsigned char " << outname.c_str() <<
109                 "[] = {" << endl;
110     
111         while (pngfile){
112                 pngfile.get(pngfile_char);
113                 cppfile << "0x";
115                 if ((unsigned short)pngfile_char > 255){
117                         cppfile << hex << setw(2) << setfill('0') <<
118                                 ((unsigned short)pngfile_char - 65280);
120                 } else {
122                         cppfile << hex << setw(2) << setfill('0') <<
123                                 (unsigned short)pngfile_char;
125                 }
127                 cppfile << ", ";
128                 pageseek++;
130                 if (pageseek == 8){
131                         cppfile << endl;
132                         pageseek = 0;
133                 }
135         }
136     
137         // End the file, close it and increment the counter.
138     
139         cppfile << "};" << endl << endl;
140         cppfile << "#endif" << endl << endl;
141         cppfile.close();
142     
143         cout << "CPP\t" << CPPFilename.c_str() << endl;    
144     
145         ++*counter;
146     
149 void CreateHPPFileDir(wxString dirfilename, wxArrayString filelist, 
150                         int* counter)
152 //-------------------------------------------------------------------
153 // CreateHPPFileDir:    Create a HPP Directory with the directory
154 //                      filename and list of files in the directory
155 //                      given.
156 //
157 // dirfilename  wxString of the directory name.
158 // filelist     wxArrayString of the directory filelist.
159 // counter      Pointer to a integer counter.
160 //-------------------------------------------------------------------  
162         fstream hppfile;
163     
164         bool fmatch = FALSE;
165         wxString finaldirname;
166         wxString HPPFilename = dirfilename.c_str();
168         wxString DirFilenameOnly;
169         wxString HPPFilenameOnly;
170         wxString CPPFilename;
172 #if defined(__WIN32__)
174         wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("\\"));
175         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
177 #else
179         wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("/"));
180         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
182 #endif
184         while (HPPFilenameTokens.HasMoreTokens()){
186                 HPPFilenameOnly = HPPFilenameTokens.GetNextToken();
188         }
190         while (DirFilenameTokens.HasMoreTokens()){
192                 DirFilenameOnly = DirFilenameTokens.GetNextToken();
194         }
196         // Make the directory filename upper case for writing to the
197         // header file.
198     
199         wxString CPPFileOnly;
201         finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
202         finaldirname.MakeUpper();
203     
204         // Write out the header file inclusion guard.
206         HPPFilename.Append(wxT(".h"));
208 #if defined(__WIN32__)
210             hppfile.open(HPPFilename.wc_str(), ios::out | ios::trunc );
212 #else
214         hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
216 #endif
218         hppfile << "#include <iostream>" << endl << endl;
219         hppfile << "#ifndef " << finaldirname.c_str() << "_H" << endl;
220         hppfile << "#define " << finaldirname.c_str() << "_H" << endl << endl;
221         hppfile << "// List all CPP files in the directory." << endl << endl;
222     
223         // Write each CPP file into the header file.
224     
225         for (int f = 0; f < filelist.GetCount() ; f++){
227                 CPPFilename = filelist[f];
229 #if defined(__HAIKU__)
231 #elif defined(__WIN32__)
233                 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("\\"));
235 #else
237                 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
239 #endif
241                 while (CPPFilenameTokenizer.HasMoreTokens()){
243                         CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
245                 }
247                 CPPFileOnly.RemoveLast(4);
248                 CPPFileOnly.Append(wxT(".cpp"));
250                 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
251                         "/" << CPPFileOnly.c_str() << "\"" << endl;
253         }
254     
255         // Write the end if and close the file.
256     
257         hppfile << endl << "#endif" << endl << endl;
258         hppfile.close();
259     
260         // Increment the HPP file counter.
262         cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
263         ++*counter;
264     
267 int main(int argc, char *argv[])
270         int fp = 0;
271         int cppg = 0;
272         int hppg = 0;
273     
274         wxArrayString dirlist;
275         wxArrayString filelist;
276         wxString BitmapHeaderFilename;
277         wxString DirFilenameWxS;
278     
279         // Check if completed file exists before doing anything
280         // else and write an error message if it does exist.
282         if (wxFileExists(wxT("bitmapsdone"))){
284                 std::cout << "Bitmap files have already been generated!" << std::endl << std::endl;
285                 std::cout << "To regenerate the files simply delete the bitmapsdone file where this helper application is run." << std::endl;
288                 return 0;
290         }
292         fstream finalhppfile;
294         if (!argv[1]){
295                 std::cout << "Error: No directory name given!" << std::endl;
296                 return -1;
297         }
298   
299         // Look in the subdirectories of the bitmaps directory and
300         // collect the names of the directories.
301     
302         std::cout << "Working out directories in bitmaps directory..." << 
303         std::endl;
305         const char *dirarg = argv[1];
307         wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
309 #if defined(__HAIKU__)
311 #elif defined(__WIN32__)
313         BitmapHeaderFilename.Append(BitmapDirName);
314         BitmapHeaderFilename.Append(wxT("\\..\\bitmaps.h"));
316 #else
318         BitmapHeaderFilename.Append(BitmapDirName);
319         BitmapHeaderFilename.Append(wxT("/../bitmaps.h"));
321 #endif
323         if ( wxDirExists(BitmapDirName) ){
324     
325                 wxDir BitmapDir(BitmapDirName);
327                 wxString BitmapSubDir;
328                 wxString BitmapSubDirFull;
330                 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
332                 while (ContinueProcess){
334 #if defined(__HAIKU__)
336                         BitmapSubDirFull.Append(BitmapSubDir);
337                         BitmapSubDirFull.Append();
339 #elif defined(__WIN32__)
341                         BitmapSubDirFull.Append(BitmapDirName);
342                         BitmapSubDirFull.Append(wxT("\\"));
343                         BitmapSubDirFull.Append(BitmapSubDir);
345 #else
347                         BitmapSubDirFull.Append(BitmapDirName);
348                         BitmapSubDirFull.Append(wxT("/"));
349                         BitmapSubDirFull.Append(BitmapSubDir);
351 #endif
353                         if (wxDirExists(BitmapSubDirFull)){
354                                 dirlist.Add(BitmapSubDirFull);
355                         }
357                         ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
359                         BitmapSubDirFull.Clear();
361                 }
362       
363         } else {
364                 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
365                 return 1;
366         }
367     
368         // Process each directory, generating a .cpp and .hpp file 
369         // for each image and then a final .hpp for the directory
370         // containing the .hpp's for the directories.
371     
372         if (dirlist.GetCount() == 0){
373                 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
374                 return 1;
375         }
376     
377         std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
378     
379         DirFilenameWxS.Empty();
381         std::cout << BitmapHeaderFilename.c_str() << std::endl;
383 #if defined(__WIN32__)
385         finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
387 #else
389         finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
391 #endif
393         finalhppfile << "#include <iostream>" << endl << endl;
394         finalhppfile << "#ifndef BITMAPS_H" << endl;
395         finalhppfile << "#define BITMAPS_H" << endl << endl;
397         for (int bi = 0; bi < dirlist.GetCount(); bi++)
398         {
400                 wxString BitmapSubDirName = dirlist[bi];
401                 wxString BitmapFilename;
402                 wxDir BitmapSubDir(BitmapSubDirName);
404                 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
406                 while (ContinueProcess){
408                         if (BitmapFilename.Right(4) == wxT(".PNG") ||
409                                 
410                                 BitmapFilename.Right(4) == wxT(".png")){
412 #if defined(__HAIKU__)
414                                 BitmapSubDirFull.Append(BitmapSubDir);
415                                 BitmapSubDirFull.Append();
417 #elif defined(__WIN32__)
419                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
420                                 DirFilenameWxS.Append(wxT("\\"));
421                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
423 #else
425                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
426                                 DirFilenameWxS.Append(wxString::FromUTF8("/"));
427                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
429 #endif
431                                 filelist.Add(DirFilenameWxS);
433                         }
435                         DirFilenameWxS.Clear();
436                         ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
438                 }
440                 for (int fi = 0; fi < filelist.GetCount(); fi++)
441                 {
442                 
443                         CreateCPPFile(dirlist[bi].wc_str(), filelist[fi].wc_str(), &cppg);
444                         fp++;
445                         
446                 }
448                 if (filelist.GetCount() > 0)
449                 {
450                 
451                         CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
452                         
453                 }
455                 filelist.Clear();
457                 wxString DirNameSplit;
459 #if defined(__HAIKU__)
461 #elif defined(__WIN32__)
463                 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
465 #else
467                 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
470 #endif
472                 while (DirListFile.HasMoreTokens()){
474                         DirNameSplit = DirListFile.GetNextToken();
476                 }
478 #if defined(__HAIKU__)
480 #elif defined(__WIN32__)
482                 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.c_str() <<
483                         ".h\"" << endl;
485 #else
487                 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
488                         ".h\"" << endl;
490 #endif
492         }
493     
494         finalhppfile << endl << "#endif" << endl;
495         finalhppfile.close();
496         ++hppg;
497     
498         // Print out the results.
500         std::cout << "Finished processing PNGs into code." << std::endl;
501         std::cout << fp << " files processed." << std::endl;
502         std::cout << cppg << " .cpp files generated." << std::endl;
503         std::cout << hppg << " .hpp files generated." << std::endl;
504     
505         // Write a success flag so that future runs won't take
506         // place thus speeding up the compilation process as
507         // required.
509         fstream bitmapflag;
511         bitmapflag.open("bitmapsdone", ios::out | ios::trunc);
512         bitmapflag << "Bitmaps as code generated. To recreate, simply delete this file and run the bitmap code generation tool again." << endl;
513         bitmapflag.close();
515         return 0;
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy