Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ignatieff.tepng;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class Encoder {
- BufferedImage img;
- private static byte[] empty = { 0x00, 0x00, 0x00, 0x00, 0x00 };
- 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 int getBit(byte b, int bit){
- if((b & (1 << bit))!=0)return 1;return 0;
- }
- private int[] convertToBits(byte[] bytes){
- int a = bytes.length*8;
- int[] r = new int[a];
- for(int i=0; i<bytes.length; i++){
- for(int j=0; j<8; j++){
- r[a-((bytes.length-1-i)*8+j)-1] = getBit(bytes[i], j);
- }
- }
- return r;
- }
- public byte[] readBytes(int x, int y){
- int[] bits = new int[40];
- for(int i=0; i<8; i++){
- int a;
- if(x+i<img.getWidth()){
- a = decodeData(x+i, y);
- }else{
- a = decodeData((x+i)-img.getWidth(), y+1);
- }
- for(int j=0; j<5; j++){
- bits[i*5+j]=getBit(a, 4-j);
- }
- }
- return convertToBytes(bits);
- }
- private byte[] convertToBytes(int[] bits){
- if(bits.length%8!=0)return null;
- int p = bits.length/8;
- byte[] r = new byte[p];
- for(int i=0; i<p; i++){
- int a = 0;
- for(int j=0; j<8; j++){
- a |= bits[i*8+j];
- if(j!=7)a<<=1;
- }
- r[i] = (byte)a;
- }
- return r;
- }
- private byte[] appendBytes(byte[] b){
- byte[] r = b;
- while(b.length%5!=0){
- byte[] q = new byte[r.length+1];
- for(int i=0; i<r.length; i++){
- q[i] = r[i];
- }
- q[r.length] = 0x00;
- r=q;
- }
- return r;
- }
- private byte[] appendBytes(byte[] b, byte[] c){
- byte[] newArray = new byte[b.length+c.length];
- for(int i=0; i<b.length; i++){
- newArray[i] = b[i];
- }
- for(int i=0; i<c.length; i++){
- newArray[b.length+i] = c[i];
- }
- return newArray;
- }
- private boolean isNull(byte[] b){
- for(int i=0; i<b.length; i++){
- if(b[i] != 0)return false;
- }
- return true;
- }
- public void encodeFile(String path, String key) throws IOException{
- encodeFile(path, key.hashCode());
- }
- public void encodeFile(String path, int key) throws IOException{
- encodeFile(readFile(path), key);
- }
- public void saveContentFile(String path, String key) throws IOException{
- saveContentFile(path, key.hashCode());
- }
- public void saveContentFile(String path, int key) throws IOException{
- File f = new File(path);
- byte[] data = readFile(key);
- FileOutputStream fos = new FileOutputStream(f);
- fos.write(data);
- fos.close();
- }
- private byte[] readFile(String path) throws IOException{
- File f = new File(path);
- FileInputStream fin = new FileInputStream(f);
- byte fileContent[] = new byte[(int)f.length()];
- fin.read(fileContent);
- fin.close();
- return fileContent;
- }
- private void printByte(byte[] b){
- String s = "{ '";
- for(int i=0; i<b.length; i++){
- s+=(int)b[i] + "', '";
- }
- System.out.println( s.substring(0, s.length()-3) + " }" );
- }
- public byte[] readFile(int key){
- byte[] bytes = new byte[0];
- Group g = new Group((img.getWidth()*img.getHeight())/8, key);
- boolean isDone = false;
- int x = 0, y = 0;
- while(!isDone){
- //System.out.println("reading from ("+x+", "+y+")");
- byte[] next = readBytes(x*8,y*8);
- //printByte(next);
- bytes = appendBytes(bytes, next);
- if(isNull(next)){isDone=true;}
- x+=g.PRIME;
- while(x>=img.getWidth()/8){x-=img.getWidth()/8;y++;}
- if(y>=img.getHeight()/8){y%=img.getHeight()/8;}
- }
- return bytes;
- }
- public void encodeFile(byte[] bytes, String key){
- encodeFile(bytes, key.hashCode());
- }
- public void encodeFile(byte[] bytes, int key){
- if(bytes.length >= (img.getWidth() * img.getHeight())/5)return;
- byte[] e = appendBytes(appendBytes(bytes, empty));
- int max = e.length/5;
- int size = (img.getWidth()*img.getHeight())/8;
- Group g = new Group(size, key);
- int x=0, y=0;
- for(int i=0; i<max; i++){
- encodeBytes(subarray(e,i*5,5),x*8,y*8);
- x+=g.PRIME;
- while(x>=img.getWidth()/8){x-=img.getWidth()/8;y++;}
- if(y>=img.getHeight()/8){y%=img.getHeight()/8;}
- }
- }
- private byte[] subarray(byte[] array, int start, int len){
- byte[] newArray = new byte[len];
- for(int i=0; i<len; i++){
- newArray[i] = array[start+i];
- }
- return newArray;
- }
- private void encodeBytes(byte[] bytes, int x, int y){
- if(bytes.length!=5){return;}
- int[] bits = convertToBits(bytes);
- for(int i=0; i<8; i++){
- int a = 0;
- for(int j=0; j<5; j++){
- a |= bits[i*5+j];
- if(j!=4)a<<=1;
- }
- if(x+i<img.getWidth()){
- encodeData(a, x+i, y);
- }else{
- encodeData(a, (x+i)-img.getWidth(), y+1);
- }
- }
- }
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement