From 1a2b56121b4eb03dc2b20b09ec9bb69c885a6066 Mon Sep 17 00:00:00 2001 From: "nor..67" Date: Wed, 19 Sep 2012 22:52:03 +0000 Subject: [PATCH] SDK: - add invert filter (thanks to @jmaasing) git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9756 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../textureeditor/ImageEditorComponent.java | 20 ++++- .../textureeditor/filters/InvertFilter.java | 73 ++++++++++++++++++ .../gde/textureeditor/resources/invert.png | Bin 0 -> 228 bytes 3 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/filters/InvertFilter.java create mode 100644 sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/resources/invert.png diff --git a/sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/ImageEditorComponent.java b/sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/ImageEditorComponent.java index a124f74e4..6ad7579ac 100644 --- a/sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/ImageEditorComponent.java +++ b/sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/ImageEditorComponent.java @@ -36,6 +36,7 @@ import org.openide.windows.TopComponent; import com.jme3.gde.textureeditor.filters.BrightFilter; import com.jme3.gde.textureeditor.filters.BumpMapFilter; import com.jme3.gde.textureeditor.filters.GrayscaleFilter; +import com.jme3.gde.textureeditor.filters.InvertFilter; import com.jme3.gde.textureeditor.filters.MirrorFilter; import com.jme3.gde.textureeditor.filters.ResizeFilter; import com.jme3.gde.textureeditor.filters.RotateLeftFilter; @@ -47,7 +48,6 @@ import org.openide.DialogDisplayer; import org.openide.NotifyDescriptor; import org.openide.NotifyDescriptor.Confirmation; import org.openide.NotifyDescriptor.Message; -import org.openide.awt.UndoRedo; import org.openide.cookies.SaveCookie; import org.openide.filesystems.FileChooserBuilder; import org.openide.filesystems.FileObject; @@ -369,6 +369,13 @@ public class ImageEditorComponent implements EditorToolTarget { final JMenuItem gray = filters.add("Grayscale"); final JMenuItem bright = filters.add("Brightness"); final JMenuItem spheremap = filters.add("SphereMapped"); + final JMenu invertMenu = new JMenu("Invert") ; + final JMenuItem invertAll = invertMenu.add("All") ; + final JMenuItem invertAlpha = invertMenu.add("Alpha") ; + final JMenuItem invertRed = invertMenu.add("Red") ; + final JMenuItem invertGreen = invertMenu.add("Green") ; + final JMenuItem invertBlue = invertMenu.add("Blue") ; + filters.add(invertMenu) ; ActionListener al = new ActionListener() { @@ -386,8 +393,17 @@ public class ImageEditorComponent implements EditorToolTarget { spawnEditor(BrightFilter.create().filter(editedImage, ImageEditorComponent.this)); } else if (source == spheremap) { spawnEditor(SphereMappedFilter.create().filter(editedImage)); + } else if (source == invertAll) { + spawnEditor(new InvertFilter().filter(editedImage, InvertFilter.Channel.All)); + } else if (source == invertAlpha) { + spawnEditor(new InvertFilter().filter(editedImage, InvertFilter.Channel.Alpha)); + } else if (source == invertRed) { + spawnEditor(new InvertFilter().filter(editedImage, InvertFilter.Channel.Red)); + } else if (source == invertGreen) { + spawnEditor(new InvertFilter().filter(editedImage, InvertFilter.Channel.Green)); + } else if (source == invertBlue) { + spawnEditor(new InvertFilter().filter(editedImage, InvertFilter.Channel.Blue)); } - } }; diff --git a/sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/filters/InvertFilter.java b/sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/filters/InvertFilter.java new file mode 100644 index 000000000..925319430 --- /dev/null +++ b/sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/filters/InvertFilter.java @@ -0,0 +1,73 @@ +package com.jme3.gde.textureeditor.filters; + +import java.awt.image.BufferedImage; + +/** + * Invert channels in an image. It takes the ARGB color component and set it to 255 - value. + */ +public class InvertFilter implements BufferedImageFilter { + + public enum Channel { + + All, Red, Green, Blue, Alpha; + } + + public BufferedImage filter(BufferedImage source, Object... args) { + final Channel channel; + if ((args == null) || (args.length < 1) || (!(args[0] instanceof Channel))) { + channel = Channel.All; + } else { + channel = (Channel) args[0]; + } + BufferedImage result = new BufferedImage( + source.getWidth(), + source.getHeight(), + BufferedImage.TYPE_INT_ARGB); + final int height = source.getHeight(); + final int width = source.getWidth(); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + final int sourceARGB = source.getRGB(x, y); + final int targetARGB = invert(sourceARGB, channel); + result.setRGB(x, y, targetARGB); + } + } + return result; + } + + private int invert(final int sourceARGB, final Channel channel) { + int a = (sourceARGB >> 24) & 0xff; + int r = (sourceARGB >> 16) & 0xff; + int g = (sourceARGB >> 8) & 0xff; + int b = sourceARGB & 0xff; + + switch (channel) { + case Alpha: + a = 255 - a; + break; + case Red: + r = 255 - r; + break; + case Green: + g = 255 - g; + break; + case Blue: + b = 255 - b; + break; + default: + a = 255 - a; + r = 255 - r; + g = 255 - g; + b = 255 - b; + } + return packPixel(a, r, g, b); + } + + private int packPixel(int alpha, int red, int green, int blue) { + final int argb = ((alpha << 24) & 0xff000000) + | ((red << 16) & 0x00ff0000) + | ((green << 8) & 0x0000ff00) + | (blue & 0x000000ff); + return argb; + } +} diff --git a/sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/resources/invert.png b/sdk/jme3-texture-editor/src/com/jme3/gde/textureeditor/resources/invert.png new file mode 100644 index 0000000000000000000000000000000000000000..399a5236c377221f2f31514253620c84ceea874c GIT binary patch literal 228 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM1SIoCSFHz9jKx9jP7LeL$-D$|I14-?iy0WW zg+Z8+Vb&aw@=i||#}JFtYp)mz9Z=wK2|Q&hW&SK<$`XU*j>1LjtoA=Ogf-Kvj_*A^`5I3X4? zRaZmX=8A?2%N8!?y=HfF*`&0M1!R&_Ii6{!7OOF5m literal 0 HcmV?d00001