diff --git a/src/_imaging.c b/src/_imaging.c index d1262f5b8..f023a6328 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -740,6 +740,7 @@ _prepare_lut_table(PyObject* table, Py_ssize_t table_size) } else { prepared[i] = table_data[i] * (255 << PRECISION_BITS) + 0.5; } + // printf("%f, %d ", table_data[i], prepared[i]); } #undef PRECISION_BITS diff --git a/src/libImaging/ColorLUT.c b/src/libImaging/ColorLUT.c index 6288625a1..dab9eb627 100644 --- a/src/libImaging/ColorLUT.c +++ b/src/libImaging/ColorLUT.c @@ -2,30 +2,104 @@ #include -#define ROUND_UP(f) ((int) ((f) >= 0.0 ? (f) + 0.5F : (f) - 0.5F)) - - /* 8 bits for result. Table can overflow [0, 1.0] range, so we need extra bits for overflow and negative values. NOTE: This value should be the same as in _imaging/_prepare_lut_table() */ #define PRECISION_BITS (16 - 8 - 2) +#define PRECISION_ROUNDING (1<<(PRECISION_BITS-1)) + + +UINT8 _lookups2[1024] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, + 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, + 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, + 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, + 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, + 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, + 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, + 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, + 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, + 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +}; + +UINT8 *lookups2 = &_lookups2[512]; + + +static inline UINT8 clip8(int in) +{ + return lookups2[(in + PRECISION_ROUNDING) >> PRECISION_BITS]; +} static inline void -interpolate3(INT16 out[3], const INT16 a[3], const INT16 b[3], float shift) +interpolate3(INT16 out[3], const INT16 a[3], const INT16 b[3], INT16 shift) { - out[0] = a[0] * (1-shift) + b[0] * shift + 0.5; - out[1] = a[1] * (1-shift) + b[1] * shift + 0.5; - out[2] = a[2] * (1-shift) + b[2] * shift + 0.5; + out[0] = (a[0] * (0x1000-shift) + b[0] * shift + (1<<11)) >> 12; + out[1] = (a[1] * (0x1000-shift) + b[1] * shift + (1<<11)) >> 12; + out[2] = (a[2] * (0x1000-shift) + b[2] * shift + (1<<11)) >> 12; } static inline void -interpolate4(INT16 out[3], const INT16 a[3], const INT16 b[3], float shift) +interpolate4(INT16 out[4], const INT16 a[4], const INT16 b[4], INT16 shift) { - out[0] = a[0] * (1-shift) + b[0] * shift + 0.5; - out[1] = a[1] * (1-shift) + b[1] * shift + 0.5; - out[2] = a[2] * (1-shift) + b[2] * shift + 0.5; - out[3] = a[3] * (1-shift) + b[3] * shift + 0.5; + out[0] = (a[0] * (0x1000-shift) + b[0] * shift + (1<<11)) >> 12; + out[1] = (a[1] * (0x1000-shift) + b[1] * shift + (1<<11)) >> 12; + out[2] = (a[2] * (0x1000-shift) + b[2] * shift + (1<<11)) >> 12; + out[3] = (a[3] * (0x1000-shift) + b[3] * shift + (1<<11)) >> 12; } static inline int @@ -94,19 +168,45 @@ ImagingColorLUT3D_linear(Imaging imOut, Imaging imIn, int table_channels, } for (y = 0; y < imOut->ysize; y++) { - UINT8 *rowIn = (UINT8 *)imIn->image[y]; - UINT8 *rowOut = (UINT8 *)imIn->image[y]; - for (x = 0; x < imOut->xsize; x++) { - float scaled1D = rowIn[x*4 + 0] * scale1D; - float scaled2D = rowIn[x*4 + 1] * scale2D; - float scaled3D = rowIn[x*4 + 2] * scale3D; - int index1D = (int) scaled1D; - int index2D = (int) scaled2D; - int index3D = (int) scaled3D; - float shift1D = scaled1D - index1D; - float shift2D = scaled2D - index2D; - float shift3D = scaled3D - index3D; - + UINT8* rowIn = (UINT8 *)imIn->image[y]; + UINT8* rowOut = (UINT8 *)imOut->image[y]; + if (table_channels == 3) { + for (x = 0; x < imOut->xsize; x++) { + float scaled1D = rowIn[x*4 + 0] * scale1D; + float scaled2D = rowIn[x*4 + 1] * scale2D; + float scaled3D = rowIn[x*4 + 2] * scale3D; + int index1D = (int) scaled1D; + int index2D = (int) scaled2D; + int index3D = (int) scaled3D; + INT16 shift1D = (scaled1D - index1D) * 0x1000 + 0.5; + INT16 shift2D = (scaled2D - index2D) * 0x1000 + 0.5; + INT16 shift3D = (scaled3D - index3D) * 0x1000 + 0.5; + int idx = table3D_index3(index1D, index2D, index3D, size1D, size1D_2D); + INT16 result[3], left[3], right[3]; + INT16 leftleft[3], leftright[3], rightleft[3], rightright[3]; + + interpolate3(leftleft, &table[idx + 0], &table[idx + 3], shift1D); + interpolate3(leftright, &table[idx + size1D*3], + &table[idx + size1D*3 + 3], shift1D); + interpolate3(left, leftleft, leftright, shift2D); + + interpolate3(rightleft, &table[idx + size1D_2D*3], + &table[idx + size1D_2D*3 + 3], shift1D); + interpolate3(rightright, &table[idx + size1D_2D*3 + size1D*3], + &table[idx + size1D_2D*3 + size1D*3 + 3], shift1D); + interpolate3(right, rightleft, rightright, shift2D); + + interpolate3(result, left, right, shift3D); + + rowOut[x*4 + 0] = clip8(result[0]); + rowOut[x*4 + 1] = clip8(result[1]); + rowOut[x*4 + 2] = clip8(result[2]); + rowOut[x*4 + 3] = rowIn[x*4 + 3]; + + // printf("%d: %f -> %d, %f; idx: %d; (%d, %d, %d)\n", + // rowIn[x*4 + 0], scaled1D, index1D, shift1D, + // idx, nearest[0], nearest[1], nearest[2]); + } } }