1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_LENS_LENS_OVERLAY_IMAGE_HELPER_H_
#define CHROME_BROWSER_UI_LENS_LENS_OVERLAY_IMAGE_HELPER_H_
#include "base/memory/ref_counted_memory.h"
#include "chrome/browser/lens/core/mojom/geometry.mojom.h"
#include "components/lens/ref_counted_lens_overlay_client_logs.h"
#include "third_party/lens_server_proto/lens_overlay_image_crop.pb.h"
#include "third_party/lens_server_proto/lens_overlay_image_data.pb.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
class SkBitmap;
namespace lens {
// A struct containing an ImageCrop proto and the bitmap that was used to
// create the ImageCrop.
struct ImageCropAndBitmap {
lens::ImageCrop image_crop;
SkBitmap region_bitmap;
};
// Encodes the SkBitmap into JPEG placing the bytes into |output|. Returns false
// if encoding fails. Outputs image processing data to the client logs.
bool EncodeImage(
const SkBitmap& image,
int compression_quality,
scoped_refptr<base::RefCountedBytes> output,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs);
// Downscales and encodes the provided bitmap and then stores it in a
// lens::ImageData object. Returns an empty object if encoding fails.
// Downscaling only occurs if the bitmap dimensions mixed with the
// ui_scale_factor exceed configured flag values. ui_scale_factor will be
// ignored if tiered downscaling is disabled. Outputs image processing
// data to the client logs.
lens::ImageData DownscaleAndEncodeBitmap(
const SkBitmap& image,
int ui_scale_factor,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs);
// Adds the significant regions to the lens::ImageData object.
void AddSignificantRegions(
lens::ImageData& image_data,
std::vector<lens::mojom::CenterRotatedBoxPtr> significant_region_boxes);
// Crops the given bitmap to the given region.
SkBitmap CropBitmapToRegion(const SkBitmap& image,
lens::mojom::CenterRotatedBoxPtr region);
// Downscales and encodes the provided bitmap region and then stores it in a
// lens::ImageCropAndBitmap object if needed. Returns a nullopt if the region is
// not set. Downscaling only occurs if the region dimensions exceed configured
// flag values. Providing region_bytes will use those bytes instead of cropping
// the region from the full page bytes. Outputs image processing data to the
// client logs.
std::optional<lens::ImageCropAndBitmap> DownscaleAndEncodeBitmapRegionIfNeeded(
const SkBitmap& image,
lens::mojom::CenterRotatedBoxPtr region,
std::optional<SkBitmap> region_bytes,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs);
// Returns a normalized bounding box from the given tab, view, and image
// bounds, clipping if the image bounds go outside the tab or view bounds.
lens::mojom::CenterRotatedBoxPtr GetCenterRotatedBoxFromTabViewAndImageBounds(
const gfx::Rect& tab_bounds,
const gfx::Rect& view_bounds,
gfx::Rect image_bounds);
// Returns the extracted color from the given image. If the extraction fails,
// the returned color is transparent. The returned color should be
// representative of min_population_pct [0.0, 1.0] of the total number of pixels
// in the image.
SkColor ExtractVibrantOrDominantColorFromImage(const SkBitmap& image,
float min_population_pct);
// Returns the hue angle of the given color in LAB space, in radians [-π, π],
// or null_opt if achromatic.
std::optional<float> CalculateHueAngle(
const std::tuple<float, float, float>& lab_color);
// Returns the chroma distance (from 0) of the given color in LAB space.
float CalculateChroma(const std::tuple<float, float, float>& lab_color);
// Returns the hue angle distance of the 2 given colors [0, 2π).
std::optional<float> CalculateHueAngleDistance(
const std::tuple<float, float, float>& lab_color1,
const std::tuple<float, float, float>& lab_color2);
// Returns std::tuple<int> of given color in CIELAB color space.
std::tuple<float, float, float> ConvertColorToLab(SkColor color);
// Returns the best matched color theme item in the given
// map of candidates, matching on the closest in hue angle distance to the
// given seed color. If seed_color is invalid, or does not have
// enough chroma, given by min_chroma, or no good matches can be found,
// returns SK_ColorTransparent.
SkColor FindBestMatchedColorOrTransparent(
const std::vector<SkColor>& candidate_colors,
SkColor seed_color,
float min_chroma);
// Returns whether the bitmaps are equal.
bool AreBitmapsEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2);
} // namespace lens
#endif // CHROME_BROWSER_UI_LENS_LENS_OVERLAY_IMAGE_HELPER_H_
|