cropping_util.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_UTILITY_CROPPING_UTIL_H_
  5. #define ASH_UTILITY_CROPPING_UTIL_H_
  6. #include "ash/ash_export.h"
  7. #include "third_party/skia/include/core/SkBitmap.h"
  8. namespace gfx {
  9. class Size;
  10. } // namespace gfx
  11. namespace ash {
  12. // Crops an image such that its aspect ratio matches that of a target size, but
  13. // does not perform any "scaling". The cropping is calculated with the image
  14. // and the target rect "center-aligned". The image dimension with the smaller
  15. // (target_size / original_size) ratio gets cropped.
  16. //
  17. // A visual example with a portrait image whose dimensions exceeds a landscape
  18. // target size:
  19. //
  20. // Before:
  21. //
  22. // Portrait Image
  23. // +---------------------------+
  24. // | |
  25. // | |
  26. // | |
  27. // | |
  28. // | Landscape Target |
  29. // | +-----------------+ |
  30. // | | | |
  31. // | | | |
  32. // | | | |
  33. // | | | |
  34. // | | | |
  35. // | +-----------------+ |
  36. // | |
  37. // | |
  38. // | |
  39. // | |
  40. // | |
  41. // +---------------------------+
  42. //
  43. // After (ok, maybe it's not the exact same aspect ratio, but you get the idea):
  44. //
  45. // Cropped Image
  46. // +---------------------------+
  47. // | |
  48. // | Landscape Target |
  49. // | +-----------------+ |
  50. // | | | |
  51. // | | | |
  52. // | | | |
  53. // | | | |
  54. // | | | |
  55. // | +-----------------+ |
  56. // | |
  57. // | |
  58. // +---------------------------+
  59. //
  60. // The ultimate result is always a cropped image whose aspect ratio matches that
  61. // of the target size. Therefore, the cropped image can subsequently be scaled
  62. // up or down to match the dimensions of the target size.
  63. //
  64. // There are no requirements for the image and target dimensions other than that
  65. // they're non-empty. This function cannot fail; the returned SkBitmap is always
  66. // non-null and points to ref-counted pixel memory shared with |image|.
  67. ASH_EXPORT SkBitmap CenterCropImage(const SkBitmap& image,
  68. const gfx::Size& target_size);
  69. } // namespace ash
  70. #endif // ASH_UTILITY_CROPPING_UTIL_H_