326 const float *
const in2 = (
float *)
i;
327 float *
out = (
float *) o;
331 const int h_width = (
width + 1) / 2;
332 const int h_height = (
height + 1) / 2;
340 const gboolean avoidshift =
d->avoidshift;
341 const int iterations =
d->iterations;
344 gboolean processpasstwo =
TRUE;
347 float *redfactor = NULL;
348 float *bluefactor = NULL;
349 float *oldraw = NULL;
351 float *RawDataTmp = NULL;
352 char *buffer1 = NULL;
353 char *thread_buffers = NULL;
354 size_t padded_buffersize = 0;
355 double fitparams[2][2][16] = { 0 };
356 float blockvar[2][2] = { { 0, 0 }, { 0, 0 } };
362 const float *
const in =
out;
364 const float caautostrength = 4.0f;
368 const int tsh = ts / 2;
370 const int v1 = ts, v2 = 2 * ts, v3 = 3 * ts, v4 = 4 * ts;
373 for(
int i = 0;
i < 2;
i++)
374 for(
int j = 0; j < 2; j++)
375 if(
FC(
i, j, filters) == 3)
382 const size_t buffsize = (size_t)h_width * h_height;
389 memset(redfactor, 0,
sizeof(
float) * buffsize);
396 memset(bluefactor, 0,
sizeof(
float) * buffsize);
403 memset(oldraw, 0,
sizeof(
float) * buffsize * 2);
408 for(
int col = (
FC(
row, 0, filters) & 1); col <
width; col += 2)
410 oldraw[
row * h_width + col / 2] = in[
row *
width + col];
432 const int border = 8;
433 const int border2 = 16;
435 const int vz1 = (
height + border2) % (ts - border2) == 0 ? 1 : 0;
436 const int hz1 = (
width + border2) % (ts - border2) == 0 ? 1 : 0;
437 const int vblsz = ceil((
float)(
height + border2) / (ts - border2) + 2 + vz1);
438 const int hblsz = ceil((
float)(
width + border2) / (ts - border2) + 2 + hz1);
440 buffer1 = (
char *)calloc((
size_t)vblsz * hblsz * (2 * 2 + 1),
sizeof(
float));
447 const size_t buffersize =
sizeof(float) * 3 * ts * ts + 6 *
sizeof(
float) * ts * tsh + 8 * 64 + 63;
456 float *blockwt = (
float *)buffer1;
457 float(*blockshifts)[2][2] = (float(*)[2][2])(buffer1 + (
sizeof(
float) * vblsz * hblsz));
459 float blockave[2][2] = { { 0, 0 }, { 0, 0 } };
460 float blocksqave[2][2] = { { 0, 0 }, { 0, 0 } };
461 float blockdenom[2][2] = { { 0, 0 }, { 0, 0 } };
463 int polyord = 4, numpar = 16;
465 const float eps = 1e-5f, eps2 = 1e-10f;
467 for (
size_t it = 0; it < iterations && processpasstwo; it++)
477 int shifthfloor[3], shiftvfloor[3], shifthceil[3], shiftvceil[3];
480 float coeff[2][3][2];
485 float shifthfrac[3], shiftvfrac[3];
487 float blockavethr[2][2] = { { 0, 0 }, { 0, 0 } }, blocksqavethr[2][2] = { { 0, 0 }, { 0, 0 } },
488 blockdenomthr[2][2] = { { 0, 0 }, { 0, 0 } };
492 char *data = (
char *)(((uintptr_t)buffer + (uintptr_t)63) / 64 * 64);
499 rgb[0] = (float(*))data;
500 rgb[1] = (float(*))(data + 1 *
sizeof(
float) * ts * ts + 1 * 64);
501 rgb[2] = (float(*))(data + 2 *
sizeof(
float) * ts * ts + 2 * 64);
504 float *rbhpfh = (float(*))(data + 3 *
sizeof(
float) * ts * ts + 3 * 64);
506 float *rbhpfv = (float(*))(data + 3 *
sizeof(
float) * ts * ts +
sizeof(float) * ts * tsh + 4 * 64);
508 float *rblpfh = (float(*))(data + 4 *
sizeof(
float) * ts * ts + 5 * 64);
510 float *rblpfv = (float(*))(data + 4 *
sizeof(
float) * ts * ts +
sizeof(float) * ts * tsh + 6 * 64);
512 float *grblpfh = (float(*))(data + 5 *
sizeof(
float) * ts * ts + 7 * 64);
514 float *grblpfv = (float(*))(data + 5 *
sizeof(
float) * ts * ts +
sizeof(float) * ts * tsh + 8 * 64);
516 float *grbdiff = rbhpfh;
518 float *gshift = rbhpfv;
524 for(
int left = -border; left <
width; left += ts - border2)
527 const int vblock = ((
top + border) / (ts - border2)) + 1;
528 const int hblock = ((left + border) / (ts - border2)) + 1;
530 const int right =
MIN(left + ts,
width + border);
531 const int rr1 = bottom -
top;
532 const int cc1 = right - left;
533 const int rrmin =
top < 0 ? border : 0;
535 const int ccmin = left < 0 ? border : 0;
536 const int ccmax = right >
width ?
width - left : cc1;
542 for(
int rr = rrmin; rr < rrmax; rr++)
543 for(
int row = rr +
top, cc = ccmin; cc < ccmax; cc++)
546 int c =
FC(rr, cc, filters);
548 int indx1 = rr * ts + cc;
549 rgb[c][indx1] = (in[indx]);
556 for(
int rr = 0; rr < border; rr++)
557 for(
int cc = ccmin; cc < ccmax; cc++)
559 int c =
FC(rr, cc, filters);
560 rgb[c][rr * ts + cc] =
rgb[c][(border2 - rr) * ts + cc];
566 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
567 for(
int cc = ccmin; cc < ccmax; cc++)
569 int c =
FC(rr, cc, filters);
570 rgb[c][(rrmax + rr) * ts + cc] = (in[(
height - rr - 2) *
width + left + cc]);
576 for(
int rr = rrmin; rr < rrmax; rr++)
577 for(
int cc = 0; cc < border; cc++)
579 int c =
FC(rr, cc, filters);
580 rgb[c][rr * ts + cc] =
rgb[c][rr * ts + border2 - cc];
586 for(
int rr = rrmin; rr < rrmax; rr++)
587 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
589 int c =
FC(rr, cc, filters);
595 if(rrmin > 0 && ccmin > 0)
597 for(
int rr = 0; rr < border; rr++)
598 for(
int cc = 0; cc < border; cc++)
600 int c =
FC(rr, cc, filters);
601 rgb[c][(rr)*ts + cc] = (in[(border2 - rr) *
width + border2 - cc]);
605 if(rrmax < rr1 && ccmax < cc1)
607 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
608 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
610 int c =
FC(rr, cc, filters);
611 rgb[c][(rrmax + rr) * ts + ccmax + cc] = (in[(
height - rr - 2) *
width + (
width - cc - 2)]);
615 if(rrmin > 0 && ccmax < cc1)
617 for(
int rr = 0; rr < border; rr++)
618 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
620 int c =
FC(rr, cc, filters);
621 rgb[c][(rr)*ts + ccmax + cc] = (in[(border2 - rr) *
width + (
width - cc - 2)]);
625 if(rrmax < rr1 && ccmin > 0)
627 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
628 for(
int cc = 0; cc < border; cc++)
630 int c =
FC(rr, cc, filters);
631 rgb[c][(rrmax + rr) * ts + cc] = (in[(
height - rr - 2) *
width + (border2 - cc)]);
639 for(
int rr = 3; rr < rr1 - 3; rr++)
642 for(
int cc = 3 + (
FC(rr, 3, filters) & 1), indx = rr * ts + cc, c =
FC(rr, cc, filters); cc < cc1 - 3; cc += 2, indx += 2)
645 float wtu = 1.f /
SQR(
eps + fabsf(
rgb[1][indx + v1] -
rgb[1][indx - v1])
646 + fabsf(
rgb[c][indx] -
rgb[c][indx - v2])
647 + fabsf(
rgb[1][indx - v1] -
rgb[1][indx - v3]));
648 float wtd = 1.f /
SQR(
eps + fabsf(
rgb[1][indx - v1] -
rgb[1][indx + v1])
649 + fabsf(
rgb[c][indx] -
rgb[c][indx + v2])
650 + fabsf(
rgb[1][indx + v1] -
rgb[1][indx + v3]));
651 float wtl = 1.f /
SQR(
eps + fabsf(
rgb[1][indx + 1] -
rgb[1][indx - 1])
652 + fabsf(
rgb[c][indx] -
rgb[c][indx - 2])
653 + fabsf(
rgb[1][indx - 1] -
rgb[1][indx - 3]));
654 float wtr = 1.f /
SQR(
eps + fabsf(
rgb[1][indx - 1] -
rgb[1][indx + 1])
655 + fabsf(
rgb[c][indx] -
rgb[c][indx + 2])
656 + fabsf(
rgb[1][indx + 1] -
rgb[1][indx + 3]));
660 rgb[1][indx] = (wtu *
rgb[1][indx - v1] + wtd *
rgb[1][indx + v1] + wtl *
rgb[1][indx - 1]
661 + wtr *
rgb[1][indx + 1])
662 / (wtu + wtd + wtl + wtr);
667 for(
int col =
MAX(left + 3, 0), indx = rr * ts + 3 - (left < 0 ? (left + 3) : 0);
668 col <
MIN(cc1 + left - 3,
width); col++, indx++)
675 for(
int rr = 4; rr < rr1 - 4; rr++)
677 for(
int cc = 4 + (
FC(rr, 2, filters) & 1), indx = rr * ts + cc, c =
FC(rr, cc, filters); cc < cc1 - 4; cc += 2, indx += 2)
679 rbhpfv[indx >> 1] = fabsf(
680 fabsf((
rgb[1][indx] -
rgb[c][indx]) - (
rgb[1][indx + v4] -
rgb[c][indx + v4]))
681 + fabsf((
rgb[1][indx - v4] -
rgb[c][indx - v4]) - (
rgb[1][indx] -
rgb[c][indx]))
682 - fabsf((
rgb[1][indx - v4] -
rgb[c][indx - v4]) - (
rgb[1][indx + v4] -
rgb[c][indx + v4])));
683 rbhpfh[indx >> 1] = fabsf(
684 fabsf((
rgb[1][indx] -
rgb[c][indx]) - (
rgb[1][indx + 4] -
rgb[c][indx + 4]))
685 + fabsf((
rgb[1][indx - 4] -
rgb[c][indx - 4]) - (
rgb[1][indx] -
rgb[c][indx]))
686 - fabsf((
rgb[1][indx - 4] -
rgb[c][indx - 4]) - (
rgb[1][indx + 4] -
rgb[c][indx + 4])));
689 float glpfv = 0.25f * (2.f *
rgb[1][indx] +
rgb[1][indx + v2] +
rgb[1][indx - v2]);
690 float glpfh = 0.25f * (2.f *
rgb[1][indx] +
rgb[1][indx + 2] +
rgb[1][indx - 2]);
692 =
eps + fabsf(glpfv - 0.25f * (2.f *
rgb[c][indx] +
rgb[c][indx + v2] +
rgb[c][indx - v2]));
694 =
eps + fabsf(glpfh - 0.25f * (2.f *
rgb[c][indx] +
rgb[c][indx + 2] +
rgb[c][indx - 2]));
696 = glpfv + 0.25f * (2.f *
rgb[c][indx] +
rgb[c][indx + v2] +
rgb[c][indx - v2]);
697 grblpfh[indx >> 1] = glpfh + 0.25f * (2.f *
rgb[c][indx] +
rgb[c][indx + 2] +
rgb[c][indx - 2]);
701 for(
int dir = 0; dir < 2; dir++)
703 for(
int k = 0;
k < 3;
k++)
705 for(
int c = 0; c < 2; c++)
714 for(
int rr = 8; rr < rr1 - 8; rr++)
716 for(
int cc = 8 + (
FC(rr, 2, filters) & 1), indx = rr * ts + cc, c =
FC(rr, cc, filters); cc < cc1 - 8; cc += 2, indx += 2)
724 float gdiff = 0.3125f * (
rgb[1][indx + ts] -
rgb[1][indx - ts])
725 + 0.09375f * (
rgb[1][indx + ts + 1] -
rgb[1][indx - ts + 1]
726 +
rgb[1][indx + ts - 1] -
rgb[1][indx - ts - 1]);
727 float deltgrb = (
rgb[c][indx] -
rgb[1][indx]);
729 float gradwt = fabsf(0.25f * rbhpfv[indx >> 1]
730 + 0.125f * (rbhpfv[(indx >> 1) + 1] + rbhpfv[(indx >> 1) - 1]))
731 * (grblpfv[(indx >> 1) - v1] + grblpfv[(indx >> 1) + v1])
732 / (
eps + 0.1f * (grblpfv[(indx >> 1) - v1] + grblpfv[(indx >> 1) + v1])
733 + rblpfv[(indx >> 1) - v1] + rblpfv[(indx >> 1) + v1]);
735 coeff[0][0][c >> 1] += gradwt * deltgrb * deltgrb;
736 coeff[0][1][c >> 1] += gradwt * gdiff * deltgrb;
737 coeff[0][2][c >> 1] += gradwt * gdiff * gdiff;
740 gdiff = 0.3125f * (
rgb[1][indx + 1] -
rgb[1][indx - 1])
741 + 0.09375f * (
rgb[1][indx + 1 + ts] -
rgb[1][indx - 1 + ts] +
rgb[1][indx + 1 - ts]
742 -
rgb[1][indx - 1 - ts]);
744 gradwt = fabsf(0.25f * rbhpfh[indx >> 1]
745 + 0.125f * (rbhpfh[(indx >> 1) + v1] + rbhpfh[(indx >> 1) - v1]))
746 * (grblpfh[(indx >> 1) - 1] + grblpfh[(indx >> 1) + 1])
747 / (
eps + 0.1f * (grblpfh[(indx >> 1) - 1] + grblpfh[(indx >> 1) + 1])
748 + rblpfh[(indx >> 1) - 1] + rblpfh[(indx >> 1) + 1]);
750 coeff[1][0][c >> 1] += gradwt * deltgrb * deltgrb;
751 coeff[1][1][c >> 1] += gradwt * gdiff * deltgrb;
752 coeff[1][2][c >> 1] += gradwt * gdiff * gdiff;
762 for(
int c = 0; c < 2; c++)
764 for(
int dir = 0; dir < 2; dir++)
770 if(
coeff[dir][2][c] > eps2)
772 CAshift[dir][c] =
coeff[dir][1][c] /
coeff[dir][2][c];
773 blockwt[vblock * hblsz + hblock] =
coeff[dir][2][c] / (
eps +
coeff[dir][0][c]);
777 CAshift[dir][c] = 17.0;
778 blockwt[vblock * hblsz + hblock] = 0;
785 if(fabsf(CAshift[dir][c]) < 2.0f)
787 blockavethr[dir][c] += CAshift[dir][c];
788 blocksqavethr[dir][c] +=
SQR(CAshift[dir][c]);
789 blockdenomthr[dir][c] += 1;
792 blockshifts[vblock * hblsz + hblock][c][dir] = CAshift[dir][c];
801#pragma omp critical(cadetectpass2)
804 for(
int dir = 0; dir < 2; dir++)
805 for(
int c = 0; c < 2; c++)
807 blockdenom[dir][c] += blockdenomthr[dir][c];
808 blocksqave[dir][c] += blocksqavethr[dir][c];
809 blockave[dir][c] += blockavethr[dir][c];
820 for(
int dir = 0; dir < 2; dir++)
821 for(
int c = 0; c < 2; c++)
823 if(blockdenom[dir][c])
826 = blocksqave[dir][c] / blockdenom[dir][c] -
SQR(blockave[dir][c] / blockdenom[dir][c]);
830 processpasstwo =
FALSE;
841 for(
int vblock = 1; vblock < vblsz - 1; vblock++)
843 for(
int c = 0; c < 2; c++)
845 for(
int i = 0;
i < 2;
i++)
847 blockshifts[vblock * hblsz][c][
i] = blockshifts[(vblock)*hblsz + 2][c][
i];
848 blockshifts[vblock * hblsz + hblsz - 1][c][
i] = blockshifts[(vblock)*hblsz + hblsz - 3][c][
i];
853 for(
int hblock = 0; hblock < hblsz; hblock++)
855 for(
int c = 0; c < 2; c++)
857 for(
int i = 0;
i < 2;
i++)
859 blockshifts[hblock][c][
i] = blockshifts[2 * hblsz + hblock][c][
i];
860 blockshifts[(vblsz - 1) * hblsz + hblock][c][
i]
861 = blockshifts[(vblsz - 3) * hblsz + hblock][c][
i];
869 double polymat[2][2][256], shiftmat[2][2][16];
871 for(
int i = 0;
i < 256;
i++)
873 polymat[0][0][
i] = polymat[0][1][
i] = polymat[1][0][
i] = polymat[1][1][
i] = 0;
876 for(
int i = 0;
i < 16;
i++)
878 shiftmat[0][0][
i] = shiftmat[0][1][
i] = shiftmat[1][0][
i] = shiftmat[1][1][
i] = 0;
881 int numblox[2] = { 0, 0 };
883 for(
int vblock = 1; vblock < vblsz - 1; vblock++)
884 for(
int hblock = 1; hblock < hblsz - 1; hblock++)
887 for(
int c = 0; c < 2; c++)
890 for(
int dir = 0; dir < 2; dir++)
894 p[0] = blockshifts[(vblock - 1) * hblsz + hblock - 1][c][dir];
895 p[1] = blockshifts[(vblock - 1) * hblsz + hblock][c][dir];
896 p[2] = blockshifts[(vblock - 1) * hblsz + hblock + 1][c][dir];
897 p[3] = blockshifts[(vblock)*hblsz + hblock - 1][c][dir];
898 p[4] = blockshifts[(vblock)*hblsz + hblock][c][dir];
899 p[5] = blockshifts[(vblock)*hblsz + hblock + 1][c][dir];
900 p[6] = blockshifts[(vblock + 1) * hblsz + hblock - 1][c][dir];
901 p[7] = blockshifts[(vblock + 1) * hblsz + hblock][c][dir];
902 p[8] = blockshifts[(vblock + 1) * hblsz + hblock + 1][c][dir];
927 if(
SQR(bstemp[0]) > caautostrength * blockvar[0][c]
928 ||
SQR(bstemp[1]) > caautostrength * blockvar[1][c])
935 for(
int dir = 0; dir < 2; dir++)
937 double powVblockInit = 1.0;
938 for(
int i = 0;
i < polyord;
i++)
940 double powHblockInit = 1.0;
941 for(
int j = 0; j < polyord; j++)
943 double powVblock = powVblockInit;
944 for(
int m = 0;
m < polyord;
m++)
946 double powHblock = powHblockInit;
947 for(
int n = 0;
n < polyord;
n++)
949 polymat[c][dir][numpar * (polyord *
i + j) + (polyord *
m +
n)]
950 += powVblock * powHblock * blockwt[vblock * hblsz + hblock];
955 shiftmat[c][dir][(polyord *
i + j)]
956 += powVblockInit * powHblockInit * bstemp[dir] * blockwt[vblock * hblsz + hblock];
957 powHblockInit *= hblock;
959 powVblockInit *= vblock;
965 numblox[1] =
MIN(numblox[0], numblox[1]);
975 fprintf(stderr,
", numblox = %d \n", numblox[1]);
976 processpasstwo =
FALSE;
983 for(
int c = 0; c < 2; c++)
984 for(
int dir = 0; dir < 2; dir++)
986 if(!
LinEqSolve(numpar, polymat[c][dir], shiftmat[c][dir], fitparams[c][dir]))
988 fprintf(stderr,
", correction pass failed -- can't solve linear equations for colour %d direction %d", c, dir);
989 processpasstwo =
FALSE;
1007 for(
int left = -border; left <
width; left += ts - border2)
1009 memset(buffer, 0, buffersize);
1010 float lblockshifts[2][2];
1011 const int vblock = ((
top + border) / (ts - border2)) + 1;
1012 const int hblock = ((left + border) / (ts - border2)) + 1;
1014 const int right =
MIN(left + ts,
width + border);
1015 const int rr1 = bottom -
top;
1016 const int cc1 = right - left;
1018 const int rrmin =
top < 0 ? border : 0;
1020 const int ccmin = left < 0 ? border : 0;
1021 const int ccmax = right >
width ?
width - left : cc1;
1027 for(
int rr = rrmin; rr < rrmax; rr++)
1028 for(
int row = rr +
top, cc = ccmin; cc < ccmax; cc++)
1030 int col = cc + left;
1031 int c =
FC(rr, cc, filters);
1033 int indx1 = rr * ts + cc;
1034 rgb[c][indx1] = (in[indx]);
1038 rgb[1][indx1] = Gtmp[indx];
1046 for(
int rr = 0; rr < border; rr++)
1047 for(
int cc = ccmin; cc < ccmax; cc++)
1049 int c =
FC(rr, cc, filters);
1050 rgb[c][rr * ts + cc] =
rgb[c][(border2 - rr) * ts + cc];
1051 rgb[1][rr * ts + cc] =
rgb[1][(border2 - rr) * ts + cc];
1057 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
1058 for(
int cc = ccmin; cc < ccmax; cc++)
1060 int c =
FC(rr, cc, filters);
1061 rgb[c][(rrmax + rr) * ts + cc] = (in[(
height - rr - 2) *
width + left + cc]);
1062 rgb[1][(rrmax + rr) * ts + cc] = Gtmp[(
height - rr - 2) *
width + left + cc];
1068 for(
int rr = rrmin; rr < rrmax; rr++)
1069 for(
int cc = 0; cc < border; cc++)
1071 int c =
FC(rr, cc, filters);
1072 rgb[c][rr * ts + cc] =
rgb[c][rr * ts + border2 - cc];
1073 rgb[1][rr * ts + cc] =
rgb[1][rr * ts + border2 - cc];
1079 for(
int rr = rrmin; rr < rrmax; rr++)
1080 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
1082 int c =
FC(rr, cc, filters);
1083 rgb[c][rr * ts + ccmax + cc] = (in[(
top + rr) *
width + (
width - cc - 2)]);
1084 rgb[1][rr * ts + ccmax + cc] = Gtmp[(
top + rr) *
width + (
width - cc - 2)];
1089 if(rrmin > 0 && ccmin > 0)
1091 for(
int rr = 0; rr < border; rr++)
1092 for(
int cc = 0; cc < border; cc++)
1094 int c =
FC(rr, cc, filters);
1095 rgb[c][(rr)*ts + cc] = (in[(border2 - rr) *
width + border2 - cc]);
1096 rgb[1][(rr)*ts + cc] = Gtmp[(border2 - rr) *
width + border2 - cc];
1100 if(rrmax < rr1 && ccmax < cc1)
1102 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
1103 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
1105 int c =
FC(rr, cc, filters);
1106 rgb[c][(rrmax + rr) * ts + ccmax + cc] = (in[(
height - rr - 2) *
width + (
width - cc - 2)]);
1107 rgb[1][(rrmax + rr) * ts + ccmax + cc] = Gtmp[(
height - rr - 2) *
width + (
width - cc - 2)];
1111 if(rrmin > 0 && ccmax < cc1)
1113 for(
int rr = 0; rr < border; rr++)
1114 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
1116 int c =
FC(rr, cc, filters);
1117 rgb[c][(rr)*ts + ccmax + cc] = (in[(border2 - rr) *
width + (
width - cc - 2)]);
1118 rgb[1][(rr)*ts + ccmax + cc] = Gtmp[(border2 - rr) *
width + (
width - cc - 2)];
1122 if(rrmax < rr1 && ccmin > 0)
1124 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
1125 for(
int cc = 0; cc < border; cc++)
1127 int c =
FC(rr, cc, filters);
1128 rgb[c][(rrmax + rr) * ts + cc] = (in[(
height - rr - 2) *
width + (border2 - cc)]);
1129 rgb[1][(rrmax + rr) * ts + cc] = Gtmp[(
height - rr - 2) *
width + (border2 - cc)];
1136 lblockshifts[0][0] = lblockshifts[0][1] = 0;
1137 lblockshifts[1][0] = lblockshifts[1][1] = 0;
1138 double powVblock = 1.0;
1139 for(
int i = 0;
i < polyord;
i++)
1141 double powHblock = powVblock;
1142 for(
int j = 0; j < polyord; j++)
1145 lblockshifts[0][0] += powHblock * fitparams[0][0][polyord *
i + j];
1146 lblockshifts[0][1] += powHblock * fitparams[0][1][polyord *
i + j];
1147 lblockshifts[1][0] += powHblock * fitparams[1][0][polyord *
i + j];
1148 lblockshifts[1][1] += powHblock * fitparams[1][1][polyord *
i + j];
1149 powHblock *= hblock;
1151 powVblock *= vblock;
1153 const float bslim = 3.99;
1154 lblockshifts[0][0] =
LIM(lblockshifts[0][0], -bslim, bslim);
1155 lblockshifts[0][1] =
LIM(lblockshifts[0][1], -bslim, bslim);
1156 lblockshifts[1][0] =
LIM(lblockshifts[1][0], -bslim, bslim);
1157 lblockshifts[1][1] =
LIM(lblockshifts[1][1], -bslim, bslim);
1161 for(
int c = 0; c < 3; c += 2)
1165 shiftvfloor[c] = floor((
float)lblockshifts[c >> 1][0]);
1166 shiftvceil[c] = ceil((
float)lblockshifts[c >> 1][0]);
1167 if (lblockshifts[c>>1][0] < 0.f) {
1168 float tmp = shiftvfloor[c];
1169 shiftvfloor[c] = shiftvceil[c];
1170 shiftvceil[c] = tmp;
1172 shiftvfrac[c] = fabsf(lblockshifts[c>>1][0] - shiftvfloor[c]);
1174 shifthfloor[c] = floor((
float)lblockshifts[c >> 1][1]);
1175 shifthceil[c] = ceil((
float)lblockshifts[c >> 1][1]);
1176 if (lblockshifts[c>>1][1] < 0.f) {
1177 float tmp = shifthfloor[c];
1178 shifthfloor[c] = shifthceil[c];
1179 shifthceil[c] = tmp;
1181 shifthfrac[c] = fabsf(lblockshifts[c>>1][1] - shifthfloor[c]);
1184 GRBdir[0][c] = lblockshifts[c >> 1][0] > 0 ? 2 : -2;
1185 GRBdir[1][c] = lblockshifts[c >> 1][1] > 0 ? 2 : -2;
1189 for(
int rr = 4; rr < rr1 - 4; rr++)
1191 for(
int cc = 4 + (
FC(rr, 2, filters) & 1), c =
FC(rr, cc, filters); cc < cc1 - 4; cc += 2)
1194 float Ginthfloor =
intp(shifthfrac[c],
rgb[1][(rr + shiftvfloor[c]) * ts + cc + shifthceil[c]],
1195 rgb[1][(rr + shiftvfloor[c]) * ts + cc + shifthfloor[c]]);
1196 float Ginthceil =
intp(shifthfrac[c],
rgb[1][(rr + shiftvceil[c]) * ts + cc + shifthceil[c]],
1197 rgb[1][(rr + shiftvceil[c]) * ts + cc + shifthfloor[c]]);
1199 float Gint =
intp(shiftvfrac[c], Ginthceil, Ginthfloor);
1204 grbdiff[((rr)*ts + cc) >> 1] = Gint -
rgb[c][(rr)*ts + cc];
1205 gshift[((rr)*ts + cc) >> 1] = Gint;
1209 shifthfrac[0] /= 2.f;
1210 shifthfrac[2] /= 2.f;
1211 shiftvfrac[0] /= 2.f;
1212 shiftvfrac[2] /= 2.f;
1216 for(
int rr = 8; rr < rr1 - 8; rr++)
1217 for(
int cc = 8 + (
FC(rr, 2, filters) & 1), c =
FC(rr, cc, filters), indx = rr * ts + cc;
1218 cc < cc1 - 8; cc += 2, indx += 2)
1221 float grbdiffold =
rgb[1][indx] -
rgb[c][indx];
1224 float grbdiffinthfloor
1225 =
intp(shifthfrac[c], grbdiff[(indx - GRBdir[1][c]) >> 1], grbdiff[indx >> 1]);
1226 float grbdiffinthceil
1227 =
intp(shifthfrac[c], grbdiff[((rr - GRBdir[0][c]) * ts + cc - GRBdir[1][c]) >> 1],
1228 grbdiff[((rr - GRBdir[0][c]) * ts + cc) >> 1]);
1230 float grbdiffint =
intp(shiftvfrac[c], grbdiffinthceil, grbdiffinthfloor);
1234 float RBint =
rgb[1][indx] - grbdiffint;
1236 if(fabsf(RBint -
rgb[c][indx]) < 0.25f * (RBint +
rgb[c][indx]))
1238 if(fabsf(grbdiffold) > fabsf(grbdiffint))
1240 rgb[c][indx] = RBint;
1247 float p0 = 1.0f / (
eps + fabsf(
rgb[1][indx] - gshift[indx >> 1]));
1248 float p1 = 1.0f / (
eps + fabsf(
rgb[1][indx] - gshift[(indx - GRBdir[1][c]) >> 1]));
1249 float p2 = 1.0f / (
eps + fabsf(
rgb[1][indx] - gshift[((rr - GRBdir[0][c]) * ts + cc) >> 1]));
1251 = 1.0f / (
eps + fabsf(
rgb[1][indx]
1252 - gshift[((rr - GRBdir[0][c]) * ts + cc - GRBdir[1][c]) >> 1]));
1254 grbdiffint = (p0 * grbdiff[indx >> 1] + p1 * grbdiff[(indx - GRBdir[1][c]) >> 1]
1255 + p2 * grbdiff[((rr - GRBdir[0][c]) * ts + cc) >> 1]
1256 + p3 * grbdiff[((rr - GRBdir[0][c]) * ts + cc - GRBdir[1][c]) >> 1])
1257 / (p0 + p1 + p2 + p3);
1261 if(fabsf(grbdiffold) > fabsf(grbdiffint))
1263 rgb[c][indx] =
rgb[1][indx] - grbdiffint;
1268 if(grbdiffold * grbdiffint < 0)
1270 rgb[c][indx] =
rgb[1][indx] - 0.5f * (grbdiffold + grbdiffint);
1275 for(
int rr = border; rr < rr1 - border; rr++)
1277 int c =
FC(rr +
top, left + border + (
FC(rr +
top, 2, filters) & 1), filters);
1279 for(
int row = rr +
top, cc = border + (
FC(rr, 2, filters) & 1),
1280 indx = (
row *
width + cc + left) >> 1;
1281 cc < cc1 - border; cc += 2, indx++)
1284 RawDataTmp[indx] =
rgb[c][(rr)*ts + cc];
1297 for(
int col = 0 + (
FC(
row, 0, filters) & 1), indx = (
row *
width + col) >> 1; col <
width;
1308 if(avoidshift && processpasstwo)
1316 const int firstCol =
FC(
row, 0, filters) & 1;
1317 const int color =
FC(
row, firstCol, filters);
1318 float *nongreen = (
color == 0) ? redfactor : bluefactor;
1319 for(
int col = firstCol; col <
width; col += 2)
1321 nongreen[(
row / 2) * h_width + col / 2] = (in[
row *
width + col] <= 1.0f || oldraw[
row * h_width + col / 2] <= 1.0f)
1322 ? 1.0f :
LIM(oldraw[
row * h_width + col / 2] / in[
row *
width + col], 0.5f, 2.0f);
1329 for(
int col = 0; col < h_width; col++)
1331 redfactor[(h_height-1) * h_width + col] = redfactor[(h_height-2) * h_width + col];
1332 bluefactor[(h_height-1) * h_width + col] = bluefactor[(h_height-2) * h_width + col];
1339 const int ngRow = 1 - (
FC(0, 0, filters) & 1);
1340 const int ngCol =
FC(ngRow, 0, filters) & 1;
1341 const int color =
FC(ngRow, ngCol, filters);
1342 float *nongreen = (
color == 0) ? redfactor : bluefactor;
1345 nongreen[
row * h_width + h_width - 1] = nongreen[
row * h_width + h_width - 2];
1350 float valmax[] = { 10.0f };
1351 float valmin[] = { 0.1f };
1371 const int firstCol =
FC(
row, 0, filters) & 1;
1372 const int color =
FC(
row, firstCol, filters);
1373 float *nongreen = (
color == 0) ? redfactor : bluefactor;
1374 for(
int col = firstCol; col <
width - 2; col += 2)
1376 const float correction = nongreen[
row / 2 * h_width + col / 2];