84 #include <config_auto.h>
89 #include "allheaders.h"
92 static const l_uint32 MaxArraySize = 100000;
119 return (
L_KERNEL *)ERROR_PTR(
"width must be > 0", __func__, NULL);
121 return (
L_KERNEL *)ERROR_PTR(
"height must be > 0", __func__, NULL);
124 size64 = (l_uint64)width * (l_uint64)height;
125 if (size64 >= (1LL << 29)) {
126 L_ERROR(
"requested width = %d, height = %d\n", __func__, width, height);
127 return (
L_KERNEL *)ERROR_PTR(
"size >= 2^29", __func__, NULL);
135 return (
L_KERNEL *)ERROR_PTR(
"data not allocated", __func__, NULL);
154 L_WARNING(
"ptr address is NULL!\n", __func__);
157 if ((kel = *pkel) == NULL)
160 for (i = 0; i < kel->
sy; i++)
161 LEPT_FREE(kel->
data[i]);
162 LEPT_FREE(kel->
data);
177 l_int32 i, j, sx, sy, cx, cy;
181 return (
L_KERNEL *)ERROR_PTR(
"kels not defined", __func__, NULL);
185 return (
L_KERNEL *)ERROR_PTR(
"keld not made", __func__, NULL);
188 for (i = 0; i < sy; i++)
189 for (j = 0; j < sx; j++)
190 keld->
data[i][j] = kels->
data[i][j];
215 return ERROR_INT(
"&val not defined", __func__, 1);
218 return ERROR_INT(
"kernel not defined", __func__, 1);
219 if (row < 0 || row >= kel->
sy)
220 return ERROR_INT(
"kernel row out of bounds", __func__, 1);
221 if (col < 0 || col >= kel->
sx)
222 return ERROR_INT(
"kernel col out of bounds", __func__, 1);
224 *pval = kel->
data[row][col];
245 return ERROR_INT(
"kel not defined", __func__, 1);
246 if (row < 0 || row >= kel->
sy)
247 return ERROR_INT(
"kernel row out of bounds", __func__, 1);
248 if (col < 0 || col >= kel->
sx)
249 return ERROR_INT(
"kernel col out of bounds", __func__, 1);
251 kel->
data[row][col] = val;
275 return ERROR_INT(
"kernel not defined", __func__, 1);
276 if (psy) *psy = kel->
sy;
277 if (psx) *psx = kel->
sx;
278 if (pcy) *pcy = kel->
cy;
279 if (pcx) *pcx = kel->
cx;
297 return ERROR_INT(
"kel not defined", __func__, 1);
315 l_int32 sx, sy, i, j;
318 return ERROR_INT(
"&sum not defined", __func__, 1);
321 return ERROR_INT(
"kernel not defined", __func__, 1);
324 for (i = 0; i < sy; i++) {
325 for (j = 0; j < sx; j++) {
326 *psum += kel->
data[i][j];
346 l_int32 sx, sy, i, j;
347 l_float32 val, minval, maxval;
350 return ERROR_INT(
"neither &min nor &max defined", __func__, 1);
351 if (pmin) *pmin = 0.0;
352 if (pmax) *pmax = 0.0;
354 return ERROR_INT(
"kernel not defined", __func__, 1);
358 maxval = -10000000.0;
359 for (i = 0; i < sy; i++) {
360 for (j = 0; j < sx; j++) {
361 val = kel->
data[i][j];
399 l_int32 i, j, sx, sy, cx, cy;
400 l_float32 sum, factor;
404 return (
L_KERNEL *)ERROR_PTR(
"kels not defined", __func__, NULL);
407 if (L_ABS(sum) < 0.00001) {
408 L_WARNING(
"null sum; not normalizing; returning a copy\n", __func__);
414 return (
L_KERNEL *)ERROR_PTR(
"keld not made", __func__, NULL);
418 factor = normsum / sum;
419 for (i = 0; i < sy; i++)
420 for (j = 0; j < sx; j++)
421 keld->
data[i][j] = factor * kels->
data[i][j];
442 l_int32 i, j, sx, sy, cx, cy;
446 return (
L_KERNEL *)ERROR_PTR(
"kels not defined", __func__, NULL);
450 return (
L_KERNEL *)ERROR_PTR(
"keld not made", __func__, NULL);
451 keld->
cy = sy - 1 - cy;
452 keld->
cx = sx - 1 - cx;
454 for (i = 0; i < sy; i++)
455 for (j = 0; j < sx; j++)
456 keld->
data[i][j] = kels->
data[sy - 1 - i][sx - 1 - j];
487 if (sx <= 0 || sx > MaxArraySize)
488 return (l_float32 **)ERROR_PTR(
"sx out of bounds", __func__, NULL);
489 if (sy <= 0 || sy > MaxArraySize)
490 return (l_float32 **)ERROR_PTR(
"sy out of bounds", __func__, NULL);
492 array = (l_float32 **)LEPT_CALLOC(sy,
sizeof(l_float32 *));
493 for (i = 0; i < sy; i++)
494 array[i] = (l_float32 *)LEPT_CALLOC(sx,
sizeof(l_float32));
515 return (
L_KERNEL *)ERROR_PTR(
"fname not defined", __func__, NULL);
518 return (
L_KERNEL *)ERROR_PTR(
"stream not opened", __func__, NULL);
521 return (
L_KERNEL *)ERROR_PTR(
"kel not returned", __func__, NULL);
538 l_int32 sy, sx, cy, cx, i, j, ret, version, ignore;
542 return (
L_KERNEL *)ERROR_PTR(
"stream not defined", __func__, NULL);
544 ret = fscanf(fp,
" Kernel Version %d\n", &version);
546 return (
L_KERNEL *)ERROR_PTR(
"not a kernel file", __func__, NULL);
547 if (version != KERNEL_VERSION_NUMBER)
548 return (
L_KERNEL *)ERROR_PTR(
"invalid kernel version", __func__, NULL);
550 if (fscanf(fp,
" sy = %d, sx = %d, cy = %d, cx = %d\n",
551 &sy, &sx, &cy, &cx) != 4)
552 return (
L_KERNEL *)ERROR_PTR(
"dimensions not read", __func__, NULL);
553 if (sx > MaxArraySize || sy > MaxArraySize) {
554 L_ERROR(
"sx = %d or sy = %d > %d\n", __func__, sx, sy, MaxArraySize);
558 return (
L_KERNEL *)ERROR_PTR(
"kel not made", __func__, NULL);
561 for (i = 0; i < sy; i++) {
562 for (j = 0; j < sx; j++)
563 ignore = fscanf(fp,
"%15f", &kel->
data[i][j]);
564 ignore = fscanf(fp,
"\n");
566 ignore = fscanf(fp,
"\n");
586 return ERROR_INT(
"fname not defined", __func__, 1);
588 return ERROR_INT(
"kel not defined", __func__, 1);
591 return ERROR_INT(
"stream not opened", __func__, 1);
610 l_int32 sx, sy, cx, cy, i, j;
613 return ERROR_INT(
"stream not defined", __func__, 1);
615 return ERROR_INT(
"kel not defined", __func__, 1);
618 fprintf(fp,
" Kernel Version %d\n", KERNEL_VERSION_NUMBER);
619 fprintf(fp,
" sy = %d, sx = %d, cy = %d, cx = %d\n", sy, sx, cy, cx);
620 for (i = 0; i < sy; i++) {
621 for (j = 0; j < sx; j++)
622 fprintf(fp,
"%15.4f", kel->
data[i][j]);
663 l_int32 n, i, j, index;
669 return (
L_KERNEL *)ERROR_PTR(
"height must be > 0", __func__, NULL);
671 return (
L_KERNEL *)ERROR_PTR(
"width must be > 0", __func__, NULL);
672 if (cy < 0 || cy >= h)
673 return (
L_KERNEL *)ERROR_PTR(
"cy invalid", __func__, NULL);
674 if (cx < 0 || cx >= w)
675 return (
L_KERNEL *)ERROR_PTR(
"cx invalid", __func__, NULL);
684 lept_stderr(
"w = %d, h = %d, num ints = %d\n", w, h, n);
685 return (
L_KERNEL *)ERROR_PTR(
"invalid integer data", __func__, NULL);
689 for (i = 0; i < h; i++) {
690 for (j = 0; j < w; j++) {
743 char *filestr, *line;
744 l_int32 nlines, i, j, first, index, w, h, cx, cy, n;
752 return (
L_KERNEL *)ERROR_PTR(
"filename not defined", __func__, NULL);
754 if ((filestr = (
char *)
l_binaryRead(filename, &size)) == NULL)
755 return (
L_KERNEL *)ERROR_PTR(
"file not found", __func__, NULL);
758 return (
L_KERNEL *)ERROR_PTR(
"file is empty", __func__, NULL);
766 for (i = 0, first = 0; i < nlines; i++) {
768 if (line[0] !=
'#') {
776 if (sscanf(line,
"%d %d", &h, &w) != 2) {
778 return (
L_KERNEL *)ERROR_PTR(
"error reading h,w", __func__, NULL);
780 if (h > MaxArraySize || w > MaxArraySize) {
781 L_ERROR(
"h = %d or w = %d > %d\n", __func__, h, w, MaxArraySize);
786 if (sscanf(line,
"%d %d", &cy, &cx) != 2) {
788 return (
L_KERNEL *)ERROR_PTR(
"error reading cy,cx", __func__, NULL);
795 for (i = first + 2; i < nlines; i++) {
797 if (line[0] ==
'\0' || line[0] ==
'\n' || line[0] ==
'#')
808 lept_stderr(
"w = %d, h = %d, num ints = %d\n", w, h, n);
809 return (
L_KERNEL *)ERROR_PTR(
"invalid integer data", __func__, NULL);
815 for (i = 0; i < h; i++) {
816 for (j = 0; j < w; j++) {
848 l_int32 i, j, w, h, d;
853 return (
L_KERNEL *)ERROR_PTR(
"pix not defined", __func__, NULL);
856 return (
L_KERNEL *)ERROR_PTR(
"pix not 8 bpp", __func__, NULL);
857 if (cy < 0 || cx < 0 || cy >= h || cx >= w)
858 return (
L_KERNEL *)ERROR_PTR(
"(cy, cx) invalid", __func__, NULL);
862 for (i = 0; i < h; i++) {
863 for (j = 0; j < w; j++) {
907 l_int32 i, j, w, h, sx, sy, cx, cy, width, x0, y0;
909 l_float32 minval, maxval, max, val, norm;
910 PIX *pixd, *pixt0, *pixt1;
913 return (
PIX *)ERROR_PTR(
"kernel not defined", __func__, NULL);
918 max = L_MAX(maxval, -minval);
920 return (
PIX *)ERROR_PTR(
"kernel elements all 0.0", __func__, NULL);
921 norm = 255. / (l_float32)max;
924 if (size == 1 && gthick == 0) {
926 for (i = 0; i < sy; i++) {
927 for (j = 0; j < sx; j++) {
929 normval = (l_int32)(norm * L_ABS(val));
938 L_WARNING(
"size < 17; setting to 17\n", __func__);
944 L_WARNING(
"grid thickness < 2; setting to 2\n", __func__);
948 w = size * sx + gthick * (sx + 1);
949 h = size * sy + gthick * (sy + 1);
953 for (i = 0; i <= sy; i++)
955 w - 1, gthick / 2 + i * (size + gthick),
957 for (j = 0; j <= sx; j++)
959 gthick / 2 + j * (size + gthick), h - 1,
970 size / 2, (l_int32)(0.88 * size),
973 (l_int32)(0.85 * size), size / 2,
975 pixRasterop(pixt1, size / 2 - width, size / 2 - width,
980 for (i = 0; i < sy; i++) {
982 for (j = 0; j < sx; j++) {
984 normval = (l_int32)(norm * L_ABS(val));
986 if (i == cy && j == cx)
1018 char *newstr, *head;
1024 return (
NUMA *)ERROR_PTR(
"str not defined", __func__, NULL);
1032 while ((head =
strtokSafe(NULL, seps, &tail)) != NULL) {
1075 return (
L_KERNEL *)ERROR_PTR(
"kel not made", __func__, NULL);
1077 normval = 1.0 / (l_float32)(height * width);
1078 for (i = 0; i < height; i++) {
1079 for (j = 0; j < width; j++) {
1114 l_int32 sx, sy, i, j;
1121 return (
L_KERNEL *)ERROR_PTR(
"kel not made", __func__, NULL);
1123 for (i = 0; i < sy; i++) {
1124 for (j = 0; j < sx; j++) {
1125 val = expf(-(l_float32)((i - halfh) * (i - halfh) +
1126 (j - halfw) * (j - halfw)) /
1127 (2. * stdev * stdev));
1169 if (!pkelx || !pkely)
1170 return ERROR_INT(
"&kelx and &kely not defined", __func__, 1);
1211 l_int32 sx, sy, i, j;
1212 l_float32 pi, squaredist, highnorm, lownorm, val;
1218 return (
L_KERNEL *)ERROR_PTR(
"kel not made", __func__, NULL);
1222 for (i = 0; i < sy; i++) {
1223 for (j = 0; j < sx; j++) {
1224 squaredist = (l_float32)((i - halfh) * (i - halfh) +
1225 (j - halfw) * (j - halfw));
1226 highnorm = 1. / (2 * stdev * stdev);
1227 lownorm = highnorm / (ratio * ratio);
1228 val = (highnorm / pi) * expf(-(highnorm * squaredist))
1229 - (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()