Added a new blackbox test with extreme shadows and highlights. We do pretty poorly...
[zxing.git] / javase / src / com / google / zxing / client / j2se / ImageConverter.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.j2se;
18
19 import com.google.zxing.BlackPointEstimationMethod;
20 import com.google.zxing.MonochromeBitmapSource;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.common.BitArray;
23
24 import java.awt.image.BufferedImage;
25 import java.io.File;
26 import java.io.IOException;
27 import java.net.URI;
28
29 import javax.imageio.ImageIO;
30
31 /**
32  * Utility application for evaluating the effectiveness of the BlackPointEstimator used by
33  * MonochromeBitmapSource. Given a set of images on the command line, it converts each to a
34  * black-and-white PNG. The result is placed in a file based on the input name, with either
35  * "_converted_row" or "_converted_2d" appended.
36  *
37  * @author alasdair@google.com (Alasdair Mackintosh)
38  * @author dswitkin@google.com (Daniel Switkin)
39  */
40 public final class ImageConverter {
41
42   private static final String FORMAT = "png";
43   private static final int WHITE = 0xFFFFFFFF;
44   private static final int BLACK = 0xFF000000;
45   private static final int RED = 0xFFFF0000;
46   private static BlackPointEstimationMethod sMethod = BlackPointEstimationMethod.ROW_SAMPLING;
47
48   private ImageConverter() {
49   }
50
51   public static void main(String[] args) throws Exception {
52     for (String arg : args) {
53       if (arg.equals("-row")) {
54         sMethod = BlackPointEstimationMethod.ROW_SAMPLING;
55       } else if (arg.equals("-2d")) {
56         sMethod = BlackPointEstimationMethod.TWO_D_SAMPLING;
57       } else if (arg.startsWith("-")) {
58         System.out.println("Ignoring unrecognized option: " + arg);
59       }
60     }
61     for (String arg : args) {
62       if (arg.startsWith("-")) {
63         continue;
64       }
65       File inputFile = new File(arg);
66       if (inputFile.exists()) {
67         if (inputFile.isDirectory()) {
68           for (File input : inputFile.listFiles()) {
69             String filename = input.getName().toLowerCase();
70             // Skip hidden files and text files (the latter is found in the blackbox tests).
71             if (filename.startsWith(".") || filename.endsWith(".txt")) {
72               continue;
73             }
74             // Skip the results of dumping the black point.
75             if (filename.contains(".mono.png") || filename.contains(".row.png") ||
76                 filename.contains(".2d.png")) {
77               continue;
78             }
79             convertImage(input.toURI());
80           }
81         } else {
82           convertImage(inputFile.toURI());
83         }
84       } else {
85         convertImage(new URI(arg));
86       }
87     }
88   }
89
90
91   private static void convertImage(URI uri) throws IOException {
92     BufferedImage image = ImageIO.read(uri.toURL());
93     MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
94     int width = src.getWidth();
95     int height = src.getHeight();
96
97     BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
98     BitArray array = new BitArray(width);
99
100     try {
101       // Run the 2D sampling once up front
102       if (sMethod == BlackPointEstimationMethod.TWO_D_SAMPLING) {
103         src.estimateBlackPoint(sMethod, 0);
104       }
105     } catch (ReaderException e) {
106       System.out.println(e.toString());
107       return;
108     }
109
110       for (int y = 0; y < height; y++) {
111         // Run the 1D sampling once per row
112         if (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) {
113           try {
114               src.estimateBlackPoint(sMethod, y);
115           } catch (ReaderException e) {
116             // Draw rows with insufficient dynamic range in red
117             for (int x = 0; x < width; x++) {
118               result.setRGB(x, y, RED);
119             }
120             continue;
121           }
122         }
123
124         // Fetch the entire row at once, then fill out the result image
125         src.getBlackRow(y, array, 0, width);
126         for (int x = 0; x < width; x++) {
127           result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
128         }
129       }
130
131
132     File output = getOutput(uri);
133     System.out.printf("Writing output to %s\n", output);
134     ImageIO.write(result, FORMAT, output);
135   }
136
137   private static File getFileOfUri(URI uri) {
138     String name = uri.getPath();
139     int slashPos = name.lastIndexOf((int) '/');
140     String parent, basename;
141     if (slashPos != -1 && slashPos != name.length() - 1) {
142       parent = name.substring(0, slashPos);
143       basename = name.substring(slashPos + 1);
144     } else {
145       parent = ".";
146       basename = name;
147     }
148     File parentFile = new File(parent);
149     if (!parentFile.exists()) {
150       return null;
151     }
152
153     File baseFile = new File(parent, basename);
154     if (!baseFile.exists()) {
155       return null;
156     }
157
158     return baseFile;
159   }
160
161   private static File getOutput(URI uri) {
162     File result = getFileOfUri(uri);
163     if (result == null) {
164       result = new File("ConvertedImage." + FORMAT);
165     } else {
166       String name = result.getPath();
167       int dotpos = name.lastIndexOf((int) '.');
168       if (dotpos != -1) {
169         name = name.substring(0, dotpos);
170       }
171       String suffix = (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) ? "row" : "2d";
172       result = new File(name + '.' + suffix + '.' + FORMAT);
173     }
174     return result;
175   }
176
177 }