Finished work on the local binarizer and renamed it to HybridBinarizer. It uses the...
[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.BinaryBitmap;
20 import com.google.zxing.LuminanceSource;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.common.BitArray;
23 import com.google.zxing.common.BitMatrix;
24 import com.google.zxing.common.HybridBinarizer;
25
26 import java.awt.image.BufferedImage;
27 import java.io.File;
28 import java.io.IOException;
29 import java.net.URI;
30
31 import javax.imageio.ImageIO;
32
33 /**
34  * A utility application for evaluating the effectiveness of various thresholding algorithms.
35  * Given a set of images on the command line, it converts each to a black-and-white PNG.
36  * The result is placed in a file based on the input name, with either ".row.png" or ".2d.png"
37  * appended.
38  *
39  * TODO: Needs to be updated to accept different Binarizer implementations.
40  * TODO: Consider whether to keep this separate app, as CommandLineRunner has similar functionality.
41  *
42  * @author alasdair@google.com (Alasdair Mackintosh)
43  * @author dswitkin@google.com (Daniel Switkin)
44  */
45 public final class ImageConverter {
46
47   private static final String FORMAT = "png";
48   private static final int WHITE = 0xFFFFFFFF;
49   private static final int BLACK = 0xFF000000;
50   private static final int RED = 0xFFFF0000;
51   private static boolean rowSampling = false;
52
53   private ImageConverter() {
54   }
55
56   public static void main(String[] args) throws Exception {
57     for (String arg : args) {
58       if ("-row".equals(arg)) {
59         rowSampling = true;
60       } else if ("-2d".equals(arg)) {
61         rowSampling = false;
62       } else if (arg.startsWith("-")) {
63         System.err.println("Ignoring unrecognized option: " + arg);
64       }
65     }
66     for (String arg : args) {
67       if (arg.startsWith("-")) {
68         continue;
69       }
70       File inputFile = new File(arg);
71       if (inputFile.exists()) {
72         if (inputFile.isDirectory()) {
73           for (File input : inputFile.listFiles()) {
74             String filename = input.getName().toLowerCase();
75             // Skip hidden files and text files (the latter is found in the blackbox tests).
76             if (filename.startsWith(".") || filename.endsWith(".txt")) {
77               continue;
78             }
79             // Skip the results of dumping the black point.
80             if (filename.contains(".mono.png") || filename.contains(".row.png") ||
81                 filename.contains(".2d.png")) {
82               continue;
83             }
84             convertImage(input.toURI());
85           }
86         } else {
87           convertImage(inputFile.toURI());
88         }
89       } else {
90         convertImage(new URI(arg));
91       }
92     }
93   }
94
95
96   private static void convertImage(URI uri) throws IOException {
97     BufferedImage image = ImageIO.read(uri.toURL());
98     LuminanceSource source = new BufferedImageLuminanceSource(image);
99     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
100     int width = bitmap.getWidth();
101     int height = bitmap.getHeight();
102
103     BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
104     BitArray array = new BitArray(width);
105
106     if (rowSampling) {
107       for (int y = 0; y < height; y++) {
108           try {
109             array = bitmap.getBlackRow(y, array);
110           } catch (ReaderException e) {
111             // Draw rows with insufficient dynamic range in red
112             for (int x = 0; x < width; x++) {
113               result.setRGB(x, y, RED);
114             }
115             continue;
116           }
117
118         for (int x = 0; x < width; x++) {
119           result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
120         }
121       }
122     } else {
123       try {
124         BitMatrix matrix = bitmap.getBlackMatrix();
125         for (int y = 0; y < height; y++) {
126           for (int x = 0; x < width; x++) {
127             result.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
128           }
129         }
130       } catch (ReaderException e) {
131
132       }
133     }
134
135     File output = getOutput(uri);
136     System.out.printf("Writing output to %s\n", output);
137     ImageIO.write(result, FORMAT, output);
138   }
139
140   private static File getFileOfUri(URI uri) {
141     String name = uri.getPath();
142     int slashPos = name.lastIndexOf((int) '/');
143     String parent;
144     String basename;
145     if (slashPos != -1 && slashPos != name.length() - 1) {
146       parent = name.substring(0, slashPos);
147       basename = name.substring(slashPos + 1);
148     } else {
149       parent = ".";
150       basename = name;
151     }
152     File parentFile = new File(parent);
153     if (!parentFile.exists()) {
154       return null;
155     }
156
157     File baseFile = new File(parent, basename);
158     if (!baseFile.exists()) {
159       return null;
160     }
161
162     return baseFile;
163   }
164
165   private static File getOutput(URI uri) {
166     File result = getFileOfUri(uri);
167     if (result == null) {
168       result = new File("ConvertedImage." + FORMAT);
169     } else {
170       String name = result.getPath();
171       int dotpos = name.lastIndexOf((int) '.');
172       if (dotpos != -1) {
173         name = name.substring(0, dotpos);
174       }
175       String suffix = rowSampling ? "row" : "2d";
176       result = new File(name + '.' + suffix + '.' + FORMAT);
177     }
178     return result;
179   }
180
181 }