Use internal SNAPSHOT couplings again
[trakem2.git] / TrakEM2_ / src / main / java / ini / trakem2 / display / graphics / MultiplyARGBComposite.java
blobc9774aa4b2aae6ddf2d1688697000df997e9ed6b
1 /**
2 * License: GPL
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License 2
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 package ini.trakem2.display.graphics;
19 import java.awt.Composite;
20 import java.awt.CompositeContext;
21 import java.awt.RenderingHints;
22 import java.awt.image.ColorModel;
23 import java.awt.image.Raster;
24 import java.awt.image.WritableRaster;
26 import mpicbg.util.Util;
28 /**
30 * @author Stephan Saalfeld <saalfeld@mpi-cbg.de>
31 * @version 0.1b
33 public class MultiplyARGBComposite implements Composite
35 static private interface Composer
37 public void compose( final int[] src, final int[] dst, final float alpha );
39 final static private class ARGB2ARGB implements Composer
41 final public void compose( final int[] src, final int[] dst, final float alpha )
43 final float srcAlpha = src[ 3 ] / 255.0f * alpha;
44 final float dstAlpha = 1.0f - srcAlpha;
46 dst[ 0 ] = Math.max( 0, Math.min( 255, Util.round( ( srcAlpha * src[ 0 ] * dst[ 0 ] ) / 255.0f + dst[ 0 ] * dstAlpha ) ) );
47 dst[ 1 ] = Math.max( 0, Math.min( 255, Util.round( ( srcAlpha * src[ 1 ] * dst[ 1 ] ) / 255.0f + dst[ 1 ] * dstAlpha ) ) );
48 dst[ 2 ] = Math.max( 0, Math.min( 255, Util.round( ( srcAlpha * src[ 2 ] * dst[ 2 ] ) / 255.0f + dst[ 2 ] * dstAlpha ) ) );
49 dst[ 3 ] = 255;
52 final static private class RGB2ARGB implements Composer
54 final public void compose( final int[] src, final int[] dst, final float alpha )
56 final float dstAlpha = 1.0f - alpha;
58 dst[ 0 ] = Math.max( 0, Math.min( 255, Util.round( ( alpha * src[ 0 ] * dst[ 0 ] ) / 255.0f + dst[ 0 ] * dstAlpha ) ) );
59 dst[ 1 ] = Math.max( 0, Math.min( 255, Util.round( ( alpha * src[ 1 ] * dst[ 1 ] ) / 255.0f + dst[ 1 ] * dstAlpha ) ) );
60 dst[ 2 ] = Math.max( 0, Math.min( 255, Util.round( ( alpha * src[ 2 ] * dst[ 2 ] ) / 255.0f + dst[ 2 ] * dstAlpha ) ) );
61 dst[ 3 ] = 255;
64 final static private class Gray2ARGB implements Composer
66 final public void compose( final int[] src, final int[] dst, final float alpha )
68 final float dstAlpha = 1.0f - alpha;
70 dst[ 0 ] = Math.max( 0, Math.min( 255, Util.round( ( alpha * src[ 0 ] * dst[ 0 ] ) / 255.0f + dst[ 0 ] * dstAlpha ) ) );
71 dst[ 1 ] = Math.max( 0, Math.min( 255, Util.round( ( alpha * src[ 0 ] * dst[ 1 ] ) / 255.0f + dst[ 1 ] * dstAlpha ) ) );
72 dst[ 2 ] = Math.max( 0, Math.min( 255, Util.round( ( alpha * src[ 0 ] * dst[ 2 ] ) / 255.0f + dst[ 2 ] * dstAlpha ) ) );
73 dst[ 3 ] = 255;
77 static private MultiplyARGBComposite instance = new MultiplyARGBComposite();
79 final private float alpha;
81 public static MultiplyARGBComposite getInstance( final float alpha )
83 if ( alpha == 1.0f ) { return instance; }
84 return new MultiplyARGBComposite( alpha );
87 private MultiplyARGBComposite()
89 this.alpha = 1.0f;
92 private MultiplyARGBComposite( final float alpha )
94 this.alpha = alpha;
97 public CompositeContext createContext( ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints )
99 final Composer c;
100 if ( srcColorModel.getNumColorComponents() > 1 )
102 if ( srcColorModel.hasAlpha() )
103 c = new ARGB2ARGB();
104 else
105 c = new RGB2ARGB();
107 else
108 c = new Gray2ARGB();
110 return new CompositeContext()
112 private Composer composer = c;
113 public void compose( Raster src, Raster dstIn, WritableRaster dstOut )
115 final int[] srcPixel = new int[ 4 ];
116 final int[] dstInPixel = new int[ 4 ];
118 for ( int x = 0; x < dstOut.getWidth(); x++ )
120 for ( int y = 0; y < dstOut.getHeight(); y++ )
122 src.getPixel( x, y, srcPixel );
123 dstIn.getPixel( x, y, dstInPixel );
125 composer.compose( srcPixel, dstInPixel, alpha );
127 dstOut.setPixel( x, y, dstInPixel );
132 public void dispose()