vs循环时将PBM图像的位反转

时间:2019-03-25 06:53:48

标签: c++ image-processing file-format pbm netpbm

我正在尝试翻转仅具有纯黑色和纯白色的简单pbm图像的像素颜色。我自己生成图像,然后读取它,然后翻转这些位,并保存生成的图像和彩色反转图像。

这是我的代码( write_pbm.cpp )-

#include "headers/write_pbm.h"

int width_pbm, height_pbm;

void input_sample_dim(){
    printf("Enter the width and height of image to create = ");
    scanf("%d %d", &width_pbm, &height_pbm);
}

void create_sample_image(){
    FILE *fp;
    fp = fopen("sample.pbm", "wb");

    fprintf(fp, "P1\n");
    fprintf(fp, "# myfile.pbm\n");
    fprintf(fp, "%d %d\n", width_pbm, height_pbm);

    for (int i = 1; i <= height_pbm; i++)
    {
        for (int j = 1; j <= width_pbm; j++)
        {
            if (j == i || (width_pbm - j + 1 == i))
                fprintf(fp, "0");
            else
                fprintf(fp, "1");

            if (j == width_pbm)
                fprintf(fp, "\n");
            else
                fprintf(fp, " ");
        }
    }

    fclose(fp);
}

void invert (){
    printf("\tinverting the image\nturning black pixels white and white pixels black\n");

    FILE *fp = fopen("sample.pbm", "rb");
    while(fgetc(fp) != '\n');
    while(fgetc(fp) != '\n');
    while(fgetc(fp) != '\n');

    FILE *fp_inv;
    fp_inv = fopen("inverted.pbm", "wb");

    fprintf(fp_inv, "P1\n");
    fprintf(fp_inv, "# inverted.pbm\n");
    fprintf(fp_inv, "%d %d\n", width_pbm, height_pbm);


    for (int i = 1; i <= height_pbm; i++){
        for (int j = 1; j <= width_pbm; j++){
            char ch = fgetc(fp);

            if (ch == '1')
                fputc('0', fp_inv);
            else if (ch == '0')
                fputc('1', fp_inv);
            else
                fputc(ch, fp_inv);
        }}
    fclose(fp);
    fclose(fp_inv);

}

下面是我要包含的标头( write_pbm.h

#ifndef Write_PBM_H
#define Write_PBM_H

#include <cstdio>
#include <iostream>

void create_sample_image(void);

void input_sample_dim(void);

void invert (void);

extern int width_pbm, height_pbm;

#endif

下面是我的主要-

#include "write/PBM/headers/write_pbm.h"

int main(){
    input_sample_dim();

    printf("writing sample image\n");
    create_sample_image();
    printf("sample image with dimenstions %d by %d created\n", width_pbm, height_pbm);
    invert();
}

所以我先制作V形十字图案,然后反转颜色并保存创建的图像和反转的图像。

假设我们提供输入10 10

然后文件sample.pbm类似于

P1
# myfile.pbm
10 10
0 1 1 1 1 1 1 1 1 0
1 0 1 1 1 1 1 1 0 1
1 1 0 1 1 1 1 0 1 1
1 1 1 0 1 1 0 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 0 1 1 0 1 1 1
1 1 0 1 1 1 1 0 1 1
1 0 1 1 1 1 1 1 0 1
0 1 1 1 1 1 1 1 1 0

inverted.pbm 看起来像这样

P1
# inverted.pbm
10 10
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0

如您所见,倒排图像中只打印了一半行。

如果我用

替换invert()的{​​{1}}的嵌套循环
write_pbm.cpp

然后在 char ch; while(!feof(fp)) { char ch = fgetc(fp); if (ch == '1') fputc('0', fp_inv); else if (ch == '0') fputc('1', fp_inv); else fputc(ch, fp_inv); } 文件(

)中提供正确的输出
inverted.pbm

我在嵌套循环和while循环中都做同样的事情,那么为什么在嵌套for循环的情况下却给出错误的输出呢?

感谢阅读本文,请提供宝贵的答复。

1 个答案:

答案 0 :(得分:1)

查看您的输入文件,似乎每个有意义的字符(即'0''1')后跟一个空格。

在有效的while循环中,您读取了所需数量的字符,直到文件结束,只是反转了正确的字符并复制了意外的字符。因此,一切都已处理。

在嵌套的for循环中,您正在读取与图片尺寸相对应的确切字符数,即10 * 10。考虑到输入文件的布局,因此,您仅读取100个字符,因此50个'1''0'和50个空格。因此,您只处理一半的输入。

不相关: 您的代码使用C ++标头,但其余所有代码仅是纯C。在C ++中,您应考虑使用fstream读取文件,而不读取C旧版FILE* API。

相关问题