Commit d67e0b8e authored by Stephen Chenney's avatar Stephen Chenney Committed by Commit Bot

Ensure non-zero block size for image classification

When classifying images for dark mode the src rect
can be smaller than 1x1 (the src is a floating point rect).
Rather than rounding down, giving divide by 0, we should
round up.

The downstream code was reviewed to look for cases where this
might break and none were identified.

Bug: 1119761
Change-Id: I4b8eea35ff33274a0f30e68f1487677ea4f19c29
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2413040
Auto-Submit: Stephen Chenney <schenney@chromium.org>
Reviewed-by: default avatarPhilip Rogers <pdr@chromium.org>
Commit-Queue: Stephen Chenney <schenney@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807538}
parent 6d29c9e8
......@@ -8,6 +8,7 @@
#include "base/memory/singleton.h"
#include "base/optional.h"
#include "third_party/blink/renderer/platform/geometry/int_size.h"
#include "third_party/blink/renderer/platform/graphics/darkmode/darkmode_classifier.h"
#include "third_party/skia/include/utils/SkNullCanvas.h"
......@@ -183,14 +184,18 @@ void DarkModeImageClassifier::GetSamples(const PaintImage& paint_image,
int num_blocks_x = kMaxBlocks;
int num_blocks_y = kMaxBlocks;
if (num_sampled_pixels > src.width() * src.height())
num_sampled_pixels = src.width() * src.height();
// Crash reports indicate that the src can be less than 1, so make
// sure it goes to 1. We know it is not 0 because GetBitmap above
// will return false for zero-sized src.
IntSize rounded_src(ceil(src.width()), ceil(src.height()));
if (num_blocks_x > src.width())
num_blocks_x = floor(src.width());
if (num_sampled_pixels > rounded_src.Width() * rounded_src.Height())
num_sampled_pixels = rounded_src.Width() * rounded_src.Height();
if (num_blocks_y > src.height())
num_blocks_y = floor(src.height());
if (num_blocks_x > rounded_src.Width())
num_blocks_x = rounded_src.Width();
if (num_blocks_y > rounded_src.Height())
num_blocks_y = rounded_src.Height();
int pixels_per_block = num_sampled_pixels / (num_blocks_x * num_blocks_y);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment