-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWaveletSampleImage.java
More file actions
768 lines (620 loc) · 20.8 KB
/
WaveletSampleImage.java
File metadata and controls
768 lines (620 loc) · 20.8 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
Convert between JPEG images and 2-d arrays of grayscale values.
Given a color image of any size:
1. Crop the long direction evenly on both ends to make the image square.
2. Resize to the next smaller power of 2.
3. Convert to grayscale.
4. Output as 2-d array of floats from 0 to 1.
Given a 2-d array of floats from 0 to 1:
1. Convert each value to a grayscale pixel.
2. Output as a JPEG.
Binary data format is described in data_io.h
Ed Karrels, June 2014
*/
import java.awt.Graphics;
import java.awt.color.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.nio.file.*;
import java.text.*;
import java.util.*;
import javax.imageio.*;
public class WaveletSampleImage {
private static final boolean VERBOSE_OUTPUT = false;
static class Matrix {
float[] data;
int width, height;
float get(int x, int y) {
return data[y*width + x];
}
}
interface MatrixToGrayscale {
int getRGB(float value);
}
static class MatrixToGrayscaleFlat implements MatrixToGrayscale {
private float pixelScale, pixelOffset;
public MatrixToGrayscaleFlat(float minValue, float maxValue) {
pixelOffset = -minValue;
pixelScale = 255.0f / (maxValue - minValue);
}
public int getRGB(float value) {
int color = (int)((value + pixelOffset) * pixelScale);
if (color > 255) color = 255;
if (color < 0) color = 0;
return color | (color << 8) | (color << 16);
}
}
static class MatrixToGrayscaleUniform implements MatrixToGrayscale {
float scale, offset;
MatrixToGrayscaleUniform(Matrix m) {
float min, max;
min = max = m.data[0];
for (int y=0; y < m.height; y++) {
for (int x=0; x < m.width; x++) {
float v = m.get(x, y);
min = Math.min(min, v);
max = Math.max(max, v);
}
}
offset = -min;
scale = 255 / (max-min);
}
public int getRGB(float value) {
int gray = (int) ((value + offset) * scale);
return gray | (gray << 8) | (gray << 16);
}
}
public static void main(String args[]) {
if (args.length < 1) printHelp();
boolean textOutput = false;
boolean doResize = true;
float minValue = -.5f, maxValue=.5f;
boolean slices = false;
List<String> inputFiles = null;
int argNo = 0;
while (argNo < args.length && args[argNo].charAt(0)=='-') {
if (args[argNo].equals("-text")) {
textOutput = true;
argNo++;
}
else if (args[argNo].equals("-noresize")) {
doResize = false;
argNo++;
}
else if (args[argNo].equals("-255")) {
minValue = 0;
maxValue = 255;
argNo++;
}
else if (args[argNo].equals("-min")) {
if (++argNo >= args.length) printHelp();
try {
minValue = Float.parseFloat(args[argNo]);
} catch (NumberFormatException e) {
System.out.println("Not a valid minimum value: " + args[argNo]);
return;
}
argNo++;
}
else if (args[argNo].equals("-max")) {
if (++argNo >= args.length) printHelp();
try {
maxValue = Float.parseFloat(args[argNo]);
} catch (NumberFormatException e) {
System.out.println("Not a valid maximum value: " + args[argNo]);
return;
}
argNo++;
}
else if (args[argNo].equals("-slices")) {
slices = true;
inputFiles = new ArrayList<String>();
argNo++;
}
else printHelp();
}
if (minValue >= maxValue) {
System.out.println("Output range is invalid: " + minValue +
" .. " + maxValue);
return;
}
// no input file listed
if (argNo >= args.length) printHelp();
String inputFile, outputFile = null;
// with slices, the destination file comes first, then source files
if (slices) {
outputFile = args[argNo++];
while (argNo < args.length) {
inputFiles.add(args[argNo++]);
}
imageSlicesToCubelets(outputFile, inputFiles, doResize);
return;
} else {
inputFile = args[argNo++];
if (argNo < args.length) outputFile = args[argNo++];
}
String suffix = filenameSuffix(inputFile).toLowerCase();
boolean sourceIsImage;
boolean destIsCubelet = false;
if (suffix.equals("jpg") || suffix.equals("jpeg") ||
suffix.equals("gif") || suffix.equals("png")) {
sourceIsImage = true;
} else if (suffix.equals("data") || suffix.equals("cube")) {
sourceIsImage = false;
} else {
System.err.println("Source file unrecognized. Should be .jpg, .jpeg, .gif, .png, .data, or .cube.");
return;
}
if (outputFile != null) {
String destExt = filenameSuffix(outputFile).toLowerCase();
if (sourceIsImage) {
if (destExt.equals("cube")) {
destIsCubelet = true;
} else if (!destExt.equals("data")) {
System.err.println("Output file must have .cube or .data extension.");
return;
}
} else {
if (!destExt.equals("jpg") && !destExt.equals("jpeg") &&
!destExt.equals("gif") && !destExt.equals("png")) {
System.err.println("Output file must have .jpg, .jpeg, .gif, or .png extension.");
return;
}
}
} else {
String destExt;
if (sourceIsImage) {
destExt = "cube";
destIsCubelet = true;
} else {
destExt = "jpg";
}
outputFile = inputFile.substring
(0, inputFile.length() - suffix.length()) + destExt;
}
if (!new File(inputFile).exists()) {
System.err.println("\"" + inputFile + "\" not found.");
return;
}
if (sourceIsImage) {
imageToMatrix(inputFile, outputFile, destIsCubelet, textOutput, doResize,
minValue, maxValue);
} else {
matrixToImage(inputFile, outputFile, minValue, maxValue);
}
}
public static void printHelp() {
System.out.println
("\n" +
" WaveletSampleImage [opts] <src> [<dest>]\n" +
" WaveletSampleImage [opts] -slices <dest> <src> [<src> ...]\n" +
" If src is a jpeg image, convert it to grayscale data, cropped and\n" +
" resized so the width and height are equal and a power of two.\n" +
" If src is data, convert the float data into a grayscale image\n" +
" Options:\n" +
" -text : output in text format rather than binary.\n" +
" -noresize : just turn to grayscale, no resizing\n" +
" -255 : pixel values will be in the range 0..255 rather than 0..1\n" +
" -min : minimum pixel value (-.5 by default)\n" +
" -max : maximum pixel value (+.5 by default)\n"
);
System.exit(0);
}
public static String filenameSuffix(String str) {
int dot = str.lastIndexOf(".");
if (dot == -1) return "";
return str.substring(dot+1);
}
/** Reads an image file. On error, returns null and prints error message. */
public static BufferedImage readImage(String filename) {
BufferedImage image;
try {
image = ImageIO.read(new File(filename));
} catch (IOException e) {
System.err.println("Error reading \"" + filename + "\": " + e);
return null;
}
if (image == null) {
String[] formats = ImageIO.getReaderFormatNames();
System.out.print(filename + " not one of ");
for (String s : formats) System.out.print(s + " ");
System.out.println();
return null;
}
return image;
}
// Resize image to a square power of two
public static BufferedImage resizeImage(BufferedImage image) {
// crop image to be square
if (VERBOSE_OUTPUT) {
System.out.println("Crop");
System.out.flush();
}
int size = image.getWidth();
if (image.getWidth() > image.getHeight()) {
size = image.getHeight();
int leftCrop = (image.getWidth() - image.getHeight()) / 2;
image = image.getSubimage(leftCrop, 0, image.getHeight(),
image.getHeight());
} else if (image.getHeight() > image.getWidth()) {
int topCrop = (image.getHeight() - image.getWidth()) / 2;
image = image.getSubimage(0, topCrop, image.getWidth(),
image.getWidth());
}
// rescale the image the to next smaller power of 2
if (VERBOSE_OUTPUT) {
System.out.println("Rescale");
System.out.flush();
}
int newSize = Integer.highestOneBit(size);
float scaleFactor = (float)newSize / size;
// System.out.printf("Rescale %d to %d: %f\n", size, newSize, scaleFactor);
if (newSize != size) {
AffineTransform rescaleXform = AffineTransform.getScaleInstance
(scaleFactor, scaleFactor);
AffineTransformOp rescaleOp = new AffineTransformOp
(rescaleXform, AffineTransformOp.TYPE_BICUBIC);
// must match the image type of the input image
BufferedImage newImage = new BufferedImage
(newSize, newSize, image.getType());
rescaleOp.filter(image, newImage);
image = newImage;
}
return image;
}
public static BufferedImage imageToGrayscale(BufferedImage image) {
// convert to grayscale
if (VERBOSE_OUTPUT) {
System.out.println("Convert to grayscale");
System.out.flush();
}
BufferedImage grayImage = new BufferedImage
(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics g = grayImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return grayImage;
}
public static byte[] imageToBytes(BufferedImage image) {
int h = image.getHeight(), w = image.getWidth();
byte[] bytes = new byte[h * w];
int pos = 0;
for (int y=0; y < h; y++) {
for (int x=0; x < w; x++) {
bytes[pos++] = (byte) (image.getRGB(x, y) & 0xff);
}
}
return bytes;
}
/* Reads a JPEG image, convert to grayscale text data. */
public static void imageToMatrix(String inputFile, String outputFile,
boolean isCubeletOutput,
boolean textOutput, boolean doResize,
float minValue, float maxValue) {
if (VERBOSE_OUTPUT) {
System.out.println("Read image");
System.out.flush();
}
BufferedImage image = readImage(inputFile);
if (image == null) return;
if (doResize)
image = resizeImage(image);
int outputWidth = image.getWidth();
int outputHeight = image.getHeight();
image = imageToGrayscale(image);
// enable this to output a grayscale jpg
/*
try {
ImageIO.write(image, "jpeg", new File("output.jpg"));
} catch (IOException e) {
System.err.println("Error saving image: " + e);
}
*/
if (VERBOSE_OUTPUT) {
System.out.println("Write data");
System.out.flush();
}
// for cubelet files use imageDataBytes, otherwise imageData
float[] imageData = null;
byte[] imageDataBytes = null;
if (isCubeletOutput) {
imageDataBytes = imageToBytes(image);
} else {
imageData = new float[outputWidth * outputHeight];
float colorScale = (maxValue - minValue) / 255.0f;
int pos = 0;
for (int y=0; y < outputHeight; y++) {
for (int x=0; x < outputWidth; x++) {
int colorInt = image.getRGB(x, y) & 0xff;
imageData[pos++] = colorInt * colorScale + minValue;
}
}
}
try {
long startNano = System.nanoTime();
writeMatrix(imageData, imageDataBytes, outputWidth, outputHeight,
outputFile, isCubeletOutput, !textOutput);
double elapsed = (System.nanoTime() - startNano) / 1e9;
System.out.printf("%d x %d grayscale data written to \"%s\" " +
"in %.3f sec\n",
image.getWidth(), image.getHeight(), outputFile,
elapsed);
} catch (IOException e) {
System.err.println("Error writing to \"" + outputFile + "\": " + e);
}
}
/* Read a text data file, convert to a grayscale JPEG. */
public static void matrixToImage(String inputFile, String outputFile,
float minValue, float maxValue) {
Matrix m = null;
try {
m = readMatrix(inputFile);
} catch (IOException e) {
System.err.println("Error reading \"" + inputFile + "\": " + e);
return;
}
// MatrixToGrayscale toGray = new MatrixToGrayscaleUniform(m);
MatrixToGrayscale toGray = new MatrixToGrayscaleFlat(minValue, maxValue);
// save the data as a grayscale image
BufferedImage image = new BufferedImage
(m.width, m.height, BufferedImage.TYPE_BYTE_GRAY);
int i = 0;
for (int y=0; y < m.height; y++) {
for (int x=0; x < m.width; x++) {
int color = toGray.getRGB(m.get(x, y));
image.setRGB(x, y, color);
}
}
try {
ImageIO.write(image, "jpeg", new File(outputFile));
} catch (IOException e) {
System.err.println("Error saving image: " + e);
}
System.out.printf("%d x %d image written to \"%s\".\n",
m.width, m.height, outputFile);
}
// use imageDataBytes if isCubeletOutput, otherwise imageData
public static void writeMatrix(float[] imageData, byte[] imageDataBytes,
int width, int height,
String outputFile, boolean isCubeletOutput,
boolean binaryFormat)
throws IOException {
if (isCubeletOutput) {
writeMatrixCubelet(imageDataBytes, width, height, outputFile);
} else {
if (binaryFormat) {
writeMatrixBinary(imageData, width, height, outputFile);
} else {
writeMatrixText(imageData, width, height, outputFile);
}
}
}
public static Matrix readMatrix(String inputFile) throws IOException {
if (isCubeletFile(inputFile)) {
return readMatrixCubelet(inputFile);
} else {
if (isBinaryFile(inputFile)) {
return readMatrixBinary(inputFile);
} else {
return readMatrixText(inputFile);
}
}
}
public static void writeMatrixText(float[] imageData, int width, int height,
String outputFile)
throws IOException {
DecimalFormat formatter = new DecimalFormat("0.00000");
BufferedWriter out = new BufferedWriter
(new FileWriter(outputFile));
out.write(width + " cols " + height + " rows\n");
int pos = 0;
for (int y=0; y < height; y++) {
for (int x=0; x < width; x++) {
if (x > 0) out.write(' ');
out.write(formatter.format(imageData[pos++]));
}
out.write('\n');
}
out.close();
}
// Write in little-endian order
private static void writeMatrixBinary(float[] imageData, int width,
int height, String outputFile)
throws IOException {
DataOutputStream out = new DataOutputStream
(new BufferedOutputStream(new FileOutputStream(outputFile)));
out.writeBytes("binary \n");
out.writeInt(reverseBytes(width));
out.writeInt(reverseBytes(height));
for (float f : imageData)
out.writeInt(reverseBytes(Float.floatToRawIntBits(f)));
out.close();
}
private static void writeMatrixCubelet(byte[] imageData, int width,
int height, String outputFile) {
try {
Cubelet cube = new Cubelet();
cube.setSize(width, height, 1);
cube.datatype = Cubelet.DataType.UINT8;
cube.byteData = imageData;
CubeletFile.Writer out = new CubeletFile.Writer();
out.open(outputFile);
out.addCubelet(cube);
out.close();
} catch (IOException e) {
System.err.println("Error writing cubelet file \"" + outputFile +
"\": " + e);
}
}
private static final int reverseBytes(final int x) {
return
((x & 0xff) << 24) |
((x & 0xff00) << 8) |
((x & 0xff0000) >>> 8) |
((x & 0xff000000) >>> 24);
}
public static boolean isBinaryFile(String filename) throws IOException {
return firstLineMatches(filename, "binary \n");
}
public static boolean isCubeletFile(String filename) throws IOException {
return firstLineMatches(filename, "SCU cubelets 1.0\n");
}
public static boolean firstLineMatches(String filename,
String expectedHeader) {
int lineLen = expectedHeader.length();
byte buf[] = new byte[lineLen];
FileInputStream in = null;
try {
in = new FileInputStream(filename);
in.read(buf);
in.close();
} catch (IOException e) {
return false;
}
String firstLine = new String(buf);
return firstLine.equals(expectedHeader);
}
private static Matrix readMatrixText(String inputFile) throws IOException {
Scanner in = new Scanner(new File(inputFile));
Matrix data = new Matrix();
if (!in.hasNextInt()) {
System.out.println("Bad header row");
return null;
}
data.width = in.nextInt();
if (!in.next().equals("cols")) {
System.out.println("Bad header row");
return null;
}
if (!in.hasNextInt()) {
System.out.println("Bad header row");
return null;
}
data.height = in.nextInt();
if (!in.next().equals("rows")) {
System.out.println("Bad header row");
return null;
}
// read the float data
int size = data.width * data.height;
data.data = new float[size];
for (int i=0; i < size; i++) {
if (!in.hasNextFloat()) {
System.out.printf("Error after %d values read.\n", i);
return null;
}
data.data[i] = in.nextFloat();
}
in.close();
return data;
}
private static Matrix readMatrixBinary(String inputFile) throws IOException {
Matrix data = new Matrix();
DataInputStream in = new DataInputStream
(new BufferedInputStream(new FileInputStream(inputFile)));
// skip over 8 bytes; should be "binary \n"
in.readLong();
data.width = reverseBytes(in.readInt());
data.height = reverseBytes(in.readInt());
if (VERBOSE_OUTPUT)
System.out.printf("binary size %d x %d\n", data.width, data.height);
int size = data.width * data.height;
data.data = new float[size];
for (int i=0; i < size; i++) {
int tmp = in.readInt();
data.data[i] = Float.intBitsToFloat(reverseBytes(tmp));
}
return data;
}
private static Matrix readMatrixCubelet(String inputFile) throws IOException {
Matrix data = new Matrix();
CubeletFile.Reader reader = new CubeletFile.Reader();
try {
reader.open(inputFile);
} catch (FileNotFoundException e) {
System.err.println("\"" + inputFile + "\" not found.");
return null;
} catch (Exception e) {
System.err.println("Error reading \"" + inputFile + "\": " + e);
return null;
}
Cubelet cube = new Cubelet();
while (true) {
if (!reader.next(cube)) {
System.err.println("No uncompressed cubelets found in \"" +
inputFile + "\".");
return null;
}
// fetch the first uncompressed cubelet
if (cube.compressionAlg == Cubelet.CompressionAlg.NONE) {
try {
reader.getData(cube);
} catch (Exception e) {
System.err.println("Error reading cubelet data: " + e);
return null;
}
break;
}
}
Matrix image = new Matrix();
image.width = cube.width;
image.height = cube.height;
int count = image.width * image.height;
image.data = new float[count];
if (cube.datatype == Cubelet.DataType.UINT8) {
for (int i=0; i < count; i++) {
image.data[i] = (cube.byteData[i] & 0xff) * (1.0f/255) - .5f;
}
} else {
for (int i=0; i < count; i++) {
image.data[i] = cube.floatData[i];
}
}
return image;
}
public static void imageSlicesToCubelets
(String outputFile, List<String> inputFiles, boolean doResize) {
if (new File(outputFile).exists()) {
System.out.println("Error: " + outputFile + " exists.");
return;
}
CubeletFile.Writer out = null;
try {
out = new CubeletFile.Writer();
out.open(outputFile);
} catch (IOException e) {
System.err.println("Error opening output cubelet file \"" + outputFile
+ "\": " + e);
return;
}
int z = -1;
for (String inputFile : inputFiles) {
z++;
BufferedImage image = readImage(inputFile);
if (image == null) break;
if (doResize) image = resizeImage(image);
image = imageToGrayscale(image);
byte[] imageData = imageToBytes(image);
Cubelet cube = new Cubelet();
cube.setSize(image.getWidth(), image.getHeight(), 1);
cube.setOffset(0, 0, z);
cube.datatype = Cubelet.DataType.UINT8;
cube.byteData = imageData;
try {
out.addCubelet(cube);
} catch (IOException e) {
System.err.println("Error adding cubelet " + cube + ": " + e);
break;
}
System.out.println("Added " + z + ": " + image.getWidth() + "x" +
image.getHeight() + ": " + inputFile);
System.out.flush();
}
try {
out.close();
} catch (IOException e) {
System.err.println("Error closing cubelet file: " + e);
}
}
}