~ubuntu-branches/ubuntu/maverick/qgo/maverick

« back to all changes in this revision

Viewing changes to src/imagehandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin A. Godisch
  • Date: 2005-01-01 23:07:10 UTC
  • Revision ID: james.westby@ubuntu.com-20050101230710-fhng6yidm47xlb2i
Tags: upstream-1.0.0-r2
ImportĀ upstreamĀ versionĀ 1.0.0-r2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
*
 
3
* imagehandler.cpp
 
4
*
 
5
* the stone rendering part has been coded by Marin Ferecatu - thanks, good job!
 
6
*
 
7
*/
 
8
 
 
9
#include "imagehandler.h"
 
10
#include "icons.h"
 
11
#include "qglobal.h"
 
12
#include "setting.h"
 
13
#include <qpixmap.h>
 
14
//#include <iostream>
 
15
 
 
16
#ifdef USE_XPM
 
17
#include WOOD_PIC
 
18
#include WOOD2_PIC
 
19
#include WOOD3_PIC
 
20
#include WOOD4_PIC
 
21
#include WOOD5_PIC
 
22
#include TABLE_PIC
 
23
#include ALT_GHOST_BLACK
 
24
#include ALT_GHOST_WHITE
 
25
#endif
 
26
 
 
27
#ifdef Q_WS_WIN
 
28
 #define M_PI 3.141592653
 
29
 double drand48() { return rand()*1.0/RAND_MAX; }
 
30
#endif
 
31
 
 
32
 
 
33
/*
 
34
* Static class variables
 
35
*/
 
36
QPixmap* ImageHandler::woodPixmap1 = NULL;
 
37
QPixmap* ImageHandler::woodPixmap2 = NULL;
 
38
QPixmap* ImageHandler::woodPixmap3 = NULL;
 
39
QPixmap* ImageHandler::woodPixmap4 = NULL;
 
40
QPixmap* ImageHandler::woodPixmap5 = NULL;
 
41
QPixmap* ImageHandler::tablePixmap = NULL;
 
42
QCanvasPixmapArray* ImageHandler::altGhostPixmaps = NULL;
 
43
int ImageHandler::classCounter = 0;
 
44
int ImageHandler::antialiasingColor = 0;
 
45
int ImageHandler::ac1 = 0;
 
46
int ImageHandler::ac2 = 0;
 
47
int ImageHandler::ac3 = 0;
 
48
int ImageHandler::ac4 = 0;
 
49
int ImageHandler::ac5 = 0;
 
50
 
 
51
/**
 
52
* Stone rendering code
 
53
* Heavily inspired from Jago and CGoban code
 
54
* http://www.igoweb.org/~wms/comp/cgoban
 
55
* /http://www.rene-grothmann.de/jago/
 
56
**/
 
57
 
 
58
void ImageHandler::icopy(int *im, QImage &qim, int w, int h) {
 
59
        
 
60
        for (int y = 0; y < h; y++) {
 
61
                uint *p = (uint *)qim.scanLine(y);
 
62
                for(int x = 0; x < w; x++) {
 
63
                        p[x] = im[y*h + x];
 
64
                }
 
65
        }
 
66
}
 
67
 
 
68
 
 
69
void ImageHandler::decideAppearance(WhiteDesc *desc, int size)  {
 
70
        double  minStripeW, maxStripeW, theta;
 
71
        
 
72
        minStripeW = (double)size / 20.0;
 
73
        if (minStripeW < 2.5)
 
74
                minStripeW = 2.5;
 
75
        maxStripeW = (double)size / 5.0;
 
76
        if (maxStripeW < 4.0)
 
77
                maxStripeW = 4.0;
 
78
        
 
79
        theta = drand48() * 2.0 * M_PI;
 
80
        desc->cosTheta = cos(theta);
 
81
        desc->sinTheta = sin(theta);
 
82
        desc->stripeWidth = 1.5*minStripeW +
 
83
                (drand48() * (maxStripeW - minStripeW));
 
84
        
 
85
        desc->xAdd = 3*desc->stripeWidth +
 
86
                (double)size * 3.0;
 
87
        
 
88
        desc->stripeMul = 3.0;
 
89
        desc->zMul = drand48() * 650.0 + 70.0;
 
90
}
 
91
 
 
92
 
 
93
double ImageHandler::getStripe(WhiteDesc &white, double bright, double z, int x, int y) {
 
94
        double wBright;
 
95
        
 
96
        bright = bright/256.0;
 
97
        
 
98
        double wStripeLoc = x*white.cosTheta - y*white.sinTheta +       white.xAdd;
 
99
        double wStripeColor = fmod(wStripeLoc + (z * z * z * white.zMul) *
 
100
                white.stripeWidth,
 
101
                white.stripeWidth) / white.stripeWidth;
 
102
        wStripeColor = wStripeColor * white.stripeMul - 0.5;
 
103
        if (wStripeColor < 0.0)
 
104
                wStripeColor = -2.0 * wStripeColor;
 
105
        if (wStripeColor > 1.0)
 
106
                wStripeColor = 1.0;
 
107
        wStripeColor = wStripeColor * 0.15 + 0.85;
 
108
        
 
109
        wBright = bright*wStripeColor;
 
110
        
 
111
        if (wBright > 255)
 
112
                wBright = 255;
 
113
        if (wBright < 0)
 
114
                wBright = 0;
 
115
        
 
116
        return wBright*255;
 
117
}
 
118
 
 
119
void ImageHandler::paintBlackStone (QImage &bi, int d) {
 
120
        
 
121
        const double pixel=0.8,shadow=0.99;
 
122
        // board color
 
123
        int col = antialiasingColor; //0xecb164;
 
124
        int blue=col&0x0000FF,green=(col&0x00FF00)>>8,red=(col&0xFF0000)>>16;
 
125
        
 
126
        bool Alias=true;
 
127
        
 
128
        // these are the images
 
129
        int *pb=new int[d*d];
 
130
        int i, j, g, k;
 
131
        double di, dj, d2=(double)d/2.0-5e-1, r=d2-2e-1, f=sqrt(3);
 
132
        double x, y, z, xr, xg, hh;
 
133
        
 
134
        k=0;
 
135
        
 
136
        bool smallerstones = false;
 
137
        if (smallerstones) r-=1;
 
138
        
 
139
        for (i=0; i<d; i++)
 
140
                for (j=0; j<d; j++) {
 
141
                        di=i-d2; dj=j-d2;
 
142
                        hh=r-sqrt(di*di+dj*dj);
 
143
                        if (hh>=0) {
 
144
                                z=r*r-di*di-dj*dj;
 
145
                                if (z>0) z=sqrt(z)*f;
 
146
                                else z=0;
 
147
                                x=di; y=dj;
 
148
                                xr=sqrt(6*(x*x+y*y+z*z));
 
149
                                xr=(2*z-x+y)/xr;
 
150
                                if (xr>0.9) xg=(xr-0.9)*10;
 
151
                                else xg=0;
 
152
                                if (hh>pixel || !Alias) {
 
153
                                        g=(int)(10+10*xr+xg*140);
 
154
                                        pb[k]=(255<<24)|(g<<16)|(g<<8)|g;
 
155
                                }
 
156
                                else {
 
157
                                        hh=(pixel-hh)/pixel;
 
158
                                        g=(int)(10+10*xr+xg*140);
 
159
                                        double shade;
 
160
                                        if (di-dj<r/3) shade=1;
 
161
                                        else shade=shadow;
 
162
                                        pb[k]=((255<<24)
 
163
                                                |(((int)((1-hh)*g+hh*shade*red))<<16)
 
164
                                                |(((int)((1-hh)*g+hh*shade*green))<<8)
 
165
                                                |((int)((1-hh)*g+hh*shade*blue)));
 
166
                                }
 
167
                        }
 
168
                        else pb[k]=0;
 
169
                        k++;
 
170
                        
 
171
                }
 
172
                
 
173
                // now copy the result in QImages
 
174
                icopy(pb, bi, d, d);
 
175
                
 
176
                // free memory
 
177
                delete[] pb;
 
178
}
 
179
 
 
180
// non shaded white stones
 
181
void ImageHandler::paintWhiteStone1 (QImage &wi, int d, bool stripes) {
 
182
        
 
183
        WhiteDesc desc;
 
184
        decideAppearance(&desc, d);
 
185
        
 
186
        const double pixel=0.8,shadow=0.99;
 
187
        // board color
 
188
        int col = antialiasingColor; //0xecb164;
 
189
        int blue=col&0x0000FF,green=(col&0x00FF00)>>8,red=(col&0xFF0000)>>16;
 
190
        
 
191
        bool Alias=true;
 
192
        
 
193
        // these are the images
 
194
        int *pw=new int[d*d];
 
195
        int i, j, g, k;
 
196
        double di, dj, d2=(double)d/2.0-5e-1, r=d2-2e-1, f=sqrt(3);
 
197
        double x, y, z, xr, xg, hh;
 
198
        
 
199
        k=0;
 
200
        
 
201
        bool smallerstones = false;
 
202
        if (smallerstones) r-=1;
 
203
        
 
204
        for (i=0; i<d; i++)
 
205
                for (j=0; j<d; j++) {
 
206
                        di=i-d2; dj=j-d2;
 
207
                        hh=r-sqrt(di*di+dj*dj);
 
208
                        if (hh>=0) {
 
209
                                z=r*r-di*di-dj*dj;
 
210
                                if (z>0) z=sqrt(z)*f;
 
211
                                else z=0;
 
212
                                x=di; y=dj;
 
213
                                xr=sqrt(6*(x*x+y*y+z*z));
 
214
                                xr=(2*z-x+y)/xr;
 
215
                                if (xr>0.9) xg=(xr-0.9)*10;
 
216
                                else xg=0;
 
217
                                if (hh>pixel || !Alias) {
 
218
                                        g=(int)(200+10*xr+xg*45);
 
219
                                        if (stripes)
 
220
                                                g = (int)getStripe(desc, g, xr/7.0, i, j);
 
221
                                        pw[k]=(255<<24)|(g<<16)|((g)<<8)|(g);
 
222
                                }
 
223
                                else {
 
224
                                        hh=(pixel-hh)/pixel;
 
225
                                        //g=(int)(10+10*xr+xg*140);
 
226
                                        double shade;
 
227
                                        if (di-dj<r/3) shade=1;
 
228
                                        else shade=shadow;
 
229
                                        g=(int)(200+10*xr+xg*45);
 
230
                                        if (stripes)
 
231
                                                g = (int)getStripe(desc, g, xr/7.0, i, j);
 
232
                                        pw[k]=((255<<24)
 
233
                                                |(((int)((1-hh)*g+hh*shade*red))<<16)
 
234
                                                |(((int)((1-hh)*g+hh*shade*green))<<8)
 
235
                                                |((int)((1-hh)*g+hh*shade*blue)));
 
236
                                }
 
237
                        }
 
238
                        else pw[k]=0;
 
239
                        k++;
 
240
                        
 
241
                }
 
242
                
 
243
                // now copy the result in QImages
 
244
                icopy(pw, wi, d, d);
 
245
                
 
246
                // free memory
 
247
                delete[] pw;
 
248
}
 
249
 
 
250
// shaded white stones
 
251
void ImageHandler::paintWhiteStone2 (QImage &wi, int d, bool stripes) {
 
252
 
 
253
        WhiteDesc desc;
 
254
        decideAppearance(&desc, d);
 
255
 
 
256
        // the angle from which the shadow starts (measured to the light direction = pi/4)
 
257
        // alpha should be in (0, pi)
 
258
        const double ALPHA = M_PI/4;
 
259
        // how big the shadow is (should be < d/2)
 
260
        const double STRIPE = d/5.0;
 
261
 
 
262
        double theta;
 
263
        const double pixel=0.8, shadow=0.99;
 
264
        // board color
 
265
        int col = antialiasingColor; //0xecb164;
 
266
        int blue=col&0x0000FF,green=(col&0x00FF00)>>8,red=(col&0xFF0000)>>16;
 
267
 
 
268
        bool Alias=true;
 
269
        
 
270
        // these are the images
 
271
        int *pw=new int[d*d];
 
272
        int i, j, g, k;
 
273
        double di, dj, d2=(double)d/2.0-5e-1, r=d2-2e-1, f=sqrt(3);
 
274
        double x, y, z, xr, xg, hh;
 
275
        
 
276
        k=0;
 
277
        
 
278
        bool smallerstones = false;
 
279
        if (smallerstones) r-=1;
 
280
 
 
281
        for (i=0; i<d; i++)
 
282
                for (j=0; j<d; j++) {
 
283
                        di=i-d2; dj=j-d2;
 
284
                        hh=r-sqrt(di*di+dj*dj);
 
285
                        if (hh>=0) {
 
286
                                z=r*r-di*di-dj*dj;
 
287
                                if (z>0) z=sqrt(z)*f;
 
288
                                else z=0;
 
289
                                x=di; y=dj;
 
290
                                xr=sqrt(6*(x*x+y*y+z*z));
 
291
                                xr=(2*z-x+y)/xr;
 
292
                                if (xr>0.9) xg=(xr-0.9)*10;
 
293
                                else xg=0;
 
294
 
 
295
                                theta = atan2(double(j-d/2), double(i-d/2)) + M_PI - M_PI/4 + M_PI/2;
 
296
                                bool stripeband = theta > ALPHA && theta < 2*M_PI-ALPHA;
 
297
 
 
298
                                if (theta > M_PI)
 
299
                                        theta = (2*M_PI-theta);
 
300
 
 
301
                                double stripe = STRIPE*sin((M_PI/2)*(theta-ALPHA)/(M_PI-ALPHA));
 
302
                                if (stripe < 1) stripe = 1;
 
303
                                
 
304
                                double gmin=(int)(0+10*xr+xg*45);
 
305
                                double gmax=(int)(200+10*xr+xg*45);
 
306
                                gmin = gmax - (gmax-gmin)*(1-exp(-1*(theta-ALPHA)/(M_PI-ALPHA)));
 
307
                                
 
308
                                if (hh < STRIPE && hh > pixel && stripeband) {
 
309
                                        
 
310
                                        if (hh > stripe) g = (int)gmax;
 
311
                                        else g = int(gmin + (gmax-gmin)*(hh/stripe));
 
312
                                        
 
313
                                        if (stripes)
 
314
                                                g = (int)getStripe(desc, g, xr/7.0, i, j);
 
315
                                        pw[k]=(255<<24)|(g<<16)|((g)<<8)|(g);
 
316
                                }
 
317
                                else if (hh>pixel || !Alias) {
 
318
                                        g=(int)(200+10*xr+xg*45);
 
319
                                        if (stripes)
 
320
                                                g = (int)getStripe(desc, g, xr/7.0, i, j);
 
321
                                        pw[k]=(255<<24)|(g<<16)|((g)<<8)|(g);
 
322
                                }
 
323
                                else {
 
324
                                        hh=(pixel-hh)/pixel;
 
325
                                        //g=(int)(10+10*xr+xg*140);
 
326
                                        double shade;
 
327
                                        if (di-dj<r/3) shade=1;
 
328
                                        else shade=shadow;
 
329
                                        if(stripeband) g=(int)gmin;
 
330
                                        else g=(int)(200+10*xr+xg*45);
 
331
                                        if (stripes)
 
332
                                                g = (int)getStripe(desc, g, xr/7.0, i, j);
 
333
                                        pw[k]=((255<<24)
 
334
                                                                 |(((int)((1-hh)*g+hh*shade*red))<<16)
 
335
                                                                 |(((int)((1-hh)*g+hh*shade*green))<<8)
 
336
                                                                 |((int)((1-hh)*g+hh*shade*blue)));
 
337
                                }
 
338
                        }
 
339
                        else pw[k]=0;
 
340
                        k++;
 
341
                }
 
342
 
 
343
        // now copy the result in QImages
 
344
        icopy(pw, wi, d, d);
 
345
 
 
346
        // free memory
 
347
        delete[] pw;
 
348
 
 
349
}
 
350
 
 
351
void ImageHandler::paintWhiteStone (QImage &wi, int d, bool noShadow, bool stripes)
 
352
{
 
353
        if (noShadow)
 
354
                paintWhiteStone1 (wi, d, stripes);
 
355
        else
 
356
                paintWhiteStone2 (wi, d, stripes);
 
357
}
 
358
 
 
359
 
 
360
// This function generates a realistic wood like board image.
 
361
// It is not really used in the code but included only for
 
362
// conveninence.
 
363
/*
 
364
void createwood (QImage &im, int w, int h, int color, bool shadows, int ox, int oy, int d) {
 
365
 
 
366
        if (w==0 || h==0) return;
 
367
 
 
368
        int *p=new int[w*h];
 
369
        int *ps=0;
 
370
 
 
371
        if (shadows) ps=new int[w*h];
 
372
 
 
373
        int i,j;
 
374
        double f=9e-1;
 
375
        int col=color;
 
376
        int blue=col&0x0000FF,green=(col&0x00FF00)>>8,red=(col&0xFF0000)>>16;
 
377
        double r,g,b;
 
378
        double x,y,dist;
 
379
        
 
380
        bool fine=false;
 
381
        
 
382
        for (i=0; i<h; i++)
 
383
                for (j=0; j<w; j++) {   
 
384
                        if (fine)
 
385
                                f=((sin(18*(double)j/w)+1)/2
 
386
                                +(sin(3*(double)j/w)+1)/10
 
387
                                +0.2*cos(5*(double)i/h)+
 
388
                                +0.1*sin(11*(double)i/h))
 
389
                                *20+0.5;
 
390
                        else
 
391
                                f=((sin(14*(double)j/w)+1)/2
 
392
                                +0.2*cos(3*(double)i/h)+
 
393
                                +0.1*sin(11*(double)i/h))
 
394
                                *10+0.5;
 
395
                        f=f-floor(f);
 
396
                        if (f<2e-1) f=1-f/2;
 
397
                        else if (f<4e-1) f=1-(4e-1-f)/2;
 
398
                        else f=1;
 
399
                        if (i==w-1 || (i==w-2 && j<w-2) || j==0
 
400
                                || (j==1 && i>1)) f=f/2;
 
401
                        if (i==0 || (i==1 && j>1) || j>=w-1
 
402
                                || (j==w-2 && i<h-1)) { 
 
403
                                r=128+red*f/2; g=128+green*f/2; b=128+blue*f/2;
 
404
                        }
 
405
                        else {  
 
406
                                r=red*f; g=green*f; b=blue*f;
 
407
                        }
 
408
                        p[w*i+j]=(255<<24)|((int)(r)<<16)|((int)(g)<<8)|(int)(b);
 
409
                        
 
410
                        if (shadows) {  f=1;
 
411
                        y=abs((i-(ox+d/2+(i-ox)/d*(double)d)));
 
412
                        x=abs((j-(oy+d/2+(j-oy)/d*(double)d)));
 
413
                        dist=sqrt(x*x+y*y)/d*2;
 
414
                        if (dist<1.0) f=0.9*dist;
 
415
                        ps[w*i+j]=(255<<24)|((int)(r*f)<<16)|((int)(g*f)<<8)|(int)(b*f);
 
416
                        }
 
417
                }
 
418
                
 
419
                if (shadows)
 
420
                        icopy(ps, im, w, h);
 
421
                else
 
422
                        icopy(p, im, w, h);
 
423
}
 
424
*/
 
425
/**
 
426
* end stone rendering code
 
427
**/
 
428
 
 
429
 
 
430
// end MF
 
431
 
 
432
ImageHandler::ImageHandler()
 
