Debian Codegraph @

src/board/imagehandler.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2009 by The qGo Project *
3 * *
4 * This file is part of qGo. *
5 * *
6 * qGo 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 *
14 * GNU 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; if not, see <http://www.gnu.org/licenses/> *
18 * or write to the Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22
23 /*
24 *
25 * imagehandler.cpp
26 *
27 * the stone rendering part has been coded by Marin Ferecatu - thanks, good job!
28 *
29 */
30
31 #include <cmath>
32
33 #include "defines.h"
34 #include "imagehandler.h"
35
36
37 #ifdef Q_OS_WIN
38 #ifndef M_PI
39 #define M_PI 3.141592653
40 #endif //M_PI
41 double drand48() { return rand()*1.0/RAND_MAX; }
42 #endif
43
44
45 /*
46 * Static class variables
47 */
48 QPixmap *ImageHandler::woodPixmap1 = NULL;
49
50 QPixmap *ImageHandler::tablePixmap = NULL;
51 QList<QPixmap> *ImageHandler::altGhostPixmaps = NULL;
52 int ImageHandler::classCounter = 0;
53
54
55 /*
56 * Stone rendering code
57 * Heavily inspired from Jago and CGoban code
58 * http://www.igoweb.org/~wms/comp/cgoban
59 * /http://www.rene-grothmann.de/jago/
60 */
61
62
63 ImageHandler::ImageHandler()
64 {
65 // Load the pixmaps
66 // if (tablePixmap == NULL)
67 tablePixmap = new QPixmap(":/boardicons/resources/pics/table.png");
68 // if (woodPixmap1 == NULL)
69 woodPixmap1 = new QPixmap(":/boardicons/resources/pics/wood.png");
70 /* I wanted to make it look like an old style go manual, but apparently
71 * this is possible by changing the stone type to "Ugly 2d" and I
72 * made this little paper graphic with some speckle... but I'll add a
73 * comment here in case someone else has the same bright idea for a
74 * "classic" mode */
75 //paperPixmap = new QPixmap(":/boardicons/resources/pics/paper.png");
76
77 // if (tablePixmap == NULL || tablePixmap->isNull())
78 // qFatal("Could not load pixmaps.");
79
80 stonePixmaps = NULL;
81 ghostPixmaps = NULL;
82 smallStonePixmaps = NULL;
83 #ifdef DONTREDRAWSTONES
84 stonePixmapsScaled = NULL;
85 ghostPixmapsScaled = NULL;
86 smallStonePixmapsScaled = NULL;
87 #endif //DONTREDRAWSTONES
88 #define STONEIMAGE_MAXSIZE 400
89 painting_buffer = new int[STONEIMAGE_MAXSIZE*STONEIMAGE_MAXSIZE];
90
91 // Init the alternate ghost pixmaps
92 if (altGhostPixmaps == NULL)
93 {
94 altGhostPixmaps = new QList<QPixmap>();//::QList();
95
96 QPixmap alt1 = QPixmap(":/boardicons/resources/pics/alt_ghost_black.png");
97 QPixmap alt2 = QPixmap(":/boardicons/resources/pics/alt_ghost_white.png");
98
99 if (alt1.isNull() || alt2.isNull())
100 qFatal("Could not load alt_ghost pixmaps.");
101 // QList<QPixmap*> list;
102
103 // list.append(alt1);
104 // list.append(alt2);
105 QList<QPoint*> hotspots;
106 // hotspots.append(new QPoint(alt1.width()/2, alt1.height()/2));
107 // hotspots.append(new QPoint(alt2.width()/2, alt2.height()/2));
108 // altGhostPixmaps = new QList<QPixmap*>::QList(list);//, hotspots);
109
110 altGhostPixmaps->append(alt1);
111 altGhostPixmaps->append(alt2);
112 }
113 Q_CHECK_PTR(altGhostPixmaps);
114
115 classCounter ++;
116
117 QSettings settings;
118
119 isDisplayBoard = false;
120
121 stonePixmaps = new QList<QPixmap>();//::QList();
122 if(preferences.terr_stone_mark)
123 smallStonePixmaps = new QList<QPixmap>();
124 ghostPixmaps = new QList<QPixmap>();
125 }
126
127 ImageHandler::~ImageHandler()
128 {
129
130 classCounter --;
131 if (classCounter == 0)
132 {
133 delete woodPixmap1;
134 woodPixmap1 = NULL;
135
136 delete tablePixmap;
137 tablePixmap = NULL;
138
139 altGhostPixmaps->clear();
140 // delete altGhostPixmaps;
141 altGhostPixmaps = NULL;
142 }
143
144 delete stonePixmaps;
145 delete ghostPixmaps;
146 delete smallStonePixmaps;
147 #ifdef DONTREDRAWSTONES
148 delete stonePixmapsScaled;
149 delete ghostPixmapsScaled;
150 delete smallStonePixmapsScaled;
151 #endif //DONTREDRAWSTONES
152
153 delete[] painting_buffer;
154 }
155
156 void ImageHandler::icopy(int *im, QImage &qim, int w, int h)
157 {
158
159 for (int y = 0; y < h; y++)
160 {
161 uint *p = (uint *)qim.scanLine(y);
162 for(int x = 0; x < w; x++)
163 {
164 p[x] = im[y*h + x];
165 }
166 }
167 }
168
169 void ImageHandler::decideAppearance(WhiteDesc *desc, int size)
170 {
171 double minStripeW, maxStripeW, theta;
172
173 minStripeW = (double)size / 20.0;
174 if (minStripeW < 1.5)
175 minStripeW = 1.5;
176 maxStripeW = (double)size / 10.0;
177 if (maxStripeW < 2.5)
178 maxStripeW = 2.5;
179
180 theta = drand48() * 2.0 * M_PI;
181 desc->cosTheta = cos(theta);
182 desc->sinTheta = sin(theta);
183 desc->stripeWidth = 1.5*minStripeW +
184 (drand48() * (maxStripeW - minStripeW));
185
186 desc->xAdd = 3*desc->stripeWidth +
187 (double)size * 3.0;
188
189 desc->stripeMul = 3.0;
190 desc->zMul = drand48() * 650.0 + 70.0;
191
192 }
193
194 double ImageHandler::getStripe(WhiteDesc &white, double bright, double z, int x, int y) {
195 double wBright;
196
197 bright = bright/256.0;
198
199 double wStripeLoc = x*white.cosTheta - y*white.sinTheta + white.xAdd;
200 double wStripeColor = fmod(wStripeLoc + (z * z * z * white.zMul) *
201 white.stripeWidth,
202 white.stripeWidth) / white.stripeWidth;
203 wStripeColor = wStripeColor * white.stripeMul - 0.5;
204 if (wStripeColor < 0.0)
205 wStripeColor = -2.0 * wStripeColor;
206 if (wStripeColor > 1.0)
207 wStripeColor = 1.0;
208 wStripeColor = wStripeColor * 0.15 + 0.85;
209
210 if (bright < 0.95)
211 wBright = bright*wStripeColor;
212 else
213 wBright = bright*sqrt(sqrt(wStripeColor));
214
215 if (wBright > 255)
216 wBright = 255;
217 if (wBright < 0)
218 wBright = 0;
219
220 return wBright*255;
221
222 }
223
224 void ImageHandler::paintBlackStone (QImage &bi, int d, int stone_render)
225 {
226 const double pixel=0.8;//,shadow=0.99;
227
228
229 bool Alias=true;
230
231 // these are the images
232 int i, j, g,g1,g2, k;
233 double di, dj, d2=(double)d/2.0-5e-1, r=d2-2e-1, f=sqrt(3.0);
234 double x, y, z, xr,xr1, xr2, xg1,xg2,hh;
235
236 k=0;
237
238 bool smallerstones = false;
239 if (smallerstones) r-=1;
240
241 for (i=0; i<d; i++)
242 for (j=0; j<d; j++) {
243 di=i-d2; dj=j-d2;
244 hh=r-sqrt(di*di+dj*dj);
245 if (hh>=0)
246 {
247 if (stone_render !=1)
248 {
249 z=r*r-di*di-dj*dj;
250 if (z>0) z=sqrt(z)*f;
251 else z=0;
252 x=di; y=dj;
253 xr=sqrt(6*(x*x+y*y+z*z));
254 xr1=(2*z-x+y)/xr;
255 //xr2=(1.6*z+x+1.75*y)/xr;
256 xr2=(2*z+x-y)/xr;
257
258 if (xr1>0.9) xg1=(xr1-0.9)*10;
259 else xg1=0;
260 //if (xr2>1) xg2=(xr2-1)*10;
261 if (xr2>0.96) xg2=(xr2-0.96)*10;
262 else xg2=0;
263
264 //random = drand48();
265
266 g1=(int)(5+10*drand48() + 10*xr1 + xg1*140);
267 g2=(int)(10+10* xr2+xg2*160);
268 g=(g1 > g2 ? g1 : g2);
269 //g=(int)1/ (1/g1 + 1/g2);
270
271 if (hh>pixel || !Alias) {
272 painting_buffer[k]=(255<<24)|(g<<16)|(g<<8)|g;
273 }
274 else {
275 painting_buffer[k]=((int)(hh/pixel*255)<<24)|(g<<16)|(g<<8)|g;
276 }
277
278 }
279 else //code for flat stones
280 {
281 g=0;
282 painting_buffer[k]=((int)(255)<<24)|(g<<16)|(g<<8)|g;
283 }
284 }
285 else painting_buffer[k]=0;
286 k++;
287
288 }
289
290 // now copy the result in QImages
291 icopy(painting_buffer, bi, d, d);
292 }
293
294 // shadow under stones
295 void ImageHandler::paintShadowStone (QImage &si, int d) {
296
297 //const double pixel=0.8,shadow=0.99;
298
299 // these are the images
300 int i, j, k;
301 double di, dj, d2=(double)d/2.0-5e-1, r=d2-2e-1;
302 double hh;
303
304 k=0;
305
306 bool smallerstones = false;
307 if (smallerstones) r-=1;
308
309 for (i=0; i<d; i++)
310 for (j=0; j<d; j++) {
311 di=i-d2; dj=j-d2;
312 hh=r-sqrt(di*di+dj*dj);
313 if (hh>=0) {
314 hh=2*hh/r ;
315 if (hh> 1) hh=1;
316
317 painting_buffer[k]=((int)(255*hh)<<24)|(1<<16)|(1<<8)|(1);
318 }
319 else painting_buffer[k]=0;
320 k++;
321
322 }
323
324 // now copy the result in QImages
325 icopy(painting_buffer, si, d, d);
326 }
327
328 // shaded white stones
329 void ImageHandler::paintWhiteStone (QImage &wi, int d, int stone_render)//bool stripes ) {
330 {
331 WhiteDesc desc;
332 decideAppearance(&desc, d);
333
334 // the angle from which the dim starts (measured to the light direction = pi/4)
335 // alpha should be in (0, pi)
336 const double ALPHA = M_PI/4;
337 // how big the dim is (should be < d/2)
338 const double STRIPE = d/5.0;
339
340 double theta;
341 const double pixel=0.8;//, shadow=0.99;
342
343 bool Alias=true;
344
345 // these are the images
346 int i, j, g, g1,g2,k;
347 double di, dj, d2=(double)d/2.0-5e-1, r=d2-2e-1, f=sqrt(3.0);
348 double x, y, z, xr, xr1, xr2, xg1,xg2, hh;
349
350 k=0;
351
352 bool smallerstones = false;
353 if (smallerstones) r-=1;
354
355 for (i=0; i<d; i++)
356 for (j=0; j<d; j++) {
357 di=i-d2; dj=j-d2;
358 hh=r-sqrt(di*di+dj*dj);
359 if (hh>=0)
360 {
361 if (stone_render != 1)
362 {
363 z=r*r-di*di-dj*dj;
364 if (z>0) z=sqrt(z)*f;
365 else z=0;
366 x=di; y=dj;
367 xr=sqrt(6*(x*x+y*y+z*z));
368 xr1=(2*z-x+y)/xr;
369 xr2=(2*z+x-y)/xr;
370
371 //#define WHITE_SHINE_START 0.9
372 //#define WHITE_SHINE_END 0.92
373 #define WHITE_SHINE_START 0.9795
374 #define WHITE_SHINE_END 0.98
375 if (xr1>WHITE_SHINE_START) xg1=(xr1-WHITE_SHINE_START)*10;
376 else xg1=0;
377
378 if (xr2>WHITE_SHINE_END) xg2=(xr2-WHITE_SHINE_END)*10;
379 else xg2=0;
380 //#define WHITENESS 200
381 #define WHITENESS 235
382 //#define WHITENESS_SHINE 10
383 #define WHITENESS_SHINE 5
384 g2=(int)(WHITENESS+WHITENESS_SHINE* xr2+xg2*45);
385 //g=(g1 > g2 ? g1 : g2);
386
387 theta = atan2(double(j-d/2), double(i-d/2)) + M_PI - M_PI/4 + M_PI/2;
388 bool stripeband = theta > ALPHA && theta < 2*M_PI-ALPHA;
389
390 if (theta > M_PI)
391 theta = (2*M_PI-theta);
392
393 double stripe = STRIPE*sin((M_PI/2)*(theta-ALPHA)/(M_PI-ALPHA));
394 if (stripe < 1) stripe = 1;
395
396 double g1min=(int)(0+WHITENESS_SHINE*xr1+xg1*45), g2min=(int)(0+WHITENESS_SHINE*xr2+xg2*45);
397 double g1max=(int)(WHITENESS+WHITENESS_SHINE*xr1+xg1*45), g2max=(int)(WHITENESS+WHITENESS_SHINE* xr2+xg2*45);;
398 g1min = g1max - (g1max-g1min)*(1-exp(-1*(theta-ALPHA)/(M_PI-ALPHA)));
399 g2min = g2max - (g2max-g2min)*(1-exp(-1*(theta-ALPHA)/(M_PI-ALPHA)));
400
401 if ((hh < STRIPE) && hh > pixel && stripeband)
402 {
403
404 if (hh > stripe) g1 = (int)g1max;
405 else g1 = int(g1min + (g1max-g1min)*sqrt(hh/stripe));
406
407 if (hh > stripe) g2 = (int)g2max;
408 else g2 = int(g2min + (g2max-g2min)*sqrt(hh/stripe));
409
410 g=(g1 > g2 ? g1 : g2);
411
412 if (stone_render == 0) //stripes)
413 g = (int)getStripe(desc, g, xr1/7.0, i, j);
414 painting_buffer[k]=(255<<24)|(g<<16)|((g)<<8)|(g);
415 }
416 else if (( hh > pixel ) || (!Alias) )
417 {
418 //g1=(int)(190+10*drand48()+10*xr1+xg1*45);
419
420 g=(int)(g1max > g2max ? g1max : g2max);
421
422 if (stone_render == 0)//stripes)
423 g = (int)getStripe(desc, g, xr1/7.0, i, j);
424 painting_buffer[k]=(255<<24)|(g<<16)|((g)<<8)|(g);
425 }
426 else {
427
428 g1=(int)(stripeband ? g1min : g1max);
429 g2=(int)(stripeband ? g2min : g2max);
430
431 g=(g1 > g2 ? g1 : g2);
432
433 if (stone_render == 0)//stripes)
434 g = (int)getStripe(desc, g, xr1/7.0, i, j);
435
436 painting_buffer[k]=((int)(hh/pixel*255)<<24)|(g<<16)|(g<<8)|g;
437 }
438 }
439 else // Code for flat stones
440 {
441 // draws a black circle for 2D stones
442 if ((hh>=-1)&&(hh<=1))
443 {
444 g=0;
445 painting_buffer[k]=((int)(255)<<24)|(g<<16)|(g<<8)|g;
446 }
447 else if (hh>0)
448 {
449 g=255;
450 painting_buffer[k]=((int)(255)<<24)|(g<<16)|(g<<8)|g;
451 }
452 }
453
454 }
455 else painting_buffer[k]=0;
456 k++;
457 }
458
459 // now copy the result in QImages
460 icopy(painting_buffer, wi, d, d);
461 }
462
463
464 QPixmap *ImageHandler::getBoardPixmap(QString filename)
465 {
466 if (! QFile::exists (QString(filename)))
467 {
468 if(filename != QString())
469 qDebug("Can't open board picture: \"%s\"", filename.toLatin1().constData());
470 return woodPixmap1;
471 }
472
473 QPixmap *p = new QPixmap(filename) ;
474
475 if (p->isNull())
476 return woodPixmap1;
477 else
478 return p;
479
480 }
481
482 QPixmap *ImageHandler::getTablePixmap(QString filename)
483 {
484 if (! QFile::exists (QString(filename)))
485 {
486 if(filename != QString())
487 qDebug("Can't open table picture: \"%s\"", filename.toLatin1().constData());
488 return tablePixmap;
489 }
490
491 QPixmap *p = new QPixmap(filename) ;
492
493 if (p->isNull())
494 return tablePixmap;
495 else
496 return p;
497
498 }
499
500 void ImageHandler::setDisplay(bool isDisplay)
501 {
502 isDisplayBoard = isDisplay;
503 }
504
505 #ifdef DONTREDRAWSTONES
506 void ImageHandler::generateStonePixmaps(int size)//, bool smallerStones)
507 #else
508 void ImageHandler::setSquareSize(int size)
509 #endif //DONTREDRAWSTONES
510 {
511 QSettings settings;
512
513 Q_CHECK_PTR(stonePixmaps);
514 Q_CHECK_PTR(ghostPixmaps);
515
516 int stone_look = ( isDisplayBoard ? 1 : settings.value("STONES_LOOK").toInt());
517 int smallstones_size;
518
519 stonePixmaps->clear();
520 ghostPixmaps->clear();
521 if(preferences.terr_stone_mark)
522 {
523 smallstones_size = (int)(size / SMALL_STONE_TERR_DIVISOR);
524 smallStonePixmaps->clear();
525 }
526
527 //repaint black stones
528 QImage ib = QImage(size, size, QImage::Format_ARGB32);
529 QImage iws;
530 // ib.setAlphaBuffer(true);
531 paintBlackStone(ib, size, stone_look);
532 //stonePixmaps->append(QPixmap::fromImage(ib));//,
533 // Qt::PreferDither |
534 // Qt::DiffuseAlphaDither |
535 // Qt::DiffuseDither) );
536 // stonePixmaps->image(0)->setOffset(size/2, size/2);
537 stonePixmaps->append(QPixmap::fromImage(ib,
538 Qt::PreferDither |
539 Qt::DiffuseAlphaDither |
540 Qt::DiffuseDither) );
541
542 QImage gb(ib);
543 ghostImage(&gb);
544 ghostPixmaps->append(QPixmap::fromImage(gb));
545
546 //small black stone
547 if(preferences.terr_stone_mark)
548 {
549 QImage ibs = QImage(smallstones_size, smallstones_size, QImage::Format_ARGB32);
550 paintBlackStone(ibs, smallstones_size, stone_look);
551 smallStonePixmaps->append(QPixmap::fromImage(ibs));
552
553 // small white stones
554 iws = QImage(smallstones_size, smallstones_size, QImage::Format_ARGB32);
555 }
556 // white stones
557 QImage iw1 = QImage(size, size, QImage::Format_ARGB32);
558
559
560
561 for (int i=1 ; i<=WHITE_STONES_NB; i++)
562 {
563 paintWhiteStone(iw1, size, stone_look);
564 //stonePixmaps->append(QPixmap::fromImage(iw1));
565 stonePixmaps->append(QPixmap::fromImage(iw1,
566 Qt::PreferDither |
567 Qt::DiffuseAlphaDither |
568 Qt::DiffuseDither) );
569 QImage gw1(iw1);
570 ghostImage(&gw1);
571 ghostPixmaps->append(QPixmap::fromImage(gw1));
572
573 if(preferences.terr_stone_mark)
574 {
575 paintWhiteStone(iws, smallstones_size, stone_look);
576 smallStonePixmaps->append(QPixmap::fromImage(iws));
577
578 /*smallStonePixmaps->append(QPixmap::fromImage(iws,
579 Qt::PreferDither |
580 Qt::DiffuseAlphaDither |
581 Qt::DiffuseDither) );*/
582 }
583 }
584
585 // shadow
586 QImage is = QImage(size, size, QImage::Format_ARGB32);
587
588 if (stone_look == 0)
589 paintShadowStone(is, size);
590 else
591 is.fill(0);
592
593 //stonePixmaps->append(QPixmap::fromImage(is));
594 stonePixmaps->append(QPixmap::fromImage(is,
595 Qt::PreferDither |
596 Qt::DiffuseAlphaDither |
597 Qt::DiffuseDither) );
598 }
599
600 #ifdef DONTREDRAWSTONES
601 void ImageHandler::rescale(int size)
602 {
603 int i;
604 QPixmap p;
605 int smallstones_size;
606 if(preferences.terr_stone_mark)
607 smallstones_size = (int)(size / SMALL_STONE_TERR_DIVISOR);
608
609 for(i = 0; i < stonePixmaps->size(); i++)
610 {
611 p = (*stonePixmaps)[i].scaled(size, size);
612 (*stonePixmapsScaled)[i] = p;
613 }
614 if(preferences.terr_stone_mark)
615 {
616 for(i = 0; i < smallStonePixmaps->size(); i++)
617 {
618 p = (*smallStonePixmaps)[i].scaled(smallstones_size, smallstones_size);
619 (*smallStonePixmapsScaled)[i] = p;
620 }
621 }
622 for(i = 0; i < ghostPixmaps->size(); i++)
623 {
624 p = (*ghostPixmaps)[i].scaled(size, size);
625 (*ghostPixmapsScaled)[i] = p;
626 }
627 }
628 #endif //DONTREDRAWSTONES
629
630 void ImageHandler::ghostImage(QImage *img)
631 {
632
633 int w = img->width(),
634 h = img->height(),
635 x, y;
636
637 for (y=0; y<h; y++)
638 {
639 uint *line = (uint*)img->scanLine(y);
640 for (x=0; x<w; x++)
641 {
642 //if ((x%2 && !(y%2)) || (!(x%2) && y%2))
643 {
644 line[x] = qRgba(qRed(line[x]), qGreen(line[x]), qBlue(line[x]), (line[x] ? 125 : 0));
645 }
646 }
647 }
648
649 }