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