-
Notifications
You must be signed in to change notification settings - Fork 0
/
LcdImage.java
179 lines (163 loc) · 5.4 KB
/
LcdImage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @author Clément Petit (282626)
* @author Yanis Berkani (271348)
*/
package ch.epfl.gameboj.component.lcd;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import static ch.epfl.gameboj.Preconditions.checkArgument;
/**
* represents a GameBoy image.
*/
public final class LcdImage {
private final int width, height;
private final List<LcdImageLine> lineList;
/**
* initializes the width and the height of the image and the list of its
* lines.
*
* @param width
* the image width (must be equal to the size of each line
* in the list, although this condition is not checked for a cost reason)
* @param height
* the image height (must be equal to the size of the image line
* list)
* @param lineList
* the list of the image lines (must not be empty)
* @throws IllegalArgumentException
* if lineList is empty or if the height or the width are invalid
*/
public LcdImage(int width, int height, List<LcdImageLine> lineList) {
checkArgument(width > 0 & height > 0);
checkArgument(!(lineList.isEmpty()) && (height == lineList.size()));
this.width = width;
this.height = height;
this.lineList = Collections.unmodifiableList(new ArrayList<>(lineList));
}
/**
* returns the width of the image.
*
* @return the image width
*/
public int width() {
return width;
}
/**
* returns the height of the image.
*
* @return the image height.
*/
public int height() {
return height;
}
/**
* gets, in the form of an integer included between 0 and 3, the color of
* the pixel of given index (x,y).
*
* @param x
* the pixel abscissa (must be positive)
* @param y
* the pixel ordinate (must be positive and strictly inferior to
* the height of this image)
* @throws IllegalArgumentException
* if x or y is invalid
* @return an integer included between 0 and 3 that represents the color of
* the pixel of given index (x,y)
*/
public int get(int x, int y) {
checkArgument(x >= 0 && y >= 0 && y < lineList.size());
boolean msbColor = lineList.get(y).msb().testBit(x);
boolean lsbColor = lineList.get(y).lsb().testBit(x);
if (msbColor) {
if (lsbColor) {
return 3;
} else {
return 2;
}
} else {
if (lsbColor) {
return 1;
} else {
return 0;
}
}
}
/**
* checks if the given object is an LcdImage and if its line list is
* equal to this image's line list.
*
* @param that
* the object
* @return true if the given object is an LcdImage and if its line
* list is equal to this image's line list, and false otherwise
*/
public boolean equals(Object that) {
if (!(that instanceof LcdImage)) {
return false;
}
return this.lineList.equals(((LcdImage) that).lineList);
}
/**
* returns the hashcode of the image's line list.
*
* @return the hashcode of the image's line list
*/
public int hashcode() {
return Objects.hash(lineList);
}
/**
* represents an LcdImage builder.
*/
public final static class Builder {
private int width, height;
private List<LcdImageLine> lineList;
/**
* initializes the image builder with the given width and height
* and all its pixels with the color 0.
*
* @param width
* the image width (must be positive)
* @param height
* the image height (must be positive)
* @throws IllegalArgumentException
* if the width or the height is invalid
*/
public Builder(int width, int height) {
checkArgument(width >= 0 && height >= 0);
this.width = width;
this.height = height;
lineList = new ArrayList<>(Collections.nCopies(height,
new LcdImageLine.Builder(width).build()));
}
/**
* sets the line of given index to the given value.
*
* @param index
* the index (must be positive and strictly inferior to the
* size of the image line list)
* @param newValue
* the new ImageLine's value (must have a size equal to the builder's width)
* @throws IndexOutOfBoundsException
* if the index is invalid
* @throws IllegalArgumentException
* if the newValue's size is invalid
* @return the builder
*/
public Builder setLine(int index, LcdImageLine newValue) {
Objects.checkIndex(index, lineList.size());
checkArgument(newValue.size() == width);
lineList.set(index, newValue);
return this;
}
/**
* returns the image built from that builder.
*
* @return the built image
*/
public LcdImage build() {
return new LcdImage(width, height, lineList);
}
}
}