gfx/skia/trunk/src/core/SkDistanceFieldGen.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rwxr-xr-x

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2014 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkDistanceFieldGen.h"
michael@0 9 #include "SkPoint.h"
michael@0 10
michael@0 11 struct DFData {
michael@0 12 float fAlpha; // alpha value of source texel
michael@0 13 float fDistSq; // distance squared to nearest (so far) edge texel
michael@0 14 SkPoint fDistVector; // distance vector to nearest (so far) edge texel
michael@0 15 };
michael@0 16
michael@0 17 enum NeighborFlags {
michael@0 18 kLeft_NeighborFlag = 0x01,
michael@0 19 kRight_NeighborFlag = 0x02,
michael@0 20 kTopLeft_NeighborFlag = 0x04,
michael@0 21 kTop_NeighborFlag = 0x08,
michael@0 22 kTopRight_NeighborFlag = 0x10,
michael@0 23 kBottomLeft_NeighborFlag = 0x20,
michael@0 24 kBottom_NeighborFlag = 0x40,
michael@0 25 kBottomRight_NeighborFlag = 0x80,
michael@0 26 kAll_NeighborFlags = 0xff,
michael@0 27
michael@0 28 kNeighborFlagCount = 8
michael@0 29 };
michael@0 30
michael@0 31 // We treat an "edge" as a place where we cross from black to non-black, or vice versa.
michael@0 32 // 'neighborFlags' is used to limit the directions in which we test to avoid indexing
michael@0 33 // outside of the image
michael@0 34 static bool found_edge(const unsigned char* imagePtr, int width, int neighborFlags) {
michael@0 35 // the order of these should match the neighbor flags above
michael@0 36 const int kNum8ConnectedNeighbors = 8;
michael@0 37 const int offsets[8] = {-1, 1, -width-1, -width, -width+1, width-1, width, width+1 };
michael@0 38 SkASSERT(kNum8ConnectedNeighbors == kNeighborFlagCount);
michael@0 39
michael@0 40 // search for an edge
michael@0 41 bool currVal = (*imagePtr != 0);
michael@0 42 for (int i = 0; i < kNum8ConnectedNeighbors; ++i) {
michael@0 43 bool checkVal;
michael@0 44 if ((1 << i) & neighborFlags) {
michael@0 45 const unsigned char* checkPtr = imagePtr + offsets[i];
michael@0 46 checkVal = (*checkPtr != 0);
michael@0 47 } else {
michael@0 48 checkVal = false;
michael@0 49 }
michael@0 50 SkASSERT(checkVal == 0 || checkVal == 1);
michael@0 51 SkASSERT(currVal == 0 || currVal == 1);
michael@0 52 if (checkVal != currVal) {
michael@0 53 return true;
michael@0 54 }
michael@0 55 }
michael@0 56
michael@0 57 return false;
michael@0 58 }
michael@0 59
michael@0 60 static void init_glyph_data(DFData* data, unsigned char* edges, const unsigned char* image,
michael@0 61 int dataWidth, int dataHeight,
michael@0 62 int imageWidth, int imageHeight,
michael@0 63 int pad) {
michael@0 64 data += pad*dataWidth;
michael@0 65 data += pad;
michael@0 66 edges += (pad*dataWidth + pad);
michael@0 67
michael@0 68 for (int j = 0; j < imageHeight; ++j) {
michael@0 69 for (int i = 0; i < imageWidth; ++i) {
michael@0 70 if (255 == *image) {
michael@0 71 data->fAlpha = 1.0f;
michael@0 72 } else {
michael@0 73 data->fAlpha = (*image)*0.00392156862f; // 1/255
michael@0 74 }
michael@0 75 int checkMask = kAll_NeighborFlags;
michael@0 76 if (i == 0) {
michael@0 77 checkMask &= ~(kLeft_NeighborFlag|kTopLeft_NeighborFlag|kBottomLeft_NeighborFlag);
michael@0 78 }
michael@0 79 if (i == imageWidth-1) {
michael@0 80 checkMask &= ~(kRight_NeighborFlag|kTopRight_NeighborFlag|kBottomRight_NeighborFlag);
michael@0 81 }
michael@0 82 if (j == 0) {
michael@0 83 checkMask &= ~(kTopLeft_NeighborFlag|kTop_NeighborFlag|kTopRight_NeighborFlag);
michael@0 84 }
michael@0 85 if (j == imageHeight-1) {
michael@0 86 checkMask &= ~(kBottomLeft_NeighborFlag|kBottom_NeighborFlag|kBottomRight_NeighborFlag);
michael@0 87 }
michael@0 88 if (found_edge(image, imageWidth, checkMask)) {
michael@0 89 *edges = 255; // using 255 makes for convenient debug rendering
michael@0 90 }
michael@0 91 ++data;
michael@0 92 ++image;
michael@0 93 ++edges;
michael@0 94 }
michael@0 95 data += 2*pad;
michael@0 96 edges += 2*pad;
michael@0 97 }
michael@0 98 }
michael@0 99
michael@0 100 // from Gustavson (2011)
michael@0 101 // computes the distance to an edge given an edge normal vector and a pixel's alpha value
michael@0 102 // assumes that direction has been pre-normalized
michael@0 103 static float edge_distance(const SkPoint& direction, float alpha) {
michael@0 104 float dx = direction.fX;
michael@0 105 float dy = direction.fY;
michael@0 106 float distance;
michael@0 107 if (SkScalarNearlyZero(dx) || SkScalarNearlyZero(dy)) {
michael@0 108 distance = 0.5f - alpha;
michael@0 109 } else {
michael@0 110 // this is easier if we treat the direction as being in the first octant
michael@0 111 // (other octants are symmetrical)
michael@0 112 dx = SkScalarAbs(dx);
michael@0 113 dy = SkScalarAbs(dy);
michael@0 114 if (dx < dy) {
michael@0 115 SkTSwap(dx, dy);
michael@0 116 }
michael@0 117
michael@0 118 // a1 = 0.5*dy/dx is the smaller fractional area chopped off by the edge
michael@0 119 // to avoid the divide, we just consider the numerator
michael@0 120 float a1num = 0.5f*dy;
michael@0 121
michael@0 122 // we now compute the approximate distance, depending where the alpha falls
michael@0 123 // relative to the edge fractional area
michael@0 124
michael@0 125 // if 0 <= alpha < a1
michael@0 126 if (alpha*dx < a1num) {
michael@0 127 // TODO: find a way to do this without square roots?
michael@0 128 distance = 0.5f*(dx + dy) - SkScalarSqrt(2.0f*dx*dy*alpha);
michael@0 129 // if a1 <= alpha <= 1 - a1
michael@0 130 } else if (alpha*dx < (dx - a1num)) {
michael@0 131 distance = (0.5f - alpha)*dx;
michael@0 132 // if 1 - a1 < alpha <= 1
michael@0 133 } else {
michael@0 134 // TODO: find a way to do this without square roots?
michael@0 135 distance = -0.5f*(dx + dy) + SkScalarSqrt(2.0f*dx*dy*(1.0f - alpha));
michael@0 136 }
michael@0 137 }
michael@0 138
michael@0 139 return distance;
michael@0 140 }
michael@0 141
michael@0 142 static void init_distances(DFData* data, unsigned char* edges, int width, int height) {
michael@0 143 // skip one pixel border
michael@0 144 DFData* currData = data;
michael@0 145 DFData* prevData = data - width;
michael@0 146 DFData* nextData = data + width;
michael@0 147
michael@0 148 for (int j = 0; j < height; ++j) {
michael@0 149 for (int i = 0; i < width; ++i) {
michael@0 150 if (*edges) {
michael@0 151 // we should not be in the one-pixel outside band
michael@0 152 SkASSERT(i > 0 && i < width-1 && j > 0 && j < height-1);
michael@0 153 // gradient will point from low to high
michael@0 154 // +y is down in this case
michael@0 155 // i.e., if you're outside, gradient points towards edge
michael@0 156 // if you're inside, gradient points away from edge
michael@0 157 SkPoint currGrad;
michael@0 158 currGrad.fX = (prevData+1)->fAlpha - (prevData-1)->fAlpha
michael@0 159 + SK_ScalarSqrt2*(currData+1)->fAlpha
michael@0 160 - SK_ScalarSqrt2*(currData-1)->fAlpha
michael@0 161 + (nextData+1)->fAlpha - (nextData-1)->fAlpha;
michael@0 162 currGrad.fY = (nextData-1)->fAlpha - (prevData-1)->fAlpha
michael@0 163 + SK_ScalarSqrt2*nextData->fAlpha
michael@0 164 - SK_ScalarSqrt2*prevData->fAlpha
michael@0 165 + (nextData+1)->fAlpha - (prevData+1)->fAlpha;
michael@0 166 currGrad.setLengthFast(1.0f);
michael@0 167
michael@0 168 // init squared distance to edge and distance vector
michael@0 169 float dist = edge_distance(currGrad, currData->fAlpha);
michael@0 170 currGrad.scale(dist, &currData->fDistVector);
michael@0 171 currData->fDistSq = dist*dist;
michael@0 172 } else {
michael@0 173 // init distance to "far away"
michael@0 174 currData->fDistSq = 2000000.f;
michael@0 175 currData->fDistVector.fX = 1000.f;
michael@0 176 currData->fDistVector.fY = 1000.f;
michael@0 177 }
michael@0 178 ++currData;
michael@0 179 ++prevData;
michael@0 180 ++nextData;
michael@0 181 ++edges;
michael@0 182 }
michael@0 183 }
michael@0 184 }
michael@0 185
michael@0 186 // Danielsson's 8SSEDT
michael@0 187
michael@0 188 // first stage forward pass
michael@0 189 // (forward in Y, forward in X)
michael@0 190 static void F1(DFData* curr, int width) {
michael@0 191 // upper left
michael@0 192 DFData* check = curr - width-1;
michael@0 193 SkPoint distVec = check->fDistVector;
michael@0 194 float distSq = check->fDistSq - 2.0f*(distVec.fX + distVec.fY - 1.0f);
michael@0 195 if (distSq < curr->fDistSq) {
michael@0 196 distVec.fX -= 1.0f;
michael@0 197 distVec.fY -= 1.0f;
michael@0 198 curr->fDistSq = distSq;
michael@0 199 curr->fDistVector = distVec;
michael@0 200 }
michael@0 201
michael@0 202 // up
michael@0 203 check = curr - width;
michael@0 204 distVec = check->fDistVector;
michael@0 205 distSq = check->fDistSq - 2.0f*distVec.fY + 1.0f;
michael@0 206 if (distSq < curr->fDistSq) {
michael@0 207 distVec.fY -= 1.0f;
michael@0 208 curr->fDistSq = distSq;
michael@0 209 curr->fDistVector = distVec;
michael@0 210 }
michael@0 211
michael@0 212 // upper right
michael@0 213 check = curr - width+1;
michael@0 214 distVec = check->fDistVector;
michael@0 215 distSq = check->fDistSq + 2.0f*(distVec.fX - distVec.fY + 1.0f);
michael@0 216 if (distSq < curr->fDistSq) {
michael@0 217 distVec.fX += 1.0f;
michael@0 218 distVec.fY -= 1.0f;
michael@0 219 curr->fDistSq = distSq;
michael@0 220 curr->fDistVector = distVec;
michael@0 221 }
michael@0 222
michael@0 223 // left
michael@0 224 check = curr - 1;
michael@0 225 distVec = check->fDistVector;
michael@0 226 distSq = check->fDistSq - 2.0f*distVec.fX + 1.0f;
michael@0 227 if (distSq < curr->fDistSq) {
michael@0 228 distVec.fX -= 1.0f;
michael@0 229 curr->fDistSq = distSq;
michael@0 230 curr->fDistVector = distVec;
michael@0 231 }
michael@0 232 }
michael@0 233
michael@0 234 // second stage forward pass
michael@0 235 // (forward in Y, backward in X)
michael@0 236 static void F2(DFData* curr, int width) {
michael@0 237 // right
michael@0 238 DFData* check = curr + 1;
michael@0 239 float distSq = check->fDistSq;
michael@0 240 SkPoint distVec = check->fDistVector;
michael@0 241 distSq = check->fDistSq + 2.0f*distVec.fX + 1.0f;
michael@0 242 if (distSq < curr->fDistSq) {
michael@0 243 distVec.fX += 1.0f;
michael@0 244 curr->fDistSq = distSq;
michael@0 245 curr->fDistVector = distVec;
michael@0 246 }
michael@0 247 }
michael@0 248
michael@0 249 // first stage backward pass
michael@0 250 // (backward in Y, forward in X)
michael@0 251 static void B1(DFData* curr, int width) {
michael@0 252 // left
michael@0 253 DFData* check = curr - 1;
michael@0 254 SkPoint distVec = check->fDistVector;
michael@0 255 float distSq = check->fDistSq - 2.0f*distVec.fX + 1.0f;
michael@0 256 if (distSq < curr->fDistSq) {
michael@0 257 distVec.fX -= 1.0f;
michael@0 258 curr->fDistSq = distSq;
michael@0 259 curr->fDistVector = distVec;
michael@0 260 }
michael@0 261 }
michael@0 262
michael@0 263 // second stage backward pass
michael@0 264 // (backward in Y, backwards in X)
michael@0 265 static void B2(DFData* curr, int width) {
michael@0 266 // right
michael@0 267 DFData* check = curr + 1;
michael@0 268 SkPoint distVec = check->fDistVector;
michael@0 269 float distSq = check->fDistSq + 2.0f*distVec.fX + 1.0f;
michael@0 270 if (distSq < curr->fDistSq) {
michael@0 271 distVec.fX += 1.0f;
michael@0 272 curr->fDistSq = distSq;
michael@0 273 curr->fDistVector = distVec;
michael@0 274 }
michael@0 275
michael@0 276 // bottom left
michael@0 277 check = curr + width-1;
michael@0 278 distVec = check->fDistVector;
michael@0 279 distSq = check->fDistSq - 2.0f*(distVec.fX - distVec.fY - 1.0f);
michael@0 280 if (distSq < curr->fDistSq) {
michael@0 281 distVec.fX -= 1.0f;
michael@0 282 distVec.fY += 1.0f;
michael@0 283 curr->fDistSq = distSq;
michael@0 284 curr->fDistVector = distVec;
michael@0 285 }
michael@0 286
michael@0 287 // bottom
michael@0 288 check = curr + width;
michael@0 289 distVec = check->fDistVector;
michael@0 290 distSq = check->fDistSq + 2.0f*distVec.fY + 1.0f;
michael@0 291 if (distSq < curr->fDistSq) {
michael@0 292 distVec.fY += 1.0f;
michael@0 293 curr->fDistSq = distSq;
michael@0 294 curr->fDistVector = distVec;
michael@0 295 }
michael@0 296
michael@0 297 // bottom right
michael@0 298 check = curr + width+1;
michael@0 299 distVec = check->fDistVector;
michael@0 300 distSq = check->fDistSq + 2.0f*(distVec.fX + distVec.fY + 1.0f);
michael@0 301 if (distSq < curr->fDistSq) {
michael@0 302 distVec.fX += 1.0f;
michael@0 303 distVec.fY += 1.0f;
michael@0 304 curr->fDistSq = distSq;
michael@0 305 curr->fDistVector = distVec;
michael@0 306 }
michael@0 307 }
michael@0 308
michael@0 309 // enable this to output edge data rather than the distance field
michael@0 310 #define DUMP_EDGE 0
michael@0 311
michael@0 312 #if !DUMP_EDGE
michael@0 313 static unsigned char pack_distance_field_val(float dist, float distanceMagnitude) {
michael@0 314 if (dist <= -distanceMagnitude) {
michael@0 315 return 255;
michael@0 316 } else if (dist > distanceMagnitude) {
michael@0 317 return 0;
michael@0 318 } else {
michael@0 319 return (unsigned char)((distanceMagnitude-dist)*128.0f/distanceMagnitude);
michael@0 320 }
michael@0 321 }
michael@0 322 #endif
michael@0 323
michael@0 324 // assumes an 8-bit image and distance field
michael@0 325 bool SkGenerateDistanceFieldFromImage(unsigned char* distanceField,
michael@0 326 const unsigned char* image,
michael@0 327 int width, int height,
michael@0 328 int distanceMagnitude) {
michael@0 329 SkASSERT(NULL != distanceField);
michael@0 330 SkASSERT(NULL != image);
michael@0 331
michael@0 332 // the final distance field will have additional texels on each side to handle
michael@0 333 // the maximum distance
michael@0 334 // we expand our temp data by one more on each side to simplify
michael@0 335 // the scanning code -- will always be treated as infinitely far away
michael@0 336 int pad = distanceMagnitude+1;
michael@0 337
michael@0 338 // set params for distance field data
michael@0 339 int dataWidth = width + 2*pad;
michael@0 340 int dataHeight = height + 2*pad;
michael@0 341
michael@0 342 // create temp data
michael@0 343 size_t dataSize = dataWidth*dataHeight*sizeof(DFData);
michael@0 344 SkAutoSMalloc<1024> dfStorage(dataSize);
michael@0 345 DFData* dataPtr = (DFData*) dfStorage.get();
michael@0 346 sk_bzero(dataPtr, dataSize);
michael@0 347
michael@0 348 SkAutoSMalloc<1024> edgeStorage(dataWidth*dataHeight*sizeof(char));
michael@0 349 unsigned char* edgePtr = (unsigned char*) edgeStorage.get();
michael@0 350 sk_bzero(edgePtr, dataWidth*dataHeight*sizeof(char));
michael@0 351
michael@0 352 // copy glyph into distance field storage
michael@0 353 init_glyph_data(dataPtr, edgePtr, image,
michael@0 354 dataWidth, dataHeight,
michael@0 355 width, height, pad);
michael@0 356
michael@0 357 // create initial distance data, particularly at edges
michael@0 358 init_distances(dataPtr, edgePtr, dataWidth, dataHeight);
michael@0 359
michael@0 360 // now perform Euclidean distance transform to propagate distances
michael@0 361
michael@0 362 // forwards in y
michael@0 363 DFData* currData = dataPtr+dataWidth+1; // skip outer buffer
michael@0 364 unsigned char* currEdge = edgePtr+dataWidth+1;
michael@0 365 for (int j = 1; j < dataHeight-1; ++j) {
michael@0 366 // forwards in x
michael@0 367 for (int i = 1; i < dataWidth-1; ++i) {
michael@0 368 // don't need to calculate distance for edge pixels
michael@0 369 if (!*currEdge) {
michael@0 370 F1(currData, dataWidth);
michael@0 371 }
michael@0 372 ++currData;
michael@0 373 ++currEdge;
michael@0 374 }
michael@0 375
michael@0 376 // backwards in x
michael@0 377 --currData; // reset to end
michael@0 378 --currEdge;
michael@0 379 for (int i = 1; i < dataWidth-1; ++i) {
michael@0 380 // don't need to calculate distance for edge pixels
michael@0 381 if (!*currEdge) {
michael@0 382 F2(currData, dataWidth);
michael@0 383 }
michael@0 384 --currData;
michael@0 385 --currEdge;
michael@0 386 }
michael@0 387
michael@0 388 currData += dataWidth+1;
michael@0 389 currEdge += dataWidth+1;
michael@0 390 }
michael@0 391
michael@0 392 // backwards in y
michael@0 393 currData = dataPtr+dataWidth*(dataHeight-2) - 1; // skip outer buffer
michael@0 394 currEdge = edgePtr+dataWidth*(dataHeight-2) - 1;
michael@0 395 for (int j = 1; j < dataHeight-1; ++j) {
michael@0 396 // forwards in x
michael@0 397 for (int i = 1; i < dataWidth-1; ++i) {
michael@0 398 // don't need to calculate distance for edge pixels
michael@0 399 if (!*currEdge) {
michael@0 400 B1(currData, dataWidth);
michael@0 401 }
michael@0 402 ++currData;
michael@0 403 ++currEdge;
michael@0 404 }
michael@0 405
michael@0 406 // backwards in x
michael@0 407 --currData; // reset to end
michael@0 408 --currEdge;
michael@0 409 for (int i = 1; i < dataWidth-1; ++i) {
michael@0 410 // don't need to calculate distance for edge pixels
michael@0 411 if (!*currEdge) {
michael@0 412 B2(currData, dataWidth);
michael@0 413 }
michael@0 414 --currData;
michael@0 415 --currEdge;
michael@0 416 }
michael@0 417
michael@0 418 currData -= dataWidth-1;
michael@0 419 currEdge -= dataWidth-1;
michael@0 420 }
michael@0 421
michael@0 422 // copy results to final distance field data
michael@0 423 currData = dataPtr + dataWidth+1;
michael@0 424 currEdge = edgePtr + dataWidth+1;
michael@0 425 unsigned char *dfPtr = distanceField;
michael@0 426 for (int j = 1; j < dataHeight-1; ++j) {
michael@0 427 for (int i = 1; i < dataWidth-1; ++i) {
michael@0 428 #if DUMP_EDGE
michael@0 429 unsigned char val = sk_float_round2int(255*currData->fAlpha);
michael@0 430 if (*currEdge) {
michael@0 431 val = 128;
michael@0 432 }
michael@0 433 *dfPtr++ = val;
michael@0 434 #else
michael@0 435 float dist;
michael@0 436 if (currData->fAlpha > 0.5f) {
michael@0 437 dist = -SkScalarSqrt(currData->fDistSq);
michael@0 438 } else {
michael@0 439 dist = SkScalarSqrt(currData->fDistSq);
michael@0 440 }
michael@0 441 *dfPtr++ = pack_distance_field_val(dist, (float)distanceMagnitude);
michael@0 442 #endif
michael@0 443 ++currData;
michael@0 444 ++currEdge;
michael@0 445 }
michael@0 446 currData += 2;
michael@0 447 currEdge += 2;
michael@0 448 }
michael@0 449
michael@0 450 return true;
michael@0 451 }

mercurial