/*
 * Image/J Plugins
 * Copyright (C) 2002-2011 Jarek Sacha
 * Author's email: jsacha at users dot sourceforge dot net
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Latest release available at http://sourceforge.net/projects/ij-plugins/
 */
package net.sf.ij_plugins.color;

import ij.process.ByteProcessor;
import ij.process.ColorProcessor;
import net.sf.ij_plugins.util.Validate;

/**
 * <p/>
 *
 * @author Jarek Sacha
 * @version $Revision: 1.1 $
 * @since Apr 5, 2005 9:20:35 PM
 */
public class ColorProcessorUtils {
    private ColorProcessorUtils() {
    }

    /**
     * Splits ColorProcessor into ByteProcessors representing each of three bands (red, gree, and blue).
     *
     * @param cp input color processor
     * @return ByteProcessor for each band.
     * @see #mergeRGB(ij.process.ByteProcessor[])
     */
    static public ByteProcessor[] splitRGB(final ColorProcessor cp) {
        final int width = cp.getWidth();
        final int height = cp.getHeight();

        final ByteProcessor redBp = new ByteProcessor(width, height);
        final ByteProcessor greenBp = new ByteProcessor(width, height);
        final ByteProcessor blueBp = new ByteProcessor(width, height);

        final byte[] redPixels = (byte[]) redBp.getPixels();
        final byte[] greenPixels = (byte[]) greenBp.getPixels();
        final byte[] bluePixels = (byte[]) blueBp.getPixels();

        cp.getRGB(redPixels, greenPixels, bluePixels);

        return new ByteProcessor[]{redBp, greenBp, blueBp};
    }

    /**
     * Merges RGB bands into a ColorProcessor.
     *
     * @param bps ByteProcessor for red, green, and blue band.
     * @return merged bands
     */
    static public ColorProcessor mergeRGB(final ByteProcessor[] bps) {

        Validate.argumentNotNull(bps, "bps");
        if (bps.length != 3) {
            throw new IllegalArgumentException("Size of array 'bps' has to equal 3, got "
                    + bps.length + ".");
        }

        final int width = bps[0].getWidth();
        final int height = bps[0].getHeight();

        if (!(width == bps[1].getWidth() && height == bps[1].getHeight()
                && width == bps[2].getWidth() && height == bps[2].getHeight())) {
            throw new IllegalArgumentException("All imput processor have to be of the same size.");
        }

        final byte[][] pixels = new byte[3][];
        for (int i = 0; i < bps.length; i++) {
            pixels[i] = (byte[]) bps[i].getPixels();
        }

        final ColorProcessor dest = new ColorProcessor(width, height);

        dest.setRGB(pixels[0], pixels[1], pixels[2]);

        return dest;
    }
}

@@ -40,17 +40,17 @@
 2002-201
-1
+2
  Jarek S
@@ -968,16 +968,38 @@
 color;%0A%0A
+import ij.ImageStack;%0A
 import i
@@ -1109,12 +1109,55 @@
 %0A * 
-%3Cp/%3E
+Helper methods for working with ColorProcessor.
 %0A *%0A
@@ -1183,37 +1183,8 @@
 cha%0A
- * @version $Revision: 1.1 $%0A
  * @
@@ -1223,16 +1223,22 @@
 %0Apublic 
+final 
 class Co
@@ -1257,16 +1257,17 @@
 Utils %7B%0A
+%0A
     priv
@@ -1401,16 +1401,17 @@
 ed, gree
+n
 , and bl
@@ -1567,27 +1567,27 @@
  */%0A    
-static publ
+public stat
 ic ByteP
@@ -1629,24 +1629,69 @@
 essor cp) %7B%0A
+        Validate.argumentNotNull(cp, %22cp%22);%0A%0A
         fina
@@ -2459,19 +2459,19 @@
     
-static publ
+public stat
 ic C
@@ -2522,17 +2522,16 @@
  bps) %7B%0A
-%0A
         
@@ -2576,19 +2576,31 @@
         
-if 
+Validate.isTrue
 (bps.len
@@ -2607,63 +2607,14 @@
 gth 
-!= 3) %7B%0A            throw new IllegalArgumentException(
+== 3, 
 %22Siz
@@ -2655,28 +2655,8 @@
 ot %22
-%0A                   
  + b
@@ -2668,34 +2668,24 @@
 ngth + %22.%22);
-%0A        %7D
 %0A%0A        fi
@@ -2975,17 +2975,17 @@
 n(%22All i
-m
+n
 put proc
@@ -3344,11 +3344,602 @@
 t;%0A    %7D
+%0A%0A    /**%0A     * Convert RGB image to a stack of ByteProcessors.%0A     *%0A     * @param cp RGB image%0A     * @return RGB stack%0A     * @see #splitRGB(ij.process.ColorProcessor)%0A     */%0A    public static ImageStack toStack(final ColorProcessor cp) %7B%0A        Validate.argumentNotNull(cp, %22cp%22);%0A%0A        final ByteProcessor%5B%5D rgb = splitRGB(cp);%0A        final String%5B%5D labels = %7B%22R%22, %22G%22, %22B%22%7D;%0A        final ImageStack stack = new ImageStack(cp.getWidth(), cp.getHeight());%0A        for (int i = 0; i %3C 3; i++) %7B%0A            stack.addSlice(labels%5Bi%5D, rgb%5Bi%5D);%0A        %7D%0A        return stack;%0A    %7D
 %0A%7D%0A

Add method for concerting RGB images to a stack.