433
{
 
434
    // Load the pixmaps
 
435
#ifdef USE_XPM
 
436
    if (tablePixmap == NULL)
 
437
                tablePixmap = new QPixmap(const_cast<const char**>(table_xpm));
 
438
    if (woodPixmap1 == NULL)
 
439
                woodPixmap1 = new QPixmap(const_cast<const char**>(wood_xpm));
 
440
    if (woodPixmap2 == NULL)
 
441
                woodPixmap2 = new QPixmap(const_cast<const char**>(wood2_xpm));
 
442
    if (woodPixmap3 == NULL)
 
443
                woodPixmap3 = new QPixmap(const_cast<const char**>(wood3_xpm));
 
444
    if (woodPixmap4 == NULL)
 
445
                woodPixmap4 = new QPixmap(const_cast<const char**>(wood4_xpm));
 
446
    if (woodPixmap5 == NULL)
 
447
                woodPixmap5 = new QPixmap(const_cast<const char**>(wood5_xpm));
 
448
#else
 
449
    if (tablePixmap == NULL)
 
450
                tablePixmap = new QPixmap(TABLE_PIC);
 
451
    if (woodPixmap1 == NULL)
 
452
                //      img_wood = QImage(WOOD_PIC);
 
453
                woodPixmap1 = new QPixmap(WOOD_PIC);
 
454
    if (woodPixmap2 == NULL)
 
455
                woodPixmap2 = new QPixmap(WOOD2_PIC);
 
456
    if (woodPixmap3 == NULL)
 
457
                woodPixmap3 = new QPixmap(WOOD3_PIC);
 
458
    if (woodPixmap4 == NULL)
 
459
                woodPixmap4 = new QPixmap(WOOD4_PIC);
 
460
    if (woodPixmap5 == NULL)
 
461
                woodPixmap5 = new QPixmap(WOOD5_PIC);
 
462
#endif
 
463
 
 
464
    if (tablePixmap == NULL || tablePixmap->isNull())
 
465
                qFatal("Could not load pixmaps.");
 
466
    
 
467
        // get medium color of table pixmap used for antialiasing
 
468
        CHECK_PTR(woodPixmap1);
 
469
        QImage img_ = woodPixmap1->convertToImage();
 
470
        if (img_.valid(0, 0))
 
471
                ac1 = img_.pixel(0, 0);
 
472
        CHECK_PTR(woodPixmap2);
 
473
        img_ = woodPixmap2->convertToImage();
 
474
        if (img_.valid(0, 0))
 
475
                ac2 = img_.pixel(0, 0);
 
476
        CHECK_PTR(woodPixmap3);
 
477
        img_ = woodPixmap3->convertToImage();
 
478
        if (img_.valid(0, 0))
 
479
                ac3 = img_.pixel(0, 0);
 
480
        CHECK_PTR(woodPixmap4);
 
481
        img_ = woodPixmap4->convertToImage();
 
482
        if (img_.valid(0, 0))
 
483
                ac4 = img_.pixel(0, 0);
 
484
        CHECK_PTR(woodPixmap5);
 
485
        img_ = woodPixmap5->convertToImage();
 
486
        if (img_.valid(0, 0))
 
487
                ac5 = img_.pixel(0, 0);
 
488
 
 
489
    stonePixmaps = NULL;
 
490
    ghostPixmaps = NULL;
 
491
        
 
492
    // Init the alternate ghost pixmaps
 
493
    if (altGhostPixmaps == NULL)
 
494
    {
 
495
#ifdef USE_XPM
 
496
                QPixmap alt1(const_cast<const char**>(alt_ghost_black_xpm));
 
497
                QPixmap alt2(const_cast<const char**>(alt_ghost_white_xpm));
 
498
#else
 
499
                QPixmap alt1(ALT_GHOST_BLACK);
 
500
                QPixmap alt2(ALT_GHOST_WHITE);
 
501
#endif
 
502
                if (alt1.isNull() || alt2.isNull())
 
503
                        qFatal("Could not load alt_ghost pixmaps.");
 
504
                QList<QPixmap> list;
 
505
                list.append(&alt1);
 
506
                list.append(&alt2);
 
507
                QList<QPoint> hotspots;
 
508
                hotspots.append(new QPoint(alt1.width()/2, alt1.height()/2));
 
509
                hotspots.append(new QPoint(alt2.width()/2, alt2.height()/2));
 
510
                altGhostPixmaps = new QCanvasPixmapArray(list, hotspots);
 
511
    }
 
512
    CHECK_PTR(altGhostPixmaps);
 
513
        
 
514
    classCounter ++;
 
515
}
 
516
 
 
517
ImageHandler::~ImageHandler()
 
518
{
 
519
    classCounter --;
 
520
    if (classCounter == 0)
 
521
    {
 
522
                delete woodPixmap1;
 
523
                woodPixmap1 = NULL;
 
524
                delete woodPixmap2;
 
525
                woodPixmap2 = NULL;
 
526
                delete woodPixmap3;
 
527
                woodPixmap3 = NULL;
 
528
                delete woodPixmap4;
 
529
                woodPixmap4 = NULL;
 
530
                delete woodPixmap5;
 
531
                woodPixmap5 = NULL;
 
532
                delete tablePixmap;
 
533
                tablePixmap = NULL;
 
534
                delete altGhostPixmaps;
 
535
                altGhostPixmaps = NULL;
 
536
    }
 
537
        
 
538
    delete stonePixmaps;
 
539
    delete ghostPixmaps;
 
540
}
 
541
 
 
542
QPixmap* ImageHandler::getBoardPixmap(skinType s)
 
543
{
 
544
        switch (s)
 
545
        {
 
546
        case skinLight:
 
547
                return woodPixmap1;
 
548
        case skinDark:
 
549
                return woodPixmap2;
 
550
        case skin3:
 
551
                return woodPixmap3;
 
552
        case skin4:
 
553
                return woodPixmap4;
 
554
        case skin5:
 
555
                return woodPixmap5;
 
556
        default:
 
557
                return woodPixmap4;
 
558
        }
 
559
}
 
560
 
 
561
void ImageHandler::init(int size)
 
562
{
 
563
        // Scale the images
 
564
        size = size * 9 / 10;
 
565
        
 
566
        //*******
 
567
        bool noShadow = !setting->readBoolEntry("STONES_SHADOW");
 
568
        bool stripes = setting->readBoolEntry("STONES_SHELLS");
 
569
        QImage ib = QImage(size, size, 32);
 
570
        ib.setAlphaBuffer(TRUE);
 
571
        paintBlackStone(ib, size);
 
572
        
 
573
        QImage iw1 = QImage(size, size, 32);
 
574
        iw1.setAlphaBuffer(TRUE);
 
575
        paintWhiteStone(iw1, size, noShadow, stripes);
 
576
 
 
577
        QImage iw2, iw3, iw4, iw5, iw6, iw7, iw8;
 
578
 
 
579
        if (stripes)
 
580
        {
 
581
                iw2 = QImage(size, size, 32);
 
582
                iw2.setAlphaBuffer(TRUE);
 
583
                paintWhiteStone(iw2, size, noShadow, stripes);
 
584
                
 
585
                iw3 = QImage(size, size, 32);
 
586
                iw3.setAlphaBuffer(TRUE);
 
587
                paintWhiteStone(iw3, size, noShadow, stripes);
 
588
 
 
589
                iw4 = QImage(size, size, 32);
 
590
                iw4.setAlphaBuffer(TRUE);
 
591
                paintWhiteStone(iw4, size, noShadow, stripes);
 
592
 
 
593
                iw5 = QImage(size, size, 32);
 
594
                iw5.setAlphaBuffer(TRUE);
 
595
                paintWhiteStone(iw5, size, noShadow, stripes);
 
596
 
 
597
                iw6 = QImage(size, size, 32);
 
598
                iw6.setAlphaBuffer(TRUE);
 
599
                paintWhiteStone(iw6, size, noShadow, stripes);
 
600
 
 
601
                iw7 = QImage(size, size, 32);
 
602
                iw7.setAlphaBuffer(TRUE);
 
603
                paintWhiteStone(iw7, size, noShadow, stripes);
 
604
 
 
605
                iw8 = QImage(size, size, 32);
 
606
                iw8.setAlphaBuffer(TRUE);
 
607
                paintWhiteStone(iw8, size, noShadow, stripes);
 
608
        }
 
609
        else
 
610
        {
 
611
                iw2 = iw1;//.copy();
 
612
                iw3 = iw1;//.copy();
 
613
                iw4 = iw1;//.copy();
 
614
                iw5 = iw1;//.copy();
 
615
                iw6 = iw1;//.copy();
 
616
                iw7 = iw1;//.copy();
 
617
                iw8 = iw1;//.copy();
 
618
        }
 
619
 
 
620
        //*********
 
621
        
 
622
        
 
623
        // Convert images to pixmaps
 
624
        QList<QPixmap> list;
 
625
        QPixmap *ibp = new QPixmap();
 
626
        ibp->convertFromImage(ib, 
 
627
                QPixmap::PreferDither | 
 
628
                QPixmap::DiffuseAlphaDither | 
 
629
                QPixmap::DiffuseDither);
 
630
        list.append(ibp);
 
631
        
 
632
        QPixmap *iw1p = new QPixmap();
 
633
        iw1p->convertFromImage(iw1, 
 
634
                QPixmap::PreferDither | 
 
635
                QPixmap::DiffuseAlphaDither | 
 
636
                QPixmap::DiffuseDither);
 
637
        list.append(iw1p);
 
638
 
 
639
        QPixmap *iw2p, *iw3p, *iw4p, *iw5p, *iw6p, *iw7p, *iw8p;
 
640
 
 
641
        if (stripes)
 
642
        {
 
643
                iw2p = new QPixmap();
 
644
                iw2p->convertFromImage(iw2, 
 
645
                        QPixmap::PreferDither | 
 
646
                        QPixmap::DiffuseAlphaDither | 
 
647
                        QPixmap::DiffuseDither);
 
648
                list.append(iw2p);
 
649
                
 
650
                iw3p = new QPixmap();
 
651
                iw3p->convertFromImage(iw3, 
 
652
                        QPixmap::PreferDither | 
 
653
                        QPixmap::DiffuseAlphaDither | 
 
654
                        QPixmap::DiffuseDither);
 
655
                list.append(iw3p);
 
656
                
 
657
                iw4p = new QPixmap();
 
658
                iw4p->convertFromImage(iw4, 
 
659
                        QPixmap::PreferDither | 
 
660
                        QPixmap::DiffuseAlphaDither | 
 
661
                        QPixmap::DiffuseDither);
 
662
                list.append(iw4p);
 
663
                
 
664
                iw5p = new QPixmap();
 
665
                iw5p->convertFromImage(iw5, 
 
666
                        QPixmap::PreferDither | 
 
667
                        QPixmap::DiffuseAlphaDither | 
 
668
                        QPixmap::DiffuseDither);
 
669
                list.append(iw5p);
 
670
                
 
671
                iw6p = new QPixmap();
 
672
                iw6p->convertFromImage(iw6, 
 
673
                        QPixmap::PreferDither | 
 
674
                        QPixmap::DiffuseAlphaDither | 
 
675
                        QPixmap::DiffuseDither);
 
676
                list.append(iw6p);
 
677
                
 
678
                iw7p = new QPixmap();
 
679
                iw7p->convertFromImage(iw7, 
 
680
                        QPixmap::PreferDither | 
 
681
                        QPixmap::DiffuseAlphaDither | 
 
682
                        QPixmap::DiffuseDither);
 
683
                list.append(iw7p);
 
684
                
 
685
                iw8p = new QPixmap();
 
686
                iw8p->convertFromImage(iw8, 
 
687
                        QPixmap::PreferDither | 
 
688
                        QPixmap::DiffuseAlphaDither | 
 
689
                        QPixmap::DiffuseDither);
 
690
                list.append(iw8p);
 
691
        }
 
692
        else
 
693
        {
 
694
                list.append(iw1p);
 
695
                list.append(iw1p);
 
696
                list.append(iw1p);
 
697
                list.append(iw1p);
 
698
                list.append(iw1p);
 
699
                list.append(iw1p);
 
700
                list.append(iw1p);
 
701
        }
 
702
        
 
703
        // Create the hotspots as middle of the image
 
704
        QList<QPoint> hotspots;
 
705
        QPoint point(size/2, size/2);
 
706
        hotspots.append(&point);
 
707
        hotspots.append(&point);
 
708
        hotspots.append(&point);
 
709
        hotspots.append(&point);
 
710
        hotspots.append(&point);
 
711
        hotspots.append(&point);
 
712
        hotspots.append(&point);
 
713
        hotspots.append(&point);
 
714
        hotspots.append(&point);
 
715
        
 
716
        // Assemble the data in the QCanvasPixmapArray
 
717
        stonePixmaps = new QCanvasPixmapArray(list, hotspots);
 
718
        
 
719
        // Do the same for the ghosts
 
720
        QImage gb(ib);
 
721
        QImage gw1(iw1);
 
722
        QImage gw2(iw2);
 
723
        QImage gw3(iw3);
 
724
        QImage gw4(iw4);
 
725
        QImage gw5(iw5);
 
726
        QImage gw6(iw6);
 
727
        QImage gw7(iw7);
 
728
        QImage gw8(iw8);
 
729
        ghostImage(&gb);
 
730
        ghostImage(&gw1);
 
731
        ghostImage(&gw2);
 
732
        ghostImage(&gw3);
 
733
        ghostImage(&gw4);
 
734
        ghostImage(&gw5);
 
735
        ghostImage(&gw6);
 
736
        ghostImage(&gw7);
 
737
        ghostImage(&gw8);
 
738
        list.clear();
 
739
        list.append(new QCanvasPixmap(gb));
 
740
        list.append(new QCanvasPixmap(gw1));
 
741
        list.append(new QCanvasPixmap(gw2));
 
742
        list.append(new QCanvasPixmap(gw3));
 
743
        list.append(new QCanvasPixmap(gw4));
 
744
        list.append(new QCanvasPixmap(gw5));
 
745
        list.append(new QCanvasPixmap(gw6));
 
746
        list.append(new QCanvasPixmap(gw7));
 
747
        list.append(new QCanvasPixmap(gw8));
 
748
        ghostPixmaps = new QCanvasPixmapArray(list, hotspots);
 
749
}
 
