OSDN Git Service

GLConsumer: Fix crop math
authorDan Stoza <stoza@google.com>
Tue, 9 Jun 2015 22:05:23 +0000 (15:05 -0700)
committerDan Stoza <stoza@google.com>
Wed, 10 Jun 2015 17:24:51 +0000 (10:24 -0700)
When we have excess pixels that need to be removed from (for example)
the left and right sides, we currently do something like:

  left += excess / 2;
  right -= excess / 2;

If excess is odd, however, this removes 1 too few pixels since the odd
pixel gets rounded down twice. This changes the math to effectively:

  left += excess / 2;
  right -= (excess - excess / 2);

Which removes the correct total number of pixels.

Bug: 19611086
Change-Id: I8d1ad9fe7ba67c149794c148663d12acbccccef0

libs/gui/GLConsumer.cpp

index 9fcac2d..757e08a 100644 (file)
@@ -901,14 +901,18 @@ Rect GLConsumer::getCurrentCrop() const {
 
         // The crop is too wide
         if (newWidth < currentWidth) {
-            uint32_t dw = (currentWidth - newWidth) / 2;
-            outCrop.left += dw;
-            outCrop.right -= dw;
+            uint32_t dw = currentWidth - newWidth;
+            auto halfdw = dw / 2;
+            outCrop.left += halfdw;
+            // Not halfdw because it would subtract 1 too few when dw is odd
+            outCrop.right -= (dw - halfdw);
         // The crop is too tall
         } else if (newHeight < currentHeight) {
-            uint32_t dh = (currentHeight - newHeight) / 2;
-            outCrop.top += dh;
-            outCrop.bottom -= dh;
+            uint32_t dh = currentHeight - newHeight;
+            auto halfdh = dh / 2;
+            outCrop.top += halfdh;
+            // Not halfdh because it would subtract 1 too few when dh is odd
+            outCrop.bottom -= (dh - halfdh);
         }
 
         GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",