| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """ | 5 """ |
| 6 Bitmap is a basic wrapper for image pixels. It includes some basic processing | 6 Bitmap is a basic wrapper for image pixels. It includes some basic processing |
| 7 tools: crop, find bounding box of a color and compute histogram of color values. | 7 tools: crop, find bounding box of a color and compute histogram of color values. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import array | 10 import array |
| 11 import base64 | 11 import base64 |
| 12 import cStringIO | 12 import cStringIO |
| 13 import collections | 13 import collections |
| 14 import struct | 14 import struct |
| 15 import subprocess | 15 import subprocess |
| 16 import sys | |
| 17 | 16 |
| 18 from telemetry.core import util | 17 from telemetry.core import util |
| 18 from telemetry.core.platform import factory |
| 19 from telemetry.util import support_binaries |
| 19 | 20 |
| 20 util.AddDirToPythonPath(util.GetTelemetryDir(), 'third_party', 'png') | 21 util.AddDirToPythonPath(util.GetTelemetryDir(), 'third_party', 'png') |
| 21 import png # pylint: disable=F0401 | 22 import png # pylint: disable=F0401 |
| 22 | 23 |
| 23 | 24 |
| 24 def HistogramDistance(hist1, hist2): | 25 def HistogramDistance(hist1, hist2): |
| 25 """Earth mover's distance. | 26 """Earth mover's distance. |
| 26 | 27 |
| 27 http://en.wikipedia.org/wiki/Earth_mover's_distance | 28 http://en.wikipedia.org/wiki/Earth_mover's_distance |
| 28 First, normalize the two histograms. Then, treat the two histograms as | 29 First, normalize the two histograms. Then, treat the two histograms as |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 WHITE = RgbaColor(255, 255, 255) | 115 WHITE = RgbaColor(255, 255, 255) |
| 115 | 116 |
| 116 | 117 |
| 117 class _BitmapTools(object): | 118 class _BitmapTools(object): |
| 118 """Wraps a child process of bitmaptools and allows for one command.""" | 119 """Wraps a child process of bitmaptools and allows for one command.""" |
| 119 CROP_PIXELS = 0 | 120 CROP_PIXELS = 0 |
| 120 HISTOGRAM = 1 | 121 HISTOGRAM = 1 |
| 121 BOUNDING_BOX = 2 | 122 BOUNDING_BOX = 2 |
| 122 | 123 |
| 123 def __init__(self, dimensions, pixels): | 124 def __init__(self, dimensions, pixels): |
| 124 suffix = '.exe' if sys.platform == 'win32' else '' | 125 binary = support_binaries.FindPath( |
| 125 binary = util.FindSupportBinary('bitmaptools' + suffix) | 126 'bitmaptools', factory.GetPlatformBackendForCurrentOS().GetOSName()) |
| 126 assert binary, 'You must build bitmaptools first!' | 127 assert binary, 'You must build bitmaptools first!' |
| 127 | 128 |
| 128 self._popen = subprocess.Popen([binary], | 129 self._popen = subprocess.Popen([binary], |
| 129 stdin=subprocess.PIPE, | 130 stdin=subprocess.PIPE, |
| 130 stdout=subprocess.PIPE, | 131 stdout=subprocess.PIPE, |
| 131 stderr=subprocess.PIPE) | 132 stderr=subprocess.PIPE) |
| 132 | 133 |
| 133 # dimensions are: bpp, width, height, boxleft, boxtop, boxwidth, boxheight | 134 # dimensions are: bpp, width, height, boxleft, boxtop, boxwidth, boxheight |
| 134 packed_dims = struct.pack('iiiiiii', *dimensions) | 135 packed_dims = struct.pack('iiiiiii', *dimensions) |
| 135 self._popen.stdin.write(packed_dims) | 136 self._popen.stdin.write(packed_dims) |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 def ColorHistogram(self, ignore_color=None, tolerance=0): | 341 def ColorHistogram(self, ignore_color=None, tolerance=0): |
| 341 """Computes a histogram of the pixel colors in this Bitmap. | 342 """Computes a histogram of the pixel colors in this Bitmap. |
| 342 Args: | 343 Args: |
| 343 ignore_color: An RgbaColor to exclude from the bucket counts. | 344 ignore_color: An RgbaColor to exclude from the bucket counts. |
| 344 tolerance: A tolerance for the ignore_color. | 345 tolerance: A tolerance for the ignore_color. |
| 345 | 346 |
| 346 Returns: | 347 Returns: |
| 347 A ColorHistogram namedtuple with 256 integers in each field: r, g, and b. | 348 A ColorHistogram namedtuple with 256 integers in each field: r, g, and b. |
| 348 """ | 349 """ |
| 349 return self._PrepareTools().Histogram(ignore_color, tolerance) | 350 return self._PrepareTools().Histogram(ignore_color, tolerance) |
| OLD | NEW |