fix stbir__linear_to_srgb_uchar:
1. table stored threshhold of transition from i to i+1, but wants to be i-1 to i 2. table was computed by dividing uchar by 256.0 instead of 255.0, causing it to be 100% wrong
This commit is contained in:
parent
30c7a981ec
commit
16d68d14f8
@ -450,38 +450,38 @@ static float stbir__srgb_uchar_to_linear_float[256] = {
|
||||
// sRGB transition values, scaled by 1<<28
|
||||
static int stbir__srgb_offset_to_linear_scaled[256] =
|
||||
{
|
||||
40579, 121738, 202897, 284056, 365216, 446375, 527534, 608693,
|
||||
689852, 771011, 852421, 938035, 1028466, 1123787, 1224073, 1329393,
|
||||
1439819, 1555418, 1676257, 1802402, 1933917, 2070867, 2213313, 2361317,
|
||||
2514938, 2674237, 2839271, 3010099, 3186776, 3369359, 3557903, 3752463,
|
||||
3953090, 4159840, 4372764, 4591913, 4817339, 5049091, 5287220, 5531775,
|
||||
5782804, 6040356, 6304477, 6575216, 6852618, 7136729, 7427596, 7725263,
|
||||
8029775, 8341176, 8659511, 8984821, 9317151, 9656544, 10003040, 10356683,
|
||||
10717513, 11085572, 11460901, 11843540, 12233529, 12630908, 13035717, 13447994,
|
||||
13867779, 14295110, 14730025, 15172563, 15622760, 16080655, 16546285, 17019686,
|
||||
17500894, 17989948, 18486882, 18991734, 19504536, 20025326, 20554138, 21091010,
|
||||
21635972, 22189062, 22750312, 23319758, 23897432, 24483368, 25077600, 25680162,
|
||||
26291086, 26910406, 27538152, 28174360, 28819058, 29472282, 30134062, 30804430,
|
||||
31483418, 32171058, 32867378, 33572412, 34286192, 35008744, 35740104, 36480296,
|
||||
37229356, 37987316, 38754196, 39530036, 40314860, 41108700, 41911584, 42723540,
|
||||
43544600, 44374792, 45214140, 46062680, 46920440, 47787444, 48663720, 49549300,
|
||||
50444212, 51348480, 52262136, 53185204, 54117712, 55059688, 56011160, 56972156,
|
||||
57942704, 58922824, 59912552, 60911908, 61920920, 62939616, 63968024, 65006168,
|
||||
66054072, 67111760, 68179272, 69256616, 70343832, 71440936, 72547952, 73664920,
|
||||
74791848, 75928776, 77075720, 78232704, 79399760, 80576904, 81764168, 82961576,
|
||||
84169152, 85386920, 86614904, 87853120, 89101608, 90360384, 91629480, 92908904,
|
||||
94198688, 95498864, 96809440, 98130456, 99461928, 100803872, 102156320, 103519296,
|
||||
104892824, 106276920, 107671616, 109076928, 110492880, 111919504, 113356808, 114804824,
|
||||
116263576, 117733080, 119213360, 120704448, 122206352, 123719104, 125242720, 126777232,
|
||||
128322648, 129879000, 131446312, 133024600, 134613888, 136214192, 137825552, 139447968,
|
||||
141081456, 142726080, 144381808, 146048704, 147726768, 149416016, 151116496, 152828192,
|
||||
154551168, 156285408, 158030944, 159787808, 161556000, 163335568, 165126512, 166928864,
|
||||
168742640, 170567856, 172404544, 174252704, 176112384, 177983568, 179866320, 181760640,
|
||||
183666528, 185584032, 187513168, 189453952, 191406400, 193370544, 195346384, 197333952,
|
||||
199333264, 201344352, 203367216, 205401904, 207448400, 209506752, 211576960, 213659056,
|
||||
215753056, 217858976, 219976832, 222106656, 224248464, 226402272, 228568096, 230745952,
|
||||
232935872, 235137872, 237351968, 239578176, 241816512, 244066992, 246329648, 248604512,
|
||||
250891568, 253190848, 255502368, 257826160, 260162240, 262510608, 264871312, 267244336,
|
||||
0, 40738, 122216, 203693, 285170, 366648, 448125, 529603,
|
||||
611080, 692557, 774035, 855852, 942009, 1033024, 1128971, 1229926,
|
||||
1335959, 1447142, 1563542, 1685229, 1812268, 1944725, 2082664, 2226148,
|
||||
2375238, 2529996, 2690481, 2856753, 3028870, 3206888, 3390865, 3580856,
|
||||
3776916, 3979100, 4187460, 4402049, 4622919, 4850123, 5083710, 5323731,
|
||||
5570236, 5823273, 6082892, 6349140, 6622065, 6901714, 7188133, 7481369,
|
||||
7781466, 8088471, 8402427, 8723380, 9051372, 9386448, 9728650, 10078021,
|
||||
10434603, 10798439, 11169569, 11548036, 11933879, 12327139, 12727857, 13136073,
|
||||
13551826, 13975156, 14406100, 14844697, 15290987, 15745007, 16206795, 16676389,
|
||||
17153826, 17639142, 18132374, 18633560, 19142734, 19659934, 20185196, 20718552,
|
||||
21260042, 21809696, 22367554, 22933648, 23508010, 24090680, 24681686, 25281066,
|
||||
25888850, 26505076, 27129772, 27762974, 28404716, 29055026, 29713942, 30381490,
|
||||
31057708, 31742624, 32436272, 33138682, 33849884, 34569912, 35298800, 36036568,
|
||||
36783260, 37538896, 38303512, 39077136, 39859796, 40651528, 41452360, 42262316,
|
||||
43081432, 43909732, 44747252, 45594016, 46450052, 47315392, 48190064, 49074096,
|
||||
49967516, 50870356, 51782636, 52704392, 53635648, 54576432, 55526772, 56486700,
|
||||
57456236, 58435408, 59424248, 60422780, 61431036, 62449032, 63476804, 64514376,
|
||||
65561776, 66619028, 67686160, 68763192, 69850160, 70947088, 72053992, 73170912,
|
||||
74297864, 75434880, 76581976, 77739184, 78906536, 80084040, 81271736, 82469648,
|
||||
83677792, 84896192, 86124888, 87363888, 88613232, 89872928, 91143016, 92423512,
|
||||
93714432, 95015816, 96327688, 97650056, 98982952, 100326408, 101680440, 103045072,
|
||||
104420320, 105806224, 107202800, 108610064, 110028048, 111456776, 112896264, 114346544,
|
||||
115807632, 117279552, 118762328, 120255976, 121760536, 123276016, 124802440, 126339832,
|
||||
127888216, 129447616, 131018048, 132599544, 134192112, 135795792, 137410592, 139036528,
|
||||
140673648, 142321952, 143981456, 145652208, 147334208, 149027488, 150732064, 152447968,
|
||||
154175200, 155913792, 157663776, 159425168, 161197984, 162982240, 164777968, 166585184,
|
||||
168403904, 170234160, 172075968, 173929344, 175794320, 177670896, 179559120, 181458992,
|
||||
183370528, 185293776, 187228736, 189175424, 191133888, 193104112, 195086128, 197079968,
|
||||
199085648, 201103184, 203132592, 205173888, 207227120, 209292272, 211369392, 213458480,
|
||||
215559568, 217672656, 219797792, 221934976, 224084240, 226245600, 228419056, 230604656,
|
||||
232802400, 235012320, 237234432, 239468736, 241715280, 243974080, 246245120, 248528464,
|
||||
250824112, 253132064, 255452368, 257785040, 260130080, 262487520, 264857376, 267239664,
|
||||
};
|
||||
|
||||
static float stbir__srgb_to_linear(float f)
|
||||
@ -506,23 +506,16 @@ static unsigned char stbir__linear_to_srgb_uchar(float f)
|
||||
int v = 0;
|
||||
int i;
|
||||
|
||||
// Everything below 128 is off by 1. This fixes that.
|
||||
int fix = 0;
|
||||
i = v + 128; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 64; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 32; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 16; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 8; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 4; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 2; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 1; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
|
||||
// Adding 1 to 0 with the fix gives incorrect results for input 0. This fixes that.
|
||||
if (x < 81000)
|
||||
return 0;
|
||||
|
||||
i = 128; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i; else fix = 1;
|
||||
i = v + 64; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 32; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 16; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 8; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 4; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 2; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
i = v + 1; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
|
||||
|
||||
return (unsigned char)v + fix;
|
||||
return (unsigned char) v;
|
||||
}
|
||||
|
||||
static float stbir__filter_trapezoid(float x, float scale)
|
||||
|
@ -810,20 +810,21 @@ void test_suite(int argc, char **argv)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0 // linear_to_srgb_uchar table
|
||||
for (i=0; i < 256; ++i) {
|
||||
float f = stbir__srgb_to_linear((i-0.5f)/255.0f);
|
||||
printf("%9d, ", (int) ((f) * (1<<28)));
|
||||
if ((i & 7) == 7)
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
for (i = 0; i < 256; i++) {
|
||||
float f = stbir__srgb_to_linear(float(i) / 255);
|
||||
int n = stbir__linear_to_srgb_uchar(f);
|
||||
STBIR_ASSERT(n == i);
|
||||
}
|
||||
|
||||
#if 0 // linear_to_srgb_uchar table
|
||||
for (i=0; i < 256; ++i) {
|
||||
float f = stbir__srgb_to_linear((i+0.5f)/256.0f);
|
||||
printf("%9d, ", (int) ((f) * (1<<28)));
|
||||
if ((i & 7) == 7)
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
test_filters();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user