You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将GIMP导出的RGBA8888格式C数组转换为ARGB8888格式

RGBA8888 转 ARGB8888 转换方案

核心转换逻辑

每组4字节为一个像素单元,仅需交换首字节与末字节即可完成格式转换:

// 伪代码
for i from 0 to array_length step 4:
    swap(array[i], array[i+3])

多语言实现

C/C++ 实现(直接处理数组)

适合在PC端预转换生成新的C数组文件,避免MCU端耗时操作:

#include <stdio.h>
#include <stdint.h>

#define IMAGE_SIZE 307200

void convertRGBAtoARGB(uint8_t *dest, const uint8_t *src, size_t size) {
    for (size_t i = 0; i < size; i += 4) {
        dest[i] = src[i+3];    // 原RGBA的A字节移到ARGB的首位置
        dest[i+1] = src[i+1];  // G字节保持不变
        dest[i+2] = src[i+2];  // B字节保持不变
        dest[i+3] = src[i];    // 原RGBA的R字节移到ARGB的末位置
    }
}

// 示例:从Flash数组转换到SRAM帧缓冲,或生成新的C数组文件
int main() {
    extern const uint8_t displayImage[IMAGE_SIZE];
    uint8_t frameBuffer[IMAGE_SIZE];
    
    convertRGBAtoARGB(frameBuffer, displayImage, IMAGE_SIZE);
    
    // 将转换后的数据写入新头文件,直接替换原displayImage
    FILE *fp = fopen("converted_image.h", "w");
    if (fp) {
        fprintf(fp, "const uint8_t displayImage[%d] = {\n", IMAGE_SIZE);
        for (size_t i = 0; i < IMAGE_SIZE; i++) {
            fprintf(fp, "0x%02X, ", frameBuffer[i]);
            if ((i+1) % 16 == 0) fprintf(fp, "\n");
        }
        fprintf(fp, "};\n");
        fclose(fp);
    }
    return 0;
}

Python 实现(处理C源文件中的数组文本)

适合直接修改GIMP导出的C源文件:

def convert_c_array(input_path, output_path):
    with open(input_path, 'r') as f:
        content = f.read()
    
    # 提取数组中的十六进制值
    import re
    hex_values = re.findall(r'0x([0-9A-Fa-f]{2})', content)
    converted = []
    
    for i in range(0, len(hex_values), 4):
        # 交换第0和第3个字节
        converted.append(hex_values[i+3])
        converted.append(hex_values[i+1])
        converted.append(hex_values[i+2])
        converted.append(hex_values[i])
    
    # 生成新的C数组内容
    new_content = re.sub(
        r'(const uint8_t displayImage\[\d+\] = \{)[^}]+\};',
        lambda m: f"{m.group(1)}\n    " + ",\n    ".join([f"0x{v}" for v in converted]) + "\n};",
        content
    )
    
    with open(output_path, 'w') as f:
        f.write(new_content)

# 调用示例
convert_c_array("original_image.h", "converted_image.h")

Java 实现(二进制文件或数组处理)

import java.io.*;

public class RGBAtoARGBConverter {
    public static void main(String[] args) throws IOException {
        File inputFile = new File("original_image.bin");
        File outputFile = new File("converted_image.bin");
        
        try (InputStream in = new FileInputStream(inputFile);
             OutputStream out = new FileOutputStream(outputFile)) {
            
            byte[] buffer = new byte[4];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                if (bytesRead == 4) {
                    // 交换首末字节
                    byte temp = buffer[0];
                    buffer[0] = buffer[3];
                    buffer[3] = temp;
                    out.write(buffer);
                } else {
                    // 处理不完整的像素(理论上不会出现)
                    out.write(buffer, 0, bytesRead);
                }
            }
        }
    }
}

脚本实现

Linux Bash 脚本(处理二进制文件)

借助xxd和sed实现字节交换,适合批量处理:

#!/bin/bash
INPUT_FILE="original_image.bin"
OUTPUT_FILE="converted_image.bin"

# 将二进制转为十六进制文本,交换每组8个字符(对应4字节)的前2和后2字符,再转回二进制
xxd -p "$INPUT_FILE" | sed -E 's/([0-9a-f]{2})([0-9a-f]{4})([0-9a-f]{2})/\3\2\1/g' | xxd -r -p > "$OUTPUT_FILE"

Windows 批处理(调用PowerShell实现)

批处理本身难以处理二进制,通过PowerShell完成核心转换:

@echo off
set INPUT_FILE=original_image.bin
set OUTPUT_FILE=converted_image.bin

powershell -Command "$bytes = [System.IO.File]::ReadAllBytes('%INPUT_FILE%'); for ($i=0; $i -lt $bytes.Length; $i+=4) { $temp = $bytes[$i]; $bytes[$i] = $bytes[$i+3]; $bytes[$i+3] = $temp }; [System.IO.File]::WriteAllBytes('%OUTPUT_FILE%', $bytes)"

额外提示

如果使用GIMP导出时,可尝试查找自定义导出插件,部分插件支持直接生成ARGB格式的C数组,省去后续转换步骤。

内容的提问来源于stack exchange,提问作者tiger19tony72

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 22:50:42