Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Encoder Class
- package com.ignatieff.tepng;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class Encoder {
- BufferedImage img;
- public Encoder(BufferedImage image){
- img = image;
- }
- public Encoder(String path) throws IOException{
- this(loadImage(path));
- }
- private static BufferedImage loadImage(String path) throws IOException{
- File f = new File(path);
- BufferedImage i = ImageIO.read(f);
- return i;
- }
- public void saveImage(String path) throws IOException{
- File f = new File(path);
- ImageIO.write(img, "png", f);
- }
- public static int getBit(int input, int bit){
- if((input & (1 << bit)) != 0)return 1;
- return 0;
- }
- private boolean writeChar(char c, int x, int y){
- int i = translateChar(c);
- if(i==-1)return false;
- encodeData(i, x, y);
- return true;
- }
- private char readChar(int x, int y){
- return translateInt(decodeData(x,y));
- }
- public void encodeMessage(String message){
- encodeMessage(message, 1);
- }
- public void encodeMessage(String message, String key){
- encodeMessage(message, key.hashCode());
- }
- public String getMessage(){
- return getMessage(1);
- }
- public String getMessage(String key){
- return getMessage(key.hashCode());
- }
- public void encodeMessage(String message, int key){
- int max = img.getHeight() * img.getWidth();
- if(message.length()+5 >= max){return;}
- Group g = new Group(max, key);
- char[] c = (message+"'.!.'").toCharArray();
- int x = 0, y = 0;
- for(int i=0;i<c.length;i++){
- if(writeChar(c[i],x,y)){
- x+=g.PRIME;
- while(x>=img.getWidth()){x-=img.getWidth();y++;}
- if(y>=img.getHeight()){y%=img.getHeight();}
- }
- }
- }
- public String getMessage(int key){
- String r = "";
- Group g = new Group(img.getHeight() * img.getWidth(), key);
- int x=0, y=0;
- while(!r.contains("'.!.'")){
- while(x>=img.getWidth()){x-=img.getWidth();y++;}
- if(y>=img.getHeight()){y%=img.getHeight();}
- r+=readChar(x,y);
- x+=g.PRIME;
- }
- return r.substring(0,r.length()-5);
- }
- private int decodeData(int x, int y){
- int argb = img.getRGB(x, y);
- int r = (argb)&0xFF;
- int g = (argb>>8)&0xFF;
- int b = (argb>>16)&0xFF;
- int rDec = (getBit(r, 1) << 1) | getBit(r, 0);
- int gDec = getBit(g, 0);
- int bDec = (getBit(b, 1) << 1) | getBit(b, 0);
- int data = (bDec << 3)|(gDec << 2)|rDec;
- return data;
- }
- private void encodeData(int data, int x, int y){
- if(data>=32){return;}
- int rEnc = (getBit(data, 1) << 1)|getBit(data, 0);
- int gEnc = getBit(data, 2);
- int bEnc = (getBit(data, 4) << 1)|getBit(data, 3);
- int argb = img.getRGB(x, y);
- int r = (((argb)&0xFF) >> 2) << 2;
- int g = (((argb>>8)&0xFF) >> 1) << 1;
- int b = (((argb>>16)&0xFF) >> 2) << 2;
- int newR = r | rEnc;
- int newG = g | gEnc;
- int newB = b | bEnc;
- int newARGB = (newB << 16)|(newG << 8)|newR;
- img.setRGB(x, y, newARGB);
- }
- public static char translateInt(int i){
- if(i <= 25)return (char)(i+65);
- switch(i){
- case 26:
- return ' ';
- case 27:
- return '.';
- case 28:
- return ',';
- case 29:
- return '!';
- case 30:
- return '?';
- case 31:
- return '\'';
- }
- return '#';
- }
- public static int translateChar(char c){
- if((int)c >= 65 && (int)c <=90){
- return (int)c-65;
- }
- if((int)c >= 97 && (int)c <=122){
- return (int)c-97;
- }
- switch(c){
- case ' ':
- return 26;
- case '.':
- return 27;
- case ',':
- return 28;
- case '!':
- return 29;
- case '?':
- return 30;
- case '\'':
- return 31;
- }
- return -1;
- }
- }
- //Group Class
- package com.ignatieff.tepng;
- import java.math.BigInteger;
- public class Group {
- public int PRIME;
- public Group(int size, int hash){
- int p = (int)Math.abs(hash % size);
- int i=0;
- BigInteger b = BigInteger.valueOf(p);
- while(!b.isProbablePrime(10)){
- int m = 1;++i;
- if(i%2==0){m*=-1;}
- b = BigInteger.valueOf(p+m*i);
- }
- PRIME = b.intValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement