| 1 | #include <qimage.h>
|
|---|
| 2 | #include <qcolor.h>
|
|---|
| 3 |
|
|---|
| 4 | static inline int blendComponent( int v, int av, int s, int as )
|
|---|
| 5 | {
|
|---|
| 6 | //shadow gets a color inversely proportional to the
|
|---|
| 7 | //alpha value
|
|---|
| 8 | s = (s*(255-as))/255;
|
|---|
| 9 | //then do standard blending
|
|---|
| 10 | return as*s + av*v -(av*as*s)/255;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | static inline QRgb blendShade( QRgb v, QRgb s )
|
|---|
| 14 | {
|
|---|
| 15 | //pick a number: shadow is 1/3 of object
|
|---|
| 16 | int as = qAlpha(s)/3;
|
|---|
| 17 | int av = qAlpha(v);
|
|---|
| 18 | if ( as == 0 || av == 255 )
|
|---|
| 19 | return v;
|
|---|
| 20 |
|
|---|
| 21 | int a = as + av -(as*av)/255;
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | int r = blendComponent( qRed(v),av, qRed(s), as)/a;
|
|---|
| 25 | int g = blendComponent( qGreen(v),av, qGreen(s), as)/a;
|
|---|
| 26 | int b = blendComponent( qBlue(v),av, qBlue(s), as)/a;
|
|---|
| 27 |
|
|---|
| 28 | return qRgba(r,g,b,a);
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | int main( int*, char**)
|
|---|
| 32 | {
|
|---|
| 33 | QImage *img;
|
|---|
| 34 |
|
|---|
| 35 | img = new QImage( "in.png" );
|
|---|
| 36 | int w,h;
|
|---|
| 37 | int y;
|
|---|
| 38 | img->setAlphaBuffer( TRUE );
|
|---|
| 39 | *img = img->convertDepth( 32 );
|
|---|
| 40 | w = img->width();
|
|---|
| 41 | h = img->height();
|
|---|
| 42 | #if 0
|
|---|
| 43 | for ( y = 0; y < h; y ++ ) {
|
|---|
| 44 | uint *line = (uint*)img->scanLine( y );
|
|---|
| 45 | for ( int x = 0; x < w; x++ ) {
|
|---|
| 46 | uint pixel = line[x];
|
|---|
| 47 | int r = qRed(pixel);
|
|---|
| 48 | int g = qGreen(pixel);
|
|---|
| 49 | int b = qBlue(pixel);
|
|---|
| 50 | int min = QMIN( r, QMIN( g, b ) );
|
|---|
| 51 | int max = QMAX( r, QMAX( g, b ) );
|
|---|
| 52 | r -= min;
|
|---|
| 53 | g -= min;
|
|---|
| 54 | b -= min;
|
|---|
| 55 | if ( max !=min ) {
|
|---|
| 56 | r = (r*255)/(max-min);
|
|---|
| 57 | g = (g*255)/(max-min);
|
|---|
| 58 | b = (b*255)/(max-min);
|
|---|
| 59 | }
|
|---|
| 60 | int a = 255-min;
|
|---|
| 61 | a -= (max-min)/3; //hack more transparency for colors.
|
|---|
| 62 | line[x] = qRgba( r, g, b, a );
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | #endif
|
|---|
| 66 | *img = img->smoothScale( w/2, h/2 );
|
|---|
| 67 |
|
|---|
| 68 | qDebug( "saving out.png");
|
|---|
| 69 | img->save( "out.png", "PNG" );
|
|---|
| 70 |
|
|---|
| 71 | w = img->width();
|
|---|
| 72 | h = img->height();
|
|---|
| 73 |
|
|---|
| 74 | QImage *img2 = new QImage( w, h, 32 );
|
|---|
| 75 | img2->setAlphaBuffer( TRUE );
|
|---|
| 76 | for ( y = 0; y < h; y++ ) {
|
|---|
| 77 | for ( int x = 0; x < w; x++ ) {
|
|---|
| 78 | QRgb shader = img->pixel( x, y );
|
|---|
| 79 |
|
|---|
| 80 | int as = qAlpha(shader)/3;
|
|---|
| 81 |
|
|---|
| 82 | int r = (qRed(shader)*(255-as))/255;
|
|---|
| 83 | int g = (qGreen(shader)*(255-as))/255;
|
|---|
| 84 | int b = (qBlue(shader)*(255-as))/255;
|
|---|
| 85 |
|
|---|
| 86 | img2->setPixel( x, y, qRgba(r,g,b,as) );
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | img2->save( "outshade.png", "PNG" );
|
|---|
| 91 |
|
|---|
| 92 | }
|
|---|