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 * scale_reg.c 00018 * 00019 * This tests a number of scaling operations, through the pixScale() 00020 * interface. 00021 */ 00022 00023 #include "allheaders.h" 00024 00025 static const char *image[10] = {"feyn.tif", /* 1 bpp */ 00026 "weasel2.png", /* 2 bpp; no cmap */ 00027 "weasel2.4c.png", /* 2 bpp; cmap */ 00028 "weasel4.png", /* 4 bpp; no cmap */ 00029 "weasel4.16c.png", /* 4 bpp; cmap */ 00030 "weasel8.png", /* 8 bpp; no cmap */ 00031 "weasel8.240c.png", /* 8 bpp; cmap */ 00032 "test16.png", /* 16 bpp rgb */ 00033 "marge.jpg", /* 32 bpp rgb */ 00034 "test24.jpg"}; /* 32 bpp rgb */ 00035 00036 00037 static const l_int32 SPACE = 30; 00038 static const l_int32 WIDTH = 300; 00039 static const l_float32 FACTOR[5] = {2.3, 1.5, 1.1, 0.6, 0.3}; 00040 00041 static void AddScaledImages(PIXA *pixa, const char *fname, l_int32 width); 00042 static void PixSave32(PIXA *pixa, PIX *pixc); 00043 static void PixaSaveDisplay(PIXA *pixa, L_REGPARAMS *rp); 00044 00045 00046 main(int argc, 00047 char **argv) 00048 { 00049 l_int32 i; 00050 PIX *pixs, *pixc, *pixd; 00051 PIXA *pixa; 00052 L_REGPARAMS *rp; 00053 00054 if (regTestSetup(argc, argv, &rp)) 00055 return 1; 00056 00057 /* Test 1 bpp */ 00058 fprintf(stderr, "\n-------------- Testing 1 bpp ----------\n"); 00059 pixa = pixaCreate(0); 00060 pixs = pixRead(image[0]); 00061 pixc = pixScale(pixs, 0.32, 0.32); 00062 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00063 pixSaveTiled(pixc, pixa, 1, 1, SPACE, 32); 00064 pixDestroy(&pixc); 00065 00066 pixc = pixScaleToGray3(pixs); 00067 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00068 PixSave32(pixa, pixc); 00069 00070 pixc = pixScaleToGray4(pixs); 00071 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00072 pixSaveTiled(pixc, pixa, 1, 1, SPACE, 32); 00073 pixDestroy(&pixc); 00074 00075 pixc = pixScaleToGray6(pixs); 00076 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00077 PixSave32(pixa, pixc); 00078 00079 pixc = pixScaleToGray8(pixs); 00080 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00081 PixSave32(pixa, pixc); 00082 00083 pixc = pixScaleToGray16(pixs); 00084 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00085 PixSave32(pixa, pixc); 00086 pixDestroy(&pixs); 00087 PixaSaveDisplay(pixa, rp); 00088 00089 for (i = 1; i < 10; i++) { 00090 pixa = pixaCreate(0); 00091 AddScaledImages(pixa, image[i], WIDTH); 00092 PixaSaveDisplay(pixa, rp); 00093 } 00094 00095 /* Test 2 bpp without colormap */ 00096 fprintf(stderr, "\n-------------- Testing 2 bpp without cmap ----------\n"); 00097 pixa = pixaCreate(0); 00098 pixs = pixRead(image[1]); 00099 pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32); 00100 pixc = pixScale(pixs, 2.25, 2.25); 00101 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00102 PixSave32(pixa, pixc); 00103 pixc = pixScale(pixs, 0.85, 0.85); 00104 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00105 PixSave32(pixa, pixc); 00106 pixc = pixScale(pixs, 0.65, 0.65); 00107 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00108 PixSave32(pixa, pixc); 00109 PixaSaveDisplay(pixa, rp); 00110 pixDestroy(&pixs); 00111 00112 /* Test 2 bpp with colormap */ 00113 fprintf(stderr, "\n-------------- Testing 2 bpp with cmap ----------\n"); 00114 pixa = pixaCreate(0); 00115 pixs = pixRead(image[2]); 00116 pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32); 00117 pixc = pixScale(pixs, 2.25, 2.25); 00118 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00119 PixSave32(pixa, pixc); 00120 pixc = pixScale(pixs, 0.85, 0.85); 00121 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00122 PixSave32(pixa, pixc); 00123 pixc = pixScale(pixs, 0.65, 0.65); 00124 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00125 PixSave32(pixa, pixc); 00126 PixaSaveDisplay(pixa, rp); 00127 pixDestroy(&pixs); 00128 00129 /* Test 4 bpp without colormap */ 00130 fprintf(stderr, "\n-------------- Testing 4 bpp without cmap ----------\n"); 00131 pixa = pixaCreate(0); 00132 pixs = pixRead(image[3]); 00133 pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32); 00134 pixc = pixScale(pixs, 1.72, 1.72); 00135 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00136 PixSave32(pixa, pixc); 00137 pixc = pixScale(pixs, 0.85, 0.85); 00138 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00139 PixSave32(pixa, pixc); 00140 pixc = pixScale(pixs, 0.65, 0.65); 00141 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00142 PixSave32(pixa, pixc); 00143 PixaSaveDisplay(pixa, rp); 00144 pixDestroy(&pixs); 00145 00146 /* Test 4 bpp with colormap */ 00147 fprintf(stderr, "\n-------------- Testing 4 bpp with cmap ----------\n"); 00148 pixa = pixaCreate(0); 00149 pixs = pixRead(image[4]); 00150 pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32); 00151 pixc = pixScale(pixs, 1.72, 1.72); 00152 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00153 PixSave32(pixa, pixc); 00154 pixc = pixScale(pixs, 0.85, 0.85); 00155 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00156 PixSave32(pixa, pixc); 00157 pixc = pixScale(pixs, 0.65, 0.65); 00158 regTestWritePixAndCheck(rp, pixc, IFF_PNG); 00159 PixSave32(pixa, pixc); 00160 PixaSaveDisplay(pixa, rp); 00161 pixDestroy(&pixs); 00162 00163 /* Test 8 bpp without colormap */ 00164 fprintf(stderr, "\n-------------- Testing 8 bpp without cmap ----------\n"); 00165 pixa = pixaCreate(0); 00166 pixs = pixRead(image[5]); 00167 pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32); 00168 pixc = pixScale(pixs, 1.92, 1.92); 00169 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00170 PixSave32(pixa, pixc); 00171 pixc = pixScale(pixs, 0.85, 0.85); 00172 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00173 PixSave32(pixa, pixc); 00174 pixc = pixScale(pixs, 0.65, 0.65); 00175 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00176 PixSave32(pixa, pixc); 00177 PixaSaveDisplay(pixa, rp); 00178 pixDestroy(&pixs); 00179 00180 /* Test 8 bpp with colormap */ 00181 fprintf(stderr, "\n-------------- Testing 8 bpp with cmap ----------\n"); 00182 pixa = pixaCreate(0); 00183 pixs = pixRead(image[6]); 00184 pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32); 00185 pixc = pixScale(pixs, 1.92, 1.92); 00186 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00187 PixSave32(pixa, pixc); 00188 pixc = pixScale(pixs, 0.85, 0.85); 00189 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00190 PixSave32(pixa, pixc); 00191 pixc = pixScale(pixs, 0.65, 0.65); 00192 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00193 PixSave32(pixa, pixc); 00194 PixaSaveDisplay(pixa, rp); 00195 pixDestroy(&pixs); 00196 00197 /* Test 16 bpp */ 00198 fprintf(stderr, "\n-------------- Testing 16 bpp ------------\n"); 00199 pixa = pixaCreate(0); 00200 pixs = pixRead(image[7]); 00201 pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32); 00202 pixc = pixScale(pixs, 1.92, 1.92); 00203 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00204 PixSave32(pixa, pixc); 00205 pixc = pixScale(pixs, 0.85, 0.85); 00206 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00207 PixSave32(pixa, pixc); 00208 pixc = pixScale(pixs, 0.65, 0.65); 00209 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00210 PixSave32(pixa, pixc); 00211 PixaSaveDisplay(pixa, rp); 00212 pixDestroy(&pixs); 00213 00214 /* Test 32 bpp */ 00215 fprintf(stderr, "\n-------------- Testing 32 bpp ------------\n"); 00216 pixa = pixaCreate(0); 00217 pixs = pixRead(image[8]); 00218 pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32); 00219 pixc = pixScale(pixs, 1.42, 1.42); 00220 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00221 PixSave32(pixa, pixc); 00222 pixc = pixScale(pixs, 0.85, 0.85); 00223 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00224 PixSave32(pixa, pixc); 00225 pixc = pixScale(pixs, 0.65, 0.65); 00226 regTestWritePixAndCheck(rp, pixc, IFF_JFIF_JPEG); 00227 PixSave32(pixa, pixc); 00228 PixaSaveDisplay(pixa, rp); 00229 pixDestroy(&pixs); 00230 00231 regTestCleanup(rp); 00232 return 0; 00233 } 00234 00235 static void 00236 AddScaledImages(PIXA *pixa, 00237 const char *fname, 00238 l_int32 width) 00239 { 00240 l_int32 i, w; 00241 l_float32 scalefactor; 00242 PIX *pixs, *pixt1, *pixt2, *pix32; 00243 00244 pixs = pixRead(fname); 00245 w = pixGetWidth(pixs); 00246 for (i = 0; i < 5; i++) { 00247 scalefactor = (l_float32)width / (FACTOR[i] * (l_float32)w); 00248 pixt1 = pixScale(pixs, FACTOR[i], FACTOR[i]); 00249 pixt2 = pixScale(pixt1, scalefactor, scalefactor); 00250 pix32 = pixConvertTo32(pixt2); 00251 if (i == 0) 00252 pixSaveTiled(pix32, pixa, 1, 1, SPACE, 32); 00253 else 00254 pixSaveTiled(pix32, pixa, 1, 0, SPACE, 32); 00255 pixDestroy(&pixt1); 00256 pixDestroy(&pixt2); 00257 pixDestroy(&pix32); 00258 } 00259 pixDestroy(&pixs); 00260 return; 00261 } 00262 00263 static void 00264 PixSave32(PIXA *pixa, PIX *pixc) 00265 { 00266 PIX *pix32; 00267 pix32 = pixConvertTo32(pixc); 00268 pixSaveTiled(pix32, pixa, 1, 0, SPACE, 32); 00269 pixDestroy(&pixc); 00270 pixDestroy(&pix32); 00271 return; 00272 } 00273 00274 static void 00275 PixaSaveDisplay(PIXA *pixa, L_REGPARAMS *rp) 00276 { 00277 PIX *pixd; 00278 00279 pixd = pixaDisplay(pixa, 0, 0); 00280 regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); 00281 pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display); 00282 pixDestroy(&pixd); 00283 pixaDestroy(&pixa); 00284 return; 00285 } 00286