01: /*
02: * Geotools2 - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002, Geotools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package org.geotools.renderer.shape;
18:
19: /**
20: *
21: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/shapefile-renderer/src/main/java/org/geotools/renderer/shape/ScreenMap.java $
22: */
23: public class ScreenMap {
24: int[] pixels;
25: int width;
26: int height;
27: private int minx;
28: private int miny;
29:
30: public ScreenMap() {
31: }
32:
33: public ScreenMap(int x, int y, int width, int height) {
34: this .width = width;
35: this .height = height;
36: this .minx = x;
37: this .miny = y;
38:
39: int arraySize = ((width * height) / 32) + 1;
40: pixels = new int[arraySize];
41: }
42:
43: /**
44: * Sets location at position x,y to the value.
45: */
46: public void set(int x, int y, boolean value) {
47: if ((x - minx) < 0 || (x - minx) > width - 1 || (y - miny) < 0
48: || (y - miny) > height - 1)
49: return;
50: int bit = bit(x - minx, y - miny);
51: int index = bit / 32;
52: int offset = bit % 32;
53: int mask = 1;
54: mask = mask << offset;
55:
56: if (value) {
57: pixels[index] = pixels[index] | mask;
58: } else {
59: int tmp = pixels[index];
60: tmp = ~tmp;
61: tmp = (tmp | mask);
62: tmp = ~tmp;
63: pixels[index] = tmp;
64: }
65: }
66:
67: /**
68: * Returns true if the pixel at location x,y is set or out of bounds.
69: */
70: public boolean get(int x, int y) {
71: if ((x - minx) < 0 || (x - minx) > width - 1 || (y - miny) < 0
72: || (y - miny) > height - 1)
73: return true;
74: int bit = bit(x - minx, y - miny);
75: int index = bit / 32;
76: int offset = bit % 32;
77: int mask = 1 << offset;
78:
79: try {
80: return ((pixels[index] & mask) != 0) ? true : false;
81: } catch (Exception e) {
82:
83: return true;
84: }
85: }
86:
87: private int bit(int x, int y) {
88: return (width * y) + x;
89: }
90: }
|