I am trying to create an 8x8 PGM chessboard in C

Hi, I'm trying to write a C program that creates a PGM file with a resolution of 800x800 pixels, and then fills the file with alternating black and white squares of 100x100 pixels in size. It compiles fine, freezes at runtime, and the resulting PGM file only appears to be a thin alternating black and white line.

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char* argv[])
{
    /*Declaring variables for rows, columns, boardsize, squaresize and pgmData 
    array*/
    int row, col,i,j;
    int iBoardSize = 800, iSquareSize = 100;
    int **iPgmData;

    /*Allocate memory for the data*/
    row = 800;
    col = 800;

    iPgmData = malloc(row * sizeof(int *));
    for (i = 0; i < row;i++)
        iPgmData[i] = malloc(col * sizeof(int));

    /*Assign data to the array the desired result is an 8x8 checkboard of 100x100
    pixel squares of alternating black and white.*/
    for (row = 0; row < iBoardSize; row++){
        for (col = 0; col < iBoardSize; col++){
            if ((row / iSquareSize + col / iSquareSize ) %2 == 0)
                iPgmData[row][col] = 0;
            else
                iPgmData[row][col] = 255;

        }
    }

    /* Open the PGM file */
    FILE* image = fopen(argv[1], "wb");
    if (image == NULL){
      fprintf(stderr, "Can't open output file %s!\n", argv[1]);
      exit(1);
    }

    /*Write the header*/
    fprintf(image, "P2\n%d %d\n255\n", iBoardSize, iBoardSize);

    for (row = 0;row <= iBoardSize; row++){
        for (col = 0;col <= iBoardSize; col++){
            if (col < iBoardSize)
                fprintf(image, "%d ", iPgmData[row][col]);
            else
                fprintf(image, "\n");
        }
    }

    /*Write pgmData*/
    fwrite(iPgmData, 1, iBoardSize*iBoardSize*sizeof(int *), image);

    /*Close the PGM*/
    fclose(image);

    /*Free allocated memory*/
    free(iPgmData);

    return 0;
}
+4
source share
2 answers

the published program is much more complicated and contains several problems associated with indexing into arrays and how data is written to the output file.

The following code contains comments, performs appropriate error checking, and creates the desired image file.

#include <stdio.h>  // fopen(), fclose(), fprintf(), FILE
#include <stdlib.h>

#include <errno.h>  // errno
#include <string.h> // strerror()

#define MAX_ROWS    (800)
#define MAX_COLS    (800)
#define BOARD_SIZE  (800)
#define SQUARE_SIZE (100)

int main (int argc, char* argv[])
{
    int row; // image row index
    int col; // image column index


    if( 2 != argc )
    { // then required command line parameter missing
        fprintf( stderr, "USAGE: %s <PGM_filename>\n", argv[0]);
        exit( EXIT_FAILURE );
    }

    // implied else, correct number of command line parameters

    /* Open the PGM file */
    FILE* image = fopen(argv[1], "wb");
    if (!image)
    {
      fprintf(stderr, "Can't open output file %s for write, due to: %s\n", argv[1], strerror( errno ) );
      exit( EXIT_FAILURE);
    }

    /*Write the header*/
    fprintf(image, "P5\n%d %d\n255\n", BOARD_SIZE, BOARD_SIZE);

    /* write the gray scale image */
    for (row = 0;row < BOARD_SIZE; row++)
    {
        for (col = 0;col < BOARD_SIZE; col++)
        {

            if( (row/SQUARE_SIZE)%2 == 0 )
            {
                if( (col/SQUARE_SIZE)%2 == 0 )
                {
                    fprintf( image, "%c", 255 );
                }

                else
                {
                    fprintf( image, "%c", 0 );
                }
            }

            else
            {
                if( (col/SQUARE_SIZE)%2 == 0 )
                {
                    fprintf( image, "%c", 0 );
                }

                else
                {
                    fprintf( image, "%c", 255 );
                }
            }
        }
    }

    fclose(image);

    return 0;
} // end function: main
+2
source

The problems that I see:

for (row = 0;row <= iBoardSize; row++){
             ^^^^^^^^^^^^^^^^ Wrong. Should be row < iBoardSize
   for (col = 0;col <= iBoardSize; col++){
                ^^^^^^^^^^^^^^^^ Wrong. Should be col < iBoardSize
      if (col < iBoardSize)
         fprintf(image, "%d ", iPgmData[row][col]);
      else
         fprintf(image, "\n");
   }
}

:

for (row = 0;row < iBoardSize; row++){
   for (col = 0;col < iBoardSize; col++){
      fprintf(image, "%d ", iPgmData[row][col]);
   }
   fprintf(image, "\n");
}

fwrite

fwrite(iPgmData, 1, iBoardSize*iBoardSize*sizeof(int *), image);

fwrite, . . , . , PGM.

free

free(iPgmData);

, . , . :

for (i = 0; i < row;i++)
   free(iPgmData[i]);

free(iPgmData);
+6

Source: https://habr.com/ru/post/1627104/


All Articles