84 #include <config_auto.h>
89 #include "allheaders.h"
92 static const l_uint32 MaxArraySize = 100000;
118 PROCNAME(
"kernelCreate");
121 return (
L_KERNEL *)ERROR_PTR(
"width must be > 0", procName, NULL);
123 return (
L_KERNEL *)ERROR_PTR(
"height must be > 0", procName, NULL);
126 size64 = (l_uint64)width * (l_uint64)height;
127 if (size64 >= (1LL << 29)) {
128 L_ERROR(
"requested width = %d, height = %d\n", procName, width, height);
129 return (
L_KERNEL *)ERROR_PTR(
"size >= 2^29", procName, NULL);
137 return (
L_KERNEL *)ERROR_PTR(
"data not allocated", procName, NULL);
155 PROCNAME(
"kernelDestroy");
158 L_WARNING(
"ptr address is NULL!\n", procName);
161 if ((kel = *pkel) == NULL)
164 for (i = 0; i < kel->
sy; i++)
165 LEPT_FREE(kel->
data[i]);
166 LEPT_FREE(kel->
data);
181 l_int32 i, j, sx, sy, cx, cy;
184 PROCNAME(
"kernelCopy");
187 return (
L_KERNEL *)ERROR_PTR(
"kels not defined", procName, NULL);
191 return (
L_KERNEL *)ERROR_PTR(
"keld not made", procName, NULL);
194 for (i = 0; i < sy; i++)
195 for (j = 0; j < sx; j++)
196 keld->
data[i][j] = kels->
data[i][j];
220 PROCNAME(
"kernelGetElement");
223 return ERROR_INT(
"&val not defined", procName, 1);
226 return ERROR_INT(
"kernel not defined", procName, 1);
227 if (row < 0 || row >= kel->
sy)
228 return ERROR_INT(
"kernel row out of bounds", procName, 1);
229 if (col < 0 || col >= kel->
sx)
230 return ERROR_INT(
"kernel col out of bounds", procName, 1);
232 *pval = kel->
data[row][col];
252 PROCNAME(
"kernelSetElement");
255 return ERROR_INT(
"kel not defined", procName, 1);
256 if (row < 0 || row >= kel->
sy)
257 return ERROR_INT(
"kernel row out of bounds", procName, 1);
258 if (col < 0 || col >= kel->
sx)
259 return ERROR_INT(
"kernel col out of bounds", procName, 1);
261 kel->
data[row][col] = val;
280 PROCNAME(
"kernelGetParameters");
287 return ERROR_INT(
"kernel not defined", procName, 1);
288 if (psy) *psy = kel->
sy;
289 if (psx) *psx = kel->
sx;
290 if (pcy) *pcy = kel->
cy;
291 if (pcx) *pcx = kel->
cx;
308 PROCNAME(
"kernelSetOrigin");
311 return ERROR_INT(
"kel not defined", procName, 1);
329 l_int32 sx, sy, i, j;
331 PROCNAME(
"kernelGetSum");
334 return ERROR_INT(
"&sum not defined", procName, 1);
337 return ERROR_INT(
"kernel not defined", procName, 1);
340 for (i = 0; i < sy; i++) {
341 for (j = 0; j < sx; j++) {
342 *psum += kel->
data[i][j];
362 l_int32 sx, sy, i, j;
363 l_float32 val, minval, maxval;
365 PROCNAME(
"kernelGetMinmax");
368 return ERROR_INT(
"neither &min nor &max defined", procName, 1);
369 if (pmin) *pmin = 0.0;
370 if (pmax) *pmax = 0.0;
372 return ERROR_INT(
"kernel not defined", procName, 1);
376 maxval = -10000000.0;
377 for (i = 0; i < sy; i++) {
378 for (j = 0; j < sx; j++) {
379 val = kel->
data[i][j];
417 l_int32 i, j, sx, sy, cx, cy;
418 l_float32 sum, factor;
421 PROCNAME(
"kernelNormalize");
424 return (
L_KERNEL *)ERROR_PTR(
"kels not defined", procName, NULL);
427 if (L_ABS(sum) < 0.00001) {
428 L_WARNING(
"null sum; not normalizing; returning a copy\n", procName);
434 return (
L_KERNEL *)ERROR_PTR(
"keld not made", procName, NULL);
438 factor = normsum / sum;
439 for (i = 0; i < sy; i++)
440 for (j = 0; j < sx; j++)
441 keld->
data[i][j] = factor * kels->
data[i][j];
462 l_int32 i, j, sx, sy, cx, cy;
465 PROCNAME(
"kernelInvert");
468 return (
L_KERNEL *)ERROR_PTR(
"kels not defined", procName, NULL);
472 return (
L_KERNEL *)ERROR_PTR(
"keld not made", procName, NULL);
473 keld->
cy = sy - 1 - cy;
474 keld->
cx = sx - 1 - cx;
476 for (i = 0; i < sy; i++)
477 for (j = 0; j < sx; j++)
478 keld->
data[i][j] = kels->
data[sy - 1 - i][sx - 1 - j];
509 PROCNAME(
"create2dFloatArray");
511 if (sx <= 0 || sx > MaxArraySize)
512 return (l_float32 **)ERROR_PTR(
"sx out of bounds", procName, NULL);
513 if (sy <= 0 || sy > MaxArraySize)
514 return (l_float32 **)ERROR_PTR(
"sy out of bounds", procName, NULL);
516 array = (l_float32 **)LEPT_CALLOC(sy,
sizeof(l_float32 *));
517 for (i = 0; i < sy; i++)
518 array[i] = (l_float32 *)LEPT_CALLOC(sx,
sizeof(l_float32));
538 PROCNAME(
"kernelRead");
541 return (
L_KERNEL *)ERROR_PTR(
"fname not defined", procName, NULL);
544 return (
L_KERNEL *)ERROR_PTR(
"stream not opened", procName, NULL);
547 return (
L_KERNEL *)ERROR_PTR(
"kel not returned", procName, NULL);
564 l_int32 sy, sx, cy, cx, i, j, ret, version, ignore;
567 PROCNAME(
"kernelReadStream");
570 return (
L_KERNEL *)ERROR_PTR(
"stream not defined", procName, NULL);
572 ret = fscanf(fp,
" Kernel Version %d\n", &version);
574 return (
L_KERNEL *)ERROR_PTR(
"not a kernel file", procName, NULL);
575 if (version != KERNEL_VERSION_NUMBER)
576 return (
L_KERNEL *)ERROR_PTR(
"invalid kernel version", procName, NULL);
578 if (fscanf(fp,
" sy = %d, sx = %d, cy = %d, cx = %d\n",
579 &sy, &sx, &cy, &cx) != 4)
580 return (
L_KERNEL *)ERROR_PTR(
"dimensions not read", procName, NULL);
581 if (sx > MaxArraySize || sy > MaxArraySize) {
582 L_ERROR(
"sx = %d or sy = %d > %d\n", procName, sx, sy, MaxArraySize);
586 return (
L_KERNEL *)ERROR_PTR(
"kel not made", procName, NULL);
589 for (i = 0; i < sy; i++) {
590 for (j = 0; j < sx; j++)
591 ignore = fscanf(fp,
"%15f", &kel->
data[i][j]);
592 ignore = fscanf(fp,
"\n");
594 ignore = fscanf(fp,
"\n");
613 PROCNAME(
"kernelWrite");
616 return ERROR_INT(
"fname not defined", procName, 1);
618 return ERROR_INT(
"kel not defined", procName, 1);
621 return ERROR_INT(
"stream not opened", procName, 1);
640 l_int32 sx, sy, cx, cy, i, j;
642 PROCNAME(
"kernelWriteStream");
645 return ERROR_INT(
"stream not defined", procName, 1);
647 return ERROR_INT(
"kel not defined", procName, 1);
650 fprintf(fp,
" Kernel Version %d\n", KERNEL_VERSION_NUMBER);
651 fprintf(fp,
" sy = %d, sx = %d, cy = %d, cx = %d\n", sy, sx, cy, cx);
652 for (i = 0; i < sy; i++) {
653 for (j = 0; j < sx; j++)
654 fprintf(fp,
"%15.4f", kel->
data[i][j]);
695 l_int32 n, i, j, index;
700 PROCNAME(
"kernelCreateFromString");
703 return (
L_KERNEL *)ERROR_PTR(
"height must be > 0", procName, NULL);
705 return (
L_KERNEL *)ERROR_PTR(
"width must be > 0", procName, NULL);
706 if (cy < 0 || cy >= h)
707 return (
L_KERNEL *)ERROR_PTR(
"cy invalid", procName, NULL);
708 if (cx < 0 || cx >= w)
709 return (
L_KERNEL *)ERROR_PTR(
"cx invalid", procName, NULL);
718 lept_stderr(
"w = %d, h = %d, num ints = %d\n", w, h, n);
719 return (
L_KERNEL *)ERROR_PTR(
"invalid integer data", procName, NULL);
723 for (i = 0; i < h; i++) {
724 for (j = 0; j < w; j++) {
777 char *filestr, *line;
778 l_int32 nlines, i, j, first, index, w, h, cx, cy, n;
785 PROCNAME(
"kernelCreateFromFile");
788 return (
L_KERNEL *)ERROR_PTR(
"filename not defined", procName, NULL);
790 if ((filestr = (
char *)
l_binaryRead(filename, &size)) == NULL)
791 return (
L_KERNEL *)ERROR_PTR(
"file not found", procName, NULL);
794 return (
L_KERNEL *)ERROR_PTR(
"file is empty", procName, NULL);
802 for (i = 0, first = 0; i < nlines; i++) {
804 if (line[0] !=
'#') {
812 if (sscanf(line,
"%d %d", &h, &w) != 2) {
814 return (
L_KERNEL *)ERROR_PTR(
"error reading h,w", procName, NULL);
816 if (h > MaxArraySize || w > MaxArraySize) {
817 L_ERROR(
"h = %d or w = %d > %d\n", procName, h, w, MaxArraySize);
822 if (sscanf(line,
"%d %d", &cy, &cx) != 2) {
824 return (
L_KERNEL *)ERROR_PTR(
"error reading cy,cx", procName, NULL);
831 for (i = first + 2; i < nlines; i++) {
833 if (line[0] ==
'\0' || line[0] ==
'\n' || line[0] ==
'#')
844 lept_stderr(
"w = %d, h = %d, num ints = %d\n", w, h, n);
845 return (
L_KERNEL *)ERROR_PTR(
"invalid integer data", procName, NULL);
851 for (i = 0; i < h; i++) {
852 for (j = 0; j < w; j++) {
884 l_int32 i, j, w, h, d;
888 PROCNAME(
"kernelCreateFromPix");
891 return (
L_KERNEL *)ERROR_PTR(
"pix not defined", procName, NULL);
894 return (
L_KERNEL *)ERROR_PTR(
"pix not 8 bpp", procName, NULL);
895 if (cy < 0 || cx < 0 || cy >= h || cx >= w)
896 return (
L_KERNEL *)ERROR_PTR(
"(cy, cx) invalid", procName, NULL);
900 for (i = 0; i < h; i++) {
901 for (j = 0; j < w; j++) {
945 l_int32 i, j, w, h, sx, sy, cx, cy, width, x0, y0;
947 l_float32 minval, maxval, max, val, norm;
948 PIX *pixd, *pixt0, *pixt1;
950 PROCNAME(
"kernelDisplayInPix");
953 return (
PIX *)ERROR_PTR(
"kernel not defined", procName, NULL);
958 max = L_MAX(maxval, -minval);
960 return (
PIX *)ERROR_PTR(
"kernel elements all 0.0", procName, NULL);
961 norm = 255. / (l_float32)max;
964 if (size == 1 && gthick == 0) {
966 for (i = 0; i < sy; i++) {
967 for (j = 0; j < sx; j++) {
969 normval = (l_int32)(norm * L_ABS(val));
978 L_WARNING(
"size < 17; setting to 17\n", procName);
984 L_WARNING(
"grid thickness < 2; setting to 2\n", procName);
988 w = size * sx + gthick * (sx + 1);
989 h = size * sy + gthick * (sy + 1);
993 for (i = 0; i <= sy; i++)
995 w - 1, gthick / 2 + i * (size + gthick),
997 for (j = 0; j <= sx; j++)
999 gthick / 2 + j * (size + gthick), h - 1,
1010 size / 2, (l_int32)(0.88 * size),
1013 (l_int32)(0.85 * size), size / 2,
1015 pixRasterop(pixt1, size / 2 - width, size / 2 - width,
1020 for (i = 0; i < sy; i++) {
1022 for (j = 0; j < sx; j++) {
1024 normval = (l_int32)(norm * L_ABS(val));
1026 if (i == cy && j == cx)
1028 x0 += size + gthick;
1030 y0 += size + gthick;
1058 char *newstr, *head;
1063 PROCNAME(
"parseStringForNumbers");
1066 return (
NUMA *)ERROR_PTR(
"str not defined", procName, NULL);
1074 while ((head =
strtokSafe(NULL, seps, &tail)) != NULL) {
1116 PROCNAME(
"makeFlatKernel");
1119 return (
L_KERNEL *)ERROR_PTR(
"kel not made", procName, NULL);
1121 normval = 1.0 / (l_float32)(height * width);
1122 for (i = 0; i < height; i++) {
1123 for (j = 0; j < width; j++) {
1158 l_int32 sx, sy, i, j;
1162 PROCNAME(
"makeGaussianKernel");
1167 return (
L_KERNEL *)ERROR_PTR(
"kel not made", procName, NULL);
1169 for (i = 0; i < sy; i++) {
1170 for (j = 0; j < sx; j++) {
1171 val = expf(-(l_float32)((i - halfh) * (i - halfh) +
1172 (j - halfw) * (j - halfw)) /
1173 (2. * stdev * stdev));
1215 PROCNAME(
"makeGaussianKernelSep");
1217 if (!pkelx || !pkely)
1218 return ERROR_INT(
"&kelx and &kely not defined", procName, 1);
1259 l_int32 sx, sy, i, j;
1260 l_float32 pi, squaredist, highnorm, lownorm, val;
1263 PROCNAME(
"makeDoGKernel");
1268 return (
L_KERNEL *)ERROR_PTR(
"kel not made", procName, NULL);
1272 for (i = 0; i < sy; i++) {
1273 for (j = 0; j < sx; j++) {
1274 squaredist = (l_float32)((i - halfh) * (i - halfh) +
1275 (j - halfw) * (j - halfw));
1276 highnorm = 1. / (2 * stdev * stdev);
1277 lownorm = highnorm / (ratio * ratio);
1278 val = (highnorm / pi) * expf(-(highnorm * squaredist))
1279 - (lownorm / pi) * expf(-(lownorm * squaredist));
l_ok pixRenderLine(PIX *pix, l_int32 x1, l_int32 y1, l_int32 x2, l_int32 y2, l_int32 width, l_int32 op)
pixRenderLine()
L_KERNEL * kernelRead(const char *fname)
kernelRead()
l_ok kernelGetMinMax(L_KERNEL *kel, l_float32 *pmin, l_float32 *pmax)
kernelGetMinMax()
L_KERNEL * kernelInvert(L_KERNEL *kels)
kernelInvert()
L_KERNEL * makeFlatKernel(l_int32 height, l_int32 width, l_int32 cy, l_int32 cx)
makeFlatKernel()
void kernelDestroy(L_KERNEL **pkel)
kernelDestroy()
l_ok kernelWrite(const char *fname, L_KERNEL *kel)
kernelWrite()
l_ok kernelSetOrigin(L_KERNEL *kel, l_int32 cy, l_int32 cx)
kernelSetOrigin()
L_KERNEL * kernelCreate(l_int32 height, l_int32 width)
kernelCreate()
l_ok kernelGetParameters(L_KERNEL *kel, l_int32 *psy, l_int32 *psx, l_int32 *pcy, l_int32 *pcx)
kernelGetParameters()
l_ok kernelGetSum(L_KERNEL *kel, l_float32 *psum)
kernelGetSum()
NUMA * parseStringForNumbers(const char *str, const char *seps)
parseStringForNumbers()
L_KERNEL * makeGaussianKernel(l_int32 halfh, l_int32 halfw, l_float32 stdev, l_float32 max)
makeGaussianKernel()
L_KERNEL * kernelNormalize(L_KERNEL *kels, l_float32 normsum)
kernelNormalize()
l_ok kernelWriteStream(FILE *fp, L_KERNEL *kel)
kernelWriteStream()
L_KERNEL * makeDoGKernel(l_int32 halfh, l_int32 halfw, l_float32 stdev, l_float32 ratio)
makeDoGKernel()
PIX * kernelDisplayInPix(L_KERNEL *kel, l_int32 size, l_int32 gthick)
kernelDisplayInPix()
l_ok kernelSetElement(L_KERNEL *kel, l_int32 row, l_int32 col, l_float32 val)
kernelSetElement()
l_float32 ** create2dFloatArray(l_int32 sy, l_int32 sx)
create2dFloatArray()
l_ok kernelGetElement(L_KERNEL *kel, l_int32 row, l_int32 col, l_float32 *pval)
kernelGetElement()
L_KERNEL * kernelCopy(L_KERNEL *kels)
kernelCopy()
L_KERNEL * kernelReadStream(FILE *fp)
kernelReadStream()
L_KERNEL * kernelCreateFromPix(PIX *pix, l_int32 cy, l_int32 cx)
kernelCreateFromPix()
l_ok makeGaussianKernelSep(l_int32 halfh, l_int32 halfw, l_float32 stdev, l_float32 max, L_KERNEL **pkelx, L_KERNEL **pkely)
makeGaussianKernelSep()
L_KERNEL * kernelCreateFromString(l_int32 h, l_int32 w, l_int32 cy, l_int32 cx, const char *kdata)
kernelCreateFromString()
L_KERNEL * kernelCreateFromFile(const char *filename)
kernelCreateFromFile()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
NUMA * numaCreate(l_int32 n)
numaCreate()
void numaDestroy(NUMA **pna)
numaDestroy()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok numaJoin(NUMA *nad, NUMA *nas, l_int32 istart, l_int32 iend)
numaJoin()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
l_ok pixSetPixel(PIX *pix, l_int32 x, l_int32 y, l_uint32 val)
pixSetPixel()
l_ok pixGetPixel(PIX *pix, l_int32 x, l_int32 y, l_uint32 *pval)
pixGetPixel()
l_ok pixSetAll(PIX *pix)
pixSetAll()
l_ok pixPaintThroughMask(PIX *pixd, PIX *pixm, l_int32 x, l_int32 y, l_uint32 val)
pixPaintThroughMask()
l_ok pixSetMaskedGeneral(PIX *pixd, PIX *pixm, l_uint32 val, l_int32 x, l_int32 y)
pixSetMaskedGeneral()
l_ok pixRasterop(PIX *pixd, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, PIX *pixs, l_int32 sx, l_int32 sy)
pixRasterop()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
void lept_stderr(const char *fmt,...)
lept_stderr()
char * stringNew(const char *src)
stringNew()
char * strtokSafe(char *cstr, const char *seps, char **psaveptr)
strtokSafe()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
FILE * fopenReadStream(const char *filename)
fopenReadStream()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()