![JAR search and dependency download from the Maven repository](/logo.png)
org.mabb.gfxassert.graphics.GfxAssertImage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of GfxAssert Show documentation
Show all versions of GfxAssert Show documentation
GfxAssert is a library for image and geometric asserts in test code and provides hamcrest matchers for doing so.
The newest version!
/*
* Copyright (C) Matthew Abboud 2016
*
* GfxAssert is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GfxAssert is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GfxAssert. If not, see .
*/
package org.mabb.gfxassert.graphics;
import org.mabb.gfxassert.geom.ShapeSubset;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
class GfxAssertImage extends BufferedImage {
public GfxAssertImage(BufferedImage target) {
super(target.getColorModel(), target.copyData(null),
target.getColorModel().isAlphaPremultiplied(), null);
}
public boolean contains(ShapeSubset search, Color findColor) {
StopOnColorFound strat = new StopOnColorFound(findColor);
searchPixels(search, strat);
return strat.found;
}
public List findAllColors(ShapeSubset area) {
FindAllColors strat = new FindAllColors();
searchPixels(area, strat);
return strat.colors;
}
public HashMap findColorsCount(ShapeSubset area) {
FindColorsUsage strat = new FindColorsUsage();
searchPixels(area, strat);
return strat.colors;
}
public double findColorPixelPercentage(ShapeSubset area, Color color) {
return (findColorsCount(area).get(color) / getWidth() * getHeight()) * 100.0;
}
protected void searchPixels(ShapeSubset area, PixelSearchStrategy strat) {
List shapes = area.getToScale(getBounds());
for (Rectangle2D rectOn : shapes)
searchPixelsInRect(rectOn, strat);
}
protected void searchPixelsInRect(Rectangle2D rect, PixelSearchStrategy strat) {
int xEnd = (int) rect.getMaxX();
int xBegin = (int) rect.getX();
int yBegin = (int) rect.getY();
int yEnd = (int) rect.getMaxY();
for (int x = xBegin; x < xEnd; x++) {
for (int y = yBegin; y < yEnd; y++) {
Color colorOn = new Color(getRGB(x, y), true);
boolean shouldStop = strat.onPixel(colorOn, x, y);
if (shouldStop)
return;
}
}
}
protected Rectangle2D.Double getBounds() {
return new Rectangle2D.Double(0, 0, getWidth(), getHeight());
}
interface PixelSearchStrategy {
/**
* @return true if the image pixel search should end.
*/
boolean onPixel(Color color, int x, int y);
}
class StopOnColorFound implements PixelSearchStrategy {
public boolean found = false;
private final Color stopOn;
StopOnColorFound(Color color) {
this.stopOn = color;
}
public boolean onPixel(Color color, int x, int y) {
found = stopOn.equals(color);
return found;
}
}
class FindAllColors implements PixelSearchStrategy {
final List colors = new LinkedList();
public boolean onPixel(Color color, int x, int y) {
if (!colors.contains(color))
colors.add(color);
return false;
}
}
class FindColorsUsage implements PixelSearchStrategy {
final HashMap colors = new LinkedHashMap();
public boolean onPixel(Color color, int x, int y) {
if (colors.containsKey(color))
colors.put(color, colors.get(color) + 1);
else
colors.put(color, 1);
return false;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy