gpt4 book ai didi

c - 我应该如何使用 libpng 库将 RGBA 位图转换为 PNG 并将 PNG 转换回 RGBA 位图

转载 作者:行者123 更新时间:2023-11-30 20:50:44 25 4
gpt4 key购买 nike

我尝试使用 libpng 库,但遇到了问题。

我现在正在使用 CLion,并尝试在我的项目中包含这个 libpng 库。问题在于配置该库以在我的项目中使用它而不是使用该库。我认为我可以将 libpng 中的所有 *.c、*.h 文件复制到 CLion 中我自己的项目中,从中创建静态库 liblibpng.a 并链接到它。我什至编译了 liblibpng.a 但当我尝试使用它时,即简单地包括 ${path}/libpng-x.x.x/png.h 时,png.h 文件中会出现错误和警告,例如缺少 png_uint_32 定义。好吧,我可以像 unsigned int 一样对其进行类型定义。

我正在尝试阅读 README.txt 和其他手册/文档、INSTALL.txt?但是有很多操作系统的可能性,我只想在我的项目中包含 png 功能。为什么需要安装这个库呢?我希望我的应用程序在将来创建后能够轻松移植到其他计算机,而根本不需要安装第 3 方库。

我的问题是很容易开始使用这个 libpng,也可能是我自己的 CMake 项目中的 zlib 库,我更喜欢静态库链接,我不需要任何特定的安装,等等。

我认为这个声明有问题,因为这不是定义 png_uint_32 只是抛出错误!它来自 pngconf.h

#if UINT_MAX > 4294967294
typedef unsigned int png_uint_32;
#elif ULONG_MAX > 4294967294
typedef unsigned long int png_uint_32;
#else
# error "libpng requires an unsigned 32-bit (or more) type"
#endif

最佳答案

简短的回答,只需使用lodepng。它比 libpng 使用起来简单得多,并且来自单一源,因此您不必费力地链接和设置包含路径。

长答案

/*
write an rgba image to a memory buffer in PNG format, without any fanciness.

Params: rgba - the rgba values
width - image width
height - image height
outsize - return for size of output buffer
Returns: pointer to allocated buffer holding png data
*/
void *makePNGBuffer(unsigned char *rgba, int width, int height, size_t *outsize)
{
int code = 0;
// FILE *fp;
png_structp png_ptr = 0;
png_infop info_ptr =0;
png_bytep row = 0;

struct mem_encode state = {0, 0};

*outsize = 0;

// Initialize write structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fprintf(stderr, "Could not allocate write struct\n");
code = 1;
goto finalise;
}

// Initialize info structure
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fprintf(stderr, "Could not allocate info struct\n");
code = 1;
goto finalise;
}

// Setup Exception handling
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "Error during png creation\n");
code = 1;
goto finalise;
}

// png_init_io(png_ptr, fp);

/* if my_png_flush() is not needed, change the arg to NULL */
png_set_write_fn(png_ptr, &state, my_png_write_data, my_png_flush);

// Write header (8 bit colour depth)
png_set_IHDR(png_ptr, info_ptr, width, height,
8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

// Set title
/*
if (title != NULL) {
png_text title_text;
title_text.compression = PNG_TEXT_COMPRESSION_NONE;
title_text.key = "Title";
title_text.text = title;
png_set_text(png_ptr, info_ptr, &title_text, 1);
}
*/

png_write_info(png_ptr, info_ptr);

// Allocate memory for one row (3 bytes per pixel - RGB)
row = (png_bytep) malloc(4 * width * sizeof(png_byte));

// Write image data
int x, y;
for (y=0 ; y<height ; y++) {
for (x=0 ; x<width ; x++) {
// setRGB(&(row[x*3]), buffer[y*width + x]);
row[x*4] = rgba[(y*width +x)*4];
row[x*4+1] = rgba[(y*width +x)*4+1];
row[x*4+2] = rgba[(y*width +x)*4+2];
row[x*4+3] = rgba[(y*width +x)*4+3];
}
png_write_row(png_ptr, row);
}

// End write
png_write_end(png_ptr, NULL);

finalise:
// if (fp != NULL) fclose(fp);
if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
if (row != NULL) free(row);

*outsize = state.size;
return state.buffer;

}

关于c - 我应该如何使用 libpng 库将 RGBA 位图转换为 PNG 并将 PNG 转换回 RGBA 位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39815996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com