Guest User

Untitled

a guest
Nov 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. /**
  2. * @(#)GraphicCompression.java
  3. *
  4. *
  5. * @author David Wan, Nick Tyson
  6. * @version 1.00 2011/8/23
  7. */
  8.  
  9. public class GraphicCompression {
  10.  
  11. String image = "1010100000111100001000000011110000000000001111000010000000011100000000100000001000111100";
  12. //101015B4A4B17B4A13B4A4B19B3A8B18B13B4A00
  13. // Empty string for compressed image
  14. String compressedImage = "";
  15.  
  16. public GraphicCompression() {
  17. //Constructor calls the compress method and displays the compressed String
  18.  
  19. compressedImage = compress(image);
  20.  
  21. //Outputs the lengths of the original String and the compressed string
  22. System.out.printf("Original File Size : %2d bits : " + image + "\n", image.length() );
  23. System.out.printf("Comprssd File Size : %2d bits : " + compressedImage + "\n", compressedImage.length());
  24.  
  25. String decompressed = decompress(compressedImage); // Decompressed image of compressed
  26. boolean passedTest = image.equals(decompressed); // Boolean variable to see if the decompression is right
  27.  
  28. // Print if decompression/compression worked
  29. if (passedTest) {
  30. System.out.println("File compression and decompression successful");
  31. } else {
  32. System.out.println("Decompression FAILED! : " + decompressed);
  33. } // End if/else
  34. } // End GraphicCompression()
  35.  
  36.  
  37. private String compress(String s){
  38. //Method compresses the original into a new String that is of shorter length
  39.  
  40. // Create String for new image
  41. String newImage = "";
  42.  
  43. // String for output of the compressed image
  44. String compComp = "";
  45. // String to hold either "A" or "B"
  46. String letter = "";
  47.  
  48. // Int for how many values counted
  49. int counter = 0;
  50. // Int for the number of the place of the counter
  51. int c = image.charAt(0);
  52. // Loop for compression
  53. for(int i=0;i<image.length();i++) {
  54.  
  55. // If statement to advance counter
  56. if(c == image.charAt(i)){
  57. counter++;
  58. } else {
  59. if(c ==&#039;1&#039;) {
  60. // Assign "A" for the number 1
  61. letter = "A";
  62. } else {
  63. // Assign "B" for the number 0
  64. letter = "B";
  65. } // End if/else
  66. // Each set of number and letter
  67. String compressed = letter + counter;
  68. // Add compressed to the compressed file
  69. compComp += compressed;
  70. // Set counter back to 1 for next set
  71. counter = 1;
  72. } // End if/else
  73.  
  74. // Set c to the number that i is occupying
  75. c = image.charAt(i);
  76. } // End for loop
  77. // Add B2 to the end of the file
  78. compComp += "B2";
  79. return compComp;
  80. } // End compress(String s)
Add Comment
Please, Sign In to add comment