1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <qimage.h>
|
---|
43 | #include <qcolor.h>
|
---|
44 |
|
---|
45 | static inline int blendComponent( int v, int av, int s, int as )
|
---|
46 | {
|
---|
47 | //shadow gets a color inversely proportional to the
|
---|
48 | //alpha value
|
---|
49 | s = (s*(255-as))/255;
|
---|
50 | //then do standard blending
|
---|
51 | return as*s + av*v -(av*as*s)/255;
|
---|
52 | }
|
---|
53 |
|
---|
54 | static inline QRgb blendShade( QRgb v, QRgb s )
|
---|
55 | {
|
---|
56 | //pick a number: shadow is 1/3 of object
|
---|
57 | int as = qAlpha(s)/3;
|
---|
58 | int av = qAlpha(v);
|
---|
59 | if ( as == 0 || av == 255 )
|
---|
60 | return v;
|
---|
61 |
|
---|
62 | int a = as + av -(as*av)/255;
|
---|
63 |
|
---|
64 |
|
---|
65 | int r = blendComponent( qRed(v),av, qRed(s), as)/a;
|
---|
66 | int g = blendComponent( qGreen(v),av, qGreen(s), as)/a;
|
---|
67 | int b = blendComponent( qBlue(v),av, qBlue(s), as)/a;
|
---|
68 |
|
---|
69 | return qRgba(r,g,b,a);
|
---|
70 | }
|
---|
71 |
|
---|
72 | int main( int*, char**)
|
---|
73 | {
|
---|
74 | QImage *img;
|
---|
75 |
|
---|
76 | img = new QImage( "in.png" );
|
---|
77 | int w,h;
|
---|
78 | int y;
|
---|
79 | img->setAlphaBuffer( TRUE );
|
---|
80 | *img = img->convertDepth( 32 );
|
---|
81 | w = img->width();
|
---|
82 | h = img->height();
|
---|
83 | #if 0
|
---|
84 | for ( y = 0; y < h; y ++ ) {
|
---|
85 | uint *line = (uint*)img->scanLine( y );
|
---|
86 | for ( int x = 0; x < w; x++ ) {
|
---|
87 | uint pixel = line[x];
|
---|
88 | int r = qRed(pixel);
|
---|
89 | int g = qGreen(pixel);
|
---|
90 | int b = qBlue(pixel);
|
---|
91 | int min = QMIN( r, QMIN( g, b ) );
|
---|
92 | int max = QMAX( r, QMAX( g, b ) );
|
---|
93 | r -= min;
|
---|
94 | g -= min;
|
---|
95 | b -= min;
|
---|
96 | if ( max !=min ) {
|
---|
97 | r = (r*255)/(max-min);
|
---|
98 | g = (g*255)/(max-min);
|
---|
99 | b = (b*255)/(max-min);
|
---|
100 | }
|
---|
101 | int a = 255-min;
|
---|
102 | a -= (max-min)/3; //hack more transparency for colors.
|
---|
103 | line[x] = qRgba( r, g, b, a );
|
---|
104 | }
|
---|
105 | }
|
---|
106 | #endif
|
---|
107 | *img = img->smoothScale( w/2, h/2 );
|
---|
108 |
|
---|
109 | qDebug( "saving out.png");
|
---|
110 | img->save( "out.png", "PNG" );
|
---|
111 |
|
---|
112 | w = img->width();
|
---|
113 | h = img->height();
|
---|
114 |
|
---|
115 | QImage *img2 = new QImage( w, h, 32 );
|
---|
116 | img2->setAlphaBuffer( TRUE );
|
---|
117 | for ( y = 0; y < h; y++ ) {
|
---|
118 | for ( int x = 0; x < w; x++ ) {
|
---|
119 | QRgb shader = img->pixel( x, y );
|
---|
120 |
|
---|
121 | int as = qAlpha(shader)/3;
|
---|
122 |
|
---|
123 | int r = (qRed(shader)*(255-as))/255;
|
---|
124 | int g = (qGreen(shader)*(255-as))/255;
|
---|
125 | int b = (qBlue(shader)*(255-as))/255;
|
---|
126 |
|
---|
127 | img2->setPixel( x, y, qRgba(r,g,b,as) );
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | img2->save( "outshade.png", "PNG" );
|
---|
132 |
|
---|
133 | }
|
---|