/* Verify output of various PNG splitters. Exits with 0 if output files were as expected. */ #include #include #include #include static int IsBlank(png_bytep pixels, int offset) { return pixels[offset] == 0 && pixels[offset + 1] == 0 && pixels[offset + 2] == 0 && pixels[offset + 3] == 0; } static int IsSame(png_bytep a, png_bytep b, int offset) { return a[offset] == b[offset] && a[offset + 1] == b[offset + 1] && a[offset + 2] == b[offset + 2] && a[offset + 3] == b[offset + 3]; } static void OutputDiff(png_bytep original, png_bytep split1, png_bytep split2, int x, int y, int offset) { printf("(%d,%d): original=%02x%02x%02x%02x, " "split1=%02x%02x%02x%02x, " "split2=%02x%02x%02x%02x\n", x, y, original[offset], original[offset + 1], original[offset + 2], original[offset + 3], split1[offset], split1[offset + 1], split1[offset + 2], split1[offset + 3], split2[offset], split2[offset + 1], split2[offset + 2], split2[offset + 3]); } int main(int argc, char **argv) { png_image image[3]; png_bytep pixel[3]; int i, x, y, errors = 0; /* Load images. */ if( argc != 4 ) { printf("%s \n", *argv); return 1; } for(i = 0; i < 3; i++) { memset(&image[i], 0, sizeof(png_image)); image[i].version = PNG_IMAGE_VERSION; if( !png_image_begin_read_from_file(&image[i], argv[i + 1]) ) { printf("Error reading %s\n", argv[i + 1]); return 1; } image[i].format = PNG_FORMAT_RGBA; pixel[i] = (png_bytep)malloc(image[i].width * image[i].height * 4); if( pixel[i] == NULL ) { puts("Out of memory"); return 1; } if( !png_image_finish_read(&image[i], NULL, pixel[i], 0, NULL) ) { printf("Error loading %s\n", argv[i + 1]); return 1; } } /* Verify dimensions. */ for(i = 1; i <= 2; i++) { if( image[i].width != image[0].width || image[i].height != image[0].height ) { printf("%s: image dimensions mismatched: " "expected (%u,%u), got (%u,%u)\n", argv[i + 1], image[0].width, image[0].height, image[i].width, image[i].height); errors++; } } /* Check each pixel. */ for(y = i = 0; y < (int)image[0].height; y++) { for(x = 0; x < (int)image[0].width; x++, i += 4) { if( IsBlank(pixel[0], i) ) { /* If original pixel was blank, both of the output pixels must also be blank. */ if( IsBlank(pixel[1], i) && IsBlank(pixel[2], i) ) continue; } else { /* If original pixel was not blank, exactly one of the output pixels must be blank, while the other output pixel must be the same as the original pixel. */ if( (IsBlank(pixel[1], i) && IsSame(pixel[2], pixel[0], i)) || (IsBlank(pixel[2], i) && IsSame(pixel[1], pixel[0], i)) ) { continue; } } errors++; if( errors < 10 ) OutputDiff(pixel[0], pixel[1], pixel[2], x, y, i); } } if( errors > 0 ) { printf("%d errors\n", errors); return 1; } return 0; }