2
* Keramik KWin embed tool (version 1.0)
4
* Copyright (C) 2002 Fredrik H�glund <fredrik@kde.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the license, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; see the file COPYING. If not, write to
18
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
* Boston, MA 02110-1301, USA.
23
#include <qtextstream.h>
26
#include <qfileinfo.h>
27
#include <qdatetime.h>
31
static int primes[] = {
32
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
33
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
34
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
35
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
36
179, 181, 191, 193, 197, 199, 211, 223, 227, 229
47
class KeramikEmbedder {
52
void embed( const char * );
57
QPtrList<EmbedImage> *index;
61
KeramikEmbedder::KeramikEmbedder()
63
QDateTime date( QDateTime::currentDateTime() );
64
QString datestring( date.toString() );
66
file = new QFile( "tiles.h" );
67
file->open( IO_WriteOnly | IO_Truncate );
69
stream.setDevice( file );
72
stream << " * Generated by embedtool 1.0 on " << datestring << endl;
75
stream << "#ifndef __TILES_H\n";
76
stream << "#define __TILES_H\n\n";
77
stream << "#include <qimage.h>\n";
78
stream << "#include <qdict.h>\n\n";
79
stream << "namespace Keramik {\n\n";
81
index = new QPtrList<EmbedImage>;
82
index->setAutoDelete( true );
85
KeramikEmbedder::~KeramikEmbedder()
87
stream << "} // namespace Keramik\n\n";
88
stream << "#endif // __TILES_H\n\n";
89
stream << "// vim: set noet ts=4 sw=4:\n";
96
void KeramikEmbedder::embed( const char *name )
98
QFileInfo fileinfo( name );
99
QString basename( fileinfo.baseName() );
100
QString codename( basename );
101
QImage image( name );
103
codename = codename.replace( QRegExp("[^a-zA-Z0-9]"), "_" );
105
stream << "\tstatic const QRgb " << codename << "_data[] = {" << endl << "\t\t";
106
stream.setf( QTextStream::hex | QTextStream::right );
109
int pixels = image.width() * image.height();
110
Q_UINT32 *data = reinterpret_cast<Q_UINT32*>( image.bits() );
111
bool hasAlpha = false;
114
for ( int i = 0, j = 0; i < pixels; i++ ) {
115
if ( qAlpha( *data ) && qAlpha( *data ) != 0xff )
118
stream << "0x" << qSetW(8) << *(data++);
120
if ( i != pixels-1 ) {
125
stream << endl << "\t\t";
133
stream << endl << "\t}; // " << codename << "_data" << endl << endl;
135
EmbedImage *imginfo = new EmbedImage;
136
imginfo->width = image.width();
137
imginfo->height = image.height();
138
imginfo->alpha = hasAlpha;
139
imginfo->name = codename;
140
imginfo->string = basename;
141
index->append( imginfo );
144
void KeramikEmbedder::writeIndex()
146
stream << "\tstruct EmbedImage {\n";
147
stream << "\t\tconst char *name;\n";
148
stream << "\t\tint width;\n";
149
stream << "\t\tint height;\n";
150
stream << "\t\tbool alpha;\n";
151
stream << "\t\tconst QRgb *data;\n";
152
stream << "\t};\n\n";
155
stream << "\tstatic const EmbedImage image_db[] = {\n";
156
for ( EmbedImage *image = index->first(); image; image = index->next() )
158
stream << "\t\t{ \"" << image->string << "\", "
159
<< image->width << ", " << image->height <<
160
", " << (image->alpha ? "true" : "false")
161
<< ", " << image->name << "_data }";
162
if ( i++ < index->count() - 1 )
166
stream << "\t};\n\n";
170
for ( i = 0; i < 50; i++ )
171
if ( (prime = primes[i]) >= index->count() )
174
stream << "\tclass KeramikImageDb {\n";
175
stream << "\tprivate:\n";
176
stream << "\t\tstatic KeramikImageDb *m_inst;\n";
177
stream << "\t\tQDict<QImage> *db;\n\n";
178
stream << "\t\tKeramikImageDb() {\n";
179
stream << "\t\t\tdb = new QDict<QImage>( " << prime << " );\n";
180
stream << "\t\t\tdb->setAutoDelete( true );\n\n";
181
stream << "\t\t\tfor ( int i = 0; i < " << index->count() << "; i++ ) {\n";
182
stream << "\t\t\t\tQImage *img = new QImage( (uchar*)image_db[i].data,\n";
183
stream << "\t\t\t\t\t\timage_db[i].width, image_db[i].height,\n";
184
stream << "\t\t\t\t\t\t32, NULL, 0, QImage::LittleEndian );\n\n";
185
stream << "\t\t\t\tif ( image_db[i].alpha )\n";
186
stream << "\t\t\t\t\timg->setAlphaBuffer( true );\n\n";
187
stream << "\t\t\t\tdb->insert( image_db[i].name, img );\n";
188
stream << "\t\t\t}\n";
189
stream << "\t\t}\n\n";
190
stream << "\t\t~KeramikImageDb() {\n";
191
stream << "\t\t\tdelete db;\n";
192
stream << "\t\t}\n\n";
193
stream << "\tpublic:\n";
194
stream << "\t\tstatic KeramikImageDb* instance() {\n";
195
stream << "\t\t\tif ( ! m_inst ) m_inst = new KeramikImageDb;\n";
196
stream << "\t\t\treturn m_inst;\n";
197
stream << "\t\t}\n\n";
198
stream << "\t\tstatic void release() {\n";
199
stream << "\t\t\tif ( m_inst ) delete m_inst;\n";
200
stream << "\t\t\tm_inst = NULL;\n";
201
stream << "\t\t}\n\n";
202
stream << "\t\tQImage *image( const QString &name ) const {\n";
203
stream << "\t\t\treturn db->find( name );\n";
204
stream << "\t\t}\n\n";
205
stream << "\t}; // class KeramikImageDb\n\n";
206
stream << "\tKeramikImageDb *KeramikImageDb::m_inst = NULL;\n\n";
209
int main( int argv, char **argc )
212
std::cout << "Insufficient arguments" << std::endl;
216
KeramikEmbedder embedder;
218
for ( int i = 1; i < argv; i++ )
220
std::cout << argc[i] << std::endl;
221
embedder.embed( argc[i] );
224
embedder.writeIndex();
229
// vim: set noet ts=4 sw=4: