321 const float *
const in2 = (
float *)
i;
322 float *
out = (
float *) o;
326 const int h_width = (
width + 1) / 2;
327 const int h_height = (
height + 1) / 2;
335 const gboolean avoidshift =
d->avoidshift;
336 const int iterations =
d->iterations;
339 gboolean processpasstwo =
TRUE;
342 float *redfactor = NULL;
343 float *bluefactor = NULL;
344 float *oldraw = NULL;
346 float *RawDataTmp = NULL;
347 char *buffer1 = NULL;
348 char *thread_buffers = NULL;
349 size_t padded_buffersize = 0;
350 double fitparams[2][2][16] = { 0 };
351 float blockvar[2][2] = { { 0, 0 }, { 0, 0 } };
357 const float *
const in =
out;
359 const float caautostrength = 4.0f;
363 const int tsh = ts / 2;
365 const int v1 = ts, v2 = 2 * ts, v3 = 3 * ts, v4 = 4 * ts;
368 for(
int i = 0;
i < 2;
i++)
369 for(
int j = 0; j < 2; j++)
370 if(
FC(
i, j, filters) == 3)
377 const size_t buffsize = (size_t)h_width * h_height;
384 memset(redfactor, 0,
sizeof(
float) * buffsize);
391 memset(bluefactor, 0,
sizeof(
float) * buffsize);
398 memset(oldraw, 0,
sizeof(
float) * buffsize * 2);
403 for(
int col = (
FC(
row, 0, filters) & 1); col <
width; col += 2)
405 oldraw[
row * h_width + col / 2] = in[
row *
width + col];
427 const int border = 8;
428 const int border2 = 16;
430 const int vz1 = (
height + border2) % (ts - border2) == 0 ? 1 : 0;
431 const int hz1 = (
width + border2) % (ts - border2) == 0 ? 1 : 0;
432 const int vblsz = ceil((
float)(
height + border2) / (ts - border2) + 2 + vz1);
433 const int hblsz = ceil((
float)(
width + border2) / (ts - border2) + 2 + hz1);
435 buffer1 = (
char *)calloc((
size_t)vblsz * hblsz * (2 * 2 + 1),
sizeof(
float));
442 const size_t buffersize =
sizeof(float) * 3 * ts * ts + 6 *
sizeof(
float) * ts * tsh + 8 * 64 + 63;
451 float *blockwt = (
float *)buffer1;
452 float(*blockshifts)[2][2] = (float(*)[2][2])(buffer1 + (
sizeof(
float) * vblsz * hblsz));
454 float blockave[2][2] = { { 0, 0 }, { 0, 0 } };
455 float blocksqave[2][2] = { { 0, 0 }, { 0, 0 } };
456 float blockdenom[2][2] = { { 0, 0 }, { 0, 0 } };
458 int polyord = 4, numpar = 16;
460 const float eps = 1e-5f, eps2 = 1e-10f;
462 for (
size_t it = 0; it < iterations && processpasstwo; it++)
472 int shifthfloor[3], shiftvfloor[3], shifthceil[3], shiftvceil[3];
475 float coeff[2][3][2];
480 float shifthfrac[3], shiftvfrac[3];
482 float blockavethr[2][2] = { { 0, 0 }, { 0, 0 } }, blocksqavethr[2][2] = { { 0, 0 }, { 0, 0 } },
483 blockdenomthr[2][2] = { { 0, 0 }, { 0, 0 } };
487 char *data = (
char *)(((uintptr_t)buffer + (uintptr_t)63) / 64 * 64);
494 rgb[0] = (float(*))data;
495 rgb[1] = (float(*))(data + 1 *
sizeof(
float) * ts * ts + 1 * 64);
496 rgb[2] = (float(*))(data + 2 *
sizeof(
float) * ts * ts + 2 * 64);
499 float *rbhpfh = (float(*))(data + 3 *
sizeof(
float) * ts * ts + 3 * 64);
501 float *rbhpfv = (float(*))(data + 3 *
sizeof(
float) * ts * ts +
sizeof(float) * ts * tsh + 4 * 64);
503 float *rblpfh = (float(*))(data + 4 *
sizeof(
float) * ts * ts + 5 * 64);
505 float *rblpfv = (float(*))(data + 4 *
sizeof(
float) * ts * ts +
sizeof(float) * ts * tsh + 6 * 64);
507 float *grblpfh = (float(*))(data + 5 *
sizeof(
float) * ts * ts + 7 * 64);
509 float *grblpfv = (float(*))(data + 5 *
sizeof(
float) * ts * ts +
sizeof(float) * ts * tsh + 8 * 64);
511 float *grbdiff = rbhpfh;
513 float *gshift = rbhpfv;
519 for(
int left = -border; left <
width; left += ts - border2)
522 const int vblock = ((
top + border) / (ts - border2)) + 1;
523 const int hblock = ((left + border) / (ts - border2)) + 1;
525 const int right =
MIN(left + ts,
width + border);
526 const int rr1 = bottom -
top;
527 const int cc1 = right - left;
528 const int rrmin =
top < 0 ? border : 0;
530 const int ccmin = left < 0 ? border : 0;
531 const int ccmax = right >
width ?
width - left : cc1;
537 for(
int rr = rrmin; rr < rrmax; rr++)
538 for(
int row = rr +
top, cc = ccmin; cc < ccmax; cc++)
541 int c =
FC(rr, cc, filters);
543 int indx1 = rr * ts + cc;
544 rgb[c][indx1] = (in[indx]);
551 for(
int rr = 0; rr < border; rr++)
552 for(
int cc = ccmin; cc < ccmax; cc++)
554 int c =
FC(rr, cc, filters);
555 rgb[c][rr * ts + cc] =
rgb[c][(border2 - rr) * ts + cc];
561 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
562 for(
int cc = ccmin; cc < ccmax; cc++)
564 int c =
FC(rr, cc, filters);
565 rgb[c][(rrmax + rr) * ts + cc] = (in[(
height - rr - 2) *
width + left + cc]);
571 for(
int rr = rrmin; rr < rrmax; rr++)
572 for(
int cc = 0; cc < border; cc++)
574 int c =
FC(rr, cc, filters);
575 rgb[c][rr * ts + cc] =
rgb[c][rr * ts + border2 - cc];
581 for(
int rr = rrmin; rr < rrmax; rr++)
582 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
584 int c =
FC(rr, cc, filters);
590 if(rrmin > 0 && ccmin > 0)
592 for(
int rr = 0; rr < border; rr++)
593 for(
int cc = 0; cc < border; cc++)
595 int c =
FC(rr, cc, filters);
596 rgb[c][(rr)*ts + cc] = (in[(border2 - rr) *
width + border2 - cc]);
600 if(rrmax < rr1 && ccmax < cc1)
602 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
603 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
605 int c =
FC(rr, cc, filters);
606 rgb[c][(rrmax + rr) * ts + ccmax + cc] = (in[(
height - rr - 2) *
width + (
width - cc - 2)]);
610 if(rrmin > 0 && ccmax < cc1)
612 for(
int rr = 0; rr < border; rr++)
613 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
615 int c =
FC(rr, cc, filters);
616 rgb[c][(rr)*ts + ccmax + cc] = (in[(border2 - rr) *
width + (
width - cc - 2)]);
620 if(rrmax < rr1 && ccmin > 0)
622 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
623 for(
int cc = 0; cc < border; cc++)
625 int c =
FC(rr, cc, filters);
626 rgb[c][(rrmax + rr) * ts + cc] = (in[(
height - rr - 2) *
width + (border2 - cc)]);
634 for(
int rr = 3; rr < rr1 - 3; rr++)
637 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)
640 float wtu = 1.f /
SQR(
eps + fabsf(
rgb[1][indx + v1] -
rgb[1][indx - v1])
641 + fabsf(
rgb[c][indx] -
rgb[c][indx - v2])
642 + fabsf(
rgb[1][indx - v1] -
rgb[1][indx - v3]));
643 float wtd = 1.f /
SQR(
eps + fabsf(
rgb[1][indx - v1] -
rgb[1][indx + v1])
644 + fabsf(
rgb[c][indx] -
rgb[c][indx + v2])
645 + fabsf(
rgb[1][indx + v1] -
rgb[1][indx + v3]));
646 float wtl = 1.f /
SQR(
eps + fabsf(
rgb[1][indx + 1] -
rgb[1][indx - 1])
647 + fabsf(
rgb[c][indx] -
rgb[c][indx - 2])
648 + fabsf(
rgb[1][indx - 1] -
rgb[1][indx - 3]));
649 float wtr = 1.f /
SQR(
eps + fabsf(
rgb[1][indx - 1] -
rgb[1][indx + 1])
650 + fabsf(
rgb[c][indx] -
rgb[c][indx + 2])
651 + fabsf(
rgb[1][indx + 1] -
rgb[1][indx + 3]));
655 rgb[1][indx] = (wtu *
rgb[1][indx - v1] + wtd *
rgb[1][indx + v1] + wtl *
rgb[1][indx - 1]
656 + wtr *
rgb[1][indx + 1])
657 / (wtu + wtd + wtl + wtr);
662 for(
int col =
MAX(left + 3, 0), indx = rr * ts + 3 - (left < 0 ? (left + 3) : 0);
663 col <
MIN(cc1 + left - 3,
width); col++, indx++)
670 for(
int rr = 4; rr < rr1 - 4; rr++)
672 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)
674 rbhpfv[indx >> 1] = fabsf(
675 fabsf((
rgb[1][indx] -
rgb[c][indx]) - (
rgb[1][indx + v4] -
rgb[c][indx + v4]))
676 + fabsf((
rgb[1][indx - v4] -
rgb[c][indx - v4]) - (
rgb[1][indx] -
rgb[c][indx]))
677 - fabsf((
rgb[1][indx - v4] -
rgb[c][indx - v4]) - (
rgb[1][indx + v4] -
rgb[c][indx + v4])));
678 rbhpfh[indx >> 1] = fabsf(
679 fabsf((
rgb[1][indx] -
rgb[c][indx]) - (
rgb[1][indx + 4] -
rgb[c][indx + 4]))
680 + fabsf((
rgb[1][indx - 4] -
rgb[c][indx - 4]) - (
rgb[1][indx] -
rgb[c][indx]))
681 - fabsf((
rgb[1][indx - 4] -
rgb[c][indx - 4]) - (
rgb[1][indx + 4] -
rgb[c][indx + 4])));
684 float glpfv = 0.25f * (2.f *
rgb[1][indx] +
rgb[1][indx + v2] +
rgb[1][indx - v2]);
685 float glpfh = 0.25f * (2.f *
rgb[1][indx] +
rgb[1][indx + 2] +
rgb[1][indx - 2]);
687 =
eps + fabsf(glpfv - 0.25f * (2.f *
rgb[c][indx] +
rgb[c][indx + v2] +
rgb[c][indx - v2]));
689 =
eps + fabsf(glpfh - 0.25f * (2.f *
rgb[c][indx] +
rgb[c][indx + 2] +
rgb[c][indx - 2]));
691 = glpfv + 0.25f * (2.f *
rgb[c][indx] +
rgb[c][indx + v2] +
rgb[c][indx - v2]);
692 grblpfh[indx >> 1] = glpfh + 0.25f * (2.f *
rgb[c][indx] +
rgb[c][indx + 2] +
rgb[c][indx - 2]);
696 for(
int dir = 0; dir < 2; dir++)
698 for(
int k = 0;
k < 3;
k++)
700 for(
int c = 0; c < 2; c++)
709 for(
int rr = 8; rr < rr1 - 8; rr++)
711 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)
719 float gdiff = 0.3125f * (
rgb[1][indx + ts] -
rgb[1][indx - ts])
720 + 0.09375f * (
rgb[1][indx + ts + 1] -
rgb[1][indx - ts + 1]
721 +
rgb[1][indx + ts - 1] -
rgb[1][indx - ts - 1]);
722 float deltgrb = (
rgb[c][indx] -
rgb[1][indx]);
724 float gradwt = fabsf(0.25f * rbhpfv[indx >> 1]
725 + 0.125f * (rbhpfv[(indx >> 1) + 1] + rbhpfv[(indx >> 1) - 1]))
726 * (grblpfv[(indx >> 1) - v1] + grblpfv[(indx >> 1) + v1])
727 / (
eps + 0.1f * (grblpfv[(indx >> 1) - v1] + grblpfv[(indx >> 1) + v1])
728 + rblpfv[(indx >> 1) - v1] + rblpfv[(indx >> 1) + v1]);
730 coeff[0][0][c >> 1] += gradwt * deltgrb * deltgrb;
731 coeff[0][1][c >> 1] += gradwt * gdiff * deltgrb;
732 coeff[0][2][c >> 1] += gradwt * gdiff * gdiff;
735 gdiff = 0.3125f * (
rgb[1][indx + 1] -
rgb[1][indx - 1])
736 + 0.09375f * (
rgb[1][indx + 1 + ts] -
rgb[1][indx - 1 + ts] +
rgb[1][indx + 1 - ts]
737 -
rgb[1][indx - 1 - ts]);
739 gradwt = fabsf(0.25f * rbhpfh[indx >> 1]
740 + 0.125f * (rbhpfh[(indx >> 1) + v1] + rbhpfh[(indx >> 1) - v1]))
741 * (grblpfh[(indx >> 1) - 1] + grblpfh[(indx >> 1) + 1])
742 / (
eps + 0.1f * (grblpfh[(indx >> 1) - 1] + grblpfh[(indx >> 1) + 1])
743 + rblpfh[(indx >> 1) - 1] + rblpfh[(indx >> 1) + 1]);
745 coeff[1][0][c >> 1] += gradwt * deltgrb * deltgrb;
746 coeff[1][1][c >> 1] += gradwt * gdiff * deltgrb;
747 coeff[1][2][c >> 1] += gradwt * gdiff * gdiff;
757 for(
int c = 0; c < 2; c++)
759 for(
int dir = 0; dir < 2; dir++)
765 if(
coeff[dir][2][c] > eps2)
767 CAshift[dir][c] =
coeff[dir][1][c] /
coeff[dir][2][c];
768 blockwt[vblock * hblsz + hblock] =
coeff[dir][2][c] / (
eps +
coeff[dir][0][c]);
772 CAshift[dir][c] = 17.0;
773 blockwt[vblock * hblsz + hblock] = 0;
780 if(fabsf(CAshift[dir][c]) < 2.0f)
782 blockavethr[dir][c] += CAshift[dir][c];
783 blocksqavethr[dir][c] +=
SQR(CAshift[dir][c]);
784 blockdenomthr[dir][c] += 1;
787 blockshifts[vblock * hblsz + hblock][c][dir] = CAshift[dir][c];
796#pragma omp critical(cadetectpass2)
799 for(
int dir = 0; dir < 2; dir++)
800 for(
int c = 0; c < 2; c++)
802 blockdenom[dir][c] += blockdenomthr[dir][c];
803 blocksqave[dir][c] += blocksqavethr[dir][c];
804 blockave[dir][c] += blockavethr[dir][c];
815 for(
int dir = 0; dir < 2; dir++)
816 for(
int c = 0; c < 2; c++)
818 if(blockdenom[dir][c])
821 = blocksqave[dir][c] / blockdenom[dir][c] -
SQR(blockave[dir][c] / blockdenom[dir][c]);
825 processpasstwo =
FALSE;
836 for(
int vblock = 1; vblock < vblsz - 1; vblock++)
838 for(
int c = 0; c < 2; c++)
840 for(
int i = 0;
i < 2;
i++)
842 blockshifts[vblock * hblsz][c][
i] = blockshifts[(vblock)*hblsz + 2][c][
i];
843 blockshifts[vblock * hblsz + hblsz - 1][c][
i] = blockshifts[(vblock)*hblsz + hblsz - 3][c][
i];
848 for(
int hblock = 0; hblock < hblsz; hblock++)
850 for(
int c = 0; c < 2; c++)
852 for(
int i = 0;
i < 2;
i++)
854 blockshifts[hblock][c][
i] = blockshifts[2 * hblsz + hblock][c][
i];
855 blockshifts[(vblsz - 1) * hblsz + hblock][c][
i]
856 = blockshifts[(vblsz - 3) * hblsz + hblock][c][
i];
864 double polymat[2][2][256], shiftmat[2][2][16];
866 for(
int i = 0;
i < 256;
i++)
868 polymat[0][0][
i] = polymat[0][1][
i] = polymat[1][0][
i] = polymat[1][1][
i] = 0;
871 for(
int i = 0;
i < 16;
i++)
873 shiftmat[0][0][
i] = shiftmat[0][1][
i] = shiftmat[1][0][
i] = shiftmat[1][1][
i] = 0;
876 int numblox[2] = { 0, 0 };
878 for(
int vblock = 1; vblock < vblsz - 1; vblock++)
879 for(
int hblock = 1; hblock < hblsz - 1; hblock++)
882 for(
int c = 0; c < 2; c++)
885 for(
int dir = 0; dir < 2; dir++)
889 p[0] = blockshifts[(vblock - 1) * hblsz + hblock - 1][c][dir];
890 p[1] = blockshifts[(vblock - 1) * hblsz + hblock][c][dir];
891 p[2] = blockshifts[(vblock - 1) * hblsz + hblock + 1][c][dir];
892 p[3] = blockshifts[(vblock)*hblsz + hblock - 1][c][dir];
893 p[4] = blockshifts[(vblock)*hblsz + hblock][c][dir];
894 p[5] = blockshifts[(vblock)*hblsz + hblock + 1][c][dir];
895 p[6] = blockshifts[(vblock + 1) * hblsz + hblock - 1][c][dir];
896 p[7] = blockshifts[(vblock + 1) * hblsz + hblock][c][dir];
897 p[8] = blockshifts[(vblock + 1) * hblsz + hblock + 1][c][dir];
922 if(
SQR(bstemp[0]) > caautostrength * blockvar[0][c]
923 ||
SQR(bstemp[1]) > caautostrength * blockvar[1][c])
930 for(
int dir = 0; dir < 2; dir++)
932 double powVblockInit = 1.0;
933 for(
int i = 0;
i < polyord;
i++)
935 double powHblockInit = 1.0;
936 for(
int j = 0; j < polyord; j++)
938 double powVblock = powVblockInit;
939 for(
int m = 0;
m < polyord;
m++)
941 double powHblock = powHblockInit;
942 for(
int n = 0;
n < polyord;
n++)
944 polymat[c][dir][numpar * (polyord *
i + j) + (polyord *
m +
n)]
945 += powVblock * powHblock * blockwt[vblock * hblsz + hblock];
950 shiftmat[c][dir][(polyord *
i + j)]
951 += powVblockInit * powHblockInit * bstemp[dir] * blockwt[vblock * hblsz + hblock];
952 powHblockInit *= hblock;
954 powVblockInit *= vblock;
960 numblox[1] =
MIN(numblox[0], numblox[1]);
970 fprintf(stderr,
", numblox = %d \n", numblox[1]);
971 processpasstwo =
FALSE;
978 for(
int c = 0; c < 2; c++)
979 for(
int dir = 0; dir < 2; dir++)
981 if(!
LinEqSolve(numpar, polymat[c][dir], shiftmat[c][dir], fitparams[c][dir]))
983 fprintf(stderr,
", correction pass failed -- can't solve linear equations for colour %d direction %d", c, dir);
984 processpasstwo =
FALSE;
1002 for(
int left = -border; left <
width; left += ts - border2)
1004 memset(buffer, 0, buffersize);
1005 float lblockshifts[2][2];
1006 const int vblock = ((
top + border) / (ts - border2)) + 1;
1007 const int hblock = ((left + border) / (ts - border2)) + 1;
1009 const int right =
MIN(left + ts,
width + border);
1010 const int rr1 = bottom -
top;
1011 const int cc1 = right - left;
1013 const int rrmin =
top < 0 ? border : 0;
1015 const int ccmin = left < 0 ? border : 0;
1016 const int ccmax = right >
width ?
width - left : cc1;
1022 for(
int rr = rrmin; rr < rrmax; rr++)
1023 for(
int row = rr +
top, cc = ccmin; cc < ccmax; cc++)
1025 int col = cc + left;
1026 int c =
FC(rr, cc, filters);
1028 int indx1 = rr * ts + cc;
1029 rgb[c][indx1] = (in[indx]);
1033 rgb[1][indx1] = Gtmp[indx];
1041 for(
int rr = 0; rr < border; rr++)
1042 for(
int cc = ccmin; cc < ccmax; cc++)
1044 int c =
FC(rr, cc, filters);
1045 rgb[c][rr * ts + cc] =
rgb[c][(border2 - rr) * ts + cc];
1046 rgb[1][rr * ts + cc] =
rgb[1][(border2 - rr) * ts + cc];
1052 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
1053 for(
int cc = ccmin; cc < ccmax; cc++)
1055 int c =
FC(rr, cc, filters);
1056 rgb[c][(rrmax + rr) * ts + cc] = (in[(
height - rr - 2) *
width + left + cc]);
1057 rgb[1][(rrmax + rr) * ts + cc] = Gtmp[(
height - rr - 2) *
width + left + cc];
1063 for(
int rr = rrmin; rr < rrmax; rr++)
1064 for(
int cc = 0; cc < border; cc++)
1066 int c =
FC(rr, cc, filters);
1067 rgb[c][rr * ts + cc] =
rgb[c][rr * ts + border2 - cc];
1068 rgb[1][rr * ts + cc] =
rgb[1][rr * ts + border2 - cc];
1074 for(
int rr = rrmin; rr < rrmax; rr++)
1075 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
1077 int c =
FC(rr, cc, filters);
1078 rgb[c][rr * ts + ccmax + cc] = (in[(
top + rr) *
width + (
width - cc - 2)]);
1079 rgb[1][rr * ts + ccmax + cc] = Gtmp[(
top + rr) *
width + (
width - cc - 2)];
1084 if(rrmin > 0 && ccmin > 0)
1086 for(
int rr = 0; rr < border; rr++)
1087 for(
int cc = 0; cc < border; cc++)
1089 int c =
FC(rr, cc, filters);
1090 rgb[c][(rr)*ts + cc] = (in[(border2 - rr) *
width + border2 - cc]);
1091 rgb[1][(rr)*ts + cc] = Gtmp[(border2 - rr) *
width + border2 - cc];
1095 if(rrmax < rr1 && ccmax < cc1)
1097 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
1098 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
1100 int c =
FC(rr, cc, filters);
1101 rgb[c][(rrmax + rr) * ts + ccmax + cc] = (in[(
height - rr - 2) *
width + (
width - cc - 2)]);
1102 rgb[1][(rrmax + rr) * ts + ccmax + cc] = Gtmp[(
height - rr - 2) *
width + (
width - cc - 2)];
1106 if(rrmin > 0 && ccmax < cc1)
1108 for(
int rr = 0; rr < border; rr++)
1109 for(
int cc = 0; cc <
MIN(border, cc1 - ccmax); cc++)
1111 int c =
FC(rr, cc, filters);
1112 rgb[c][(rr)*ts + ccmax + cc] = (in[(border2 - rr) *
width + (
width - cc - 2)]);
1113 rgb[1][(rr)*ts + ccmax + cc] = Gtmp[(border2 - rr) *
width + (
width - cc - 2)];
1117 if(rrmax < rr1 && ccmin > 0)
1119 for(
int rr = 0; rr <
MIN(border, rr1 - rrmax); rr++)
1120 for(
int cc = 0; cc < border; cc++)
1122 int c =
FC(rr, cc, filters);
1123 rgb[c][(rrmax + rr) * ts + cc] = (in[(
height - rr - 2) *
width + (border2 - cc)]);
1124 rgb[1][(rrmax + rr) * ts + cc] = Gtmp[(
height - rr - 2) *
width + (border2 - cc)];
1131 lblockshifts[0][0] = lblockshifts[0][1] = 0;
1132 lblockshifts[1][0] = lblockshifts[1][1] = 0;
1133 double powVblock = 1.0;
1134 for(
int i = 0;
i < polyord;
i++)
1136 double powHblock = powVblock;
1137 for(
int j = 0; j < polyord; j++)
1140 lblockshifts[0][0] += powHblock * fitparams[0][0][polyord *
i + j];
1141 lblockshifts[0][1] += powHblock * fitparams[0][1][polyord *
i + j];
1142 lblockshifts[1][0] += powHblock * fitparams[1][0][polyord *
i + j];
1143 lblockshifts[1][1] += powHblock * fitparams[1][1][polyord *
i + j];
1144 powHblock *= hblock;
1146 powVblock *= vblock;
1148 const float bslim = 3.99;
1149 lblockshifts[0][0] =
LIM(lblockshifts[0][0], -bslim, bslim);
1150 lblockshifts[0][1] =
LIM(lblockshifts[0][1], -bslim, bslim);
1151 lblockshifts[1][0] =
LIM(lblockshifts[1][0], -bslim, bslim);
1152 lblockshifts[1][1] =
LIM(lblockshifts[1][1], -bslim, bslim);
1156 for(
int c = 0; c < 3; c += 2)
1160 shiftvfloor[c] = floor((
float)lblockshifts[c >> 1][0]);
1161 shiftvceil[c] = ceil((
float)lblockshifts[c >> 1][0]);
1162 if (lblockshifts[c>>1][0] < 0.f) {
1163 float tmp = shiftvfloor[c];
1164 shiftvfloor[c] = shiftvceil[c];
1165 shiftvceil[c] = tmp;
1167 shiftvfrac[c] = fabsf(lblockshifts[c>>1][0] - shiftvfloor[c]);
1169 shifthfloor[c] = floor((
float)lblockshifts[c >> 1][1]);
1170 shifthceil[c] = ceil((
float)lblockshifts[c >> 1][1]);
1171 if (lblockshifts[c>>1][1] < 0.f) {
1172 float tmp = shifthfloor[c];
1173 shifthfloor[c] = shifthceil[c];
1174 shifthceil[c] = tmp;
1176 shifthfrac[c] = fabsf(lblockshifts[c>>1][1] - shifthfloor[c]);
1179 GRBdir[0][c] = lblockshifts[c >> 1][0] > 0 ? 2 : -2;
1180 GRBdir[1][c] = lblockshifts[c >> 1][1] > 0 ? 2 : -2;
1184 for(
int rr = 4; rr < rr1 - 4; rr++)
1186 for(
int cc = 4 + (
FC(rr, 2, filters) & 1), c =
FC(rr, cc, filters); cc < cc1 - 4; cc += 2)
1189 float Ginthfloor =
intp(shifthfrac[c],
rgb[1][(rr + shiftvfloor[c]) * ts + cc + shifthceil[c]],
1190 rgb[1][(rr + shiftvfloor[c]) * ts + cc + shifthfloor[c]]);
1191 float Ginthceil =
intp(shifthfrac[c],
rgb[1][(rr + shiftvceil[c]) * ts + cc + shifthceil[c]],
1192 rgb[1][(rr + shiftvceil[c]) * ts + cc + shifthfloor[c]]);
1194 float Gint =
intp(shiftvfrac[c], Ginthceil, Ginthfloor);
1199 grbdiff[((rr)*ts + cc) >> 1] = Gint -
rgb[c][(rr)*ts + cc];
1200 gshift[((rr)*ts + cc) >> 1] = Gint;
1204 shifthfrac[0] /= 2.f;
1205 shifthfrac[2] /= 2.f;
1206 shiftvfrac[0] /= 2.f;
1207 shiftvfrac[2] /= 2.f;
1211 for(
int rr = 8; rr < rr1 - 8; rr++)
1212 for(
int cc = 8 + (
FC(rr, 2, filters) & 1), c =
FC(rr, cc, filters), indx = rr * ts + cc;
1213 cc < cc1 - 8; cc += 2, indx += 2)
1216 float grbdiffold =
rgb[1][indx] -
rgb[c][indx];
1219 float grbdiffinthfloor
1220 =
intp(shifthfrac[c], grbdiff[(indx - GRBdir[1][c]) >> 1], grbdiff[indx >> 1]);
1221 float grbdiffinthceil
1222 =
intp(shifthfrac[c], grbdiff[((rr - GRBdir[0][c]) * ts + cc - GRBdir[1][c]) >> 1],
1223 grbdiff[((rr - GRBdir[0][c]) * ts + cc) >> 1]);
1225 float grbdiffint =
intp(shiftvfrac[c], grbdiffinthceil, grbdiffinthfloor);
1229 float RBint =
rgb[1][indx] - grbdiffint;
1231 if(fabsf(RBint -
rgb[c][indx]) < 0.25f * (RBint +
rgb[c][indx]))
1233 if(fabsf(grbdiffold) > fabsf(grbdiffint))
1235 rgb[c][indx] = RBint;
1242 float p0 = 1.0f / (
eps + fabsf(
rgb[1][indx] - gshift[indx >> 1]));
1243 float p1 = 1.0f / (
eps + fabsf(
rgb[1][indx] - gshift[(indx - GRBdir[1][c]) >> 1]));
1244 float p2 = 1.0f / (
eps + fabsf(
rgb[1][indx] - gshift[((rr - GRBdir[0][c]) * ts + cc) >> 1]));
1246 = 1.0f / (
eps + fabsf(
rgb[1][indx]
1247 - gshift[((rr - GRBdir[0][c]) * ts + cc - GRBdir[1][c]) >> 1]));
1249 grbdiffint = (p0 * grbdiff[indx >> 1] + p1 * grbdiff[(indx - GRBdir[1][c]) >> 1]
1250 + p2 * grbdiff[((rr - GRBdir[0][c]) * ts + cc) >> 1]
1251 + p3 * grbdiff[((rr - GRBdir[0][c]) * ts + cc - GRBdir[1][c]) >> 1])
1252 / (p0 + p1 + p2 + p3);
1256 if(fabsf(grbdiffold) > fabsf(grbdiffint))
1258 rgb[c][indx] =
rgb[1][indx] - grbdiffint;
1263 if(grbdiffold * grbdiffint < 0)
1265 rgb[c][indx] =
rgb[1][indx] - 0.5f * (grbdiffold + grbdiffint);
1270 for(
int rr = border; rr < rr1 - border; rr++)
1272 int c =
FC(rr +
top, left + border + (
FC(rr +
top, 2, filters) & 1), filters);
1274 for(
int row = rr +
top, cc = border + (
FC(rr, 2, filters) & 1),
1275 indx = (
row *
width + cc + left) >> 1;
1276 cc < cc1 - border; cc += 2, indx++)
1279 RawDataTmp[indx] =
rgb[c][(rr)*ts + cc];
1292 for(
int col = 0 + (
FC(
row, 0, filters) & 1), indx = (
row *
width + col) >> 1; col <
width;
1303 if(avoidshift && processpasstwo)
1311 const int firstCol =
FC(
row, 0, filters) & 1;
1312 const int color =
FC(
row, firstCol, filters);
1313 float *nongreen = (color == 0) ? redfactor : bluefactor;
1314 for(
int col = firstCol; col <
width; col += 2)
1316 nongreen[(
row / 2) * h_width + col / 2] = (in[
row *
width + col] <= 1.0f || oldraw[
row * h_width + col / 2] <= 1.0f)
1317 ? 1.0f :
LIM(oldraw[
row * h_width + col / 2] / in[
row *
width + col], 0.5f, 2.0f);
1324 for(
int col = 0; col < h_width; col++)
1326 redfactor[(h_height-1) * h_width + col] = redfactor[(h_height-2) * h_width + col];
1327 bluefactor[(h_height-1) * h_width + col] = bluefactor[(h_height-2) * h_width + col];
1334 const int ngRow = 1 - (
FC(0, 0, filters) & 1);
1335 const int ngCol =
FC(ngRow, 0, filters) & 1;
1336 const int color =
FC(ngRow, ngCol, filters);
1337 float *nongreen = (color == 0) ? redfactor : bluefactor;
1340 nongreen[
row * h_width + h_width - 1] = nongreen[
row * h_width + h_width - 2];
1345 float valmax[] = { 10.0f };
1346 float valmin[] = { 0.1f };
1366 const int firstCol =
FC(
row, 0, filters) & 1;
1367 const int color =
FC(
row, firstCol, filters);
1368 float *nongreen = (color == 0) ? redfactor : bluefactor;
1369 for(
int col = firstCol; col <
width - 2; col += 2)
1371 const float correction = nongreen[
row / 2 * h_width + col / 2];