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 * pixserial_reg.c 00018 * 00019 * Tests the fast (uncompressed) serialization of pix to a string 00020 * in memory and the deserialization back to a pix. 00021 */ 00022 00023 #include "allheaders.h" 00024 00025 /* Use this set */ 00026 static l_int32 nfiles = 10; 00027 static const char *filename[] = { 00028 "feyn.tif", /* 1 bpp */ 00029 "dreyfus2.png", /* 2 bpp cmapped */ 00030 "dreyfus4.png", /* 4 bpp cmapped */ 00031 "weasel4.16c.png", /* 4 bpp cmapped */ 00032 "dreyfus8.png", /* 8 bpp cmapped */ 00033 "weasel8.240c.png", /* 8 bpp cmapped */ 00034 "karen8.jpg", /* 8 bpp, not cmapped */ 00035 "test16.tif", /* 8 bpp, not cmapped */ 00036 "marge.jpg", /* rgb */ 00037 "test24.jpg" /* rgb */ 00038 }; 00039 00040 main(int argc, 00041 char **argv) 00042 { 00043 char buf[256]; 00044 size_t size; 00045 l_int32 i, w, h; 00046 l_int32 format, bps, spp, iscmap, format2, w2, h2, bps2, spp2, iscmap2; 00047 l_uint8 *data; 00048 l_uint32 *data32, *data32r; 00049 BOX *box; 00050 PIX *pixs, *pixt, *pixt2, *pixd; 00051 L_REGPARAMS *rp; 00052 00053 if (regTestSetup(argc, argv, &rp)) 00054 return 1; 00055 00056 /* Test basic serialization/deserialization */ 00057 data32 = NULL; 00058 for (i = 0; i < nfiles; i++) { 00059 pixs = pixRead(filename[i]); 00060 /* Serialize to memory */ 00061 pixSerializeToMemory(pixs, &data32, &size); 00062 /* Just for fun, write and read back from file */ 00063 l_binaryWrite("/tmp/array", "w", data32, size); 00064 data32r = (l_uint32 *)l_binaryRead("/tmp/array", &size); 00065 /* Deserialize */ 00066 pixd = pixDeserializeFromMemory(data32r, size); 00067 regTestComparePix(rp, pixs, pixd); /* i */ 00068 pixDestroy(&pixd); 00069 pixDestroy(&pixs); 00070 lept_free(data32); 00071 lept_free(data32r); 00072 } 00073 00074 /* Test read/write fileio interface */ 00075 for (i = 0; i < nfiles; i++) { 00076 pixs = pixRead(filename[i]); 00077 pixGetDimensions(pixs, &w, &h, NULL); 00078 box = boxCreate(0, 0, L_MIN(150, w), L_MIN(150, h)); 00079 pixt = pixClipRectangle(pixs, box, NULL); 00080 boxDestroy(&box); 00081 snprintf(buf, sizeof(buf), "/tmp/pixs.%d.spix", rp->index + 1); 00082 pixWrite(buf, pixt, IFF_SPIX); 00083 regTestCheckFile(rp, buf); /* nfiles + 2 * i */ 00084 pixt2 = pixRead(buf); 00085 regTestComparePix(rp, pixt, pixt2); /* nfiles + 2 * i + 1 */ 00086 pixDestroy(&pixs); 00087 pixDestroy(&pixt); 00088 pixDestroy(&pixt2); 00089 } 00090 00091 /* Test read header. Note that for rgb input, spp = 3, 00092 * but for 32 bpp spix, we set spp = 4. */ 00093 data = NULL; 00094 for (i = 0; i < nfiles; i++) { 00095 pixs = pixRead(filename[i]); 00096 pixWriteMem(&data, &size, pixs, IFF_SPIX); 00097 pixReadHeader(filename[i], &format, &w, &h, &bps, &spp, &iscmap); 00098 pixReadHeaderMem(data, size, &format2, &w2, &h2, &bps2, 00099 &spp2, &iscmap2); 00100 if (format2 != IFF_SPIX || w != w2 || h != h2 || bps != bps2 || 00101 iscmap != iscmap2) { 00102 if (rp->fp) 00103 fprintf(rp->fp, "Failure comparing data"); 00104 else 00105 fprintf(stderr, "Failure comparing data"); 00106 } 00107 pixDestroy(&pixs); 00108 lept_free(data); 00109 } 00110 00111 #if 0 00112 /* Do timing */ 00113 for (i = 0; i < nfiles; i++) { 00114 pixs = pixRead(filename[i]); 00115 startTimer(); 00116 pixSerializeToMemory(pixs, &data32, &size); 00117 pixd = pixDeserializeFromMemory(data32, size); 00118 fprintf(stderr, "Time for %s: %7.3f sec\n", filename[i], stopTimer()); 00119 lept_free(data32); 00120 pixDestroy(&pixs); 00121 pixDestroy(&pixd); 00122 } 00123 #endif 00124 00125 regTestCleanup(rp); 00126 return 0; 00127 } 00128