// We treat an "edge" as a place where we cross from >=128 to <128, or vice versa, or // where we have two non-zero pixels that are <128. // 'neighborFlags' is used to limit the directions in which we test to avoid indexing // outside of the image staticbool found_edge(constunsignedchar* imagePtr, int width, int neighborFlags) { // the order of these should match the neighbor flags above constint kNum8ConnectedNeighbors = 8; constint offsets[8] = {-1, 1, -width-1, -width, -width+1, width-1, width, width+1 };
SkASSERT(kNum8ConnectedNeighbors == kNeighborFlagCount);
// search for an edge unsignedchar currVal = *imagePtr; unsignedchar currCheck = (currVal >> 7); for (int i = 0; i < kNum8ConnectedNeighbors; ++i) { unsignedchar neighborVal; if ((1 << i) & neighborFlags) { constunsignedchar* checkPtr = imagePtr + offsets[i];
neighborVal = *checkPtr;
} else {
neighborVal = 0;
} unsignedchar neighborCheck = (neighborVal >> 7);
SkASSERT(currCheck == 0 || currCheck == 1);
SkASSERT(neighborCheck == 0 || neighborCheck == 1); // if sharp transition if (currCheck != neighborCheck || // or both <128 and >0
(!currCheck && !neighborCheck && currVal && neighborVal)) { returntrue;
}
}
returnfalse;
}
staticvoid init_glyph_data(DFData* data, unsignedchar* edges, constunsignedchar* image, int dataWidth, int dataHeight, int imageWidth, int imageHeight, int pad) {
data += pad*dataWidth;
data += pad;
edges += (pad*dataWidth + pad);
for (int j = 0; j < imageHeight; ++j) { for (int i = 0; i < imageWidth; ++i) { if (255 == *image) {
data->fAlpha = 1.0f;
} else {
data->fAlpha = (*image)*0.00392156862f; // 1/255
} int checkMask = kAll_NeighborFlags; if (i == 0) {
checkMask &= ~(kLeft_NeighborFlag|kTopLeft_NeighborFlag|kBottomLeft_NeighborFlag);
} if (i == imageWidth-1) {
checkMask &= ~(kRight_NeighborFlag|kTopRight_NeighborFlag|kBottomRight_NeighborFlag);
} if (j == 0) {
checkMask &= ~(kTopLeft_NeighborFlag|kTop_NeighborFlag|kTopRight_NeighborFlag);
} if (j == imageHeight-1) {
checkMask &= ~(kBottomLeft_NeighborFlag|kBottom_NeighborFlag|kBottomRight_NeighborFlag);
} if (found_edge(image, imageWidth, checkMask)) {
*edges = 255; // using 255 makes for convenient debug rendering
}
++data;
++image;
++edges;
}
data += 2*pad;
edges += 2*pad;
}
}
// from Gustavson (2011) // computes the distance to an edge given an edge normal vector and a pixel's alpha value // assumes that direction has been pre-normalized staticfloat edge_distance(const SkPoint& direction, float alpha) { float dx = direction.fX; float dy = direction.fY; float distance; if (SkScalarNearlyZero(dx) || SkScalarNearlyZero(dy)) {
distance = 0.5f - alpha;
} else { // this is easier if we treat the direction as being in the first octant // (other octants are symmetrical)
dx = SkScalarAbs(dx);
dy = SkScalarAbs(dy); if (dx < dy) { using std::swap;
swap(dx, dy);
}
// a1 = 0.5*dy/dx is the smaller fractional area chopped off by the edge // to avoid the divide, we just consider the numerator float a1num = 0.5f*dy;
// we now compute the approximate distance, depending where the alpha falls // relative to the edge fractional area
// if 0 <= alpha < a1 if (alpha*dx < a1num) { // TODO: find a way to do this without square roots?
distance = 0.5f*(dx + dy) - SkScalarSqrt(2.0f*dx*dy*alpha); // if a1 <= alpha <= 1 - a1
} elseif (alpha*dx < (dx - a1num)) {
distance = (0.5f - alpha)*dx; // if 1 - a1 < alpha <= 1
} else { // TODO: find a way to do this without square roots?
distance = -0.5f*(dx + dy) + SkScalarSqrt(2.0f*dx*dy*(1.0f - alpha));
}
}
return distance;
}
staticvoid init_distances(DFData* data, unsignedchar* edges, int width, int height) { // skip one pixel border
DFData* currData = data;
DFData* prevData = data - width;
DFData* nextData = data + width;
for (int j = 0; j < height; ++j) { for (int i = 0; i < width; ++i) { if (*edges) { // we should not be in the one-pixel outside band
SkASSERT(i > 0 && i < width-1 && j > 0 && j < height-1); // gradient will point from low to high // +y is down in this case // i.e., if you're outside, gradient points towards edge // if you're inside, gradient points away from edge
SkPoint currGrad;
currGrad.fX = (prevData+1)->fAlpha - (prevData-1)->fAlpha
+ SK_ScalarSqrt2*(currData+1)->fAlpha
- SK_ScalarSqrt2*(currData-1)->fAlpha
+ (nextData+1)->fAlpha - (nextData-1)->fAlpha;
currGrad.fY = (nextData-1)->fAlpha - (prevData-1)->fAlpha
+ SK_ScalarSqrt2*nextData->fAlpha
- SK_ScalarSqrt2*prevData->fAlpha
+ (nextData+1)->fAlpha - (prevData+1)->fAlpha;
SkPointPriv::SetLengthFast(&currGrad, 1.0f);
// enable this to output edge data rather than the distance field #define DUMP_EDGE 0
#if !DUMP_EDGE template <int distanceMagnitude> staticunsignedchar pack_distance_field_val(float dist) { // The distance field is constructed as unsigned char values, so that the zero value is at 128, // Beside 128, we have 128 values in range [0, 128), but only 127 values in range (128, 255]. // So we multiply distanceMagnitude by 127/128 at the latter range to avoid overflow.
dist = SkTPin<float>(-dist, -distanceMagnitude, distanceMagnitude * 127.0f / 128.0f);
// Scale into the positive range for unsigned distance.
dist += distanceMagnitude;
// Scale into unsigned char range. // Round to place negative and positive values as equally as possible around 128 // (which represents zero). return (unsignedchar)SkScalarRoundToInt(dist / (2 * distanceMagnitude) * 256.0f);
} #endif
// assumes a padded 8-bit image and distance field // width and height are the original width and height of the image staticbool generate_distance_field_from_image(unsignedchar* distanceField, constunsignedchar* copyPtr, int width, int height) {
SkASSERT(distanceField);
SkASSERT(copyPtr);
// we expand our temp data by one more on each side to simplify // the scanning code -- will always be treated as infinitely far away int pad = SK_DistanceFieldPad + 1;
// set params for distance field data int dataWidth = width + 2*pad; int dataHeight = height + 2*pad;
// now perform Euclidean distance transform to propagate distances
// forwards in y
DFData* currData = dataPtr+dataWidth+1; // skip outer buffer unsignedchar* currEdge = edgePtr+dataWidth+1; for (int j = 1; j < dataHeight-1; ++j) { // forwards in x for (int i = 1; i < dataWidth-1; ++i) { // don't need to calculate distance for edge pixels if (!*currEdge) {
F1(currData, dataWidth);
}
++currData;
++currEdge;
}
// backwards in x
--currData; // reset to end
--currEdge; for (int i = 1; i < dataWidth-1; ++i) { // don't need to calculate distance for edge pixels if (!*currEdge) {
F2(currData, dataWidth);
}
--currData;
--currEdge;
}
// backwards in y
currData = dataPtr+dataWidth*(dataHeight-2) - 1; // skip outer buffer
currEdge = edgePtr+dataWidth*(dataHeight-2) - 1; for (int j = 1; j < dataHeight-1; ++j) { // forwards in x for (int i = 1; i < dataWidth-1; ++i) { // don't need to calculate distance for edge pixels if (!*currEdge) {
B1(currData, dataWidth);
}
++currData;
++currEdge;
}
// backwards in x
--currData; // reset to end
--currEdge; for (int i = 1; i < dataWidth-1; ++i) { // don't need to calculate distance for edge pixels if (!*currEdge) {
B2(currData, dataWidth);
}
--currData;
--currEdge;
}
// copy results to final distance field data
currData = dataPtr + dataWidth+1;
currEdge = edgePtr + dataWidth+1; unsignedchar *dfPtr = distanceField; for (int j = 1; j < dataHeight-1; ++j) { for (int i = 1; i < dataWidth-1; ++i) { #if DUMP_EDGE float alpha = currData->fAlpha; float edge = 0.0f; if (*currEdge) {
edge = 0.25f;
} // blend with original image float result = alpha + (1.0f-alpha)*edge; unsignedchar val = sk_float_round2int(255*result);
*dfPtr++ = val; #else float dist; if (currData->fAlpha > 0.5f) {
dist = -SkScalarSqrt(currData->fDistSq);
} else {
dist = SkScalarSqrt(currData->fDistSq);
}
*dfPtr++ = pack_distance_field_val<SK_DistanceFieldMagnitude>(dist); #endif
++currData;
++currEdge;
}
currData += 2;
currEdge += 2;
}
returntrue;
}
// assumes an 8-bit image and distance field bool SkGenerateDistanceFieldFromA8Image(unsignedchar* distanceField, constunsignedchar* image, int width, int height, size_t rowBytes) {
SkASSERT(distanceField);
SkASSERT(image);
// assumes a 16-bit lcd mask and 8-bit distance field bool SkGenerateDistanceFieldFromLCD16Mask(unsignedchar* distanceField, constunsignedchar* image, int w, int h, size_t rowBytes) {
SkASSERT(distanceField);
SkASSERT(image);
// assumes a 1-bit image and 8-bit distance field bool SkGenerateDistanceFieldFromBWImage(unsignedchar* distanceField, constunsignedchar* image, int width, int height, size_t rowBytes) {
SkASSERT(distanceField);
SkASSERT(image);
// we copy our source image into a padded copy to ensure we catch edge transitions // around the outside constunsignedchar* currSrcScanLine = image;
sk_bzero(copyPtr, (width+2)*sizeof(char)); unsignedchar* currDestPtr = copyPtr + width + 2; for (int i = 0; i < height; ++i) {
*currDestPtr++ = 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.