OLD | NEW |
1 // qcms | 1 // qcms |
2 // Copyright (C) 2009 Mozilla Foundation | 2 // Copyright (C) 2009 Mozilla Foundation |
3 // | 3 // |
4 // Permission is hereby granted, free of charge, to any person obtaining | 4 // Permission is hereby granted, free of charge, to any person obtaining |
5 // a copy of this software and associated documentation files (the "Software"), | 5 // a copy of this software and associated documentation files (the "Software"), |
6 // to deal in the Software without restriction, including without limitation | 6 // to deal in the Software without restriction, including without limitation |
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, | 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
8 // and/or sell copies of the Software, and to permit persons to whom the Softwar
e | 8 // and/or sell copies of the Software, and to permit persons to whom the Softwar
e |
9 // is furnished to do so, subject to the following conditions: | 9 // is furnished to do so, subject to the following conditions: |
10 // | 10 // |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 | 327 |
328 // Does the curve belong to this case? | 328 // Does the curve belong to this case? |
329 if (NumZeroes > 1 || NumPoles > 1) | 329 if (NumZeroes > 1 || NumPoles > 1) |
330 { | 330 { |
331 int a, b, sample; | 331 int a, b, sample; |
332 | 332 |
333 // Identify if value fall downto 0 or FFFF zone | 333 // Identify if value fall downto 0 or FFFF zone |
334 if (Value == 0) return 0; | 334 if (Value == 0) return 0; |
335 // if (Value == 0xFFFF) return 0xFFFF; | 335 // if (Value == 0xFFFF) return 0xFFFF; |
336 sample = (length-1) * ((double) Value * (1./65535.)); | 336 sample = (length-1) * ((double) Value * (1./65535.)); |
337 if (LutTable[sample] == 0) | |
338 return 0; | |
339 if (LutTable[sample] == 0xffff) | 337 if (LutTable[sample] == 0xffff) |
340 return 0xffff; | 338 return 0xffff; |
341 | 339 |
342 // else restrict to valid zone | 340 // else restrict to valid zone |
343 | 341 |
344 a = ((NumZeroes-1) * 0xFFFF) / (length-1); | 342 a = ((NumZeroes-1) * 0xFFFF) / (length-1); |
345 b = ((length-1 - NumPoles) * 0xFFFF) / (length-1); | 343 b = ((length-1 - NumPoles) * 0xFFFF) / (length-1); |
346 | 344 |
347 l = a - 1; | 345 l = a - 1; |
348 r = b + 1; | 346 r = b + 1; |
349 | 347 |
350 // Ensure a valid binary search range | 348 // Ensure a valid binary search range |
351 | 349 |
352 if (l < 1) | 350 if (l < 1) |
353 l = 1; | 351 l = 1; |
354 if (r > 0x10000) | 352 if (r > 0x10000) |
355 r = 0x10000; | 353 r = 0x10000; |
| 354 |
| 355 // If the search range is inverted due to degeneracy, |
| 356 // deem LutTable non-invertible in this search range. |
| 357 // Refer to https://bugzil.la/1132467 |
| 358 |
| 359 if (r <= l) |
| 360 return 0; |
356 } | 361 } |
357 | 362 |
358 // Seems not a degenerated case... apply binary search | 363 // Seems not a degenerated case... apply binary search |
359 | 364 |
360 while (r > l) { | 365 while (r > l) { |
361 | 366 |
362 x = (l + r) / 2; | 367 x = (l + r) / 2; |
363 | 368 |
364 res = (int) lut_interp_linear16((uint16_fract_t) (x-1), LutTable
, length); | 369 res = (int) lut_interp_linear16((uint16_fract_t) (x-1), LutTable
, length); |
365 | 370 |
366 if (res == Value) { | 371 if (res == Value) { |
367 | 372 |
368 // Found exact match. | 373 // Found exact match. |
369 | 374 |
370 return (uint16_fract_t) (x - 1); | 375 return (uint16_fract_t) (x - 1); |
371 } | 376 } |
372 | 377 |
373 if (res > Value) r = x - 1; | 378 if (res > Value) r = x - 1; |
374 else l = x + 1; | 379 else l = x + 1; |
375 } | 380 } |
376 | 381 |
377 // Not found, should we interpolate? | 382 // Not found, should we interpolate? |
378 | 383 |
| 384 // Get surrounding nodes |
379 | 385 |
380 // Get surrounding nodes | 386 assert(x >= 1); |
381 | 387 |
382 val2 = (length-1) * ((double) (x - 1) / 65535.0); | 388 val2 = (length-1) * ((double) (x - 1) / 65535.0); |
383 | 389 |
384 cell0 = (int) floor(val2); | 390 cell0 = (int) floor(val2); |
385 cell1 = (int) ceil(val2); | 391 cell1 = (int) ceil(val2); |
386 | 392 |
| 393 assert(cell0 >= 0); |
| 394 assert(cell1 >= 0); |
| 395 assert(cell0 < length); |
| 396 assert(cell1 < length); |
| 397 |
387 if (cell0 == cell1) return (uint16_fract_t) x; | 398 if (cell0 == cell1) return (uint16_fract_t) x; |
388 | 399 |
389 y0 = LutTable[cell0] ; | 400 y0 = LutTable[cell0] ; |
390 x0 = (65535.0 * cell0) / (length-1); | 401 x0 = (65535.0 * cell0) / (length-1); |
391 | 402 |
392 y1 = LutTable[cell1] ; | 403 y1 = LutTable[cell1] ; |
393 x1 = (65535.0 * cell1) / (length-1); | 404 x1 = (65535.0 * cell1) / (length-1); |
394 | 405 |
395 a = (y1 - y0) / (x1 - x0); | 406 a = (y1 - y0) / (x1 - x0); |
396 b = y0 - a * x0; | 407 b = y0 - a * x0; |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 14, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, | 659 14, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, |
649 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 24, | 660 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 24, |
650 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, | 661 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, |
651 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, | 662 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, |
652 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, | 663 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, |
653 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, | 664 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, |
654 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, | 665 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, |
655 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, | 666 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, |
656 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 13 | 667 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 13 |
657 }; | 668 }; |
OLD | NEW |