Add more unit tests for client.result, and more small code tweaks.
[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("-")) {
62         continue;
63       }
64       File inputFile = new File(arg);
65       if (inputFile.exists()) {
66         if (inputFile.isDirectory()) {
67           for (File input : inputFile.listFiles()) {
68             convertImage(input.toURI());
69           }
70         } else {
71           convertImage(inputFile.toURI());
72         }
73       } else {
74         convertImage(new URI(arg));
75       }
76     }
77   }
78
79
80   private static void convertImage(URI uri) throws IOException {
81     BufferedImage image = ImageIO.read(uri.toURL());
82     MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
83     int width = src.getWidth();
84     int height = src.getHeight();
85
86     BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
87     BitArray array = new BitArray(width);
88
89     try {
90       // Run the 2D sampling once up front
91       if (sMethod == BlackPointEstimationMethod.TWO_D_SAMPLING) {
92         src.estimateBlackPoint(sMethod, 0);
93       }
94     } catch (ReaderException e) {
95       System.out.println(e.toString());
96       return;
97     }
98
99       for (int y = 0; y < height; y++) {
100         // Run the 1D sampling once per row
101         if (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) {
102           try {
103               src.estimateBlackPoint(sMethod, y);
104           } catch (ReaderException e) {
105             // Draw rows with insufficient dynamic range in red
106             for (int x = 0; x < width; x++) {
107               result.setRGB(x, y, RED);
108             }
109             continue;
110           }
111         }
112
113         // Fetch the entire row at once, then fill out the result image
114         src.getBlackRow(y, array, 0, width);
115         for (int x = 0; x < width; x++) {
116           result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
117         }
118       }
119
120
121     File output = getOutput(uri);
122     System.out.printf("Writing output to %s\n", output);
123     ImageIO.write(result, FORMAT, output);
124   }
125
126   private static File getFileOfUri(URI uri) {
127     String name = uri.getPath();
128     int slashPos = name.lastIndexOf((int) '/');
129     String parent, basename;
130     if (slashPos != -1 && slashPos != name.length() - 1) {
131       parent = name.substring(0, slashPos);
132       basename = name.substring(slashPos + 1);
133     } else {
134       parent = ".";
135       basename = name;
136     }
137     File parentFile = new File(parent);
138     if (!parentFile.exists()) {
139       return null;
140     }
141
142     File baseFile = new File(parent, basename);
143     if (!baseFile.exists()) {
144       return null;
145     }
146
147     return baseFile;
148   }
149
150   private static File getOutput(URI uri) {
151     File result = getFileOfUri(uri);
152     if (result == null) {
153       result = new File("ConvertedImage." + FORMAT);
154     } else {
155       String name = result.getPath();
156       int dotpos = name.lastIndexOf((int) '.');
157       if (dotpos != -1) {
158         name = name.substring(0, dotpos);
159       }
160       String suffix = (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) ? "row" : "2d";
161       result = new File(name + "_converted_" + suffix + '.' + FORMAT);
162     }
163     return result;
164   }
165
166 }