#include "chrome/browser/ui/lens/lens_overlay_image_helper.h"
#include <numbers>
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/numerics/safe_math.h"
#include "chrome/browser/ui/lens/ref_counted_lens_overlay_client_logs.h"
#include "components/lens/lens_features.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/lens_server_proto/lens_overlay_phase_latencies_metadata.pb.h"
#include "third_party/lens_server_proto/lens_overlay_polygon.pb.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/codec/webp_codec.h"
#include "ui/gfx/color_analysis.h"
#include "ui/gfx/color_conversions.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia_operations.h"
namespace {
bool ShouldDownscaleSize(const gfx::Size& size,
int max_area,
int max_width,
int max_height) { … }
bool ShouldDownscaleSizeWithUiScaling(const gfx::Size& size,
int max_area,
int max_width,
int max_height,
int ui_scale_factor) { … }
double GetPreferredScale(const gfx::Size& original_size,
int target_width,
int target_height) { … }
gfx::Size GetPreferredSize(const gfx::Size& original_size,
int target_width,
int target_height) { … }
void AddClientLogsForDownscale(
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs,
const SkBitmap& original_image,
const SkBitmap& downscaled_image) { … }
void AddClientLogsForEncode(
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs,
scoped_refptr<base::RefCountedBytes> output_bytes) { … }
SkBitmap DownscaleImage(
const SkBitmap& image,
int target_width,
int target_height,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs) { … }
SkBitmap DownscaleImageIfNeededWithTieredApproach(
const SkBitmap& image,
int ui_scale_factor,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs) { … }
SkBitmap DownscaleImageIfNeeded(
const SkBitmap& image,
int ui_scale_factor,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs) { … }
SkBitmap CropAndDownscaleImageIfNeeded(
const SkBitmap& image,
gfx::Rect region,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs) { … }
gfx::Rect GetRectForRegion(const SkBitmap& image,
const lens::mojom::CenterRotatedBoxPtr& region) { … }
}
namespace lens {
bool EncodeImage(
const SkBitmap& image,
int compression_quality,
scoped_refptr<base::RefCountedBytes> output,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs) { … }
bool EncodeImageMaybeWithTransparency(
const SkBitmap& image,
int compression_quality,
scoped_refptr<base::RefCountedBytes> output,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs) { … }
lens::ImageData DownscaleAndEncodeBitmap(
const SkBitmap& image,
int ui_scale_factor,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs) { … }
void AddSignificantRegions(
lens::ImageData& image_data,
std::vector<lens::mojom::CenterRotatedBoxPtr> significant_region_boxes) { … }
SkBitmap CropBitmapToRegion(const SkBitmap& image,
lens::mojom::CenterRotatedBoxPtr region) { … }
std::optional<lens::ImageCrop> DownscaleAndEncodeBitmapRegionIfNeeded(
const SkBitmap& image,
lens::mojom::CenterRotatedBoxPtr region,
std::optional<SkBitmap> region_bytes,
scoped_refptr<lens::RefCountedLensOverlayClientLogs> client_logs) { … }
lens::mojom::CenterRotatedBoxPtr GetCenterRotatedBoxFromTabViewAndImageBounds(
const gfx::Rect& tab_bounds,
const gfx::Rect& view_bounds,
gfx::Rect image_bounds) { … }
SkColor ExtractVibrantOrDominantColorFromImage(const SkBitmap& image,
float min_population_pct) { … }
std::optional<float> CalculateHueAngle(
const std::tuple<float, float, float>& lab_color) { … }
float CalculateChroma(const std::tuple<float, float, float>& lab_color) { … }
std::optional<float> CalculateHueAngleDistance(
const std::tuple<float, float, float>& lab_color1,
const std::tuple<float, float, float>& lab_color2) { … }
std::tuple<float, float, float> ConvertColorToLab(SkColor color) { … }
SkColor FindBestMatchedColorOrTransparent(
const std::vector<SkColor>& candidate_colors,
SkColor seed_color,
float min_chroma) { … }
}