blob: a65fcc416d10666d4ac444695436e17d3a79419a [file] [log] [blame]
chudy@google.come606d6e2012-07-12 14:31:25 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "SkHitBox.h"
11
12SkHitBox::SkHitBox() {
13 fHitBox = NULL;
14 fX = -1;
15 fY = -1;
16 fLayer = -1;
17}
18
19SkHitBox::~SkHitBox() {}
20
21void SkHitBox::alloc(int width, int height) {
22 free(fHitBox);
23 int length = width * height;
24 fHitBox = (int*) malloc(length * sizeof(int));
25 for (int i = 0; i < length; i++) {
26 fHitBox[i] = 0;
27 }
28}
29
30void SkHitBox::updateHitBox(SkBitmap* newBitmap, int layer) {
31 int length = fPrev.width() * fPrev.height();
32 int* prevBase = (int*)fPrev.getPixels();
33 int* currBase = (int*)newBitmap->getPixels();
34
35 for (int i = 0; i < length; i++) {
36 if (SkUnPreMultiply::PMColorToColor(prevBase[i]) !=
37 SkUnPreMultiply::PMColorToColor(currBase[i])) {
38 fHitBox[i] = layer;
39 }
40 }
41 if (fPrev.empty()) {
42 alloc(newBitmap->width(), newBitmap->height());
43 fPrev.setConfig(SkBitmap::kARGB_8888_Config, newBitmap->width(), newBitmap->height());
44 fPrev.allocPixels();
45 }
46 newBitmap->deepCopyTo(&fPrev, SkBitmap::kARGB_8888_Config);
47}
48
49void SkHitBox::updateHitPoint(SkBitmap* newBitmap, int layer) {
50 int* prevBase = (int*)fPrev.getPixels();
51 int* currBase = (int*)newBitmap->getPixels();
52 int pixel = fY * fPrev.width() + fX;
53
54 if (pointIsSet() && !fPrev.empty()) {
55 if (SkUnPreMultiply::PMColorToColor(prevBase[pixel]) !=
56 SkUnPreMultiply::PMColorToColor(currBase[pixel])) {
57 fLayer = layer;
58 }
59 }
60 if (fPrev.empty()) {
61 alloc(newBitmap->width(), newBitmap->height());
62 fPrev.setConfig(SkBitmap::kARGB_8888_Config, newBitmap->width(), newBitmap->height());
63 fPrev.allocPixels();
64 }
65 newBitmap->deepCopyTo(&fPrev, SkBitmap::kARGB_8888_Config);
66}