Leptonica 1.68
C Image Processing Library

writetext_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  * writetext_reg.c
00018  *
00019  *   Regression test for writing a block of text in one of 4 locations
00020  *   relative to a pix.  This tests writing on 8 different types of images.
00021  *   Output is written to /tmp/pixd[1,2,3,4].png
00022  */
00023 
00024 #include "allheaders.h"
00025 
00026 void AddTextAndSave(PIXA *pixa, PIX *pixs, L_BMF *bmf, const char *textstr,
00027                     l_int32 location, l_uint32 val);
00028 
00029 const char  *textstr[] =
00030            {"This is a simple test of text writing: 8 bpp",
00031             "This is a simple test of text writing: 32 bpp",
00032             "This is a simple test of text writing: 8 bpp cmapped",
00033             "This is a simple test of text writing: 4 bpp cmapped",
00034             "This is a simple test of text writing: 4 bpp",
00035             "This is a simple test of text writing: 2 bpp cmapped",
00036             "This is a simple test of text writing: 2 bpp",
00037             "This is a simple test of text writing: 1 bpp"};
00038 
00039 const char  *topstr[] =
00040            {"Text is added above each image",
00041             "Text is added over the top of each image",
00042             "Text is added over the bottom of each image",
00043             "Text is added below each image"};
00044 
00045 const l_uint32  colors[6] = {0x4090e000, 0x40e09000, 0x9040e000, 0x90e04000,
00046                              0xe0409000, 0xe0904000};
00047 
00048 
00049 main(int    argc,
00050      char **argv)
00051 {
00052 char          outname[256], buf[512];
00053 l_int32       loc, i;
00054 L_BMF        *bmf, *bmftop;
00055 PIX          *pixs, *pixt, *pixd;
00056 PIX          *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8;
00057 PIXA         *pixa;
00058 L_REGPARAMS  *rp;
00059 SARRAY       *sa;
00060 
00061     if (regTestSetup(argc, argv, &rp))
00062         return 1;
00063 
00064     bmf = bmfCreate("./fonts", 6);
00065     bmftop = bmfCreate("./fonts", 10);
00066     pixs = pixRead("lucasta-47.jpg");
00067     pix1 = pixScale(pixs, 0.4, 0.4);          /* 8 bpp grayscale */
00068     pix2 = pixConvertTo32(pix1);              /* 32 bpp rgb */
00069     pix3 = pixThresholdOn8bpp(pix1, 12, 1);   /* 8 bpp cmapped */
00070     pix4 = pixThresholdTo4bpp(pix1, 10, 1);   /* 4 bpp cmapped */
00071     pix5 = pixThresholdTo4bpp(pix1, 10, 0);   /* 4 bpp not cmapped */
00072     pix6 = pixThresholdTo2bpp(pix1, 3, 1);    /* 2 bpp cmapped */
00073     pix7 = pixThresholdTo2bpp(pix1, 3, 0);    /* 2 bpp not cmapped */
00074     pix8 = pixThresholdToBinary(pix1, 160);   /* 1 bpp */
00075 
00076     for (loc = 1; loc < 5; loc++) {
00077         pixa = pixaCreate(0);
00078         AddTextAndSave(pixa, pix1, bmf, textstr[0], loc, 800);
00079         AddTextAndSave(pixa, pix2, bmf, textstr[1], loc, 0xff000000);
00080         AddTextAndSave(pixa, pix3, bmf, textstr[2], loc, 0x00ff0000);
00081         AddTextAndSave(pixa, pix4, bmf, textstr[3], loc, 0x0000ff00);
00082         AddTextAndSave(pixa, pix5, bmf, textstr[4], loc, 800);
00083         AddTextAndSave(pixa, pix6, bmf, textstr[5], loc, 0xff000000);
00084         AddTextAndSave(pixa, pix7, bmf, textstr[6], loc, 800);
00085         AddTextAndSave(pixa, pix8, bmf, textstr[7], loc, 800);
00086         pixt = pixaDisplay(pixa, 0, 0);
00087         pixd = pixAddSingleTextblock(pixt, bmftop, topstr[loc - 1],
00088                                      0xff00ff00, L_ADD_ABOVE, NULL);
00089         regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /*  0 - 4 */
00090         pixDisplayWithTitle(pixd, 50 * loc, 50, NULL, rp->display);
00091         pixDestroy(&pixt);
00092         pixDestroy(&pixd);
00093         pixaDestroy(&pixa);
00094     }
00095 
00096     pixDestroy(&pixs);
00097     pixDestroy(&pix1);
00098     pixDestroy(&pix2);
00099     pixDestroy(&pix3);
00100     pixDestroy(&pix4);
00101     pixDestroy(&pix5);
00102     pixDestroy(&pix6);
00103     pixDestroy(&pix7);
00104     pixDestroy(&pix8);
00105     bmfDestroy(&bmf);
00106     bmfDestroy(&bmftop);
00107 
00108         /* Write multiple lines in different colors, filling up
00109          * the colormap and requesting even more colors. */
00110     pixs = pixRead("weasel4.11c.png");
00111     pix1 = pixConvertTo8(pixs, 0);
00112     pix2 = pixScale(pixs, 8.0, 8.0);
00113     pix3 = pixQuantFromCmap(pix2, pixGetColormap(pixs), 4, 5,
00114                             L_EUCLIDEAN_DISTANCE);
00115     regTestWritePixAndCheck(rp, pix3, IFF_PNG);  /* 5 */
00116     pixDisplayWithTitle(pix3, 0, 500, NULL, rp->display);
00117     bmf = bmfCreate("fonts", 10);
00118     sa = sarrayCreate(6);
00119     for (i = 0; i < 6; i++) {
00120         snprintf(buf, sizeof(buf), "This is textline %d\n", i);
00121         sarrayAddString(sa, buf, L_COPY);
00122     }
00123     for (i = 0; i < 6; i++) {
00124         pixSetTextline(pix3, bmf, sarrayGetString(sa, i, L_NOCOPY),
00125                        colors[i], 50, 120 + 60 * i, NULL, NULL);
00126     }
00127     regTestWritePixAndCheck(rp, pix3, IFF_PNG);  /* 6 */
00128     pixDisplayWithTitle(pix3, 600, 500, NULL, rp->display);
00129     pixDestroy(&pixs);
00130     pixDestroy(&pix1);
00131     pixDestroy(&pix2);
00132     pixDestroy(&pix3);
00133     bmfDestroy(&bmf);
00134     sarrayDestroy(&sa);
00135 
00136     regTestCleanup(rp);
00137     return 0;
00138 }
00139 
00140 
00141 void
00142 AddTextAndSave(PIXA        *pixa,
00143                PIX         *pixs,
00144                L_BMF       *bmf,
00145                const char  *textstr,
00146                l_int32      location,
00147                l_uint32     val)
00148 {
00149 l_int32  n, newrow, ovf;
00150 PIX     *pixt;
00151 
00152     pixt = pixAddSingleTextblock(pixs, bmf, textstr, val, location, &ovf);
00153     n = pixaGetCount(pixa);
00154     newrow = (n % 4) ? 0 : 1;
00155     pixSaveTiledOutline(pixt, pixa, 1, newrow, 30, 2, 32);
00156     if (ovf) fprintf(stderr, "Overflow writing text in image %d\n", n + 1);
00157     pixDestroy(&pixt);
00158     return;
00159 }
00160 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines