Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ltd.genuinesounds.image;
- import java.awt.image.BufferedImage;
- class ShapeCase {
- private static final byte[] arrayRules = {
- 14, 1, 3, 5, 12, 1, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 14, 1, 3, 5, 12, 16, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 12, 1, 3, 5, 12, 16, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 13, 16, 3, 5, 12, 16, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 14, 1, 3, 5, 13, 16, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 14, 16, 3, 5, 13, 17, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 12, 16, 3, 5, 12, 16, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 13, 17, 3, 5, 12, 15, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 14, 1, 19, 5, 12, 16, 3, 5, 18, 6, 4, 4, 2, 6, 4, 4,
- 14, 1, 19, 5, 12, 1, 19, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 12, 16, 3, 5, 12, 16, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 12, 16, 19, 5, 12, 12, 19, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 14, 1, 3, 5, 12, 16, 3, 5, 18, 6, 4, 4, 18, 6, 4, 4,
- 14, 16, 3, 5, 13, 17, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4,
- 12, 1, 3, 5, 12, 12, 3, 5, 18, 6, 4, 4, 18, 6, 4, 4,
- 13, 17, 3, 5, 12, 15, 3, 5, 2, 6, 4, 4, 2, 6, 4, 4
- };
- private HQ2x theEngine;
- private int[] colorMatrix = new int[9];
- private boolean[] bitMatrix = new boolean[8];
- public ShapeCase(HQ2x engine) {
- theEngine = engine;
- }
- public ShapeCase(HQ2x engine, BufferedImage image, int x, int y) {
- theEngine = engine;
- // 0 1 2
- // 3 4 5
- // 6 7 8
- if (x < 1 || y < 1 || x >= image.getWidth() - 1 || y >= image.getHeight() - 1) {
- colorMatrix[0] = getColor(image, x - 1, y - 1);
- colorMatrix[1] = getColor(image, x, y - 1);
- colorMatrix[2] = getColor(image, x + 1, y - 1);
- colorMatrix[3] = getColor(image, x - 1, y);
- colorMatrix[4] = getColor(image, x, y);
- colorMatrix[5] = getColor(image, x + 1, y);
- colorMatrix[6] = getColor(image, x - 1, y + 1);
- colorMatrix[7] = getColor(image, x, y + 1);
- colorMatrix[8] = getColor(image, x + 1, y + 1);
- } else {
- colorMatrix[0] = image.getRGB(x - 1, y - 1);
- colorMatrix[1] = image.getRGB(x, y - 1);
- colorMatrix[2] = image.getRGB(x + 1, y - 1);
- colorMatrix[3] = image.getRGB(x - 1, y);
- colorMatrix[4] = image.getRGB(x, y);
- colorMatrix[5] = image.getRGB(x + 1, y);
- colorMatrix[6] = image.getRGB(x - 1, y + 1);
- colorMatrix[7] = image.getRGB(x, y + 1);
- colorMatrix[8] = image.getRGB(x + 1, y + 1);
- }
- // 0 1 2
- // 3 4
- // 5 6 7
- bitMatrix[0] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[0]);
- bitMatrix[1] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[1]);
- bitMatrix[2] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[2]);
- bitMatrix[3] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[3]);
- bitMatrix[4] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[5]);
- bitMatrix[5] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[6]);
- bitMatrix[6] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[7]);
- bitMatrix[7] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[8]);
- }
- public ShapeCase rotatedCCWCopy() {
- ShapeCase result = new ShapeCase(theEngine);
- result.colorMatrix[0] = colorMatrix[2];
- result.colorMatrix[1] = colorMatrix[5];
- result.colorMatrix[2] = colorMatrix[8];
- result.colorMatrix[3] = colorMatrix[1];
- result.colorMatrix[4] = colorMatrix[4];
- result.colorMatrix[5] = colorMatrix[7];
- result.colorMatrix[6] = colorMatrix[0];
- result.colorMatrix[7] = colorMatrix[3];
- result.colorMatrix[8] = colorMatrix[6];
- result.bitMatrix[0] = bitMatrix[2];
- result.bitMatrix[1] = bitMatrix[4];
- result.bitMatrix[2] = bitMatrix[7];
- result.bitMatrix[3] = bitMatrix[1];
- result.bitMatrix[4] = bitMatrix[6];
- result.bitMatrix[5] = bitMatrix[0];
- result.bitMatrix[6] = bitMatrix[3];
- result.bitMatrix[7] = bitMatrix[5];
- return result;
- }
- public int result() {
- int count = 0;
- for (int i = 0; i < 8; i++) {
- count += bitMatrix[i] ? (1 << i) : 0;
- }
- byte rule = arrayRules[count];
- switch (rule) {
- case 1:
- return case3x1(colorMatrix[4], colorMatrix[0]);
- case 2:
- return case3x1(colorMatrix[4], colorMatrix[3]);
- case 3:
- return case3x1(colorMatrix[4], colorMatrix[1]);
- case 4:
- return case2x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- case 5:
- return case2x1x1(colorMatrix[4], colorMatrix[0], colorMatrix[1]);
- case 6:
- return case2x1x1(colorMatrix[4], colorMatrix[0], colorMatrix[3]);
- case 7:
- return case5x2x1(colorMatrix[4], colorMatrix[1], colorMatrix[3]);
- case 8:
- return case5x2x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- case 9:
- return case6x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- case 10:
- return case2x3x3(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- case 11:
- return case14x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- case 12:
- if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
- return case2x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- else
- return colorMatrix[4];
- case 13:
- if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
- return case2x3x3(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- else
- return colorMatrix[4];
- case 14:
- if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
- return case14x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- else
- return colorMatrix[4];
- case 15:
- if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
- return case2x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- else
- return case3x1(colorMatrix[4], colorMatrix[0]);
- case 16:
- if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
- return case6x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- else
- return case3x1(colorMatrix[4], colorMatrix[0]);
- case 17:
- if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
- return case2x3x3(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- else
- return case3x1(colorMatrix[4], colorMatrix[0]);
- case 18:
- if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[5]))
- return case5x2x1(colorMatrix[4], colorMatrix[1], colorMatrix[3]);
- else
- return case3x1(colorMatrix[4], colorMatrix[3]);
- case 19:
- if (theEngine.threshold.isSimilar(colorMatrix[3], colorMatrix[7]))
- return case5x2x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
- else
- return case3x1(colorMatrix[4], colorMatrix[1]);
- default:
- return colorMatrix[4];
- }
- }
- private int getColor(BufferedImage image, int x, int y) {
- if (x < 0 || y < 0 || x >= image.getWidth() || y >= image.getHeight())
- return 0;
- return image.getRGB(x, y);
- }
- public int case3x1(int color1, int color2) {
- return theEngine.normal.lerp(color1, 3, color2, 1, 0, 0);
- }
- public int case2x1x1(int color1, int color2, int color3) {
- return theEngine.normal.lerp(color1, 2, color2, 1, color3, 1);
- }
- public int case5x2x1(int color1, int color2, int color3) {
- return theEngine.normal.lerp(color1, 5, color2, 2, color3, 1);
- }
- public int case6x1x1(int color1, int color2, int color3) {
- return theEngine.normal.lerp(color1, 6, color2, 1, color3, 1);
- }
- public int case2x3x3(int color1, int color2, int color3) {
- return theEngine.normal.lerp(color1, 2, color2, 3, color3, 3);
- }
- public int case14x1x1(int color1, int color2, int color3) {
- return theEngine.normal.lerp(color1, 14, color2, 1, color3, 1);
- }
- }
Advertisement