pgm 是便携式灰度地图。如果我们想在 c 中将二维数组存储为 png、jpeg 或任何其他图像格式的图像,则在写入文件之前,我们必须做大量工作以某种指定的格式对数据进行编码。
Netpbm 格式提供了一种简单且便携的解决方案。 Netpbm是一个开源的图形程序包,基本上使用在linux或Unix平台上。它也可以在 Microsoft Windows 系统下运行。
每个文件都以两个字节的幻数开头。这个幻数用于识别文件的类型。类型有 PBM、PGM、PPM 等。它还标识编码(ASCII 或二进制)。幻数是大写的 P 后跟一个数字。
ASCII 编码允许人类可读并轻松传输到其他平台;二进制格式在文件大小方面更有效,但可能存在本机字节顺序问题。
如何编写 PGM 文件?
ZYCH自由策划企业网站管理系统是一个智能ASP网站管理程序,是基于自由策划企业网站系列的升级版,结合以往版本的功能优势,解决了频道模板不能自由添加删减的问题,系统开发代码编写工整,方便读懂,系统采用程序模板分离式开发。方便制作模板后台模板切换,模板采用动态编写,此模板方式写入快,代码编写自由,即能满足直接使用也能满足二次开发。全新的后台界面,不管是在程序的内部结构还是界面风格及CSS上都做了大量
1
#include <stdio.h>
main() {
int i, j;
int w = 13, h = 13;
// This 2D array will be converted into an image The size is 13 x 13
int image[13][13] = {
{ 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 },
{ 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31},
{ 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47},
{ 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63},
{ 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79},
{ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95 },
{ 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111},
{ 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127},
{ 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143},
{ 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159},
{ 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175},
{ 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191},
{ 207, 207, 207, 207, 207, 207, 207, 207, 207, 207, 207, 207, 207}
};
FILE* pgmimg;
pgmimg = fopen("my_pgmimg.pgm", "wb"); //write the file in binary mode
fprintf(pgmimg, "P2</p><p>"); // Writing Magic Number to the File
fprintf(pgmimg, "%d %d</p><p>", w, h); // Writing Width and Height into the
file
fprintf(pgmimg, "255</p><p>"); // Writing the maximum gray value
int count = 0;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
fprintf(pgmimg, "%d ", image[i][j]); //Copy gray value from
array to file
}
fprintf(pgmimg, "</p><p>");
}
fclose(pgmimg);
}PGM 图像如下所示

以上就是C程序以PGM格式写入图像的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号