GenuineSounds

ShapeCase

Jun 18th, 2012
210
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.51 KB | None | 0 0
  1. package ltd.genuinesounds.image;
  2.  
  3. import java.awt.image.BufferedImage;
  4.  
  5. class ShapeCase {
  6.  
  7.     private static final byte[] arrayRules = {
  8.         14,  1,  3,  5, 12,  1,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  9.         14,  1,  3,  5, 12, 16,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  10.         12,  1,  3,  5, 12, 16,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  11.         13, 16,  3,  5, 12, 16,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  12.         14,  1,  3,  5, 13, 16,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  13.         14, 16,  3,  5, 13, 17,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  14.         12, 16,  3,  5, 12, 16,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  15.         13, 17,  3,  5, 12, 15,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  16.         14,  1, 19,  5, 12, 16,  3,  5, 18,  6,  4,  4,  2,  6,  4,  4,
  17.         14,  1, 19,  5, 12,  1, 19,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  18.         12, 16,  3,  5, 12, 16,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  19.         12, 16, 19,  5, 12, 12, 19,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  20.         14,  1,  3,  5, 12, 16,  3,  5, 18,  6,  4,  4, 18,  6,  4,  4,
  21.         14, 16,  3,  5, 13, 17,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4,
  22.         12,  1,  3,  5, 12, 12,  3,  5, 18,  6,  4,  4, 18,  6,  4,  4,
  23.         13, 17,  3,  5, 12, 15,  3,  5,  2,  6,  4,  4,  2,  6,  4,  4
  24.     };
  25.     private HQ2x theEngine;
  26.     private int[] colorMatrix = new int[9];
  27.     private boolean[] bitMatrix = new boolean[8];
  28.  
  29.     public ShapeCase(HQ2x engine) {
  30.         theEngine = engine;
  31.     }
  32.  
  33.     public ShapeCase(HQ2x engine, BufferedImage image, int x, int y) {
  34.         theEngine = engine;
  35.         // 0 1 2
  36.         // 3 4 5
  37.         // 6 7 8
  38.         if (x < 1 || y < 1 || x >= image.getWidth() - 1 || y >= image.getHeight() - 1) {
  39.             colorMatrix[0] = getColor(image, x - 1, y - 1);
  40.             colorMatrix[1] = getColor(image, x, y - 1);
  41.             colorMatrix[2] = getColor(image, x + 1, y - 1);
  42.             colorMatrix[3] = getColor(image, x - 1, y);
  43.             colorMatrix[4] = getColor(image, x, y);
  44.             colorMatrix[5] = getColor(image, x + 1, y);
  45.             colorMatrix[6] = getColor(image, x - 1, y + 1);
  46.             colorMatrix[7] = getColor(image, x, y + 1);
  47.             colorMatrix[8] = getColor(image, x + 1, y + 1);
  48.         } else {
  49.             colorMatrix[0] = image.getRGB(x - 1, y - 1);
  50.             colorMatrix[1] = image.getRGB(x, y - 1);
  51.             colorMatrix[2] = image.getRGB(x + 1, y - 1);
  52.             colorMatrix[3] = image.getRGB(x - 1, y);
  53.             colorMatrix[4] = image.getRGB(x, y);
  54.             colorMatrix[5] = image.getRGB(x + 1, y);
  55.             colorMatrix[6] = image.getRGB(x - 1, y + 1);
  56.             colorMatrix[7] = image.getRGB(x, y + 1);
  57.             colorMatrix[8] = image.getRGB(x + 1, y + 1);
  58.         }
  59.         // 0 1 2
  60.         // 3   4
  61.         // 5 6 7
  62.         bitMatrix[0] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[0]);
  63.         bitMatrix[1] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[1]);
  64.         bitMatrix[2] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[2]);
  65.         bitMatrix[3] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[3]);
  66.         bitMatrix[4] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[5]);
  67.         bitMatrix[5] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[6]);
  68.         bitMatrix[6] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[7]);
  69.         bitMatrix[7] = theEngine.threshold.isSimilar(colorMatrix[4], colorMatrix[8]);
  70.     }
  71.  
  72.     public ShapeCase rotatedCCWCopy() {
  73.         ShapeCase result = new ShapeCase(theEngine);
  74.         result.colorMatrix[0] = colorMatrix[2];
  75.         result.colorMatrix[1] = colorMatrix[5];
  76.         result.colorMatrix[2] = colorMatrix[8];
  77.         result.colorMatrix[3] = colorMatrix[1];
  78.         result.colorMatrix[4] = colorMatrix[4];
  79.         result.colorMatrix[5] = colorMatrix[7];
  80.         result.colorMatrix[6] = colorMatrix[0];
  81.         result.colorMatrix[7] = colorMatrix[3];
  82.         result.colorMatrix[8] = colorMatrix[6];
  83.         result.bitMatrix[0] = bitMatrix[2];
  84.         result.bitMatrix[1] = bitMatrix[4];
  85.         result.bitMatrix[2] = bitMatrix[7];
  86.         result.bitMatrix[3] = bitMatrix[1];
  87.         result.bitMatrix[4] = bitMatrix[6];
  88.         result.bitMatrix[5] = bitMatrix[0];
  89.         result.bitMatrix[6] = bitMatrix[3];
  90.         result.bitMatrix[7] = bitMatrix[5];
  91.         return result;
  92.     }
  93.  
  94.     public int result() {
  95.         int count = 0;
  96.         for (int i = 0; i < 8; i++) {
  97.             count += bitMatrix[i] ? (1 << i) : 0;
  98.         }
  99.         byte rule = arrayRules[count];
  100.         switch (rule) {
  101.         case 1:
  102.             return case3x1(colorMatrix[4], colorMatrix[0]);
  103.         case 2:
  104.             return case3x1(colorMatrix[4], colorMatrix[3]);
  105.         case 3:
  106.             return case3x1(colorMatrix[4], colorMatrix[1]);
  107.         case 4:
  108.             return case2x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  109.         case 5:
  110.             return case2x1x1(colorMatrix[4], colorMatrix[0], colorMatrix[1]);
  111.         case 6:
  112.             return case2x1x1(colorMatrix[4], colorMatrix[0], colorMatrix[3]);
  113.         case 7:
  114.             return case5x2x1(colorMatrix[4], colorMatrix[1], colorMatrix[3]);
  115.         case 8:
  116.             return case5x2x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  117.         case 9:
  118.             return case6x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  119.         case 10:
  120.             return case2x3x3(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  121.         case 11:
  122.             return case14x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  123.         case 12:
  124.             if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
  125.                 return case2x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  126.             else
  127.                 return colorMatrix[4];
  128.         case 13:
  129.             if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
  130.                 return case2x3x3(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  131.             else
  132.                 return colorMatrix[4];
  133.         case 14:
  134.             if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
  135.                 return case14x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  136.             else
  137.                 return colorMatrix[4];
  138.         case 15:
  139.             if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
  140.                 return case2x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  141.             else
  142.                 return case3x1(colorMatrix[4], colorMatrix[0]);
  143.         case 16:
  144.             if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
  145.                 return case6x1x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  146.             else
  147.                 return case3x1(colorMatrix[4], colorMatrix[0]);
  148.         case 17:
  149.             if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[3]))
  150.                 return case2x3x3(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  151.             else
  152.                 return case3x1(colorMatrix[4], colorMatrix[0]);
  153.         case 18:
  154.             if (theEngine.threshold.isSimilar(colorMatrix[1], colorMatrix[5]))
  155.                 return case5x2x1(colorMatrix[4], colorMatrix[1], colorMatrix[3]);
  156.             else
  157.                 return case3x1(colorMatrix[4], colorMatrix[3]);
  158.         case 19:
  159.             if (theEngine.threshold.isSimilar(colorMatrix[3], colorMatrix[7]))
  160.                 return case5x2x1(colorMatrix[4], colorMatrix[3], colorMatrix[1]);
  161.             else
  162.                 return case3x1(colorMatrix[4], colorMatrix[1]);
  163.         default:
  164.             return colorMatrix[4];
  165.         }
  166.     }
  167.  
  168.     private int getColor(BufferedImage image, int x, int y) {
  169.         if (x < 0 || y < 0 || x >= image.getWidth() || y >= image.getHeight())
  170.             return 0;
  171.         return image.getRGB(x, y);
  172.     }
  173.  
  174.     public int case3x1(int color1, int color2) {
  175.         return theEngine.normal.lerp(color1, 3, color2, 1, 0, 0);
  176.     }
  177.  
  178.     public int case2x1x1(int color1, int color2, int color3) {
  179.         return theEngine.normal.lerp(color1, 2, color2, 1, color3, 1);
  180.     }
  181.  
  182.     public int case5x2x1(int color1, int color2, int color3) {
  183.         return theEngine.normal.lerp(color1, 5, color2, 2, color3, 1);
  184.     }
  185.  
  186.     public int case6x1x1(int color1, int color2, int color3) {
  187.         return theEngine.normal.lerp(color1, 6, color2, 1, color3, 1);
  188.     }
  189.  
  190.     public int case2x3x3(int color1, int color2, int color3) {
  191.         return theEngine.normal.lerp(color1, 2, color2, 3, color3, 3);
  192.     }
  193.  
  194.     public int case14x1x1(int color1, int color2, int color3) {
  195.         return theEngine.normal.lerp(color1, 14, color2, 1, color3, 1);
  196.     }
  197. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment