|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 suboptimal |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 2 |
| 7 | + * of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | + */ |
| 18 | +package sub.optimal; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import sub.optimal.hacklace2.Converter; |
| 22 | + |
| 23 | +/** |
| 24 | + * Main class of ImageToHacklace2 converter.<br><br> |
| 25 | + * This tool converts images files into a string of hexadecimal bytes which can be downloaded<br> |
| 26 | + * as part of an animation onto the "HackLace2" |
| 27 | + * (see <a href="http://wiki.fab4u.de/wiki/Hacklace">http://wiki.fab4u.de/wiki/Hacklace</a>). |
| 28 | + * |
| 29 | + * @author SubOptimal |
| 30 | + * @version 0.1.0 |
| 31 | + */ |
| 32 | +public class Img2Hl { |
| 33 | + private static final String VERSION = "0.1.0"; |
| 34 | + |
| 35 | + private static String templateFileName; |
| 36 | + private static String imageFileName; |
| 37 | + |
| 38 | + public static void main(String[] args) { |
| 39 | + processParameters(args); |
| 40 | + if (imageFileName != null) { |
| 41 | + processImageFile(); |
| 42 | + } else if (templateFileName != null) { |
| 43 | + processTemplateFile(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private static void processTemplateFile() { |
| 48 | + throw new UnsupportedOperationException("Not supported yet - see TODO"); |
| 49 | + } |
| 50 | + |
| 51 | + private static void processImageFile() { |
| 52 | + File image = new File(imageFileName); |
| 53 | + if (!image.exists() || !image.isFile()) { |
| 54 | + System.err.println(String.format("file %s: does not exist", image.getName())); |
| 55 | + System.exit(1); |
| 56 | + } |
| 57 | + |
| 58 | + if (!image.canRead()) { |
| 59 | + System.err.println(String.format("file %s: no read permission", image.getName())); |
| 60 | + System.exit(1); |
| 61 | + } |
| 62 | + |
| 63 | + Converter converter = Converter.getInstance(); |
| 64 | + System.out.println(converter.convertToHacklace(image)); |
| 65 | + } |
| 66 | + |
| 67 | + private static void processParameters(String[] args) { |
| 68 | + if (args.length == 0) { |
| 69 | + showUsage(); |
| 70 | + System.exit(1); |
| 71 | + } |
| 72 | + |
| 73 | + for (int i = 0; i < args.length; i++) { |
| 74 | + String opt = args[i]; |
| 75 | + if (opt.startsWith("--template")) { |
| 76 | + // TODO will be implemented as next ;-) |
| 77 | + processTemplateFile(); |
| 78 | + if ((i + 1) < args.length) { |
| 79 | + templateFileName = args[++i]; |
| 80 | + } else { |
| 81 | + System.err.println("missed parameter for option --template"); |
| 82 | + System.exit(1); |
| 83 | + } |
| 84 | + } else if (opt.startsWith("--")) { |
| 85 | + System.err.printf("unknown option passed '%s'%n", opt); |
| 86 | + System.exit(1); |
| 87 | + } else { |
| 88 | + imageFileName = opt; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static void showUsage() { |
| 94 | + System.err.printf("ImageToHacklace2 " + VERSION + "%n%n"); |
| 95 | + System.err.printf("usage: java -jar ImageToHacklace2 [ image_file | --template template_file ]%n%n"); |
| 96 | + System.err.printf("%16s%s%n", "image_file -", "an image file to convert into the hacklace databytes"); |
| 97 | + System.err.printf("%16s%s%n", "", "recommended formats: BMP, GIF, PNG"); |
| 98 | + System.err.printf("%16s%s%n", "--template -", "template hacklace configuration file with placeholders"); |
| 99 | + System.err.printf("%16s%s%n", "", "for image files"); |
| 100 | + } |
| 101 | +} |
0 commit comments