• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { Log } from '@ohos/base/src/main/ets/utils/Log';
17import { RectF } from '../base/Rect';
18import { ImageFilterBase } from '../base/ImageFilterBase';
19import { PixelMapWrapper } from '../base/PixelMapWrapper';
20import { MathUtils } from './MathUtils';
21
22export class ImageFilterCrop extends ImageFilterBase {
23    private static readonly FILTER_NAME: string = 'FilterCrop';
24    private TAG: string = 'ImageFilterCrop';
25    private isFlipHorizontal: boolean = false;
26    private isFlipVertically: boolean = false;
27    private rotationAngle: number = 0;
28    private horizontalAngle: number = 0;
29    private cropRect: RectF = undefined;
30
31    constructor() {
32        super(ImageFilterCrop.FILTER_NAME);
33        this.cropRect = new RectF();
34    }
35
36    render(pixelMap: PixelMapWrapper): PixelMapWrapper {
37        Log.debug(this.TAG, `render input wrapper: ${JSON.stringify(pixelMap)}`);
38        let width = pixelMap.width;
39        let height = pixelMap.height;
40        let realCropRect = new RectF();
41        realCropRect.set(this.cropRect.left, this.cropRect.top, this.cropRect.right, this.cropRect.bottom);
42        MathUtils.revertRect(realCropRect, width, height);
43        MathUtils.roundRect(realCropRect);
44        Log.debug(this.TAG, `realCropRect: ${JSON.stringify(realCropRect)}`);
45
46        let offWidth = realCropRect.getWidth();
47        let offHeight = realCropRect.getHeight();
48        offWidth = (offWidth > 0) ? offWidth : 1;
49        offHeight = (offHeight > 0) ? offHeight : 1;
50        let setting = new RenderingContextSettings(true);
51        let offCtx = new OffscreenCanvasRenderingContext2D(offWidth, offHeight, setting);
52        let tX = this.isFlipHorizontal ? -1 : 1;
53        let tY = this.isFlipVertically ? -1 : 1;
54
55        offCtx.save();
56        offCtx.translate(this.isFlipHorizontal ? offWidth : 0, this.isFlipVertically ? offHeight : 0);
57        offCtx.scale(tX, tY);
58
59        offCtx.translate(offWidth / 2, offHeight / 2);
60        offCtx.rotate(MathUtils.formulaAngle(this.rotationAngle * tX * tY + this.horizontalAngle));
61        offCtx.translate(-offWidth / 2, -offHeight / 2);
62        offCtx.translate(-realCropRect.left, -realCropRect.top);
63
64        offCtx.drawImage(pixelMap.pixelMap, 0, 0, width, height);
65        offCtx.restore();
66        let outputPixelMap = offCtx.getPixelMap(0, 0, offWidth, offHeight);
67        let output = new PixelMapWrapper(outputPixelMap, offWidth, offHeight);
68        Log.debug(this.TAG, `render output wrapper: ${JSON.stringify(output)}`);
69        return output;
70    }
71
72    setFlipHorizontal(isFlip: boolean) {
73        this.isFlipHorizontal = isFlip;
74    }
75
76    setFlipVertically(isFlip: boolean) {
77        this.isFlipVertically = isFlip;
78    }
79
80    setRotationAngle(angle: number) {
81        this.rotationAngle = angle;
82    }
83
84    setHorizontalAngle(angle: number) {
85        this.horizontalAngle = angle;
86    }
87
88    setCropRect(rect: RectF) {
89        this.cropRect.set(rect.left, rect.top, rect.right, rect.bottom);
90    }
91}