c++image-processingfile-formatpbmnetpbm

Inverting bits of PBM image while vs for loop


I am trying to flip the color of pixels of a simple pbm image which has only pure black and pure white. I am generating the image myself and then reading it and then flipping the bits and saving both the generated image and the color inverted image.

Here is my code (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);

}

Below is the header I am including (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

Below is my main -

#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();
}

So I am making a V cross kind of pattern and then inverting the colors and saving both of the created image and the inverted image.

Lets suppose we provide input 10 10

then the file sample.pbm looks like

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

and inverted.pbm looks like this

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

as you can see that only half the rows are getting printed in the inverted image.

If I replace the nested loops of invert() of write_pbm.cpp with

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);
    }

then it gives the right output in the inverted.pbm file which is

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
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1
\FF

I am doing the same thing through both the nested loops and the while loop then why is it giving the wrong output in case of nested for loops?

Thanks for reading this, Please provide your valuable response.


Solution

  • Looking at your input file, it appears that every meaningful char (i.e. '0' or '1') is followed by a space.

    In the while loop that is working, you read as many chars as needed, until the end of file, just inverting the correct chars and copying unexpected chars. So everything is processed.

    In the nested for loops, you are reading the exact number of chars corresponding to the dimensions of the picture so 10*10. Taking into account the layout of the input file, you are therefore reading only 100 chars, so 50 '1' or '0' and 50 spaces. So you process only half of the input.

    Unrelated: your code uses C++ header, but all the rest is just plain C. In C++, you should consider to use fstream to read your files, and not the C legacy FILE* API.