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);
}
Beispiel #2
0
/*gets data from reader and checks for pgm format, maxval, height and width*/
Pnmrdr_mapdata check_data(Pnmrdr_T reader, FILE *fp)
{
        Pnmrdr_mapdata data;
        data = Pnmrdr_data(reader);

        if (data.type != Pnmrdr_gray) {
                Pnmrdr_free(&reader);
                fclose(fp);
                exit(1);
        }
        if (data.denominator != 9) {
                Pnmrdr_free(&reader);
                fclose(fp);
                exit(1);
        } 
        if (data.height != 9 || data.width != 9) {              
                Pnmrdr_free(&reader);
                fclose(fp);
                exit(1);
        }
        return data;
}
Beispiel #3
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;
}
Beispiel #4
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);
}