kwin Library API Documentation

embedtool.cpp

00001 /* 00002 * $Id: embedtool.cpp,v 1.5 2002/12/02 12:00:18 garbanzo Exp $ 00003 * 00004 * Keramik KWin embed tool (version 1.0) 00005 * 00006 * Copyright (C) 2002 Fredrik Höglund <fredrik@kde.org> 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the license, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; see the file COPYING. If not, write to 00020 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00021 * Boston, MA 02111-1307, USA. 00022 */ 00023 00024 #include <qimage.h> 00025 #include <qtextstream.h> 00026 #include <qregexp.h> 00027 #include <qfile.h> 00028 #include <qfileinfo.h> 00029 #include <qdatetime.h> 00030 00031 #include <iostream> 00032 00033 static int primes[] = { 00034 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 00035 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 00036 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 00037 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 00038 179, 181, 191, 193, 197, 199, 211, 223, 227, 229 00039 }; 00040 00041 struct EmbedImage { 00042 QString string; 00043 int width; 00044 int height; 00045 bool alpha; 00046 QString name; 00047 }; 00048 00049 class KeramikEmbedder { 00050 public: 00051 KeramikEmbedder(); 00052 ~KeramikEmbedder(); 00053 00054 void embed( const char * ); 00055 void writeIndex(); 00056 00057 private: 00058 QFile *file; 00059 QPtrList<EmbedImage> *index; 00060 QTextStream stream; 00061 }; 00062 00063 KeramikEmbedder::KeramikEmbedder() 00064 { 00065 QDateTime date( QDateTime::currentDateTime() ); 00066 QString datestring( date.toString() ); 00067 00068 file = new QFile( "tiles.h" ); 00069 file->open( IO_WriteOnly | IO_Truncate ); 00070 00071 stream.setDevice( file ); 00072 00073 stream << "/*\n"; 00074 stream << " * Generated by embedtool 1.0 on " << datestring << endl; 00075 stream << " */\n\n"; 00076 00077 stream << "#ifndef __TILES_H\n"; 00078 stream << "#define __TILES_H\n\n"; 00079 stream << "#include <qimage.h>\n"; 00080 stream << "#include <qdict.h>\n\n"; 00081 stream << "namespace Keramik {\n\n"; 00082 00083 index = new QPtrList<EmbedImage>; 00084 index->setAutoDelete( true ); 00085 } 00086 00087 KeramikEmbedder::~KeramikEmbedder() 00088 { 00089 stream << "} // namespace Keramik\n\n"; 00090 stream << "#endif // __TILES_H\n\n"; 00091 stream << "// vim: set noet ts=4 sw=4:\n"; 00092 00093 file->close(); 00094 delete file; 00095 delete index; 00096 } 00097 00098 void KeramikEmbedder::embed( const char *name ) 00099 { 00100 QFileInfo fileinfo( name ); 00101 QString basename( fileinfo.baseName() ); 00102 QString codename( basename ); 00103 QImage image( name ); 00104 00105 codename = codename.replace( QRegExp("[^a-zA-Z0-9]"), "_" ); 00106 00107 stream << "\tstatic const QRgb " << codename << "_data[] = {" << endl << "\t\t"; 00108 stream.setf( QTextStream::hex | QTextStream::right ); 00109 stream.fill( '0' ); 00110 00111 int pixels = image.width() * image.height(); 00112 Q_UINT32 *data = reinterpret_cast<Q_UINT32*>( image.bits() ); 00113 bool hasAlpha = false; 00114 00115 00116 for ( int i = 0, j = 0; i < pixels; i++ ) { 00117 if ( qAlpha( *data ) && qAlpha( *data ) != 0xff ) 00118 hasAlpha = true; 00119 00120 stream << "0x" << qSetW(8) << *(data++); 00121 00122 if ( i != pixels-1 ) { 00123 stream << ','; 00124 00125 if ( j++ > 4 ) { 00126 j = 0; 00127 stream << endl << "\t\t"; 00128 } else 00129 stream << ' '; 00130 } 00131 } 00132 00133 stream.reset(); 00134 00135 stream << endl << "\t}; // " << codename << "_data" << endl << endl; 00136 00137 EmbedImage *imginfo = new EmbedImage; 00138 imginfo->width = image.width(); 00139 imginfo->height = image.height(); 00140 imginfo->alpha = hasAlpha; 00141 imginfo->name = codename; 00142 imginfo->string = basename; 00143 index->append( imginfo ); 00144 } 00145 00146 void KeramikEmbedder::writeIndex() 00147 { 00148 stream << "\tstruct EmbedImage {\n"; 00149 stream << "\t\tconst char *name;\n"; 00150 stream << "\t\tint width;\n"; 00151 stream << "\t\tint height;\n"; 00152 stream << "\t\tbool alpha;\n"; 00153 stream << "\t\tconst QRgb *data;\n"; 00154 stream << "\t};\n\n"; 00155 00156 uint i = 0; 00157 stream << "\tstatic const EmbedImage image_db[] = {\n"; 00158 for ( EmbedImage *image = index->first(); image; image = index->next() ) 00159 { 00160 stream << "\t\t{ \"" << image->string << "\", " 00161 << image->width << ", " << image->height << 00162 ", " << (image->alpha ? "true" : "false") 00163 << ", " << image->name << "_data }"; 00164 if ( i++ < index->count() - 1 ) 00165 stream << ','; 00166 stream << endl; 00167 } 00168 stream << "\t};\n\n"; 00169 00170 uint prime = 0; 00171 00172 for ( i = 0; i < 50; i++ ) 00173 if ( (prime = primes[i]) >= index->count() ) 00174 break; 00175 00176 stream << "\tclass KeramikImageDb {\n"; 00177 stream << "\tprivate:\n"; 00178 stream << "\t\tstatic KeramikImageDb *m_inst;\n"; 00179 stream << "\t\tQDict<QImage> *db;\n\n"; 00180 stream << "\t\tKeramikImageDb() {\n"; 00181 stream << "\t\t\tdb = new QDict<QImage>( " << prime << " );\n"; 00182 stream << "\t\t\tdb->setAutoDelete( true );\n\n"; 00183 stream << "\t\t\tfor ( int i = 0; i < " << index->count() << "; i++ ) {\n"; 00184 stream << "\t\t\t\tQImage *img = new QImage( (uchar*)image_db[i].data,\n"; 00185 stream << "\t\t\t\t\t\timage_db[i].width, image_db[i].height,\n"; 00186 stream << "\t\t\t\t\t\t32, NULL, 0, QImage::LittleEndian );\n\n"; 00187 stream << "\t\t\t\tif ( image_db[i].alpha )\n"; 00188 stream << "\t\t\t\t\timg->setAlphaBuffer( true );\n\n"; 00189 stream << "\t\t\t\tdb->insert( image_db[i].name, img );\n"; 00190 stream << "\t\t\t}\n"; 00191 stream << "\t\t}\n\n"; 00192 stream << "\t\t~KeramikImageDb() {\n"; 00193 stream << "\t\t\tdelete db;\n"; 00194 stream << "\t\t}\n\n"; 00195 stream << "\tpublic:\n"; 00196 stream << "\t\tstatic KeramikImageDb* instance() {\n"; 00197 stream << "\t\t\tif ( ! m_inst ) m_inst = new KeramikImageDb;\n"; 00198 stream << "\t\t\treturn m_inst;\n"; 00199 stream << "\t\t}\n\n"; 00200 stream << "\t\tstatic void release() {\n"; 00201 stream << "\t\t\tif ( m_inst ) delete m_inst;\n"; 00202 stream << "\t\t\tm_inst = NULL;\n"; 00203 stream << "\t\t}\n\n"; 00204 stream << "\t\tQImage *image( const QString &name ) const {\n"; 00205 stream << "\t\t\treturn db->find( name );\n"; 00206 stream << "\t\t}\n\n"; 00207 stream << "\t}; // class KeramikImageDb\n\n"; 00208 stream << "\tKeramikImageDb *KeramikImageDb::m_inst = NULL;\n\n"; 00209 } 00210 00211 int main( int argv, char **argc ) 00212 { 00213 if ( argv < 2 ) { 00214 std::cout << "Insufficient arguments" << std::endl; 00215 return 1; 00216 } 00217 00218 KeramikEmbedder embedder; 00219 00220 for ( int i = 1; i < argv; i++ ) 00221 { 00222 std::cout << argc[i] << std::endl; 00223 embedder.embed( argc[i] ); 00224 } 00225 00226 embedder.writeIndex(); 00227 00228 return 0; 00229 } 00230 00231 // vim: set noet ts=4 sw=4: 00232
KDE Logo
This file is part of the documentation for kwin Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Dec 16 19:08:40 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003