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 * maze_reg.c 00018 * 00019 * Tests the functions in maze.c: binary and gray maze search, 00020 * largest rectangle in bg or fg. 00021 */ 00022 00023 #include <string.h> 00024 #include "allheaders.h" 00025 00026 #define NPATHS 6 00027 static const l_int32 x0[NPATHS] = {42, 73, 73, 42, 324, 471}; 00028 static const l_int32 y0[NPATHS] = {117, 319, 319, 117, 170, 201}; 00029 static const l_int32 x1[NPATHS] = {419, 419, 233, 326, 418, 128}; 00030 static const l_int32 y1[NPATHS] = {383, 383, 112, 168, 371, 341}; 00031 00032 static const l_int32 NBOXES = 20; 00033 static const l_int32 POLARITY = 0; /* background */ 00034 00035 main(int argc, 00036 char **argv) 00037 { 00038 l_int32 i, w, h, bx, by, bw, bh, index, rval, gval, bval; 00039 BOX *box; 00040 BOXA *boxa; 00041 PIX *pixm, *pixs, *pixg, *pixt, *pixd; 00042 PIXA *pixa; 00043 PIXCMAP *cmap; 00044 PTA *pta; 00045 PTAA *ptaa; 00046 L_REGPARAMS *rp; 00047 00048 if (regTestSetup(argc, argv, &rp)) 00049 return 1; 00050 pixa = pixaCreate(0); 00051 00052 /* ---------------- Shortest path in binary maze ---------------- */ 00053 /* Generate the maze */ 00054 pixm = generateBinaryMaze(200, 200, 20, 20, 0.65, 0.25); 00055 pixd = pixExpandBinaryReplicate(pixm, 3); 00056 pixSaveTiledOutline(pixd, pixa, 1, 1, 20, 2, 32); 00057 pixDestroy(&pixd); 00058 00059 /* Find the shortest path between two points */ 00060 pta = pixSearchBinaryMaze(pixm, 20, 20, 170, 170, NULL); 00061 pixt = pixDisplayPta(NULL, pixm, pta); 00062 pixd = pixScaleBySampling(pixt, 3., 3.); 00063 pixSaveTiledOutline(pixd, pixa, 1, 0, 20, 2, 32); 00064 regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 0 */ 00065 ptaDestroy(&pta); 00066 pixDestroy(&pixt); 00067 pixDestroy(&pixd); 00068 pixDestroy(&pixm); 00069 00070 00071 /* ---------------- Shortest path in gray maze ---------------- */ 00072 pixg = pixRead("test8.jpg"); 00073 pixGetDimensions(pixg, &w, &h, NULL); 00074 ptaa = ptaaCreate(NPATHS); 00075 for (i = 0; i < NPATHS; i++) { 00076 if (x0[i] >= w || x1[i] >= w || y0[i] >= h || y1[i] >= h) { 00077 fprintf(stderr, "path %d extends beyond image; skipping\n", i); 00078 continue; 00079 } 00080 pta = pixSearchGrayMaze(pixg, x0[i], y0[i], x1[i], y1[i], NULL); 00081 ptaaAddPta(ptaa, pta, L_INSERT); 00082 } 00083 00084 pixt = pixDisplayPtaa(pixg, ptaa); 00085 pixd = pixScaleBySampling(pixt, 2., 2.); 00086 pixSaveTiledOutline(pixd, pixa, 1, 1, 20, 2, 32); 00087 regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 1 */ 00088 ptaaDestroy(&ptaa); 00089 pixDestroy(&pixg); 00090 pixDestroy(&pixt); 00091 pixDestroy(&pixd); 00092 00093 00094 /* ---------------- Largest rectangles in image ---------------- */ 00095 pixs = pixRead("test1.png"); 00096 pixd = pixConvertTo8(pixs, FALSE); 00097 cmap = pixcmapCreateRandom(8, 1, 1); 00098 pixSetColormap(pixd, cmap); 00099 00100 boxa = boxaCreate(0); 00101 for (i = 0; i < NBOXES; i++) { 00102 pixFindLargestRectangle(pixs, POLARITY, &box, NULL); 00103 boxGetGeometry(box, &bx, &by, &bw, &bh); 00104 pixSetInRect(pixs, box); 00105 fprintf(stderr, "bx = %5d, by = %5d, bw = %5d, bh = %5d, area = %d\n", 00106 bx, by, bw, bh, bw * bh); 00107 boxaAddBox(boxa, box, L_INSERT); 00108 } 00109 00110 for (i = 0; i < NBOXES; i++) { 00111 index = 32 + (i & 254); 00112 pixcmapGetColor(cmap, index, &rval, &gval, &bval); 00113 box = boxaGetBox(boxa, i, L_CLONE); 00114 pixRenderHashBoxArb(pixd, box, 6, 2, L_NEG_SLOPE_LINE, 1, 00115 rval, gval, bval); 00116 boxDestroy(&box); 00117 } 00118 pixSaveTiledOutline(pixd, pixa, 1, 1, 20, 2, 32); 00119 regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 2 */ 00120 pixDestroy(&pixs); 00121 pixDestroy(&pixd); 00122 boxaDestroy(&boxa); 00123 00124 pixd = pixaDisplay(pixa, 0, 0); 00125 regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 3 */ 00126 pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display); 00127 pixDestroy(&pixd); 00128 pixaDestroy(&pixa); 00129 00130 regTestCleanup(rp); 00131 return 0; 00132 } 00133