| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia 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 | }
|
|---|