/* test-pnm.c Edouard.Thiel@lif.univ-mrs.fr - 09/12/2010 Test read/write of pnm files. */ /* DO NOT EDIT !!! Generated by npic-templa from "test-pnm.ct" */ #include void my_random_init () { srand ((int) time (NULL)); } int my_random (int n) { return (rand() / (RAND_MAX + 1.0)) * n; } int my_random1 () { int k = my_random (200); return k < 100 ? 0 : 1; } int my_random16 () { int k = my_random (255); return k | k << 8; } void test_pnm_2c (char *name1, char *name2, int w, int h, int maxgrey) { Npic_image *np1, *np2; Npic_image_2c *I1, *I2; int x, y, ok = 1; printf ("Creating 2C %dx%d ... ", h, w); fflush (stdout); np1 = NpicCreateImage_2c (h, w, 0, 0); if (np1 == NULL) return; I1 = NpicCastImage (np1); for (y = 0; y < I1->ymax; y++) for (x = 0; x < I1->xmax; x++) I1->pix[y][x] = (maxgrey == 1) ? my_random1 ()*255 : my_random (maxgrey); printf ("saving %s ... ", name1); fflush (stdout); if (NpicWriteImage (np1, name1) != NPIC_SUCCESS) { fprintf (stderr, "write error\n"); exit (1); } printf ("loading %s ... ", name2); fflush (stdout); np2 = NpicReadImage (name2); if (np2 == NULL) exit (1); I2 = NpicCastImage (np2); printf ("diff ... "); fflush (stdout); for (y = 0; y < I1->ymax; y++) for (x = 0; x < I1->xmax; x++) if (I1->pix[y][x] != I2->pix[y][x]) { printf ("\n ERROR: y = %d x = %d I1 = %d I2 = %d\n", y, x, I1->pix[y][x], I2->pix[y][x]); ok = 0; break; } NpicDestroyImage (np1); NpicDestroyImage (np2); if (ok) printf ("done\n"); } void test_pnm_2l (char *name1, char *name2, int w, int h, int maxgrey) { Npic_image *np1, *np2; Npic_image_2l *I1, *I2; int x, y, ok = 1; printf ("Creating 2L %dx%d ... ", h, w); fflush (stdout); np1 = NpicCreateImage_2l (h, w, 0, 0); if (np1 == NULL) return; I1 = NpicCastImage (np1); for (y = 0; y < I1->ymax; y++) for (x = 0; x < I1->xmax; x++) I1->pix[y][x] = (maxgrey == 1) ? my_random1 ()*255 : my_random (maxgrey); printf ("saving %s ... ", name1); fflush (stdout); if (NpicWriteImage (np1, name1) != NPIC_SUCCESS) { fprintf (stderr, "write error\n"); exit (1); } printf ("loading %s ... ", name2); fflush (stdout); np2 = NpicReadImage (name2); if (np2 == NULL) exit (1); I2 = NpicCastImage (np2); printf ("diff ... "); fflush (stdout); for (y = 0; y < I1->ymax; y++) for (x = 0; x < I1->xmax; x++) if (I1->pix[y][x] != I2->pix[y][x]) { printf ("\n ERROR: y = %d x = %d I1 = %" NPIC_PL " I2 = %" NPIC_PL "\n", y, x, I1->pix[y][x], I2->pix[y][x]); ok = 0; break; } NpicDestroyImage (np1); NpicDestroyImage (np2); if (ok) printf ("done\n"); } void test_pnm_2q (char *name1, char *name2, int w, int h, int maxgrey) { Npic_image *np1, *np2; Npic_image_2q *I1, *I2; int x, y, ok = 1; printf ("Creating 2Q %dx%d ... ", h, w); fflush (stdout); np1 = NpicCreateImage_2q (h, w, 0, 0); if (np1 == NULL) return; I1 = NpicCastImage (np1); for (y = 0; y < I1->ymax; y++) for (x = 0; x < I1->xmax; x++) { I1->pix[y][x].a = (maxgrey == 255) ? my_random16 () : my_random (maxgrey); I1->pix[y][x].b = (maxgrey == 255) ? my_random16 () : my_random (maxgrey); I1->pix[y][x].c = (maxgrey == 255) ? my_random16 () : my_random (maxgrey); I1->pix[y][x].d = 0; } printf ("saving %s ... ", name1); fflush (stdout); if (NpicWriteImage (np1, name1) != NPIC_SUCCESS) { fprintf (stderr, "write error\n"); exit (1); } printf ("loading %s ... ", name2); fflush (stdout); np2 = NpicReadImage (name2); if (np2 == NULL) exit (1); I2 = NpicCastImage (np2); printf ("diff ... "); fflush (stdout); for (y = 0; y < I1->ymax; y++) for (x = 0; x < I1->xmax; x++) if (I1->pix[y][x].a != I2->pix[y][x].a || I1->pix[y][x].b != I2->pix[y][x].b || I1->pix[y][x].c != I2->pix[y][x].c ) { printf ("\n ERROR: y = %d x = %d I1 = %d %d %d I2 = %d %d %d\n", y, x, I1->pix[y][x].a, I1->pix[y][x].b, I1->pix[y][x].c, I2->pix[y][x].a, I2->pix[y][x].b, I2->pix[y][x].c); ok = 0; break; } NpicDestroyImage (np1); NpicDestroyImage (np2); if (ok) printf ("done\n"); } int main (int argc, char **argv) { int x, y, h = 13, w = 9; my_random_init (); printf ("TESTING BITMAP\n"); test_pnm_2c ("tmp1.pnm:ascii,bitmap", "tmp1.pnm", h, w, 1); test_pnm_2c ("tmp1.pbm:ascii" , "tmp1.pbm", h, w, 1); for (y = 10; y <= 20; y++) for (x = 10; x <= 20; x++) { test_pnm_2c ("tmp1.pnm:bitmap" , "tmp1.pnm", y, x, 1); test_pnm_2c ("tmp1.pbm" , "tmp1.pbm", y, x, 1); } printf ("TESTING GREYMAP\n"); test_pnm_2c ("tmp1.pnm:ascii", "tmp1.pnm", h, w, 255); test_pnm_2c ("tmp1.pgm:ascii", "tmp1.pgm", h, w, 255); test_pnm_2c ("tmp1.pnm" , "tmp1.pnm", h, w, 255); test_pnm_2c ("tmp1.pgm" , "tmp1.pgm", h, w, 255); test_pnm_2l ("tmp1.pnm:ascii,16-bit", "tmp1.pnm", h, w, 65535); test_pnm_2l ("tmp1.pgm:ascii,16-bit", "tmp1.pgm", h, w, 65535); test_pnm_2l ("tmp1.pnm:16-bit" , "tmp1.pnm", h, w, 65535); test_pnm_2l ("tmp1.pgm:16-bit" , "tmp1.pgm", h, w, 65535); test_pnm_2l ("tmp1.pnm:16-bit,little-endian", "tmp1.pnm:little-endian", h, w, 65535); test_pnm_2l ("tmp1.pgm:16-bit,little-endian", "tmp1.pgm:little-endian", h, w, 65535); printf ("TESTING PIXMAP\n"); test_pnm_2q ("tmp1.pnm:ascii", "tmp1.pnm", h, w, 255); test_pnm_2q ("tmp1.ppm:ascii", "tmp1.ppm", h, w, 255); test_pnm_2q ("tmp1.pnm" , "tmp1.pnm", h, w, 255); test_pnm_2q ("tmp1.ppm" , "tmp1.ppm", h, w, 255); test_pnm_2q ("tmp1.pnm:ascii,16-bit", "tmp1.pnm", h, w, 65535); test_pnm_2q ("tmp1.ppm:ascii,16-bit", "tmp1.ppm", h, w, 65535); test_pnm_2q ("tmp1.pnm:16-bit" , "tmp1.pnm", h, w, 65535); test_pnm_2q ("tmp1.ppm:16-bit" , "tmp1.ppm", h, w, 65535); test_pnm_2q ("tmp1.pnm:16-bit,little-endian", "tmp1.pnm:little-endian", h, w, 65535); test_pnm_2q ("tmp1.ppm:16-bit,little-endian", "tmp1.ppm:little-endian", h, w, 65535); exit(0); }