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 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 return (unsigned char)((distanceMagnitude-dist)*128.0f/distanceMagnitude
); | 325 return (unsigned char)((distanceMagnitude-dist)*128.0f/distanceMagnitude
); |
326 } | 326 } |
327 } | 327 } |
328 #endif | 328 #endif |
329 | 329 |
330 // assumes a padded 8-bit image and distance field | 330 // assumes a padded 8-bit image and distance field |
331 // width and height are the original width and height of the image | 331 // width and height are the original width and height of the image |
332 static bool generate_distance_field_from_image(unsigned char* distanceField, | 332 static bool generate_distance_field_from_image(unsigned char* distanceField, |
333 const unsigned char* copyPtr, | 333 const unsigned char* copyPtr, |
334 int width, int height) { | 334 int width, int height) { |
335 SkASSERT(NULL != distanceField); | 335 SkASSERT(distanceField); |
336 SkASSERT(NULL != copyPtr); | 336 SkASSERT(copyPtr); |
337 | 337 |
338 // we expand our temp data by one more on each side to simplify | 338 // we expand our temp data by one more on each side to simplify |
339 // the scanning code -- will always be treated as infinitely far away | 339 // the scanning code -- will always be treated as infinitely far away |
340 int pad = SK_DistanceFieldPad + 1; | 340 int pad = SK_DistanceFieldPad + 1; |
341 | 341 |
342 // set params for distance field data | 342 // set params for distance field data |
343 int dataWidth = width + 2*pad; | 343 int dataWidth = width + 2*pad; |
344 int dataHeight = height + 2*pad; | 344 int dataHeight = height + 2*pad; |
345 | 345 |
346 // create temp data | 346 // create temp data |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 currEdge += 2; | 455 currEdge += 2; |
456 } | 456 } |
457 | 457 |
458 return true; | 458 return true; |
459 } | 459 } |
460 | 460 |
461 // assumes an 8-bit image and distance field | 461 // assumes an 8-bit image and distance field |
462 bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField, | 462 bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField, |
463 const unsigned char* image, | 463 const unsigned char* image, |
464 int width, int height, int rowBytes) { | 464 int width, int height, int rowBytes) { |
465 SkASSERT(NULL != distanceField); | 465 SkASSERT(distanceField); |
466 SkASSERT(NULL != image); | 466 SkASSERT(image); |
467 | 467 |
468 // create temp data | 468 // create temp data |
469 SkAutoSMalloc<1024> copyStorage((width+2)*(height+2)*sizeof(char)); | 469 SkAutoSMalloc<1024> copyStorage((width+2)*(height+2)*sizeof(char)); |
470 unsigned char* copyPtr = (unsigned char*) copyStorage.get(); | 470 unsigned char* copyPtr = (unsigned char*) copyStorage.get(); |
471 | 471 |
472 // we copy our source image into a padded copy to ensure we catch edge trans
itions | 472 // we copy our source image into a padded copy to ensure we catch edge trans
itions |
473 // around the outside | 473 // around the outside |
474 const unsigned char* currSrcScanLine = image; | 474 const unsigned char* currSrcScanLine = image; |
475 sk_bzero(copyPtr, (width+2)*sizeof(char)); | 475 sk_bzero(copyPtr, (width+2)*sizeof(char)); |
476 unsigned char* currDestPtr = copyPtr + width + 2; | 476 unsigned char* currDestPtr = copyPtr + width + 2; |
477 for (int i = 0; i < height; ++i) { | 477 for (int i = 0; i < height; ++i) { |
478 *currDestPtr++ = 0; | 478 *currDestPtr++ = 0; |
479 memcpy(currDestPtr, currSrcScanLine, rowBytes); | 479 memcpy(currDestPtr, currSrcScanLine, rowBytes); |
480 currSrcScanLine += rowBytes; | 480 currSrcScanLine += rowBytes; |
481 currDestPtr += width; | 481 currDestPtr += width; |
482 *currDestPtr++ = 0; | 482 *currDestPtr++ = 0; |
483 } | 483 } |
484 sk_bzero(currDestPtr, (width+2)*sizeof(char)); | 484 sk_bzero(currDestPtr, (width+2)*sizeof(char)); |
485 | 485 |
486 return generate_distance_field_from_image(distanceField, copyPtr, width, hei
ght); | 486 return generate_distance_field_from_image(distanceField, copyPtr, width, hei
ght); |
487 } | 487 } |
488 | 488 |
489 // assumes a 1-bit image and 8-bit distance field | 489 // assumes a 1-bit image and 8-bit distance field |
490 bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField, | 490 bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField, |
491 const unsigned char* image, | 491 const unsigned char* image, |
492 int width, int height, int rowBytes) { | 492 int width, int height, int rowBytes) { |
493 SkASSERT(NULL != distanceField); | 493 SkASSERT(distanceField); |
494 SkASSERT(NULL != image); | 494 SkASSERT(image); |
495 | 495 |
496 // create temp data | 496 // create temp data |
497 SkAutoSMalloc<1024> copyStorage((width+2)*(height+2)*sizeof(char)); | 497 SkAutoSMalloc<1024> copyStorage((width+2)*(height+2)*sizeof(char)); |
498 unsigned char* copyPtr = (unsigned char*) copyStorage.get(); | 498 unsigned char* copyPtr = (unsigned char*) copyStorage.get(); |
499 | 499 |
500 // we copy our source image into a padded copy to ensure we catch edge trans
itions | 500 // we copy our source image into a padded copy to ensure we catch edge trans
itions |
501 // around the outside | 501 // around the outside |
502 const unsigned char* currSrcScanLine = image; | 502 const unsigned char* currSrcScanLine = image; |
503 sk_bzero(copyPtr, (width+2)*sizeof(char)); | 503 sk_bzero(copyPtr, (width+2)*sizeof(char)); |
504 unsigned char* currDestPtr = copyPtr + width + 2; | 504 unsigned char* currDestPtr = copyPtr + width + 2; |
505 for (int i = 0; i < height; ++i) { | 505 for (int i = 0; i < height; ++i) { |
506 *currDestPtr++ = 0; | 506 *currDestPtr++ = 0; |
507 int rowWritesLeft = width; | 507 int rowWritesLeft = width; |
508 const unsigned char *maskPtr = currSrcScanLine; | 508 const unsigned char *maskPtr = currSrcScanLine; |
509 while (rowWritesLeft > 0) { | 509 while (rowWritesLeft > 0) { |
510 unsigned mask = *maskPtr++; | 510 unsigned mask = *maskPtr++; |
511 for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) { | 511 for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) { |
512 *currDestPtr++ = (mask & (1 << i)) ? 0xff : 0; | 512 *currDestPtr++ = (mask & (1 << i)) ? 0xff : 0; |
513 } | 513 } |
514 } | 514 } |
515 currSrcScanLine += rowBytes; | 515 currSrcScanLine += rowBytes; |
516 *currDestPtr++ = 0; | 516 *currDestPtr++ = 0; |
517 } | 517 } |
518 sk_bzero(currDestPtr, (width+2)*sizeof(char)); | 518 sk_bzero(currDestPtr, (width+2)*sizeof(char)); |
519 | 519 |
520 return generate_distance_field_from_image(distanceField, copyPtr, width, hei
ght); | 520 return generate_distance_field_from_image(distanceField, copyPtr, width, hei
ght); |
521 } | 521 } |
OLD | NEW |