该程序我正在创建一个隐写术程序,通过将随机红色像素值更改为 ascii 字符来隐藏 .ppm 图像中的秘密消息。该程序基于 stackoverflow 上用于读取和写入 ppm 图像的代码(读取 PPM 文件并将其存储在数组中;用 C 编码),所有其他代码都是我自己的工作。我已经完成了所有必要的功能,例如写入、读取、编码和解码文件,但我正在努力掌握 fwrite 功能。
当前,当程序对图像进行编码时,它会在 .ppm 中将其转换为结构中的 rgb 值。然后它通过将红色值编辑为 ascii 字符来隐藏秘密消息。将图像“打印”到文件时会出现问题。程序完成后,生成的图像大约是应打印图像的 90%。示例如下: 未完成图像示例
我已经检查过它是否通过打印所有 rgb 值来存储所有值都被正确存储,并且确实如此。(使用 showPPM 方法)。是否没有足够的内存来写入图像?写入功能的图像是否过大?这些是我的猜测。
关于我应该如何更改 writePPM 函数以便我将 100% 的图像正确打印到文件的任何信息都会很棒。
这是下面的代码:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
typedef struct {
unsigned char red,green,blue;
} PPMPixel;
typedef struct {
int x, y;
PPMPixel *data;
} PPMImage;
void writePPM(PPMImage *img);
static PPMImage *getPPM(const char *filename)
{
char buff[16];
PPMImage *img;
FILE *fp;
int c, rgb_comp_color;
//open PPM …Run Code Online (Sandbox Code Playgroud) 到目前为止,我可以读取每一行并将其打印到控制台:
void readFile(){
string line;
ifstream myfile("example1.pgm");
if (myfile.is_open()){
while (myfile.good()){
getline (myfile,line);
cout << line;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,pgm文件显然在数据开始之前总是有以下内容:
P2
# test.pgm
24 7
15
Run Code Online (Sandbox Code Playgroud)
我如何调整我的代码,以便检查"P2"是否存在,忽略任何注释(#),并存储变量和后续像素数据?
我有点失落,对c ++很陌生,所以任何帮助都是有用的.
谢谢