Advertisement
Chiddix

Rasterizer

Jan 31st, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.03 KB | None | 0 0
  1. public class Rasterizer extends CacheableNode {
  2.    
  3.     public static int[] pixels;
  4.     public static int width;
  5.     public static int height;
  6.     public static int topY;
  7.     public static int bottomY;
  8.     public static int topX;
  9.     public static int bottomX;
  10.     public static int virtualBottomX;
  11.     public static int centerX;
  12.     public static int centerY;
  13.  
  14.     public static void createRasterizer(int[] pixels, int width, int height) {
  15.         Rasterizer.pixels = pixels;
  16.         Rasterizer.width = width;
  17.         Rasterizer.height = height;
  18.         Rasterizer.setBoundaries(0, 0, width, height);
  19.     }
  20.  
  21.     public static void resetConstants() {
  22.         Rasterizer.topX = 0;
  23.         Rasterizer.topY = 0;
  24.         Rasterizer.bottomX = Rasterizer.width;
  25.         Rasterizer.bottomY = Rasterizer.height;
  26.         Rasterizer.virtualBottomX = Rasterizer.bottomX - 1;
  27.         Rasterizer.centerX = Rasterizer.bottomX / 2;
  28.     }
  29.  
  30.     public static void setBoundaries(int x, int y, int width, int height) {
  31.         if (x < 0) {
  32.             x = 0;
  33.         }
  34.         if (y < 0) {
  35.             y = 0;
  36.         }
  37.         if (width > Rasterizer.width) {
  38.             width = Rasterizer.width;
  39.         }
  40.         if (height > Rasterizer.height) {
  41.             height = Rasterizer.height;
  42.         }
  43.         Rasterizer.topX = x;
  44.         Rasterizer.topY = y;
  45.         Rasterizer.bottomX = width;
  46.         Rasterizer.bottomY = height;
  47.         Rasterizer.virtualBottomX = Rasterizer.bottomX - 1;
  48.         Rasterizer.centerX = Rasterizer.bottomX / 2;
  49.         Rasterizer.centerY = Rasterizer.bottomY / 2;
  50.     }
  51.  
  52.     public static void resetPixels() {
  53.         int pixelCount = Rasterizer.width * Rasterizer.height;
  54.         for (int pixel = 0; pixel < pixelCount; pixel++) {
  55.             Rasterizer.pixels[pixel] = 0;
  56.         }
  57.     }
  58.  
  59.     public static void drawFilledRectangleAlhpa(int x, int y, int width, int height, int color, int alpha) {
  60.         if (x < Rasterizer.topX) {
  61.             width -= Rasterizer.topX - x;
  62.             x = Rasterizer.topX;
  63.         }
  64.         if (y < Rasterizer.topY) {
  65.             height -= Rasterizer.topY - y;
  66.             y = Rasterizer.topY;
  67.         }
  68.         if (x + width > Rasterizer.bottomX) {
  69.             width = Rasterizer.bottomX - x;
  70.         }
  71.         if (y + height > Rasterizer.bottomY) {
  72.             height = Rasterizer.bottomY - y;
  73.         }
  74.         int a = 256 - alpha;
  75.         int r = (color >> 16 & 0xff) * alpha;
  76.         int g = (color >> 8 & 0xff) * alpha;
  77.         int b = (color & 0xff) * alpha;
  78.         int widthOffset = Rasterizer.width - width;
  79.         int pixel = x + y * Rasterizer.width;
  80.         for (int heightCounter = 0; heightCounter < height; heightCounter++) {
  81.             for (int widthCounter = -width; widthCounter < 0; widthCounter++) {
  82.                 int red = (Rasterizer.pixels[pixel] >> 16 & 0xff) * a;
  83.                 int green = (Rasterizer.pixels[pixel] >> 8 & 0xff) * a;
  84.                 int blue = (Rasterizer.pixels[pixel] & 0xff) * a;
  85.                 int rgba = (r + red >> 8 << 16) + (g + green >> 8 << 8) + (b + blue >> 8);
  86.                 Rasterizer.pixels[pixel++] = rgba;
  87.             }
  88.             pixel += widthOffset;
  89.         }
  90.     }
  91.  
  92.     public static void drawFilledRectangle(int x, int y, int width, int height, int color) {
  93.         if (x < Rasterizer.topX) {
  94.             width -= Rasterizer.topX - x;
  95.             x = Rasterizer.topX;
  96.         }
  97.         if (y < Rasterizer.topY) {
  98.             height -= Rasterizer.topY - y;
  99.             y = Rasterizer.topY;
  100.         }
  101.         if (x + width > Rasterizer.bottomX) {
  102.             width = Rasterizer.bottomX - x;
  103.         }
  104.         if (y + height > Rasterizer.bottomY) {
  105.             height = Rasterizer.bottomY - y;
  106.         }
  107.         int pixelOffset = Rasterizer.width - width;
  108.         int pixel = x + y * Rasterizer.width;
  109.         for (int heightCounter = -height; heightCounter < 0; heightCounter++) {
  110.             for (int widthCounter = -width; widthCounter < 0; widthCounter++) {
  111.                 Rasterizer.pixels[pixel++] = color;
  112.             }
  113.             pixel += pixelOffset;
  114.         }
  115.     }
  116.  
  117.     public static void drawUnfilledRectangle(int x, int y, int width, int height, int color) {
  118.         Rasterizer.drawHorizontalLine(x, y, width, color);
  119.         Rasterizer.drawHorizontalLine(x, y + height - 1, width, color);
  120.         Rasterizer.drawVerticalLine(x, y, height, color);
  121.         Rasterizer.drawVerticalLine(x + width - 1, y, height, color);
  122.     }
  123.  
  124.     public static void drawUnfilledRectangleAlpha(int x, int y, int width, int height, int color, int alpha) {
  125.         Rasterizer.drawHorizontalLineAlpha(x, y, width, color, alpha);
  126.         Rasterizer.drawHorizontalLineAlpha(x, y + height - 1, width, color, alpha);
  127.         if (height < 3) {
  128.             Rasterizer.drawVerticalLineAlpha(x, y + 1, height - 2, color, alpha);
  129.             Rasterizer.drawVerticalLineAlpha(x + width - 1, y + 1, height - 2, color, alpha);
  130.         }
  131.     }
  132.  
  133.     public static void drawHorizontalLine(int x, int y, int length, int color) {
  134.         if (y >= Rasterizer.topY && y < Rasterizer.bottomY) {
  135.             if (x < Rasterizer.topX) {
  136.                 length -= Rasterizer.topX - x;
  137.                 x = Rasterizer.topX;
  138.             }
  139.             if (x + length > Rasterizer.bottomX) {
  140.                 length = Rasterizer.bottomX - x;
  141.             }
  142.             int pixelOffset = x + y * Rasterizer.width;
  143.             for (int pixel = 0; pixel < length; pixel++) {
  144.                 Rasterizer.pixels[pixelOffset + pixel] = color;
  145.             }
  146.         }
  147.     }
  148.  
  149.     public static void drawHorizontalLineAlpha(int x, int y, int length, int color, int alpha) {
  150.         if (y >= Rasterizer.topY && y < Rasterizer.bottomY) {
  151.             if (x < Rasterizer.topX) {
  152.                 length -= Rasterizer.topX - x;
  153.                 x = Rasterizer.topX;
  154.             }
  155.             if (x + length > Rasterizer.bottomX) {
  156.                 length = Rasterizer.bottomX - x;
  157.             }
  158.             int a = 256 - alpha;
  159.             int r = (color >> 16 & 0xff) * alpha;
  160.             int g = (color >> 8 & 0xff) * alpha;
  161.             int b = (color & 0xff) * alpha;
  162.             int pixelOffset = x + y * Rasterizer.width;
  163.             for (int lengthCounter = 0; lengthCounter < length; lengthCounter++) {
  164.                 int red = (Rasterizer.pixels[pixelOffset] >> 16 & 0xff) * a;
  165.                 int green = (Rasterizer.pixels[pixelOffset] >> 8 & 0xff) * a;
  166.                 int blue = (Rasterizer.pixels[pixelOffset] & 0xff) * a;
  167.                 int rgba = (r + red >> 8 << 16) + (g + green >> 8 << 8) + (b + blue >> 8);
  168.                 Rasterizer.pixels[pixelOffset++] = rgba;
  169.             }
  170.         }
  171.     }
  172.  
  173.     public static void drawVerticalLine(int x, int y, int length, int color) {
  174.         if (x >= Rasterizer.topX && x < Rasterizer.bottomX) {
  175.             if (y < Rasterizer.topY) {
  176.                 length -= Rasterizer.topY - y;
  177.                 y = Rasterizer.topY;
  178.             }
  179.             if (y + length > Rasterizer.bottomY) {
  180.                 length = Rasterizer.bottomY - y;
  181.             }
  182.             int pixelOffset = x + y * Rasterizer.width;
  183.             for (int pixel = 0; pixel < length; pixel++) {
  184.                 Rasterizer.pixels[pixelOffset + pixel * Rasterizer.width] = color;
  185.             }
  186.         }
  187.     }
  188.  
  189.     public static void drawVerticalLineAlpha(int x, int y, int length, int color, int alpha) {
  190.         if (x >= Rasterizer.topX && x < Rasterizer.bottomX) {
  191.             if (y < Rasterizer.topY) {
  192.                 length -= Rasterizer.topY - y;
  193.                 y = Rasterizer.topY;
  194.             }
  195.             if (y + length > Rasterizer.bottomY) {
  196.                 length = Rasterizer.bottomY - y;
  197.             }
  198.             int a = 256 - alpha;
  199.             int r = (color >> 16 & 0xff) * alpha;
  200.             int g = (color >> 8 & 0xff) * alpha;
  201.             int b = (color & 0xff) * alpha;
  202.             int pixel = x + y * Rasterizer.width;
  203.             for (int lengthCounter = 0; lengthCounter < length; lengthCounter++) {
  204.                 int red = (Rasterizer.pixels[pixel] >> 16 & 0xff) * a;
  205.                 int blue = (Rasterizer.pixels[pixel] >> 8 & 0xff) * a;
  206.                 int green = (Rasterizer.pixels[pixel] & 0xff) * a;
  207.                 int rgba = (r + red >> 8 << 16) + (g + blue >> 8 << 8) + (b + green >> 8);
  208.                 Rasterizer.pixels[pixel] = rgba;
  209.                 pixel += Rasterizer.width;
  210.             }
  211.         }
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement