Use internal SNAPSHOT couplings again
[trakem2.git] / TrakEM2_ / src / main / java / ini / trakem2 / display / graphics / DifferenceARGBComposite.java
blobce2f8f34675f7531067b0140dc889c801ea5549b
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 DifferenceARGBComposite 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;
45 dst[ 0 ] = Math.max( 0, Math.min( 255, Util.round( Math.abs( dst[ 0 ] - src[ 0 ] * srcAlpha ) ) ) );
46 dst[ 1 ] = Math.max( 0, Math.min( 255, Util.round( Math.abs( dst[ 1 ] - src[ 1 ] * srcAlpha ) ) ) );
47 dst[ 2 ] = Math.max( 0, Math.min( 255, Util.round( Math.abs( dst[ 2 ] - src[ 2 ] * srcAlpha ) ) ) );
48 dst[ 3 ] = 255;
51 final static private class RGB2ARGB implements Composer
53 final public void compose( final int[] src, final int[] dst, final float alpha )
55 dst[ 0 ] = Math.max( 0, Math.min( 255, Util.round( Math.abs( dst[ 0 ] - src[ 0 ] * alpha ) ) ) );
56 dst[ 1 ] = Math.max( 0, Math.min( 255, Util.round( Math.abs( dst[ 1 ] - src[ 1 ] * alpha ) ) ) );
57 dst[ 2 ] = Math.max( 0, Math.min( 255, Util.round( Math.abs( dst[ 2 ] - src[ 2 ] * alpha ) ) ) );
58 dst[ 3 ] = 255;
61 final static private class Gray2ARGB implements Composer
63 final public void compose( final int[] src, final int[] dst, final float alpha )
65 dst[ 0 ] = Math.max( 0, Math.min( 255, Util.round( Math.abs( dst[ 0 ] - src[ 0 ] * alpha ) ) ) );
66 dst[ 1 ] = Math.max( 0, Math.min( 255, Util.round( Math.abs( dst[ 1 ] - src[ 0 ] * alpha ) ) ) );
67 dst[ 2 ] = Math.max( 0, Math.min( 255, Util.round( Math.abs( dst[ 2 ] - src[ 0 ] * alpha ) ) ) );
68 dst[ 3 ] = 255;
72 static private DifferenceARGBComposite instance = new DifferenceARGBComposite();
74 final private float alpha;
76 public static DifferenceARGBComposite getInstance( final float alpha )
78 if ( alpha == 1.0f ) { return instance; }
79 return new DifferenceARGBComposite( alpha );
82 private DifferenceARGBComposite()
84 this.alpha = 1.0f;
87 private DifferenceARGBComposite( final float alpha )
89 this.alpha = alpha;
92 public CompositeContext createContext( ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints )
94 final Composer c;
95 if ( srcColorModel.getNumColorComponents() > 1 )
97 if ( srcColorModel.hasAlpha() )
98 c = new ARGB2ARGB();
99 else
100 c = new RGB2ARGB();
102 else
103 c = new Gray2ARGB();
105 return new CompositeContext()
107 private Composer composer = c;
108 public void compose( Raster src, Raster dstIn, WritableRaster dstOut )
110 final int[] srcPixel = new int[ 4 ];
111 final int[] dstInPixel = new int[ 4 ];
113 for ( int x = 0; x < dstOut.getWidth(); x++ )
115 for ( int y = 0; y < dstOut.getHeight(); y++ )
117 src.getPixel( x, y, srcPixel );
118 dstIn.getPixel( x, y, dstInPixel );
120 composer.compose( srcPixel, dstInPixel, alpha );
122 dstOut.setPixel( x, y, dstInPixel );
127 public void dispose()