C:将 RGB 值的结构写入文件以创建 ppm 图像 - 文件过早结束

C: Writing structs of RGB values to file to create ppm image - premature end of file

我正在创建一个隐写术程序,该程序通过将随机红色像素值更改为 ascii 字符来在 .ppm 图像中隐藏秘密消息。 该程序基于用于读写 ppm 图像 (read PPM file and store it in an array; coded with C) 的 Whosebug 上的代码,所有其他代码都是我自己的工作。我已经完成了所有必要的功能,如写入、读取、编码和解码文件,但我正在努力掌握 fwrite 功能。

目前,当程序对图像进行编码时,它会在 .ppm 中将其转换为结构中的 rgb 值。然后它通过将红色值编辑为 ascii 字符来隐藏秘密消息。当涉及到 "printing" 图像到文件时,问题就出现了。当程序完成时,生成的图像大约是应打印图像的 90%。示例如下: Example of the unfinished image

我已经检查过它正在存储所有值,并通过打印所有 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 file for reading
     fp = fopen(filename, "rb");
     if (!fp) {
          fprintf(stderr, "Unable to open file '%s'\n", filename);
          exit(1);
     }

     //read image format
     if (!fgets(buff, sizeof(buff), fp)) {
          perror(filename);
          exit(1);
     }

//check the image format
if (buff[0] != 'P' || buff[1] != '3') {
     fprintf(stderr, "Invalid image format (must be 'P3')\n");
     exit(1);
}else{
    printf("P3\n");
}

//alloc memory form image
img = (PPMImage *)malloc(sizeof(PPMImage));
if (!img) {
     fprintf(stderr, "Unable to allocate memory\n");
     exit(1);
}


   c = getc(fp);
   while (c == '#') {
   while (getc(fp) != '\n') ;
     c = getc(fp);

}
ungetc(c, fp);
//read image size information
if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
     fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
     exit(1);
}else{
    printf("Height: %d\n",img->x);
    printf("Width: %d\n",img->y);

}

//read rgb component
if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
     fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
     exit(1);
}else{
    printf("%d\n",rgb_comp_color );
}

//check rgb component depth
if (rgb_comp_color!= 255) {
     fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
     exit(1);
}

while (fgetc(fp) != '\n') ;
//memory allocation for pixel data
img->data = (PPMPixel*)malloc(24*img->x * img->y * sizeof(PPMPixel));

if (!img) {
     fprintf(stderr, "Unable to allocate memory\n");
     exit(1);
}

//read pixel data from file
if (fread(img->data, 10*img->x, img->y, fp) != img->y) {
     fprintf(stderr, "Error loading image '%s'\n", filename);
     exit(1);
}

fclose(fp);
return img;
}



struct PPMImage * encode(char * text, PPMImage * img)
{
    //convert secret message to ascii code

    int i,ascii,height,width;
    int total = 0;
    int rolling = 0;
    int original = 0;
    time_t t;
    srand((unsigned) time(&t));
    height=img->y;
    width=img->x;

    for(i = 0; text[i]; i++){

        ascii = text[i];

        //create random number between 0 and max the width
        total = total + rand() % width;
        original = total;
        //printf("Random Number: %d\n",total);

        if(total >= width){
            rolling = rolling + 1;
            total = total - width;
        }

        //printf("Before R: %d \n",img->data[0].red );
        img->x=rolling;
        img->y=total;

        printf("X: %d ",rolling );
        printf("Y: %d ",total );

        //set img position
        //at position random we set the red bit equal to ascii number 
        printf("Old R:  %d ",img->data[i].red );                    
        img->data[i].red=ascii; 
        printf("New R: %d\n ",img->data[i].red );   
    }

    //take img then print it out
    //setting the img values again for printing
    img->x=width;
    img->y=height;
    writePPM(img);

}

void writePPM(PPMImage *img)
{
FILE *fp;
//open file to be written
fp = fopen("encoded.ppm", "wb");
if (!fp) {
     fprintf(stderr, "Unable to open file \n");
     exit(1);
}

//image format
fprintf(fp, "P3\n");

//comments
//need to store comments to be outputted
fprintf(fp, "# Created by Sean \n");

//image size
fprintf(fp,"%d %d\n",img->x,img->y);

// rgb component depth
fprintf(fp, "%d\n",255);

//write pixels currently not fully working
fwrite(img->data, sizeof(img->data), 3*img->y*img->x, fp);

//close file stream
fclose(fp);
}

void showPPM(PPMImage *img)
{
    int i;
    if(img){

    for(i=-1;i<img->x*img->y;i++){
        printf("Number: %d\n",i);
        printf("R: %d ",img->data[i].red );
        printf("G: %d ",img->data[i].green );
        printf("B: %d\n ",img->data[i].blue );

     }
}
}


char * decode(PPMImage * i1,PPMImage * i2){

//compare difference in number of bits in red pixels
//if there is a different then take the red pixel value from the encrypted image
//then translate it from ascii to chars then print.
printf("Decoding......\n");

int i;
     for(i=-1;i<i1->x*i1->y;i++){
            if(i1->data[i].red != i2->data[i].red){
                printf("%c",i1->data[i].red );
            }
     }

//to be able to test and finish this need to write code for encoding

}

int main(int argc, char *argv[]){

//input statements
if(argc == 3){
    PPMImage *image;
    image = getPPM(argv[2]);
    //uncomment the showPPM to display all rgb values in the encoded files
    //showPPM(image);
    if(argv[1] = "e"){
    printf("Please enter your secret message to be encoded estimated max characters: %d\n",image->y);   

        //need to add user input
    encode("test output!",image);
    }
}else if(argc == 4){
    PPMImage *i1;
    PPMImage *i2;
    i1 = getPPM(argv[2]);
    i2 = getPPM(argv[3]);

    if(argv[1] = "d"){
        decode(i1,i2);
    }
}else{
    printf("Wrong arguments");
}
}

问题实际上出在PPM中读取的代码,您修改后的代码看似有效,但实际上无效,因为文件格式与您认为的不同。

您链接到的代码用于读取 "raw" 格式的 PPM 文件。这些文件以 "P6" 代码开头。在这些文件中,每个 RGB 值都存储为 1 个或 2 个字节(取决于 RGB 分量深度是否小于 256)。因此,如果最大值为 255,则每个值 1 个字节,因此文件大小为 width * height * 3.

但是,您修改了代码以读取 "plain" PPM 文件,这些文件以 "P3" 代码开头,检查 P3 并读取更多数据。这些文件不将 RGB 值存储为原始二进制数据,而是以十进制格式指定值的 ASCII 文本,以白色 space 分隔。因此,例如,如果您的原始格式值为 93,则它只有 1 个字节且值为 93,但在 "plain" 格式中它将是 3(或更多)字节:一个或多个字节带有space(或制表符)的 ASCII 值,然后是“9”的 ASCII 值(即 57),然后是“3”的 ASCII 值(即 51)。不能根据宽高来计算文件的大小,因为whitespace是可变的,每个值都可以用1到3位来表示。

尽管您没有将数据解析为 ASCII-encoded 文本,但您的 PPM-reading 代码 似乎 可以工作,因为您只是阅读数据块,(可选)修改一些随机字节,然后完全或大部分不变地再次写出它。

因此,您可能的解决方案是:

  • 将 getPPM 代码改回原来的样子并使用实际的 P6 文件。
  • 编写一个 PPM reader 将数据正确解析为包含 whitespace-separated 个十进制数字的 ASCII 文本(您可以写成 P3 或 P6)。

更多信息:PPM Format Specification