Example usage for java.awt.image Raster getSamples

List of usage examples for java.awt.image Raster getSamples

Introduction

In this page you can find the example usage for java.awt.image Raster getSamples.

Prototype

public double[] getSamples(int x, int y, int w, int h, int b, double[] dArray) 

Source Link

Document

Returns the samples for a specified band for a specified rectangle of pixels in a double array, one sample per array element.

Usage

From source file:com.occamlab.te.parsers.ImageParser.java

private static String checkTransparentNodata(BufferedImage buffImage, Node node) throws Exception {
    String transparentNodata = "NA";
    boolean noData = false;
    boolean transparent = true;
    int[] bandIndexes = new int[4];
    bandIndexes[0] = 3; // A
    bandIndexes[1] = 2; // B
    bandIndexes[2] = 1; // G
    bandIndexes[3] = 0; // R
    Raster raster = buffImage.getRaster();
    int minx = raster.getMinX();
    int maxx = minx + raster.getWidth();
    int miny = raster.getMinY();
    int maxy = miny + raster.getHeight();
    int bands[][] = new int[bandIndexes.length][raster.getWidth()];
    for (int y = miny; y < maxy; y++) {
        for (int i = 0; i < bandIndexes.length; i++) {
            raster.getSamples(minx, y, maxx, 1, bandIndexes[i], bands[i]);
        }// www . j  av a2  s .c o m
        for (int x = minx; x < maxx; x++) {
            int a = bands[0][x];
            int b = bands[1][x];
            int g = bands[2][x];
            int r = bands[3][x];
            if (b == 0 && g == 0 && r == 0) {
                noData = true;
                if (a != 0) {
                    transparent = false;
                }
            }
        }
    }
    transparentNodata = (noData) ? (transparent) ? "true" : "false" : "NA";
    return transparentNodata;
}

From source file:tarea1.histogram.java

private ChartPanel createChartPanel() {
    // dataset/*w ww. j a v  a  2  s  .  com*/
    HistogramDataset dataset = new HistogramDataset();
    Raster raster = image.getRaster();
    final int w = image.getWidth();
    final int h = image.getHeight();
    double[] r = new double[w * h];
    r = raster.getSamples(0, 0, w, h, 0, r);
    dataset.addSeries("Red", r, BINS);
    r = raster.getSamples(0, 0, w, h, 1, r);
    dataset.addSeries("Green", r, BINS);
    r = raster.getSamples(0, 0, w, h, 2, r);
    dataset.addSeries("Blue", r, BINS);
    // chart
    JFreeChart chart = ChartFactory.createHistogram("Histogram", "Value", "Count", dataset,
            PlotOrientation.VERTICAL, true, true, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
    renderer.setBarPainter(new StandardXYBarPainter());
    // translucent red, green & blue
    Paint[] paintArray = { new Color(0x80ff0000, true), new Color(0x8000ff00, true),
            new Color(0x800000ff, true) };
    plot.setDrawingSupplier(
            new DefaultDrawingSupplier(paintArray, DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
                    DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                    DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                    DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                    DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
    ChartPanel panel = new ChartPanel(chart);
    panel.setMouseWheelEnabled(true);
    return panel;
}

From source file:GraphicsUtil.java

public static void copyBand(Raster src, Rectangle sR, int sBand, WritableRaster dst, Rectangle dR, int dBand) {
    int dy = dR.y - sR.y;
    int dx = dR.x - sR.x;
    sR = sR.intersection(src.getBounds());
    dR = dR.intersection(dst.getBounds());
    int width, height;
    if (dR.width < sR.width)
        width = dR.width;/*from  ww w.  java 2  s.com*/
    else
        width = sR.width;
    if (dR.height < sR.height)
        height = dR.height;
    else
        height = sR.height;

    int x = sR.x + dx;
    int[] samples = null;
    for (int y = sR.y; y < sR.y + height; y++) {
        samples = src.getSamples(sR.x, y, width, 1, sBand, samples);
        dst.setSamples(x, y + dy, width, 1, dBand, samples);
    }
}

From source file:task5.Histogram.java

private ChartPanel createChartPanel() {
    // dataset//  ww w  .  j a  v  a2  s .c  om
    dataset = new HistogramDataset();
    Raster raster = img.getRaster();
    final int w = img.getWidth();
    final int h = img.getHeight();
    double[] r = new double[w * h + 1];
    r = raster.getSamples(0, 0, w, h, 0, r);
    dataset.addSeries("Red", r, BINS);
    r = raster.getSamples(0, 0, w, h, 1, r);
    dataset.addSeries("Green", r, BINS);
    r = raster.getSamples(0, 0, w, h, 2, r);
    dataset.addSeries("Blue", r, BINS);
    // chart
    JFreeChart chart = ChartFactory.createHistogram("Histogram", "Value", "Count", dataset,
            PlotOrientation.VERTICAL, true, true, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    renderer = (XYBarRenderer) plot.getRenderer();
    renderer.setBarPainter(new StandardXYBarPainter());
    // translucent red, green & blue
    Paint[] paintArray = { new Color(0x80ff0000, true), new Color(0x8000ff00, true),
            new Color(0x800000ff, true) };
    plot.setDrawingSupplier(
            new DefaultDrawingSupplier(paintArray, DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
                    DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                    DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                    DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                    DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
    ChartPanel panel = new ChartPanel(chart);
    panel.setMouseWheelEnabled(true);
    return panel;
}

From source file:org.geopublishing.atlasStyler.classification.RasterClassification.java

/**
 * This is where the magic happens. Here the attributes of the features are
 * summarized in a {@link DynamicBin1D} class.
 * /*ww  w  .jav  a 2s .c om*/
 * @throws IOException
 */
@Override
synchronized public DynamicBin1D getStatistics() throws InterruptedException, IOException {

    cancelCalculation.set(false);

    if (stats == null) {
        GridCoverage2D coverage = getStyledRaster().getGeoObject().read(null);

        stats = new DynamicBin1D();

        noDataValuesCount.set(0);

        final RenderedImage rim = coverage.getRenderedImage();

        long size = Long.valueOf(rim.getHeight()) * Long.valueOf(rim.getWidth());
        long maxPixels = 3000000l;
        if (size > maxPixels) {
            setSubsampling((int) (size / maxPixels));
            LOGGER.info("Subsampling to every " + getSubsampling() + " pixel");
        }

        for (int row = 0; row < rim.getHeight(); row++) {

            if (row % getSubsampling() != 0) {
                // Skipping this line for Subsampling
                continue;
            } else {
                // DO
                //               System.out.println("");
            }

            int x1 = 0;
            int w = rim.getWidth();

            int y1 = row;
            int h = 1;

            Raster data = rim.getData(new Rectangle(x1, y1, w, h));
            double[] values = data.getSamples(0, y1, w, h, getBand(), (double[]) null);

            final DoubleArrayList doubleArrayList = new DoubleArrayList(values);

            if (getStyledRaster().getNodataValue() != null) {
                int sizewithNodata = doubleArrayList.size();
                doubleArrayList
                        .removeAll(new DoubleArrayList(new double[] { getStyledRaster().getNodataValue() }));
                noDataValuesCount.addAndGet(sizewithNodata - doubleArrayList.size());
            }

            stats.addAllOf(doubleArrayList);

            //            LOGGER.debug("Added "+doubleArrayList.size()+" to statistics");
            //            LOGGER.debug(stats.size()+" in stats");
            doubleArrayList.clear();
        }

    }
    return stats;
}

From source file:tarea1.controlador.java

public void seleccionOpcion(int z) throws IOException, Exception {
    switch (z) {/*w w w.  j  ava2s  .  co  m*/
    case 1: {
        //ELEGIR UN ARCHIVO//
        //EN CASO DE QUERER CAMBIAR EL TIPO DE ARCHIVO.
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "bmp");
        JFileChooser abrir = new JFileChooser();
        abrir.setFileSelectionMode(JFileChooser.FILES_ONLY);
        abrir.setFileFilter(filter);
        abrir.setCurrentDirectory(new File(System.getProperty("user.home")));
        int result = abrir.showOpenDialog(inicio);
        if (result == JFileChooser.APPROVE_OPTION) {
            // se seleciona el archivo de imagen original
            File selectedFile = abrir.getSelectedFile();
            ruta = selectedFile.getAbsolutePath();
            System.out.println("El archivo es: " + ruta); //ruta
            img = ImageIO.read(new File(ruta)); //se lee el archivo
            rotate = false;
            zoomv = false;
            escalav = false;
            brillos = false;
            contrastes = false;
            undoDelete = false;
            undoIndex = 0;
            Change();
            inicio.setTitle("PDI: Tarea 3 -" + ruta);

        }
    }
        break;//end case 1

    case 2: //imagen en negativo
    {

        //se crea un buffer
        BufferedImage imagenNegativa = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
        //se convierten los colores a negativo y se va guardando en el buffer
        for (int y = 0; y < alto; y++) {
            for (int x = 0; x < ancho; x++) {
                int p = img.getRGB(x, y);
                //obtenermos el valor r g b a de cada pixel
                // int a = (p>>24)&0xff;
                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;
                //se resta el rbg
                r = truncate(255 - r);
                g = truncate(255 - g);
                b = truncate(255 - b);
                //se guarda el rgb
                p = (r << 16) | (g << 8) | b;
                imagenNegativa.setRGB(x, y, p);
            }
        }
        //PARA LOS ROTACIONES
        img = imagenNegativa;

        ancho = img.getWidth();
        alto = img.getHeight();
        //se crea un buffer
        imagenNegativa = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
        //se convierten los colores a negativo y se va guardando en el buffer
        for (int y = 0; y < alto; y++) {
            for (int x = 0; x < ancho; x++) {
                int p = original.getRGB(x, y);
                //obtenermos el valor r g b a de cada pixel
                int a = (p >> 24) & 0xff;
                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;
                //se resta el rbg
                r = 255 - r;
                g = 255 - g;
                b = 255 - b;
                //se guarda el rgb
                p = (a << 24) | (r << 16) | (g << 8) | b;
                imagenNegativa.setRGB(x, y, p);
            }
        }
        img = imagenNegativa;

        Change();
    }
        break;//end case 2

    case 3: //flip imagen vertical
    {

        //buffer para la imagen
        BufferedImage mirrorimgV = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
        //recorremos pixel a pixel tooooooooooooodo el buffer
        for (int i = 0; i < alto; i++) {
            for (int izquierda = 0, derecha = ancho - 1; izquierda < alto; izquierda++, derecha--) {
                int p = img.getRGB(izquierda, i);
                mirrorimgV.setRGB(derecha, i, p);
            }
        }
        img = mirrorimgV;
        Change();

    }
        break;//end case 3

    case 4://flip imagen horizontal
    {

        BufferedImage mirrorimgH = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);

        for (int i = 0; i < ancho; i++) {
            for (int arriba = 0, abajo = alto - 1; arriba < alto; arriba++, abajo--) {
                int p = img.getRGB(i, arriba);
                mirrorimgH.setRGB(i, abajo, p);
            }
        }
        img = mirrorimgH;
        Change();

    }
        break;//end case 4

    case 5: { //boton de reset

        //RESET
        File f = null;
        //leer image
        try {
            f = new File(ruta);
            rotate = false;
            zoomv = false;
            escalav = false;
            brillos = false;
            contrastes = false;
            undoDelete = false;
            undoIndex = 0;
            img = ImageIO.read(f);
        } catch (IOException e) {
            System.out.println(e);
        }

        Change();

    }
        break; //end case 5

    case 6: { //leer en formato binario

        FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "bmp");
        JFileChooser abrir = new JFileChooser();
        abrir.setFileSelectionMode(JFileChooser.FILES_ONLY);
        abrir.setFileFilter(filter);
        //abrir.setCurrentDirectory(new File(System.getProperty("user.home")));
        abrir.setCurrentDirectory(new File(System.getProperty("user.dir")));
        int result = abrir.showOpenDialog(inicio);
        if (result == JFileChooser.APPROVE_OPTION) {

            try {
                File selectedFile = abrir.getSelectedFile();
                ruta = selectedFile.getAbsolutePath();

                FileInputStream is = null;

                is = new FileInputStream(ruta);
                bmp.read(is);
                System.out.println("aqui");
                MemoryImageSource mis = bmp.crearImageSource();
                System.out.println("hola");
                Image im = Toolkit.getDefaultToolkit().createImage(mis);
                //Para poder colorcarlo en el label
                //Image image = createImage(new MemoryImageSource(bmp.crearImageSource()));
                BufferedImage newImage = new BufferedImage(im.getWidth(null), im.getHeight(null),
                        BufferedImage.TYPE_INT_RGB);
                //obtenemos la imagen que si se puede desplgar
                Graphics2D g = newImage.createGraphics();
                g.drawImage(im, 0, 0, null);
                g.dispose();

                img = newImage;
                rotate = false;
                zoomv = false;
                escalav = false;
                brillos = false;
                contrastes = false;
                undoDelete = false;
                undoIndex = 0;
                Change();

                //add img info
                inicio.setTitle("PDI: Tarea 3 -" + ruta);
                //dimensiones, profundidad de bits, Mb ocupados
                content = ("Size: " + (bmp.tamArchivo) / 1000 + "kb\nDimension: " + bmp.ancho + " x " + bmp.alto
                        + "\nBpp: " + bmp.bitsPorPixel + "bits");
                ancho = bmp.ancho;
                alto = bmp.alto;

            } catch (Exception ex) {
                Logger.getLogger(controlador.class.getName()).log(Level.SEVERE, null, ex);
            }

        } //end approval if
    }
        break; //end case 6

    //girar CW
    case 7: {

        BufferedImage new_Image = new BufferedImage(alto, ancho, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < ancho; i++) {
            for (int j = 0; j < alto; j++) {
                int p = img.getRGB(i, j);
                new_Image.setRGB(alto - j - 1, i, p);

            }
        }

        img = new_Image;
        Change();

    }
        break;//end case 7

    //girar CCW
    case 8: {

        BufferedImage new_Image = new BufferedImage(alto, ancho, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < ancho; i++) {
            for (int j = 0; j < alto; j++) {
                int p = img.getRGB(i, j);
                new_Image.setRGB(j, ancho - i - 1, p);

            }
        }

        img = new_Image;
        Change();

    }
        break;//end case 8

    case 9: { //Guardar Imagen

        FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "bmp");
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(filter);
        fileChooser.setDialogTitle("Save");
        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
        int userSelection = fileChooser.showSaveDialog(inicio);
        if (userSelection == JFileChooser.APPROVE_OPTION) {
            File fileToSave = fileChooser.getSelectedFile();
            System.out.println("Save as file: " + fileToSave.getAbsolutePath() + ".bmp");
            System.out.println("Save as: " + fileToSave.getName());
            bmp.saveMyLifeTonight(fileToSave, img);
        }
    }
        break;

    case 10: {
        //free rotation
        double anguloCartesiano = inicio.optionr;
        double aux;
        if (rotate == false) {
            original = img;
        }
        //para la ilusion de rotar sobre la "misma imagen"
        if (anguloCartesiano < 0) {

            aux = anguloCartesiano;
            anguloCartesiano = anguloCartesiano + angulo;
            angulo = anguloCartesiano;

        } else if (anguloCartesiano > 0) {

            aux = anguloCartesiano;
            anguloCartesiano = angulo + anguloCartesiano;
            angulo = anguloCartesiano;

        }

        anguloCartesiano = anguloCartesiano * Math.PI / 180;

        //CC coordinates
        int x, y;
        double distance, anguloPolar;
        int pisoX, techoX, pisoY, techoY;
        double rasterX, rasterY;
        // colores de los pixeles
        Color colorTL = null, colorTR, colorBL, colorBR = null;
        // interpolaciones
        double intX, intY;
        double rojoT, verdeT, azulT;
        double rojoB, verdeB, azulB;

        int centroX, centroY;

        centroX = original.getWidth() / 2;
        centroY = original.getHeight() / 2;

        BufferedImage imagenRotada = new BufferedImage(original.getWidth(), original.getHeight(),
                BufferedImage.TYPE_INT_ARGB);//fondo transparente

        for (int i = 0; i < original.getHeight(); ++i)
            for (int j = 0; j < original.getWidth(); ++j) {
                // convert raster to Cartesian
                x = j - centroX;
                y = centroY - i;

                // convert Cartesian to polar
                distance = Math.sqrt(x * x + y * y);
                anguloPolar = 0.0;
                if (x == 0) {
                    if (y == 0) {
                        // centre of image, no rotation needed
                        imagenRotada.setRGB(j, i, original.getRGB(j, i));

                        continue;
                    } else if (y < 0)
                        anguloPolar = 1.5 * Math.PI;
                    else
                        anguloPolar = 0.5 * Math.PI;
                } else
                    anguloPolar = Math.atan2((double) y, (double) x);

                // 
                anguloPolar -= anguloCartesiano;

                //polr a carte
                rasterX = distance * Math.cos(anguloPolar);
                rasterY = distance * Math.sin(anguloPolar);

                // cartesiano a raster
                rasterX = rasterX + (double) centroX;
                rasterY = (double) centroY - rasterY;

                pisoX = (int) (Math.floor(rasterX));
                pisoY = (int) (Math.floor(rasterY));
                techoX = (int) (Math.ceil(rasterX));
                techoY = (int) (Math.ceil(rasterY));

                // check bounds /// AQUIWWIUEI
                if (pisoX < 0 || techoX < 0 || pisoX >= original.getWidth() || techoX >= original.getWidth()
                        || pisoY < 0 || techoY < 0 || pisoY >= original.getHeight()
                        || techoY >= original.getHeight())
                    continue;

                intX = rasterX - (double) pisoX;
                intY = rasterY - (double) pisoY;

                colorTL = new Color(original.getRGB(pisoX, pisoY));
                colorTR = new Color(original.getRGB(techoX, pisoY));
                colorBL = new Color(original.getRGB(pisoX, techoY));
                colorBR = new Color(original.getRGB(techoX, techoY));

                // interpolacion horizontal top
                rojoT = (1 - intX) * colorTL.getRed() + intX * colorTR.getRed();
                verdeT = (1 - intX) * colorTL.getGreen() + intX * colorTR.getGreen();
                azulT = (1 - intX) * colorTL.getBlue() + intX * colorTR.getBlue();
                // interpolacion horizontal bot
                rojoB = (1 - intX) * colorBL.getRed() + intX * colorBR.getRed();
                verdeB = (1 - intX) * colorBL.getGreen() + intX * colorBR.getGreen();
                azulB = (1 - intX) * colorBL.getBlue() + intX * colorBR.getBlue();
                // interpolacion vertical
                int p = original.getRGB(j, i);
                int a = (p >> 24) & 0xff;
                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;
                r = truncate(Math.round((1 - intY) * rojoT + intY * rojoB));
                g = truncate(Math.round((1 - intY) * verdeT + intY * verdeB));
                b = truncate(Math.round((1 - intY) * azulT + intY * azulB));
                p = (a << 24) | (r << 16) | (g << 8) | b;
                imagenRotada.setRGB(j, i, p);

            }
        img = imagenRotada;
        rotate = true;
        inicio.jLabel3.setBounds(0, 0, ancho, alto);
        ImageIcon icon = new ImageIcon(img);
        inicio.jLabel3.setIcon(icon);

    }
        break; //case 10

    case 11: { //histogram

        //para recorrer todos los valores y obtener los samples
        /*
        for (y) {
            for (x) {
               pixel = raster.getDataElements(x, y, pixel);
            }
        }
                            */
        int BINS = 256;
        HistogramDataset dataset = new HistogramDataset();
        Raster raster = img.getRaster();

        double[] r = new double[ancho * alto];
        ChartPanel panelB = null;
        ChartPanel panelG = null;
        ChartPanel panelR = null;
        ChartPanel panel;

        if (bmp.bitsPorPixel == 1) {

            r = raster.getSamples(0, 0, ancho, alto, 0, r);
            ColorModel ColorM = img.getColorModel();

            dataset.addSeries("Grey", r, BINS);

            //de aqui para abajo es el plotting
            // chart all
            JFreeChart chart = ChartFactory.createHistogram("Histogram", "Value", "Count", dataset,
                    PlotOrientation.VERTICAL, true, true, false);
            XYPlot plot = (XYPlot) chart.getPlot();
            XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
            renderer.setBarPainter(new StandardXYBarPainter());

            Paint[] paintArray = { new Color(0x80ff0000, true) };
            plot.setDrawingSupplier(
                    new DefaultDrawingSupplier(paintArray, DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
            panel = new ChartPanel(chart);
            panel.setMouseWheelEnabled(true);

        } else {

            r = raster.getSamples(0, 0, ancho, alto, 0, r);
            dataset.addSeries("Red", r, BINS);
            r = raster.getSamples(0, 0, ancho, alto, 1, r);
            dataset.addSeries("Green", r, BINS);
            r = raster.getSamples(0, 0, ancho, alto, 2, r);
            dataset.addSeries("Blue", r, BINS);

            //de aqui para abajo es el plotting
            // chart all
            JFreeChart chart = ChartFactory.createHistogram("Histogram", "Value", "Count", dataset,
                    PlotOrientation.VERTICAL, true, true, false);
            XYPlot plot = (XYPlot) chart.getPlot();
            XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
            renderer.setBarPainter(new StandardXYBarPainter());
            // translucent red, green & blue
            Paint[] paintArray = { new Color(0x80ff0000, true), new Color(0x8000ff00, true),
                    new Color(0x800000ff, true) };
            plot.setDrawingSupplier(
                    new DefaultDrawingSupplier(paintArray, DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
            panel = new ChartPanel(chart);
            panel.setMouseWheelEnabled(true);

            //CHART Red
            HistogramDataset datasetR = new HistogramDataset();
            r = raster.getSamples(0, 0, ancho, alto, 0, r);
            datasetR.addSeries("Red", r, BINS);
            JFreeChart chartR = ChartFactory.createHistogram("Histogram B", "Value", "Count", datasetR,
                    PlotOrientation.VERTICAL, true, true, false);
            XYPlot plotR = (XYPlot) chartR.getPlot();
            XYBarRenderer rendererR = (XYBarRenderer) plotR.getRenderer();
            rendererR.setBarPainter(new StandardXYBarPainter());
            // translucent red, green & blue
            Paint[] paintArrayR = { new Color(0x80ff0000, true)

            };
            plotR.setDrawingSupplier(
                    new DefaultDrawingSupplier(paintArrayR, DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
            panelR = new ChartPanel(chartR);
            panelR.setMouseWheelEnabled(true);

            //CHART GREEN

            HistogramDataset datasetG = new HistogramDataset();
            r = raster.getSamples(0, 0, ancho, alto, 1, r);
            datasetG.addSeries("Green", r, BINS);
            JFreeChart chartG = ChartFactory.createHistogram("Histogram G ", "Value", "Count", datasetG,
                    PlotOrientation.VERTICAL, true, true, false);
            XYPlot plotG = (XYPlot) chartG.getPlot();
            XYBarRenderer rendererG = (XYBarRenderer) plotG.getRenderer();
            rendererG.setBarPainter(new StandardXYBarPainter());
            // translucent red, green & blue
            Paint[] paintArrayG = { new Color(0x8000ff00, true)

            };
            plotG.setDrawingSupplier(
                    new DefaultDrawingSupplier(paintArrayG, DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
            panelG = new ChartPanel(chartG);
            panelG.setMouseWheelEnabled(true);

            //CHART BLUE

            HistogramDataset datasetB = new HistogramDataset();
            r = raster.getSamples(0, 0, ancho, alto, 2, r);
            datasetB.addSeries("Blue", r, BINS);
            JFreeChart chartB = ChartFactory.createHistogram("Histogram B ", "Value", "Count", datasetB,
                    PlotOrientation.VERTICAL, true, true, false);
            XYPlot plotB = (XYPlot) chartB.getPlot();
            XYBarRenderer rendererB = (XYBarRenderer) plotB.getRenderer();
            rendererB.setBarPainter(new StandardXYBarPainter());
            // translucent red, green & blue
            Paint[] paintArrayB = { new Color(0x800000ff, true)

            };
            plotB.setDrawingSupplier(
                    new DefaultDrawingSupplier(paintArrayB, DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                            DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
            panelB = new ChartPanel(chartB);
            panelB.setMouseWheelEnabled(true);

        }

        //JTabbedPane jtp=new JTabbedPane();
        if (!viewH) {

            inicio.jTabbedPane1.addTab("Histogram", panel);
            inicio.jTabbedPane1.addTab("Histogram R", panelR);
            inicio.jTabbedPane1.addTab("Histogram G", panelG);
            inicio.jTabbedPane1.addTab("Histogram B", panelB);
            viewH = true;
        } else {
            inicio.jTabbedPane1.remove(inicio.jTabbedPane1.indexOfTab("Histogram"));
            inicio.jTabbedPane1.remove(inicio.jTabbedPane1.indexOfTab("Histogram R"));
            inicio.jTabbedPane1.remove(inicio.jTabbedPane1.indexOfTab("Histogram G"));
            inicio.jTabbedPane1.remove(inicio.jTabbedPane1.indexOfTab("Histogram B"));
            viewH = false;
        }

    }
        break;

    case 12: {
        //BRILLO
        int dif = inicio.brillo;

        if (brillos == false) {
            original = img;
        }
        int ancho = img.getWidth();
        int alto = img.getHeight();
        //se crea un buffer
        BufferedImage brillito = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
        //se convierten los colores a negativo y se va guardando en el buffer
        for (int y = 0; y < alto; y++) {
            for (int x = 0; x < ancho; x++) {
                int p = original.getRGB(x, y);
                //obtenemos el valor r g b a de cada pixel
                int a = (p >> 24) & 0xff;
                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;
                //se resta el rbg
                r = truncate(r + dif);
                g = truncate(g + dif);
                b = truncate(b + dif);
                //se guarda el rgb
                p = (r << 16) | (g << 8) | b;
                brillito.setRGB(x, y, p);
            }
        }
        img = brillito;
        brillos = true;
        inicio.jLabel3.setBounds(0, 0, ancho, alto);
        ImageIcon icon = new ImageIcon(img);
        inicio.jLabel3.setIcon(icon);

    }
        break; //end case 12

    case 13: {
        //CONTRAST
        double dif = inicio.contraste;
        double level = Math.pow(((100.0 + dif) / 100.0), 2.0);

        if (contrastes == false) {
            original = img;
        }
        int ancho = original.getWidth();
        int alto = original.getHeight();
        BufferedImage contraste = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);

        for (int y = 0; y < alto; y++) {
            for (int x = 0; x < ancho; x++) {
                int p = original.getRGB(x, y);
                int a = (p >> 24) & 0xff;
                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;

                b = truncate((int) ((((((double) b / 255.0) - 0.5) * level) + 0.5) * 255.0));
                g = truncate((int) ((((((double) g / 255.0) - 0.5) * level) + 0.5) * 255.0));
                r = truncate((int) ((((((double) r / 255.0) - 0.5) * level) + 0.5) * 255.0));

                p = (r << 16) | (g << 8) | b;
                contraste.setRGB(x, y, p);
            }
        }
        img = contraste;
        contrastes = true;
        inicio.jLabel3.setBounds(0, 0, ancho, alto);
        ImageIcon icon = new ImageIcon(img);
        inicio.jLabel3.setIcon(icon);

    }
        break;// case 13

    case 14: {
        //UMBRALIZACION
        double u = inicio.umbral;
        if (inicio.jCheckBox1.isSelected()) {

            int ancho = img.getWidth();
            int alto = img.getHeight();

            BufferedImage contraste = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);

            for (int y = 0; y < alto; y++) {
                for (int x = 0; x < ancho; x++) {
                    int p = img.getRGB(x, y);

                    int a = (p >> 24) & 0xff;
                    int r = (p >> 16) & 0xff;
                    int g = (p >> 8) & 0xff;
                    int b = p & 0xff;

                    double mediana = (double) (r + b + g);
                    mediana /= 3;
                    int med = (int) Math.round(mediana);

                    b = med;
                    g = med;
                    r = med;

                    if (r <= u)
                        r = 0;
                    else
                        r = 255;

                    if (g <= u)
                        g = 0;
                    else
                        g = 255;

                    if (b <= u)
                        b = 0;
                    else
                        b = 255;

                    p = (r << 16) | (g << 8) | b;
                    contraste.setRGB(x, y, p);
                }
            }
            img = contraste;
            Change();
        }

    }
        break;

    case 15: {
        BufferedImage equalized = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
        int r, g, b, a;
        int pixel = 0;

        //look up table rgb 
        int[] rhist = new int[256];
        int[] ghist = new int[256];
        int[] bhist = new int[256];

        for (int i = 0; i < rhist.length; i++)
            rhist[i] = 0;
        for (int i = 0; i < ghist.length; i++)
            ghist[i] = 0;
        for (int i = 0; i < bhist.length; i++)
            bhist[i] = 0;

        for (int i = 0; i < img.getWidth(); i++) {
            for (int j = 0; j < img.getHeight(); j++) {

                int red = new Color(img.getRGB(i, j)).getRed();
                int green = new Color(img.getRGB(i, j)).getGreen();
                int blue = new Color(img.getRGB(i, j)).getBlue();
                rhist[red]++;
                ghist[green]++;
                bhist[blue]++;

            }
        }

        //histograma color
        ArrayList<int[]> imageHist = new ArrayList<int[]>();
        imageHist.add(rhist);
        imageHist.add(ghist);
        imageHist.add(bhist);
        //lookup table
        ArrayList<int[]> imgLT = new ArrayList<int[]>();
        // llenar 
        rhist = new int[256];
        ghist = new int[256];
        bhist = new int[256];

        for (int i = 0; i < rhist.length; i++)
            rhist[i] = 0;
        for (int i = 0; i < ghist.length; i++)
            ghist[i] = 0;
        for (int i = 0; i < bhist.length; i++)
            bhist[i] = 0;

        long rojosT = 0;
        long verdesT = 0;
        long azulT = 0;

        // 
        float factorDeEscala = (float) (255.0 / (ancho * alto));

        for (int i = 0; i < rhist.length; i++) {
            rojosT += imageHist.get(0)[i];
            int valor = (int) (rojosT * factorDeEscala);
            if (valor > 255) {
                rhist[i] = 255;
            } else
                rhist[i] = valor;

            verdesT += imageHist.get(1)[i];
            int valg = (int) (verdesT * factorDeEscala);
            if (valg > 255) {
                ghist[i] = 255;
            } else
                ghist[i] = valg;

            azulT += imageHist.get(2)[i];
            int valb = (int) (azulT * factorDeEscala);
            if (valb > 255) {
                bhist[i] = 255;
            } else
                bhist[i] = valb;
        }

        imgLT.add(rhist);
        imgLT.add(ghist);
        imgLT.add(bhist);

        for (int i = 0; i < ancho; i++) {
            for (int j = 0; j < alto; j++) {

                // colores
                a = new Color(img.getRGB(i, j)).getAlpha();
                r = new Color(img.getRGB(i, j)).getRed();
                g = new Color(img.getRGB(i, j)).getGreen();
                b = new Color(img.getRGB(i, j)).getBlue();

                // nuevos valoooooores
                r = imgLT.get(0)[r];
                g = imgLT.get(1)[g];
                b = imgLT.get(2)[b];

                // rgb otra vez
                pixel = colorToRGB(a, r, g, b);

                //imagen final
                equalized.setRGB(i, j, pixel);

            }
        }

        img = equalized;
        Change();

    }
        break;

    case 16: {
        //zoom 
        double du = inicio.zoom;
        double u = du / 100;

        if (zoomv == false) {
            original = img;
        }
        BufferedImage zoom = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);

        for (int i = 0; i < zoom.getHeight(); ++i)
            for (int j = 0; j < zoom.getWidth(); ++j) {
                //nearest
                if (tipo == 1) {

                    int ax = (int) (Math.floor(i / u));
                    int ay = (int) (Math.floor(j / u));

                    int p = original.getRGB(ax, ay);
                    zoom.setRGB(i, j, p);
                }

                //bilinear
                if (tipo == 2) {

                }

                //no loss
                if (tipo == 0) {

                    int ax = (int) (i / u);
                    int ay = (int) (j / u);

                    int p = original.getRGB(ax, ay);
                    zoom.setRGB(i, j, p);
                }

            }
        img = zoom;
        zoomv = true;
        inicio.jLabel3.setBounds(0, 0, ancho, alto);
        ImageIcon icon = new ImageIcon(img);
        inicio.jLabel3.setIcon(icon);

    }
        break;

    case 17: {
        //escala
        double du = inicio.escala;
        double u = du / 100;

        if (escalav == false) {
            original = img;
        }
        int escalaX = (int) (ancho * u);
        int escalaY = (int) (alto * u);
        BufferedImage escala = new BufferedImage(escalaX, escalaY, BufferedImage.TYPE_INT_RGB);

        for (int i = 0; i < escala.getHeight(); ++i)
            for (int j = 0; j < escala.getWidth(); ++j) {
                //R(x,y):= A(x/ax, y/ay) 
                //R(x,y):= A(Floor x/10 ,Floor /10)

                //nearest
                if (tipo == 1) {

                    int ax = (int) (Math.floor(i / u));
                    int ay = (int) (Math.floor(j / u));

                    int p = original.getRGB(ax, ay);
                    escala.setRGB(i, j, p);
                }

                //bilinear
                if (tipo == 2) {

                }

                //no loss
                if (tipo == 0) {

                    int ax = (int) (i / u);
                    int ay = (int) (j / u);

                    int p = original.getRGB(ax, ay);
                    escala.setRGB(i, j, p);
                }

            }

        img = escala;
        escalav = true;
        inicio.jLabel3.setBounds(0, 0, ancho, alto);
        ImageIcon icon = new ImageIcon(img);
        inicio.jLabel3.setIcon(icon);
        content = ("Dimension: " + img.getWidth() + " x " + img.getHeight() + "\nBpp: " + bmp.bitsPorPixel
                + "bits");

    }
        break;

    case 18://prewitt both
    {

        BufferedImage aux = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
        aux = img;
        BufferedImage y, x;

        float[][] arraya = { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } };
        float[][] arrayb = { { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 },
                { -2, -1, 0, 1, 2 }, };

        float[][] arrayc = { { -3, -2, -1, 0, 1, 2, 3 }, { -3, -2, -1, 0, 1, 2, 3 }, { -3, -2, -1, 0, 1, 2, 3 },
                { -3, -2, -1, 0, 1, 2, 3 }, { -3, -2, -1, 0, 1, 2, 3 }, { -3, -2, -1, 0, 1, 2, 3 },
                { -3, -2, -1, 0, 1, 2, 3 }, };

        float[][] array = { { -1, -1, -1 }, { 0, 0, 0 }, { 1, 1, 1 } };
        float[][] array2 = { { -2, -2, -2, -2, -2 }, { -1, -1, -1, -1, -1 }, { 0, 0, 0, 0, 0 },
                { 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 }, };
        float[][] array3 = { { -3, -3, -3, -3, -3, -3, -3 }, { -2, -2, -2, -2, -2, -2, -2 },
                { -1, -1, -1, -1, -1, -1, -1 }, { 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 1, 1, 1, 1 },
                { 2, 2, 2, 2, 2, 2, 2 }, { 3, 3, 3, 3, 3, 3, 3 }, };
        if (inicio.size == 7) {
            y = generalKernel(array3, 7);
            img = aux;
            x = generalKernel(arrayc, 7);
        } else if (inicio.size == 5) {
            y = generalKernel(array2, 5);
            img = aux;
            x = generalKernel(arrayb, 5);
        } else {
            y = generalKernel(array, 3);
            img = aux;
            x = generalKernel(arraya, 3);
        }

        for (int i = 0; i < ancho; i++) {
            for (int j = 0; j < alto; j++) {

                int p = x.getRGB(i, j);
                int p2 = y.getRGB(i, j);
                //obtenemos el valor r g b a de cada pixel

                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;

                int r2 = (p2 >> 16) & 0xff;
                int g2 = (p2 >> 8) & 0xff;
                int b2 = p2 & 0xff;
                //process
                int resR = truncate(Math.sqrt(Math.pow(r, 2) + Math.pow(r2, 2)));
                int resG = truncate(Math.sqrt(Math.pow(g, 2) + Math.pow(g2, 2)));
                int resB = truncate(Math.sqrt(Math.pow(b, 2) + Math.pow(b2, 2)));

                //se guarda el rgb
                p = (resR << 16) | (resG << 8) | resB;
                img.setRGB(i, j, p);

            }
            Change();
        }
    }
        break;

    case 19://prewitt x
    {

        BufferedImage x;

        float[][] arraya = { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } };
        float[][] arrayb = { { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 },
                { -2, -1, 0, 1, 2 }, };

        float[][] arrayc = { { -3, -2, -1, 0, 1, 2, 3 }, { -3, -2, -1, 0, 1, 2, 3 }, { -3, -2, -1, 0, 1, 2, 3 },
                { -3, -2, -1, 0, 1, 2, 3 }, { -3, -2, -1, 0, 1, 2, 3 }, { -3, -2, -1, 0, 1, 2, 3 },
                { -3, -2, -1, 0, 1, 2, 3 }, };

        if (inicio.size == 7) {
            x = generalKernel(arrayc, 7);
        } else if (inicio.size == 5) {
            x = generalKernel(arrayb, 5);
        } else {
            x = generalKernel(arraya, 3);
        }
        img = x;
        Change();

    }
        break;

    case 20://prewitt y
    {

        BufferedImage y;

        float[][] array = { { -1, -1, -1 }, { 0, 0, 0 }, { 1, 1, 1 } };
        float[][] array2 = { { -2, -2, -2, -2, -2 }, { -1, -1, -1, -1, -1 }, { 0, 0, 0, 0, 0 },
                { 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 }, };
        float[][] array3 = { { -3, -3, -3, -3, -3, -3, -3 }, { -2, -2, -2, -2, -2, -2, -2 },
                { -1, -1, -1, -1, -1, -1, -1 }, { 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 1, 1, 1, 1 },
                { 2, 2, 2, 2, 2, 2, 2 }, { 3, 3, 3, 3, 3, 3, 3 }, };

        if (inicio.size == 7) {
            y = generalKernel(array3, 7);
        } else if (inicio.size == 5) {
            y = generalKernel(array2, 5);
        } else {
            y = generalKernel(array, 3);
        }

        img = y;
        Change();

    }
        break;

    case 21://Sobel x
    {

        BufferedImage x;
        float[][] arraya = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };

        float[][] arrayb = { { -5, -4, 0, 4, 5 }, { -8, -10, 0, 10, 8 }, { -10, -20, 0, 20, 10 },
                { -8, -10, 0, 10, 8 }, { -5, -4, 0, 4, 5 }, };

        float[][] arrayc = { { 3, 2, 1, 0, -1, -2, -3 }, { 4, 3, 2, 0, -2, -3, -4 }, { 5, 4, 3, 0, -3, -4, -5 },
                { 6, 5, 4, 0, -4, -5, -6 }, { 5, 4, 3, 0, -3, -4, -5 }, { 4, 3, 2, 0, -2, -3, -4 },
                { 3, 2, 1, 0, -1, -2, -3 }, };

        if (inicio.size == 7) {
            x = generalKernel(arrayc, 7);
        } else if (inicio.size == 5) {
            x = generalKernel(arrayb, 5);
        } else {
            x = generalKernel(arraya, 3);
        }
        img = x;
        Change();

    }
        break;

    case 22://sobel y
    {

        BufferedImage y;

        float[][] array1 = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };

        float[][] array2 = { { 5, 8, 10, 8, 5 }, { 4, 10, 20, 10, 4 }, { 0, 0, 0, 0, 0 },
                { -4, -10, -20, -10, -4 }, { -5, -8, -10, -8, -5 }, };

        float[][] array3 = { { 3, 4, 5, 6, 5, 4, 3 }, { 2, 3, 4, 5, 4, 3, 2 }, { 1, 2, 3, 4, 3, 2, 1 },
                { 0, 0, 0, 0, 0, 0, 0 }, { -1, -2, -3, -4, -3, -2, -1 }, { -2, -3, -4, -5, -4, -3, -2 },
                { -3, -4, -5, -6, -5, -4, -3 }, };

        if (inicio.size == 7) {
            y = generalKernel(array3, 7);
        } else if (inicio.size == 5) {
            y = generalKernel(array2, 5);
        } else {
            y = generalKernel(array1, 3);
        }

        img = y;
        Change();

    }
        break;

    case 23://sobel both
    {

        BufferedImage aux = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
        aux = img;
        BufferedImage y, x;

        float[][] arraya = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };

        float[][] arrayb = { { -5, -4, 0, 4, 5 }, { -8, -10, 0, 10, 8 }, { -10, -20, 0, 20, 10 },
                { -8, -10, 0, 10, 8 }, { -5, -4, 0, 4, 5 }, };

        float[][] arrayc = { { 3, 2, 1, 0, -1, -2, -3 }, { 4, 3, 2, 0, -2, -3, -4 }, { 5, 4, 3, 0, -3, -4, -5 },
                { 6, 5, 4, 0, -4, -5, -6 }, { 5, 4, 3, 0, -3, -4, -5 }, { 4, 3, 2, 0, -2, -3, -4 },
                { 3, 2, 1, 0, -1, -2, -3 }, };

        float[][] array1 = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };

        float[][] array2 = { { 5, 8, 10, 8, 5 }, { 4, 10, 20, 10, 4 }, { 0, 0, 0, 0, 0 },
                { -4, -10, -20, -10, -4 }, { -5, -8, -10, -8, -5 }, };

        float[][] array3 = { { 3, 4, 5, 6, 5, 4, 3 }, { 2, 3, 4, 5, 4, 3, 2 }, { 1, 2, 3, 4, 3, 2, 1 },
                { 0, 0, 0, 0, 0, 0, 0 }, { -1, -2, -3, -4, -3, -2, -1 }, { -2, -3, -4, -5, -4, -3, -2 },
                { -3, -4, -5, -6, -5, -4, -3 }, };
        if (inicio.size == 7) {
            y = generalKernel(array3, 7);
            img = aux;
            x = generalKernel(arrayc, 7);
        } else if (inicio.size == 5) {
            y = generalKernel(array2, 5);
            img = aux;
            x = generalKernel(arrayb, 5);
        } else {
            y = generalKernel(array1, 3);
            img = aux;
            x = generalKernel(arraya, 3);
        }

        for (int i = 0; i < ancho; i++) {
            for (int j = 0; j < alto; j++) {

                int p = x.getRGB(i, j);
                int p2 = y.getRGB(i, j);
                //obtenermos el valor r g b a de cada pixel

                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;

                int r2 = (p2 >> 16) & 0xff;
                int g2 = (p2 >> 8) & 0xff;
                int b2 = p2 & 0xff;
                //process
                int resR = truncate(Math.sqrt(Math.pow(r, 2) + Math.pow(r2, 2)));
                int resG = truncate(Math.sqrt(Math.pow(g, 2) + Math.pow(g2, 2)));
                int resB = truncate(Math.sqrt(Math.pow(b, 2) + Math.pow(b2, 2)));

                //se guarda el rgb
                p = (resR << 16) | (resG << 8) | resB;
                img.setRGB(i, j, p);

            }
            Change();
        }
    }
        break;

    case 24://Gauss 
    {

        BufferedImage y;

        float[][] arraya = { { 1 / 16f, 1 / 8f, 1 / 16f }, { 1 / 8f, 1 / 4f, 1 / 8f },
                { 1 / 16f, 1 / 8f, 1 / 16f }, };
        float[][] arrayb = { { 1 / 273f, 4 / 273f, 7 / 273f, 4 / 273f, 1 / 273f },
                { 4 / 273f, 16 / 273f, 26 / 273f, 16 / 273f, 4 / 273f },
                { 7 / 273f, 26 / 273f, 41 / 273f, 26 / 273f, 7 / 273f },
                { 4 / 273f, 16 / 273f, 26 / 273f, 16 / 273f, 4 / 273f },
                { 1 / 273f, 4 / 273f, 7 / 273f, 4 / 273f, 1 / 273f }, };

        float[][] arrayc = {
                { 0.00000067f, 0.00002292f, 0.00019117f, 0.00038771f, 0.00019117f, 0.00002292f, 0.00000067f },
                { 0.00002292f, 0.00078634f, 0.00655965f, 0.01330373f, 0.00655965f, 0.00078633f, 0.00002292f },
                { 0.00019117f, 0.00655965f, 0.05472157f, 0.11098164f, 0.05472157f, 0.00655965f, 0.00019117f },
                { 0.00038771f, 0.01330373f, 0.11098164f, 0.22508352f, 0.11098164f, 0.01330373f, 0.00038771f },
                { 0.00019117f, 0.00655965f, 0.05472157f, 0.11098164f, 0.05472157f, 0.00655965f, 0.00019117f },
                { 0.00002292f, 0.00078634f, 0.00655965f, 0.01330373f, 0.00655965f, 0.00078633f, 0.00002292f },
                { 0.00000067f, 0.00002292f, 0.00019117f, 0.00038771f, 0.00019117f, 0.00002292f, 0.00000067f } };

        if (inicio.size == 7) {
            y = generalKernel(arrayc, 7);
        } else if (inicio.size == 5) {
            y = generalKernel(arrayb, 5);
        } else {
            y = generalKernel(arraya, 3);
        }

        img = y;
        Change();

    }
        break;

    case 25: {

        BufferedImage y;

        float[][] arraya = { { 1 / 9f, 1 / 9f, 1 / 9f }, { 1 / 9f, 1 / 9f, 1 / 9f },
                { 1 / 9f, 1 / 9f, 1 / 9f }, };
        float[][] arrayb = { { 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f },
                { 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f },
                { 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f },
                { 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f },
                { 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f, 1 / 25f }, };
        float[][] arrayc = { { 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f },
                { 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f },
                { 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f },
                { 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f },
                { 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f },
                { 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f },
                { 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f, 1 / 49f }, };
        if (inicio.size == 7) {
            y = generalKernel(arrayc, 7);
        } else if (inicio.size == 5) {
            y = generalKernel(arrayb, 5);
        } else {
            y = generalKernel(arraya, 3);
        }

        img = y;
        Change();

    }
        break;

    case 26://sharpen 
    {

        BufferedImage y;

        float[][] arraya = { { -1, -1, -1 }, { -1, 9, -1 }, { -1, -1, -1 }, };
        float[][] arrayb = { { -1, -1, -1, -1, -1 }, { -1, -1, -1, -1, -1 }, { -1, -1, 26, -1, -1 },
                { -1, -1, -1, -1, -1 }, { -1, -1, -1, -1, -1 }, };
        float[][] arrayc = { { -1, -1, -1, -1, -1, -1, -1 }, { -1, -2, -2, -2, -2, -2, -1 },
                { -1, -2, -3, -3, -3, -2, -1 }, { -1, -2, -3, 81, -3, -2, -1 }, { -1, -2, -3, -3, -3, -2, -1 },
                { -1, -2, -2, -2, -2, -2, -1 }, { -1, -1, -1, -1, -1, -1, -1 }, };
        if (inicio.size == 7) {
            y = generalKernel(arrayc, 7);
        } else if (inicio.size == 5) {
            y = generalKernel(arrayb, 5);
        } else {
            y = generalKernel(arraya, 3);
        }

        img = y;
        Change();

    }
        break;
    case 27: {

        kernel = new Kernel();
        kernel.show();
        kernel.setTitle("Kernel");
        kernel.setVisible(true);
        kernel.setLocationRelativeTo(null);
        kernel.setResizable(false);
        kernel.pack();

    }
        break;

    case 28: //valores
    {

        float[][] floatdata = new float[kernel.dim][kernel.dim];
        for (int i = 0; i < kernel.dim; i++) {
            for (int j = 0; j < kernel.dim; j++) {
                floatdata[i][j] = floatValue(kernel.tableData[i][j]);
            }
        }
        kernel.dispose();
        BufferedImage y;
        y = generalKernel(floatdata, kernel.dim);
        img = y;

        Change();

    }
        break;

    case 29://motion blur
    {
        BufferedImage y;

        float[][] array = { { 1 / 9f, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1 / 9f, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 1 / 9f, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 1 / 9f, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 1 / 9f, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1 / 9f, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 1 / 9f, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 1 / 9f, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 1 / 9f }, };

        /*
        float[][] arrayb = {
            {1/3f, 0, 0},
            {0, 1/3f, 0},
            {0, 0, 1/3f},
         };*/

        y = generalKernel(array, 9);

        img = y;
        Change();

    }
        break;

    } //end switch

}

From source file:com.occamlab.te.parsers.ImageParser.java

private static void processBufferedImage(BufferedImage buffimage, String formatName, NodeList nodes)
        throws Exception {
    HashMap<Object, Object> bandMap = new HashMap<Object, Object>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("subimage")) {
                Element e = (Element) node;
                int x = Integer.parseInt(e.getAttribute("x"));
                int y = Integer.parseInt(e.getAttribute("y"));
                int w = Integer.parseInt(e.getAttribute("width"));
                int h = Integer.parseInt(e.getAttribute("height"));
                processBufferedImage(buffimage.getSubimage(x, y, w, h), formatName, e.getChildNodes());
            } else if (node.getLocalName().equals("checksum")) {
                CRC32 checksum = new CRC32();
                Raster raster = buffimage.getRaster();
                DataBufferByte buffer;
                if (node.getParentNode().getLocalName().equals("subimage")) {
                    WritableRaster outRaster = raster.createCompatibleWritableRaster();
                    buffimage.copyData(outRaster);
                    buffer = (DataBufferByte) outRaster.getDataBuffer();
                } else {
                    buffer = (DataBufferByte) raster.getDataBuffer();
                }/*from www  .j  a v  a2s . c o m*/
                int numbanks = buffer.getNumBanks();
                for (int j = 0; j < numbanks; j++) {
                    checksum.update(buffer.getData(j));
                }
                Document doc = node.getOwnerDocument();
                node.appendChild(doc.createTextNode(Long.toString(checksum.getValue())));
            } else if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                if (sample.equals("all")) {
                    bandMap.put(band, null);
                } else {
                    HashMap<Object, Object> sampleMap = (HashMap<Object, Object>) bandMap.get(band);
                    if (sampleMap == null) {
                        if (!bandMap.containsKey(band)) {
                            sampleMap = new HashMap<Object, Object>();
                            bandMap.put(band, sampleMap);
                        }
                    }
                    sampleMap.put(Integer.decode(sample), new Integer(0));
                }
            } else if (node.getLocalName().equals("transparentNodata")) { // 2011-08-24
                                                                          // PwD
                String transparentNodata = checkTransparentNodata(buffimage, node);
                node.setTextContent(transparentNodata);
            }
        }
    }

    Iterator bandIt = bandMap.keySet().iterator();
    while (bandIt.hasNext()) {
        String band_str = (String) bandIt.next();
        int band_indexes[];
        if (buffimage.getType() == BufferedImage.TYPE_BYTE_BINARY
                || buffimage.getType() == BufferedImage.TYPE_BYTE_GRAY) {
            band_indexes = new int[1];
            band_indexes[0] = 0;
        } else {
            band_indexes = new int[band_str.length()];
            for (int i = 0; i < band_str.length(); i++) {
                if (band_str.charAt(i) == 'A')
                    band_indexes[i] = 3;
                if (band_str.charAt(i) == 'B')
                    band_indexes[i] = 2;
                if (band_str.charAt(i) == 'G')
                    band_indexes[i] = 1;
                if (band_str.charAt(i) == 'R')
                    band_indexes[i] = 0;
            }
        }

        Raster raster = buffimage.getRaster();
        java.util.HashMap sampleMap = (java.util.HashMap) bandMap.get(band_str);
        boolean addall = (sampleMap == null);
        if (sampleMap == null) {
            sampleMap = new java.util.HashMap();
            bandMap.put(band_str, sampleMap);
        }

        int minx = raster.getMinX();
        int maxx = minx + raster.getWidth();
        int miny = raster.getMinY();
        int maxy = miny + raster.getHeight();
        int bands[][] = new int[band_indexes.length][raster.getWidth()];

        for (int y = miny; y < maxy; y++) {
            for (int i = 0; i < band_indexes.length; i++) {
                raster.getSamples(minx, y, maxx, 1, band_indexes[i], bands[i]);
            }
            for (int x = minx; x < maxx; x++) {
                int sample = 0;
                for (int i = 0; i < band_indexes.length; i++) {
                    sample |= bands[i][x] << ((band_indexes.length - i - 1) * 8);
                }

                Integer sampleObj = new Integer(sample);

                boolean add = addall;
                if (!addall) {
                    add = sampleMap.containsKey(sampleObj);
                }
                if (add) {
                    Integer count = (Integer) sampleMap.get(sampleObj);
                    if (count == null) {
                        count = new Integer(0);
                    }
                    count = new Integer(count.intValue() + 1);
                    sampleMap.put(sampleObj, count);
                }
            }
        }
    }

    Node node = nodes.item(0);
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                HashMap sampleMap = (HashMap) bandMap.get(band);
                Document doc = node.getOwnerDocument();
                if (sample.equals("all")) {
                    Node parent = node.getParentNode();
                    Node prevSibling = node.getPreviousSibling();
                    Iterator sampleIt = sampleMap.keySet().iterator();
                    Element countnode = null;
                    int digits;
                    String prefix;
                    switch (buffimage.getType()) {
                    case BufferedImage.TYPE_BYTE_BINARY:
                        digits = 1;
                        prefix = "";
                        break;
                    case BufferedImage.TYPE_BYTE_GRAY:
                        digits = 2;
                        prefix = "0x";
                        break;
                    default:
                        prefix = "0x";
                        digits = band.length() * 2;
                    }
                    while (sampleIt.hasNext()) {
                        countnode = doc.createElementNS(node.getNamespaceURI(), "count");
                        Integer sampleInt = (Integer) sampleIt.next();
                        Integer count = (Integer) sampleMap.get(sampleInt);
                        if (band.length() > 0) {
                            countnode.setAttribute("bands", band);
                        }
                        countnode.setAttribute("sample", prefix + HexString(sampleInt.intValue(), digits));
                        Node textnode = doc.createTextNode(count.toString());
                        countnode.appendChild(textnode);
                        parent.insertBefore(countnode, node);
                        if (sampleIt.hasNext()) {
                            if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE) {
                                parent.insertBefore(prevSibling.cloneNode(false), node);
                            }
                        }
                    }
                    parent.removeChild(node);
                    node = countnode;
                } else {
                    Integer count = (Integer) sampleMap.get(Integer.decode(sample));
                    if (count == null)
                        count = new Integer(0);
                    Node textnode = doc.createTextNode(count.toString());
                    node.appendChild(textnode);
                }
            }
        }
        node = node.getNextSibling();
    }
}

From source file:org.esa.nest.gpf.ERSCalibrator.java

private static void outputRealImage(final RenderedImage I, final int startIdx, final int endIdx) {

    final Raster data = I.getData();
    final double[] real = data.getSamples(0, 0, I.getWidth(), I.getHeight(), 0, (double[]) null);

    for (int i = startIdx; i <= endIdx; i++) {
        System.out.print(real[i] + ",");
    }//from   w  ww  . ja v  a 2 s . com
    System.out.println();
}

From source file:org.esa.nest.gpf.GCPSelectionOp.java

private void determiningImageOffset(final Band slaveBand1, final Band slaveBand2, int[] offset) {

    try {/*from w  w w . ja  v  a 2 s  .com*/
        // get master and slave imagettes
        final MetadataElement absRoot = AbstractMetadata.getAbstractedMetadata(sourceProduct);
        double groundRangeSpacing = absRoot.getAttributeDouble(AbstractMetadata.range_spacing, 1);
        final double azimuthSpacing = absRoot.getAttributeDouble(AbstractMetadata.azimuth_spacing, 1);
        final boolean srgrFlag = AbstractMetadata.getAttributeBoolean(absRoot, AbstractMetadata.srgr_flag);
        if (!srgrFlag) {
            final TiePointGrid incidenceAngle = OperatorUtils.getIncidenceAngle(sourceProduct);
            final double incidenceAngleAtCentreRangePixel = incidenceAngle.getPixelDouble(sourceImageWidth / 2f,
                    sourceImageHeight / 2f);
            groundRangeSpacing /= FastMath.sin(incidenceAngleAtCentreRangePixel * Constants.DTOR);
        }
        final int nRgLooks = Math.max(1, sourceImageWidth / 2048);
        final int nAzLooks = Math.max(1, (int) ((double) nRgLooks * groundRangeSpacing / azimuthSpacing + 0.5));
        final int targetImageWidth = sourceImageWidth / nRgLooks;
        final int targetImageHeight = sourceImageHeight / nAzLooks;
        final int windowWidth = (int) FastMath.pow(2, (int) (Math.log10(targetImageWidth) / Math.log10(2)));
        final int windowHeight = (int) FastMath.pow(2, (int) (Math.log10(targetImageHeight) / Math.log10(2)));
        final double[] mI = new double[windowWidth * windowHeight];
        final double[] sI = new double[windowWidth * windowHeight];

        final int tileCountX = 4;
        final int tileCountY = 4;
        final int tileWidth = windowWidth / tileCountX;
        final int tileHeight = windowHeight / tileCountY;
        final Rectangle[] tileRectangles = new Rectangle[tileCountX * tileCountY];
        int index = 0;
        for (int tileY = 0; tileY < tileCountY; tileY++) {
            final int ypos = tileY * tileHeight;
            for (int tileX = 0; tileX < tileCountX; tileX++) {
                final Rectangle tileRectangle = new Rectangle(tileX * tileWidth, ypos, tileWidth, tileHeight);
                tileRectangles[index++] = tileRectangle;
            }
        }

        final StatusProgressMonitor status = new StatusProgressMonitor(tileRectangles.length,
                "Computing offset... ");
        int tileCnt = 0;

        final ThreadManager threadManager = new ThreadManager();
        try {
            for (final Rectangle rectangle : tileRectangles) {
                checkForCancellation();

                final Thread worker = new Thread() {

                    @Override
                    public void run() {
                        final int x0 = rectangle.x;
                        final int y0 = rectangle.y;
                        final int w = rectangle.width;
                        final int h = rectangle.height;
                        final int xMax = x0 + w;
                        final int yMax = y0 + h;

                        final int xStart = x0 * nRgLooks;
                        final int yStart = y0 * nAzLooks;
                        final int xEnd = xMax * nRgLooks;
                        final int yEnd = yMax * nAzLooks;

                        final Rectangle srcRect = new Rectangle(xStart, yStart, xEnd - xStart, yEnd - yStart);
                        final Tile mstTile1 = getSourceTile(masterBand1, srcRect);
                        final ProductData mstData1 = mstTile1.getDataBuffer();
                        final TileIndex mstIndex = new TileIndex(mstTile1);
                        final Tile slvTile1 = getSourceTile(slaveBand1, srcRect);
                        final ProductData slvData1 = slvTile1.getDataBuffer();
                        final TileIndex slvIndex = new TileIndex(slvTile1);

                        ProductData mstData2 = null;
                        ProductData slvData2 = null;
                        if (complexCoregistration) {
                            mstData2 = getSourceTile(masterBand2, srcRect).getDataBuffer();
                            slvData2 = getSourceTile(slaveBand2, srcRect).getDataBuffer();
                        }

                        final double rgAzLooks = nRgLooks * nAzLooks;

                        for (int y = y0; y < yMax; y++) {
                            final int yByWidth = y * windowWidth;
                            final int y1 = y * nAzLooks;
                            final int y2 = y1 + nAzLooks;
                            for (int x = x0; x < xMax; x++) {
                                final int x1 = x * nRgLooks;
                                final int x2 = x1 + nRgLooks;
                                mI[yByWidth + x] = getMeanValue(x1, x2, y1, y2, mstData1, mstData2, mstIndex,
                                        rgAzLooks);
                                sI[yByWidth + x] = getMeanValue(x1, x2, y1, y2, slvData1, slvData2, slvIndex,
                                        rgAzLooks);
                            }
                        }

                        status.workedOne();
                    }
                };
                threadManager.add(worker);

                // status.worked(tileCnt++);
            }
            threadManager.finish();

        } catch (Throwable e) {
            OperatorUtils.catchOperatorException("GCPSelectionOp", e);
        } finally {
            status.done();
        }

        // correlate master and slave imagettes
        final RenderedImage masterImage = createRenderedImage(mI, windowWidth, windowHeight);
        final PlanarImage masterSpectrum = dft(masterImage);

        final RenderedImage slaveImage = createRenderedImage(sI, windowWidth, windowHeight);
        final PlanarImage slaveSpectrum = dft(slaveImage);
        final PlanarImage conjugateSlaveSpectrum = conjugate(slaveSpectrum);

        final PlanarImage crossSpectrum = multiplyComplex(masterSpectrum, conjugateSlaveSpectrum);
        final PlanarImage correlatedImage = idft(crossSpectrum);
        final PlanarImage crossCorrelatedImage = magnitude(correlatedImage);

        // compute offset
        final int w = crossCorrelatedImage.getWidth();
        final int h = crossCorrelatedImage.getHeight();
        final Raster idftData = crossCorrelatedImage.getData();
        final double[] real = idftData.getSamples(0, 0, w, h, 0, (double[]) null);

        int peakRow = 0;
        int peakCol = 0;
        double peak = 0;
        for (int r = 0; r < h; r++) {
            for (int c = 0; c < w; c++) {
                if (r >= h / 4 && r <= h * 3 / 4 || c >= w / 4 && c <= w * 3 / 4) {
                    continue;
                }
                final int s = r * w + c;
                if (peak < real[s]) {
                    peak = real[s];
                    peakRow = r;
                    peakCol = c;
                }
            }
        }

        // System.out.println("peakRow = " + peakRow + ", peakCol = " + peakCol);
        if (peakRow <= h / 2) {
            offset[1] = -peakRow * nAzLooks;
        } else {
            offset[1] = (h - peakRow) * nAzLooks;
        }

        if (peakCol <= w / 2) {
            offset[0] = -peakCol * nRgLooks;
        } else {
            offset[0] = (w - peakCol) * nRgLooks;
        }
        // System.out.println("offsetX = " + offset[0] + ", offsetY = " + offset[1]);

    } catch (Throwable e) {
        OperatorUtils.catchOperatorException(getId() + " getCoarseSlaveGCPPosition ", e);
    }
}

