Java BufferedImage Operation scanFill(BufferedImage sourceImage)

Here you can find the source of scanFill(BufferedImage sourceImage)

Description

scan Fill

License

Open Source License

Declaration

protected static void scanFill(BufferedImage sourceImage) 

Method Source Code

//package com.java2s;

import java.awt.image.*;

import java.util.*;

public class Main {
    protected static void scanFill(BufferedImage sourceImage) {
        if (null == sourceImage || sourceImage.getWidth() <= 3
                || sourceImage.getHeight() <= 3)
            return;

        ArrayList<Integer> voids = new ArrayList<Integer>();
        voids.add(0); // a=r=g=b=0
        voids.add(0xFF << 24); // a=255, r=g=b=0
        voids.add(0xFFFFFFFF); // a=255, r=g=b=255
        voids.add(0x00FFFFFF); // a=0, r=g=b=255

        int NODATA_TRANSPARENT = 0x00000000;

        int width = sourceImage.getWidth();
        int height = sourceImage.getHeight();

        int[] scanline1 = new int[width + 2];
        int[] scanline2 = new int[width + 2];

        sourceImage.getRGB(0, 0, width, 1, scanline2, 1, width);

        // check the first pixel (in NW corner)
        int nw = 0x00FFFFFF & scanline2[1]; // ignore alpha value
        if (nw == 0x00808080) // r=g=b=128 (80H)
        {/*from  w w w .j a  v  a 2 s .  c o m*/
            voids.add((0xFF << 24) | nw); // alpha=255(FFH), r=g=b=128 (80H)
            voids.add(0x00FFFFFF & nw); // alpha=0, r=g=b=128 (80H)
        }

        // check the last pixel (in NE corner)
        int ne = 0x00FFFFFF & scanline2[width]; // ignore alpha value
        if (ne == 0x00808080) // r=g=b=128 (80H)
        {
            voids.add((0xFF << 24) | ne); // alpha=255(FFH), r=g=b=128 (80H)
            voids.add(0x00FFFFFF & ne); // alpha=0, r=g=b=128 (80H)
        }

        int numVoids = voids.size();
        int[] nodata = new int[numVoids];
        for (int i = 0; i < numVoids; i++) {
            nodata[i] = voids.get(i);
        }

        scanline2[0] = scanline2[width + 1] = NODATA_TRANSPARENT;

        Arrays.fill(scanline1, NODATA_TRANSPARENT);

        int pixel;
        for (int h = 0; h < height; h++) {
            int[] scanline0 = scanline1.clone();
            scanline1 = scanline2.clone();

            if (h + 1 < height) {
                sourceImage.getRGB(0, h + 1, width, 1, scanline2, 1, width);
                scanline2[0] = scanline2[width + 1] = NODATA_TRANSPARENT;
            } else
                Arrays.fill(scanline2, NODATA_TRANSPARENT);

            for (int i = 1; i <= width; i++) {
                pixel = scanline1[i];

                for (int v = 0; v < numVoids; v++) {
                    if (pixel == nodata[v]
                            && (scanline0[i - 1] == NODATA_TRANSPARENT
                                    || scanline0[i] == NODATA_TRANSPARENT
                                    || scanline0[i + 1] == NODATA_TRANSPARENT
                                    || scanline1[i - 1] == NODATA_TRANSPARENT
                                    || scanline1[i + 1] == NODATA_TRANSPARENT
                                    || scanline2[i - 1] == NODATA_TRANSPARENT
                                    || scanline2[i] == NODATA_TRANSPARENT || scanline2[i + 1] == NODATA_TRANSPARENT)) {
                        scanline1[i] = NODATA_TRANSPARENT;
                        break;
                    }
                }
            }

            sourceImage.setRGB(0, h, width, 1, scanline1, 1, width);
        }
    }
}

Related

  1. resampleImage(BufferedImage image, int height)
  2. resampleWithAffineTransformOp(BufferedImage srcImage, double sx, double sy)
  3. revertBlackAndWhite(BufferedImage img)
  4. roate90(BufferedImage src)
  5. sample9Points(BufferedImage src, int x, int y)
  6. scrapeColors(BufferedImage image)
  7. setArrayAlpha(BufferedImage image, int[] array)
  8. setArrayColor(BufferedImage image, int[][] array)
  9. setBackgroud(BufferedImage image, Color backgroundColor)