//Compress aBitmap into byte array public static byte[] Compress(AccessibleBitmapBytewise source, int byteLayer) { //Initialize List <int> distances = new List <int>(); //A list containing all the lenghts of same pixels List <int> pixels = new List <int>(); //A list containing all the pixels that correspond to the lengths in 'distances' list 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 byte lastPixel = source.GetPixelByte(0, 0, byteLayer); //The pixel 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 byte currentPixel = source.GetPixelByte(x, y, byteLayer); //If the value of the bit of this pixel matches the value of the bit of the previous pixel if (currentPixel == lastPixel) { //Values are the same, so increase current run tempDistance++; } else { //Values are not the same, so save the run distances.Add(tempDistance); pixels.Add(lastPixel); //Set the bit value for the next comparison to the bit value of this pixel lastPixel = currentPixel; //Reset the run length for the new run tempDistance = 0; } } } //Save the last run becouse this never happens in the loop distances.Add(tempDistance); pixels.Add(lastPixel); //Compress the array of run lengths using different techniques BitStreamFIFO pixelStream = VaryingIntArrayCompressor.Compress(pixels.ToArray()); //Compress the array of run lengths using different techniques BitStreamFIFO lengthStream = VaryingIntArrayCompressor.Compress(distances.ToArray()); //Combine the compressed data of the runs with the compressed data of the pixel values, then return the BitStream return(BitStreamFIFO.Merge(pixelStream, lengthStream).ToByteArray()); }
//Compress aBitmap into byte array public static byte[] Compress(AccessibleBitmapBytewise source, int byteLayer) { //Create byte array with a size equal to the amount of pixels in the image byte[] output = new byte[source.width * source.height]; //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 byte of this channel from the current pixel to the correct position in the output array output[y * source.width + x] = source.GetPixelByte(x, y, byteLayer); } } //Return all bytes of this channel return(output); }
//Compress aBitmap into byte array public static byte[] Compress(AccessibleBitmapBytewise source, int byteLayer) { //Create int array with a size equal to the amount of pixels in the image int[] layerByteArray = new int[source.width * source.height]; //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 byte of this channel from the current pixel to the correct position in the output array layerByteArray[y * source.width + x] = source.GetPixelByte(x, y, byteLayer); } } //Compress the obtained array of integers using different techniques byte[] outputArray = VaryingIntArrayCompressor.Compress(layerByteArray).ToByteArray(); //Return the output array return(outputArray); }