project 2 - For next time you need to ask the TAs a...

This preview shows page 1 - 3 out of 6 pages.

For next time you need to ask the TAs a question: import java.util.Scanner; public class RleProgram { //#1 // data to hex // useful for method 7 public static String toHexString(byte[] data) { String hex = ""; for (int i = 0; i < data.length; i++) { int num = data[i]; hex += Integer.toHexString(num); } return hex; } //#2 //Returns number of runs of data in an image data set //double this result for length of encoded (RLE) byte array // useful for method 3 public static int countRuns(byte[] flatData) { int runCount = 1; int repeat = 1; for (int i = 0; i < flatData.length - 1; i++) { if (flatData[i] != flatData[i + 1]) { runCount++; } else { repeat++; if (repeat > 15) { runCount++; repeat = 1; } } } return runCount; } //#3 //Returns encoding (in RLE) of the raw data passed in //used to generate RLE representation of a data // array[countRuns*2] (call method 2 in method 3) public static byte[] encodeRle(byte[] flatData) { byte[] array = new byte[2 * countRuns(flatData)]; int num = countRuns(flatData); int count = 0;
int count2 = 0; int temp = 0; int a = 0; for (int i = a; i < array.length; i += 2) { for (int j = a; j < flatData.length - 1; j++) { count++; count2++; temp = flatData[j]; if (count == 15) { break; } if (flatData[j] != flatData[j + 1]) { break; } if (j == flatData.length - 2) { count ++; count2 +=2; } } array[i] = (byte) count; array[i + 1] = (byte)temp; a = count2; count = 0; } return array; } //#4 //Returns decompressed size RLE data //used to generate flat data from RLE encoding (Counterpart to #2) // add all of the even index together // useful for method 5 public static int getDecodedLength(byte[] rleData) {

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture