<p>
Returns the sum of all the pixels in the image.
</p>

@param img Input image. Not modified.
324049
public static int sum( GrayU16 img ) {

		if( BoofConcurrency.USE_CONCURRENT ) {
			return ImplImageStatistics_MT.sum(img);
		} else {
			return ImplImageStatistics.sum(img);
		}
	}
885174
public static void boundImage( GrayU8 img , int min , int max ) {
		ImplPixelMath.boundImage(img,min,max);
	}
176
@Override
    public ImageSource apply(ImageSource input) {
        int w = input.getWidth();
        int h = input.getHeight();

        MatrixSource output = new MatrixSource(input);

        Vector3 n = new Vector3(0, 0, 1);

        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {

                if (x < border || x == w - border || y < border || y == h - border) {
                    output.setRGB(x, y, VectorHelper.Z_NORMAL);
                    continue;
                }

                float s0 = input.getR(x - 1, y + 1);
                float s1 = input.getR(x, y + 1);
                float s2 = input.getR(x + 1, y + 1);
                float s3 = input.getR(x - 1, y);
                float s5 = input.getR(x + 1, y);
                float s6 = input.getR(x - 1, y - 1);
                float s7 = input.getR(x, y - 1);
                float s8 = input.getR(x + 1, y - 1);

                float nx = -(s2 - s0 + 2 * (s5 - s3) + s8 - s6);
                float ny = -(s6 - s0 + 2 * (s7 - s1) + s8 - s2);

                n.set(nx, ny, scale);
                n.nor();

                int rgb = VectorHelper.vectorToColor(n);
                output.setRGB(x, y, rgb);
            }
        }

        return new MatrixSource(output);
    }
272738
public void setBufferedImage(BufferedImage img) {
        image = img;
        width = img.getWidth();
        height = img.getHeight();
        updateColorArray();
    }
