----------------------------------------------------------------------* * 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:
| 762 | * </pre> |
| 763 | */ |
| 764 | L_KERNEL * |
| 765 | kernelCreateFromFile(const char *filename) |
| 766 | { |
| 767 | char *filestr, *line; |
| 768 | l_int32 nlines, i, j, first, index, w, h, cx, cy, n; |
| 769 | l_float32 val; |
| 770 | size_t size; |
| 771 | NUMA *na, *nat; |
| 772 | SARRAY *sa; |
| 773 | L_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); |
nothing calls this directly
no test coverage detected