Leptonica 1.68
C Image Processing Library

conversion_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  * conversion_reg.c
00018  *
00019  *    Regression test (_reg) for depth conversion functions,
00020  *    including some of the octcube quantization.
00021  */
00022 
00023 #define  DFLAG    1
00024 
00025 #include "allheaders.h"
00026 
00027 main(int    argc,
00028      char **argv)
00029 {
00030 char        *errorstr;
00031 l_int32      same, error;
00032 PIX         *pixs1, *pixs2, *pixs4, *pixs8, *pixs16, *pixs32,  *pixd;
00033 PIX         *pixc2, *pixc4, *pixc8;
00034 PIX         *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6;
00035 PIXCMAP     *cmap;
00036 SARRAY      *sa;
00037 static char  mainName[] = "convert_reg";
00038 
00039     if (argc != 1)
00040         exit(ERROR_INT(" Syntax:  convert_rt", mainName, 1));
00041 
00042     if ((pixs1 = pixRead("test1.png")) == NULL)
00043         exit(ERROR_INT("pixs1 not made", mainName, 1));
00044     if ((pixs2 = pixRead("dreyfus2.png")) == NULL)
00045         exit(ERROR_INT("pixs2 not made", mainName, 1));
00046     if ((pixc2 = pixRead("weasel2.4c.png")) == NULL)
00047         exit(ERROR_INT("pixc2 not made", mainName, 1));
00048     if ((pixs4 = pixRead("weasel4.16g.png")) == NULL)
00049         exit(ERROR_INT("pixs4 not made", mainName, 1));
00050     if ((pixc4 = pixRead("weasel4.11c.png")) == NULL)
00051         exit(ERROR_INT("pixc4 not made", mainName, 1));
00052     if ((pixs8 = pixRead("karen8.jpg")) == NULL)
00053         exit(ERROR_INT("pixs8 not made", mainName, 1));
00054     if ((pixc8 = pixRead("weasel8.240c.png")) == NULL)
00055         exit(ERROR_INT("pixc8 not made", mainName, 1));
00056     if ((pixs16 = pixRead("test16.tif")) == NULL)
00057         exit(ERROR_INT("pixs16 not made", mainName, 1));
00058     if ((pixs32 = pixRead("marge.jpg")) == NULL)
00059         exit(ERROR_INT("pixs32 not made", mainName, 1));
00060     error = FALSE;
00061     sa = sarrayCreate(0);
00062 
00063         /* Conversion: 1 bpp --> 8 bpp --> 1 bpp */
00064     pixt1 = pixConvertTo8(pixs1, FALSE);
00065     pixt2 = pixThreshold8(pixt1, 1, 0, 0);
00066     pixEqual(pixs1, pixt2, &same);
00067     if (!same) {
00068         pixDisplayWithTitle(pixs1, 100, 100, "1 bpp, no cmap", DFLAG);
00069         pixDisplayWithTitle(pixt2, 500, 100, "1 bpp, no cmap", DFLAG);
00070         error = TRUE;
00071         sarrayAddString(sa, (char *)"conversion 1 bpp <==> 8 bpp", L_COPY);
00072     } else
00073         fprintf(stderr, "OK: conversion 1 bpp <==> 8 bpp\n");
00074     pixDestroy(&pixt1);
00075     pixDestroy(&pixt2);
00076 
00077         /* Conversion: 2 bpp --> 8 bpp --> 2 bpp */
00078         /* Conversion: 2 bpp cmap --> 8 bpp cmap --> 2 bpp cmap */
00079     pixt1 = pixRemoveColormap(pixs2, REMOVE_CMAP_TO_GRAYSCALE);
00080     pixt2 = pixThreshold8(pixt1, 2, 4, 0);
00081     pixt3 = pixConvertTo8(pixt2, FALSE);
00082     pixt4 = pixThreshold8(pixt3, 2, 4, 0);
00083     pixEqual(pixt2, pixt4, &same);
00084     if (!same) {
00085         pixDisplayWithTitle(pixt2, 100, 100, "2 bpp, no cmap", DFLAG);
00086         pixDisplayWithTitle(pixt4, 500, 100, "2 bpp, no cmap", DFLAG);
00087         error = TRUE;
00088         sarrayAddString(sa, (char *)"conversion 2 bpp <==> 8 bpp", L_COPY);
00089     } else
00090         fprintf(stderr, "OK: conversion 2 bpp <==> 8 bpp\n");
00091     pixt5 = pixConvertTo8(pixs2, TRUE);
00092     pixt6 = pixThreshold8(pixt5, 2, 4, 1);
00093     pixEqual(pixs2, pixt6, &same);
00094     if (!same) {
00095         pixDisplayWithTitle(pixs2, 100, 100, "2 bpp, cmap", DFLAG);
00096         pixDisplayWithTitle(pixt6, 500, 100, "2 bpp, cmap", DFLAG);
00097         error = TRUE;
00098         sarrayAddString(sa, (char *)"conversion 2 bpp <==> 8 bpp; cmap",
00099                         L_COPY);
00100     } else
00101         fprintf(stderr, "OK: conversion 2 bpp <==> 8 bpp; cmap\n");
00102     pixDestroy(&pixt1);
00103     pixDestroy(&pixt2);
00104     pixDestroy(&pixt3);
00105     pixDestroy(&pixt4);
00106     pixDestroy(&pixt5);
00107     pixDestroy(&pixt6);
00108 
00109         /* Conversion: 4 bpp --> 8 bpp --> 4 bpp */
00110         /* Conversion: 4 bpp cmap --> 8 bpp cmap --> 4 bpp cmap */
00111     pixt1 = pixRemoveColormap(pixs4, REMOVE_CMAP_TO_GRAYSCALE);
00112     pixt2 = pixThreshold8(pixt1, 4, 16, 0);
00113     pixt3 = pixConvertTo8(pixt2, FALSE);
00114     pixt4 = pixThreshold8(pixt3, 4, 16, 0);
00115     pixEqual(pixt2, pixt4, &same);
00116     if (!same) {
00117         pixDisplayWithTitle(pixt2, 100, 100, "4 bpp, no cmap", DFLAG);
00118         pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, no cmap", DFLAG);
00119         error = TRUE;
00120         sarrayAddString(sa, (char *)"conversion 4 bpp <==> 8 bpp", L_COPY);
00121     } else
00122         fprintf(stderr, "OK: conversion 4 bpp <==> 8 bpp\n");
00123     pixt5 = pixConvertTo8(pixs4, TRUE);
00124     pixt6 = pixThreshold8(pixt5, 4, 16, 1);
00125     pixEqual(pixs4, pixt6, &same);
00126     if (!same) {
00127         pixDisplayWithTitle(pixs4, 100, 100, "4 bpp, cmap", DFLAG);
00128         pixDisplayWithTitle(pixt6, 500, 100, "4 bpp, cmap", DFLAG);
00129         error = TRUE;
00130         sarrayAddString(sa, (char *)"conversion 4 bpp <==> 8 bpp, cmap",
00131                         L_COPY);
00132     } else
00133         fprintf(stderr, "OK: conversion 4 bpp <==> 8 bpp; cmap\n");
00134     pixDestroy(&pixt1);
00135     pixDestroy(&pixt2);
00136     pixDestroy(&pixt3);
00137     pixDestroy(&pixt4);
00138     pixDestroy(&pixt5);
00139     pixDestroy(&pixt6);
00140 
00141         /* Conversion: 2 bpp cmap --> 2 bpp --> 2 bpp cmap --> 2 bpp */
00142     pixt1 = pixRemoveColormap(pixs2, REMOVE_CMAP_TO_GRAYSCALE);
00143     pixt2 = pixConvertGrayToColormap(pixt1);
00144     pixt3 = pixRemoveColormap(pixt2, REMOVE_CMAP_TO_GRAYSCALE);
00145     pixt4 = pixThresholdTo2bpp(pixt3, 4, 1);
00146     pixEqual(pixt1, pixt4, &same);
00147     if (!same) {
00148         pixDisplayWithTitle(pixs2, 100, 100, "2 bpp, cmap", DFLAG);
00149         pixDisplayWithTitle(pixt4, 500, 100, "2 bpp, cmap", DFLAG);
00150         error = TRUE;
00151         sarrayAddString(sa, (char *)"conversion 2 bpp <==> 2 bpp", L_COPY);
00152     } else
00153         fprintf(stderr, "OK: conversion 2 bpp <==> 2 bpp\n");
00154     pixDestroy(&pixt1);
00155     pixDestroy(&pixt2);
00156     pixDestroy(&pixt3);
00157     pixDestroy(&pixt4);
00158 
00159         /* Conversion: 4 bpp cmap --> 4 bpp --> 4 bpp cmap --> 4 bpp */
00160     pixt1 = pixRemoveColormap(pixs4, REMOVE_CMAP_TO_GRAYSCALE);
00161     pixt2 = pixConvertGrayToColormap(pixt1);
00162     pixt3 = pixRemoveColormap(pixt2, REMOVE_CMAP_TO_GRAYSCALE);
00163     pixt4 = pixThresholdTo4bpp(pixt3, 16, 1);
00164     pixEqual(pixt1, pixt4, &same);
00165     if (!same) {
00166         pixDisplayWithTitle(pixs4, 100, 100, "4 bpp, cmap", DFLAG);
00167         pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, cmap", DFLAG);
00168         error = TRUE;
00169         sarrayAddString(sa, (char *)"conversion 4 bpp <==> 4 bpp", L_COPY);
00170     } else
00171         fprintf(stderr, "OK: conversion 4 bpp <==> 4 bpp\n");
00172     pixDestroy(&pixt1);
00173     pixDestroy(&pixt2);
00174     pixDestroy(&pixt3);
00175     pixDestroy(&pixt4);
00176 
00177 
00178         /* Conversion: 8 bpp --> 8 bpp cmap --> 8 bpp */
00179     pixt1 = pixConvertTo8(pixs8, TRUE);
00180     pixt2 = pixConvertTo8(pixt1, FALSE);
00181     pixEqual(pixs8, pixt2, &same);
00182     if (!same) {
00183         pixDisplayWithTitle(pixt1, 100, 100, "8 bpp, cmap", DFLAG);
00184         pixDisplayWithTitle(pixt2, 500, 100, "8 bpp, no cmap", DFLAG);
00185         error = TRUE;
00186         sarrayAddString(sa, (char *)"conversion 8 bpp <==> 8 bpp", L_COPY);
00187     } else
00188         fprintf(stderr, "OK: conversion 8 bpp <==> 8 bpp\n");
00189     pixDestroy(&pixt1);
00190     pixDestroy(&pixt2);
00191 
00192         /* Conversion: 2 bpp cmap --> 32 bpp --> 2 bpp cmap */
00193     pixt1 = pixConvertTo8(pixc2, TRUE);
00194     pixt2 = pixConvertTo32(pixt1);
00195     pixt3 = pixConvertTo32(pixc2);
00196     pixEqual(pixt2, pixt3, &same);
00197     if (!same) {
00198         pixDisplayWithTitle(pixt2, 100, 100, "32 bpp", DFLAG);
00199         pixDisplayWithTitle(pixt3, 500, 100, "32 bpp", DFLAG);
00200         error = TRUE;
00201         sarrayAddString(sa, (char *)"conversion 2 bpp ==> 32 bpp", L_COPY);
00202     } else
00203         fprintf(stderr, "OK: conversion 2 bpp <==> 32 bpp\n");
00204     cmap = pixGetColormap(pixc2);
00205     pixt4 = pixOctcubeQuantFromCmap(pixt3, cmap, 2, 4, L_EUCLIDEAN_DISTANCE);
00206     pixEqual(pixc2, pixt4, &same);
00207     if (!same) {
00208         pixDisplayWithTitle(pixc2, 100, 100, "4 bpp, cmap", DFLAG);
00209         pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, cmap", DFLAG);
00210         error = TRUE;
00211         sarrayAddString(sa, (char *)"conversion 2 bpp <==> 32 bpp", L_COPY);
00212     } else
00213         fprintf(stderr, "OK: conversion 2 bpp <==> 32 bpp\n");
00214     pixDestroy(&pixt1);
00215     pixDestroy(&pixt2);
00216     pixDestroy(&pixt3);
00217     pixDestroy(&pixt4);
00218 
00219         /* Conversion: 4 bpp cmap --> 32 bpp --> 4 bpp cmap */
00220     pixt1 = pixConvertTo8(pixc4, TRUE);
00221     pixt2 = pixConvertTo32(pixt1);
00222     pixt3 = pixConvertTo32(pixc4);
00223     pixEqual(pixt2, pixt3, &same);
00224     if (!same) {
00225         pixDisplayWithTitle(pixt2, 100, 100, "32 bpp", DFLAG);
00226         pixDisplayWithTitle(pixt3, 500, 100, "32 bpp", DFLAG);
00227         error = TRUE;
00228         sarrayAddString(sa, (char *)"conversion 4 bpp ==> 32 bpp", L_COPY);
00229     } else
00230         fprintf(stderr, "OK: conversion 4 bpp <==> 32 bpp\n");
00231     cmap = pixGetColormap(pixc4);
00232     pixt4 = pixOctcubeQuantFromCmap(pixt3, cmap, 2, 4, L_EUCLIDEAN_DISTANCE);
00233     pixEqual(pixc4, pixt4, &same);
00234     if (!same) {
00235         pixDisplayWithTitle(pixc4, 100, 100, "4 bpp, cmap", DFLAG);
00236         pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, cmap", DFLAG);
00237         error = TRUE;
00238         sarrayAddString(sa, (char *)"conversion 4 bpp <==> 32 bpp", L_COPY);
00239     } else
00240         fprintf(stderr, "OK: conversion 4 bpp <==> 32 bpp\n");
00241     pixDestroy(&pixt1);
00242     pixDestroy(&pixt2);
00243     pixDestroy(&pixt3);
00244     pixDestroy(&pixt4);
00245 
00246         /* Conversion: 8 bpp --> 32 bpp --> 8 bpp */
00247     pixt1 = pixConvertTo32(pixs8);
00248     pixt2 = pixConvertTo8(pixt1, FALSE);
00249     pixEqual(pixs8, pixt2, &same);
00250     if (!same) {
00251         pixDisplayWithTitle(pixs8, 100, 100, "8 bpp", DFLAG);
00252         pixDisplayWithTitle(pixt2, 500, 100, "8 bpp", DFLAG);
00253         error = TRUE;
00254         sarrayAddString(sa, (char *)"conversion 8 bpp <==> 32 bpp", L_COPY);
00255     } else
00256         fprintf(stderr, "OK: conversion 8 bpp <==> 32 bpp\n");
00257     pixDestroy(&pixt1);
00258     pixDestroy(&pixt2);
00259 
00260         /* Conversion: 8 bpp --> 16 bpp --> 8 bpp */
00261     pixt1 = pixConvert8To16(pixs8, 8);
00262     pixt2 = pixConvertTo8(pixt1, FALSE);
00263     pixEqual(pixs8, pixt2, &same);
00264     if (!same) {
00265         pixDisplayWithTitle(pixs8, 100, 100, "8 bpp", DFLAG);
00266         pixDisplayWithTitle(pixt2, 500, 100, "8 bpp", DFLAG);
00267         error = TRUE;
00268         sarrayAddString(sa, (char *)"conversion 8 bpp <==> 16 bpp", L_COPY);
00269     } else
00270         fprintf(stderr, "OK: conversion 8 bpp <==> 16 bpp\n");
00271     pixDestroy(&pixt1);
00272     pixDestroy(&pixt2);
00273 
00274         /* Conversion: 16 bpp --> 8 bpp --> 16 bpp */
00275     pixt1 = pixConvert16To8(pixs16, 1);
00276     pixt2 = pixConvertTo16(pixt1);
00277     pixWrite("/tmp/junkpix.png", pixt2, IFF_PNG);
00278     pixEqual(pixs16, pixt2, &same);
00279     if (!same) {
00280         pixDisplayWithTitle(pixs16, 100, 100, "16 bpp", DFLAG);
00281         pixDisplayWithTitle(pixt2, 500, 100, "16 bpp", DFLAG);
00282         error = TRUE;
00283         sarrayAddString(sa, (char *)"conversion 16 bpp <==> 8 bpp", L_COPY);
00284     } else
00285         fprintf(stderr, "OK: conversion 16 bpp <==> 8 bpp\n");
00286     pixDestroy(&pixt1);
00287     pixDestroy(&pixt2);
00288 
00289         /* Conversion: 8 bpp cmap --> 32 bpp --> 8 bpp cmap */
00290         /* Required to go to level 6 of octcube to get identical result */
00291     pixt1 = pixConvertTo32(pixc8);
00292     cmap = pixGetColormap(pixc8);
00293     pixt2 = pixOctcubeQuantFromCmap(pixt1, cmap, 2, 6, L_EUCLIDEAN_DISTANCE);
00294     pixEqual(pixc8, pixt2, &same);
00295     if (!same) {
00296         pixDisplayWithTitle(pixc8, 100, 100, "8 bpp cmap", DFLAG);
00297         pixDisplayWithTitle(pixt2, 500, 100, "8 bpp cmap", DFLAG);
00298         error = TRUE;
00299         sarrayAddString(sa, (char *)"conversion 8 bpp cmap <==> 32 bpp cmap",
00300                         L_COPY);
00301     } else
00302         fprintf(stderr, "OK: conversion 8 bpp <==> 32 bpp\n");
00303     pixDestroy(&pixt1);
00304     pixDestroy(&pixt2);
00305 
00306         /* Summarize results */
00307     if (error == FALSE) 
00308         fprintf(stderr, "No errors found\n");
00309     else {
00310         errorstr = sarrayToString(sa, 1);
00311         fprintf(stderr, "Errors in the following:\n %s", errorstr);
00312         lept_free(errorstr);
00313     }
00314 
00315     sarrayDestroy(&sa);
00316     pixDestroy(&pixs1);
00317     pixDestroy(&pixs2);
00318     pixDestroy(&pixs4);
00319     pixDestroy(&pixc2);
00320     pixDestroy(&pixc4);
00321     pixDestroy(&pixs8);
00322     pixDestroy(&pixc8);
00323     pixDestroy(&pixs16);
00324     pixDestroy(&pixs32);
00325     return 0;
00326 }
00327 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines