Skip to content

Commit 7770789

Browse files
committed
initial version
1 parent adf4425 commit 7770789

19 files changed

+402
-0
lines changed

HOWTO.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**prequesites**
2+
3+
Java JDK >= 1.5
4+
Maven >= 3.0
5+
6+
**get the source**
7+
8+
git clone https://github.com/SubOptimal/ImageToHacklace2
9+
10+
**compile the source**
11+
12+
mvn clean package
13+
14+
**run the ImageToHacklace2 converter**
15+
16+
cd target<br>
17+
java -jar ImageToHacklace2 image_file

TODO.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
**for next version**
2+
- add process fo hacklace template files
3+
- add documentation
4+
5+
**for the future**
6+
- add a basic image editor to generate image files

pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>sub.optimal</groupId>
6+
<artifactId>ImageToHacklace2</artifactId>
7+
<version>0.1.0</version>
8+
<packaging>jar</packaging>
9+
10+
<name>ImageToHacklace2</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<licenses>
14+
<license>
15+
<name>GNU General Public License (GPL-2.0)</name>
16+
<url>http://opensource.org/licenses/GPL-2.0</url>
17+
<distribution>repo</distribution>
18+
</license>
19+
</licenses>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<netbeans.hint.license>gpl20</netbeans.hint.license>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>[3.8,)</version>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<version>2.0.2</version>
41+
<configuration>
42+
<source>1.5</source>
43+
<target>1.5</target>
44+
</configuration>
45+
</plugin>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<version>2.9</version>
50+
<configuration>
51+
<workingDirectory>target/test-classes</workingDirectory>
52+
</configuration>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-jar-plugin</artifactId>
57+
<configuration>
58+
<archive>
59+
<manifest>
60+
<mainClass>sub.optimal.Img2Hl</mainClass>
61+
</manifest>
62+
</archive>
63+
</configuration>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
</project>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.hacklace2;
19+
20+
import java.awt.image.BufferedImage;
21+
import java.io.File;
22+
import java.io.IOException;
23+
import javax.imageio.ImageIO;
24+
25+
/**
26+
*
27+
* @author suboptimal
28+
*/
29+
public class Converter {
30+
31+
public static final String HEX_BYTE_MARKER = "\\";
32+
public static final String DATABLOCK_START = HEX_BYTE_MARKER + "1F";
33+
public static final int RGB_WHITE = 16777215;
34+
private static final String ZERO_LENGTH_STRING = "";
35+
36+
public static Converter getInstance() {
37+
return new Converter();
38+
}
39+
40+
private boolean checkDimension(int height, int width) {
41+
if (height < 1 || width < 1) {
42+
System.err.println("image dimension out of range: minimal dimension = 1 x 1 pixel");
43+
return false;
44+
}
45+
if (height > 8) {
46+
System.err.println("image height out of range: area will be truncated to 8 pixel height");
47+
}
48+
if (width > 255) {
49+
System.err.println("image width out of range: area will be truncated to 255 pixel width");
50+
}
51+
return true;
52+
}
53+
54+
public String convertToHacklace(File image) {
55+
try {
56+
BufferedImage buffImage = ImageIO.read(image);
57+
int height = buffImage.getHeight();
58+
int width = buffImage.getWidth();
59+
if (!checkDimension(height, width)) {
60+
return ZERO_LENGTH_STRING;
61+
}
62+
width = (width < 255 ? width - 1 : 254);
63+
height = (height < 8 ? height - 1 : 7);
64+
StringBuilder out = new StringBuilder();
65+
out.append(String.format("%s %02X", DATABLOCK_START, width + 1));
66+
for (int column = 0; column <= width; column++) {
67+
int hlValue = 0;
68+
for (int row = height; row >= 0; row--) {
69+
hlValue <<= 1;
70+
if ((buffImage.getRGB(column, row) & RGB_WHITE) != RGB_WHITE) {
71+
hlValue++;
72+
}
73+
}
74+
out.append(String.format(" %02X", hlValue));
75+
}
76+
out.append(HEX_BYTE_MARKER);
77+
return out.toString();
78+
} catch (IOException ex) {
79+
System.err.println("failure during reading image file: " + ex.getMessage());
80+
return ZERO_LENGTH_STRING;
81+
}
82+
}
83+
84+
}

0 commit comments

Comments
 (0)