Various code tweaks and refactorings suggested by IntelliJ
[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           for (File input : inputFile.listFiles()) {
50             convertImage(input.toURI());
51           }
52         } else {
53           convertImage(inputFile.toURI());
54         }
55       } else {
56         convertImage(new URI(arg));
57       }
58     }
59   }
60
61
62   private static void convertImage(URI uri) throws IOException {
63     BufferedImage image = ImageIO.read(uri.toURL());
64     MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
65     int width = src.getWidth();
66     int height = src.getHeight();
67
68     BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
69     for (int i = 0; i < width; i++) {
70       for (int j = 0; j < height; j++) {
71         result.setRGB(i, j, src.isBlack(i, j) ? BLACK : WHITE);
72       }
73     }
74
75     File output = getOutput(uri);
76     System.out.printf("Writing output to %s\n", output);
77     ImageIO.write(result, FORMAT, output);
78   }
79
80   private static File getFileOfUri(URI uri) {
81     String name = uri.getPath();
82     int slashPos = name.lastIndexOf((int) '/');
83     String parent, basename;
84     if (slashPos != -1 && slashPos != name.length() - 1) {
85       parent = name.substring(0, slashPos);
86       basename = name.substring(slashPos + 1);
87     } else {
88       parent = ".";
89       basename = name;
90     }
91     File parentFile = new File(parent);
92     if (!parentFile.exists()) {
93       return null;
94     }
95
96     File baseFile = new File(parent, basename);
97     if (!baseFile.exists()) {
98       return null;
99     }
100
101     return baseFile;
102   }
103
104   private static File getOutput(URI uri) {
105     File result = getFileOfUri(uri);
106     if (result == null) {
107       result = new File("ConvertedImage." + FORMAT);
108     } else {
109       String name = result.getPath();
110       int dotpos = name.lastIndexOf((int) '.');
111       if (dotpos != -1) {
112         name = name.substring(0, dotpos);
113       }
114       result = new File(name + "_converted." + FORMAT);
115     }
116     return result;
117   }
118
119 }