def GetPixelColor(self, x, y): pixels = self.pixels base = self._bpp * (y * self._width + x) if self._bpp == 4: return rgba_color.RgbaColor(pixels[base + 0], pixels[base + 1], pixels[base + 2], pixels[base + 3]) return rgba_color.RgbaColor(pixels[base + 0], pixels[base + 1], pixels[base + 2])
def testGetBoundingBox(self): pixels = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] bmp = image_util.FromRGBPixels(4, 3, pixels) box, count = image_util.GetBoundingBox(bmp, rgba_color.RgbaColor(1, 0, 0)) self.assertEquals(box, (1, 1, 2, 1)) self.assertEquals(count, 2) box, count = image_util.GetBoundingBox(bmp, rgba_color.RgbaColor(0, 1, 0)) self.assertEquals(box, None) self.assertEquals(count, 0)
def _CheckScreenshot(self): canvasRGB = rgba_color.RgbaColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255) tab = self.tab tab.EvaluateJavaScript( "window.draw({{ red }}, {{ green }}, {{ blue }});", red=canvasRGB.r, green=canvasRGB.g, blue=canvasRGB.b) screenshot = tab.Screenshot(10) # Avoid checking along antialiased boundary due to limited Adreno 3xx # interpolation precision (crbug.com/847984). We inset by one CSS pixel # adjusted by the device pixel ratio. inset = int( math.ceil(tab.EvaluateJavaScript('window.devicePixelRatio'))) # It seems that we should be able to set start_x to 2 * inset (one to # account for the inner div having left=1 and one to avoid sampling the # aa edge). For reasons not fully understood this is insufficent on # several bots (N9, 6P, mac-rel). start_x = 10 start_y = inset outer_size = 256 - inset skip = 10 for y in range(start_y, outer_size, skip): for x in range(start_x, outer_size, skip): self._CheckColorMatchAtLocation(canvasRGB, screenshot, x, y)
def _CompareScreenshotWithExpectation(expectation): """Compares a portion of the screenshot to the given expectation. Fails the test if a the screenshot does not match within the tolerance. Args: expectation: A dict defining an expected color region. It must contain 'location', 'size', and 'color' keys. See pixel_test_pages.py for examples. """ location = expectation["location"] size = expectation["size"] x0 = int(location[0] * device_pixel_ratio) x1 = int((location[0] + size[0]) * device_pixel_ratio) y0 = int(location[1] * device_pixel_ratio) y1 = int((location[1] + size[1]) * device_pixel_ratio) for x in range(x0, x1): for y in range(y0, y1): if (x < 0 or y < 0 or x >= image_util.Width(screenshot) or y >= image_util.Height(screenshot)): self.fail(('Expected pixel location [%d, %d] is out of range on ' + '[%d, %d] image') % (x, y, image_util.Width(screenshot), image_util.Height(screenshot))) actual_color = image_util.GetPixelColor(screenshot, x, y) expected_color = rgba_color.RgbaColor( expectation["color"][0], expectation["color"][1], expectation["color"][2], expectation["color"][3] if len(expectation["color"]) > 3 else 255) if not actual_color.IsEqual(expected_color, tolerance): self.fail('Expected pixel at ' + str(location) + ' (actual pixel (' + str(x) + ', ' + str(y) + ')) ' + ' to be ' + str(expectation["color"]) + " but got [" + str(actual_color.r) + ", " + str(actual_color.g) + ", " + str(actual_color.b) + ", " + str(actual_color.a) + "]")
def _CompareScreenshotSamples(screenshot, expectations, device_pixel_ratio): for expectation in expectations: location = expectation["location"] size = expectation["size"] x0 = int(location[0] * device_pixel_ratio) x1 = int((location[0] + size[0]) * device_pixel_ratio) y0 = int(location[1] * device_pixel_ratio) y1 = int((location[1] + size[1]) * device_pixel_ratio) for x in range(x0, x1): for y in range(y0, y1): if (x < 0 or y < 0 or x >= image_util.Width(screenshot) or y >= image_util.Height(screenshot)): raise page_test.Failure( ('Expected pixel location [%d, %d] is out of range on ' + '[%d, %d] image') % (x, y, image_util.Width(screenshot), image_util.Height(screenshot))) actual_color = image_util.GetPixelColor(screenshot, x, y) expected_color = rgba_color.RgbaColor(expectation["color"][0], expectation["color"][1], expectation["color"][2]) if not actual_color.IsEqual(expected_color, expectation["tolerance"]): raise page_test.Failure('Expected pixel at ' + str(location) + ' to be ' + str(expectation["color"]) + " but got [" + str(actual_color.r) + ", " + str(actual_color.g) + ", " + str(actual_color.b) + "]")
def testHistogramDistanceIgnoreColor(self): pixels = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3] bmp = image_util.FromRGBPixels(2, 2, pixels) hist1 = image_util.GetColorHistogram(bmp, ignore_color=rgba_color.RgbaColor( 1, 2, 3)) hist2 = image_util.GetColorHistogram(bmp) self.assertEquals(hist1.Distance(hist2), 0)
def Diff(self, other): # Output dimensions will be the maximum of the two input dimensions out_width = max(self.width, other.width) out_height = max(self.height, other.height) diff = [[0 for x in xrange(out_width * 3)] for x in xrange(out_height)] # Loop over each pixel and write out the difference for y in range(out_height): for x in range(out_width): if x < self.width and y < self.height: c0 = self.GetPixelColor(x, y) else: c0 = rgba_color.RgbaColor(0, 0, 0, 0) if x < other.width and y < other.height: c1 = other.GetPixelColor(x, y) else: c1 = rgba_color.RgbaColor(0, 0, 0, 0) offset = x * 3 diff[y][offset] = abs(c0.r - c1.r) diff[y][offset+1] = abs(c0.g - c1.g) diff[y][offset+2] = abs(c0.b - c1.b) # This particular method can only save to a file, so the result will be # written into an in-memory buffer and read back into a Bitmap warnings.warn( 'Using pure python png decoder, which could be very slow. To speed up, ' 'consider installing numpy & cv2 (OpenCV).') diff_img = png.from_array(diff, mode='RGB') output = cStringIO.StringIO() try: diff_img.save(output) diff = Bitmap.FromPng(output.getvalue()) finally: output.close() return diff
def CheckScreenshot(): canvasRGB = rgba_color.RgbaColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255) tab.EvaluateJavaScript("window.draw(%d, %d, %d);" % (canvasRGB.r, canvasRGB.g, canvasRGB.b)) screenshot = tab.Screenshot(5) start_x = 10 start_y = 0 outer_size = 256 skip = 10 for y in range(start_y, outer_size, skip): for x in range(start_x, outer_size, skip): CheckColorMatchAtLocation(canvasRGB, screenshot, x, y)
def _CheckScreenshot(self): canvasRGB = rgba_color.RgbaColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255) tab = self.tab tab.EvaluateJavaScript( "window.draw({{ red }}, {{ green }}, {{ blue }});", red=canvasRGB.r, green=canvasRGB.g, blue=canvasRGB.b) screenshot = tab.Screenshot(10) start_x = 10 start_y = 0 outer_size = 256 skip = 10 for y in range(start_y, outer_size, skip): for x in range(start_x, outer_size, skip): self._CheckColorMatchAtLocation(canvasRGB, screenshot, x, y)
def testHistogramIgnoreColor(self): pixels = [ 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 8, 7, 6, 5, 4, 6, 1, 2, 3, 1, 2, 3, 8, 7, 6, 5, 4, 6, 1, 2, 3 ] bmp = image_util.FromRGBPixels(4, 3, pixels) hist = image_util.GetColorHistogram(bmp, ignore_color=rgba_color.RgbaColor( 1, 2, 3)) self.assertEquals(hist.r[1], 0) self.assertEquals(hist.r[5], 2) self.assertEquals(hist.r[8], 2) self.assertEquals(hist.g[2], 0) self.assertEquals(hist.g[4], 2) self.assertEquals(hist.g[7], 2) self.assertEquals(hist.b[3], 0) self.assertEquals(hist.b[6], 4)
def testHistogramIgnoreColorTolerance(self): pixels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6] bmp = image_util.FromRGBPixels(2, 2, pixels) hist = image_util.GetColorHistogram(bmp, ignore_color=rgba_color.RgbaColor( 0, 1, 2), tolerance=1) self.assertEquals(hist.r[1], 0) self.assertEquals(hist.r[4], 1) self.assertEquals(hist.r[7], 1) self.assertEquals(hist.r[8], 1) self.assertEquals(hist.g[2], 0) self.assertEquals(hist.g[5], 1) self.assertEquals(hist.g[7], 1) self.assertEquals(hist.g[8], 1) self.assertEquals(hist.b[3], 0) self.assertEquals(hist.b[6], 2) self.assertEquals(hist.b[9], 1)
def _CompareScreenshotSamples(self, tab, screenshot, expected_colors, tolerance, device_pixel_ratio, test_machine_name): # First scan through the expected_colors and see if there are any scale # factor overrides that would preempt the device pixel ratio. This # is mainly a workaround for complex tests like the Maps test. for expectation in expected_colors: if 'scale_factor_overrides' in expectation: for override in expectation['scale_factor_overrides']: # Require exact matches to avoid confusion, because some # machine models and names might be subsets of others # (e.g. Nexus 5 vs Nexus 5X). if ('device_type' in override and (tab.browser.platform.GetDeviceTypeName() == override['device_type'])): logging.warning('Overriding device_pixel_ratio ' + str(device_pixel_ratio) + ' with scale factor ' + str(override['scale_factor']) + ' for device type ' + override['device_type']) device_pixel_ratio = override['scale_factor'] break if (test_machine_name and 'machine_name' in override and override["machine_name"] == test_machine_name): logging.warning('Overriding device_pixel_ratio ' + str(device_pixel_ratio) + ' with scale factor ' + str(override['scale_factor']) + ' for machine name ' + test_machine_name) device_pixel_ratio = override['scale_factor'] break # Only support one "scale_factor_overrides" in the expectation format. break for expectation in expected_colors: if "scale_factor_overrides" in expectation: continue location = expectation["location"] size = expectation["size"] x0 = int(location[0] * device_pixel_ratio) x1 = int((location[0] + size[0]) * device_pixel_ratio) y0 = int(location[1] * device_pixel_ratio) y1 = int((location[1] + size[1]) * device_pixel_ratio) for x in range(x0, x1): for y in range(y0, y1): if (x < 0 or y < 0 or x >= image_util.Width(screenshot) or y >= image_util.Height(screenshot)): self.fail(( 'Expected pixel location [%d, %d] is out of range on ' + '[%d, %d] image') % (x, y, image_util.Width(screenshot), image_util.Height(screenshot))) actual_color = image_util.GetPixelColor(screenshot, x, y) expected_color = rgba_color.RgbaColor( expectation["color"][0], expectation["color"][1], expectation["color"][2]) if not actual_color.IsEqual(expected_color, tolerance): self.fail('Expected pixel at ' + str(location) + ' (actual pixel (' + str(x) + ', ' + str(y) + ')) ' + ' to be ' + str(expectation["color"]) + " but got [" + str(actual_color.r) + ", " + str(actual_color.g) + ", " + str(actual_color.b) + "]")
def GetPixelColor(image, x, y): bgr = image[y][x] return rgba_color.RgbaColor(bgr[2], bgr[1], bgr[0])