Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @(#)GraphicCompression.java
- *
- *
- * @author David Wan, Nick Tyson
- * @version 1.00 2011/8/23
- */
- public class GraphicCompression {
- String image = "1010100000111100001000000011110000000000001111000010000000011100000000100000001000111100";
- //101015B4A4B17B4A13B4A4B19B3A8B18B13B4A00
- // Empty string for compressed image
- String compressedImage = "";
- public GraphicCompression() {
- //Constructor calls the compress method and displays the compressed String
- compressedImage = compress(image);
- //Outputs the lengths of the original String and the compressed string
- System.out.printf("Original File Size : %2d bits : " + image + "\n", image.length() );
- System.out.printf("Comprssd File Size : %2d bits : " + compressedImage + "\n", compressedImage.length());
- String decompressed = decompress(compressedImage); // Decompressed image of compressed
- boolean passedTest = image.equals(decompressed); // Boolean variable to see if the decompression is right
- // Print if decompression/compression worked
- if (passedTest) {
- System.out.println("File compression and decompression successful");
- } else {
- System.out.println("Decompression FAILED! : " + decompressed);
- } // End if/else
- } // End GraphicCompression()
- private String compress(String s){
- //Method compresses the original into a new String that is of shorter length
- // Create String for new image
- String newImage = "";
- // String for output of the compressed image
- String compComp = "";
- // String to hold either "A" or "B"
- String letter = "";
- // Int for how many values counted
- int counter = 0;
- // Int for the number of the place of the counter
- int c = image.charAt(0);
- // Loop for compression
- for(int i=0;i<image.length();i++) {
- // If statement to advance counter
- if(c == image.charAt(i)){
- counter++;
- } else {
- if(c =='1') {
- // Assign "A" for the number 1
- letter = "A";
- } else {
- // Assign "B" for the number 0
- letter = "B";
- } // End if/else
- // Each set of number and letter
- String compressed = letter + counter;
- // Add compressed to the compressed file
- compComp += compressed;
- // Set counter back to 1 for next set
- counter = 1;
- } // End if/else
- // Set c to the number that i is occupying
- c = image.charAt(i);
- } // End for loop
- // Add B2 to the end of the file
- compComp += "B2";
- return compComp;
- } // End compress(String s)
Add Comment
Please, Sign In to add comment