//Compress aBitmap into byte array public static BitStreamFIFO Compress(AccessibleBitmapBitwise source, int bitLayer) { //Initialize List <int> distances = new List <int>(); //A list containing all the lenghts of same bits int tempDistance = -1; //The length of one run of bits with the same value, while it is not saved yet: -1 becouse it will be increased before the first check bool lastVal = source.GetPixelBit(0, 0, bitLayer); //The bit value of the last checked pixel, to compare with the current pixel: set value to the value of the first pixel so the first check will succeed //Loop trough all lines of pixels for (int y = 0; y < source.height; y++) { //Loop trough all pixels in this line for (int x = 0; x < source.width; x++) { //Take value of the current pixel bool currentBool = source.GetPixelBit(x, y, bitLayer); //If the value of the bit of this pixel matches the value of the bit of the previous pixel if (currentBool == lastVal) { //Values are the same, so increase current run tempDistance++; } else { //Values are not the same, so save the run distances.Add(tempDistance); //Set the bit value for the next comparison to the bit value of this pixel lastVal = currentBool; //Reset the run length for the new run tempDistance = 0; } } } //Save the last run becouse this never happens in the loop distances.Add(tempDistance); //Save the bit value of the first pixel, because the decompressor needs to know this bool initialVal = source.GetPixelBit(0, 0, bitLayer); //Compress the array of run lengths using different techniques BitStreamFIFO bitStream = VaryingIntArrayCompressor.Compress(distances.ToArray()); //Combine the inititial bit value with the compressed data of the runs, then return the BitStream return(BitStreamFIFO.Merge(new BitStreamFIFO(new bool[] { initialVal }), bitStream)); }
//Compress aBitmap into byte array public static BitStreamFIFO Compress(AccessibleBitmapBitwise source, int bitLayer) { //Initialize vars BitStreamFIFO bitStream = new BitStreamFIFO(); List <int> distances = new List <int>(); int tempDistance = -1; bool lastVal = source.GetPixelBit(0, 0, bitLayer); //Iterate trough pixels for (int y = 0; y < source.height; y++) { for (int x = 0; x < source.width; x++) { //Take value of pixel & compare with previous value bool currentBool = source.GetPixelBit(x, y, bitLayer); if (currentBool == lastVal) { //Values are the same, so increase current run tempDistance++; } else { //Values are not the same, so save the run and create a new one distances.Add(tempDistance); lastVal = currentBool; tempDistance = 0; } } } //Save the last run becouse this never happens in the loop distances.Add(tempDistance); bool initialVal = source.GetPixelBit(0, 0, bitLayer); bitStream.Write(initialVal); BitStreamFIFO output = BitStreamFIFO.Merge(bitStream, VaryingIntArrayCompressor.Compress(distances.ToArray())); return(output); }
//Compress aBitmap into byte array public static BitStreamFIFO Compress(AccessibleBitmapBitwise source, int bitLayer) { //Initialize List <int> ints = new List <int>(); //List of all integers generated from the bits of this channel bool[] tmpBools = new bool[8]; //The collection of bits that will be converted to an interger int index = 0; //The index in tmpBools where a new bit should be inserted //Loop trough all lines of pixels for (int y = 0; y < source.height; y++) { //Loop trough all pixels in this line for (int x = 0; x < source.width; x++) { //Write bit of current pixel to the correct position in tmpBools tmpBools[index] = source.GetPixelBit(x, y, bitLayer); //Increase index for next bit index++; //If index is 8, tmpBools is full if (index == 8) { //Set index to 0, so the next bit will be written to the start index = 0; //Convert tmpBools to an integer & add the result to the list of integers ints.Add(new BitStreamFIFO().BoolArrayToInt(tmpBools)); } } } //If index is not 0, it has not been reset at last, so tmpBools should be saved for the last bits it contains if (index > 0) { //Convert tmpBools to an integer & add the result to the list of integers ints.Add(new BitStreamFIFO().BoolArrayToInt(tmpBools)); } //Compress the obtained array of integers using different techniques BitStreamFIFO bitStream = VaryingIntArrayCompressor.Compress(ints.ToArray()); //Return the output array return(bitStream); }
//Compress aBitmap into byte array public static BitStreamFIFO Compress(AccessibleBitmapBitwise source, int bitLayer) { //Creates a new BitStreamFIFO object where all bits will be written to BitStreamFIFO bitStream = new BitStreamFIFO(); //Loop trough all lines of pixels for (int y = 0; y < source.height; y++) { //Loop trough all pixels in this line for (int x = 0; x < source.width; x++) { //Write the bit of this channel from the current pixel to the output BitStream bitStream.Write(source.GetPixelBit(x, y, bitLayer)); } } //Return the BitStream return(bitStream); }