From source file:org.esa.nest.gpf.GCPSelectionOp.java

private boolean getSlaveGCPShift(final double[] shift, final double[] mI, final double[] sI) {
    try {/*from   w w  w .  j a  v  a2s.  co m*/
        // perform cross correlation
        final PlanarImage crossCorrelatedImage = computeCrossCorrelatedImage(mI, sI);

        // check peak validity
        /*
        final double mean = getMean(crossCorrelatedImage);
        if (Double.compare(mean, 0.0) == 0) {
        return false;
        }
                
        double max = getMax(crossCorrelatedImage);
        double qualityParam = max / mean;
        if (qualityParam <= qualityThreshold) {
        return false;
        }
        */

        // get peak shift: row and col
        final int w = crossCorrelatedImage.getWidth();
        final int h = crossCorrelatedImage.getHeight();

        final Raster idftData = crossCorrelatedImage.getData();
        final double[] real = idftData.getSamples(0, 0, w, h, 0, (double[]) null);
        //System.out.println("Cross correlated imagette:");
        //outputRealImage(real);

        int peakRow = 0;
        int peakCol = 0;
        double peak = real[0];
        for (int r = 0; r < h; r++) {
            for (int c = 0; c < w; c++) {
                final int k = r * w + c;
                if (real[k] > peak) {
                    peak = real[k];
                    peakRow = r;
                    peakCol = c;
                }
            }
        }
        //System.out.println("peak = " + peak + " at (" + peakRow + ", " + peakCol + ")");

        if (peakRow <= h / 2) {
            shift[0] = (double) (-peakRow) / (double) rowUpSamplingFactor;
        } else {
            shift[0] = (double) (h - peakRow) / (double) rowUpSamplingFactor;
        }

        if (peakCol <= w / 2) {
            shift[1] = (double) (-peakCol) / (double) colUpSamplingFactor;
        } else {
            shift[1] = (double) (w - peakCol) / (double) colUpSamplingFactor;
        }

        return true;
    } catch (Throwable t) {
        System.out.println("getSlaveGCPShift failed " + t.getMessage());
        return false;
    }
}