Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.awt.image.*;
- import javax.imageio.*;
- public class Encoder {
- static int pattern(int x, int y, int width, BufferedImage i) {
- int ret = 0;
- for(int offset = 0; offset < width; offset++) {
- boolean color = (i.getRGB(x + offset, y) & 0xFF000000) != 0;
- ret = (ret << 1) | (color ? 1 : 0);
- }
- return ret;
- }
- static void evaluate(List<Integer> data, BufferedImage i, String algo, boolean dump) {
- int score = (i.getWidth()*i.getHeight()/8)-data.size();
- System.out.format("%s:\t%d bytes\t(%+d)%n",
- algo, data.size(), -score
- );
- if (!dump) { return; }
- for(int index = 0; index < data.size(); index++) {
- System.out.format((index % 8 == 7) ? "0x%02X%n\t" : "0x%02X ",
- data.get(index)
- );
- }
- }
- public static void main(String[] args) throws IOException {
- BufferedImage image = ImageIO.read(new File(args[0]));
- // this compression scheme uses nybble fields-
- // the high half of each byte is the "pattern" and the low half is the "count"
- {
- List<Integer> data = new ArrayList<>();
- for(int y = 0; y < image.getHeight(); y++) {
- for(int x = 0; x < image.getWidth(); x += 4) {
- int p = pattern(x, y, 4, image);
- int n = 0;
- while(x+4 < image.getWidth() && n < 15) {
- if (p != pattern(x+4, y, 4, image)) { break; }
- n += 1;
- x += 4;
- }
- data.add((p << 4) | n);
- }
- }
- evaluate(data, image, "horiz-pattern", false);
- }
- // same deal but in vertical strips instead of horizontal
- {
- List<Integer> data = new ArrayList<>();
- for(int x = 0; x < image.getWidth(); x += 4) {
- for(int y = 0; y < image.getHeight(); y++) {
- int p = pattern(x, y, 4, image);
- int n = 0;
- while(y+1 < image.getHeight() && n < 15) {
- if (p != pattern(x, y+1, 4, image)) { break; }
- n += 1;
- y += 1;
- }
- data.add((p << 4) | n);
- }
- }
- evaluate(data, image, "verti-pattern", false);
- }
- System.out.println();
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement