| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "SkDistanceFieldGen.h" | 8 #include "SkDistanceFieldGen.h" |
| 9 #include "SkPoint.h" | 9 #include "SkPoint.h" |
| 10 | 10 |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 distVec.fY += 1.0f; | 309 distVec.fY += 1.0f; |
| 310 curr->fDistSq = distSq; | 310 curr->fDistSq = distSq; |
| 311 curr->fDistVector = distVec; | 311 curr->fDistVector = distVec; |
| 312 } | 312 } |
| 313 } | 313 } |
| 314 | 314 |
| 315 // enable this to output edge data rather than the distance field | 315 // enable this to output edge data rather than the distance field |
| 316 #define DUMP_EDGE 0 | 316 #define DUMP_EDGE 0 |
| 317 | 317 |
| 318 #if !DUMP_EDGE | 318 #if !DUMP_EDGE |
| 319 static unsigned char pack_distance_field_val(float dist, float distanceMagnitude
) { | 319 template <int distanceMagnitude> |
| 320 if (dist <= -distanceMagnitude) { | 320 static unsigned char pack_distance_field_val(float dist) { |
| 321 return 255; | 321 // The distance field is constructed as unsigned char values, so that the ze
ro value is at 128, |
| 322 } else if (dist > distanceMagnitude) { | 322 // Beside 128, we have 128 values in range [0, 128), but only 127 values in
range (128, 255]. |
| 323 return 0; | 323 // So we multiply distanceMagnitude by 127/128 at the latter range to avoid
overflow. |
| 324 } else { | 324 dist = SkScalarPin(-dist, -distanceMagnitude, distanceMagnitude * 127.0f / 1
28.0f); |
| 325 return (unsigned char)((distanceMagnitude-dist)*128.0f/distanceMagnitude
); | 325 |
| 326 } | 326 // Scale into the positive range for unsigned distance. |
| 327 dist += distanceMagnitude; |
| 328 |
| 329 // Scale into unsigned char range. |
| 330 // Round to place negative and positive values as equally as possible around
128 |
| 331 // (which represents zero). |
| 332 return (unsigned char)SkScalarRoundToInt(dist / (2 * distanceMagnitude) * 25
6.0f); |
| 327 } | 333 } |
| 328 #endif | 334 #endif |
| 329 | 335 |
| 330 // assumes a padded 8-bit image and distance field | 336 // assumes a padded 8-bit image and distance field |
| 331 // width and height are the original width and height of the image | 337 // width and height are the original width and height of the image |
| 332 static bool generate_distance_field_from_image(unsigned char* distanceField, | 338 static bool generate_distance_field_from_image(unsigned char* distanceField, |
| 333 const unsigned char* copyPtr, | 339 const unsigned char* copyPtr, |
| 334 int width, int height) { | 340 int width, int height) { |
| 335 SkASSERT(distanceField); | 341 SkASSERT(distanceField); |
| 336 SkASSERT(copyPtr); | 342 SkASSERT(copyPtr); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 float result = alpha + (1.0f-alpha)*edge; | 440 float result = alpha + (1.0f-alpha)*edge; |
| 435 unsigned char val = sk_float_round2int(255*result); | 441 unsigned char val = sk_float_round2int(255*result); |
| 436 *dfPtr++ = val; | 442 *dfPtr++ = val; |
| 437 #else | 443 #else |
| 438 float dist; | 444 float dist; |
| 439 if (currData->fAlpha > 0.5f) { | 445 if (currData->fAlpha > 0.5f) { |
| 440 dist = -SkScalarSqrt(currData->fDistSq); | 446 dist = -SkScalarSqrt(currData->fDistSq); |
| 441 } else { | 447 } else { |
| 442 dist = SkScalarSqrt(currData->fDistSq); | 448 dist = SkScalarSqrt(currData->fDistSq); |
| 443 } | 449 } |
| 444 *dfPtr++ = pack_distance_field_val(dist, (float)SK_DistanceFieldMagn
itude); | 450 *dfPtr++ = pack_distance_field_val<SK_DistanceFieldMagnitude>(dist); |
| 445 #endif | 451 #endif |
| 446 ++currData; | 452 ++currData; |
| 447 ++currEdge; | 453 ++currEdge; |
| 448 } | 454 } |
| 449 currData += 2; | 455 currData += 2; |
| 450 currEdge += 2; | 456 currEdge += 2; |
| 451 } | 457 } |
| 452 | 458 |
| 453 return true; | 459 return true; |
| 454 } | 460 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 *currDestPtr++ = (mask & (1 << i)) ? 0xff : 0; | 513 *currDestPtr++ = (mask & (1 << i)) ? 0xff : 0; |
| 508 } | 514 } |
| 509 } | 515 } |
| 510 currSrcScanLine += rowBytes; | 516 currSrcScanLine += rowBytes; |
| 511 *currDestPtr++ = 0; | 517 *currDestPtr++ = 0; |
| 512 } | 518 } |
| 513 sk_bzero(currDestPtr, (width+2)*sizeof(char)); | 519 sk_bzero(currDestPtr, (width+2)*sizeof(char)); |
| 514 | 520 |
| 515 return generate_distance_field_from_image(distanceField, copyPtr, width, hei
ght); | 521 return generate_distance_field_from_image(distanceField, copyPtr, width, hei
ght); |
| 516 } | 522 } |
| OLD | NEW |