- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一种解码某些 png 文件的方法,我听说过 libpng,但我不明白它是如何工作的。它将 png 文件转换为 ARGB8888 格式的字节数组还是其他格式?
最佳答案
可运行示例
此示例读取现有 PNG,对其进行修改,然后将修改后的版本写入磁盘。
修改部分是在原始字节上完成的。
用法:
./a.out [<old-png> [<new-png>]]
old-png
默认为 a.png
new-png
默认为 b.png
在 Ubuntu 18.04、libpng 1.6.34 上测试,编译为:
gcc -std=c99 main.c -lpng
改编自:https://gist.github.com/niw/5963798
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <png.h>
unsigned int width;
unsigned int height;
png_bytep *row_pointers;
static void read_png_file(char *filename) {
FILE *fp = fopen(filename, "rb");
png_byte bit_depth;
png_byte color_type;
unsigned int y;
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png) abort();
png_infop info = png_create_info_struct(png);
if (!info) abort();
if (setjmp(png_jmpbuf(png))) abort();
png_init_io(png, fp);
png_read_info(png, info);
width = png_get_image_width(png, info);
height = png_get_image_height(png, info);
color_type = png_get_color_type(png, info);
bit_depth = png_get_bit_depth(png, info);
/* Read any color_type into 8bit depth, RGBA format. */
/* See http://www.libpng.org/pub/png/libpng-manual.txt */
if (bit_depth == 16)
png_set_strip_16(png);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png);
/* PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth. */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png);
if (png_get_valid(png, info, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png);
/* These color_type don't have an alpha channel then fill it with 0xff. */
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_PALETTE)
png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
png_read_update_info(png, info);
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,info));
}
png_read_image(png, row_pointers);
fclose(fp);
}
static void write_png_file(char *filename) {
unsigned int y;
FILE *fp = fopen(filename, "wb");
if (!fp) abort();
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png) abort();
png_infop info = png_create_info_struct(png);
if (!info) abort();
if (setjmp(png_jmpbuf(png))) abort();
png_init_io(png, fp);
png_set_IHDR(
png,
info,
width,
height,
8,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
png_write_info(png, info);
/* To remove the alpha channel for PNG_COLOR_TYPE_RGB format, */
/* Use png_set_filler(). */
/*png_set_filler(png, 0, PNG_FILLER_AFTER);*/
png_write_image(png, row_pointers);
png_write_end(png, NULL);
for (y = 0; y < height; y++) {
free(row_pointers[y]);
}
free(row_pointers);
fclose(fp);
}
static void process_png(void) {
for (unsigned int y = 0; y < height; y++) {
png_bytep row = row_pointers[y];
for (unsigned int x = 0; x < width; x++) {
png_bytep px = &(row[x * 4]);
/*printf("%4d, %4d = RGBA(%3d, %3d, %3d, %3d)\n", x, y, px[0], px[1], px[2], px[3]);*/
png_byte old[4 * sizeof(png_byte)];
memcpy(old, px, sizeof(old));
px[0] = 255 - old[0];
px[1] = 255 - old[1];
px[2] = 255 - old[2];
}
}
}
int main(int argc, char *argv[]) {
char *in;
char *out;
if (argc > 1) {
in = argv[1];
} else {
in = "a.png";
}
if (argc > 2) {
out = argv[2];
} else {
out = "b.png";
}
read_png_file(in);
process_png();
write_png_file(out);
return EXIT_SUCCESS;
}
a.png
b.png
关于c - 如何使用 libpng 将 C 代码中的 png 图像解码为原始字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1362945/
我正在寻找一种经济合理的解决方案来长时间存储图片。我读到 PNG 文件格式,它与 JPEG 相比具有优越的特性,即在这些类别中: 无专利、无许可、无版税 无质量损失 尚未压缩 我有很多来自 Photo
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
我怎么能用 FFMEG 做到这一点? 输入 : 背景图片:background.png 图片帧列表:image_001.png,image_002.png ...(每张图片为一帧) 输出:所有帧都有背
$ cat png.ll ./packages/apps/Mms/res/mipmap-hdpi/ic_launcher_smsmms.png ./packages/apps/Mms/res/draw
这个问题在这里已经有了答案: Natural Sort Order in C# (18 个答案) 关闭 7 年前。 这是我的代码: private void Method1() { int
我一直在考虑用 Webp 图像替换我的 Android 应用程序中的 png 文件以减小 APK 大小。 虽然结果不错,但我想知道我是否使用了一些 png 转换器/压缩器,并且能够将尺寸减小到比我为
在 gnuplot-4.2.6 中,我可以使用 set term png medium x000000 xffffff set output 'file.png' plot x 这将生成一个带有黑色背
背景: 我正在努力使一堆 PNG 尽可能小。我正在使用诸如 PngOut、PngCrush 和 OptiPng 之类的工具。 问题: 我遇到了一个大小为 1434 KB 但只有 230 x 230 像
我正在使用 ImageMagick 调整图像大小。如果我传递 -resize WxH 选项,它会按预期运行。但是如果我通过 -resize WxH! (在调整大小时忽略纵横比),一些图像,尤其是 PN
如何访问/删除 PNG 元数据? 我正在寻找 Mac 应用程序或 PHP 代码段。 最佳答案 抱歉发布了一个 Windows 软件,但如果你没有找到任何对 MAC 有用的东西,那就是 TweakPNG
到目前为止似乎没有任何效果。我看到了 pnginfo以下消息: concept_Sjet_dream6.png... Image Width: 200 Image Length: 240 Bi
我有一个带有 Alpha channel (即透明度)的 PNG 图像,我需要创建将图像层合成到白色背景上的版本。我想使用可编写脚本的命令,使用 CLI 工具(例如 Image Magick)将 PN
我是初学者。我昨天问了一个类似的问题,但不知何故被否决了。所以这次我尽量简化问题。 带有 alpha png 的 24 位与 32 位 png 相同吗? 非常感谢您的一些提示。 最佳答案 没有“24
我有这个带点的荷兰 pdf 图像: pdf image of the netherlands with dots 当我尝试将此 pdf 转换为 png 图像时,使用 pdftools和 png像这样:
我在我的启动图像通用项目中添加了“Default.png,Default-568h@2x.png,Default@2x.png”这三个文件,我有三个不同的图像,分辨率与苹果中提到的完全相同文档,适用于
我在 Python 中使用 google app engine 并有几个静态 .png 图像文件,但它们都以“image/x-png”内容类型提供。这是一个问题,当我使用像 chrome 这样的浏览器
我做了一个 python 脚本,该脚本设法根据特定模式解散乱序(png)图像,该 python 脚本使用 ffmpeg 并进行 12 次编码来解乱它(通过裁剪特定部分并将其粘贴到现有图片上)。 因此,
我有一个 PNG 图像文件。我想将其转换为 GeoTiff。我安装了 QGIS 软件,但无法使用它,也不知道如何对图像进行地理配准。请帮我。有没有在线软件? 最佳答案 这是一个非常好的教程,其中包含有
我有一堆使用我编写的 Java 图表工具创建的图表 - 它们主要是黑白图表,带有浅绿色的块,偶尔还有其他颜色。它们当前被保存为 JPG 文件,我想将它们插入到我准备按需打印的书中。 这本书是一个 Op
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我是一名优秀的程序员,十分优秀!