SmartCrop.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2024 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.handler.mediasource.ngdm.impl.metadata;

  21. import org.apache.commons.lang3.builder.ToStringBuilder;
  22. import org.apache.commons.lang3.builder.ToStringStyle;

  23. import io.wcm.handler.media.CropDimension;
  24. import io.wcm.handler.media.Dimension;
  25. import io.wcm.handler.media.format.Ratio;

  26. /**
  27.  * Named smart cropping definition.
  28.  */
  29. public class SmartCrop {
  30.   private final String name;
  31.   private final CropDimension cropDimension;
  32.   private final double ratio;

  33.   SmartCrop(String name,
  34.       double leftPercentage, double topPercentage, double widthPercentage, double heightPercentage,
  35.       Dimension originalDimension) {

  36.     // calculate actual cropping dimension
  37.     long originalWidth = originalDimension.getWidth();
  38.     long originalHeight = originalDimension.getHeight();
  39.     long left = Math.round(originalWidth * leftPercentage);
  40.     long top = Math.round(originalHeight * topPercentage);
  41.     long width = Math.round(originalWidth * widthPercentage);
  42.     long height = Math.round(originalHeight * heightPercentage);

  43.     this.name = name;
  44.     this.cropDimension = new CropDimension(left, top, width, height, true);
  45.     this.ratio = Ratio.get(width, height);
  46.   }

  47.   SmartCrop(String name, MetadataResponse.SmartCrop smartCrop, Dimension originalDimension) {
  48.     this(name, smartCrop.left, smartCrop.top, smartCrop.normalizedWidth, smartCrop.normalizedHeight,
  49.         originalDimension);
  50.   }

  51.   public String getName() {
  52.     return name;
  53.   }

  54.   public CropDimension getCropDimension() {
  55.     return this.cropDimension;
  56.   }

  57.   public double getRatio() {
  58.     return this.ratio;
  59.   }

  60.   @Override
  61.   public String toString() {
  62.     return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_CLASS_NAME_STYLE);
  63.   }
  64. }