Advertisement
Guest User

Rasterizer2D

a guest
Jan 9th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.24 KB | None | 0 0
  1. package org.astrect.graphics;
  2.  
  3. import java.awt.geom.Ellipse2D;
  4.  
  5. import org.astrect.notrefactored.Node_Sub;
  6.  
  7. /**
  8.  * Handles 2D graphic components drawing.
  9.  */
  10. public class Rasterizer2D extends Node_Sub {
  11.  
  12.     public static Ellipse2D clipEllipse;
  13.     public static int areaPixels[];
  14.     public static int areaWidth;
  15.     public static int areaHeight;
  16.     public static int clipStartY;
  17.     public static int clipEndY;
  18.     public static int clipStartX;
  19.     public static int clipEndX;
  20.     public static int clipCenterX;
  21.     public static int clipCenterY;
  22.  
  23.     /**
  24.      * Clears the whole area that has been initialized to black.
  25.      */
  26.     public static void clearArea() {
  27.         final int i = areaWidth * areaHeight;
  28.         for (int j = 0; j < i; j++) {
  29.             areaPixels[j] = 0;
  30.         }
  31.     }
  32.  
  33.     /**
  34.      * Draws a horizontal line.
  35.      */
  36.     public static void drawHorizontalLine(int x, int y, int length, int color) {
  37.         if (y < clipStartY || y >= clipEndY) {
  38.             return;
  39.         }
  40.         if (x < clipStartX) {
  41.             length -= clipStartX - x;
  42.             x = clipStartX;
  43.         }
  44.         if (x + length > clipEndX) {
  45.             length = clipEndX - x;
  46.         }
  47.         final int i1 = x + y * areaWidth;
  48.         for (int j1 = 0; j1 < length; j1++) {
  49.             areaPixels[i1 + j1] = getColorWithEllipseClipping(i1 + j1, color);
  50.         }
  51.     }
  52.  
  53.     /**
  54.      * Draws a horizontal line with an alpha value support.
  55.      */
  56.     public static void drawHorizontalLine(int x, int y, int length, int color, int alpha) {
  57.         if (y < clipStartY || y >= clipEndY) {
  58.             return;
  59.         }
  60.         if (x < clipStartX) {
  61.             length -= clipStartX - x;
  62.             x = clipStartX;
  63.         }
  64.         if (x + length > clipEndX) {
  65.             length = clipEndX - x;
  66.         }
  67.         final int j1 = 256 - alpha;
  68.         final int k1 = (color >> 16 & 0xff) * alpha;
  69.         final int l1 = (color >> 8 & 0xff) * alpha;
  70.         final int i2 = (color & 0xff) * alpha;
  71.         int i3 = x + y * areaWidth;
  72.         for (int j3 = 0; j3 < length; j3++) {
  73.             final int j2 = (areaPixels[i3] >> 16 & 0xff) * j1;
  74.             final int k2 = (areaPixels[i3] >> 8 & 0xff) * j1;
  75.             final int l2 = (areaPixels[i3] & 0xff) * j1;
  76.             final int k3 = (k1 + j2 >> 8 << 16) + (l1 + k2 >> 8 << 8) + (i2 + l2 >> 8);
  77.             areaPixels[i3] = getColorWithEllipseClipping(i3++, k3);
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * Draws a line from (x1, y1) to (x2, y2) with the specified color.
  83.      */
  84.     public static void drawLine(int x1, int y1, int x2, int y2, int color) {
  85.         double xDifference = x1 - x2;
  86.         if (xDifference < 0) {
  87.             xDifference = -xDifference;
  88.         }
  89.         double yDifference = y1 - y2;
  90.         if (yDifference < 0) {
  91.             yDifference = -yDifference;
  92.         }
  93.         final double slope = yDifference / xDifference;
  94.         if (xDifference >= yDifference) {
  95.             for (int x = (int) xDifference; x >= 0; x--) {
  96.                 final int y = (int) (slope * x);
  97.                 if (x1 + x > Rasterizer2D.clipEndX || x1 + x < Rasterizer2D.clipStartX
  98.                         || y1 + y > Rasterizer2D.clipEndY || y1 + y < Rasterizer2D.clipStartY) {
  99.                     continue;
  100.                 }
  101.                 try {
  102.                     final int xPos = x1 < x2 ? x : -x;
  103.                     final int yPos = y1 < y2 ? y : -y;
  104.                     areaPixels[x1 + xPos + (y1 + yPos) * areaWidth] = color;
  105.                 } catch (final Exception e) {
  106.                 }
  107.             }
  108.         } else {
  109.             for (int y = (int) yDifference; y >= 0; y--) {
  110.                 final int x = (int) (y / slope);
  111.                 if (x1 + x > Rasterizer2D.clipEndX || x1 + x < Rasterizer2D.clipStartX
  112.                         || y1 + y > Rasterizer2D.clipEndY || y1 + y < Rasterizer2D.clipStartY) {
  113.                     continue;
  114.                 }
  115.                 try {
  116.                     final int xPos = x1 < x2 ? x : -x;
  117.                     final int yPos = y1 < y2 ? y : -y;
  118.                     areaPixels[x1 + xPos + (y1 + yPos) * areaWidth] = color;
  119.                 } catch (final Exception e) {
  120.                 }
  121.             }
  122.         }
  123.     }
  124.  
  125.     /**
  126.      * Draws a line from (x1, y1) to (x2, y2) with the specified color and alpha
  127.      * value.
  128.      */
  129.     public static void drawLine(int x1, int y1, int x2, int y2, int color, int alpha) {
  130.         double xDifference = x1 - x2;
  131.         if (xDifference < 0) {
  132.             xDifference = -xDifference;
  133.         }
  134.         double yDifference = y1 - y2;
  135.         if (yDifference < 0) {
  136.             yDifference = -yDifference;
  137.         }
  138.         final double slope = yDifference / xDifference;
  139.         final int alpha2 = 256 - alpha;
  140.         final int red = (color >> 16 & 0xff) * alpha;
  141.         final int green = (color >> 8 & 0xff) * alpha;
  142.         final int blue = (color & 0xff) * alpha;
  143.         if (xDifference >= yDifference) {
  144.             for (int x = (int) xDifference; x >= 0; x--) {
  145.                 final int y = (int) (slope * x);
  146.                 if (x1 + x > Rasterizer2D.clipEndX || x1 + x < Rasterizer2D.clipStartX
  147.                         || y1 + y > Rasterizer2D.clipEndY || y1 + y < Rasterizer2D.clipStartY) {
  148.                     continue;
  149.                 }
  150.                 try {
  151.                     final int xPos = x1 < x2 ? x : -x;
  152.                     final int yPos = y1 < y2 ? y : -y;
  153.                     final int pixel = x1 + xPos + (y1 + yPos) * areaWidth;
  154.                     final int j2 = (areaPixels[pixel] >> 16 & 0xff) * alpha2;
  155.                     final int k2 = (areaPixels[pixel] >> 8 & 0xff) * alpha2;
  156.                     final int l2 = (areaPixels[pixel] & 0xff) * alpha2;
  157.                     final int k3 = (red + j2 >> 8 << 16) + (green + k2 >> 8 << 8) + (blue + l2 >> 8);
  158.                     areaPixels[pixel] = k3;
  159.                 } catch (final Exception e) {
  160.                 }
  161.             }
  162.         } else {
  163.             for (int y = (int) yDifference; y >= 0; y--) {
  164.                 final int x = (int) (y / slope);
  165.                 if (x1 + x > Rasterizer2D.clipEndX || x1 + x < Rasterizer2D.clipStartX
  166.                         || y1 + y > Rasterizer2D.clipEndY || y1 + y < Rasterizer2D.clipStartY) {
  167.                     continue;
  168.                 }
  169.                 try {
  170.                     final int xPos = x1 < x2 ? x : -x;
  171.                     final int yPos = y1 < y2 ? y : -y;
  172.                     final int pixel = x1 + xPos + (y1 + yPos) * areaWidth;
  173.                     final int j2 = (areaPixels[pixel] >> 16 & 0xff) * alpha2;
  174.                     final int k2 = (areaPixels[pixel] >> 8 & 0xff) * alpha2;
  175.                     final int l2 = (areaPixels[pixel] & 0xff) * alpha2;
  176.                     final int k3 = (red + j2 >> 8 << 16) + (green + k2 >> 8 << 8) + (blue + l2 >> 8);
  177.                     areaPixels[pixel] = k3;
  178.                 } catch (final Exception e) {
  179.                 }
  180.             }
  181.         }
  182.     }
  183.  
  184.     /**
  185.      * Draws a non-filled rectangle.
  186.      */
  187.     public static void drawRectangle(int x, int y, int width, int height, int color) {
  188.         drawHorizontalLine(x, y, width, color);
  189.         drawHorizontalLine(x, y + height - 1, width, color);
  190.         drawVerticalLine(x, y, height, color);
  191.         drawVerticalLine(x + width - 1, y, height, color);
  192.     }
  193.  
  194.     /**
  195.      * Draws a non-filled rectangle with an alpha value support.
  196.      */
  197.     public static void drawRectangle(int x, int y, int width, int height, int color, int alpha) {
  198.         drawHorizontalLine(x, y, width, color, alpha);
  199.         drawHorizontalLine(x, y + height - 1, width, color, alpha);
  200.         if (height > 2) {
  201.             drawVerticalLine(x, y + 1, height - 2, color, alpha);
  202.             drawVerticalLine(x + width - 1, y + 1, height - 2, color, alpha);
  203.         }
  204.     }
  205.  
  206.     /**
  207.      * Draws a vertical line.
  208.      */
  209.     public static void drawVerticalLine(int x, int y, int length, int color) {
  210.         if (x < clipStartX || x >= clipEndX) {
  211.             return;
  212.         }
  213.         if (y < clipStartY) {
  214.             length -= clipStartY - y;
  215.             y = clipStartY;
  216.         }
  217.         if (y + length > clipEndY) {
  218.             length = clipEndY - y;
  219.         }
  220.         final int j1 = x + y * areaWidth;
  221.         for (int k1 = 0; k1 < length; k1++) {
  222.             areaPixels[j1 + k1 * areaWidth] = getColorWithEllipseClipping(j1 + k1 * areaWidth, color);
  223.         }
  224.     }
  225.  
  226.     /**
  227.      * Draws a vertical line with an alpha value support.
  228.      */
  229.     public static void drawVerticalLine(int x, int y, int length, int color, int alpha) {
  230.         if (x < clipStartX || x >= clipEndX) {
  231.             return;
  232.         }
  233.         if (y < clipStartY) {
  234.             length -= clipStartY - y;
  235.             y = clipStartY;
  236.         }
  237.         if (y + length > clipEndY) {
  238.             length = clipEndY - y;
  239.         }
  240.         final int j1 = 256 - alpha;
  241.         final int k1 = (color >> 16 & 0xff) * alpha;
  242.         final int l1 = (color >> 8 & 0xff) * alpha;
  243.         final int i2 = (color & 0xff) * alpha;
  244.         int i3 = x + y * areaWidth;
  245.         for (int j3 = 0; j3 < length; j3++) {
  246.             final int j2 = (areaPixels[i3] >> 16 & 0xff) * j1;
  247.             final int k2 = (areaPixels[i3] >> 8 & 0xff) * j1;
  248.             final int l2 = (areaPixels[i3] & 0xff) * j1;
  249.             final int k3 = (k1 + j2 >> 8 << 16) + (l1 + k2 >> 8 << 8) + (i2 + l2 >> 8);
  250.             areaPixels[i3] = getColorWithEllipseClipping(i3, k3);
  251.             i3 += areaWidth;
  252.         }
  253.     }
  254.  
  255.     /**
  256.      * Draws a filled rectangle.
  257.      */
  258.     public static void fillRectangle(int x, int y, int width, int height, int color) {
  259.         if (x < clipStartX) {
  260.             width -= clipStartX - x;
  261.             x = clipStartX;
  262.         }
  263.         if (y < clipStartY) {
  264.             height -= clipStartY - y;
  265.             y = clipStartY;
  266.         }
  267.         if (x + width > clipEndX) {
  268.             width = clipEndX - x;
  269.         }
  270.         if (y + height > clipEndY) {
  271.             height = clipEndY - y;
  272.         }
  273.         final int k1 = areaWidth - width;
  274.         int l1 = x + y * areaWidth;
  275.         for (int i2 = -height; i2 < 0; i2++) {
  276.             for (int j2 = -width; j2 < 0; j2++) {
  277.                 areaPixels[l1] = getColorWithEllipseClipping(l1++, color);
  278.             }
  279.             l1 += k1;
  280.         }
  281.  
  282.     }
  283.  
  284.     /**
  285.      * Draws a filled rectangle with an alpha value support.
  286.      */
  287.     public static void fillRectangle(int x, int y, int width, int height, int color, int alpha) {
  288.         if (x < clipStartX) {
  289.             width -= clipStartX - x;
  290.             x = clipStartX;
  291.         }
  292.         if (y < clipStartY) {
  293.             height -= clipStartY - y;
  294.             y = clipStartY;
  295.         }
  296.         if (x + width > clipEndX) {
  297.             width = clipEndX - x;
  298.         }
  299.         if (y + height > clipEndY) {
  300.             height = clipEndY - y;
  301.         }
  302.         final int l1 = 256 - alpha;
  303.         final int i2 = (color >> 16 & 0xff) * alpha;
  304.         final int j2 = (color >> 8 & 0xff) * alpha;
  305.         final int k2 = (color & 0xff) * alpha;
  306.         final int k3 = areaWidth - width;
  307.         int l3 = x + y * areaWidth;
  308.         for (int i4 = 0; i4 < height; i4++) {
  309.             for (int j4 = -width; j4 < 0; j4++) {
  310.                 final int l2 = (areaPixels[l3] >> 16 & 0xff) * l1;
  311.                 final int i3 = (areaPixels[l3] >> 8 & 0xff) * l1;
  312.                 final int j3 = (areaPixels[l3] & 0xff) * l1;
  313.                 final int k4 = (i2 + l2 >> 8 << 16) + (j2 + i3 >> 8 << 8) + (k2 + j3 >> 8);
  314.                 areaPixels[l3] = getColorWithEllipseClipping(l3++, k4);
  315.             }
  316.             l3 += k3;
  317.         }
  318.     }
  319.  
  320.     /**
  321.      * Checks if the pixel is inside clipping. Returns parameter color if true,
  322.      * otherwise original color.
  323.      */
  324.     public static int getColorWithEllipseClipping(int px, int color) {
  325.         if (clipEllipse == null) {
  326.             return color;
  327.         }
  328.         final int x = px % areaWidth;
  329.         final int y = (px - x) / areaWidth;
  330.         if (outOfClip(x, y)) {
  331.             return areaPixels[px];
  332.         }
  333.         return color;
  334.     }
  335.  
  336.     /**
  337.      * Initializes the maximum size to the drawing area.
  338.      */
  339.     public static void initializeDrawingArea(int height, int width, int pixels[]) {
  340.         areaPixels = pixels;
  341.         areaWidth = width;
  342.         areaHeight = height;
  343.         setClipRectangle(0, 0, width, height);
  344.     }
  345.  
  346.     /**
  347.      * Check if pixel is outside of clipped area. Returns true if pixel is
  348.      * outside, false otherwise.
  349.      */
  350.     private static boolean outOfClip(int x, int y) {
  351.         if (x < clipStartX || x > clipEndX || y < clipStartY || y > clipEndY) {
  352.             return true;
  353.         }
  354.         if (clipEllipse != null && !clipEllipse.contains(x, y)) {
  355.             return true;
  356.         }
  357.         return false;
  358.     }
  359.  
  360.     /**
  361.      * Sets the clipping to its defaults.
  362.      */
  363.     public static void removeClip() {
  364.         setClipRectangle(0, 0, areaWidth, areaHeight);
  365.     }
  366.  
  367.     /**
  368.      * Sets clipping by a circle.
  369.      */
  370.     public static void setClipCircle(int x, int y, int diameter) {
  371.         setClipRectangle(x, y, x + diameter, y + diameter);
  372.         clipEllipse = new Ellipse2D.Double(x, y, diameter, diameter);
  373.     }
  374.  
  375.     /**
  376.      * Sets clipping by an ellipse.
  377.      */
  378.     public static void setClipEllipse(int x, int y, int diameterX, int diameterY) {
  379.         setClipRectangle(x, y, x + diameterX, y + diameterY);
  380.         clipEllipse = new Ellipse2D.Double(x, y, diameterX, diameterY);
  381.     }
  382.  
  383.     /**
  384.      * Sets clipping by a rectangle.
  385.      */
  386.     public static void setClipRectangle(int startX, int startY, int endX, int endY) {
  387.         clipEllipse = null;
  388.         if (startX < 0) {
  389.             startX = 0;
  390.         }
  391.         if (startY < 0) {
  392.             startY = 0;
  393.         }
  394.         if (endX > areaWidth) {
  395.             endX = areaWidth;
  396.         }
  397.         if (endY > areaHeight) {
  398.             endY = areaHeight;
  399.         }
  400.         clipStartX = startX;
  401.         clipStartY = startY;
  402.         clipEndX = endX;
  403.         clipEndY = endY;
  404.         clipCenterX = clipEndX / 2;
  405.         clipCenterY = clipEndY / 2;
  406.     }
  407.  
  408.     Rasterizer2D() {
  409.     }
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement