~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kstyles/keramik/genembed.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
A small utility to generate embedded images for Keramik, especially structured for easy recoloring...
 
3
 
 
4
Copyright (c) 2002, 2005 Maksim Orlovich <maksim@kde.org>
 
5
 
 
6
Permission is hereby granted, free of charge, to any person obtaining a
 
7
copy of this software and associated documentation files (the "Software"),
 
8
to deal in the Software without restriction, including without limitation
 
9
the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
10
and/or sell copies of the Software, and to permit persons to whom the
 
11
Software is furnished to do so, subject to the following conditions:
 
12
 
 
13
The above copyright notice and this permission notice shall be included in
 
14
all copies or substantial portions of the Software.
 
15
 
 
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
19
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
21
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
22
DEALINGS IN THE SOFTWARE.
 
23
 
 
24
*/
 
25
 
 
26
#include <QtCore/QCoreApplication>
 
27
#include <QtGui/QColor>
 
28
#include <QtCore/QDataStream>
 
29
#include <QtCore/QFile>
 
30
#include <QtCore/QFileInfo>
 
31
#include <QtGui/QImage>
 
32
#include <QtCore/QMap>
 
33
#include <QtCore/QTextStream>
 
34
#include <QtCore/QVector>
 
35
 
 
36
#include <qimageblitz.h>
 
37
 
 
38
#include <iostream>
 
39
using namespace std;
 
40
 
 
41
#include <string.h>
 
42
#include <math.h>
 
43
 
 
44
//NOTE: Use of old-style header is intentional for portability. See revisions 1.6 and 1.7
 
45
 
 
46
//Force-touch-embedded-revision: 2
 
47
 
 
48
#include "keramikimage.h"
 
49
 
 
50
/**
 
51
Need to generate something like this:
 
52
TargetColorAlpha, GreyAdd, SrcAlpha;
 
53
 
 
54
so that one can do (R*T+GreyAdd, G*T+GreyAdd, B*T+GreyAdd, SrcAlpha) as pixel values
 
55
*/
 
56
 
 
57
 
 
58
int evalSuffix(const QString &suffix)
 
59
{
 
60
        if (suffix == "-tl")
 
61
                return 0;
 
62
 
 
63
        if (suffix == "-tc")
 
64
                return 1;
 
65
 
 
66
        if (suffix == "-tr")
 
67
                return 2;
 
68
 
 
69
        if (suffix == "-cl")
 
70
                return 3;
 
71
 
 
72
        if (suffix == "-cc")
 
73
                return 4;
 
74
 
 
75
        if (suffix == "-cr")
 
76
                return 5;
 
77
 
 
78
        if (suffix == "-bl")
 
79
                return 6;
 
80
 
 
81
        if (suffix == "-bc")
 
82
                return 7;
 
83
 
 
84
        if (suffix == "-br")
 
85
                return 8;
 
86
 
 
87
        if (suffix == "-separator")
 
88
                return KeramikTileSeparator;
 
89
 
 
90
        if (suffix == "-slider1")
 
91
                return KeramikSlider1;
 
92
 
 
93
        if (suffix == "-slider2")
 
94
                return KeramikSlider2;
 
95
 
 
96
        if (suffix == "-slider3")
 
97
                return KeramikSlider3;
 
98
 
 
99
        if (suffix == "-slider4")
 
100
                return KeramikSlider4;
 
101
 
 
102
        if (suffix == "-groove1")
 
103
                return KeramikGroove1;
 
104
 
 
105
        if (suffix == "-groove2")
 
106
                return KeramikGroove2;
 
107
 
 
108
        if (suffix == "-1")
 
109
                return 1;
 
110
 
 
111
        if (suffix == "-2")
 
112
                return 2;
 
113
 
 
114
        if (suffix == "-3")
 
115
                return 3;
 
116
 
 
117
        return -1;
 
118
}
 
119
 
 
120
 
 
121
int main(int argc, char** argv)
 
