Leptonica 1.68
C Image Processing Library

xformbox_reg.c

Go to the documentation of this file.
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  *   xformbox_reg.c
00018  *
00019  *      Tests ordered box transforms (rotation, scaling, translation).
00020  *      Also tests the various box hashing graphics operations.
00021  */
00022 
00023 #include "allheaders.h"
00024 
00025     /* Consts for second set */
00026 static const l_int32  SHIFTX_2 = 50;
00027 static const l_int32  SHIFTY_2 = 70;
00028 static const l_float32  SCALEX_2 = 1.17;
00029 static const l_float32  SCALEY_2 = 1.13;
00030 static const l_float32  ROTATION_2 = 0.10;   /* radian */
00031 
00032     /* Consts for third set */
00033 static const l_int32  SHIFTX_3 = 44;
00034 static const l_int32  SHIFTY_3 = 39;
00035 static const l_float32  SCALEX_3 = 0.83;
00036 static const l_float32  SCALEY_3 = 0.78;
00037 static const l_float32  ROTATION_3 = 0.11;   /* radian */
00038 
00039 
00040 l_int32 RenderTransformedBoxa(PIX *pixt, BOXA *boxa, l_int32 i);
00041 
00042 
00043 main(int    argc,
00044      char **argv)
00045 {
00046 l_int32      i, n, ws, hs, w, h, rval, gval, bval, order;
00047 l_float32   *mat1, *mat2, *mat3;
00048 l_float32    matd[9];
00049 BOX         *box, *boxt;
00050 BOXA        *boxa, *boxat, *boxa1, *boxa2, *boxa3, *boxa4, *boxa5;
00051 PIX         *pix, *pixs, *pixb, *pixc, *pixt, *pixt1, *pixt2, *pixt3;
00052 PIXA        *pixa;
00053 static char  mainName[] = "xformbox_reg";
00054 
00055     /* ----------------------------------------------------------- *
00056      *                Test hash rendering in 3 modes               *
00057      * ----------------------------------------------------------- */
00058     pixs = pixRead("feyn.tif");
00059     box = boxCreate(461, 429, 1393, 342);
00060     pixt1 = pixClipRectangle(pixs, box, NULL);
00061     boxa = pixConnComp(pixt1, NULL, 8);
00062     n = boxaGetCount(boxa);
00063     pixt2 = pixConvertTo8(pixt1, 1);
00064     pixt3 = pixConvertTo32(pixt1);
00065     for (i = 0; i < n; i++) {
00066         boxt = boxaGetBox(boxa, i, L_CLONE);
00067         rval = (1413 * i) % 256;
00068         gval = (4917 * i) % 256;
00069         bval = (7341 * i) % 256;
00070         pixRenderHashBox(pixt1, boxt, 8, 2, i % 4, 1, L_SET_PIXELS);
00071         pixRenderHashBoxArb(pixt2, boxt, 7, 2, i % 4, 1, rval, gval, bval);
00072         pixRenderHashBoxBlend(pixt3, boxt, 7, 2, i % 4, 1, rval, gval, bval,
00073                               0.5);
00074         boxDestroy(&boxt);
00075     }
00076     pixDisplay(pixt1, 0, 0);
00077     pixDisplay(pixt2, 0, 300);
00078     pixDisplay(pixt3, 0, 570);
00079     pixWrite("/tmp/junkpixt1.png", pixt1, IFF_PNG);
00080     pixWrite("/tmp/junkpixt2.png", pixt2, IFF_PNG);
00081     pixWrite("/tmp/junkpixt3.png", pixt3, IFF_PNG);
00082 
00083     boxaDestroy(&boxa);
00084     boxDestroy(&box);
00085     pixDestroy(&pixs);
00086     pixDestroy(&pixt1);
00087     pixDestroy(&pixt2);
00088     pixDestroy(&pixt3);
00089 
00090 
00091     /* ----------------------------------------------------------- *
00092      *    Test box transforms with either translation or scaling   *
00093      *    combined with rotation, using the simple 'ordered'       *
00094      *    function.  Show that the order of the operations does    *
00095      *    not matter; different hashing schemes end up in the      *
00096      *    identical boxes.                                         *
00097      * ----------------------------------------------------------- */
00098     pix = pixRead("feyn.tif");
00099     box = boxCreate(420, 360, 1500, 465);
00100     pixt = pixClipRectangle(pix, box, NULL);
00101     pixs = pixAddBorderGeneral(pixt, 0, 200, 0, 0, 0);
00102     boxDestroy(&box);
00103     pixDestroy(&pix);
00104     pixDestroy(&pixt);
00105     boxa = pixConnComp(pixs, NULL, 8);
00106     n = boxaGetCount(boxa);
00107     pixa = pixaCreate(0);
00108 
00109     pixt = pixConvertTo32(pixs);
00110     for (i = 0; i < 3; i++) {
00111         if (i == 0)
00112             order = L_TR_SC_RO;
00113         else if (i == 1)
00114             order = L_TR_RO_SC;
00115         else
00116             order = L_SC_TR_RO;
00117         boxat = boxaTransformOrdered(boxa, SHIFTX_2, SHIFTY_2, 1.0, 1.0,
00118                                      450, 250, ROTATION_2, order);
00119         RenderTransformedBoxa(pixt, boxat, i);
00120         boxaDestroy(&boxat);
00121     }
00122     pixSaveTiled(pixt, pixa, 1, 1, 30, 32);
00123     pixDestroy(&pixt);
00124 
00125     pixt = pixConvertTo32(pixs);
00126     for (i = 0; i < 3; i++) {
00127         if (i == 0)
00128             order = L_RO_TR_SC;
00129         else if (i == 1)
00130             order = L_RO_SC_TR;
00131         else
00132             order = L_SC_RO_TR;
00133         boxat = boxaTransformOrdered(boxa, SHIFTX_2, SHIFTY_2, 1.0, 1.0,
00134                                      450, 250, ROTATION_2, order);
00135         RenderTransformedBoxa(pixt, boxat, i + 4);
00136         boxaDestroy(&boxat);
00137     }
00138     pixSaveTiled(pixt, pixa, 1, 1, 30, 0);
00139     pixDestroy(&pixt);
00140 
00141     pixt = pixConvertTo32(pixs);
00142     for (i = 0; i < 3; i++) {
00143         if (i == 0)
00144             order = L_TR_SC_RO;
00145         else if (i == 1)
00146             order = L_SC_RO_TR;
00147         else
00148             order = L_SC_TR_RO;
00149         boxat = boxaTransformOrdered(boxa, 0, 0, SCALEX_2, SCALEY_2,
00150                                      450, 250, ROTATION_2, order);
00151         RenderTransformedBoxa(pixt, boxat, i + 8);
00152         boxaDestroy(&boxat);
00153     }
00154     pixSaveTiled(pixt, pixa, 1, 1, 30, 0);
00155     pixDestroy(&pixt);
00156 
00157     pixt = pixConvertTo32(pixs);
00158     for (i = 0; i < 3; i++) {
00159         if (i == 0)
00160             order = L_RO_TR_SC;
00161         else if (i == 1)
00162             order = L_RO_SC_TR;
00163         else
00164             order = L_TR_RO_SC;
00165         boxat = boxaTransformOrdered(boxa, 0, 0, SCALEX_2, SCALEY_2,
00166                                      450, 250, ROTATION_2, order);
00167         RenderTransformedBoxa(pixt, boxat, i + 16);
00168         boxaDestroy(&boxat);
00169     }
00170     pixSaveTiled(pixt, pixa, 1, 1, 30, 0);
00171     pixDestroy(&pixt);
00172 
00173     pixt = pixaDisplay(pixa, 0, 0);
00174     pixWrite("/tmp/junkxform1.png", pixt, IFF_PNG);
00175     pixDisplay(pixt, 1000, 0);
00176     pixDestroy(&pixt);
00177     pixDestroy(&pixs);
00178     boxaDestroy(&boxa);
00179     pixaDestroy(&pixa);
00180 
00181 
00182     /* ----------------------------------------------------------- *
00183      *    Do more testing of box and pta transforms.  Show that    *
00184      *    resulting boxes are identical by three methods.          *
00185      * ----------------------------------------------------------- */
00186         /* Set up pix and boxa */ 
00187     pixa = pixaCreate(0);
00188     pix = pixRead("lucasta.1.300.tif");
00189     pixTranslate(pix, pix, 70, 0, L_BRING_IN_WHITE);
00190     pixt = pixCloseBrick(NULL, pix, 14, 5);
00191     pixOpenBrick(pixt, pixt, 1, 2);
00192     boxa = pixConnComp(pixt, NULL, 8);
00193     pixs = pixConvertTo32(pix);
00194     pixc = pixCopy(NULL, pixs);
00195     RenderTransformedBoxa(pixc, boxa, 113);
00196     pixSaveTiled(pixc, pixa, 2, 1, 30, 32);
00197     pixDestroy(&pix);
00198     pixDestroy(&pixc);
00199     pixDestroy(&pixt);
00200 
00201         /* (a) Do successive discrete operations: shift, scale, rotate */
00202     pixt1 = pixTranslate(NULL, pixs, SHIFTX_3, SHIFTY_3, L_BRING_IN_WHITE);    
00203     boxa1 = boxaTranslate(boxa, SHIFTX_3, SHIFTY_3);
00204     pixc = pixCopy(NULL, pixt1);
00205     RenderTransformedBoxa(pixc, boxa1, 213);
00206     pixSaveTiled(pixc, pixa, 2, 0, 30, 32);
00207     pixDestroy(&pixc);
00208 
00209     pixt2 = pixScale(pixt1, SCALEX_3, SCALEY_3);
00210     boxa2 = boxaScale(boxa1, SCALEX_3, SCALEY_3);
00211     pixc = pixCopy(NULL, pixt2);
00212     RenderTransformedBoxa(pixc, boxa2, 313);
00213     pixSaveTiled(pixc, pixa, 2, 1, 30, 32);
00214     pixDestroy(&pixc);
00215 
00216     pixGetDimensions(pixt2, &w, &h, NULL);
00217     pixt3 = pixRotateAM(pixt2, ROTATION_3, L_BRING_IN_WHITE);
00218     boxa3 = boxaRotate(boxa2, w / 2, h / 2, ROTATION_3);
00219     pixc = pixCopy(NULL, pixt3);
00220     RenderTransformedBoxa(pixc, boxa3, 413);
00221     pixSaveTiled(pixc, pixa, 2, 0, 30, 32);
00222     pixDestroy(&pixc);
00223 
00224         /* (b) Set up and use the composite transform */
00225     mat1 = createMatrix2dTranslate(SHIFTX_3, SHIFTY_3);
00226     mat2 = createMatrix2dScale(SCALEX_3, SCALEY_3);
00227     mat3 = createMatrix2dRotate(w / 2, h / 2, ROTATION_3);
00228     l_productMat3(mat3, mat2, mat1, matd, 3);
00229     boxa4 = boxaAffineTransform(boxa, matd);
00230     pixc = pixCopy(NULL, pixt3);
00231     RenderTransformedBoxa(pixc, boxa4, 513);
00232     pixSaveTiled(pixc, pixa, 2, 1, 30, 32);
00233     pixDestroy(&pixc);
00234 
00235         /* (c) Use the special 'ordered' function */
00236     pixGetDimensions(pixs, &ws, &hs, NULL);
00237     boxa5 = boxaTransformOrdered(boxa, SHIFTX_3, SHIFTY_3,
00238                                  SCALEX_3, SCALEY_3,
00239                                  ws / 2, hs / 2, ROTATION_3, L_TR_SC_RO);
00240     pixc = pixCopy(NULL, pixt3);
00241     RenderTransformedBoxa(pixc, boxa5, 613);
00242     pixSaveTiled(pixc, pixa, 2, 0, 30, 32);
00243     pixDestroy(&pixc);
00244                              
00245     pixDestroy(&pixt1);
00246     pixDestroy(&pixt2);
00247     pixDestroy(&pixt3);
00248     boxaDestroy(&boxa1);
00249     boxaDestroy(&boxa2);
00250     boxaDestroy(&boxa3);
00251     boxaDestroy(&boxa4);
00252     boxaDestroy(&boxa5);
00253     lept_free(mat1);
00254     lept_free(mat2);
00255     lept_free(mat3);
00256 
00257     pixt = pixaDisplay(pixa, 0, 0);
00258     pixWrite("/tmp/junkxform2.png", pixt, IFF_PNG);
00259     pixDisplay(pixt, 1000, 300);
00260     pixDestroy(&pixt);
00261     pixDestroy(&pixs);
00262     boxaDestroy(&boxa);
00263     pixaDestroy(&pixa);
00264     return 0;
00265 }
00266 
00267 
00268 l_int32
00269 RenderTransformedBoxa(PIX    *pixt,
00270                       BOXA   *boxa,
00271                       l_int32 i)
00272 {
00273 l_int32  j, n, rval, gval, bval;
00274 BOX     *box;
00275 
00276     n = boxaGetCount(boxa);
00277     rval = (1413 * i) % 256;
00278     gval = (4917 * i) % 256;
00279     bval = (7341 * i) % 256;
00280     for (j = 0; j < n; j++) {
00281         box = boxaGetBox(boxa, j, L_CLONE);
00282         pixRenderHashBoxArb(pixt, box, 10, 3, i % 4, 1, rval, gval, bval);
00283         boxDestroy(&box);
00284     }
00285     return 0;
00286 }
00287 
00288 
00289 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines