Skip to content

Commit

Permalink
Revert 243469 "[telemetry] Implement per-pixel algorithms in Bit..."
Browse files Browse the repository at this point in the history
> [telemetry] Implement per-pixel algorithms in Bitmap as a C++ extension.
> 
> The extension provides fast bitmap operations with no external
> dependencies. However, it is not available on all platforms.
> 
> BUG=323813
> TEST=telemetry bitmap_unittest
> R=bulach@chromium.org, tonyg@chromium.org
> 
> Review URL: https://codereview.chromium.org/121493004

TBR=szym@chromium.org

Review URL: https://codereview.chromium.org/127533002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243478 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
thestig@chromium.org committed Jan 8, 2014
1 parent 8b44050 commit 82c9256
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 536 deletions.
1 change: 0 additions & 1 deletion build/all.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@
'../third_party/cacheinvalidation/cacheinvalidation.gyp:cacheinvalidation_unittests',
'../third_party/libaddressinput/libaddressinput.gyp:libaddressinput_unittests',
'../third_party/libphonenumber/libphonenumber.gyp:libphonenumber_unittests',
'../tools/telemetry/telemetry.gyp:*',
'../webkit/renderer/compositor_bindings/compositor_bindings_tests.gyp:webkit_compositor_bindings_unittests',
],
}],
Expand Down
14 changes: 0 additions & 14 deletions chrome/telemetry.isolate
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,5 @@
],
},
}],
['OS=="android" or OS=="linux" or OS=="mac"', {
'variables': {
'isolate_dependency_tracked': [
'<(PRODUCT_DIR)/bitmaptools.so',
],
},
}],
['OS=="win"', {
'variables': {
'isolate_dependency_tracked': [
'<(PRODUCT_DIR)/bitmaptools.pyd',
],
},
}],
]
}
45 changes: 0 additions & 45 deletions tools/telemetry/telemetry.gyp

This file was deleted.

116 changes: 67 additions & 49 deletions tools/telemetry/telemetry/core/bitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def __init__(self, bpp, width, height, pixels, metadata=None):
self._height = height
self._pixels = pixels
self._metadata = metadata or {}
self._crop_box = None

@property
def bpp(self):
Expand All @@ -61,36 +60,16 @@ def bpp(self):
@property
def width(self):
"""Width of the bitmap."""
if self._crop_box:
return self._crop_box[2]
return self._width

@property
def height(self):
"""Height of the bitmap."""
if self._crop_box:
return self._crop_box[3]
return self._height

@property
def _as_tuple(self):
# If we got a list of ints, we need to convert it into a byte buffer.
pixels = self._pixels
if type(pixels) is not bytearray:
pixels = bytearray(pixels)
if type(pixels) is not bytes:
pixels = bytes(pixels)
crop_box = self._crop_box or (0, 0, self._width, self._height)
return pixels, self._width, self._bpp, crop_box

@property
def pixels(self):
"""Flat pixel array of the bitmap."""
if self._crop_box:
from telemetry.core import bitmaptools
self._pixels = bitmaptools.Crop(self._as_tuple)
_, _, self._width, self._height = self._crop_box
self._crop_box = None
if type(self._pixels) is not bytearray:
self._pixels = bytearray(self._pixels)
return self._pixels
Expand All @@ -104,13 +83,12 @@ def metadata(self):

def GetPixelColor(self, x, y):
"""Returns a RgbaColor for the pixel at (x, y)."""
pixels = self.pixels
base = self._bpp * (y * self._width + x)
if self._bpp == 4:
return RgbaColor(pixels[base + 0], pixels[base + 1],
pixels[base + 2], pixels[base + 3])
return RgbaColor(pixels[base + 0], pixels[base + 1],
pixels[base + 2])
return RgbaColor(self._pixels[base + 0], self._pixels[base + 1],
self._pixels[base + 2], self._pixels[base + 3])
return RgbaColor(self._pixels[base + 0], self._pixels[base + 1],
self._pixels[base + 2])

def WritePngFile(self, path):
with open(path, "wb") as f:
Expand All @@ -131,11 +109,24 @@ def FromBase64Png(base64_png):
return Bitmap.FromPng(base64.b64decode(base64_png))

def IsEqual(self, other, tolerance=0):
"""Determines whether two Bitmaps are identical within a given tolerance.
Ignores alpha channel."""
from telemetry.core import bitmaptools
# pylint: disable=W0212
return bitmaptools.Equal(self._as_tuple, other._as_tuple, tolerance)
"""Determines whether two Bitmaps are identical within a given tolerance."""

# Dimensions must be equal
if self.width != other.width or self.height != other.height:
return False

# Loop over each pixel and test for equality
if tolerance or self.bpp != other.bpp:
for y in range(self.height):
for x in range(self.width):
c0 = self.GetPixelColor(x, y)
c1 = other.GetPixelColor(x, y)
if not c0.IsEqual(c1, tolerance):
return False
else:
return self.pixels == other.pixels

return True

