Minor code tweaks
[zxing.git] / javase / src / com / google / zxing / client / j2se / ImageConverter.java
1 /*
2  * Copyright 2007 Google Inc.
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
21 import javax.imageio.ImageIO;
22 import java.awt.image.BufferedImage;
23 import java.io.File;
24 import java.io.IOException;
25 import java.net.URI;
26
27 /**
28  * Utility application for evaluating the effectiveness of the
29  * MonochromeBitmapSource. Given a set of images on the command line,
30  * converts each to a black-and-white GIF. The result is placed in 
31  * a file based on the input name, with "_converted" appended.
32  *
33  * @author alasdair@google.com (Alasdair Mackintosh)
34  */
35 public final class ImageConverter {
36
37   private static final String FORMAT = "gif";
38   private static final int WHITE = 0xFFFFFFFF;
39   private static final int BLACK = 0x000000FF;
40
41   private ImageConverter() {
42   }
43
44   public static void main(String[] args) throws Exception {
45     for (String arg : args) {
46       File inputFile = new File(arg);
47       if (inputFile.exists()) {
48         if (inputFile.isDirectory()) {
49           int count = 0;
50           for (File input : inputFile.listFiles()) {
51             convertImage(input.toURI());
52           }
53         } else {
54           convertImage(inputFile.toURI());
55         }
56       } else {
57         convertImage(new URI(arg));
58       }
59     }
60   }
61
62
63   private static void convertImage(URI uri) throws IOException {
64     BufferedImage image = ImageIO.read(uri.toURL());
65     MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
66     int width = src.getWidth();
67     int height = src.getHeight();
68     
69     BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
70     for (int i = 0; i < width; i++) {
71       for (int j = 0; j < height; j++) {
72         result.setRGB(i, j, src.isBlack(i,j) ? BLACK : WHITE);
73       }
74     }
75
76     File output = getOutput(uri);
77     System.out.printf("Writing output to %s\n", output);
78     ImageIO.write(result, FORMAT, output);
79   }
80
81   private static File getFileOfUri(URI uri) {
82     String name = uri.getPath();
83     int slashPos = name.lastIndexOf((int) '/');
84     String parent, basename;
85     if (slashPos != -1 && slashPos != name.length()-1) {
86       parent = name.substring(0, slashPos);
87       basename = name.substring(slashPos+1);
88     } else {
89       parent = ".";
90       basename = name;
91     }
92     File parentFile = new File(parent);
93     if (!parentFile.exists()) {
94       return null;
95     }
96
97     File baseFile = new File(parent,basename);
98     if (!baseFile.exists()) {
99       return null;
100     }
101
102     return baseFile;
103   }
104
105   private static File getOutput(URI uri) {
106     File result = getFileOfUri(uri);
107     if (result == null) {
108       result = new File("ConvertedImage." + FORMAT);
109     } else {
110       String name = result.getPath();
111       int dotpos = name.lastIndexOf((int) '.');
112       if (dotpos != -1) {
113         name = name.substring(0, dotpos);
114       }
115       result = new File(name + "_converted." + FORMAT);
116     }
117     return result;
118   }
119
120 }