~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to kwin/clients/keramik/embedtool.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-27 12:09:48 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527120948-dottsyd5rcwhzd36
Tags: 4:4.0.80-1ubuntu1
* Merge with Debian
 - remove 97_fix_target_link_libraries.diff
 - Add replaces/conflicts on -kde4 packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Keramik KWin embed tool (version 1.0)
3
 
 *
4
 
 * Copyright (C) 2002 Fredrik H�glund <fredrik@kde.org>
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
20
 
 */
21
 
 
22
 
#include <qimage.h>
23
 
#include <qtextstream.h>
24
 
#include <qregexp.h>
25
 
#include <qfile.h>
26
 
#include <qfileinfo.h>
27
 
#include <qdatetime.h>
28
 
 
29
 
#include <iostream>
30
 
 
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
37
 
};
38
 
 
39
 
struct EmbedImage {
40
 
        QString string;
41
 
        int width;
42
 
        int height;
43
 
        bool alpha;
44
 
        QString name;
45
 
};
46
 
 
47
 
class KeramikEmbedder {
48
 
public:
49
 
        KeramikEmbedder();
50
 
        ~KeramikEmbedder();
51
 
        
52
 
        void embed( const char * );
53
 
        void writeIndex();
54
 
        
55
 
private:
56
 
        QFile *file;
57
 
        QPtrList<EmbedImage> *index;
58
 
        QTextStream stream;
59
 
};
60
 
 
61
 
KeramikEmbedder::KeramikEmbedder()
62
 
{
63
 
        QDateTime date( QDateTime::currentDateTime() );
64
 
        QString datestring( date.toString() );
65
 
        
66
 
        file = new QFile( "tiles.h" );
67
 
        file->open( IO_WriteOnly | IO_Truncate );
68
 
 
69
 
        stream.setDevice( file );
70
 
        
71
 
        stream << "/*\n";
72
 
        stream << " * Generated by embedtool 1.0 on " << datestring << endl;
73
 
        stream << " */\n\n";
74
 
        
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";
80
 
        
81
 
        index = new QPtrList<EmbedImage>;
82
 
        index->setAutoDelete( true );
83
 
}
84
 
 
85
 
KeramikEmbedder::~KeramikEmbedder()
86
 
{       
87
 
        stream << "} // namespace Keramik\n\n";
88
 
        stream << "#endif // __TILES_H\n\n";
89
 
        stream << "// vim: set noet ts=4 sw=4:\n";
90
 
 
91
 
        file->close();
92
 
        delete file;
93
 
        delete index;
94
 
}
95
 
 
96
 
void KeramikEmbedder::embed( const char *name )
97
 
{
98
 
        QFileInfo   fileinfo( name );
99
 
        QString     basename( fileinfo.baseName() );
100
 
        QString     codename( basename );
101
 
        QImage      image( name );
102
 
        
103
 
        codename = codename.replace( QRegExp("[^a-zA-Z0-9]"), "_" );
104
 
        
105
 
        stream << "\tstatic const QRgb " << codename << "_data[] = {" << endl << "\t\t";
106
 
        stream.setf( QTextStream::hex | QTextStream::right );
107
 
        stream.fill( '0' );
108
 
        
109
 
        int pixels = image.width() * image.height();
110
 
        Q_UINT32 *data = reinterpret_cast<Q_UINT32*>( image.bits() );
111
 
        bool hasAlpha = false;
112
 
 
113
 
        
114
 
        for ( int i = 0, j = 0; i < pixels; i++ ) {
115
 
                if ( qAlpha( *data ) && qAlpha( *data ) != 0xff )
116
 
                        hasAlpha = true;
117
 
                
118
 
                stream << "0x" << qSetW(8) << *(data++);
119
 
                
120
 
                if ( i != pixels-1 ) {
121
 
                        stream << ',';
122
 
                
123
 
                        if ( j++ > 4 ) {
124
 
                                j = 0;
125
 
                                stream << endl << "\t\t";
126
 
                        } else
127
 
                                stream << ' ';
128
 
                }
129
 
        }
130
 
 
131
 
        stream.reset();
132
 
        
133
 
        stream << endl << "\t}; // " << codename << "_data" << endl << endl;
134
 
 
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 ); 
142
 
}
143
 
 
144
 
void KeramikEmbedder::writeIndex()
145
 
{
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";
153
 
 
154
 
        uint i = 0;
155
 
        stream << "\tstatic const EmbedImage image_db[] = {\n";
156
 
        for ( EmbedImage *image = index->first(); image; image = index->next() )
157
 
        {
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 )
163
 
                        stream << ',';
164
 
                stream << endl;
165
 
        }
166
 
        stream << "\t};\n\n";
167
 
 
168
 
        uint prime = 0;
169
 
        
170
 
        for ( i = 0; i < 50; i++ )
171
 
                if ( (prime = primes[i]) >= index->count() )
172
 
                        break;
173
 
        
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";
207
 
}
208
 
 
209
 
int main( int argv, char **argc )
210
 
{
211
 
        if ( argv < 2 ) {
212
 
                std::cout << "Insufficient arguments" << std::endl;
213
 
                return 1;
214
 
        }
215
 
 
216
 
        KeramikEmbedder embedder;
217
 
 
218
 
        for ( int i = 1; i < argv; i++ )
219
 
        {
220
 
                std::cout << argc[i] << std::endl;
221
 
                embedder.embed( argc[i] );
222
 
        }
223
 
 
224
 
        embedder.writeIndex();
225
 
        
226
 
        return 0;
227
 
}
228
 
 
229
 
// vim: set noet ts=4 sw=4:
230