122
{
 
123
        if (argc < 2)
 
124
                return 0;
 
125
 
 
126
        QCoreApplication qapp(argc, argv);
 
127
        QVector<KeramikEmbedImage> images;
 
128
 
 
129
        QStringList imageList;
 
130
        if (argc == 3 && (strcmp(argv[1], "--file")==0)) {
 
131
                QFile f( argv[2] );
 
132
                if (!f.open(QIODevice::ReadOnly))
 
133
                        return 0;
 
134
                QByteArray ba = f.readLine();
 
135
                while (!ba.isEmpty()) {
 
136
                        imageList += ba.trimmed();
 
137
                        ba = f.readLine();
 
138
                }
 
139
                f.close();
 
140
        } else {
 
141
                for (int c = 1; c<argc; c++)
 
142
                        imageList += argv[c];
 
143
        }
 
144
 
 
145
        cout<<"#include <QHash>\n\n";
 
146
        cout<<"#include \"keramikimage.h\"\n\n";
 
147
 
 
148
        QMap<QString, int> assignID;
 
149
        int nextID = 0;
 
150
 
 
151
        for(QStringList::iterator it = imageList.begin(); it != imageList.end(); ++it)
 
152
        {
 
153
                QImage input((*it));
 
154
                input = input.convertToFormat( QImage::Format_ARGB32 );
 
155
 
 
156
                QFileInfo fi((*it));
 
157
                QString s = fi.baseName();
 
158
 
 
159
                KeramikEmbedImage image;
 
160
 
 
161
                int pos;
 
162
 
 
163
                QString id = s;
 
164
 
 
165
                int readJustID = 0;
 
166
 
 
167
 
 
168
                if ((pos = s.lastIndexOf("-")) != -1)
 
169
                {
 
170
                                int suffix = evalSuffix(s.mid(pos));
 
171
                                if (suffix !=-1 )
 
172
                                {
 
173
                                                id = s.mid(0,pos);
 
174
                                                readJustID = suffix;
 
175
                                }
 
176
                }
 
177
 
 
178
                if (!assignID.contains(id))
 
179
                {
 
180
                        assignID[id] = nextID;
 
181
                        nextID += 256;
 
182
                }
 
183
 
 
184
                s.replace('-','_');
 
185
 
 
186
 
 
187
                if (s.contains("button"))
 
188
                        Blitz::contrast(input, true);
 
189
 
 
190
                int fullID = assignID[id] + readJustID;//Subwidget..
 
191
 
 
192
                bool highlights = true;
 
193
                bool shadows  = true;
 
194
 
 
195
                float gamma    = 1.0;
 
196
                int brightAdj = 0;
 
197
 
 
198
 
 
199
 
 
200
                if (s.contains("toolbar") || s.contains("tab-top-active") || s.contains("menubar") )
 
201
                {
 
202
//                      highlights = false;
 
203
                        gamma    = 1/1.25f;
 
204
                        //brightAdj = 10;
 
205
                        shadows = false;
 
206
                }
 
207
 
 
208
                if (s.contains("scrollbar") && s.contains("groove"))
 
209
                {
 
210
                        //highlights = false;
 
211
                        //gamma = 1.5;
 
212
                        shadows = false;
 
213
                }
 
214
                        //brightAdj = -10;
 
215
 
 
216
                if (s.contains("scrollbar") && s.contains("slider"))
 
217
                {
 
218
                        //highlights = false;
 
219
                        gamma =1/0.7f;
 
220
                        //shadows = false;
 
221
                }
 
222
 
 
223
 
 
224
                if (s.contains("menuitem"))
 
225
                {
 
226
                        //highlights = false;
 
227
                        gamma =1/0.6f;
 
228
                        //shadows = false;
 
229
                }
 
230
 
 
231
                image.width   = input.width();
 
232
                image.height = input.height();
 
233
                image.id         = fullID;
 
234
                image.data     = reinterpret_cast<unsigned char*>(strdup(s.toLatin1()));
 
235
 
 
236
 
 
237
                bool reallySolid = true;
 
238
 
 
239
                int pixCount = 0;
 
240
                int pixSolid = 0;
 
241
 
 
242
                cout<<"static const unsigned char "<<qPrintable(s)<<"[]={\n";
 
243
 
 
244
                quint32* read  = reinterpret_cast< quint32* >(input.bits() );
 
245
                int size = input.width()*input.height();
 
246
 
 
247
                for (int pos=0; pos<size; pos++)
 
248
                {
 
249
                        QRgb basePix = (QRgb)*read;
 
250
 
 
251
                        if (qAlpha(basePix) != 255)
 
252
                                reallySolid = false;
 
253
                        else
 
254
                                pixSolid++;
 
255
 
 
256
                        pixCount++;
 
257
                        read++;
 
258
                }
 
259
 
 
260
                image.haveAlpha = !reallySolid;
 
261
 
 
262
                images.push_back(image);
 
263
 
 
264
                read  = reinterpret_cast< quint32* >(input.bits() );
 
265
                for (int pos=0; pos<size; pos++)
 
266
                {
 
267
                        QRgb basePix = (QRgb)*read;
 
268
                        //cout<<(r*destAlpha.alphas[pos])<<"\n";
 
269
                        //cout<<(int)destAlpha.alphas[pos]<<"\n";
 
270
                        QColor clr(basePix);
 
271
                        int h,s,v;
 
272
                        clr.getHsv(&h,&s,&v);
 
273
 
 
274
                        v=qGray(basePix);
 
275
 
 
276
                        int targetColorAlpha = 0 , greyAdd = 0;
 
277
                        //int srcAlpha = qAlpha(basePix);
 
278
 
 
279
                        if (s>0 || v > 128)
 
280
                        { //Non-shadow
 
281
                                float fv = v/255.0;
 
282
                                fv = pow(fv, gamma);
 
283
                                v = int(255.5*fv);
 
284
 
 
285
 
 
286
                                if (s<17 && highlights) //A bit of a highligt..
 
287
                                {
 
288
                                        float effectPortion = (16 - s)/15.0;
 
289
 
 
290
                                        greyAdd             = (int)(v/4.0 * effectPortion*1.2);
 
291
                                        targetColorAlpha = v - greyAdd;
 
292
                                }
 
293
                                else
 
294
                                {
 
295
                                        targetColorAlpha = v;//(int)(fv*255);
 
296
                                        greyAdd              = 0;
 
297
                                }
 
298
                        }
 
299
                        else
 
300
                        {
 
301
                                if (shadows)
 
302
                                {
 
303
                                        targetColorAlpha = 0;
 
304
                                        greyAdd              = v;
 
305
                                }
 
306
                                else
 
307
                                {
 
308
                                        targetColorAlpha = v;//(int)(fv*255);
 
309
                                        greyAdd              = 0;
 
310
                                }
 
311
                        }
 
312
 
 
313
                        greyAdd+=brightAdj;
 
314
 
 
315
                        if (reallySolid)
 
316
                                cout<<targetColorAlpha<<","<<greyAdd<<",";
 
317
                        else
 
318
                                cout<<targetColorAlpha<<","<<greyAdd<<","<<qAlpha(basePix)<<",";
 
319
                        //cout<<qRed(basePix)<<","<<qGreen(basePix)<<","<<qBlue(basePix)<<","<<qAlpha(basePix)<<",";
 
320
 
 
321
                        if (pos%8 == 7)
 
322
                                cout<<"\n";
 
323
 
 
324
                        read++;
 
325
                }
 
326
 
 
327
                //cerr<<qPrintable(s)<<":"<<pixSolid<<"/"<<pixCount<<"("<<reallySolid<<")\n";
 
328
 
 
329
                cout<<!reallySolid<<"\n";
 
330
 
 
331
                cout<<"};\n\n";
 
332
        }
 
333
 
 
334
        cout<<"static const KeramikEmbedImage  image_db[] = {\n";
 
335
 
 
336
        for (int c=0; c<images.size(); c++)
 
337
        {
 
338
                cout<<"\t{ "<<(images[c].haveAlpha?"true":"false")<<","<<images[c].width<<", "<<images[c].height<<", "<<images[c].id<<", "<<(char *)images[c].data<<"},";
 
339
                cout<<"\n";
 
340
        }
 
341
        cout<<"\t{0, 0, 0, 0, 0}\n";
 
342
        cout<<"};\n\n";
 
343
 
 
344
        cout<<"class KeramikImageDb\n";
 
345
        cout<<"{\n";
 
346
        cout<<"public:\n";
 
347
        cout<<"\tstatic KeramikImageDb* getInstance()\n";
 
348
        cout<<"\t{\n";
 
349
        cout<<"\t\tif (!instance) instance = new KeramikImageDb;\n";
 
350
        cout<<"\t\treturn instance;\n";
 
351
        cout<<"\t}\n\n";
 
352
        cout<<"\tstatic void release()\n";
 
353
        cout<<"\t{\n";
 
354
        cout<<"\t\tdelete instance;\n";
 
355
        cout<<"\t\tinstance=0;\n";
 
356
        cout<<"\t}\n\n";
 
357
        cout<<"\tconst KeramikEmbedImage* getImage(int id)\n";
 
358
        cout<<"\t{\n";
 
359
        cout<<"\t\treturn images[id];\n";
 
360
        cout<<"\t}\n\n";
 
361
        cout<<"private:\n";
 
362
        cout<<"\tKeramikImageDb()\n";
 
363
        cout<<"\t{\n";
 
364
        cout<<"\t\timages.reserve(503);";
 
365
        cout<<"\t\tfor (int c=0; image_db[c].width; c++)\n";
 
366
        cout<<"\t\t\timages.insert(image_db[c].id, &image_db[c]);\n";
 
367
        cout<<"\t}\n";
 
368
        cout<<"\tstatic KeramikImageDb* instance;\n";
 
369
        cout<<"\tQHash<int, const KeramikEmbedImage*> images;\n";
 
370
        cout<<"};\n\n";
 
371
        cout<<"KeramikImageDb* KeramikImageDb::instance = 0;\n\n";
 
372
 
 
373
        cout<<"const KeramikEmbedImage* KeramikGetDbImage(int id)\n";
 
374
        cout<<"{\n";
 
375
        cout<<"\treturn KeramikImageDb::getInstance()->getImage(id);\n";
 
376
        cout<<"}\n\n";
 
377
 
 
378
        cout<<"void KeramikDbCleanup()\n";
 
379
        cout<<"{\n";
 
380
        cout<<"\t\tKeramikImageDb::release();\n";
 
381
        cout<<"}\n";
 
382
        cout.flush();
 
383
 
 
384
 
 
385
        QFile file("keramikrc.h");
 
386
        file.open(QIODevice::WriteOnly);
 
387
        QTextStream ts( &file);
 
388
        ts<<"#ifndef KERAMIK_RC_H\n";
 
389
        ts<<"#define KERAMIK_RC_H\n";
 
390
 
 
391
        ts<<"enum KeramikWidget {\n";
 
392
        for (QMap<QString, int>::iterator i = assignID.begin(); i != assignID.end(); ++i)
 
393
        {
 
394
                QString name = "keramik_"+i.key();
 
395
                name.replace('-','_');
 
396
                ts<<"\t"<<name<<" = "<<i.value()<<",\n";
 
397
        }
 
398
        ts<<"\tkeramik_last\n";
 
399
        ts<<"};\n";
 
400
 
 
401
        ts<<"#endif\n";
 
402
        file.close();
 
403
 
 
404
        return 0;
 
405
}
 
406
 
 
407
// vim: ts=4 sw=4 noet