1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// A simple structure to store ARGB data
typedef struct {
    unsigned char a; // alpha channel
    unsigned char r; // red channel
    unsigned char g; // green channel
    unsigned char b; // blue channel
} argb_t;

// A simple function to print ARGB data in hexadecimal format
void print_argb(argb_t *data, size_t size) {
    for (size_t i = 0; i < size; i++) {
        printf("%02X%02X%02X%02X ", data[i].a, data[i].r, data[i].g, data[i].b);
        if ((i + 1) % 10 == 0) printf("\n");
    }
}

// A simple function to compress ARGB data using run-length encoding (RLE)
// RLE is a lossless compression algorithm that replaces consecutive identical bytes with a count and a byte value
// For example: FFFFFFFF FFFFFFFF FFFFFFFF -> 03 FF
// The compressed data is stored in a buffer and its size is returned as an output parameter
unsigned char *compress_argb(argb_t *data, size_t size, size_t *out_size) {
    // Allocate a buffer to store the compressed data
    // The worst case scenario is that the compressed data is twice as large as the original data
    unsigned char *buffer = malloc(size * 2);
    if (buffer == NULL) return NULL;

    // Initialize some variables for the compression loop
    size_t index = 0; // the index of the current byte in the original data
    size_t count = 0; // the count of consecutive identical bytes in the original data
    unsigned char value = 0; // the value of the current byte in the original data

    // Loop through the original data and compress it using RLE
    while (index < size * sizeof(argb_t)) {
        // Get the current byte value from the original data
        value = ((unsigned char *)data)[index];

        // Count how many consecutive identical bytes there are from this position onwards
        count = 1;
        while (index + count < size * sizeof(argb_t) && ((unsigned char *)data)[index + count] == value) {
            count++;
        }

        // Write the count and the value to the buffer as two bytes
        buffer[*out_size] = (unsigned char)count;
        buffer[*out_size + 1] = value;

        // Increment the output size by two bytes
        *out_size += 2;

        // Increment the index by the count of identical bytes
        index += count;
    }

    return buffer;
}

// A simple function to decompress ARGB data using run-length encoding (RLE)
// RLE is a lossless compression algorithm that replaces a count and a byte value with consecutive identical bytes
// For example: 03 FF -> FFFFFFFF FFFFFFFF FFFFFFFF
// The decompressed data is stored in a buffer and its size is returned as an output parameter
argb_t *decompress_argb(unsigned char *data, size_t size, size_t *out_size) {
   

   // Allocate a buffer to store the decompressed data
   // The worst case scenario is that the decompressed data is half as large as the compressed data
   argb_t *buffer = malloc(size / 2);
   if (buffer == NULL) return NULL;

   // Initialize some variables for the decompression loop
   size_t index = 0; //the index of the current byte in the compressed data
   size_t count = 0; //the count of consecutive identical bytes in the decompressed data
   unsigned char value = 0; //the value of each identical byte in the decompressed data
// Loop through the compressed data and decompress it using RLE
   while (index < size) {
       // Get the count and the value from the compressed data as two bytes
       count = (size_t)data[index];
       value = data[index + 1];

       // Write the count of identical bytes to the buffer with the same value
       memset(((unsigned char *)buffer) + *out_size, value, count);

       // Increment the output size by the count of identical bytes
       *out_size += count;

       // Increment the index by two bytes
       index += 2;
   }

   return buffer;
}

// A simple function to test the compression and decompression functions
void test_argb() {
    // Create some sample ARGB data
    argb_t sample[] = {
        {0xFF, 0xFF, 0xFF, 0xFF}, // white
        {0xFF, 0x00, 0x00, 0x00}, // black
        {0xFF, 0xFF, 0x00, 0x00}, // red
        {0xFF, 0x00, 0xFF, 0x00}, // green
        {0xFF, 0x00, 0x00, 0xFF}, // blue
        {0xFF, 0xC8, 0xC8, 0xC8}, // gray
        {0xFF, 0xC8, 0xC8, 0xC8}, // gray
        {0xFF, 0xC8, 0xC8, 0xC8}, // gray
        {0xFF, 0x00, 0x00, 0xFF}, // blue
        {0xFF, 0x00, 0x00, 0xFF}, // blue
        {0xFF, 0x00, 0x00, 0xFF}, // blue
    };

    // Print the original data
    printf("Original data:\n");
    print_argb(sample, sizeof(sample) / sizeof(argb_t));
    printf("\n");

    // Compress the data using RLE
    size_t compressed_size = 0;
    unsigned char *compressed_data = compress_argb(sample,
                                                   sizeof(sample) / sizeof(argb_t),
                                                   &compressed_size);

    // Print the compressed data in hexadecimal format
    printf("Compressed data:\n");
    for (size_t i = 0; i < compressed_size; i++) {
        printf("%02X ", compressed_data[i]);
        if ((i + 1) % 10 == 0) printf("\n");
    }
    printf("\n");

    // Decompress the data using RLE
    size_t decompressed_size = 0;
argb_t *decompressed_data = decompress_argb(compressed_data,
                                                 compressed_size,
                                                 &decompressed_size);

    // Print the decompressed data
    printf("Decompressed data:\n");
    print_argb(decompressed_data, decompressed_size / sizeof(argb_t));
    printf("\n");

    // Free the allocated memory
    free(compressed_data);
    free(decompressed_data);
}

// The main function to run the test
int main() {
    test_argb();
    return 0;
}
posted @ 2023-03-14 11:13  LhTian21  阅读(19)  评论(0)    收藏  举报