Leptonica 1.68
C Image Processing Library
|
00001 /*====================================================================* 00002 - Copyright (C) 2001 Leptonica. All rights reserved. 00003 - This software is distributed in the hope that it will be 00004 - useful, but with NO WARRANTY OF ANY KIND. 00005 - No author or distributor accepts responsibility to anyone for the 00006 - consequences of using this software, or for whether it serves any 00007 - particular purpose or works at all, unless he or she says so in 00008 - writing. Everyone is granted permission to copy, modify and 00009 - redistribute this source code, for commercial or non-commercial 00010 - purposes, with the following restrictions: (1) the origin of this 00011 - source code must not be misrepresented; (2) modified versions must 00012 - be plainly marked as such; and (3) this notice may not be removed 00013 - or altered from any source or modified source distribution. 00014 *====================================================================*/ 00015 00016 /* 00017 * conncomp_reg.c 00018 * 00019 * Regression test for connected components (both 4 and 8 00020 * connected), including regeneration of the original 00021 * image from the components. This is also an implicit 00022 * test of rasterop. 00023 */ 00024 00025 #include <string.h> 00026 #include "allheaders.h" 00027 00028 #define NTIMES 10 00029 00030 main(int argc, 00031 char **argv) 00032 { 00033 l_uint8 *array1, *array2; 00034 l_int32 i, n, np, same, diff; 00035 size_t nbytes1, nbytes2; 00036 FILE *fp; 00037 BOX *box; 00038 BOXA *boxa, *boxa2; 00039 PIX *pixs, *pixd; 00040 PIXA *pixa; 00041 PIXCMAP *cmap; 00042 static char mainName[] = "conncomp_reg"; 00043 00044 if (argc != 1) 00045 exit(ERROR_INT(" Syntax: conncomp_reg", mainName, 1)); 00046 00047 if ((pixs = pixRead("feyn.tif")) == NULL) 00048 exit(ERROR_INT("pixs not made", mainName, 1)); 00049 00050 /* Test pixConnComp() with output to both boxa and pixa */ 00051 /* First, test with 4-cc */ 00052 boxa = pixConnComp(pixs, &pixa, 4); 00053 n = boxaGetCount(boxa); 00054 fprintf(stderr, "Number of 4 c.c. b.b: %d\n", n); 00055 np = pixaGetCount(pixa); 00056 fprintf(stderr, "Number of 4 c.c. pix: %d\n", np); 00057 pixd = pixaDisplay(pixa, pixGetWidth(pixs), pixGetHeight(pixs)); 00058 pixWrite("/tmp/junkout1.png", pixd, IFF_PNG); 00059 pixEqual(pixs, pixd, &same); 00060 if (same == 1) 00061 fprintf(stderr, "Source and reconstructed pix are the same.\n"); 00062 else 00063 fprintf(stderr, "Error: source and reconstructed pix differ!\n"); 00064 pixaDestroy(&pixa); 00065 boxaDestroy(&boxa); 00066 pixDestroy(&pixd); 00067 00068 /* Test with 8-cc */ 00069 boxa = pixConnComp(pixs, &pixa, 8); 00070 n = boxaGetCount(boxa); 00071 fprintf(stderr, "Number of 8 c.c. b.b: %d\n", n); 00072 np = pixaGetCount(pixa); 00073 fprintf(stderr, "Number of 8 c.c. pix: %d\n", np); 00074 pixd = pixaDisplay(pixa, pixGetWidth(pixs), pixGetHeight(pixs)); 00075 pixWrite("/tmp/junkout2.png", pixd, IFF_PNG); 00076 pixEqual(pixs, pixd, &same); 00077 if (same == 1) 00078 fprintf(stderr, "Source and reconstructed pix are the same.\n"); 00079 else 00080 fprintf(stderr, "Error: source and reconstructed pix differ!\n"); 00081 pixaDestroy(&pixa); 00082 boxaDestroy(&boxa); 00083 pixDestroy(&pixd); 00084 00085 /* Test i/o */ 00086 boxa = pixConnComp(pixs, NULL, 4); 00087 fp = lept_fopen("/tmp/junk1.ba", "wb+"); 00088 boxaWriteStream(fp, boxa); 00089 lept_fclose(fp); 00090 fp = lept_fopen("/tmp/junk1.ba", "rb"); 00091 boxa2 = boxaReadStream(fp); 00092 lept_fclose(fp); 00093 fp = lept_fopen("/tmp/junk2.ba", "wb+"); 00094 boxaWriteStream(fp, boxa2); 00095 lept_fclose(fp); 00096 array1 = l_binaryRead("/tmp/junk1.ba", &nbytes1); 00097 array2 = l_binaryRead("/tmp/junk2.ba", &nbytes2); 00098 diff = strcmp((char *)array1, (char *)array2); 00099 if (nbytes1 != nbytes2 || diff) 00100 fprintf(stderr, "I/O error for boxes.\n"); 00101 else 00102 fprintf(stderr, "I/O valid for boxes.\n"); 00103 lept_free(array1); 00104 lept_free(array2); 00105 boxaDestroy(&boxa); 00106 boxaDestroy(&boxa2); 00107 00108 /* Just for fun, display each component as a random color 00109 * in cmapped 8 bpp. Background is color 0; it is set to white. */ 00110 boxa = pixConnComp(pixs, &pixa, 4); 00111 pixd = pixaDisplayRandomCmap(pixa, pixGetWidth(pixs), pixGetHeight(pixs)); 00112 cmap = pixGetColormap(pixd); 00113 pixcmapResetColor(cmap, 0, 255, 255, 255); /* reset background to white */ 00114 pixDisplay(pixd, 100, 100); 00115 boxaDestroy(&boxa); 00116 pixDestroy(&pixd); 00117 pixaDestroy(&pixa); 00118 00119 pixDestroy(&pixs); 00120 return 0; 00121 } 00122 00123