964795
@Override
    public ImageSource apply(ImageSource source) {
        int width = source.getWidth();
        int height = source.getHeight();

        int[][] result = new int[height][width];

        if (source.isGrayscale()) {
            for (int i = 0; i < height; i++) {

                int iMin = Math.max(0, i - blockRadius);
                int iMax = Math.min(height, i + blockRadius + 1);
                int h = iMax - iMin;

                int jMin = Math.max(0, -blockRadius);
                int jMax = Math.min(width - 1, blockRadius);

                int[] hist = new int[bins + 1];
                int[] clippedHist = new int[bins + 1];

                for (int k = iMin; k < iMax; k++) {
                    for (int l = jMin; l < jMax; l++) {
                        ++hist[roundPositive(source.getGray(l, k) / 255.0f * bins)];
                    }
                }

                for (int j = 0; j < width; j++) {
                    int v = roundPositive(source.getGray(j, i) / 255.0f * bins);

                    int xMin = Math.max(0, j - blockRadius);
                    int xMax = j + blockRadius + 1;
                    int w = Math.min(width, xMax) - xMin;
                    int n = h * w;

                    int limit = (int) (slope * n / bins + 0.5f);

                    /* remove left behind values from histogram */
                    if (xMin > 0) {
                        int xMin1 = xMin - 1;
                        for (int yi = iMin; yi < iMax; ++yi) {
                            --hist[roundPositive(source.getGray(xMin1, yi) / 255.0f * bins)];
                        }
                    }

                    /* add newly included values to histogram */
                    if (xMax <= width) {
                        int xMax1 = xMax - 1;
                        for (int yi = iMin; yi < iMax; ++yi) {
                            ++hist[roundPositive(source.getGray(xMax1, yi) / 255.0f * bins)];
                        }
                    }

                    System.arraycopy(hist, 0, clippedHist, 0, hist.length);
                    int clippedEntries = 0, clippedEntriesBefore;

                    do {
                        clippedEntriesBefore = clippedEntries;
                        clippedEntries = 0;
                        for (int z = 0; z <= bins; ++z) {
                            int d = clippedHist[z] - limit;
                            if (d > 0) {
                                clippedEntries += d;
                                clippedHist[z] = limit;
                            }
                        }

                        int d = clippedEntries / (bins + 1);
                        int m = clippedEntries % (bins + 1);
                        for (int z = 0; z <= bins; ++z) {
                            clippedHist[z] += d;
                        }

                        if (m != 0) {
                            int s = bins / m;
                            for (int z = 0; z <= bins; z += s) {
                                ++clippedHist[z];
                            }
                        }
                    }
                    while (clippedEntries != clippedEntriesBefore);

                    /* build cdf of clipped histogram */
                    int hMin = bins;
                    for (int z = 0; z < hMin; ++z) {
                        if (clippedHist[z] != 0) hMin = z;
                    }

                    int cdf = 0;
                    for (int z = hMin; z <= v; ++z) {
                        cdf += clippedHist[z];
                    }

                    int cdfMax = cdf;
                    for (int z = v + 1; z <= bins; ++z) {
                        cdfMax += clippedHist[z];
                    }

                    int cdfMin = clippedHist[hMin];

                    result[i][j] = roundPositive((cdf - cdfMin) / (float) (cdfMax - cdfMin) * 255.0f);
                }

                for (int a = 0; a < width; a++) {
                    source.setGray(a, i, result[i][a]);
                }
            }
        } else {
            MatrixSource gray = new MatrixSource(source);

            Grayscale gs = new Grayscale(algorithm);
            // It is not OneBandSource
            gs.apply(gray);

            for (int i = 0; i < height; i++) {

                int iMin = Math.max(0, i - blockRadius);
                int iMax = Math.min(height, i + blockRadius + 1);
                int h = iMax - iMin;

                int jMin = Math.max(0, -blockRadius);
                int jMax = Math.min(width - 1, blockRadius);

                int[] hist = new int[bins + 1];
                int[] clippedHist = new int[bins + 1];

                for (int k = iMin; k < iMax; k++) {
                    for (int l = jMin; l < jMax; l++) {
                        ++hist[roundPositive(gray.getGray(l, k) / 255.0f * bins)];
                    }
                }

                for (int j = 0; j < width; j++) {
                    int v = roundPositive(gray.getGray(j, i) / 255.0f * bins);

                    int xMin = Math.max(0, j - blockRadius);
                    int xMax = j + blockRadius + 1;
                    int w = Math.min(width, xMax) - xMin;
                    int n = h * w;

                    int limit = (int) (slope * n / bins + 0.5f);

                    /* remove left behind values from histogram */
                    if (xMin > 0) {
                        int xMin1 = xMin - 1;
                        for (int yi = iMin; yi < iMax; ++yi) {
                            --hist[roundPositive(gray.getGray(xMin1, yi) / 255.0f * bins)];
                        }
                    }

                    /* add newly included values to histogram */
                    if (xMax <= width) {
                        int xMax1 = xMax - 1;
                        for (int yi = iMin; yi < iMax; ++yi) {
                            ++hist[roundPositive(gray.getGray(xMax1, yi) / 255.0f * bins)];
                        }
                    }

                    System.arraycopy(hist, 0, clippedHist, 0, hist.length);
                    int clippedEntries = 0, clippedEntriesBefore;
                    do {
                        clippedEntriesBefore = clippedEntries;
                        clippedEntries = 0;
                        for (int z = 0; z <= bins; ++z) {
                            int d = clippedHist[z] - limit;
                            if (d > 0) {
                                clippedEntries += d;
                                clippedHist[z] = limit;
                            }
                        }

                        int d = clippedEntries / (bins + 1);
                        int m = clippedEntries % (bins + 1);
                        for (int z = 0; z <= bins; ++z)
                            clippedHist[z] += d;

                        if (m != 0) {
                            int s = bins / m;
                            for (int z = 0; z <= bins; z += s)
                                ++clippedHist[z];
                        }
                    }
                    while (clippedEntries != clippedEntriesBefore);

                    /* build cdf of clipped histogram */
                    int hMin = bins;
                    for (int z = 0; z < hMin; ++z) {
                        if (clippedHist[z] != 0) hMin = z;
                    }

                    int cdf = 0;
                    for (int z = hMin; z <= v; ++z) {
                        cdf += clippedHist[z];
                    }

                    int cdfMax = cdf;
                    for (int z = v + 1; z <= bins; ++z) {
                        cdfMax += clippedHist[z];
                    }

                    int cdfMin = clippedHist[hMin];

                    result[i][j] = roundPositive((cdf - cdfMin) / (float) (cdfMax - cdfMin) * 255.0f);
                }

                for (int a = 0; a < width; a++) {
                    float s = (float) result[i][a] / (float) gray.getGray(a, i);

                    float r = ColorHelper.clamp(roundPositive(s * source.getR(a, i)));
                    float g = ColorHelper.clamp(roundPositive(s * source.getG(a, i)));
                    float b = ColorHelper.clamp(roundPositive(s * source.getB(a, i)));

                    int rgb = ColorHelper.getRGB((int) r, (int) g, (int) b);

                    source.setRGB(a, i, rgb);
                }
            }
        }
        return source;
    }
727107
public static void boundImage( GrayS16 img , int min , int max ) {
		ImplPixelMath.boundImage(img,min,max);
	}
882830
public static BufferedImage getImage(InputStream input) throws IOException
    {
        final BufferedImage buffer = ImageIO.read(input);
        if (buffer == null)
        {
            throw new IOException("Invalid image !");
        }
        return copyImage(buffer);
    }
1085163
public static double variance( GrayU16 img , double mean ) {

		if( BoofConcurrency.USE_CONCURRENT ) {
			return ImplImageStatistics_MT.variance(img,mean);
		} else {
			return ImplImageStatistics.variance(img,mean);
		}
	}
215671
@Override
    public ImageSource apply(ImageSource input) {
        if (input.isGrayscale()) {
            return input;
        }
        if (!isAlgorithm) {
            double r, g, b, gray;

            for (int i = 0; i < EffectHelper.getSize(input); i++) {
                r = EffectHelper.getRed(i, input);
                g = EffectHelper.getGreen(i, input);
                b = EffectHelper.getBlue(i, input);
                int a = EffectHelper.getAlpha(i, input);

                gray = (r * redCoefficient + g * greenCoefficient + b * blueCoefficient);

                EffectHelper.setRGB(i, (int) gray, (int) gray, (int) gray, a, input);
            }
            return input;
        } else {
            return apply(input, grayscaleMethod);
        }
    }
1008978
public void setImage(BufferedImage im) {
		icon.setImage(im);
		if (image == null) {
			this.image = im;
//			this.setSize(image.getWidth(), image.getHeight());
			int panelX = image.getWidth(); 
			int panelY = image.getHeight(); 
			panel.setSize(panelX, panelY);

//			Toolkit tk = Toolkit.getDefaultToolkit();  
//			int xSize = ((int) tk.getScreenSize().getWidth());  
//			int ySize = ((int) tk.getScreenSize().getHeight());
			
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedLookAndFeelException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
			Rectangle bounds = ge.getMaximumWindowBounds();

			int deltaX = 30;
			int deltaY = 50;
			int xSize = Math.min(bounds.width, panelX+deltaX);
			int ySize = Math.min(bounds.height, panelY+deltaY);
			
			this.setPreferredSize(new Dimension(xSize, ySize));
			
			this.setResizable(true);
			this.pack();
		}
		this.getContentPane().repaint();
		if (!this.isVisible()) this.setVisible(true);
	}
545425
public static double variance( GrayS16 img , double mean ) {

		if( BoofConcurrency.USE_CONCURRENT ) {
			return ImplImageStatistics_MT.variance(img,mean);
		} else {
			return ImplImageStatistics.variance(img,mean);
		}
	}
453823
public static double mean( InterleavedU16 img ) {
		return sum(img)/(double)(img.width*img.height*img.numBands);
	}
484513
public static long sum( InterleavedS64 img ) {

		if( BoofConcurrency.USE_CONCURRENT ) {
			return ImplImageStatistics_MT.sum(img);
		} else {
			return ImplImageStatistics.sum(img);
		}
	}
717742
public static int sum( GrayU8 img ) {

		if( BoofConcurrency.USE_CONCURRENT ) {
			return ImplImageStatistics_MT.sum(img);
		} else {
			return ImplImageStatistics.sum(img);
		}
	}
43873
public BufferedImage getImage() throws IOException {
        if (image == null) {
            // No content, no image
            if (bufferedOut == null) {
                return null;
            }

            // Read from the byte buffer
            InputStream byteStream = bufferedOut.createInputStream();
            ImageInputStream input = null;
            try {
                input = ImageIO.createImageInputStream(byteStream);
                Iterator readers = ImageIO.getImageReaders(input);
                if (readers.hasNext()) {
                    // Get the correct reader
                    ImageReader reader = (ImageReader) readers.next();
                    try {
                        reader.setInput(input);

                        ImageReadParam param = reader.getDefaultReadParam();

                        // Get default size
                        int originalWidth = reader.getWidth(0);
                        int originalHeight = reader.getHeight(0);
//////////////////
// PRE-PROCESS (prepare): param, size, format?, request, response?
                        // TODO: AOI strategy?
                        // Extract AOI from request
                        Rectangle aoi = extractAOIFromRequest(originalWidth, originalHeight, originalRequest);

                        if (aoi != null) {
                            param.setSourceRegion(aoi);
                            originalWidth = aoi.width;
                            originalHeight = aoi.height;
                        }

                        // TODO: Size and subsampling strategy?
                        // If possible, extract size from request
                        Dimension size = extractSizeFromRequest(originalWidth, originalHeight, originalRequest);
                        double readSubSamplingFactor = getReadSubsampleFactorFromRequest(originalRequest);

                        if (size != null) {
                            //System.out.println("Size: " + size);
                            if (param.canSetSourceRenderSize()) {
                                param.setSourceRenderSize(size);
                            }
                            else {
                                int subX = (int) Math.max(originalWidth / (size.width * readSubSamplingFactor), 1.0);
                                int subY = (int) Math.max(originalHeight / (size.height * readSubSamplingFactor), 1.0);

                                if (subX > 1 || subY > 1) {
                                    param.setSourceSubsampling(subX, subY, subX > 1 ? subX / 2 : 0, subY > 1 ? subY / 2 : 0);
                                }
                            }
                        }

                        // Need base URI for SVG with links/stylesheets etc
                        maybeSetBaseURIFromRequest(param);

/////////////////////

                        // Finally, read the image using the supplied parameter
                        BufferedImage image = reader.read(0, param);

                        // TODO: If we sub-sampled, it would be a good idea to blur before resampling,
                        // to avoid jagged lines artifacts

                        // If reader doesn't support dynamic sizing, scale now
                        image = resampleImage(image, size);

                        // Fill bgcolor behind image, if transparent
                        extractAndSetBackgroundColor(image); // TODO: Move to flush/POST-PROCESS

                        // Set image
                        this.image = image;
                    }
                    finally {
                        reader.dispose();
                    }
                }
                else {
                    context.log("ERROR: No suitable image reader found (content-type: " + originalContentType + ").");
                    context.log("ERROR: Available formats: " + getFormatsString());

                    throw new IIOException("Unable to transcode image: No suitable image reader found (content-type: " + originalContentType + ").");
                }

                // Free resources, as the image is now either read, or unreadable
                bufferedOut = null;
            }
            finally {
                if (input != null) {
                    input.close();
                }
            }
        }

        // Image is usually a BufferedImage, but may also be a RenderedImage
        return image != null ? ImageUtil.toBuffered(image) : null;
    }
1323880
@Override
    protected Integer theCalculation(Bitmap image) {
        Map histogram = new HashMap();

        for(int i = 0; i < image.getWidth(); i++)
            for(int j = 0; j < image.getHeight(); j++) {
                int rgb = image.getPixel(i,j);

                int rgbArray[] = getRGBArrayFromPixel(rgb);

                if(!isGray(rgbArray)) {

                    Integer counter = (Integer) histogram.get(rgb);

                    if (counter == null)
                        counter = 0;
                    counter++;

                    histogram.put(rgb, counter);
                }
            }

        return Color.parseColor("#"+getMostCommonColor(histogram));
    }
34315
private Graphics2D initGraphics(BufferedImage img)
	{
		Graphics2D g = img.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
		if (options.antialias)
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		g.setFont(font);

		if (options.debug)
		{
			int color = 0x666666;
			for (int i = 0; i < img.getWidth(); i += 25)
			{
				if (i % 500 == 0)
					color = 0x333333;
				else if (i % 100 == 0)
					color = 0x777777;
				else
					color = 0xBBBBBB;
				g.setColor(new Color(color));
				g.drawLine(i, 0, i, img.getWidth());
				g.drawLine(0, i, img.getHeight(), i);
			}
		}

		g.setColor(options.debug ? Color.BLACK : Color.WHITE);

		return g;
	}