def Diff(self, other):
"""Returns a new Bitmap that represents the difference between this image
Expand Down Expand Up @@ -178,28 +169,55 @@ def Diff(self, other):
return diff

def GetBoundingBox(self, color, tolerance=0):
"""Finds the minimum box surrounding all occurences of |color|.
Returns: (top, left, width, height), match_count
Ignores the alpha channel."""
from telemetry.core import bitmaptools
int_color = (color.r << 16) | (color.g << 8) | color.b
return bitmaptools.BoundingBox(self._as_tuple, int_color, tolerance)

def Crop(self, left, top, width, height):
"""Crops the current bitmap down to the specified box."""
cur_box = self._crop_box or (0, 0, self._width, self._height)
cur_left, cur_top, cur_width, cur_height = cur_box
"""Returns a (top, left, width, height) tuple of the minimum box
surrounding all occurences of |color|."""
# TODO(szym): Implement this.
raise NotImplementedError("GetBoundingBox not yet implemented.")

def Crop(self, top, left, width, height):
"""Crops the current bitmap down to the specified box.
TODO(szym): Make this O(1).
"""
if (left < 0 or top < 0 or
(left + width) > cur_width or
(top + height) > cur_height):
(left + width) > self.width or
(top + height) > self.height):
raise ValueError('Invalid dimensions')

self._crop_box = cur_left + left, cur_top + top, width, height
img_data = [[0 for x in xrange(width * self.bpp)]
for y in xrange(height)]

# Copy each pixel in the sub-rect.
# TODO(tonyg): Make this faster by avoiding the copy and artificially
# restricting the dimensions.
for y in range(height):
for x in range(width):
c = self.GetPixelColor(x + left, y + top)
offset = x * self.bpp
img_data[y][offset] = c.r
img_data[y][offset + 1] = c.g
img_data[y][offset + 2] = c.b
if self.bpp == 4:
img_data[y][offset + 3] = c.a

# 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
crop_img = png.from_array(img_data, mode='RGBA' if self.bpp == 4 else 'RGB')
output = cStringIO.StringIO()
try:
crop_img.save(output)
width, height, pixels, meta = png.Reader(
bytes=output.getvalue()).read_flat()
self._width = width
self._height = height
self._pixels = pixels
self._metadata = meta
finally:
output.close()

return self

def ColorHistogram(self):
"""Computes a histogram of the pixel colors in this Bitmap.
Returns a list of 3x256 integers."""
from telemetry.core import bitmaptools
return bitmaptools.Histogram(self._as_tuple)
"""Returns a histogram of the pixel colors in this Bitmap."""
# TODO(szym): Implement this.
raise NotImplementedError("ColorHistogram not yet implemented.")
40 changes: 5 additions & 35 deletions tools/telemetry/telemetry/core/bitmap_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ def testWriteCroppedBmpToPngFile(self):
def testIsEqual(self):
bmp = bitmap.Bitmap.FromBase64Png(test_png)
file_bmp = bitmap.Bitmap.FromPngFile(test_png_path)
self.assertTrue(bmp.IsEqual(file_bmp, tolerance=1))
# Zero tolerance IsEqual has a different implementation.
self.assertTrue(bmp.IsEqual(file_bmp))

def testDiff(self):
Expand Down Expand Up @@ -104,43 +102,15 @@ def testDiff(self):
diff_bmp.GetPixelColor(2, 1).AssertIsRGB(255, 255, 255)
diff_bmp.GetPixelColor(2, 2).AssertIsRGB(255, 255, 255)

def testGetBoundingBox(self):
def testCrop(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 = bitmap.Bitmap(3, 4, 3, pixels)
box, count = bmp.GetBoundingBox(bitmap.RgbaColor(1, 0, 0))
self.assertEquals(box, (1, 1, 2, 1))
self.assertEquals(count, 2)

box, count = bmp.GetBoundingBox(bitmap.RgbaColor(0, 1, 0))
self.assertEquals(box, None)
self.assertEquals(count, 0)

def testCrop(self):
pixels = [0,0,0, 1,0,0, 2,0,0, 3,0,0,
0,1,0, 1,1,0, 2,1,0, 3,1,0,
0,2,0, 1,2,0, 2,2,0, 3,2,0]
bmp = bitmap.Bitmap(3, 4, 3, pixels)
bmp.Crop(1, 2, 2, 1)
bmp.Crop(1, 1, 2, 1)

self.assertEquals(bmp.width, 2)
self.assertEquals(bmp.height, 1)
bmp.GetPixelColor(0, 0).AssertIsRGB(1, 2, 0)
bmp.GetPixelColor(1, 0).AssertIsRGB(2, 2, 0)
self.assertEquals(bmp.pixels, bytearray([1,2,0, 2,2,0]))

def testHistogram(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 = bitmap.Bitmap(3, 4, 3, pixels)
bmp.Crop(1, 1, 2, 2)

histogram = bmp.ColorHistogram()
self.assertEquals(sum(histogram), bmp.width * bmp.height * 3)
self.assertEquals(histogram[5], 2)
self.assertEquals(histogram[8], 2)
self.assertEquals(histogram[4 + 256], 2)
self.assertEquals(histogram[7 + 256], 2)
self.assertEquals(histogram[6 + 512], 4)
bmp.GetPixelColor(0, 0).AssertIsRGB(1, 0, 0)
bmp.GetPixelColor(1, 0).AssertIsRGB(1, 0, 0)
self.assertEquals(bmp.pixels, bytearray([1,0,0, 1,0,0]))
38 changes: 0 additions & 38 deletions tools/telemetry/telemetry/core/bitmaptools/__init__.py

This file was deleted.

Loading

0 comments on commit 82c9256

Please sign in to comment.