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 * pdfseg_reg.c 00018 * 00019 * Generates segmented images and encodes them efficiently in pdf. 00020 * The encoding is mixed-raster, with the image parts encoded as 00021 * DCT at one resolution and the non-image parts encoded at (typically) 00022 * a higher resolution. 00023 * 00024 * Uses 6 images, all segmented and scaled to a fixed width 00025 */ 00026 00027 #include "allheaders.h" 00028 00029 /* All images scaled to this width */ 00030 static const l_int32 WIDTH = 800; 00031 00032 main(int argc, 00033 char **argv) 00034 { 00035 l_int32 h; 00036 l_float32 scalefactor; 00037 BOX *box; 00038 BOXA *boxa1, *boxa2; 00039 BOXAA *baa; 00040 PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8, *pix9; 00041 L_REGPARAMS *rp; 00042 00043 if (regTestSetup(argc, argv, &rp)) 00044 return 1; 00045 00046 lept_rmdir("segtest"); 00047 lept_mkdir("segtest"); 00048 baa = boxaaCreate(5); 00049 00050 /* Image region input. */ 00051 pix1 = pixRead("wet-day.jpg"); 00052 pix2 = pixScaleToSize(pix1, WIDTH, 0); 00053 pixWrite("/tmp/segtest/0.jpg", pix2, IFF_JFIF_JPEG); 00054 regTestCheckFile(rp, "/tmp/segtest/0.jpg"); /* 0 */ 00055 box = boxCreate(105, 161, 620, 872); /* image region */ 00056 boxa1 = boxaCreate(1); 00057 boxaAddBox(boxa1, box, L_INSERT); 00058 boxaaAddBoxa(baa, boxa1, L_INSERT); 00059 pixDestroy(&pix1); 00060 pixDestroy(&pix2); 00061 00062 /* Compute image region at w = 2 * WIDTH */ 00063 pix1 = pixRead("candelabrum-11.jpg"); 00064 pix2 = pixScaleToSize(pix1, WIDTH, 0); 00065 pix3 = pixConvertTo1(pix2, 100); 00066 pix4 = pixExpandBinaryPower2(pix3, 2); /* w = 2 * WIDTH */ 00067 pix5 = pixGenHalftoneMask(pix4, NULL, NULL, 1); 00068 pix6 = pixMorphSequence(pix5, "c20.1 + c1.20", 0); 00069 pix7 = pixMaskConnComp(pix6, 8, &boxa1); 00070 pix8 = pixReduceBinary2(pix7, NULL); /* back to w = WIDTH */ 00071 pix9 = pixBackgroundNormSimple(pix2, pix8, NULL); 00072 pixWrite("/tmp/segtest/1.jpg", pix9, IFF_JFIF_JPEG); 00073 regTestCheckFile(rp, "/tmp/segtest/1.jpg"); /* 1 */ 00074 boxa2 = boxaTransform(boxa1, 0, 0, 0.5, 0.5); /* back to w = WIDTH */ 00075 boxaaAddBoxa(baa, boxa2, L_INSERT); 00076 pixDestroy(&pix1); 00077 pixDestroy(&pix2); 00078 pixDestroy(&pix3); 00079 pixDestroy(&pix4); 00080 pixDestroy(&pix5); 00081 pixDestroy(&pix6); 00082 pixDestroy(&pix7); 00083 pixDestroy(&pix8); 00084 pixDestroy(&pix9); 00085 boxaDestroy(&boxa1); 00086 00087 /* Use mask to find image region */ 00088 pix1 = pixRead("lion-page.00016.jpg"); 00089 pix2 = pixScaleToSize(pix1, WIDTH, 0); 00090 pixWrite("/tmp/segtest/2.jpg", pix2, IFF_JFIF_JPEG); 00091 regTestCheckFile(rp, "/tmp/segtest/2.jpg"); /* 2 */ 00092 pix3 = pixRead("lion-mask.00016.tif"); 00093 pix4 = pixScaleToSize(pix3, WIDTH, 0); 00094 boxa1 = pixConnComp(pix4, NULL, 8); 00095 boxaaAddBoxa(baa, boxa1, L_INSERT); 00096 pixDestroy(&pix1); 00097 pixDestroy(&pix2); 00098 pixDestroy(&pix3); 00099 pixDestroy(&pix4); 00100 00101 /* Compute image region at full res */ 00102 pix1 = pixRead("rabi.png"); 00103 scalefactor = (l_float32)WIDTH / (l_float32)pixGetWidth(pix1); 00104 pix2 = pixScaleToGray(pix1, scalefactor); 00105 pixWrite("/tmp/segtest/3.jpg", pix2, IFF_JFIF_JPEG); 00106 regTestCheckFile(rp, "/tmp/segtest/3.jpg"); /* 3 */ 00107 pix3 = pixGenHalftoneMask(pix1, NULL, NULL, 0); 00108 pix4 = pixMorphSequence(pix3, "c20.1 + c1.20", 0); 00109 boxa1 = pixConnComp(pix4, NULL, 8); 00110 boxa2 = boxaTransform(boxa1, 0, 0, scalefactor, scalefactor); 00111 boxaaAddBoxa(baa, boxa2, L_INSERT); 00112 pixDestroy(&pix1); 00113 pixDestroy(&pix2); 00114 pixDestroy(&pix3); 00115 pixDestroy(&pix4); 00116 boxaDestroy(&boxa1); 00117 00118 /* Page with no image regions */ 00119 pix1 = pixRead("lucasta-47.jpg"); 00120 pix2 = pixScaleToSize(pix1, WIDTH, 0); 00121 boxa1 = boxaCreate(1); 00122 pixWrite("/tmp/segtest/4.jpg", pix2, IFF_JFIF_JPEG); 00123 regTestCheckFile(rp, "/tmp/segtest/4.jpg"); /* 4 */ 00124 boxaaAddBoxa(baa, boxa1, L_INSERT); 00125 pixDestroy(&pix1); 00126 pixDestroy(&pix2); 00127 00128 /* Page that is all image */ 00129 pix1 = pixRead("map1.jpg"); 00130 pix2 = pixScaleToSize(pix1, WIDTH, 0); 00131 pixWrite("/tmp/segtest/5.jpg", pix2, IFF_JFIF_JPEG); 00132 regTestCheckFile(rp, "/tmp/segtest/5.jpg"); /* 5 */ 00133 h = pixGetHeight(pix2); 00134 box = boxCreate(0, 0, WIDTH, h); 00135 boxa1 = boxaCreate(1); 00136 boxaAddBox(boxa1, box, L_INSERT); 00137 boxaaAddBoxa(baa, boxa1, L_INSERT); 00138 pixDestroy(&pix1); 00139 pixDestroy(&pix2); 00140 00141 /* Save the boxaa file */ 00142 boxaaWrite("/tmp/segtest/seg.baa", baa); 00143 regTestCheckFile(rp, "/tmp/segtest/seg.baa"); /* 6 */ 00144 00145 /* Do the conversion */ 00146 l_pdfSetDateAndVersion(FALSE); 00147 convertSegmentedFilesToPdf("/tmp/segtest", ".jpg", 100, L_G4_ENCODE, 00148 140, baa, 75, 0.6, "Segmentation Test", 00149 "/tmp/pdfseg.7.pdf"); 00150 regTestCheckFile(rp, "/tmp/pdfseg.7.pdf"); /* 7 */ 00151 00152 boxaaDestroy(&baa); 00153 regTestCleanup(rp); 00154 return 0; 00155 }