| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 // This is necessary because the gAMA chunk actually stores 1/gamma. | 315 // This is necessary because the gAMA chunk actually stores 1/gamma. |
| 316 return 1.0f / png_fixed_point_to_float(x); | 316 return 1.0f / png_fixed_point_to_float(x); |
| 317 } | 317 } |
| 318 | 318 |
| 319 static constexpr float gSRGB_toXYZD50[] { | 319 static constexpr float gSRGB_toXYZD50[] { |
| 320 0.4358f, 0.3853f, 0.1430f, // Rx, Gx, Bx | 320 0.4358f, 0.3853f, 0.1430f, // Rx, Gx, Bx |
| 321 0.2224f, 0.7170f, 0.0606f, // Ry, Gy, Gz | 321 0.2224f, 0.7170f, 0.0606f, // Ry, Gy, Gz |
| 322 0.0139f, 0.0971f, 0.7139f, // Rz, Gz, Bz | 322 0.0139f, 0.0971f, 0.7139f, // Rz, Gz, Bz |
| 323 }; | 323 }; |
| 324 | 324 |
| 325 static bool convert_to_D50(SkMatrix44* toXYZD50, float toXYZ[9], float whitePoin
t[2]) { | |
| 326 float wX = whitePoint[0]; | |
| 327 float wY = whitePoint[1]; | |
| 328 if (wX < 0.0f || wY < 0.0f || (wX + wY > 1.0f)) { | |
| 329 return false; | |
| 330 } | |
| 331 | |
| 332 // Calculate the XYZ illuminant. Call this the src illuminant. | |
| 333 float wZ = 1.0f - wX - wY; | |
| 334 float scale = 1.0f / wY; | |
| 335 // TODO (msarett): | |
| 336 // What are common src illuminants? I'm guessing we will almost always see
D65. Should | |
| 337 // we go ahead and save a precomputed D65->D50 Bradford matrix? Should we e
xit early if | |
| 338 // if the src illuminant is D50? | |
| 339 SkVector3 srcXYZ = SkVector3::Make(wX * scale, 1.0f, wZ * scale); | |
| 340 | |
| 341 // The D50 illuminant. | |
| 342 SkVector3 dstXYZ = SkVector3::Make(0.96422f, 1.0f, 0.82521f); | |
| 343 | |
| 344 // Calculate the chromatic adaptation matrix. We will use the Bradford meth
od, thus | |
| 345 // the matrices below. The Bradford method is used by Adobe and is widely c
onsidered | |
| 346 // to be the best. | |
| 347 // http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html | |
| 348 SkMatrix mA, mAInv; | |
| 349 mA.setAll(0.8951f, 0.2664f, -0.1614f, -0.7502f, 1.7135f, 0.0367f, 0.0389f, -
0.0685f, 1.0296f); | |
| 350 mAInv.setAll(0.9869929f, -0.1470543f, 0.1599627f, 0.4323053f, 0.5183603f, 0.
0492912f, | |
| 351 -0.0085287f, 0.0400428f, 0.9684867f); | |
| 352 | |
| 353 // Map illuminant into cone response domain. | |
| 354 SkVector3 srcCone; | |
| 355 srcCone.fX = mA[0] * srcXYZ.fX + mA[1] * srcXYZ.fY + mA[2] * srcXYZ.fZ; | |
| 356 srcCone.fY = mA[3] * srcXYZ.fX + mA[4] * srcXYZ.fY + mA[5] * srcXYZ.fZ; | |
| 357 srcCone.fZ = mA[6] * srcXYZ.fX + mA[7] * srcXYZ.fY + mA[8] * srcXYZ.fZ; | |
| 358 SkVector3 dstCone; | |
| 359 dstCone.fX = mA[0] * dstXYZ.fX + mA[1] * dstXYZ.fY + mA[2] * dstXYZ.fZ; | |
| 360 dstCone.fY = mA[3] * dstXYZ.fX + mA[4] * dstXYZ.fY + mA[5] * dstXYZ.fZ; | |
| 361 dstCone.fZ = mA[6] * dstXYZ.fX + mA[7] * dstXYZ.fY + mA[8] * dstXYZ.fZ; | |
| 362 | |
| 363 SkMatrix DXToD50; | |
| 364 DXToD50.setIdentity(); | |
| 365 DXToD50[0] = dstCone.fX / srcCone.fX; | |
| 366 DXToD50[4] = dstCone.fY / srcCone.fY; | |
| 367 DXToD50[8] = dstCone.fZ / srcCone.fZ; | |
| 368 DXToD50.postConcat(mAInv); | |
| 369 DXToD50.preConcat(mA); | |
| 370 | |
| 371 SkMatrix toXYZ3x3; | |
| 372 toXYZ3x3.setAll(toXYZ[0], toXYZ[3], toXYZ[6], toXYZ[1], toXYZ[4], toXYZ[7],
toXYZ[2], toXYZ[5], | |
| 373 toXYZ[8]); | |
| 374 toXYZ3x3.postConcat(DXToD50); | |
| 375 | |
| 376 toXYZD50->set3x3(toXYZ3x3[0], toXYZ3x3[3], toXYZ3x3[6], | |
| 377 toXYZ3x3[1], toXYZ3x3[4], toXYZ3x3[7], | |
| 378 toXYZ3x3[2], toXYZ3x3[5], toXYZ3x3[8]); | |
| 379 return true; | |
| 380 } | |
| 381 | |
| 382 #endif // LIBPNG >= 1.6 | 325 #endif // LIBPNG >= 1.6 |
| 383 | 326 |
| 384 // Returns a colorSpace object that represents any color space information in | 327 // Returns a colorSpace object that represents any color space information in |
| 385 // the encoded data. If the encoded data contains no color space, this will | 328 // the encoded data. If the encoded data contains no color space, this will |
| 386 // return NULL. | 329 // return NULL. |
| 387 sk_sp<SkColorSpace> read_color_space(png_structp png_ptr, png_infop info_ptr) { | 330 sk_sp<SkColorSpace> read_color_space(png_structp png_ptr, png_infop info_ptr) { |
| 388 | 331 |
| 389 #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_M
INOR >= 6) | 332 #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_M
INOR >= 6) |
| 390 | 333 |
| 391 // First check for an ICC profile | 334 // First check for an ICC profile |
| (...skipping 17 matching lines...) Expand all Loading... |
| 409 | 352 |
| 410 // sRGB chunks also store a rendering intent: Absolute, Relative, | 353 // sRGB chunks also store a rendering intent: Absolute, Relative, |
| 411 // Perceptual, and Saturation. | 354 // Perceptual, and Saturation. |
| 412 // FIXME (msarett): Extract this information from the sRGB chunk once | 355 // FIXME (msarett): Extract this information from the sRGB chunk once |
| 413 // we are able to handle this information in | 356 // we are able to handle this information in |
| 414 // SkColorSpace. | 357 // SkColorSpace. |
| 415 return SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); | 358 return SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
| 416 } | 359 } |
| 417 | 360 |
| 418 // Next, check for chromaticities. | 361 // Next, check for chromaticities. |
| 419 png_fixed_point toXYZFixed[9]; | 362 png_fixed_point chrm[8]; |
| 420 float toXYZ[9]; | |
| 421 png_fixed_point whitePointFixed[2]; | |
| 422 float whitePoint[2]; | |
| 423 png_fixed_point gamma; | 363 png_fixed_point gamma; |
| 424 float gammas[3]; | 364 float gammas[3]; |
| 425 if (png_get_cHRM_XYZ_fixed(png_ptr, info_ptr, &toXYZFixed[0], &toXYZFixed[1]
, &toXYZFixed[2], | 365 if (png_get_cHRM_fixed(png_ptr, info_ptr, &chrm[0], &chrm[1], &chrm[2], &chr
m[3], &chrm[4], |
| 426 &toXYZFixed[3], &toXYZFixed[4], &toXYZFixed[5], &
toXYZFixed[6], | 366 &chrm[5], &chrm[6], &chrm[7])) |
| 427 &toXYZFixed[7], &toXYZFixed[8]) && | |
| 428 png_get_cHRM_fixed(png_ptr, info_ptr, &whitePointFixed[0], &whitePointFi
xed[1], nullptr, | |
| 429 nullptr, nullptr, nullptr, nullptr, nullptr)) | |
| 430 { | 367 { |
| 431 for (int i = 0; i < 9; i++) { | 368 SkColorSpacePrimaries primaries; |
| 432 toXYZ[i] = png_fixed_point_to_float(toXYZFixed[i]); | 369 primaries.fRX = png_fixed_point_to_float(chrm[2]); |
| 433 } | 370 primaries.fRY = png_fixed_point_to_float(chrm[3]); |
| 434 whitePoint[0] = png_fixed_point_to_float(whitePointFixed[0]); | 371 primaries.fGX = png_fixed_point_to_float(chrm[4]); |
| 435 whitePoint[1] = png_fixed_point_to_float(whitePointFixed[1]); | 372 primaries.fGY = png_fixed_point_to_float(chrm[5]); |
| 373 primaries.fBX = png_fixed_point_to_float(chrm[6]); |
| 374 primaries.fBY = png_fixed_point_to_float(chrm[7]); |
| 375 primaries.fWX = png_fixed_point_to_float(chrm[0]); |
| 376 primaries.fWY = png_fixed_point_to_float(chrm[1]); |
| 436 | 377 |
| 437 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor); | 378 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor); |
| 438 if (!convert_to_D50(&toXYZD50, toXYZ, whitePoint)) { | 379 if (!primaries.toXYZD50(&toXYZD50)) { |
| 439 toXYZD50.set3x3RowMajorf(gSRGB_toXYZD50); | 380 toXYZD50.set3x3RowMajorf(gSRGB_toXYZD50); |
| 440 } | 381 } |
| 441 | 382 |
| 442 if (PNG_INFO_gAMA == png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) { | 383 if (PNG_INFO_gAMA == png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) { |
| 443 float value = png_inverted_fixed_point_to_float(gamma); | 384 float value = png_inverted_fixed_point_to_float(gamma); |
| 444 gammas[0] = value; | 385 gammas[0] = value; |
| 445 gammas[1] = value; | 386 gammas[1] = value; |
| 446 gammas[2] = value; | 387 gammas[2] = value; |
| 447 | 388 |
| 448 return SkColorSpace_Base::NewRGB(gammas, toXYZD50); | 389 return SkColorSpace_Base::NewRGB(gammas, toXYZD50); |
| (...skipping 856 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1305 SkCodec* outCodec = nullptr; | 1246 SkCodec* outCodec = nullptr; |
| 1306 if (read_header(streamDeleter.get(), chunkReader, &outCodec, nullptr, nullpt
r)) { | 1247 if (read_header(streamDeleter.get(), chunkReader, &outCodec, nullptr, nullpt
r)) { |
| 1307 // Codec has taken ownership of the stream. | 1248 // Codec has taken ownership of the stream. |
| 1308 SkASSERT(outCodec); | 1249 SkASSERT(outCodec); |
| 1309 streamDeleter.release(); | 1250 streamDeleter.release(); |
| 1310 return outCodec; | 1251 return outCodec; |
| 1311 } | 1252 } |
| 1312 | 1253 |
| 1313 return nullptr; | 1254 return nullptr; |
| 1314 } | 1255 } |
| OLD | NEW |