int main(int argc, char *argv[])
{
        FILE *file;
        unsigned num_pixels, sum = 0;
        float avg = 0.0;

        /* Raises CRE if more than 1 file specified */
        assert(argc < 3);

        if (argc == 1)
                file = stdin;
        else /* argc == 2 */
                file = fopen(argv[1], "rb");

        Pnmrdr_T reader = Pnmrdr_new(file); /* Raises CRE if file is NULL */
        Pnmrdr_mapdata data = Pnmrdr_data(reader);

        /* Raises CRE if the file is of an incorrect type */
        assert(data.type == Pnmrdr_gray);

        num_pixels = data.width * data.height;  
        for (unsigned i = 0; i < num_pixels; i++)
                sum += Pnmrdr_get(reader);
        avg = sum / (float)(num_pixels * data.denominator);
        printf("%0.3f\n", avg);
        
        Pnmrdr_free(&reader);
        fclose(file);

        exit(EXIT_SUCCESS);
}
Exemple #2
0
/* opens the pgm file and extracts reader */
Pnmrdr_T file_handler(char *argv[], FILE **fp) 
{
        Pnmrdr_T reader = NULL;
        *fp = fopen(argv[1], "rb"); 

        if (*fp == NULL) {
                exit(1);
        }
        reader = Pnmrdr_new(*fp);

        return reader;

}
Exemple #3
0
/* main calls helper functions to get and process data from pgm file or stdin*/
int main(int argc, char *argv[])
{
        Pnmrdr_T reader = NULL;
        FILE *fp = NULL;

        if (argc == 1) {
                reader = Pnmrdr_new(stdin);
        } else if (argc == 2) {
                reader = file_handler(argv, &fp);
        } else if (argc > 2) {
                exit(1);
        }

        Pnmrdr_mapdata data = check_data(reader, fp);
        UArray2_T uarray2 = pixels_to_array(reader, data, fp);
        correct_sudoku(uarray2);

        UArray2_free(&uarray2);
        
        exit(0);
}
Exemple #4
0
Bit2_T pbmread( FILE *input) {
    Pnmrdr_T r = Pnmrdr_new(input);

    Pnmrdr_mapdata data = Pnmrdr_data(r);
    assert(data.type == Pnmrdr_bit);
    int w_pixels = data.width;
    int h_pixels = data.height;
    assert(w_pixels > 0 && h_pixels > 0);

    Bit2_T bitmap;
    bitmap = Bit2_new(w_pixels, h_pixels);
    assert (bitmap);

    for ( int j = 0; j < h_pixels; j++) {
        for (int i = 0; i < w_pixels; i++) {
            int pixel = Pnmrdr_get(r);
            Bit2_put(bitmap, i, j, pixel);
        }
    }

    Pnmrdr_free(&r);
    return bitmap;
}
Exemple #5
0
/*                                read_in_solution()
 *
 * Function takes an empty 2-Dimensional array where the solution will be stored
 * for checking and a pointer to the file with the solution to be checked. Using
 * the Pnmrdr interface, it ensures that the format of the solution is correct
 * and uses a helper function to fill the 2-D array with the puzzle entries.
 */
void read_in_solution(UArray2_T array, FILE *srcfile)
{

    void *reader = NULL;
    Pnmrdr_mapdata data;
    int width = 0;
    int height = 0;
    int max_intensity = 0;

    reader = Pnmrdr_new(srcfile);
    data = Pnmrdr_data(reader);

    assert(data.type == Pnmrdr_gray);

    width = data.width;
    height = data.height;
    max_intensity = data.denominator;

    assert(max_intensity == 9);
    assert(width == 9 && height == 9);

    UArray2_map_row_major(array, fill_array, reader);
    Pnmrdr_free((Pnmrdr_T *)&reader);
}