Leptonica 1.68
C Image Processing Library

lowaccess_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  * lowaccess_reg.c
00018  *
00019  *    Test low-level accessors 
00020  *
00021  *    Note that the gnu C++ compiler:
00022  *      * allows a non-void* ptr to be passed to a function f(void *ptr)
00023  *      * forbids a void* ptr to be passed to a function f(non-void *ptr)
00024  *        ('forbids' may be too strong: it issues a warning)
00025  *    
00026  *    For this reason, the l_getData*() and l_setData*() accessors
00027  *    now take a (void *)lineptr, but internally cast to (l_uint32 *)
00028  *    so that the addressing arithmetic works properly.
00029  *
00030  *    By the same token, the GET_DATA_*() and SET_DATA_*() macro
00031  *    accessors now cast the input ptr to (l_uint32 *) for 1, 2 and 4 bpp.
00032  *    This allows them to take a (void *)lineptr.
00033  */
00034 
00035 #include "allheaders.h"
00036 
00037 static l_int32 compareResults(PIX *pixs, PIX *pixt1, PIX *pixt2,
00038                               l_int32 count1, l_int32 count2,
00039                               const char *descr);
00040 
00041 main(int    argc,
00042      char **argv)
00043 {
00044 l_int32      x, y, i, j, k, w, h, w2, w4, w8, w16, w32, wpl, nerrors;
00045 l_int32      count1, count2, count3, ret, val1, val2;
00046 l_uint32     val32;
00047 l_uint32    *data, *line, *line1, *line2, *data1, *data2;
00048 void       **lines1, **linet1, **linet2;
00049 PIX         *pixs, *pixt1, *pixt2;
00050 static char  mainName[] = "lowaccess_reg";
00051 
00052     pixs = pixRead("feyn.tif");   /* width divisible by 16 */
00053     pixGetDimensions(pixs, &w, &h, NULL);
00054     data = pixGetData(pixs);
00055     wpl = pixGetWpl(pixs);
00056     lines1 = pixGetLinePtrs(pixs, NULL);
00057 
00058         /* Get timing for the 3 different methods */
00059     startTimer();
00060     for (k = 0; k < 10; k++) {
00061         count1 = 0;
00062         for (i = 0; i < h; i++) {
00063             for (j = 0; j < w; j++) {
00064                 if (GET_DATA_BIT(lines1[i], j))
00065                     count1++;
00066             }
00067         }
00068     }
00069     fprintf(stderr, "Time with line ptrs     = %5.3f sec, count1 = %d\n",
00070             stopTimer(), count1);
00071 
00072     startTimer();
00073     for (k = 0; k < 10; k++) {
00074         count2 = 0;
00075         for (i = 0; i < h; i++) {
00076             line = data + i * wpl;
00077             for (j = 0; j < w; j++) {
00078                if (l_getDataBit(line, j))
00079                     count2++;
00080             }
00081         }
00082     }
00083     fprintf(stderr, "Time with l_get*        = %5.3f sec, count2 = %d\n",
00084             stopTimer(), count2);
00085 
00086     startTimer();
00087     for (k = 0; k < 10; k++) {
00088         count3 = 0;
00089         for (i = 0; i < h; i++) {
00090             for (j = 0; j < w; j++) {
00091                 pixGetPixel(pixs, j, i, &val32);
00092                 count3 += val32;
00093             }
00094         }
00095     }
00096     fprintf(stderr, "Time with pixGetPixel() = %5.3f sec, count3 = %d\n",
00097             stopTimer(), count3);
00098 
00099     pixt1 = pixCreateTemplate(pixs);
00100     data1 = pixGetData(pixt1);
00101     linet1 = pixGetLinePtrs(pixt1, NULL);
00102     pixt2 = pixCreateTemplate(pixs);
00103     data2 = pixGetData(pixt2);
00104     linet2 = pixGetLinePtrs(pixt2, NULL);
00105 
00106     nerrors = 0;
00107 
00108         /* Test different methods for 1 bpp */
00109     count1 = 0;
00110     for (i = 0; i < h; i++) {
00111         for (j = 0; j < w; j++) {
00112             val1 = GET_DATA_BIT(lines1[i], j);
00113             count1 += val1;
00114             if (val1) SET_DATA_BIT(linet1[i], j);
00115         }
00116     }
00117     count2 = 0;
00118     for (i = 0; i < h; i++) {
00119         line = data + i * wpl;
00120         line2 = data2 + i * wpl;
00121         for (j = 0; j < w; j++) {
00122             val2 = l_getDataBit(line, j);
00123             count2 += val2;
00124             if (val2) l_setDataBit(line2, j);
00125         }
00126     }
00127     ret = compareResults(pixs, pixt1, pixt2, count1, count2, "1 bpp");
00128     nerrors += ret;
00129 
00130         /* Test different methods for 2 bpp */
00131     count1 = 0;
00132     w2 = w / 2;
00133     for (i = 0; i < h; i++) {
00134         for (j = 0; j < w2; j++) {
00135             val1 = GET_DATA_DIBIT(lines1[i], j);
00136             count1 += val1;
00137             val1 += 0xbbbbbbbc;
00138             SET_DATA_DIBIT(linet1[i], j, val1);
00139         }
00140     }
00141     count2 = 0;
00142     for (i = 0; i < h; i++) {
00143         line = data + i * wpl;
00144         line2 = data2 + i * wpl;
00145         for (j = 0; j < w2; j++) {
00146             val2 = l_getDataDibit(line, j);
00147             count2 += val2;
00148             val2 += 0xbbbbbbbc;
00149             l_setDataDibit(line2, j, val2);
00150         }
00151     }
00152     ret = compareResults(pixs, pixt1, pixt2, count1, count2, "2 bpp");
00153     nerrors += ret;
00154 
00155         /* Test different methods for 4 bpp */
00156     count1 = 0;
00157     w4 = w / 4;
00158     for (i = 0; i < h; i++) {
00159         for (j = 0; j < w4; j++) {
00160             val1 = GET_DATA_QBIT(lines1[i], j);
00161             count1 += val1;
00162             val1 += 0xbbbbbbb0;
00163             SET_DATA_QBIT(linet1[i], j, val1);
00164         }
00165     }
00166     count2 = 0;
00167     for (i = 0; i < h; i++) {
00168         line = data + i * wpl;
00169         line2 = data2 + i * wpl;
00170         for (j = 0; j < w4; j++) {
00171             val2 = l_getDataQbit(line, j);
00172             count2 += val2;
00173             val2 += 0xbbbbbbb0;
00174             l_setDataQbit(line2, j, val2);
00175         }
00176     }
00177     ret = compareResults(pixs, pixt1, pixt2, count1, count2, "4 bpp");
00178     nerrors += ret;
00179 
00180         /* Test different methods for 8 bpp */
00181     count1 = 0;
00182     w8 = w / 8;
00183     for (i = 0; i < h; i++) {
00184         for (j = 0; j < w8; j++) {
00185             val1 = GET_DATA_BYTE(lines1[i], j);
00186             count1 += val1;
00187             val1 += 0xbbbbbb00;
00188             SET_DATA_BYTE(linet1[i], j, val1);
00189         }
00190     }
00191     count2 = 0;
00192     for (i = 0; i < h; i++) {
00193         line = data + i * wpl;
00194         line2 = data2 + i * wpl;
00195         for (j = 0; j < w8; j++) {
00196             val2 = l_getDataByte(line, j);
00197             count2 += val2;
00198             val2 += 0xbbbbbb00;
00199             l_setDataByte(line2, j, val2);
00200         }
00201     }
00202     ret = compareResults(pixs, pixt1, pixt2, count1, count2, "8 bpp");
00203     nerrors += ret;
00204 
00205         /* Test different methods for 16 bpp */
00206     count1 = 0;
00207     w16 = w / 16;
00208     for (i = 0; i < h; i++) {
00209         for (j = 0; j < w16; j++) {
00210             val1 = GET_DATA_TWO_BYTES(lines1[i], j);
00211             count1 += val1;
00212             val1 += 0xbbbb0000;
00213             SET_DATA_TWO_BYTES(linet1[i], j, val1);
00214         }
00215     }
00216     count2 = 0;
00217     for (i = 0; i < h; i++) {
00218         line = data + i * wpl;
00219         line2 = data2 + i * wpl;
00220         for (j = 0; j < w16; j++) {
00221             val2 = l_getDataTwoBytes(line, j);
00222             count2 += val2;
00223             val2 += 0xbbbb0000;
00224             l_setDataTwoBytes(line2, j, val2);
00225         }
00226     }
00227     ret = compareResults(pixs, pixt1, pixt2, count1, count2, "16 bpp");
00228     nerrors += ret;
00229 
00230         /* Test different methods for 32 bpp */
00231     count1 = 0;
00232     w32 = w / 32;
00233     for (i = 0; i < h; i++) {
00234         for (j = 0; j < w32; j++) {
00235             val1 = GET_DATA_FOUR_BYTES(lines1[i], j);
00236             count1 += val1 & 0xfff;
00237             SET_DATA_FOUR_BYTES(linet1[i], j, val1);
00238         }
00239     }
00240     count2 = 0;
00241     for (i = 0; i < h; i++) {
00242         line = data + i * wpl;
00243         line2 = data2 + i * wpl;
00244         for (j = 0; j < w32; j++) {
00245             val2 = l_getDataFourBytes(line, j);
00246             count2 += val2 & 0xfff;
00247             l_setDataFourBytes(line2, j, val2);
00248         }
00249     }
00250     ret = compareResults(pixs, pixt1, pixt2, count1, count2, "32 bpp");
00251     nerrors += ret;
00252 
00253     if (!nerrors)
00254         fprintf(stderr, "****  No errors  ****\n");
00255     else
00256         fprintf(stderr, "****  %d errors found!  ****\n", nerrors);
00257 
00258     pixDestroy(&pixs);
00259     pixDestroy(&pixt1);
00260     pixDestroy(&pixt2);
00261     lept_free(lines1);
00262     lept_free(linet1);
00263     lept_free(linet2);
00264     return 0;
00265 }
00266 
00267 
00268 static l_int32
00269 compareResults(PIX         *pixs,
00270                PIX         *pixt1,
00271                PIX         *pixt2,
00272                l_int32      count1,
00273                l_int32      count2,
00274                const char  *descr)
00275 {
00276 l_int32  ret, same;
00277 
00278     ret = 0;
00279     pixEqual(pixs, pixt1, &same);
00280     if (!same) {
00281         fprintf(stderr, "pixt1 != pixs in %s\n", descr);
00282         ret = 1;
00283     }
00284     pixEqual(pixs, pixt2, &same);
00285     if (!same) {
00286         fprintf(stderr, "pixt2 != pixs in %s\n", descr);
00287         ret = 1;
00288     }
00289     if (count1 != count2) {
00290         fprintf(stderr, "Counts not same in %s\n", descr);
00291         ret = 1;
00292     } else
00293         fprintf(stderr, "Counts equal in %s: %d\n", descr, count1);
00294     pixClearAll(pixt1);
00295     pixClearAll(pixt2);
00296     if (!ret)
00297         fprintf(stderr, "All OK for %s\n", descr);
00298     return ret;
00299 }
00300 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines