Advertisement
Guest User

PNG Encoder MK-II

a guest
Nov 11th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Encoder Class
  2. package com.ignatieff.tepng;
  3.  
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7.  
  8. import javax.imageio.ImageIO;
  9.  
  10. public class Encoder {
  11.    
  12.     BufferedImage img;
  13.    
  14.     public Encoder(BufferedImage image){
  15.         img = image;
  16.     }
  17.    
  18.     public Encoder(String path) throws IOException{
  19.         this(loadImage(path));
  20.     }
  21.    
  22.     private static BufferedImage loadImage(String path) throws IOException{
  23.         File f = new File(path);
  24.         BufferedImage i = ImageIO.read(f);
  25.         return i;
  26.     }
  27.    
  28.     public void saveImage(String path) throws IOException{
  29.         File f = new File(path);
  30.         ImageIO.write(img, "png", f);
  31.     }
  32.    
  33.     public static int getBit(int input, int bit){
  34.         if((input & (1 << bit)) != 0)return 1;
  35.         return 0;
  36.     }
  37.    
  38.     private boolean writeChar(char c, int x, int y){
  39.         int i = translateChar(c);
  40.         if(i==-1)return false;
  41.         encodeData(i, x, y);
  42.         return true;
  43.     }
  44.    
  45.     private char readChar(int x, int y){
  46.         return translateInt(decodeData(x,y));
  47.     }
  48.    
  49.     public void encodeMessage(String message){
  50.         encodeMessage(message, 1);
  51.     }
  52.    
  53.     public void encodeMessage(String message, String key){
  54.         encodeMessage(message, key.hashCode());
  55.     }
  56.    
  57.     public String getMessage(){
  58.         return getMessage(1);
  59.     }
  60.    
  61.     public String getMessage(String key){
  62.         return getMessage(key.hashCode());
  63.     }
  64.    
  65.     public void encodeMessage(String message, int key){
  66.         int max = img.getHeight() * img.getWidth();
  67.         if(message.length()+5 >= max){return;}
  68.         Group g = new Group(max, key);
  69.         char[] c = (message+"'.!.'").toCharArray();
  70.         int x = 0, y = 0;
  71.         for(int i=0;i<c.length;i++){
  72.             if(writeChar(c[i],x,y)){
  73.                 x+=g.PRIME;
  74.                 while(x>=img.getWidth()){x-=img.getWidth();y++;}
  75.                 if(y>=img.getHeight()){y%=img.getHeight();}
  76.             }
  77.         }
  78.     }
  79.    
  80.     public String getMessage(int key){
  81.         String r = "";
  82.         Group g = new Group(img.getHeight() * img.getWidth(), key);
  83.         int x=0, y=0;
  84.         while(!r.contains("'.!.'")){
  85.             while(x>=img.getWidth()){x-=img.getWidth();y++;}
  86.             if(y>=img.getHeight()){y%=img.getHeight();}
  87.             r+=readChar(x,y);
  88.             x+=g.PRIME;
  89.         }
  90.         return r.substring(0,r.length()-5);
  91.     }
  92.    
  93.     private int decodeData(int x, int y){
  94.         int argb = img.getRGB(x, y);
  95.        
  96.         int r = (argb)&0xFF;
  97.         int g = (argb>>8)&0xFF;
  98.         int b = (argb>>16)&0xFF;
  99.        
  100.         int rDec = (getBit(r, 1) << 1) | getBit(r, 0);
  101.         int gDec = getBit(g, 0);
  102.         int bDec = (getBit(b, 1) << 1) | getBit(b, 0);
  103.        
  104.         int data = (bDec << 3)|(gDec << 2)|rDec;
  105.        
  106.         return data;
  107.     }
  108.    
  109.     private void encodeData(int data, int x, int y){
  110.         if(data>=32){return;}
  111.        
  112.         int rEnc = (getBit(data, 1) << 1)|getBit(data, 0);
  113.         int gEnc = getBit(data, 2);
  114.         int bEnc = (getBit(data, 4) << 1)|getBit(data, 3);
  115.         int argb = img.getRGB(x, y);
  116.  
  117.         int r = (((argb)&0xFF) >> 2) << 2;
  118.         int g = (((argb>>8)&0xFF) >> 1) << 1;
  119.         int b = (((argb>>16)&0xFF) >> 2) << 2;
  120.        
  121.         int newR = r | rEnc;
  122.         int newG = g | gEnc;
  123.         int newB = b | bEnc;
  124.        
  125.         int newARGB = (newB << 16)|(newG << 8)|newR;
  126.        
  127.         img.setRGB(x, y, newARGB);
  128.     }
  129.    
  130.     public static char translateInt(int i){
  131.         if(i <= 25)return (char)(i+65);
  132.         switch(i){
  133.             case 26:
  134.                 return ' ';
  135.             case 27:
  136.                 return '.';
  137.             case 28:
  138.                 return ',';
  139.             case 29:
  140.                 return '!';
  141.             case 30:
  142.                 return '?';
  143.             case 31:
  144.                 return '\'';
  145.         }
  146.         return '#';
  147.     }
  148.    
  149.     public static int translateChar(char c){
  150.         if((int)c >= 65 && (int)c <=90){
  151.             return (int)c-65;
  152.         }
  153.         if((int)c >= 97 && (int)c <=122){
  154.             return (int)c-97;
  155.         }
  156.         switch(c){
  157.             case ' ':
  158.                 return 26;
  159.             case '.':
  160.                 return 27;
  161.             case ',':
  162.                 return 28;
  163.             case '!':
  164.                 return 29;
  165.             case '?':
  166.                 return 30;
  167.             case '\'':
  168.                 return 31;
  169.         }
  170.         return -1;
  171.     }
  172.    
  173. }
  174.  
  175.  
  176. //Group Class
  177. package com.ignatieff.tepng;
  178.  
  179. import java.math.BigInteger;
  180.  
  181. public class Group {
  182.     public int PRIME;
  183.    
  184.     public Group(int size, int hash){
  185.         int p = (int)Math.abs(hash % size);
  186.         int i=0;
  187.         BigInteger b = BigInteger.valueOf(p);
  188.         while(!b.isProbablePrime(10)){
  189.             int m = 1;++i;
  190.             if(i%2==0){m*=-1;}
  191.             b = BigInteger.valueOf(p+m*i);
  192.         }
  193.         PRIME = b.intValue();
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement