/*! * \brief recogWriteStream() * * \param[in] fp file stream opened for "wb" * \param[in] recog * \return 0 if OK, 1 on error */ l_int32 recogWriteStream(FILE *fp, L_RECOG *recog) { PROCNAME("recogWriteStream"); if (!fp) return ERROR_INT("stream not defined", procName, 1); if (!recog) return ERROR_INT("recog not defined", procName, 1); fprintf(fp, "\nRecog Version %d\n", RECOG_VERSION_NUMBER); fprintf(fp, "Size of character set = %d\n", recog->setsize); fprintf(fp, "Binarization threshold = %d\n", recog->threshold); fprintf(fp, "Maxyshift = %d\n", recog->maxyshift); fprintf(fp, "Scale to width = %d\n", recog->scalew); fprintf(fp, "Scale to height = %d\n", recog->scaleh); fprintf(fp, "Normalized line width = %d\n", recog->linew); fprintf(fp, "\nLabels for character set:\n"); l_dnaWriteStream(fp, recog->dna_tochar); sarrayWriteStream(fp, recog->sa_text); fprintf(fp, "\nPixaa of all samples in the training set:\n"); pixaaWriteStream(fp, recog->pixaa); return 0; }
/*! * l_dnaaWriteStream() * * Input: stream, daa * Return: 0 if OK, 1 on error */ l_int32 l_dnaaWriteStream(FILE *fp, L_DNAA *daa) { l_int32 i, n; L_DNA *da; PROCNAME("l_dnaaWriteStream"); if (!fp) return ERROR_INT("stream not defined", procName, 1); if (!daa) return ERROR_INT("daa not defined", procName, 1); n = l_dnaaGetCount(daa); fprintf(fp, "\nL_Dnaa Version %d\n", DNA_VERSION_NUMBER); fprintf(fp, "Number of L_Dna = %d\n\n", n); for (i = 0; i < n; i++) { if ((da = l_dnaaGetDna(daa, i, L_CLONE)) == NULL) return ERROR_INT("da not found", procName, 1); fprintf(fp, "L_Dna[%d]:", i); l_dnaWriteStream(fp, da); l_dnaDestroy(&da); } return 0; }
/*! * l_dnaWrite() * * Input: filename, da * Return: 0 if OK, 1 on error */ l_int32 l_dnaWrite(const char *filename, L_DNA *da) { FILE *fp; PROCNAME("l_dnaWrite"); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (!da) return ERROR_INT("da not defined", procName, 1); if ((fp = fopenWriteStream(filename, "w")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (l_dnaWriteStream(fp, da)) return ERROR_INT("da not written to stream", procName, 1); fclose(fp); return 0; }
main(int argc, char **argv) { char *str; l_uint8 *data1, *data2; l_int32 i, n, start, end, same1, same2; size_t size1, size2; FILE *fp; L_DNA *da; SARRAY *sa; L_BYTEA *lba1, *lba2, *lba3, *lba4, *lba5; static char mainName[] = "byteatest"; if (argc != 1) exit(ERROR_INT("syntax: whatever11", mainName, 1)); /* Test basic init, join and split */ lba1 = l_byteaInitFromFile("feyn.tif"); lba2 = l_byteaInitFromFile("test24.jpg"); size1 = l_byteaGetSize(lba1); size2 = l_byteaGetSize(lba2); l_byteaJoin(lba1, &lba2); lba3 = l_byteaInitFromMem(lba1->data, size1); lba4 = l_byteaInitFromMem(lba1->data + size1, size2); /* Split by hand */ l_binaryWrite("junk1", "w", lba3->data, lba3->size); l_binaryWrite("junk2", "w", lba4->data, lba4->size); filesAreIdentical("feyn.tif", "junk1", &same1); filesAreIdentical("test24.jpg", "junk2", &same2); if (same1 && same2) fprintf(stderr, "OK for join file\n"); else fprintf(stderr, "Error: files are different!\n"); /* Split by function */ l_byteaSplit(lba1, size1, &lba5); l_binaryWrite("junk3", "w", lba1->data, lba1->size); l_binaryWrite("junk4", "w", lba5->data, lba5->size); filesAreIdentical("feyn.tif", "junk3", &same1); filesAreIdentical("test24.jpg", "junk4", &same2); if (same1 && same2) fprintf(stderr, "OK for split file\n"); else fprintf(stderr, "Error: files are different!\n"); l_byteaDestroy(&lba1); l_byteaDestroy(&lba2); l_byteaDestroy(&lba3); l_byteaDestroy(&lba4); l_byteaDestroy(&lba5); /* Test appending */ data1 = l_binaryRead("whatever10.c", &size1); sa = sarrayCreateLinesFromString((char *)data1, 1); lba1 = l_byteaCreate(0); n = sarrayGetCount(sa); for (i = 0; i < n; i++) { str = sarrayGetString(sa, i, L_NOCOPY); l_byteaAppendString(lba1, str); l_byteaAppendString(lba1, (char *)"\n"); } data2 = l_byteaGetData(lba1, &size2); l_binaryWrite("junk1.txt", "w", data2, size2); filesAreIdentical("whatever10.c", "junk1.txt", &same1); if (same1) fprintf(stderr, "OK for appended file\n"); else fprintf(stderr, "Error: appended file is different!\n"); lept_free(data1); sarrayDestroy(&sa); l_byteaDestroy(&lba1); /* Test search */ convertToPdf("test24.jpg", L_JPEG_ENCODE, 0, "junk3.pdf", 0, 0, 100, NULL, 0, NULL); lba1 = l_byteaInitFromFile("junk3.pdf"); l_byteaFindEachSequence(lba1, (l_uint8 *)" 0 obj\n", 7, &da); l_dnaWriteStream(stderr, da); l_byteaDestroy(&lba1); l_dnaDestroy(&da); /* Test write to file */ lba1 = l_byteaInitFromFile("feyn.tif"); fp = lept_fopen("junk5", "wb"); size1 = l_byteaGetSize(lba1); for (start = 0; start < size1; start += 1000) { end = L_MIN(start + 1000 - 1, size1 - 1); l_byteaWriteStream(fp, lba1, start, end); } lept_fclose(fp); l_byteaDestroy(&lba1); return 0; }