从png文件,读取linux fb显示数据

png文件包含透明度数据。
以下函数可以从png文件,读取ARGB数据、图像宽、高,用来做linux fb显示数据:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>

#include "png.h"


/*****************************************************************************
 *
 * Macro definition
 *
 *****************************************************************************/


/*****************************************************************************
 *
 * Structure/Class Definition
 *
 *****************************************************************************/
//
typedef struct Png2FbData {
    int             width;
    int             height;
    uint32_t        *buf;
} Png2FbData;

/*****************************************************************************
 *
 * Data definition
 *
 *****************************************************************************/

/*****************************************************************************
 *
 * Function Entity
 *
 *****************************************************************************/

int GetPngData(const char *file_name, Png2FbData *png_data) {

    int         i;
    int         j;
    int         w;
    int         h;
    FILE        *file


    file = fopen(file_name, "rb");
    if (file == NULL) {
        printf(" %s open failed\r\n", file_name);
        return -1;
    }
    printf("%s open OK\r\n", file_name);

    //
    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
    png_infop info_ptr = png_create_info_struct(png_ptr);

    if (setjmp(png_jmpbuf(png_ptr))) {
        png_destroy_read_struct(&png_ptr, 0, 0);
        fclose(file);
        return -1;
    }

    //
    png_init_io(png_ptr, file);
    png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, 0);

    int m_width = png_get_image_width(png_ptr, info_ptr);
    int m_height = png_get_image_height(png_ptr, info_ptr);

    png_bytep *row_pointers = png_get_rows(png_ptr, info_ptr);

    //
    png_data->buf = (uint32_t *)malloc(m_height * m_width * sizeof(uint32_t));
    if (NULL == png_data->buf) {
        png_destroy_read_struct(&png_ptr, &info_ptr, 0);
        fclose(file);
        printf("malloc err\r\n");
        return -1;
    }

    //
    for (i = 0; i < m_height; i++) {
        for (j = 0, h = 0; j < (4 * m_width); j += 4) {
            // row_pointers[i][j + 2];      // blue
            // row_pointers[i][j + 1];      // green
            // row_pointers[i][j];          // red
            // row_pointers[i][j + 3];      // alpha
            png_data->buf[i * m_width + h] = (row_pointers[i][j + 3] << 24) | (row_pointers[i][j] << 16) | 
                                             (row_pointers[i][j + 1] << 8) | (row_pointers[i][j + 2]);
            h ++;
        }
    }
    png_data->height = m_height;
    png_data->width = m_width;

    //
    png_destroy_read_struct(&png_ptr, &info_ptr, 0);
    fclose(file);

    printf("%s load finish, width = %d, height = %d\n", file_name, png_data->width, png_data->height);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值