Advertisement
Chiddix

Rasterizer

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