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 * pixmem_reg.c 00018 * 00019 * Tests the low-level pix data accessors, and functions that 00020 * call them. 00021 */ 00022 00023 #include <stdio.h> 00024 #include <stdlib.h> 00025 #include "allheaders.h" 00026 00027 void Compare(PIX *pix1, PIX *pix2, l_int32 *perror); 00028 00029 main(int argc, 00030 char **argv) 00031 { 00032 l_int32 error; 00033 l_uint32 *data; 00034 PIX *pix1, *pix2, *pix3, *pix1c, *pix2c, *pix1t, *pix2t, *pixd; 00035 PIXA *pixa; 00036 static char mainName[] = "pixmem_reg"; 00037 00038 error = 0; 00039 pixa = pixaCreate(0); 00040 00041 /* Copy with internal resizing: onto a cmapped image */ 00042 pix1 = pixRead("weasel4.16c.png"); 00043 pix2 = pixRead("feyn-fract.tif"); 00044 pix3 = pixRead("lucasta.150.jpg"); 00045 fprintf(stderr, "before copy 2 --> 3\n"); 00046 pixCopy(pix3, pix2); 00047 Compare(pix2, pix3, &error); 00048 pixSaveTiled(pix3, pixa, 4, 1, 30, 32); 00049 fprintf(stderr, "before copy 3 --> 1\n"); 00050 pixCopy(pix1, pix3); 00051 Compare(pix2, pix1, &error); 00052 pixSaveTiled(pix1, pixa, 4, 0, 30, 32); 00053 pixDestroy(&pix1); 00054 pixDestroy(&pix2); 00055 pixDestroy(&pix3); 00056 00057 /* Copy with internal resizing: from a cmapped image */ 00058 pix1 = pixRead("weasel4.16c.png"); 00059 pix2 = pixRead("feyn-fract.tif"); 00060 pix3 = pixRead("lucasta.150.jpg"); 00061 fprintf(stderr, "before copy 1 --> 2\n"); 00062 pixCopy(pix2, pix1); 00063 Compare(pix2, pix1, &error); 00064 pixSaveTiled(pix2, pixa, 1, 1, 30, 32); 00065 fprintf(stderr, "before copy 2 --> 3\n"); 00066 pixCopy(pix3, pix2); 00067 Compare(pix3, pix2, &error); 00068 pixSaveTiled(pix3, pixa, 1, 0, 30, 32); 00069 pixDestroy(&pix1); 00070 pixDestroy(&pix2); 00071 pixDestroy(&pix3); 00072 00073 /* Transfer of data pixs --> pixd, when pixs is not cloned. 00074 * pixs is destroyed. */ 00075 pix1 = pixRead("weasel4.16c.png"); 00076 pix2 = pixRead("feyn-fract.tif"); 00077 pix3 = pixRead("lucasta.150.jpg"); 00078 pix1c = pixCopy(NULL, pix1); 00079 fprintf(stderr, "before transfer 1 --> 2\n"); 00080 pixTransferAllData(pix2, &pix1, 0, 0); 00081 Compare(pix2, pix1c, &error); 00082 pixSaveTiled(pix2, pixa, 1, 1, 30, 32); 00083 fprintf(stderr, "before transfer 2 --> 3\n"); 00084 pixTransferAllData(pix3, &pix2, 0, 0); 00085 Compare(pix3, pix1c, &error); 00086 pixSaveTiled(pix3, pixa, 1, 0, 30, 32); 00087 pixDestroy(&pix1c); 00088 pixDestroy(&pix3); 00089 00090 /* Another transfer of data pixs --> pixd, when pixs is not cloned. 00091 * pixs is destroyed. */ 00092 pix1 = pixRead("weasel4.16c.png"); 00093 pix2 = pixRead("feyn-fract.tif"); 00094 pix3 = pixRead("lucasta.150.jpg"); 00095 pix1c = pixCopy(NULL, pix1); 00096 pix2c = pixCopy(NULL, pix2); 00097 fprintf(stderr, "before copy transfer 1 --> 2\n"); 00098 pixTransferAllData(pix2, &pix1c, 0, 0); 00099 Compare(pix2, pix1, &error); 00100 pixSaveTiled(pix2, pixa, 1, 0, 30, 32); 00101 fprintf(stderr, "before copy transfer 2 --> 3\n"); 00102 pixTransferAllData(pix3, &pix2, 0, 0); 00103 Compare(pix3, pix1, &error); 00104 pixSaveTiled(pix3, pixa, 1, 0, 30, 32); 00105 pixDestroy(&pix1); 00106 pixDestroy(&pix2c); 00107 pixDestroy(&pix3); 00108 00109 /* Transfer of data pixs --> pixd, when pixs is cloned. 00110 * pixs has its refcount reduced by 1. */ 00111 pix1 = pixRead("weasel4.16c.png"); 00112 pix2 = pixRead("feyn-fract.tif"); 00113 pix3 = pixRead("lucasta.150.jpg"); 00114 pix1c = pixClone(pix1); 00115 pix2c = pixClone(pix2); 00116 fprintf(stderr, "before clone transfer 1 --> 2\n"); 00117 pixTransferAllData(pix2, &pix1c, 0, 0); 00118 Compare(pix2, pix1, &error); 00119 pixSaveTiled(pix2, pixa, 1, 0, 30, 32); 00120 fprintf(stderr, "before clone transfer 2 --> 3\n"); 00121 pixTransferAllData(pix3, &pix2c, 0, 0); 00122 Compare(pix3, pix1, &error); 00123 pixSaveTiled(pix3, pixa, 1, 0, 30, 32); 00124 pixDestroy(&pix1); 00125 pixDestroy(&pix2); 00126 pixDestroy(&pix3); 00127 00128 /* Extraction of data when pixs is not cloned, putting 00129 * the data into a new template of pixs. */ 00130 pix2 = pixRead("feyn-fract.tif"); 00131 fprintf(stderr, "no clone: before extraction and reinsertion of 2\n"); 00132 pix2c = pixCopy(NULL, pix2); /* for later reference */ 00133 data = pixExtractData(pix2); 00134 pix2t = pixCreateTemplateNoInit(pix2); 00135 pixFreeData(pix2t); 00136 pixSetData(pix2t, data); 00137 Compare(pix2c, pix2t, &error); 00138 pixSaveTiled(pix2t, pixa, 4, 1, 30, 32); 00139 pixDestroy(&pix2); 00140 pixDestroy(&pix2c); 00141 pixDestroy(&pix2t); 00142 00143 /* Extraction of data when pixs is cloned, putting 00144 * a copy of the data into a new template of pixs. */ 00145 pix1 = pixRead("weasel4.16c.png"); 00146 fprintf(stderr, "clone: before extraction and reinsertion of 1\n"); 00147 pix1c = pixClone(pix1); /* bump refcount of pix1 to 2 */ 00148 data = pixExtractData(pix1); /* should make a copy of data */ 00149 pix1t = pixCreateTemplateNoInit(pix1); 00150 pixFreeData(pix1t); 00151 pixSetData(pix1t, data); 00152 Compare(pix1c, pix1t, &error); 00153 pixSaveTiled(pix1t, pixa, 1, 0, 30, 32); 00154 pixDestroy(&pix1); 00155 pixDestroy(&pix1c); 00156 pixDestroy(&pix1t); 00157 00158 pixd = pixaDisplay(pixa, 0, 0); 00159 pixDisplay(pixd, 100, 100); 00160 pixWrite("/tmp/junkpixmem.png", pixd, IFF_PNG); 00161 pixaDestroy(&pixa); 00162 pixDestroy(&pixd); 00163 00164 if (error) 00165 fprintf(stderr, "Fail: an error occurred\n"); 00166 else 00167 fprintf(stderr, "Success: no errors\n"); 00168 return 0; 00169 } 00170 00171 00172 void Compare(PIX *pix1, 00173 PIX *pix2, 00174 l_int32 *perror) 00175 { 00176 l_int32 same; 00177 00178 if (!pix1 || !pix2) { 00179 fprintf(stderr, "pix not defined\n"); 00180 *perror = 1; 00181 return; 00182 } 00183 pixEqual(pix1, pix2, &same); 00184 if (same) 00185 fprintf(stderr, "OK\n"); 00186 else { 00187 fprintf(stderr, "Fail: not equal\n"); 00188 *perror = 1; 00189 } 00190 return; 00191 } 00192 00193