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;
}

__EOF__

本文作者LhTian
本文链接https://www.cnblogs.com/LhTian/p/17214275.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   LhTian21  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示