-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyImageReader.java
More file actions
109 lines (91 loc) · 3.5 KB
/
MyImageReader.java
File metadata and controls
109 lines (91 loc) · 3.5 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
/*
* --------------------------------------------------------------------------
* CS111 - Digital Image Processing - Spring '10
* Instructor - Prof Aditi Majumder
* TA - Uddipan Mukherjee - umukherj@uci.edu
* --------------------------------------------------------------------------
This is a class for reading a sample image file
The image should be in .jpg format
The filename of the image is passed as a string
*/
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.image.*;
public class MyImageReader
{
public static BufferedImage readImageIntoBufferedImage(String fileName)
{
BufferedImage image = null;
//Check that the file name indicates we have a jpg file
if ( !fileName.endsWith(".jpg") )
{
System.out.println("This is not a jpg file.");
return null;
}
try {
// Read from a file
File file = new File(fileName);
image = ImageIO.read(file);
} catch (IOException e) {
System.out.println("Could not open file.");
return null;
}
return image;
}
/* This method gives the number of bands (number of color channels) in for an input image */
public static int numchannels(String fileName)
{
BufferedImage image = null;
image = readImageIntoBufferedImage(fileName);
return image.getRaster().getNumBands();
}
/* This method opens the jpg mage file given by the input string, and decodes the jpg,
* putting the relevant values into a two dimensional array of ints. For the time being,
* we are only dealing with grayscale. However, in the future, we will need to specify
* on which "band" we are doing the constrast enhancement.
* If any of the values are too large to fit in a int, the method returns null.
*/
public static int[][][] readImageInto2DArray(String fileName)
{
BufferedImage image = null;
int height, width, band, numbands;
image = readImageIntoBufferedImage( fileName );
WritableRaster raster = image.getRaster();
//GRAY: For now, the number of bands should be 1.
int[] pixelValues = new int[ raster.getNumBands() ];
height = raster.getHeight();
width = raster.getWidth();
//ADITI: Accomodates color image
numbands = raster.getNumBands();
// GRAY: This may be specified differently for color images
//ADITI: This is not needed any longer
//band = 0;
//allocate our two dimensional array.
//ADITI: Changed to accomodate color. It is better to have the channel as the first
//dimension, since it can allow us to work with each channel independently.
int rasterValues[][][];
rasterValues = new int[numbands][][];
for (band=0; band < numbands; band++)
{
rasterValues[band] = new int[height][];
for ( int y = 0; y < rasterValues[band].length; y++ )
rasterValues[band][y] = new int[ width ];
}
for ( int x = 0; x < width; x++ )
for( int y = 0; y <height; y++ )
{
pixelValues = raster.getPixel( x, y, pixelValues);
for (band = 0; band < numbands; band++)
{
if ( pixelValues[band] > Integer.MAX_VALUE )
{
System.out.println(" Pixel value too big for int.");
return null;
}
else
rasterValues[band][y][x] = pixelValues[band];
}
}
return rasterValues;
}
}