File: GrayImageRow.java

package info (click to toggle)
libpj-java 0.0~20150107%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,396 kB
  • sloc: java: 99,543; ansic: 987; sh: 153; xml: 26; makefile: 10; sed: 4
file content (294 lines) | stat: -rw-r--r-- 9,526 bytes parent folder | download | duplicates (2)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//******************************************************************************
//
// File:    GrayImageRow.java
// Package: edu.rit.image
// Unit:    Class edu.rit.image.GrayImageRow
//
// This Java source file is copyright (C) 2007 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************

package edu.rit.image;

import java.util.Arrays;

/**
 * Class GrayImageRow provides one row of a grayscale image. The image row is
 * layered on top of a byte array (type <TT>byte[]</TT>). Use the
 * <TT>setArray()</TT> method to specify which array this is. The width of the
 * image row is equal to the number of elements in the underlying array. The
 * image row's underlying array usually is (but does not have to be) one row of
 * an image's underlying matrix.
 * <P>
 * To read and write the pixels of an image row, use the <TT>getIntPixel()</TT>,
 * <TT>getPixel()</TT>, <TT>setIntPixel()</TT>, and <TT>setPixel()</TT> methods.
 * These methods represent a pixel value as an integer in the range 0 .. 255 or
 * as a floating point number in the range 0.0 .. 1.0. The image row's
 * <I>interpretation</I> attribute specifies how to map between the numerical
 * pixel value and the shade of gray. If the interpretation is
 * <TT>ZERO_IS_WHITE</TT>, then a value of 0 or 0.0 represents white and 255 or
 * 1.0 represents black. If the interpretation is <TT>ZERO_IS_BLACK</TT>, then a
 * value of 0 or 0.0 represents black and 255 or 1.0 represents white. The
 * default interpretation is <TT>ZERO_IS_BLACK</TT>. To change the
 * interpretation, call the <TT>setInterpretation()</TT> method.
 * <P>
 * Changing the contents of the underlying matrix directly will also change the
 * image row. In the underlying matrix, a value of 0 represents black and a
 * value of 255 represents white.
 * 
 * @author  Alan Kaminsky
 * @version 11-Nov-2007
 */
public class GrayImageRow
	{

// Exported enumerations.

	/**
	 * A pixel value of 0 or 0.0 is white, 255 or 1.0 is black.
	 */
	public static final PJGGrayImage.Interpretation ZERO_IS_WHITE =
		PJGGrayImage.Interpretation.ZERO_IS_WHITE;

	/**
	 * A pixel value of 0 or 0.0 is black, 255 or 1.0 is white.
	 */
	public static final PJGGrayImage.Interpretation ZERO_IS_BLACK =
		PJGGrayImage.Interpretation.ZERO_IS_BLACK;

// Hidden data members.

	PJGGrayImage.Transformation myTransformation =
		PJGGrayImage.ZERO_IS_BLACK_TRANSFORMATION;
	byte[] myArray;

// Exported constructors.

	/**
	 * Construct a new grayscale image row on top of the given array. The image
	 * row's interpretation is {@link #ZERO_IS_BLACK}.
	 *
	 * @param  theArray  Underlying array.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if <TT>theArray</TT> is null.
	 */
	public GrayImageRow
		(byte[] theArray)
		{
		setArray (theArray);
		}

// Exported operations.

	/**
	 * Obtain this image row's underlying array.
	 *
	 * @return  Underlying array.
	 */
	public byte[] getArray()
		{
		return myArray;
		}

	/**
	 * Set this image row's underlying array.
	 *
	 * @param  theArray  Underlying array.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if <TT>theArray</TT> is null.
	 */
	public void setArray
		(byte[] theArray)
		{
		if (theArray == null)
			{
			throw new NullPointerException
				("GrayImageRow.setArray(): theArray is null");
			}
		myArray = theArray;
		}

	/**
	 * Get this image row's interpretation.
	 *
	 * @return  {@link #ZERO_IS_WHITE} or {@link #ZERO_IS_BLACK}.
	 */
	public PJGGrayImage.Interpretation getInterpretation()
		{
		if (myTransformation == PJGGrayImage.ZERO_IS_WHITE_TRANSFORMATION)
			{
			return ZERO_IS_WHITE;
			}
		else
			{
			return ZERO_IS_BLACK;
			}
		}

	/**
	 * Set this image row's interpretation. If this method is not called, this
	 * image row's default interpretation is {@link #ZERO_IS_BLACK}.
	 *
	 * @param  theInterpretation  {@link #ZERO_IS_WHITE} or
	 *                            {@link #ZERO_IS_BLACK}.
	 */
	public void setInterpretation
		(PJGGrayImage.Interpretation theInterpretation)
		{
		switch (theInterpretation)
			{
			case ZERO_IS_WHITE:
				myTransformation = PJGGrayImage.ZERO_IS_WHITE_TRANSFORMATION;
				break;
			case ZERO_IS_BLACK:
				myTransformation = PJGGrayImage.ZERO_IS_BLACK_TRANSFORMATION;
				break;
			}
		}

	/**
	 * Obtain the pixel at the given column in this image row.
	 *
	 * @param  c  Column index.
	 *
	 * @return  Pixel[<I>c</I>] as an integer in the range 0 .. 255.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if the underlying matrix row has not
	 *     been specified.
	 * @exception  ArrayIndexOutOfBoundsException
	 *     (unchecked exception) Thrown if <TT>c</TT> is out of bounds.
	 */
	public int getIntPixel
		(int c)
		{
		return myTransformation.inverseTransformInt (myArray[c]);
		}

	/**
	 * Obtain the pixel at the given column in this image row.
	 *
	 * @param  c  Column index.
	 *
	 * @return  Pixel[<I>c</I>] as a floating point number in the range 0.0 ..
	 *          1.0.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if the underlying matrix row has not
	 *     been specified.
	 * @exception  ArrayIndexOutOfBoundsException
	 *     (unchecked exception) Thrown if <TT>c</TT> is out of bounds.
	 */
	public float getPixel
		(int c)
		{
		return myTransformation.inverseTransformFloat (myArray[c]);
		}

	/**
	 * Set the pixel at the given column in this image row. If <TT>val</TT> is
	 * outside the range 0 .. 255, it is pinned to the appropriate boundary.
	 *
	 * @param  c    Column index.
	 * @param  val  New pixel value as an integer in the range 0 .. 255.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if the underlying matrix row has not
	 *     been specified.
	 * @exception  ArrayIndexOutOfBoundsException
	 *     (unchecked exception) Thrown if <TT>c</TT> is out of bounds.
	 */
	public void setIntPixel
		(int c,
		 int val)
		{
		myArray[c] = myTransformation.transformInt (val);
		}

	/**
	 * Set the pixel at the given column in this image row. If <TT>val</TT> is
	 * outside the range 0.0 .. 1.0, it is pinned to the appropriate boundary.
	 *
	 * @param  c    Column index.
	 * @param  val  New pixel value as a floating point number in the range 0.0
	 *              .. 1.0.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if the underlying matrix row has not
	 *     been specified.
	 * @exception  ArrayIndexOutOfBoundsException
	 *     (unchecked exception) Thrown if <TT>c</TT> is out of bounds.
	 */
	public void setPixel
		(int c,
		 float val)
		{
		myArray[c] = myTransformation.transformFloat (val);
		}

	/**
	 * Set all pixels in this image row to the given value. If <TT>val</TT> is
	 * outside the range 0 .. 255, it is pinned to the appropriate boundary.
	 *
	 * @param  val  New pixel value as an integer in the range 0 .. 255.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if the underlying matrix row has not
	 *     been specified.
	 */
	public void fill
		(int val)
		{
		Arrays.fill (myArray, myTransformation.transformInt (val));
		}

	/**
	 * Set all pixels in this image row to the given value. If <TT>val</TT> is
	 * outside the range 0.0 .. 1.0, it is pinned to the appropriate boundary.
	 *
	 * @param  val  New pixel value as a floating point number in the range 0.0
	 *              .. 1.0.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if the underlying matrix row has not
	 *     been specified.
	 */
	public void fill
		(float val)
		{
		Arrays.fill (myArray, myTransformation.transformFloat (val));
		}

	}