求助: C语言- 利用libpng读取png大小,并转换为jpg类型图片


大家好,

最近研究C语言图像处理这方面遇到一个难题,详情如下:

目标: 将png类型图片的信息读入内存,提取所需要的数据并保存,然后转换为如jpg等其它图片格式。

  1. libpng库已经装好,通过makefile调用库文件路径也正确。

  2. 写了一个读取函数, 如下:

   
  #include <stdio.h>
  
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <inttypes.h>

#include "png.h"

#include "pngio.h"
#include "error.h"
#include "image.h"


/*==============================================================================*
* read_png *
*==============================================================================*/

int x, y;

int width, height;

int channels;

png_byte color_type;
png_byte bit_depth;

png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep *row_pointers;



png_uint_32 w, h;
size_t npixels;
//png_uint_32* raster;
//png_uint_32




int read_png (char * input_filename, picture *thePicture, FILE *status_log)
{
int err = no_err;

char header[8]; // 8 is the maximum size that can be checked

/* open file and test for it being a png */
FILE *fp = fopen(input_filename, "rb");
if (fp){

//abort_("[read_png_file] File %s could not be opened for reading", file_name);

fread(header, 1, 8, fp);

/* checked out with a proper PNG signature */
if (png_check_sig(header, 8)){

/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

if (png_ptr){

info_ptr = png_create_info_struct(png_ptr);
if (info_ptr){


if (setjmp(png_jmpbuf(png_ptr))){

fprintf(status_log, "Error during init_io. \n");

}else{

//takes our file stream pointer(fp) and stores it in the png_ptr struct for later use.
png_init_io(png_ptr, fp);
//lets libpng know that we already checked the 8 signature bytes
png_set_sig_bytes(png_ptr, 8);

//information is stored in the information struct
png_read_info(png_ptr, info_ptr);

//png_read_update_info(png_ptr, info_ptr);

width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);

//w = png_get_image_width(png_ptr, info_ptr);
//h = png_get_image_height(png_ptr, info_ptr);

fprintf(status_log,"png-image-details: width=%i - height=%i\n",width,height);

//npixels = width * height;
npixels = width * height;


/* Read the whole image into memory at once. */

/* read file */
if (setjmp(png_jmpbuf(png_ptr)))

fprintf(status_log, "Error during read_image. \n");


png_uint_32 *raster = malloc(npixels * 4 *sizeof(png_uint_32));

//row_pointers = (png_bytep*) malloc(npixels * sizeof(png_bytep));
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);

for (y=0; y<height; y++)
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));

png_read_image(png_ptr, row_pointers);


if(raster != NULL){


channels = png_get_channels(png_ptr, info_ptr);
fprintf(status_log,"Bild-Kanal ist %d .\n", channels);

fprintf(status_log,"Bild ist geladen\n");

thePicture->pixelmap = row_pointers;
thePicture->width = width;
thePicture->height = height;
thePicture->type = png_type;


}else{

fprintf(status_log,"Memory error.\n");
err = mem_err;

}
fclose(fp);

}


}else{
fprintf(status_log, "png_create_info_struct failed. \n");
//err = mem_err;
}

}else{

fprintf(status_log, "png_create_read_struct failed. \n");
}


}else{

fprintf(status_log, "checking PNG failed. \n");

}

}else{

fprintf(status_log,"Error. Fail to open the png.\n");
err = file_err;

}


return err;

}
  1. 测试结果如下:

原png图片:
请输入图片描述

转换后jpg:
请输入图片描述

请问是什么原因造成这样的效果,代码有什么地方需要修改吗?

先谢谢了。

图像处理

C·诺亚方舟 11 years, 10 months ago

Your Answer