1 /*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
27 /*
28 * byteatest.c
29 *
30 */
31
32 #include <string.h>
33 #include "allheaders.h"
34
main(int argc,char ** argv)35 int main(int argc,
36 char **argv)
37 {
38 char *str;
39 l_uint8 *data1, *data2;
40 l_int32 i, n, same1, same2;
41 size_t size1, size2, slice, total, start, end;
42 FILE *fp;
43 L_DNA *da;
44 SARRAY *sa;
45 L_BYTEA *lba1, *lba2, *lba3, *lba4, *lba5;
46 static char mainName[] = "byteatest";
47
48 if (argc != 1)
49 return ERROR_INT("syntax: byteatest", mainName, 1);
50
51 setLeptDebugOK(1);
52 lept_mkdir("bytea");
53
54 /* Test basic init, join and split */
55 lba1 = l_byteaInitFromFile("feyn.tif");
56 lba2 = l_byteaInitFromFile("test24.jpg");
57 size1 = l_byteaGetSize(lba1);
58 size2 = l_byteaGetSize(lba2);
59 l_byteaJoin(lba1, &lba2);
60 lba3 = l_byteaInitFromMem(lba1->data, size1);
61 lba4 = l_byteaInitFromMem(lba1->data + size1, size2);
62
63 /* Split by hand */
64 l_binaryWrite("/tmp/bytea/junk1.dat", "w", lba3->data, lba3->size);
65 l_binaryWrite("/tmp/bytea/junk2.dat", "w", lba4->data, lba4->size);
66 filesAreIdentical("feyn.tif", "/tmp/bytea/junk1.dat", &same1);
67 filesAreIdentical("test24.jpg", "/tmp/bytea/junk2.dat", &same2);
68 if (same1 && same2)
69 fprintf(stderr, "OK for join file\n");
70 else
71 fprintf(stderr, "Error: files are different!\n");
72
73 /* Split by function */
74 l_byteaSplit(lba1, size1, &lba5);
75 l_binaryWrite("/tmp/bytea/junk3.dat", "w", lba1->data, lba1->size);
76 l_binaryWrite("/tmp/bytea/junk4.dat", "w", lba5->data, lba5->size);
77 filesAreIdentical("feyn.tif", "/tmp/bytea/junk3.dat", &same1);
78 filesAreIdentical("test24.jpg", "/tmp/bytea/junk4.dat", &same2);
79 if (same1 && same2)
80 fprintf(stderr, "OK for split file\n");
81 else
82 fprintf(stderr, "Error: files are different!\n");
83 l_byteaDestroy(&lba1);
84 l_byteaDestroy(&lba2);
85 l_byteaDestroy(&lba3);
86 l_byteaDestroy(&lba4);
87 l_byteaDestroy(&lba5);
88
89 /* Test appending with strings */
90 data1 = l_binaryRead("kernel_reg.c", &size1);
91 sa = sarrayCreateLinesFromString((char *)data1, 1);
92 lba1 = l_byteaCreate(0);
93 n = sarrayGetCount(sa);
94 for (i = 0; i < n; i++) {
95 str = sarrayGetString(sa, i, L_NOCOPY);
96 l_byteaAppendString(lba1, str);
97 l_byteaAppendString(lba1, (char *)"\n");
98 }
99 data2 = l_byteaGetData(lba1, &size2);
100 l_binaryWrite("/tmp/bytea/junk5.dat", "w", data2, size2);
101 filesAreIdentical("kernel_reg.c", "/tmp/bytea/junk5.dat", &same1);
102 if (same1)
103 fprintf(stderr, "OK for appended string data\n");
104 else
105 fprintf(stderr, "Error: appended string data is different!\n");
106 lept_free(data1);
107 sarrayDestroy(&sa);
108 l_byteaDestroy(&lba1);
109
110 /* Test appending with binary data */
111 slice = 1000;
112 total = nbytesInFile("breviar.38.150.jpg");
113 lba1 = l_byteaCreate(100);
114 n = 1 + total / slice;
115 fprintf(stderr, "******************************************************\n");
116 fprintf(stderr, "* Testing error checking: ignore two reported errors *\n");
117 for (i = 0, start = 0; i <= n; i++, start += slice) {
118 data1 = l_binaryReadSelect("breviar.38.150.jpg", start, slice, &size1);
119 l_byteaAppendData(lba1, data1, size1);
120 lept_free(data1);
121 }
122 fprintf(stderr, "******************************************************\n");
123 data2 = l_byteaGetData(lba1, &size2);
124 l_binaryWrite("/tmp/bytea/junk6.dat", "w", data2, size2);
125 filesAreIdentical("breviar.38.150.jpg", "/tmp/bytea/junk6.dat", &same1);
126 if (same1)
127 fprintf(stderr, "OK for appended binary data\n");
128 else
129 fprintf(stderr, "Error: appended binary data is different!\n");
130 l_byteaDestroy(&lba1);
131
132 /* Test search */
133 convertToPdf("test24.jpg", L_JPEG_ENCODE, 0, "/tmp/bytea/junk7.pdf",
134 0, 0, 100, NULL, NULL, 0);
135 lba1 = l_byteaInitFromFile("/tmp/bytea/junk7.pdf");
136 l_byteaFindEachSequence(lba1, (l_uint8 *)" 0 obj\n", 7, &da);
137 /* l_dnaWriteStream(stderr, da); */
138 n = l_dnaGetCount(da);
139 if (n == 6)
140 fprintf(stderr, "OK for search: found 6 instances\n");
141 else
142 fprintf(stderr, "Error in search: found %d instances, not 6\n", n);
143 l_byteaDestroy(&lba1);
144 l_dnaDestroy(&da);
145
146 /* Test write to file */
147 lba1 = l_byteaInitFromFile("feyn.tif");
148 fp = lept_fopen("/tmp/bytea/junk8.dat", "wb");
149 size1 = l_byteaGetSize(lba1);
150 for (start = 0; start < size1; start += 1000) {
151 end = L_MIN(start + 1000 - 1, size1 - 1);
152 l_byteaWriteStream(fp, lba1, start, end);
153 }
154 lept_fclose(fp);
155 filesAreIdentical("feyn.tif", "/tmp/bytea/junk8.dat", &same1);
156 if (same1)
157 fprintf(stderr, "OK for written binary data\n");
158 else
159 fprintf(stderr, "Error: written binary data is different!\n");
160 l_byteaDestroy(&lba1);
161
162 return 0;
163 }
164
165
166