-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay2DArray.java
More file actions
269 lines (261 loc) · 11.2 KB
/
Copy pathDisplay2DArray.java
File metadata and controls
269 lines (261 loc) · 11.2 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Class: Display2DArray
//
// Description: Implements the paint() routine using one or more 2D arrays containing the
// image (one for grayscale or binary images, 3 for color images). Assumes
// that the image is oriented correctly (i.e,. paint() does not flip it).
// Allows for showing an image one time or continuously (for example, if it
// is being constantly updated by a digital camera.
// Provides routines for referencing or copying image data. The referencing
// options provide for potentially faster processing but at the expense of
// creating aliases. The copy options are slower but safer.
// Also includes the capability to show text on the image.
//
// Author: Steve Donaldson
// Date: 08/29/2008
import java.awt.*;
import java.awt.event.*;
public class Display2DArray extends Frame {
public int imageType; //1=24 bit color;2=256 level gray scale;3=binary (b/w)
public int imageWidth; //image width in pixels
public int imageHeight; //image height in pixels
public int imageSize; //height*width (*3 for images of type 1)
public int pixels[][]; //pixel values in 2D for type 1 or type 2 images
public int redPixels[][]; //red pixel values for type 1 images
public int greenPixels[][]; //green pixel values for type 1 images
public int bluePixels[][]; //blue pixel values for type 1 images
public boolean showOnce; //true=show the image one time; false=continuously
//redraw the image. The "false" option is useful for
//situations such as those when the image pixels are
//constantly changing in the source file, such as
//may occur when the file is being constantly
//updated by a digital camera. The choice to
//continuously redraw only has effect if the
//sourceImage parameter is not null.
ImageClass sourceImage; //if the image to be displayed is based on an object
//of type ImageClass, this is that object (null
//otherwise)
public static int windowHeaderOffset = 30; //space for window title bar
public static int windowSideOffset = 4; //space for window side bar(s)
public static int windowBotttomOffset = 4; //space for window bottom bar
public int textLineCount; //the number of lines of text in array text[]
public String text[]; //each row contains a line of text to be displayed
public int textPosition[][]; //the row and column positions in the image at which
//to display the text
//**************************************************************************************
Display2DArray(int type, int width, int height, int size, int redValues[][], int greenValues[][], int blueValues[][], int values[][], ImageClass source) {
imageType = type;
imageWidth = width;
imageHeight = height;
imageSize = size;
redPixels=redValues;
greenPixels = greenValues;
bluePixels = blueValues;
pixels = values;
sourceImage = source;
showOnce = true;
textLineCount = 0;
text = null;
textPosition = null;
}
//**************************************************************************************
Display2DArray(ImageClass imageObject) {
imageType = imageObject.imageType;
imageWidth = imageObject.imageWidth;
imageHeight = imageObject.imageHeight;
imageSize = imageObject.imageSize;
redPixels = imageObject.redPixels;
greenPixels = imageObject.greenPixels;
bluePixels = imageObject.bluePixels;
pixels = imageObject.pixels;
sourceImage = imageObject;
showOnce = true;
textLineCount = 0;
text = null;
textPosition = null;
}
//**************************************************************************************
Display2DArray(Display2DArray displayObject) {
imageType = displayObject.imageType;
imageWidth = displayObject.imageWidth;
imageHeight = displayObject.imageHeight;
imageSize = displayObject.imageSize;
redPixels = displayObject.redPixels;
greenPixels = displayObject.greenPixels;
bluePixels = displayObject.bluePixels;
pixels = displayObject.pixels;
sourceImage = displayObject.sourceImage;
showOnce = true;
}
//**************************************************************************************
//Method: referenceColorArrayData
//Description: Makes the color arrays for this image reference the RGB pixel values
// from a specified set of color arrays. Note that a change to the source
// arrays will be reflected in this image if subsequently displayed.
//Parameters: redValues[][] - source red pixels
// greenValues[][] - source green pixels
// blueValues[][] - source blue pixels
//Returns: true if successful; false otherwise
//Calls: nothing
public boolean referenceColorArrayData(int redValues[][], int greenValues[][], int blueValues[][]) {
if ((redValues != null) && (greenValues != null) && (blueValues != null)) {
redPixels=redValues;
greenPixels = greenValues;
bluePixels = blueValues;
return true;
}
return false;
}
//**************************************************************************************
//Method: referenceGrayArrayData
//Description: Makes the grayscale array for this image reference the gray pixel values
// from a specified grayscale array. Note that a change to the source
// array will be reflected in this image if subsequently displayed.
//Parameters: values[][] - source gray pixels
//Returns: true if successful; false otherwise
//Calls: nothing
public boolean referenceGrayArrayData(int values[][]) {
if (values != null) {
pixels = values;
return true;
}
return false;
}
//**************************************************************************************
//Method: copyColorArrayData
//Description: Copies RGB pixel values from a specified set of arrays into the arrays
// for this image. Making a copy prevents a change to the source arrays
// from subsequently affecting this image.
//Parameters: redValues[][] - source red pixels
// greenValues[][] - source green pixels
// blueValues[][] - source blue pixels
//Returns: true if successful; false otherwise
//Calls: nothing
public boolean copyColorArrayData(int redValues[][], int greenValues[][], int blueValues[][]) {
if ((redValues != null) && (greenValues != null) && (blueValues != null)) {
redPixels = new int[imageHeight][imageWidth];
greenPixels = new int[imageHeight][imageWidth];
bluePixels = new int[imageHeight][imageWidth];
for (int r = 0; r < imageHeight; r++)
for (int c = 0; c < imageWidth; c++) {
redPixels[r][c] = redValues[r][c];
greenPixels[r][c] = greenValues[r][c];
bluePixels[r][c] = blueValues[r][c];
}
return true;
}
return false;
}
//**************************************************************************************
//Method: copyGrayArrayData
//Description: Copies grayscale pixel values from a specified array into the array
// for this image. Making a copy prevents a change to the source array
// from subsequently affecting this image.
//Parameters: values[][] - source gray pixels
//Returns: true if successful; false otherwise
//Calls: nothing
public boolean copyGrayArrayData(int values[][]) {
if (values != null) {
pixels = new int[imageHeight][imageWidth];
for(int r=0;r<imageHeight;r++)
for(int c=0;c<imageWidth;c++)
pixels[r][c] = values[r][c];
return true;
}
return false;
}
//**************************************************************************************
//Method: showImage
//Description: Initializes graphics window parameters and displays its contents.
//Parameters: title - title of the graphics window
// displayOnce - show the image a single time or display it continuously
// (i.e., as for continuous digital camera input)
//Returns: nothing
//Calls: various Java graphics routines
public void showImage(String title, boolean displayOnce) {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
showOnce = displayOnce;
setTitle(title);
setSize(imageWidth + 2 * windowSideOffset, imageHeight + windowHeaderOffset + windowBotttomOffset);
setVisible(true);
}
//**************************************************************************************
//Method: closeImageDisplay
//Description: Terminates continuous display (if applicable) and closes the graphics
// window.
//Parameters: none
//Returns: nothing
//Calls: Java setVisible
public void closeImageDisplay() {
showOnce = true; //exit endless loop in paint()
setVisible(false);
}
//**************************************************************************************
//Method: paint
//Description: Display an image stored in a 2D array. Overrides the paint method
// inherited from Frame (via Container). Allows for showing an image
// one time or continuously (for example, if it is being constantly
// updated by a digital camera.
//Parameters: g - the graphics object
//Returns: nothing
//Calls: setColorValues for an ImageClass object
// setGrayValues for an ImageClass object
// referenceColorArrayData
// referenceGrayArrayData
// plus various Java graphics routines
public void paint(Graphics g) {
int row, column, pixel;
Color color = new Color(0);
int i, a = 0, b = 0;
while (a == b) {
if (imageType == 1) {
for (row = 0; row < imageHeight; row++) {
for (column = 0; column < imageWidth; column++) {
color = new Color(redPixels[row][column], greenPixels[row][column], bluePixels[row][column]);
g.setColor(color);
g.drawLine(column + windowSideOffset, row + windowHeaderOffset, column + windowSideOffset, row + windowHeaderOffset);
}
}
}
else if ((imageType == 2) || (imageType == 3)) {
for (row = 0; row < imageHeight; row++) {
for (column = 0; column < imageWidth; column++) {
pixel = pixels[row][column];
color = new Color(pixel, pixel, pixel);
g.setColor(color);
g.drawLine(column + windowSideOffset, row + windowHeaderOffset, column + windowSideOffset, row + windowHeaderOffset);
}
}
}
g.setColor(Color.white);
Font f = new Font("sansserif", Font.BOLD, 12);
g.setFont(f);
for (i = 0; i < textLineCount; i++)
g.drawString(text[i], textPosition[i][1] + windowSideOffset, textPosition[i][0] + windowHeaderOffset);
if (showOnce)
b++; //exit
else {
if (sourceImage != null) {
sourceImage.getRawPixelData();
if (imageType == 1) {
sourceImage.setColorValues();
referenceColorArrayData(sourceImage.redPixels, sourceImage.greenPixels, sourceImage.bluePixels);
}
else {
sourceImage.setGrayValues();
referenceGrayArrayData(sourceImage.pixels);
}
}
//else //removed to allow for continuous display based on updates to this object other than from
// b++; //an ImageClass object
}
}
}
//end paint()
//**************************************************************************************
} //end Display2DArray class
//******************************************************************************************
//******************************************************************************************