MCPcopy Create free account
hub / github.com/creatale/node-dv / kernelCreateFromFile

Function kernelCreateFromFile

deps/leptonica/src/kernel.c:764–846  ·  view source on GitHub ↗

----------------------------------------------------------------------* * Making a kernel from a simple file format * *----------------------------------------------------------------------*/ ! * \brief kernelCreateFromFile() * * \param[in] filename * \return kernel, or NULL on error * * * Notes: * (1) The file contains, in the following order:

Source from the content-addressed store, hash-verified

762 * </pre>
763 */
764L_KERNEL *
765kernelCreateFromFile(const char *filename)
766{
767char *filestr, *line;
768l_int32 nlines, i, j, first, index, w, h, cx, cy, n;
769l_float32 val;
770size_t size;
771NUMA *na, *nat;
772SARRAY *sa;
773L_KERNEL *kel;
774
775 PROCNAME("kernelCreateFromFile");
776
777 if (!filename)
778 return (L_KERNEL *)ERROR_PTR("filename not defined", procName, NULL);
779
780 if ((filestr = (char *)l_binaryRead(filename, &size)) == NULL)
781 return (L_KERNEL *)ERROR_PTR("file not found", procName, NULL);
782 if (size == 0) {
783 LEPT_FREE(filestr);
784 return (L_KERNEL *)ERROR_PTR("file is empty", procName, NULL);
785 }
786
787 sa = sarrayCreateLinesFromString(filestr, 1);
788 LEPT_FREE(filestr);
789 nlines = sarrayGetCount(sa);
790
791 /* Find the first data line. */
792 for (i = 0, first = 0; i < nlines; i++) {
793 line = sarrayGetString(sa, i, L_NOCOPY);
794 if (line[0] != '#') {
795 first = i;
796 break;
797 }
798 }
799
800 /* Find the kernel dimensions and origin location. */
801 line = sarrayGetString(sa, first, L_NOCOPY);
802 if (sscanf(line, "%d %d", &h, &w) != 2) {
803 sarrayDestroy(&sa);
804 return (L_KERNEL *)ERROR_PTR("error reading h,w", procName, NULL);
805 }
806 line = sarrayGetString(sa, first + 1, L_NOCOPY);
807 if (sscanf(line, "%d %d", &cy, &cx) != 2) {
808 sarrayDestroy(&sa);
809 return (L_KERNEL *)ERROR_PTR("error reading cy,cx", procName, NULL);
810 }
811
812 /* Extract the data. This ends when we reach eof, or when we
813 * encounter a line of data that is either a null string or
814 * contains just a newline. */
815 na = numaCreate(0);
816 for (i = first + 2; i < nlines; i++) {
817 line = sarrayGetString(sa, i, L_NOCOPY);
818 if (line[0] == '\0' || line[0] == '\n' || line[0] == '#')
819 break;
820 nat = parseStringForNumbers(line, " \t\n");
821 numaJoin(na, nat, 0, -1);

Callers

nothing calls this directly

Calls 14

l_binaryReadFunction · 0.85
sarrayGetCountFunction · 0.85
sarrayGetStringFunction · 0.85
sarrayDestroyFunction · 0.85
numaCreateFunction · 0.85
parseStringForNumbersFunction · 0.85
numaJoinFunction · 0.85
numaDestroyFunction · 0.85
numaGetCountFunction · 0.85
kernelCreateFunction · 0.85
kernelSetOriginFunction · 0.85

Tested by

no test coverage detected