Updated Alasdair's ImageConverter tool to take -row and -2d arguments to evaluate...
[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.MonochromeBitmapSource;
20 import com.google.zxing.BlackPointEstimationMethod;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.common.BitArray;
23
24 import javax.imageio.ImageIO;
25 import java.awt.image.BufferedImage;
26 import java.io.File;
27 import java.io.IOException;
28 import java.net.URI;
29
30 /**
31  * Utility application for evaluating the effectiveness of the BlackPointEstimator used by
32  * MonochromeBitmapSource. Given a set of images on the command line, it converts each to a
33  * black-and-white PNG. The result is placed in a file based on the input name, with either
34  * "_converted_row" or "_converted_2d" appended.
35  *
36  * @author alasdair@google.com (Alasdair Mackintosh)
37  * @author dswitkin@google.com (Daniel Switkin)
38  */
39 public final class ImageConverter {
40
41   private static final String FORMAT = "png";
42   private static final int WHITE = 0xFFFFFFFF;
43   private static final int BLACK = 0xFF000000;
44   private static final int RED = 0xFFFF0000;
45   private static BlackPointEstimationMethod sMethod = BlackPointEstimationMethod.ROW_SAMPLING;
46
47   private ImageConverter() {
48   }
49
50   public static void main(String[] args) throws Exception {
51     for (String arg : args) {
52       if (arg.equals("-row")) {
53         sMethod = BlackPointEstimationMethod.ROW_SAMPLING;
54       } else if (arg.equals("-2d")) {
55         sMethod = BlackPointEstimationMethod.TWO_D_SAMPLING;
56       } else if (arg.startsWith("-")) {
57         System.out.println("Ignoring unrecognized option: " + arg);
58       }
59     }
60     for (String arg : args) {
61       if (arg.startsWith("-")) continue;
62       File inputFile = new File(arg);
63       if (inputFile.exists()) {
64         if (inputFile.isDirectory()) {
65           for (File input : inputFile.listFiles()) {
66             convertImage(input.toURI());
67           }
68         } else {
69           convertImage(inputFile.toURI());
70         }
71       } else {
72         convertImage(new URI(arg));
73       }
74     }
75   }
76
77
78   private static void convertImage(URI uri) throws IOException {
79     BufferedImage image = ImageIO.read(uri.toURL());
80     MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
81     int width = src.getWidth();
82     int height = src.getHeight();
83
84     BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
85     BitArray array = new BitArray(width);
86
87     try {
88       // Run the 2D sampling once up front
89       if (sMethod == BlackPointEstimationMethod.TWO_D_SAMPLING) {
90         src.estimateBlackPoint(sMethod, 0);
91       }
92     } catch (ReaderException e) {
93       System.out.println(e.toString());
94       return;
95     }
96
97       for (int y = 0; y < height; y++) {
98         // Run the 1D sampling once per row
99         if (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) {
100           try {
101               src.estimateBlackPoint(sMethod, y);
102           } catch (ReaderException e) {
103             // Draw rows with insufficient dynamic range in red
104             for (int x = 0; x < width; x++) {
105               result.setRGB(x, y, RED);
106             }
107             continue;
108           }
109         }
110
111         // Fetch the entire row at once, then fill out the result image
112         src.getBlackRow(y, array, 0, width);
113         for (int x = 0; x < width; x++) {
114           result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
115         }
116       }
117
118
119     File output = getOutput(uri);
120     System.out.printf("Writing output to %s\n", output);
121     ImageIO.write(result, FORMAT, output);
122   }
123
124   private static File getFileOfUri(URI uri) {
125     String name = uri.getPath();
126     int slashPos = name.lastIndexOf((int) '/');
127     String parent, basename;
128     if (slashPos != -1 && slashPos != name.length() - 1) {
129       parent = name.substring(0, slashPos);
130       basename = name.substring(slashPos + 1);
131     } else {
132       parent = ".";
133       basename = name;
134     }
135     File parentFile = new File(parent);
136     if (!parentFile.exists()) {
137       return null;
138     }
139
140     File baseFile = new File(parent, basename);
141     if (!baseFile.exists()) {
142       return null;
143     }
144
145     return baseFile;
146   }
147
148   private static File getOutput(URI uri) {
149     File result = getFileOfUri(uri);
150     if (result == null) {
151       result = new File("ConvertedImage." + FORMAT);
152     } else {
153       String name = result.getPath();
154       int dotpos = name.lastIndexOf((int) '.');
155       if (dotpos != -1) {
156         name = name.substring(0, dotpos);
157       }
158       String suffix = (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) ? "row" : "2d";
159       result = new File(name + "_converted_" + suffix + "." + FORMAT);
160     }
161     return result;
162   }
163
164 }