No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarkerUtils.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package org.insa.graphics.drawing.overlays;
  2. import java.awt.Color;
  3. import java.awt.Image;
  4. import java.awt.image.BufferedImage;
  5. import java.io.DataInputStream;
  6. import org.insa.graphics.drawing.Drawing.AlphaMode;
  7. public class MarkerUtils {
  8. /**
  9. * Create an image to represent a marker using the given color for the outer and
  10. * inner part, and the given mode for the inner part.
  11. *
  12. * @param outer Outer color of the marker.
  13. * @param inner Inner color of the marker.
  14. * @param mode Mode to use to fill the inner part of the marker.
  15. *
  16. * @return An image representing a marker.
  17. *
  18. * @see MarkerUtils#getMarkerForColor(Color, AlphaMode)
  19. */
  20. public static Image getMarkerForColor(Color outer, Color inner, AlphaMode mode) {
  21. // create image
  22. int[][] mask = readMarkerMask();
  23. BufferedImage image = new BufferedImage(mask[0].length, mask.length,
  24. BufferedImage.TYPE_4BYTE_ABGR);
  25. // Color[] map = getColorMapping(color);
  26. int outerRGB = outer.getRGB() & 0x00ffffff;
  27. int innerRGB = inner.getRGB() & 0x00ffffff;
  28. for (int i = 0; i < image.getHeight(); ++i) {
  29. for (int j = 0; j < image.getWidth(); ++j) {
  30. if (i >= MIN_Y_CENTER && i < MAX_Y_CENTER && j >= MIN_X_CENTER
  31. && j < MAX_X_CENTER) {
  32. image.setRGB(j, i, innerRGB
  33. | ((mode == AlphaMode.OPAQUE ? MAXIMUM_MAX_VALUE : mask[i][j]) << 24));
  34. }
  35. else {
  36. image.setRGB(j, i, outerRGB | (mask[i][j] << 24));
  37. }
  38. }
  39. }
  40. return image;
  41. }
  42. // Mask cache
  43. private static int[][] MASK_CACHE = null;
  44. // Hand-made... These defines the "center" of the marker, that can be filled
  45. // with a different color.
  46. private static final int MIN_X_CENTER = 40, MAX_X_CENTER = 101, MIN_Y_CENTER = 40,
  47. MAX_Y_CENTER = 100;
  48. private static final int MAXIMUM_MAX_VALUE = 249;
  49. /**
  50. * @return Retrieve the mask from the mask file or from the cache.
  51. */
  52. private static int[][] readMarkerMask() {
  53. if (MASK_CACHE == null) {
  54. try {
  55. DataInputStream dis = new DataInputStream(
  56. MarkerUtils.class.getResourceAsStream("/marker_mask.bin"));
  57. int nrows = dis.readInt();
  58. int ncols = dis.readInt();
  59. MASK_CACHE = new int[nrows][ncols];
  60. for (int i = 0; i < nrows; ++i) {
  61. for (int j = 0; j < ncols; ++j) {
  62. MASK_CACHE[i][j] = dis.readUnsignedByte();
  63. }
  64. }
  65. dis.close();
  66. }
  67. catch (Exception e) {
  68. e.printStackTrace();
  69. MASK_CACHE = null;
  70. }
  71. }
  72. return MASK_CACHE;
  73. }
  74. }