Chromium Code Reviews| Index: tools/telemetry/telemetry/snapshot.py |
| diff --git a/tools/telemetry/telemetry/snapshot.py b/tools/telemetry/telemetry/snapshot.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e2a596a22b9a977b881d76dab5aa89ccc2b30396 |
| --- /dev/null |
| +++ b/tools/telemetry/telemetry/snapshot.py |
| @@ -0,0 +1,50 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +import sys |
| +import os |
| +import base64 |
| + |
| +sys.path.append(os.path.join(os.path.dirname(__file__), '../third_party')) |
| +import png # pylint: disable=F0401 |
|
nduca
2012/11/09 21:19:00
this is a bit surprising, I dont like it. Is there
|
| + |
|
nduca
2012/11/09 21:19:00
please also guard against this running twice, e.g.
|
| +class Snapshot(object): |
|
nduca
2012/11/09 21:19:00
Why call this snapshot and not, say, PngBitmap?
nduca
2012/11/09 21:19:00
This class should be unit tested. Put a png in uni
|
| + """Utilities for inspecting a snapshot of a tab""" |
| + |
| + def __init__(self, base64_png): |
| + self._png_data = base64.b64decode(base64_png) |
| + self._png = png.Reader(bytes=self._png_data) |
| + rgba8_data = self._png.asRGBA8() |
| + self._width = rgba8_data[0] |
| + self._height = rgba8_data[1] |
| + self._pixels = list(rgba8_data[2]) |
| + self._metadata = rgba8_data[3] |
| + |
| + @property |
| + def width(self): |
| + """Width of the snapshot""" |
| + return self._width |
| + |
| + @property |
| + def height(self): |
| + """Height of the snapshot""" |
| + return self._height |
| + |
| + def GetPixel(self, x, y): |
|
nduca
2012/11/09 21:19:00
GetPixelColor
|
| + """Returns a 3 element tuple describing the pixel color at (x, y)""" |
| + row = self._pixels[y] |
| + offset = x * 4 |
| + return [row[offset], row[offset + 1], row[offset + 2]] |
|
nduca
2012/11/09 21:19:00
make a color class
|
| + |
| + def CheckPixelColor(self, x, y, expected_color, tolerance=0): |
| + """Verifies that the pixel at (x, y) is the expected color""" |
|
nduca
2012/11/09 21:19:00
This is a method on the color class.
|
| + pixel_color = self.GetPixel(x, y) |
| + r_diff = abs(pixel_color[0] - expected_color[0]) |
| + g_diff = abs(pixel_color[1] - expected_color[1]) |
| + b_diff = abs(pixel_color[2] - expected_color[2]) |
| + return r_diff <= tolerance and g_diff <= tolerance and b_diff <= tolerance |
| + |
| + def WriteFile(self, path): |
| + f = open(path, "wb") |
|
nduca
2012/11/09 21:19:00
prefer this style
with open(..) as f:
f.wri
|
| + f.write(self._png_data) |
| + f.close() |