基于标准C,适用于小型嵌入式设备的PNG转RGB库,亲测可用

基于标准C,适用于小型嵌入式设备的PNG转RGB库,亲测可用

下载链接

GITHUB:http://lodev.org/lodepng/

C语言使用lodepng.c和lodepng.h这两个文件就可以完成png转rgb的操作

重要的api接口

/*需要提前从文件中读取文件内容*/
unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) {
  return lodepng_decode_memory(out, w, h, in, insize, LCT_RGB, 8);
}	//解码24位png图片
unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) {
  return lodepng_decode_memory(out, w, h, in, insize, LCT_RGBA, 8);
}	//解码32位png图片

/*api预置了从文件中读取内容的操作*/
unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) {
  return lodepng_decode_file(out, w, h, filename, LCT_RGB, 8);
}	//解码24位png图片
unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) {
  return lodepng_decode_file(out, w, h, filename, LCT_RGBA, 8);
}	//解码32位png图片

例子

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "lodepng.h"
#include <unistd.h>
#include <stdlib.h>

int main()
{

	unsigned char *png_data = NULL;

	int fd=open("./test.png",O_RDWR);
	size_t filesize=lseek(fd, 0,SEEK_END);
	lseek(fd, 0,SEEK_SET);
	
	printf( "test.png size:%ld ",filesize);
	png_data=(unsigned char *)malloc(filesize);
	memset(png_data, 0, filesize);
	read(fd, png_data, filesize);
	
	unsigned char *rgb_data = NULL;
    unsigned  width,height;	
    int ret = lodepng_decode_memory(&rgb_data, &width, &height, png_data, filesize, LCT_RGBA, 8);	//32位png图片,位深度8bit,转RGB888
    if (ret != 0) {
        printf( "lodepng decode failed %d\n",ret);
    }

	printf("width:%d heught:%d \n",width,height);

	int i;
	printf("png data:\n");
	for(i=0;i<filesize;i++){
		printf("%02X ",png_data[i]);
	}
	printf("\n");

	printf("rgb_data:\n");
	for(i=0;i<width*height;i++){
		printf("%02X ",rgb_data[i]);
	}
	printf("\n");
	
	close(fd);
	free(png_data);

}

执行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/QQ135102692/article/details/117223693