| 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 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 double y0, y1, x0, x1; | 306 double y0, y1, x0, x1; |
| 307 double a, b, f; | 307 double a, b, f; |
| 308 | 308 |
| 309 // July/27 2001 - Expanded to handle degenerated curves with an arbitrar
y | 309 // July/27 2001 - Expanded to handle degenerated curves with an arbitrar
y |
| 310 // number of elements containing 0 at the begining of the table (Zeroes) | 310 // number of elements containing 0 at the begining of the table (Zeroes) |
| 311 // and another arbitrary number of poles (FFFFh) at the end. | 311 // and another arbitrary number of poles (FFFFh) at the end. |
| 312 // First the zero and pole extents are computed, then value is compared. | 312 // First the zero and pole extents are computed, then value is compared. |
| 313 | 313 |
| 314 NumZeroes = 0; | 314 NumZeroes = 0; |
| 315 while (LutTable[NumZeroes] == 0 && NumZeroes < length-1) | 315 while (LutTable[NumZeroes] == 0 && NumZeroes < length-1) |
| 316 NumZeroes++; | 316 NumZeroes++; |
| 317 | 317 |
| 318 // There are no zeros at the beginning and we are trying to find a zero,
so | 318 // There are no zeros at the beginning and we are trying to find a zero,
so |
| 319 // return anything. It seems zero would be the less destructive choice | 319 // return anything. It seems zero would be the less destructive choice |
| 320 /* I'm not sure that this makes sense, but oh well... */ | 320 /* I'm not sure that this makes sense, but oh well... */ |
| 321 if (NumZeroes == 0 && Value == 0) | 321 if (NumZeroes == 0 && Value == 0) |
| 322 return 0; | 322 return 0; |
| 323 | 323 |
| 324 NumPoles = 0; | 324 NumPoles = 0; |
| 325 while (LutTable[length-1- NumPoles] == 0xFFFF && NumPoles < length-1) | 325 while (LutTable[length-1- NumPoles] == 0xFFFF && NumPoles < length-1) |
| 326 NumPoles++; | 326 NumPoles++; |
| 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; | 331 int a, b; |
| 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 | 336 |
| 337 // else restrict to valid zone | 337 // else restrict to valid zone |
| 338 | 338 |
| 339 a = ((NumZeroes-1) * 0xFFFF) / (length-1); | 339 a = ((NumZeroes-1) * 0xFFFF) / (length-1); |
| 340 b = ((length-1 - NumPoles) * 0xFFFF) / (length-1); | 340 b = ((length-1 - NumPoles) * 0xFFFF) / (length-1); |
| 341 | 341 |
| 342 l = a - 1; | 342 l = a - 1; |
| 343 r = b + 1; | 343 r = b + 1; |
| 344 } | 344 } |
| 345 | 345 |
| 346 | 346 |
| 347 // Seems not a degenerated case... apply binary search | 347 // Seems not a degenerated case... apply binary search |
| 348 | 348 |
| 349 while (r > l) { | 349 while (r > l) { |
| 350 | 350 |
| 351 x = (l + r) / 2; | 351 x = (l + r) / 2; |
| 352 | 352 |
| 353 » » res = (int) lut_interp_linear16((uint16_fract_t) (x-1), LutTable
, length); | 353 res = (int) lut_interp_linear16((uint16_fract_t) (x-1), LutTable
, length); |
| 354 | 354 |
| 355 if (res == Value) { | 355 if (res == Value) { |
| 356 | 356 |
| 357 // Found exact match. | 357 // Found exact match. |
| 358 | 358 |
| 359 return (uint16_fract_t) (x - 1); | 359 return (uint16_fract_t) (x - 1); |
| 360 } | 360 } |
| 361 | 361 |
| 362 if (res > Value) r = x - 1; | 362 if (res > Value) r = x - 1; |
| 363 else l = x + 1; | 363 else l = x + 1; |
| 364 } | 364 } |
| 365 | 365 |
| 366 // Not found, should we interpolate? | 366 // Not found, should we interpolate? |
| 367 | 367 |
| 368 | 368 |
| 369 // Get surrounding nodes | 369 // Get surrounding nodes |
| 370 | 370 |
| 371 val2 = (length-1) * ((double) (x - 1) / 65535.0); | 371 val2 = (length-1) * ((double) (x - 1) / 65535.0); |
| 372 | 372 |
| 373 cell0 = (int) floor(val2); | 373 cell0 = (int) floor(val2); |
| 374 cell1 = (int) ceil(val2); | 374 cell1 = (int) ceil(val2); |
| 375 | 375 |
| 376 if (cell0 == cell1) return (uint16_fract_t) x; | 376 if (cell0 == cell1) return (uint16_fract_t) x; |
| 377 | 377 |
| 378 y0 = LutTable[cell0] ; | 378 y0 = LutTable[cell0] ; |
| 379 x0 = (65535.0 * cell0) / (length-1); | 379 x0 = (65535.0 * cell0) / (length-1); |
| 380 | 380 |
| 381 y1 = LutTable[cell1] ; | 381 y1 = LutTable[cell1] ; |
| 382 x1 = (65535.0 * cell1) / (length-1); | 382 x1 = (65535.0 * cell1) / (length-1); |
| 383 | 383 |
| 384 a = (y1 - y0) / (x1 - x0); | 384 a = (y1 - y0) / (x1 - x0); |
| 385 b = y0 - a * x0; | 385 b = y0 - a * x0; |
| 386 | 386 |
| 387 if (fabs(a) < 0.01) return (uint16_fract_t) x; | 387 if (fabs(a) < 0.01) return (uint16_fract_t) x; |
| 388 | 388 |
| 389 f = ((Value - b) / a); | 389 f = ((Value - b) / a); |
| 390 | 390 |
| 391 if (f < 0.0) return (uint16_fract_t) 0; | 391 if (f < 0.0) return (uint16_fract_t) 0; |
| 392 if (f >= 65535.0) return (uint16_fract_t) 0xFFFF; | 392 if (f >= 65535.0) return (uint16_fract_t) 0xFFFF; |
| 393 | 393 |
| 394 return (uint16_fract_t) floor(f + 0.5); | 394 return (uint16_fract_t) floor(f + 0.5); |
| 395 | |
| 396 } | 395 } |
| 397 | 396 |
| 398 /* | 397 /* |
| 399 The number of entries needed to invert a lookup table should not | 398 The number of entries needed to invert a lookup table should not |
| 400 necessarily be the same as the original number of entries. This is | 399 necessarily be the same as the original number of entries. This is |
| 401 especially true of lookup tables that have a small number of entries. | 400 especially true of lookup tables that have a small number of entries. |
| 402 | 401 |
| 403 For example: | 402 For example: |
| 404 Using a table like: | 403 Using a table like: |
| 405 {0, 3104, 14263, 34802, 65535} | 404 {0, 3104, 14263, 34802, 65535} |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 // measurement or data, however it is what lcms uses
. | 567 // measurement or data, however it is what lcms uses
. |
| 569 *output_gamma_lut_length = trc->count; | 568 *output_gamma_lut_length = trc->count; |
| 570 if (*output_gamma_lut_length < 256) | 569 if (*output_gamma_lut_length < 256) |
| 571 *output_gamma_lut_length = 256; | 570 *output_gamma_lut_length = 256; |
| 572 | 571 |
| 573 *output_gamma_lut = invert_lut(trc->data, trc->count, *o
utput_gamma_lut_length); | 572 *output_gamma_lut = invert_lut(trc->data, trc->count, *o
utput_gamma_lut_length); |
| 574 } | 573 } |
| 575 } | 574 } |
| 576 | 575 |
| 577 } | 576 } |
| OLD | NEW |