-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyImageWriter.java
More file actions
120 lines (102 loc) · 3.83 KB
/
MyImageWriter.java
File metadata and controls
120 lines (102 loc) · 3.83 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
/*
* --------------------------------------------------------------------------
* CS111 - Digital Image Processing - Spring '10
* Instructor - Prof Aditi Majumder
* TA - Uddipan Mukherjee - umukherj@uci.edu
* --------------------------------------------------------------------------
This is a class for writing a sample image file
The image data is in a 3D array - a 2D array for each color channel
The filename of the image and the output filename are passed as strings
*/
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.image.*;
public class MyImageWriter
{
public static boolean writeImage2(String outputFileName, int[][][] imageData)
{
BufferedImage outputImage = new BufferedImage( imageData[0].length, imageData[0][0].length, BufferedImage.TYPE_BYTE_GRAY );
WritableRaster outputRaster;
outputRaster = outputImage.getRaster().createCompatibleWritableRaster();
// GRAY
// when writing color files, we will have to handle multible bands
int[] pixelData = new int[ outputRaster.getNumBands() ];
int band = 0;
int numbands = outputRaster.getNumBands();
int height, width;
height = outputRaster.getHeight();
width = outputRaster.getWidth();
for ( int y = 0; y < height; y++ )
for ( int x = 0; x < width; x++ )
{
for ( band = 0; band < numbands; band++ )
{
pixelData[ band ] = imageData[band][y][x];
}
outputRaster.setPixel(x, y, pixelData );
}
outputImage.setData( outputRaster );
File outputFile = new File( outputFileName );
try
{
if ( !ImageIO.write( outputImage, "jpg", outputFile ))
{
System.out.println("Could not find image format for output image.");
return false;
}
}
catch ( Exception e )
{
System.out.println("Could not write output file.");
return false;
}
return true;
}
public static boolean writeImage(String inputFileName, String outputFileName, int[][][] imageData)
{
BufferedImage inputImage = MyImageReader.readImageIntoBufferedImage( inputFileName );
if ( inputImage == null )
{
System.out.println(" Could not open input image.");
return false;
}
BufferedImage outputImage = new BufferedImage( inputImage.getWidth(), inputImage.getHeight(),
inputImage.getType() );
WritableRaster outputRaster, inputRaster;
inputRaster = inputImage.getRaster();
outputRaster = inputRaster.createCompatibleWritableRaster();
// GRAY
// when writing color files, we will have to handle multible bands
int[] pixelData = new int[ outputRaster.getNumBands() ];
int band = 0;
int numbands = outputRaster.getNumBands();
int height, width;
height = outputRaster.getHeight();
width = outputRaster.getWidth();
for ( int y = 0; y < height; y++ )
for ( int x = 0; x < width; x++ )
{
for ( band = 0; band < numbands; band++ )
{
pixelData[ band ] = imageData[band][y][x];
}
outputRaster.setPixel(x, y, pixelData );
}
outputImage.setData( outputRaster );
File outputFile = new File( outputFileName );
try
{
if ( !ImageIO.write( outputImage, "jpg", outputFile ))
{
System.out.println("Could not find image format for output image.");
return false;
}
}
catch ( Exception e )
{
System.out.println("Could not write output file.");
return false;
}
return true;
}
}