-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageSplitter.java
251 lines (180 loc) · 7.28 KB
/
ImageSplitter.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
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
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.commons.io.FilenameUtils;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.PNGEncodeParam; //@Todo more encoders and user choice
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam; //@Todo more docedrs and user choice
import com.sun.media.jai.codecimpl.TIFFImage;
public class ImageSplitter extends JPanel implements ActionListener {
private static final long serialVersionUID = -2685655382409831974L;
JFileChooser chooser;
String choosertitle;
JButton go;
JFrame frame;
public static void main(String[] foo) {
new ImageSplitter();
}
private void marchThroughImages(List<String> fileName, String directoryName, List<String> o_names)
throws IOException {
Raster raster[] = new Raster[fileName.size()];
String message = "";
WritableRaster wRaster_lower8 = null;
WritableRaster wRaster_upper8 = null;
ColorModel cm = null;
//byte[] map = new byte[] {(byte)0x00, (byte)0xff};
// ColorModel cm = new IndexColorModel(1, 2, map, map, map);
final BufferedImage png_i = ImageIO.read(new File(directoryName + "\\cm.png"));
cm = png_i.getColorModel();
//wRaster = image.getData().createCompatibleWritableRaster();
wRaster_lower8 = png_i.getData().createCompatibleWritableRaster(); //@TODO don't read it from file (build in program and user choice)
wRaster_upper8 = png_i.getData().createCompatibleWritableRaster(); //@TODO don't read it from file (build in program and user choice)
int totalFiles = fileName.size();
for (int i = 0; i < totalFiles; i++) {
File file = new File(fileName.get(i));
SeekableStream s = new FileSeekableStream(file);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
raster[i] = dec.decodeAsRaster();
TIFFImage image = (TIFFImage) dec.decodeAsRenderedImage();
message += "Images Processed " + fileName.get(i) + " width: "
+ raster[0].getWidth() + " height: "
+ raster[0].getHeight() + " Pixel Size: "
+ image.getColorModel().getPixelSize() + "\n";
s.close();
}
PNGEncodeParam encParam = null;
File file_o = null;
FileOutputStream fileoutput = null;
ImageEncoder enc = null;
int w = raster[0].getWidth(), h = raster[0].getHeight();
System.out.println("Oto kurwa : " + w + " " + h);
int lower8Pixel[][][] = new int[w][h][3]; //change 3 to 4 for png with alpha
int upper8Pixel[][][] = new int[w][h][3]; //change 3 to 4 for png with alpha
for (int i = 0; i < totalFiles; i++) {
for (int width = 0; width < w; width++) {
for (int height = 0; height < h; height++) {
int[] pixelA = null;
pixelA = raster[i].getPixel(width, height, pixelA);
if(width == 20 && height == 31) System.out.println("oto pixelA : " + Integer.toBinaryString(pixelA[0])); //for checking @TODO write 00.es from left to make 16 size
lower8Pixel[width][height][0] = pixelA[0] % 256;
lower8Pixel[width][height][1] = pixelA[1] % 256;
lower8Pixel[width][height][2] = pixelA[2] % 256;
//lower8Pixel[width][height][3] = 255; //if alpha
if(width == 20 && height == 31) System.out.println("oto lowerA : " + Integer.toBinaryString(lower8Pixel[width][height][0])); //for checking @TODO write 00.es from left to make 8 size
upper8Pixel[width][height][0] = (pixelA[0] - lower8Pixel[width][height][0])/256;
upper8Pixel[width][height][1] = (pixelA[1] - lower8Pixel[width][height][1])/256;
upper8Pixel[width][height][2] = (pixelA[2] - lower8Pixel[width][height][2])/256;
//upper8Pixel[width][height][3] = 255; //if alpha
if(width == 20 && height == 31) System.out.println("oto upperA : " + Integer.toBinaryString(upper8Pixel[width][height][0])); //for checking @TODO write 00.es from left to make 8 size
wRaster_lower8.setPixel(width, height,
lower8Pixel[width][height]);
wRaster_upper8.setPixel(width, height,
upper8Pixel[width][height]);
}
}
file_o = new File(directoryName + "\\lower_"
+ o_names.get(i) + ".png");
fileoutput = new FileOutputStream(file_o);
encParam = null;
enc = ImageCodec.createImageEncoder("png", fileoutput,
encParam);
enc.encode(wRaster_lower8, cm);
fileoutput.close();
file_o = new File(directoryName + "\\upper_"
+ o_names.get(i) + ".png");
fileoutput = new FileOutputStream(file_o);
encParam = null;
enc = ImageCodec.createImageEncoder("png", fileoutput,
encParam);
enc.encode(wRaster_upper8, cm);
fileoutput.close();
}
System.out.println(message);
}
public ImageSplitter() {
frame = new JFrame("TIFF Image Calibrator");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(this, "Center");
frame.setSize(this.getPreferredSize());
frame.setVisible(true);
go = new JButton("Select Folder");
go.addActionListener(this);
add(go);
}
@Override
public void actionPerformed(ActionEvent e) {
go.setEnabled(false);
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File(
"."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
String directoryName = "";
//
// disable the "All files" option.
//
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
List<String> results = new ArrayList<String>();
List<String> onlynames = new ArrayList<String>();
File[] files = null;
try {
directoryName = chooser.getSelectedFile().getCanonicalPath();
files = new File(directoryName).listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.toLowerCase().endsWith("tif")
|| name.toLowerCase().endsWith("tiff"))
return true;
else
return false;
}
});
for (File file : files) {
if (file.isFile()) {
System.out.println(file.getCanonicalPath());
results.add(file.getCanonicalPath());
onlynames.add(FilenameUtils.removeExtension(file.getName()));
//onlynames.add(FilenameUtils.getBaseName(file)); //not a string
}
}
marchThroughImages(results, directoryName, onlynames);
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
System.out
.println("No Selection.Please Select the Folder containing TIFF Images.");
}
go.setEnabled(true);
}
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}