1.準備好png,zlib庫以及頭文件
頭文件
png.h pngconf.h zconf.h zlib.h
庫文件
libpng.so libz.so
2.編譯參數
arm-linux-gnueabi-gcc linux_imghw.c -o linux_imghw -lz -lpng -L./
3.實例
#include <stdio.h>
#include "png.h"
void tga_read(char *file_path)
{
char type_header[12] = {0};
char header[6] = {0};
int image_width;
int image_height;
FILE *of = fopen(file_path, "rb");
fread(type_header, sizeof(char), sizeof(type_header), of);
fread(header, sizeof(char), sizeof(header), of);
image_width = header[1] * 256 + header[0];
image_height = header[3] * 256 + header[2];
fclose(of);
printf("%d,w:%d,h:%d\n", image_width < image_height, image_width, image_height);
}
int png_read(char *file_path)
{
png_structp png_ptr;
png_infop info_ptr;
int color_type = 0;
int image_size = 0;
int image_width, image_height;
FILE *p_fp;
if ((p_fp = fopen(file_path, "rb")) == NULL)
{
fprintf(stderr, "%s open fail!!!\n", file_path);
return -1;
}
/* 創建一個png_structp結構體,如果不想自定義錯誤處理,後3個參數可以傳NULL */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
{
fclose(p_fp);
return -1;
}
/* 創建一個info_ptr,必須的 */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fclose(p_fp);
png_destroy_read_struct(&png_ptr, NULL, NULL);
return -1;
}
/* 如果上面png_create_read_struct沒有自定義錯誤處理,這裏是必須的 */
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(p_fp);
return -1;
}
/* 初始化文件IO */
png_init_io(png_ptr, p_fp);
png_set_sig_bytes(png_ptr, 0);
png_read_info(png_ptr, info_ptr);
/* 獲取圖像的寬高 */
image_width = png_get_image_width(png_ptr, info_ptr);
image_height = png_get_image_height(png_ptr, info_ptr);
printf("%d,w:%d,h:%d\n", image_width<image_height, image_width, image_height);
/* 清理圖像,釋放內存 */
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
fclose(p_fp);
return 0;
}
int main(int argc, char**argv) {
if(argc != 2) {
printf("input param error\n");
return 0;
}
if((strstr(argv[1], ".png") != NULL) || (strstr(argv[1], ".PNG") != NULL)) {
png_read(argv[1]);
} else if((strstr(argv[1], ".tga") != NULL) || (strstr(argv[1], ".TGA") != NULL)) {
tga_read(argv[1]);
} else {
printf("not support,only support png or tga\n");
}
return 0;
}
下載
https://download.csdn.net/download/zengzhihao/12471215