我们都知道,我们是通过framebuffer来显示图片的,但是我们有不能直接把png图片显示到framebuffer,因为framebuffer是不能直接识别png图片的。
我们如何把一个1.png图片转换为framebuffer需要的格式呢?
1. 了解framebuffer的大小
# cat /sys/class/graphics/fb0/window_axis
0 0 1919 1079
我们查看fb0是1920*1080的大小
2. 查看需要显示的图片的大小及位数
32 bit的 500 x 333 的图片
3. 把图片转换为framebuffer的格式
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "png.h"
static int open_png(const char* name,
png_structp* png_ptr,
png_infop* info_ptr,
png_uint_32* width,
png_uint_32* height,
png_byte* channels){
unsigned char header[8];
size_t bytesRead;
int result = 0;
FILE* fp = NULL;
//png_structp png_ptr = NULL;
//png_infop info_ptr = NULL;
int color_type, bit_depth;
//png_uint_32 width;
//png_uint_32 height;
//png_byte channels;
fp = fopen(name, "rb");
if (fp == NULL) {
result = -1;
goto exit;
}
bytesRead = fread(header, 1, sizeof(header), fp);
if (bytesRead != sizeof(header)) {
result = -2;
goto exit;
}
if (png_sig_cmp(header, 0, sizeof(header))) {
result = -3;
goto exit;
}
*png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!*png_ptr) {
result = -4;
goto exit;
}
*info_ptr = png_create_info_struct(*png_ptr);
if (!*info_ptr) {
result = -5;
goto exit;
}
if (setjmp(png_jmpbuf(*png_ptr))) {
result = -6;
goto exit;
}
png_init_io(*png_ptr, fp);
png_set_sig_bytes(*png_ptr, sizeof(header));
png_read_info(*png_ptr, *info_ptr);
png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
&color_type, NULL, NULL, NULL);
*channels = png_get_channels(*png_ptr, *info_ptr);
printf("pnginfo bit_depth:%d, channels:%d, color_type:%d\n", bit_depth, *channels, color_type);
return result;
exit:
if (fp != NULL) {
fclose(fp);
fp = NULL;
}
return result;
}
// Copy 'input_row' to 'output_row', transforming it to the
// framebuffer pixel format. The input format depends on the value of
// 'channels':
//
// 1 - input is 8-bit grayscale
// 3 - input is 24-bit RGB
// 4 - input is 32-bit RGBA/RGBX
//
// 'width' is the number of pixels in the row.
static void transform_rgb_to_draw(unsigned char* input_row,
unsigned char* output_row,
int channels, int width) {
int x;
unsigned char* ip = input_row;
unsigned char* op = output_row;
switch (channels) {
case 1:
// expand gray level to RGBX
for (x = 0; x < width; ++x) {
*op++ = *ip;
*op++ = *ip;
*op++ = *ip;
*op++ = 0xff;
ip++;
}
break;
case 3:
// expand RGBA to RGBX
for (x = 0; x < width; ++x) {
*op++ = *ip++;
*op++ = *ip++;
*op++ = *ip++;
*op++ = 0xff;
}
break;
case 4:
// copy RGBA to RGBX
memcpy(output_row, input_row, width*4);
break;
}
}
int main(int argc ,char **argv) {
int result = 0;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_uint_32 width, height;
png_byte channels;
// 获取png图片的宽 高等属性
result = open_png("./1.png", &png_ptr, &info_ptr, &width, &height, &channels);
if (result < 0) return result;
printf("width:%d height:%d\n", width, height);
//申请存放图片的内存大小
unsigned char *data = (unsigned char *)malloc(width*height*4);
if (data == NULL) return -1;
png_set_bgr(png_ptr);
int y = 0;
unsigned char *p_row = (unsigned char*)(malloc(width * 4));
//把每一行数据读取出来,并转换为rgb值
for (y = 0; y < height; ++y) {
png_read_row(png_ptr, p_row, NULL);
transform_rgb_to_draw(p_row, data + y * (width * 4), channels, width);
}
free(p_row);
//free(data);
//申请framebuffer大小的内存
unsigned char *data_out = (unsigned char *)malloc(1920*1080*4);
if (data_out == NULL) return -1;
memset(data_out, 0, 1920*1080*4);
unsigned char* src_p = data;
// 显示到坐标(300,400)的地址处
unsigned char* dst_p = data_out + 300*(1920*4) + 400*(4);
int i;
int j;
int h;
int w;
h = height;
w = width;
//透明度处理
for (i = 0; i < h; ++i) {
if (4 == 4) {
unsigned char *p0 = src_p;
unsigned char *p1 = dst_p;
unsigned char alpha = 255;
for (j = 0; j < w; j++) {
alpha = *(p0+3);
*p1 = (*p1 * (255-alpha) + *p0++ * alpha) / 255;
++p1;
*p1 = (*p1 * (255-alpha) + *p0++ * alpha) / 255;
++p1;
*p1 = (*p1 * (255-alpha) + *p0++ * alpha) / 255;
++p1;
*p1++ = 255;
++p0;
}
} else {
//memcpy(dst_p, src_p, w * source->pixel_bytes);
}
src_p += width*4;
dst_p += 1920*4;
}
free(data);
//存储为本地文件
FILE *p_o = fopen("./fb0.img", "wb+");
if (p_o == NULL) {
printf("open out file failed!\n");
return -1;
}
int len = fwrite(data_out, 1, 1920*1080*4, p_o);
fclose(p_o);
free(data_out);
return 0;
}
我们把转换后的数据存储为本地文件fb0.img
4. 我们在界面显示1.png
扫描二维码关注公众号,回复:
5516163 查看本文章
直接在串口下:busybox dd if=/udisk/fb0.img of=/dev/graphics/fb0, 即可在界面显示1.png图片了