Skip to content

Commit 522d9f6

Browse files
committed
Refine the scaling mechanism for Qfactor feature
1 parent 6f9c857 commit 522d9f6

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

source/core/codestream/j2kmarkers.cpp

+40-40
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ QCC_marker::QCC_marker(uint16_t Csiz, uint16_t c, uint8_t number_of_guardbits, u
749749
0.053497514821622}; // gain is doubled(x2)
750750

751751
// Square roots of the visual weighting factors for 4:4:4 YCbCr content
752-
const double W_b_sqrt[3][16] = {{0.0901, 0.2758, 0.2758, 0.7018, 0.8378, 0.8378, 1.0000, 1.0000, 1.0000,
752+
const double W_b_sqrt[3][15] = {{0.0901, 0.2758, 0.2758, 0.7018, 0.8378, 0.8378, 1.0000, 1.0000, 1.0000,
753753
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000},
754754
{0.0263, 0.0863, 0.0863, 0.1362, 0.2564, 0.2564, 0.3346, 0.4691, 0.4691,
755755
0.5444, 0.6523, 0.6523, 0.7078, 0.7797, 0.7797},
@@ -831,58 +831,58 @@ QCC_marker::QCC_marker(uint16_t Csiz, uint16_t c, uint8_t number_of_guardbits, u
831831
// lossy with qfactor: The detail of Qfactor feature is described in HTJ2K white paper at
832832
// https://htj2k.com/wp-content/uploads/white-paper.pdf
833833
double M_Q;
834+
uint8_t t0 = 65, t1 = 97;
835+
const double alpha_T0 = 0.04;
836+
const double alpha_T1 = 0.10;
837+
const double M_T0 = 2.0 * (1.0 - t0 / 100.0);
838+
const double M_T1 = 2.0 * (1.0 - t1 / 100.0);
839+
double alpha_Q = alpha_T0;
840+
double qfactor_power = 1.0;
841+
834842
if (qfactor < 50) {
835843
M_Q = 50.0 / qfactor;
836844
} else {
837845
M_Q = 2.0 * (1.0 - qfactor / 100.0);
838846
}
839-
constexpr double M_T0 = 2.0 * (1.0 - 65.0 / 100.0);
840-
constexpr double M_T1 = 2.0 * (1.0 - 97.0 / 100.0);
841-
const double alpha_T0 = 0.04;
842-
const double alpha_T1 = 0.10;
843-
double alpha_Q = alpha_T0;
844-
// adjust the scaling factor alpha_Q
845-
if (M_Q >= M_T1) {
846-
alpha_Q = alpha_T1;
847-
} else if (M_T0 < M_Q && M_Q < M_T1) {
848-
alpha_Q = alpha_T1 * pow((alpha_T0 / alpha_T1), (log(M_T1) - log(M_Q)) / (log(M_T1) - log(M_T0)));
847+
// adjust the scaling
848+
if (qfactor >= t1) {
849+
qfactor_power = 0.0;
850+
alpha_Q = alpha_T1;
851+
} else if (qfactor > t0) {
852+
qfactor_power = (log(M_T1) - log(M_Q)) / (log(M_T1) - log(M_T0));
853+
alpha_Q = alpha_T1 * pow(alpha_T0 / alpha_T1, qfactor_power);
849854
}
855+
856+
const double eps0 = sqrt(0.5) / static_cast<double>(1 << RI);
857+
double delta_Q = alpha_Q * M_Q + eps0;
858+
double delta_ref = delta_Q * G_c_sqrt[0];
859+
double G_c = G_c_sqrt[Cqcc]; // gain of color transform
860+
850861
for (int i = 0; i < epsilon.size(); ++i) {
851-
int32_t eps, m;
852-
double w_b, g_c;
853-
w_b = W_b_sqrt[Cqcc][i];
854-
// adjust the scaling factor w_b
855-
if (M_Q >= M_T1) {
856-
w_b = 1.0;
857-
} else if (M_T0 < M_Q && M_Q < M_T1) {
858-
w_b = pow(w_b, (log(M_T1) - log(M_Q)) / (log(M_T1) - log(M_T0)));
859-
}
860-
if (i > 15 || i == epsilon.size() - 1) {
861-
w_b = 1.0;
862-
}
863-
g_c = G_c_sqrt[Cqcc];
862+
int32_t exponent, mantissa;
863+
double w_b;
864+
// w_b for LL band shall be 1.0
865+
w_b = (i == epsilon.size() - 1) ? 1.0 : pow(W_b_sqrt[Cqcc][i], qfactor_power);
864866

865-
double delta_Q = alpha_Q * M_Q + (1 / sqrt(2) / (1 << RI));
866-
double delta_ref = delta_Q * G_c_sqrt[0];
867-
double fval = delta_ref / (sqrt(wmse_or_BIBO[i]) * w_b * g_c);
868-
for (eps = 0; fval < 1.0; eps++) {
867+
double fval = delta_ref / (sqrt(wmse_or_BIBO[i]) * w_b * G_c);
868+
for (exponent = 0; fval < 1.0; exponent++) {
869869
fval *= 2.0;
870870
}
871-
m = static_cast<int32_t>(floor((fval - 1.0) * static_cast<double>(1 << 11) + 0.5));
872-
if (m >= (1 << 11)) {
873-
m = 0;
874-
eps--;
871+
mantissa = static_cast<int32_t>(floor((fval - 1.0) * static_cast<double>(1 << 11) + 0.5));
872+
if (mantissa >= (1 << 11)) {
873+
mantissa = 0;
874+
exponent--;
875875
}
876-
if (eps > 31) {
877-
eps = 31;
878-
m = 0;
876+
if (exponent > 31) {
877+
exponent = 31;
878+
mantissa = 0;
879879
}
880-
if (eps < 0) {
881-
eps = 0;
882-
m = (1 << 11) - 1;
880+
if (exponent < 0) {
881+
exponent = 0;
882+
mantissa = (1 << 11) - 1;
883883
}
884-
epsilon[epsilon.size() - i - 1] = eps;
885-
mu[epsilon.size() - i - 1] = m;
884+
epsilon[epsilon.size() - i - 1] = exponent;
885+
mu[epsilon.size() - i - 1] = mantissa;
886886
}
887887
}
888888

0 commit comments

Comments
 (0)