我正在使用 raspberry dispmanx 开发一个简单的图形应用程序。 我的目标是通过 stdin(来自 RGBA 数组,格式化为字符串,来自 python 应用程序)获取 png 文件,将其转换为 dispmanx 期望的 IMAGE_T 格式以显示它。
现在,我只有一个可以从文件中获取 PNG 的功能,但是将图像写入磁盘、tmpfs 上的事件需要太多时间。
如何重写此函数以使用字符串作为输入? 我会单独管理这个,但我不知道如何从变量模拟文件以提供 png_ptr 和 info_ptr ...
#include <png.h>
#include <stdlib.h>
#include "bcm_host.h"
#include "loadpng.h"
//-------------------------------------------------------------------------
#ifndef ALIGN_TO_16
#define ALIGN_TO_16(x) ((x + 15) & ~15)
#endif
//-------------------------------------------------------------------------
bool
loadPngData(
IMAGE_T* image,
const char *file)
{
FILE* fpin = fopen(file, "rb");
if (fpin == NULL)
{
fprintf(stderr, "loadpng: can't open file for reading\n");
return false;
}
//---------------------------------------------------------------------
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
if (png_ptr == NULL)
{
fclose(fpin);
return false;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, 0, 0);
fclose(fpin);
return false;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
fclose(fpin);
return false;
}
//---------------------------------------------------------------------
png_init_io(png_ptr, imgdata);
png_read_info(png_ptr, info_ptr);
//---------------------------------------------------------------------
png_byte colour_type = png_get_color_type(png_ptr, info_ptr);
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
VC_IMAGE_TYPE_T type = VC_IMAGE_RGB888;
if (colour_type & PNG_COLOR_MASK_ALPHA)
{
type = VC_IMAGE_RGBA32;
}
initImage(image,
type,
png_get_image_width(png_ptr, info_ptr),
png_get_image_height(png_ptr, info_ptr),
false);
//---------------------------------------------------------------------
double gamma = 0.0;
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
{
png_set_gamma(png_ptr, 2.2, gamma);
}
//---------------------------------------------------------------------
if (colour_type == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(png_ptr);
}
if ((colour_type == PNG_COLOR_TYPE_GRAY) && (bit_depth < 8))
{
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(png_ptr);
}
if (bit_depth == 16)
{
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
png_set_scale_16(png_ptr);
#else
png_set_strip_16(png_ptr);
#endif
}
if (colour_type == PNG_COLOR_TYPE_GRAY ||
colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
{
png_set_gray_to_rgb(png_ptr);
}
//---------------------------------------------------------------------
png_read_update_info(png_ptr, info_ptr);
//---------------------------------------------------------------------
png_bytepp row_pointers = malloc(image->height * sizeof(png_bytep));
png_uint_32 j = 0;
for (j = 0 ; j < image->height ; ++j)
{
row_pointers[j] = image->buffer + (j * image->pitch);
}
//---------------------------------------------------------------------
png_read_image(png_ptr, row_pointers);
//---------------------------------------------------------------------
fclose(fpin);
free(row_pointers);
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
return true;
}
编辑:可以从 python 以原始 RGBA 或 png 格式(作为带有标题和 block 的字符串)提供图像。我想原始 RGBA 会更快(跳过 png 格式),但是使用 PNG 字符串修改这个函数可能更简单...... 欢迎任何线索!
最佳答案
如果您可以获得原始 RGBA 数据,则不需要此功能。此函数用于将压缩的 png 格式解码为原始 RGBA。您可能还需要提供的唯一内容是宽度和高度。
现在唯一的问题是什么会更有效率——将原始的、未压缩的数据发送到 C 程序或用 Python 将其编码为 PNG,发送编码数据并在 C 程序中解码。
这里最大的问题是 png_init_io(png_ptr, fpin);
(我在函数调用中将 imgData
更改为 fpin
,但是你可能想传递数组而不是 FILE
句柄)。在 section 5 中的 libpng
文档中有一个描述如何为输入/输出提供自己的功能。您必须将 png_init_io
替换为您自己采用 const char*
而不是 FILE*
的函数。这是 png_init_io
的一部分:
Input/Output in libpng is done through png_read() and png_write(), which currently just call fread() and fwrite(). The FILE * is stored in png_struct and is initialized via png_init_io(). If you wish to change the method of I/O, the library supplies callbacks that you can set through the function png_set_read_fn() and png_set_write_fn() at run time, instead of calling the png_init_io() function. These functions also provide a void pointer that can be retrieved via the function png_get_io_ptr(). For example:
png_set_read_fn(png_structp read_ptr, voidp read_io_ptr, png_rw_ptr read_data_fn) png_set_write_fn(png_structp write_ptr, voidp write_io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn); voidp read_io_ptr = png_get_io_ptr(read_ptr); voidp write_io_ptr = png_get_io_ptr(write_ptr);
The replacement I/O functions must have prototypes as follows:
void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length); void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length); void user_flush_data(png_structp png_ptr);
我从来没有操纵过这些函数,所以我不能在这里帮助你,但似乎你唯一需要改变的是跳过打开文件并从数组中读取与原始数据相同的数据 block 实现与文件有关。您必须阅读本节,可能会检查源代码以查看需要完成的操作,并考虑如果您有可能获得已解码的数据是否值得花时间。
关于c++ - 将字符串转换为 IMAGE_T,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43321271/