// 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/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"

class SkBitmap;

namespace lens {

class ImageData;

// 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);

}  // namespace lens

#endif  // CHROME_BROWSER_UI_LENS_LENS_OVERLAY_IMAGE_HELPER_H_
