static void initBadShiftTable(UTHashTable *jumpTable, MMBitmapRef needle) { const MMPoint lastPoint = MMPointMake(needle->width - 1, needle->height - 1); const size_t maxColors = needle->width * needle->height; MMPoint scan; printf("lastpoint=%d,%d, maxColors=%d\n", lastPoint.x, lastPoint.y, maxColors); /* Allocate max size initially to avoid a million calls to malloc(). */ initHashTable(jumpTable, maxColors, sizeof(struct shiftNode)); printf("init table finished!\n"); int i = 0; /* Populate jumpTable with analysis of |needle|. */ for (scan.y = lastPoint.y; ; --scan.y) { for (scan.x = lastPoint.x; ; --scan.x) { MMRGBHex color = MMRGBHexAtPoint(needle, scan.x, scan.y); if (!tableHasKey(jumpTable, color)) { i++; addNodeToTable(jumpTable, color, MMPointMake(needle->width - scan.x, needle->height - scan.y)); } if (scan.x == 0) break; /* Avoid infinite loop from unsigned type. */ } if (scan.y == 0) break; } printf("i = %d\n", i); //printf("init func finished!\n"); }
static int needleAtOffset(MMBitmapRef needle, MMBitmapRef haystack, MMPoint offset, float tolerance) { const MMPoint lastPoint = MMPointMake(needle->width - 1, needle->height - 1); MMPoint scan; /* Note that |needle| is searched backwards, in accordance with the * Boyer-Moore search algorithm. */ for (scan.y = lastPoint.y; ; --scan.y) { for (scan.x = lastPoint.x; ; --scan.x) { MMRGBHex ncolor = MMRGBHexAtPoint(needle, scan.x, scan.y); MMRGBHex hcolor = MMRGBHexAtPoint(haystack, offset.x + scan.x, offset.y + scan.y); if (!MMRGBHexSimilarToColor(ncolor, hcolor, tolerance)) return 0; if (scan.x == 0) break; /* Avoid infinite loop from unsigned type. */ } if (scan.y == 0) break; } return 1; }
static PyObject *Bitmap_get_color(BitmapObject *self, PyObject *args) { MMPoint point; if (!PyArg_ParseTuple(args, "kk", &point.x, &point.y) || !Bitmap_Ready(self)) return NULL; if (!MMBitmapPointInBounds(self->bitmap, point)) { PyErr_SetString(PyExc_ValueError, "Point out of bounds"); return NULL; } return Py_BuildValue("I", MMRGBHexAtPoint(self->bitmap, point.x, point.y)); }