750
 
 
751
void ImageHandler::rescale(int size, bool smallerStones)
 
752
{
 
753
        CHECK_PTR(stonePixmaps);
 
754
        CHECK_PTR(ghostPixmaps);
 
755
 
 
756
        size = size + 2;
 
757
 
 
758
        // get antialiasing color
 
759
        switch (setting->readIntEntry("SKIN"))
 
760
        {
 
761
                case 0:
 
762
                        antialiasingColor = ac1;
 
763
                        break;
 
764
                case 1:
 
765
                        antialiasingColor = ac2;
 
766
                        break;
 
767
                case 2:
 
768
                        antialiasingColor = ac3;
 
769
                        break;
 
770
                case 3:
 
771
                        antialiasingColor = ac4;
 
772
                        break;
 
773
                case 4:
 
774
                        antialiasingColor = ac5;
 
775
                        break;
 
776
        }
 
777
 
 
778
        // Scale the images
 
779
        if (smallerStones)
 
780
                size = size - 1;
 
781
 
 
782
        bool noShadow = !setting->readBoolEntry("STONES_SHADOW");
 
783
        bool stripes = setting->readBoolEntry("STONES_SHELLS");
 
784
        QImage ib = QImage(size, size, 32);
 
785
        ib.setAlphaBuffer(TRUE);
 
786
        paintBlackStone(ib, size);
 
787
 
 
788
        QImage iw1 = QImage(size, size, 32);
 
789
        iw1.setAlphaBuffer(TRUE);
 
790
        paintWhiteStone(iw1, size, noShadow, stripes);
 
791
 
 
792
        QImage iw2, iw3, iw4, iw5, iw6, iw7, iw8;
 
793
 
 
794
        if (stripes)
 
795
        {
 
796
                iw2 = QImage(size, size, 32);
 
797
                iw2.setAlphaBuffer(TRUE);
 
798
                paintWhiteStone(iw2, size, noShadow, stripes);
 
799
                
 
800
                iw3 = QImage(size, size, 32);
 
801
                iw3.setAlphaBuffer(TRUE);
 
802
                paintWhiteStone(iw3, size, noShadow, stripes);
 
803
 
 
804
                iw4 = QImage(size, size, 32);
 
805
                iw4.setAlphaBuffer(TRUE);
 
806
                paintWhiteStone(iw4, size, noShadow, stripes);
 
807
 
 
808
                iw5 = QImage(size, size, 32);
 
809
                iw5.setAlphaBuffer(TRUE);
 
810
                paintWhiteStone(iw5, size, noShadow, stripes);
 
811
 
 
812
                iw6 = QImage(size, size, 32);
 
813
                iw6.setAlphaBuffer(TRUE);
 
814
                paintWhiteStone(iw6, size, noShadow, stripes);
 
815
 
 
816
                iw7 = QImage(size, size, 32);
 
817
                iw7.setAlphaBuffer(TRUE);
 
818
                paintWhiteStone(iw7, size, noShadow, stripes);
 
819
 
 
820
                iw8 = QImage(size, size, 32);
 
821
                iw8.setAlphaBuffer(TRUE);
 
822
                paintWhiteStone(iw8, size, noShadow, stripes);
 
823
        }
 
824
        else
 
825
        {
 
826
                iw2 = iw1;//.copy();
 
827
                iw3 = iw1;//.copy();
 
828
                iw4 = iw1;//.copy();
 
829
                iw5 = iw1;//.copy();
 
830
                iw6 = iw1;//.copy();
 
831
                iw7 = iw1;//.copy();
 
832
                iw8 = iw1;//.copy();
 
833
        }
 
834
 
 
835
        // Replace the images in the array
 
836
 
 
837
        stonePixmaps->setImage(0, new QCanvasPixmap(ib));
 
838
        stonePixmaps->image(0)->setOffset(size/2, size/2);
 
839
        stonePixmaps->setImage(1, new QCanvasPixmap(iw1));
 
840
        stonePixmaps->image(1)->setOffset(size/2, size/2);
 
841
        stonePixmaps->setImage(2, new QCanvasPixmap(iw2));
 
842
        stonePixmaps->image(2)->setOffset(size/2, size/2);
 
843
        stonePixmaps->setImage(3, new QCanvasPixmap(iw3));
 
844
        stonePixmaps->image(3)->setOffset(size/2, size/2);
 
845
        stonePixmaps->setImage(4, new QCanvasPixmap(iw4));
 
846
        stonePixmaps->image(4)->setOffset(size/2, size/2);
 
847
        stonePixmaps->setImage(5, new QCanvasPixmap(iw5));
 
848
        stonePixmaps->image(5)->setOffset(size/2, size/2);
 
849
        stonePixmaps->setImage(6, new QCanvasPixmap(iw6));
 
850
        stonePixmaps->image(6)->setOffset(size/2, size/2);
 
851
        stonePixmaps->setImage(7, new QCanvasPixmap(iw7));
 
852
        stonePixmaps->image(7)->setOffset(size/2, size/2);
 
853
        stonePixmaps->setImage(8, new QCanvasPixmap(iw8));
 
854
        stonePixmaps->image(8)->setOffset(size/2, size/2);
 
855
 
 
856
        // Do the same for the ghosts
 
857
        QImage gb(ib);
 
858
        QImage gw1(iw1);
 
859
        QImage gw2(iw2);
 
860
        QImage gw3(iw3);
 
861
        QImage gw4(iw4);
 
862
        QImage gw5(iw5);
 
863
        QImage gw6(iw6);
 
864
        QImage gw7(iw7);
 
865
        QImage gw8(iw8);
 
866
        ghostImage(&gb);
 
867
        ghostImage(&gw1);
 
868
        ghostImage(&gw2);
 
869
        ghostImage(&gw3);
 
870
        ghostImage(&gw4);
 
871
        ghostImage(&gw5);
 
872
        ghostImage(&gw6);
 
873
        ghostImage(&gw7);
 
874
        ghostImage(&gw8);
 
875
        ghostPixmaps->setImage(0, new QCanvasPixmap(gb));
 
876
        ghostPixmaps->image(0)->setOffset(size/2, size/2);
 
877
        ghostPixmaps->setImage(1, new QCanvasPixmap(gw1));
 
878
        ghostPixmaps->image(1)->setOffset(size/2, size/2);
 
879
        ghostPixmaps->setImage(2, new QCanvasPixmap(gw2));
 
880
        ghostPixmaps->image(2)->setOffset(size/2, size/2);
 
881
        ghostPixmaps->setImage(3, new QCanvasPixmap(gw3));
 
882
        ghostPixmaps->image(3)->setOffset(size/2, size/2);
 
883
        ghostPixmaps->setImage(4, new QCanvasPixmap(gw4));
 
884
        ghostPixmaps->image(4)->setOffset(size/2, size/2);
 
885
        ghostPixmaps->setImage(5, new QCanvasPixmap(gw5));
 
886
        ghostPixmaps->image(5)->setOffset(size/2, size/2);
 
887
        ghostPixmaps->setImage(6, new QCanvasPixmap(gw6));
 
888
        ghostPixmaps->image(6)->setOffset(size/2, size/2);
 
889
        ghostPixmaps->setImage(7, new QCanvasPixmap(gw7));
 
890
        ghostPixmaps->image(7)->setOffset(size/2, size/2);
 
891
        ghostPixmaps->setImage(8, new QCanvasPixmap(gw8));
 
892
        ghostPixmaps->image(8)->setOffset(size/2, size/2);
 
893
}
 
894
 
 
895
void ImageHandler::ghostImage(QImage *img)
 
896
{
 
897
    img->setAlphaBuffer(true);
 
898
    img->convertDepth(32);
 
899
 
 
900
    int w = img->width(),
 
901
                h = img->height(),
 
902
                x, y;
 
903
 
 
904
    for (y=0; y<h; y++)
 
905
    {
 
906
                uint *line = (uint*)img->scanLine(y);
 
907
                for (x=0; x<w; x++)
 
908
                {
 
909
                        if ((x%2 && !(y%2)) || (!(x%2) && y%2))
 
910
                        {
 
911
                                line[x] = qRgba(qRed(line[x]), qGreen(line[x]), qBlue(line[x]), 0);
 
912
                        }
 
913
                }
 
914
    }
 
915
}