diff --git a/entry/src/main/ets/MainAbility/glide/transform/BlurTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/BlurTransformation.ets index 22337b5..f8bf166 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/BlurTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/BlurTransformation.ets @@ -13,11 +13,11 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" -import {TransformUtils} from "../transform/TransformUtils" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" +import {TransformUtils} from "../transform/TransformUtils.ets" import image from "@ohos.multimedia.image" import {fastBlur} from "../utils/FastBlur" diff --git a/entry/src/main/ets/MainAbility/glide/transform/BrightnessFilterTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/BrightnessFilterTransformation.ets index 3c8d9a5..747965c 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/BrightnessFilterTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/BrightnessFilterTransformation.ets @@ -13,11 +13,10 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" -import {TransformUtils} from "../transform/TransformUtils" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" import image from "@ohos.multimedia.image" @@ -74,58 +73,24 @@ export class BrightnessFilterTransformation implements BaseTransform { } } let data = await imageSource.createPixelMap(options); - let readPromise; - let writePromise; - for (let w = 0;w <= targetWidth; w++) { - for (let h = 0;h <= targetHeight; h++) { - var buffer = new ArrayBuffer(5); + let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); + await data.readPixelsToBuffer(bufferData); - readPromise = new Promise((resolve, reject) => { - var positionRen = { - pixels: buffer, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - data.readPixels(positionRen, () => { - resolve(); - }); - }); - await readPromise; + var dataArray = new Uint8Array(bufferData); - - writePromise = new Promise((resolve, reject) => { - var bytes = new Uint8Array(buffer); - var buffer1B = new ArrayBuffer(5); - var bytes1B = new Uint8Array(buffer1B); - var writePositionRenB = { - pixels: buffer1B, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - bytes1B[0] = bytes[0]; - bytes1B[1] = this.checkVisAble(bytes[1] * this._mBrightness + bytes[1]); - bytes1B[2] = this.checkVisAble(bytes[2] * this._mBrightness + bytes[2]); - bytes1B[3] = this.checkVisAble(bytes[3] * this._mBrightness + bytes[3]); - bytes1B[4] = bytes[4]; - data.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; - } + for (let index = 0; index < dataArray.length; index += 4) { + dataArray[index] = this.checkVisAble(dataArray[index] * this._mBrightness + dataArray[index]); + dataArray[index+1] = this.checkVisAble(dataArray[index+1] * this._mBrightness + dataArray[index+1]); + dataArray[index+2] = this.checkVisAble(dataArray[index+2] * this._mBrightness + dataArray[index+2]); + dataArray[index+3] = this.checkVisAble(dataArray[index+3] * this._mBrightness + dataArray[index+3]); } - func("", data); + await data.writeBufferToPixels(bufferData); + if (func) { + func("", data); + } } private checkVisAble(input: number) { diff --git a/entry/src/main/ets/MainAbility/glide/transform/ContrastFilterTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/ContrastFilterTransformation.ets index acf3b77..be60894 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/ContrastFilterTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/ContrastFilterTransformation.ets @@ -13,10 +13,10 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" import image from "@ohos.multimedia.image" @@ -86,58 +86,24 @@ export class ContrastFilterTransformation implements BaseTransform { } let data = await imageSource.createPixelMap(options); - let readPromise; - let writePromise; - for (let w = 0;w <= targetWidth; w++) { - for (let h = 0;h <= targetHeight; h++) { - var buffer = new ArrayBuffer(5); - readPromise = new Promise((resolve, reject) => { - var positionRen = { - pixels: buffer, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - data.readPixels(positionRen, () => { - resolve(); - }); - }); - await readPromise; + let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); + await data.readPixelsToBuffer(bufferData); - writePromise = new Promise((resolve, reject) => { - var bytes = new Uint8Array(buffer); - var buffer1B = new ArrayBuffer(5); - var bytes1B = new Uint8Array(buffer1B); - var writePositionRenB = { - pixels: buffer1B, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - let brightness = 0; //亮度的偏移量,可以默认0 + var dataArray = new Uint8Array(bufferData); - bytes1B[0] = bytes[0]; - bytes1B[1] = this.checkVisAble((bytes[1] - 127) * this._mContrast + brightness + 127); - bytes1B[2] = this.checkVisAble((bytes[2] - 127) * this._mContrast + brightness + 127); - bytes1B[3] = this.checkVisAble((bytes[3] - 127) * this._mContrast + brightness + 127); - bytes1B[4] = bytes[4]; - data.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; - } + let brightness = 0; //亮度的偏移量,可以默认0 + for (let index = 0; index < dataArray.length; index += 4) { + dataArray[index] = this.checkVisAble((dataArray[index] - 127) * this._mContrast + brightness + 127); + dataArray[index+1] = this.checkVisAble((dataArray[index+1] - 127) * this._mContrast + brightness + 127); + dataArray[index+2] = this.checkVisAble((dataArray[index+2] - 127) * this._mContrast + brightness + 127); + dataArray[index+3] = this.checkVisAble((dataArray[index+3] - 127) * this._mContrast + brightness + 127); } - func("", data); - + await data.writeBufferToPixels(bufferData); + if (func) { + func("", data); + } } private checkVisAble(input: number) { diff --git a/entry/src/main/ets/MainAbility/glide/transform/CropCircleTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/CropCircleTransformation.ets index 4e05865..1c3e8e8 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/CropCircleTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/CropCircleTransformation.ets @@ -13,12 +13,11 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" -import {TransformUtils} from "../transform/TransformUtils" - +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" +import {TransformUtils} from "../transform/TransformUtils.ets" import image from "@ohos.multimedia.image" export class CropCircleTransformation implements BaseTransform { @@ -26,7 +25,6 @@ export class CropCircleTransformation implements BaseTransform { private mCenterX: number= 0; private mCenterY: number= 0; private mRadius: number= 0; - private mTransform_pixelMap: any; getName() { return CropCircleTransformation.TAG + ";mCenterX:" + this.mCenterX @@ -76,19 +74,18 @@ export class CropCircleTransformation implements BaseTransform { } imageSource.createPixelMap(options) .then(p => { - this.transformCircle(p, func); - }) + this.transformCircle(p, func); + }) .catch(e => { - console.error(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " transform e:" + e); - if (func) { - func(Constants.PROJECT_TAG + CropCircleTransformation.TAG + "e" + e, null); - } - }) + console.error(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " transform e:" + e); + if (func) { + func(Constants.PROJECT_TAG + CropCircleTransformation.TAG + "e" + e, null); + } + }) } - private async transformCircle(p: PixelMap, func?: AsyncTransform) { - this.mTransform_pixelMap = p; - let imageInfo = await this.mTransform_pixelMap.getImageInfo(); + private async transformCircle(data: any, func?: AsyncTransform) { + let imageInfo = await data.getImageInfo(); let size = { width: imageInfo.size.width, height: imageInfo.size.height @@ -109,37 +106,27 @@ export class CropCircleTransformation implements BaseTransform { this.mCenterX = width / 2; this.mCenterY = height / 2; - let writePromise; + let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); + await data.readPixelsToBuffer(bufferData); + + var dataArray = new Uint8Array(bufferData); for (var h = 0;h <= height; h++) { for (var w = 0;w <= width; w++) { if (this.isContainsCircle(w, h)) { continue; } - writePromise = new Promise((resolve, reject) => { - var buffer1 = new ArrayBuffer(5); - var bytes1 = new Uint8Array(buffer1); - var writePositionRenB = { - pixels: buffer1, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - for (let j = 0;j < 5; j++) { - bytes1[j] = 0; - } - this.mTransform_pixelMap.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; + //针对的点 + let index = (h * width + w) * 4; + dataArray[index] = 0; + dataArray[index+1] = 0; + dataArray[index+2] = 0; + dataArray[index+3] = 0; } } + await data.writeBufferToPixels(bufferData); if (func) { - func("", this.mTransform_pixelMap); + func("", data); } } diff --git a/entry/src/main/ets/MainAbility/glide/transform/CropCircleWithBorderTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/CropCircleWithBorderTransformation.ets index 20541eb..d6b0316 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/CropCircleWithBorderTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/CropCircleWithBorderTransformation.ets @@ -13,16 +13,15 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" -import {TransformUtils} from "../transform/TransformUtils" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" +import {TransformUtils} from "../transform/TransformUtils.ets" import image from "@ohos.multimedia.image" export class CropCircleWithBorderTransformation implements BaseTransform { private static TAG: string= "CropCircleTransformation"; - private mTransform_pixelMap: any; private mBorderSize: number= 5; private mCenterX: number= 0; private mCenterY: number= 0; @@ -104,8 +103,7 @@ export class CropCircleWithBorderTransformation implements BaseTransform) { - this.mTransform_pixelMap = pixelMap; + private async transformPixelMap(pixelMap: any, width: number, height: number, func?: AsyncTransform) { this.mRadius = 0; if (width > height) { this.mRadius = height / 2; @@ -114,8 +112,13 @@ export class CropCircleWithBorderTransformation implements BaseTransform((resolve, reject) => { - var buffer1B = new ArrayBuffer(5); - var bytes1B = new Uint8Array(buffer1B); - var writePositionRenB = { - pixels: buffer1B, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - for (let j = 0;j < 5; j++) { - bytes1B[j] = 0; - } - this.mTransform_pixelMap.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; + dataArray[index] = 0; + dataArray[index+1] = 0; + dataArray[index+2] = 0; + dataArray[index+3] = 0; } else { //设置broke - var buffer = new ArrayBuffer(5); - readPromise = new Promise((resolve, reject) => { - var positionRen = { - pixels: buffer, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - this.mTransform_pixelMap.readPixels(positionRen, () => { - resolve(); - }); - }); - await readPromise; - writePromise = new Promise((resolve, reject) => { - var bytes = new Uint8Array(buffer); - var buffer1 = new ArrayBuffer(5); - var bytes1 = new Uint8Array(buffer1); - var writePositionRen = { - pixels: buffer1, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - for (let j = 0;j < 5; j++) { - if (j == 1 && this.mBColor > 0) { - bytes1[j] = this.mBColor; - } else if (j == 2 && this.mGColor > 0) { - bytes1[j] = this.mGColor; - } else if (j == 3 && this.mRColor > 0) { - bytes1[j] = this.mRColor; - } else { - bytes1[j] = bytes[j]; - } - } - this.mTransform_pixelMap.writePixels(writePositionRen, () => { - resolve(); - }); - }) - await writePromise; + dataArray[index] = this.mRColor; + dataArray[index+1] = this.mGColor; + dataArray[index+2] = this.mBColor; } } } + + await pixelMap.writeBufferToPixels(bufferData); if (func) { - func("", this.mTransform_pixelMap); + func("", pixelMap); } } diff --git a/entry/src/main/ets/MainAbility/glide/transform/CropSquareTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/CropSquareTransformation.ets index bf6fcdf..d743ced 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/CropSquareTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/CropSquareTransformation.ets @@ -13,10 +13,10 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" import image from "@ohos.multimedia.image" diff --git a/entry/src/main/ets/MainAbility/glide/transform/CropTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/CropTransformation.ets index 4a4bffa..0c5691a 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/CropTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/CropTransformation.ets @@ -13,11 +13,11 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" -import {TransformUtils} from "../transform/TransformUtils" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" +import {TransformUtils} from "../transform/TransformUtils.ets" import image from "@ohos.multimedia.image" diff --git a/entry/src/main/ets/MainAbility/glide/transform/GrayscaleTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/GrayscaleTransformation.ets index e31885a..64108a0 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/GrayscaleTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/GrayscaleTransformation.ets @@ -13,11 +13,11 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" -import {TransformUtils} from "../transform/TransformUtils" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" +import {TransformUtils} from "../transform/TransformUtils.ets" import image from "@ohos.multimedia.image" export class GrayscaleTransformation implements BaseTransform { @@ -25,7 +25,6 @@ export class GrayscaleTransformation implements BaseTransform { return "GrayscaleTransformation:" + this; } - async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { console.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty"); @@ -65,53 +64,30 @@ export class GrayscaleTransformation implements BaseTransform { } } let data = await imageSource.createPixelMap(options); - let readPromise; - let writePromise; - for (let w = 0;w <= targetWidth; w++) { - for (let h = 0;h <= targetHeight; h++) { - var buffer = new ArrayBuffer(5); - readPromise = new Promise((resolve, reject) => { - var positionRen = { - pixels: buffer, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - data.readPixels(positionRen, () => { - resolve(); - }); - }); - await readPromise; - writePromise = new Promise((resolve, reject) => { - var bytes = new Uint8Array(buffer); - var buffer1B = new ArrayBuffer(5); - var bytes1B = new Uint8Array(buffer1B); - var writePositionRenB = { - pixels: buffer1B, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - bytes1B[0] = bytes[0]; - bytes1B[1] = this.grayscale(bytes[3], bytes[2], bytes[1]); - bytes1B[2] = this.grayscale(bytes[3], bytes[2], bytes[1]); - bytes1B[3] = this.grayscale(bytes[3], bytes[2], bytes[1]); - bytes1B[4] = bytes[4]; - data.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; - } + let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); + let bufferNewData = new ArrayBuffer(data.getPixelBytesNumber()); + await data.readPixelsToBuffer(bufferData); + + var dataArray = new Uint8Array(bufferData); + var dataNewArray = new Uint8Array(bufferNewData); + + for (let index = 0; index < dataArray.length; index += 4) { + const r = dataArray[index]; + const g = dataArray[index+1]; + const b = dataArray[index+2]; + const f = dataArray[index+3]; + //b g r + dataNewArray[index] = this.grayscale(r, g, b); + dataNewArray[index+1] = this.grayscale(r, g, b); + dataNewArray[index+2] = this.grayscale(r, g, b); + dataNewArray[index+3] = f; + } + + await data.writeBufferToPixels(bufferNewData); + if (func) { + func('', data); } - func('',data); } /** diff --git a/entry/src/main/ets/MainAbility/glide/transform/InvertFilterTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/InvertFilterTransformation.ets index 7585c8d..efad02c 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/InvertFilterTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/InvertFilterTransformation.ets @@ -13,10 +13,10 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" import image from "@ohos.multimedia.image" /** @@ -73,55 +73,21 @@ export class InvertFilterTransformation implements BaseTransform { } let data = await imageSource.createPixelMap(options); - let readPromise; - let writePromise; - for (let w = 0;w <= targetWidth; w++) { - for (let h = 0;h <= targetHeight; h++) { - var buffer = new ArrayBuffer(5); - readPromise = new Promise((resolve, reject) => { - var positionRen = { - pixels: buffer, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - data.readPixels(positionRen, () => { - resolve(); - }); - }); - await readPromise; + let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); + await data.readPixelsToBuffer(bufferData); - writePromise = new Promise((resolve, reject) => { - var bytes = new Uint8Array(buffer); + var dataArray = new Uint8Array(bufferData); - var buffer1B = new ArrayBuffer(5); - var bytes1B = new Uint8Array(buffer1B); - var writePositionRenB = { - pixels: buffer1B, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - bytes1B[0] = bytes[0]; - bytes1B[1] = this.checkVisAble(255 - bytes[1]); - bytes1B[2] = this.checkVisAble(255 - bytes[2]); - bytes1B[3] = this.checkVisAble(255 - bytes[3]); - bytes1B[4] = bytes[4]; - data.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; - } + for (let index = 0; index < dataArray.length; index += 4) { + dataArray[index] = this.checkVisAble(255 - dataArray[index]); + dataArray[index+1] = this.checkVisAble(255 - dataArray[index+1]); + dataArray[index+2] = this.checkVisAble(255 - dataArray[index+2]); + } + await data.writeBufferToPixels(bufferData); + if (func) { + func('', data); } - func("", data); } private checkVisAble(input: number) { diff --git a/entry/src/main/ets/MainAbility/glide/transform/MaskTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/MaskTransformation.ets new file mode 100644 index 0000000..1e1e0de --- /dev/null +++ b/entry/src/main/ets/MainAbility/glide/transform/MaskTransformation.ets @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" +import {MaskUtils} from "../utils/MaskUtils.ets" +import image from "@ohos.multimedia.image" +import resmgr from "@ohos.resourceManager" + +export class MaskTransformation implements BaseTransform { + private _mResourceData: Resource; + + constructor(maskBitmap: Resource) { + this._mResourceData = maskBitmap; + } + + getName() { + return "MaskTransformation:" + this._mResourceData; + } + + async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { + if (!buf || buf.byteLength <= 0) { + console.log(Constants.PROJECT_TAG + ";MaskTransformation buf is empty"); + if (func) { + func(Constants.PROJECT_TAG + ";MaskTransformation buf is empty", null); + } + return; + } + var imageSource = image.createImageSource(buf as any); + + let imageInfo = await imageSource.getImageInfo(); + let size = { + width: imageInfo.size.width, + height: imageInfo.size.height + } + + if (!size) { + func(new Error("MaskTransformation The image size does not exist."), null) + return; + } + var pixelMapWidth = size.width; + var pixelMapHeight = size.height; + var targetWidth = request.size.width; + var targetHeight = request.size.height; + if (pixelMapWidth < targetWidth) { + targetWidth = pixelMapWidth; + } + if (pixelMapHeight < targetHeight) { + targetHeight = pixelMapHeight; + } + + var options = { + editable: true, + desiredSize: { + width: targetWidth, + height: targetHeight + } + } + imageSource.createPixelMap(options) + .then(data => { + this.openInternal(data, targetWidth, targetHeight, func) + }) + .catch(e => { + func(e, null); + }) + } + + private openInternal(bitmap: any, width: number, height: number, func: AsyncTransform) { + if (!this._mResourceData) { + throw new Error("MaskTransformation resource is empty"); + } + resmgr.getResourceManager() + .then(result => { + result.getMedia(this._mResourceData + .id) + .then(array => { + let buffer = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset); + var imageSource = image.createImageSource(buffer as any); + var options = { + editable: true, + desiredSize: { + width: width, + height: height + } + } + imageSource.createPixelMap(options) + .then(maskBitmap => { + MaskUtils.mask(bitmap, maskBitmap, func) + }) + }) + .catch(err => { + func("MaskTransformation openInternal error" + err, null); + }) + }) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/glide/transform/PixelationFilterTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/PixelationFilterTransformation.ets index db482d3..1bd04fa 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/PixelationFilterTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/PixelationFilterTransformation.ets @@ -13,11 +13,11 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" -import {TransformUtils} from "../transform/TransformUtils" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" +import {TransformUtils} from "../transform/TransformUtils.ets" import image from "@ohos.multimedia.image" import {pixelUtils} from "../utils/PixelUtils" diff --git a/entry/src/main/ets/MainAbility/glide/transform/RotateImageTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/RotateImageTransformation.ets index 0f53c92..c297854 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/RotateImageTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/RotateImageTransformation.ets @@ -12,11 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" -import {TransformUtils} from "../transform/TransformUtils" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" +import {TransformUtils} from "../transform/TransformUtils.ets" import image from "@ohos.multimedia.image" diff --git a/entry/src/main/ets/MainAbility/glide/transform/RoundedCornersTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/RoundedCornersTransformation.ets index e7c36c7..d183728 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/RoundedCornersTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/RoundedCornersTransformation.ets @@ -12,12 +12,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {ArcPoint} from "../entry/ArcPoint" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" -import {TransformUtils} from "../transform/TransformUtils" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {ArcPoint} from "../entry/ArcPoint.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" +import {TransformUtils} from "../transform/TransformUtils.ets" import image from "@ohos.multimedia.image" diff --git a/entry/src/main/ets/MainAbility/glide/transform/SepiaFilterTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/SepiaFilterTransformation.ets index 7492726..c5c16dd 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/SepiaFilterTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/SepiaFilterTransformation.ets @@ -13,10 +13,10 @@ * limitations under the License. */ -import {BaseTransform} from "../transform/BaseTransform" -import {AsyncTransform} from "../transform/AsyncTransform" -import {Constants} from "../constants/Constants" -import {RequestOption} from "../../glide/RequestOption" +import {BaseTransform} from "../transform/BaseTransform.ets" +import {AsyncTransform} from "../transform/AsyncTransform.ets" +import {Constants} from "../constants/Constants.ets" +import {RequestOption} from "../../glide/RequestOption.ets" import image from "@ohos.multimedia.image" /** @@ -69,61 +69,36 @@ export class SepiaFilterTransformation implements BaseTransform { } } let data = await imageSource.createPixelMap(options); - let readPromise; - let writePromise; - for (let w = 0;w <= targetWidth; w++) { - for (let h = 0;h <= targetHeight; h++) { - var buffer = new ArrayBuffer(5); - readPromise = new Promise((resolve, reject) => { - var positionRen = { - pixels: buffer, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - data.readPixels(positionRen, () => { - resolve(); - }); - }); - await readPromise; - writePromise = new Promise((resolve, reject) => { - var bytes = new Uint8Array(buffer); - var buffer1B = new ArrayBuffer(5); - var bytes1B = new Uint8Array(buffer1B); + let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); + let bufferNewData = new ArrayBuffer(data.getPixelBytesNumber()); + await data.readPixelsToBuffer(bufferData); - var writePositionRenB = { - pixels: buffer1B, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - bytes1B[0] = bytes[0]; - bytes1B[1] = this.checkVisAble(this.colorBlend(this.noise() - , (bytes[3] * 0.272) + (bytes[2] * 0.534) + (bytes[1] * 0.131) - , bytes[1])); - bytes1B[2] = this.checkVisAble(this.colorBlend(this.noise() - , (bytes[3] * 0.349) + (bytes[2] * 0.686) + (bytes[1] * 0.168) - , bytes[2])); - bytes1B[3] = this.checkVisAble(this.colorBlend(this.noise() - , (bytes[3] * 0.393) + (bytes[2] * 0.769) + (bytes[1] * 0.189) - , bytes[3])); - bytes1B[4] = bytes[4]; + var dataArray = new Uint8Array(bufferData); + var dataNewArray = new Uint8Array(bufferNewData); - data.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; - } + for (let index = 0; index < dataArray.length; index += 4) { + const r = dataArray[index]; + const g = dataArray[index+1]; + const b = dataArray[index+2]; + const f = dataArray[index+3]; + + dataNewArray[index+2] = this.checkVisAble(this.colorBlend(this.noise() + , (r * 0.272) + (g * 0.534) + (b * 0.131) + , b)); + dataNewArray[index+1] = this.checkVisAble(this.colorBlend(this.noise() + , (r * 0.349) + (g * 0.686) + (b * 0.168) + , g)); + dataNewArray[index] = this.checkVisAble(this.colorBlend(this.noise() + , (r * 0.393) + (g * 0.769) + (b * 0.189) + , r)); + dataNewArray[index+3] = f; + } + + await data.writeBufferToPixels(bufferNewData); + if (func) { + func("", data); } - func("", data); } private checkVisAble(input: number) { diff --git a/entry/src/main/ets/MainAbility/glide/transform/SketchFilterTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/SketchFilterTransformation.ets index 824ce93..c561638 100644 --- a/entry/src/main/ets/MainAbility/glide/transform/SketchFilterTransformation.ets +++ b/entry/src/main/ets/MainAbility/glide/transform/SketchFilterTransformation.ets @@ -13,11 +13,11 @@ * limitations under the License. */ -import {BaseTransform} from '../transform/BaseTransform' -import {AsyncTransform} from '../transform/AsyncTransform' -import {Constants} from '../constants/Constants' -import {RequestOption} from '../../glide/RequestOption' -import {TransformUtils} from '../transform/TransformUtils' +import {BaseTransform} from '../transform/BaseTransform.ets' +import {AsyncTransform} from '../transform/AsyncTransform.ets' +import {Constants} from '../constants/Constants.ets' +import {RequestOption} from '../../glide/RequestOption.ets' +import {TransformUtils} from '../transform/TransformUtils.ets' import image from '@ohos.multimedia.image' import {CalculatePixelUtils} from '../utils/CalculatePixelUtils' diff --git a/entry/src/main/ets/MainAbility/glide/transform/SwirlFilterTransformation.ets b/entry/src/main/ets/MainAbility/glide/transform/SwirlFilterTransformation.ets new file mode 100644 index 0000000..d39ab8e --- /dev/null +++ b/entry/src/main/ets/MainAbility/glide/transform/SwirlFilterTransformation.ets @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {BaseTransform} from '../transform/BaseTransform.ets' +import {AsyncTransform} from '../transform/AsyncTransform.ets' +import {Constants} from '../constants/Constants.ets' +import {RequestOption} from '../../glide/RequestOption.ets' +import {TransformUtils} from '../transform/TransformUtils.ets' +import image from '@ohos.multimedia.image' +import {PixelEntry} from '../entry/PixelEntry' +import {ColorUtils} from '../utils/ColorUtils' +import {CalculatePixelUtils} from '../utils/CalculatePixelUtils' + +export class SwirlFilterTransformation implements BaseTransform { + private _degree: number; + + constructor(degree: number) { + this._degree = degree; + } + + getName() { + return 'SwirlFilterTransformation' + this._degree; + } + + transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { + if (!buf || buf.byteLength <= 0) { + throw new Error(Constants.PROJECT_TAG + ';SwirlFilterTransformation buf is empty'); + if (func) { + func(Constants.PROJECT_TAG + ';SwirlFilterTransformation buf is empty', null); + } + return; + } + var imageSource = image.createImageSource(buf as any); + TransformUtils.getPixelMapSize(imageSource, (error, size: { + width: number, + height: number + }) => { + if (!size) { + func(error, null) + return; + } + var pixelMapWidth = size.width; + var pixelMapHeight = size.height; + var targetWidth = request.size.width; + var targetHeight = request.size.height; + if (pixelMapWidth < targetWidth) { + targetWidth = pixelMapWidth; + } + if (pixelMapHeight < targetHeight) { + targetHeight = pixelMapHeight; + } + + var options = { + editable: true, + desiredSize: { + width: targetWidth, + height: targetHeight + } + } + imageSource.createPixelMap(options) + .then((data) => { + this.swirl(data, this._degree, func); + }) + .catch((e) => { + func(e, null); + }) + }) + } + + private async swirl(bitmap: any, degree: number, func?: AsyncTransform) { + let imageInfo = await bitmap.getImageInfo(); + let size = { + width: imageInfo.size.width, + height: imageInfo.size.height + } + if (!size) { + return; + } + let width = size.width; + let height = size.height; + let pixEntry: Array = new Array(); + + + let rgbData = CalculatePixelUtils.createInt2DArray(height, width); + + let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); + await bitmap.readPixelsToBuffer(bufferData); + let dataArray = new Uint8Array(bufferData); + + let ph = 0; + let pw = 0; + for (let index = 0; index < dataArray.length; index += 4) { + const r = dataArray[index]; + const g = dataArray[index+1]; + const b = dataArray[index+2]; + const f = dataArray[index+3]; + + let entry = new PixelEntry(); + entry.a = 0; + entry.b = b; + entry.g = g; + entry.r = r; + entry.f = f; + entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b); + pixEntry.push(entry); + rgbData[ph][pw] = ColorUtils.rgb(entry.r, entry.g, entry.b); + if (pw == width - 1) { + pw = 0; + ph++; + } else { + pw++; + } + } + + let k = degree / 3600; + let cenX = width / 2; + let cenY = height / 2; + + let radius = 0; + for (let h = 0;h < height; h++) { + for (let w = 0; w < width; w++) { + + let offerX = w - cenX; + let offerY = h - cenY; + let radian = Math.atan2(offerY, offerX) + radius = Math.floor(Math.sqrt(offerX * offerX + offerY * offerY)); + + let newX = Math.floor(radius * Math.cos(radian + k * radius)) + cenX; + let newY = Math.floor(radius * Math.sin(radian + k * radius)) + cenY; + + newX = Math.floor(Math.min(width - 1, Math.max(0, newX))); + newY = Math.floor(Math.min(height - 1, Math.max(0, newY))); + + rgbData[h][w] = rgbData[newY][newX]; + } + } + + + let bufferNewData = new ArrayBuffer(bitmap.getPixelBytesNumber()); + let dataNewArray = new Uint8Array(bufferNewData); + let index = 0; + let mh = 0; + let nw = 0; + + for (let i = 0; i < dataNewArray.length; i += 4) { + let pixel_1 = rgbData[mh][nw]; + + if (nw == width - 1) { + nw = 0; + mh++; + } else { + nw++; + } + + let p_r = ColorUtils.red(pixel_1); + let p_g = ColorUtils.green(pixel_1); + let p_b = ColorUtils.blue(pixel_1); + + dataNewArray[i] = p_r; + dataNewArray[i+1] = p_g; + dataNewArray[i+2] = p_b; + dataNewArray[i+3] = pixEntry[index].f; + index++; + } + await bitmap.writeBufferToPixels(bufferNewData); + if (func) { + func("", bitmap); + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/glide/utils/CalculatePixelUtils.ets b/entry/src/main/ets/MainAbility/glide/utils/CalculatePixelUtils.ets index 2ee2de2..af2b244 100644 --- a/entry/src/main/ets/MainAbility/glide/utils/CalculatePixelUtils.ets +++ b/entry/src/main/ets/MainAbility/glide/utils/CalculatePixelUtils.ets @@ -20,41 +20,29 @@ export namespace CalculatePixelUtils { var pixEntry: Array = new Array() var pixSrc: Array = new Array() var pixNvt: Array = new Array() - let readPromise; - let writePromise; - for (let h = 0; h < height; h++) { - for (let w = 0; w < width; w++) { - let buffer = new ArrayBuffer(5); - readPromise = new Promise((resolve, reject) => { - var positionRen = { - pixels: buffer, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: w, - y: h - } - } - p.readPixels(positionRen, () => { - resolve(); - }); - }); - await readPromise; + let bufferData = new ArrayBuffer(p.getPixelBytesNumber()); + await p.readPixelsToBuffer(bufferData); + let dataArray = new Uint8Array(bufferData); - let bytes = new Uint8Array(buffer); - let entry = new PixelEntry(); - entry.a = bytes[0]; - entry.b = bytes[1]; - entry.g = bytes[2]; - entry.r = bytes[3]; - entry.f = bytes[4]; - entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b); - pixEntry.push(entry); - pixSrc.push(ColorUtils.rgb(entry.r, entry.g, entry.b)); - pixNvt.push(0); - } + for (let index = 0; index < dataArray.length; index++) { + const r = dataArray[index]; + const g = dataArray[index+1]; + const b = dataArray[index+2]; + const f = dataArray[index+3]; + + let entry = new PixelEntry(); + entry.a = 0; + entry.b = b; + entry.g = g; + entry.r = r; + entry.f = f; + entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b); + pixEntry.push(entry); + pixSrc.push(ColorUtils.rgb(entry.r, entry.g, entry.b)); + pixNvt.push(0); } + for (row = 0; row < height; row++) { for (col = 0; col < width; col++) { pos = row * width + col; @@ -74,36 +62,23 @@ export namespace CalculatePixelUtils { pixSrc[pos] = ColorUtils.rgb(clr, clr, clr); } } - for (let m = 0;m < height; m++) { //y - for (let n = 0;n < width; n++) { //x - writePromise = new Promise((resolve, reject) => { - let buffer1B = new ArrayBuffer(5); - let bytes1B = new Uint8Array(buffer1B); - var writePositionRenB = { - pixels: buffer1B, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: n, - y: m - } - } - let index = m * width + n; - bytes1B[0] = pixEntry[m * width + n].a; - bytes1B[1] = ColorUtils.blue(pixSrc[index]); - bytes1B[2] = ColorUtils.green(pixSrc[index]); - bytes1B[3] = ColorUtils.red(pixSrc[index]); - bytes1B[4] = pixEntry[m * width + n].f; - p.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; - } + let bufferNewData = new ArrayBuffer(p.getPixelBytesNumber()); + let dataNewArray = new Uint8Array(bufferNewData); + let index = 0; + + for (let i = 0; i < dataNewArray.length; i += 4) { + dataNewArray[i] = ColorUtils.red(pixSrc[index]); + dataNewArray[i+1] = ColorUtils.green(pixSrc[index]); + dataNewArray[i+2] = ColorUtils.blue(pixSrc[index]); + dataNewArray[i+3] = pixEntry[index].f; + index++; } - func("success", p); + await p.writeBufferToPixels(bufferNewData); + if (func) { + func("success", p); + } } function gaussGray(psrc: Array, horz: number, vert: number, diff --git a/entry/src/main/ets/MainAbility/glide/utils/FastBlur.ets b/entry/src/main/ets/MainAbility/glide/utils/FastBlur.ets index d690091..8a19b3a 100644 --- a/entry/src/main/ets/MainAbility/glide/utils/FastBlur.ets +++ b/entry/src/main/ets/MainAbility/glide/utils/FastBlur.ets @@ -13,7 +13,6 @@ * limitations under the License. */ -import image from "@ohos.multimedia.image" import {CalculatePixelUtils} from "./CalculatePixelUtils" import {PixelEntry} from "../entry/PixelEntry" import {AsyncTransform} from "../transform/AsyncTransform" @@ -73,42 +72,28 @@ export namespace fastBlur { var pixEntry: Array = new Array() var pix: Array = new Array() - let readPromise; - let writePromise; - for (let ph = 0; ph < h; ph++) { - for (let pw = 0; pw < w; pw++) { - let buffer = new ArrayBuffer(5); - readPromise = new Promise((resolve, reject) => { - var positionRen = { - pixels: buffer, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: pw, - y: ph - } - } - bitmap.readPixels(positionRen, () => { - resolve(); - }); - }); - await readPromise; + let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); + await bitmap.readPixelsToBuffer(bufferData); + let dataArray = new Uint8Array(bufferData); - let bytes = new Uint8Array(buffer); - let entry = new PixelEntry(); - entry.a = bytes[0]; - entry.b = bytes[1]; - entry.g = bytes[2]; - entry.r = bytes[3]; - entry.f = bytes[4]; - entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b); - pixEntry.push(entry); - pix.push(ColorUtils.rgb(entry.r, entry.g, entry.b)); - } + for (let index = 0; index < dataArray.length; index++) { + const r = dataArray[index]; + const g = dataArray[index+1]; + const b = dataArray[index+2]; + const f = dataArray[index+3]; + + let entry = new PixelEntry(); + entry.a = 0; + entry.b = b; + entry.g = g; + entry.r = r; + entry.f = f; + entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b); + pixEntry.push(entry); + pix.push(ColorUtils.rgb(entry.r, entry.g, entry.b)); } - let wm = w - 1; let hm = h - 1; let wh = w * h; @@ -288,33 +273,21 @@ export namespace fastBlur { } } - for (let m = 0;m < h; m++) { //y - for (let n = 0;n < w; n++) { //x - writePromise = new Promise((resolve, reject) => { - let buffer1B = new ArrayBuffer(5); - let bytes1B = new Uint8Array(buffer1B); - var writePositionRenB = { - pixels: buffer1B, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: n, - y: m - } - } - let index = m * w + n; - bytes1B[0] = pixEntry[m * w + n].a; - bytes1B[1] = ColorUtils.blue(pix[index]); - bytes1B[2] = ColorUtils.green(pix[index]); - bytes1B[3] = ColorUtils.red(pix[index]); - bytes1B[4] = pixEntry[m * w + n].f; - bitmap.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; - } + + let bufferNewData = new ArrayBuffer(bitmap.getPixelBytesNumber()); + let dataNewArray = new Uint8Array(bufferNewData); + let index = 0; + + for (let i = 0; i < dataNewArray.length; i += 4) { + dataNewArray[i] = ColorUtils.red(pix[index]); + dataNewArray[i+1] = ColorUtils.green(pix[index]); + dataNewArray[i+2] = ColorUtils.blue(pix[index]); + dataNewArray[i+3] = pixEntry[index].f; + index++; + } + await bitmap.writeBufferToPixels(bufferNewData); + if (func) { + func("success", bitmap); } - func("success", bitmap); } } diff --git a/entry/src/main/ets/MainAbility/glide/utils/MaskUtils.ets b/entry/src/main/ets/MainAbility/glide/utils/MaskUtils.ets new file mode 100644 index 0000000..6e3f46e --- /dev/null +++ b/entry/src/main/ets/MainAbility/glide/utils/MaskUtils.ets @@ -0,0 +1,130 @@ +import {PixelEntry} from "../entry/PixelEntry" +import {AsyncTransform} from "../transform/AsyncTransform" +import {ColorUtils} from "./ColorUtils" +import {CalculatePixelUtils} from "./CalculatePixelUtils" + +export class MaskUtils { + static async mask(bitmap: any, maskBitmap: any, func?: AsyncTransform) { + let imageInfo = await bitmap.getImageInfo(); + let size = { + width: imageInfo.size.width, + height: imageInfo.size.height + } + if (!size) { + return; + } + let width = size.width; + let height = size.height; + + let rgbData = CalculatePixelUtils.createInt2DArray(height, width); + + let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); + await bitmap.readPixelsToBuffer(bufferData); + let dataArray = new Uint8Array(bufferData); + + let ph = 0; + let pw = 0; + for (let index = 0; index < dataArray.length; index += 4) { + const r = dataArray[index]; + const g = dataArray[index+1]; + const b = dataArray[index+2]; + const f = dataArray[index+3]; + + let entry = new PixelEntry(); + entry.a = 0; + entry.b = b; + entry.g = g; + entry.r = r; + entry.f = f; + entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b); + rgbData[ph][pw] = ColorUtils.rgb(entry.r, entry.g, entry.b); + if (pw == width - 1) { + pw = 0; + ph++; + } else { + pw++; + } + } + + + let imageInfoMask = await maskBitmap.getImageInfo(); + let sizeMask = { + width: imageInfoMask.size.width, + height: imageInfoMask.size.height + } + if (!sizeMask) { + return; + } + let widthMask = sizeMask.width; + let heightMask = sizeMask.height; + let rgbDataMask = CalculatePixelUtils.createInt2DArray(heightMask, widthMask); + let pixEntry: Array = new Array(); + + let bufferData_m = new ArrayBuffer(maskBitmap.getPixelBytesNumber()); + await maskBitmap.readPixelsToBuffer(bufferData_m); + let dataArray_m = new Uint8Array(bufferData_m); + + let ph_m = 0; + let pw_m = 0; + + for (let index = 0; index < dataArray_m.length; index += 4) { + const r = bufferData_m[index]; + const g = bufferData_m[index+1]; + const b = bufferData_m[index+2]; + const f = bufferData_m[index+3]; + + let entry = new PixelEntry(); + entry.a = 0; + entry.b = b; + entry.g = g; + entry.r = r; + entry.f = f; + entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b); + pixEntry.push(entry); + if (entry.r == 0 && entry.g == 0 && entry.r == b) { + rgbDataMask[ph_m][pw_m] = rgbData[ph_m][pw_m]; + } else { + rgbDataMask[ph_m][pw_m] = ColorUtils.rgb(entry.r, entry.g, entry.b); + } + + if (pw_m == widthMask - 1) { + pw_m = 0; + ph_m++; + } else { + pw_m++; + } + } + + + let bufferNewData = new ArrayBuffer(maskBitmap.getPixelBytesNumber()); + let dataNewArray = new Uint8Array(bufferNewData); + let index = 0; + let mh = 0; + let nw = 0; + + for (let i = 0; i < dataNewArray.length; i += 4) { + let pixel_1 = rgbDataMask[mh][nw]; + + if (nw == widthMask - 1) { + nw = 0; + mh++; + } else { + nw++; + } + + let p_r = ColorUtils.red(pixel_1); + let p_g = ColorUtils.green(pixel_1); + let p_b = ColorUtils.blue(pixel_1); + + dataNewArray[i] = p_r; + dataNewArray[i+1] = p_g; + dataNewArray[i+2] = p_b; + dataNewArray[i+3] = pixEntry[index].f; + index++; + } + await maskBitmap.writeBufferToPixels(bufferNewData); + if (func) { + func("", maskBitmap); + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/glide/utils/PixelUtils.ets b/entry/src/main/ets/MainAbility/glide/utils/PixelUtils.ets index 0cc14de..73c8d72 100644 --- a/entry/src/main/ets/MainAbility/glide/utils/PixelUtils.ets +++ b/entry/src/main/ets/MainAbility/glide/utils/PixelUtils.ets @@ -36,39 +36,38 @@ export namespace pixelUtils { var pixEntry: Array = new Array() let inPixels: Array> = CalculatePixelUtils.createInt2DArray(targetHeight, targetWidth); - let readPromise; - let writePromise; - //读取 - for (let ph = 0; ph < targetHeight; ph++) { - for (let pw = 0; pw < targetWidth; pw++) { - let buffer = new ArrayBuffer(5); - readPromise = new Promise((resolve, reject) => { - var positionRen = { - pixels: buffer, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: pw, - y: ph - } - } - bitmap.readPixels(positionRen, () => { - resolve(); - }); - }); - await readPromise; - let bytes = new Uint8Array(buffer); - let entry = new PixelEntry(); - entry.a = bytes[0]; - entry.b = bytes[1]; - entry.g = bytes[2]; - entry.r = bytes[3]; - entry.f = bytes[4]; - entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b); - pixEntry.push(entry); - inPixels[ph][pw] = ColorUtils.rgb(entry.r, entry.g, entry.b); + + let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); + await bitmap.readPixelsToBuffer(bufferData); + let dataArray = new Uint8Array(bufferData); + + let ph = 0; + let pw = 0; + + + for (let index = 0; index < dataArray.length; index++) { + const r = dataArray[index]; + const g = dataArray[index+1]; + const b = dataArray[index+2]; + const f = dataArray[index+3]; + + let entry = new PixelEntry(); + entry.a = 0; + entry.b = b; + entry.g = g; + entry.r = r; + entry.f = f; + entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b); + pixEntry.push(entry); + inPixels[ph][pw] = ColorUtils.rgb(entry.r, entry.g, entry.b); + if (pw == targetWidth - 1) { + pw = 0; + ph++; + } else { + pw++; } } + var realPixel_W = pixel > targetWidth ? targetWidth : pixel; var realPixel_H = pixel > targetHeight ? targetHeight : pixel; @@ -99,38 +98,35 @@ export namespace pixelUtils { } - for (let m = 0;m < targetHeight; m++) { //y - for (let n = 0;n < targetWidth; n++) { //x - writePromise = new Promise((resolve, reject) => { - let buffer1B = new ArrayBuffer(5); - let bytes1B = new Uint8Array(buffer1B); - let writePositionRenB = { - pixels: buffer1B, - offset: 1, - stride: 1024, - region: { size: { width: 1, height: 1 }, - x: n, - y: m - } - } - let index = m * targetWidth + n; - let pxix1 = inPixels[m][n]; - let p_b = ColorUtils.blue(pxix1); - let p_g = ColorUtils.green(pxix1); - let p_r = ColorUtils.red(pxix1); + let bufferNewData = new ArrayBuffer(bitmap.getPixelBytesNumber()); + let dataNewArray = new Uint8Array(bufferNewData); + let index = 0; + let mh = 0; + let nw = 0; - bytes1B[0] = pixEntry[index].a; - bytes1B[1] = p_b; - bytes1B[2] = p_g; - bytes1B[3] = p_r; - bytes1B[4] = pixEntry[index].f; - bitmap.writePixels(writePositionRenB, () => { - resolve(); - }); - }) - await writePromise; + for (let i = 0; i < dataNewArray.length; i += 4) { + let pixel_1 = inPixels[mh][nw]; + + if (nw == targetWidth - 1) { + nw = 0; + mh++; + } else { + nw++; } + + let p_r = ColorUtils.red(pixel_1); + let p_g = ColorUtils.green(pixel_1); + let p_b = ColorUtils.blue(pixel_1); + + dataNewArray[i] = p_r; + dataNewArray[i+1] = p_g; + dataNewArray[i+2] = p_b; + dataNewArray[i+3] = pixEntry[index].f; + index++; + } + await bitmap.writeBufferToPixels(bufferNewData); + if (func) { + func("success", bitmap); } - func("success", bitmap); } } \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/pages/TransformPixelMapPage.ets b/entry/src/main/ets/MainAbility/pages/TransformPixelMapPage.ets index b547aa6..bde3a27 100644 --- a/entry/src/main/ets/MainAbility/pages/TransformPixelMapPage.ets +++ b/entry/src/main/ets/MainAbility/pages/TransformPixelMapPage.ets @@ -12,13 +12,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import {RequestOption} from "../glide/RequestOption" -import {CropCircleTransformation} from "../glide/transform/CropCircleTransformation" -import {RoundedCornersTransformation} from "../glide/transform/RoundedCornersTransformation" -import {CropCircleWithBorderTransformation} from "../glide/transform/CropCircleWithBorderTransformation" -import {RotateImageTransformation} from "../glide/transform/RotateImageTransformation" -import {CropSquareTransformation} from "../glide/transform/CropSquareTransformation" -import {CropTransformation} from "../glide/transform/CropTransformation" +import {RequestOption} from "../glide/RequestOption.ets" +import {CropCircleTransformation} from "../glide/transform/CropCircleTransformation.ets" +import {RoundedCornersTransformation} from "../glide/transform/RoundedCornersTransformation.ets" +import {CropCircleWithBorderTransformation} from "../glide/transform/CropCircleWithBorderTransformation.ets" +import {RotateImageTransformation} from "../glide/transform/RotateImageTransformation.ets" +import {CropSquareTransformation} from "../glide/transform/CropSquareTransformation.ets" +import {CropTransformation} from "../glide/transform/CropTransformation.ets" import {CropType} from "../glide/transform/CropTransformation" import {GrayscaleTransformation} from "../glide/transform/GrayscaleTransformation" import {BrightnessFilterTransformation} from "../glide/transform/BrightnessFilterTransformation" @@ -28,14 +28,16 @@ import {SepiaFilterTransformation} from "../glide/transform/SepiaFilterTransform import {SketchFilterTransformation} from "../glide/transform/SketchFilterTransformation" import {BlurTransformation} from "../glide/transform/BlurTransformation" import {PixelationFilterTransformation} from "../glide/transform/PixelationFilterTransformation" -import {PixelMapPack} from "../glide/PixelMapPack"; +import {MaskTransformation} from "../glide/transform/MaskTransformation" +import {SwirlFilterTransformation} from "../glide/transform/SwirlFilterTransformation" +import {PixelMapPack} from "../glide/PixelMapPack.ets"; /** * PixelMap transform 示例 */ let mRotate: number = 0; //let mUrl = "https://hbimg.huabanimg.com/cc6af25f8d782d3cf3122bef4e61571378271145735e9-vEVggB" -let mUrl = $r('app.media.transformBase'); +let mUrl = $r('app.media.check_big'); @Entry @Component @@ -58,6 +60,8 @@ struct TransformPixelMapPage { @State mSketchPixelMap: PixelMapPack= new PixelMapPack(); @State mBlurPixelMap: PixelMapPack= new PixelMapPack(); @State mPixelPixelMap: PixelMapPack= new PixelMapPack(); + @State mSwirlPixelMap: PixelMapPack= new PixelMapPack(); + @State mMaskPixelMap: PixelMapPack= new PixelMapPack(); build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { @@ -165,9 +169,8 @@ struct TransformPixelMapPage { this.circleTransformation(); }); Image(this.mCirclePixelMap.pixelMap) - .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -184,9 +187,8 @@ struct TransformPixelMapPage { this.circleBorderTransformation(5); }); Image(this.mCircleBorderPixelMap.pixelMap) - .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -207,9 +209,8 @@ struct TransformPixelMapPage { this.transformRotate(mRotate); }); Image(this.mRotatePixelMap.pixelMap) - .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -227,8 +228,8 @@ struct TransformPixelMapPage { }); Image(this.mSquarePixelMap.pixelMap) .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -242,7 +243,7 @@ struct TransformPixelMapPage { .height(35) .width(72) .onClick(() => { - this.clipPixelMap(25,25, CropType.TOP); + this.clipPixelMap(25, 25, CropType.TOP); }); Button() { Text($r("app.string.trans_clip_center")).fontSize(13).fontColor(Color.White) @@ -250,7 +251,7 @@ struct TransformPixelMapPage { .height(35) .width(72) .onClick(() => { - this.clipPixelMap(25,25, CropType.CENTER); + this.clipPixelMap(25, 25, CropType.CENTER); }); Button() { Text($r("app.string.trans_clip_bottom")).fontSize(13).fontColor(Color.White) @@ -258,7 +259,7 @@ struct TransformPixelMapPage { .height(35) .width(72) .onClick(() => { - this.clipPixelMap(25,25, CropType.BOTTOM); + this.clipPixelMap(25, 25, CropType.BOTTOM); }); }.margin({ top: 10 }) @@ -294,8 +295,8 @@ struct TransformPixelMapPage { }); Image(this.mGrayscalePixelMap.pixelMap) .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -312,8 +313,8 @@ struct TransformPixelMapPage { }); Image(this.mBrightnessPixelMap.pixelMap) .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -330,8 +331,8 @@ struct TransformPixelMapPage { }); Image(this.mContrastPixelMap.pixelMap) .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -348,8 +349,8 @@ struct TransformPixelMapPage { }); Image(this.mInvertPixelMap.pixelMap) .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -367,8 +368,8 @@ struct TransformPixelMapPage { }); Image(this.mSepiaPixelMap.pixelMap) .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -385,8 +386,8 @@ struct TransformPixelMapPage { }); Image(this.mSketchPixelMap.pixelMap) .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -404,8 +405,8 @@ struct TransformPixelMapPage { }); Image(this.mBlurPixelMap.pixelMap) .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -424,8 +425,46 @@ struct TransformPixelMapPage { }); Image(this.mPixelPixelMap.pixelMap) .objectFit(ImageFit.Fill) - .width(100) - .height(100) + .width(200) + .height(200) + .margin({ top: 10 }) + + }.margin({ top: 10 }); + + Column() { + Text("SwirlFilterTransformation").fontColor(Color.Gray).fontSize(16); + Button() { + Text("图片Swirl").fontSize(13).fontColor(Color.White) + } + .height(35) + .width(120) + .margin({ top: 10 }) + .onClick(() => { + this.swirlHandlePixelMap(); + }); + Image(this.mSwirlPixelMap.pixelMap) + .objectFit(ImageFit.Fill) + .width(200) + .height(200) + .margin({ top: 10 }) + + }.margin({ top: 10 }); + + Column() { + Text("MaskTransformation").fontColor(Color.Gray).fontSize(16); + Button() { + Text("图片mask").fontSize(13).fontColor(Color.White) + } + .height(35) + .width(120) + .margin({ top: 10 }) + .onClick(() => { + this.maskHandlePixelMap($r('app.media.mask_starfish')); + }); + Image(this.mMaskPixelMap.pixelMap) + .objectFit(ImageFit.Fill) + .width(200) + .height(200) .margin({ top: 10 }) }.margin({ top: 10 }); @@ -440,13 +479,13 @@ struct TransformPixelMapPage { } - /** +/** * centerCrop */ centerCrop() { var glideOption = new RequestOption(); glideOption.load($r('app.media.photo5')) - // glideOption.load(mUrl) + // glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); this.mCropPixelMap = result; @@ -454,7 +493,7 @@ struct TransformPixelMapPage { let result2 = new PixelMapPack(); result2.pixelMap = data.glideValue as PixelMap; this.mCropPixelMap = result2; - },100) + }, 100) return false; }) .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) @@ -463,13 +502,13 @@ struct TransformPixelMapPage { Glide.call(glideOption); } - /** +/** * centerInside */ centerInside() { var glideOption = new RequestOption(); glideOption.load($r('app.media.Back')) - //glideOption.load(mUrl) + //glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); this.mCropPixelMap = result; @@ -477,7 +516,7 @@ struct TransformPixelMapPage { let result2 = new PixelMapPack(); result2.pixelMap = data.glideValue as PixelMap; this.mCropPixelMap = result2; - },100) + }, 100) return false; }) .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) @@ -486,13 +525,13 @@ struct TransformPixelMapPage { Glide.call(glideOption); } - /** +/** * centerInside */ fitCenter() { var glideOption = new RequestOption() glideOption.load($r('app.media.Back')) - //glideOption.load(mUrl) + //glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); this.mCropPixelMap = result; @@ -500,7 +539,7 @@ struct TransformPixelMapPage { let result2 = new PixelMapPack(); result2.pixelMap = data.glideValue as PixelMap; this.mCropPixelMap = result2; - },100) + }, 100) return false; }) .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) @@ -508,11 +547,11 @@ struct TransformPixelMapPage { .fitCenter(); Glide.call(glideOption); } - /** +/** * 圆角设置 */ roundedCornersTransformation(top_left: number, - bottom_left: number, top_right: number, bottom_right: number) { + bottom_left: number, top_right: number, bottom_right: number) { var glideOption = new RequestOption(); @@ -525,7 +564,7 @@ struct TransformPixelMapPage { let result2 = new PixelMapPack(); result2.pixelMap = data.glideValue as PixelMap; this.mRoundPixelMap = result2; - },100) + }, 100) return false; }) .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) @@ -536,7 +575,7 @@ struct TransformPixelMapPage { Glide.call(glideOption); } - /** +/** * 裁剪圆 */ circleTransformation() { @@ -545,18 +584,18 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mCirclePixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(new CropCircleTransformation()) // .diskCacheStrategy(new NONE()) Glide.call(glideOption); } - /** +/** * 圆环裁剪 */ circleBorderTransformation(border: number) { @@ -567,18 +606,18 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mCircleBorderPixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(circleTransformation) // .diskCacheStrategy(new NONE()) Glide.call(glideOption); } - /** +/** * 旋转 */ transformRotate(angled: number) { @@ -588,18 +627,18 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mRotatePixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) Glide.call(glideOption); } - /** +/** * 正方形裁剪 */ transformSquare() { @@ -609,18 +648,18 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mSquarePixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) Glide.call(glideOption); } - /** +/** * 区域裁剪 */ clipPixelMap(width: number, height: number, cropType: CropType) { @@ -631,13 +670,13 @@ struct TransformPixelMapPage { .addListener((err, data) => { let result = new PixelMapPack(); if (cropType == CropType.TOP) { - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mClipTopPixelMap = result; } else if (cropType == CropType.CENTER) { - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mClipCenterPixelMap = result; } else if (cropType == CropType.BOTTOM) { - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mClipBottomPixelMap = result; } return false; @@ -650,7 +689,7 @@ struct TransformPixelMapPage { } - /** +/** * 灰度 */ grayscalePixelMap() { @@ -660,11 +699,11 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mGrayscalePixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) @@ -672,7 +711,7 @@ struct TransformPixelMapPage { } - /** +/** *亮度b */ brightnessPixelMap(brightness: number) { @@ -682,11 +721,11 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mBrightnessPixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) @@ -694,7 +733,7 @@ struct TransformPixelMapPage { } - /** +/** *对比度 */ contrastPixelMap(contrast: number) { @@ -704,11 +743,11 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mContrastPixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) @@ -716,7 +755,7 @@ struct TransformPixelMapPage { } - /** +/** *反转处理 */ invertPixelMap() { @@ -726,11 +765,11 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mInvertPixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) @@ -738,7 +777,7 @@ struct TransformPixelMapPage { } - /** +/** *照片老旧出来(黑褐色) */ sepiaPixelMap() { @@ -748,11 +787,11 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mSepiaPixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) @@ -760,7 +799,7 @@ struct TransformPixelMapPage { } - /** +/** *素描 */ sketchPixelMap() { @@ -770,11 +809,11 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mSketchPixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) @@ -782,7 +821,7 @@ struct TransformPixelMapPage { } - /** +/** *模糊 */ blurHandlePixelMap(radius: number) { @@ -792,18 +831,18 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mBlurPixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) Glide.call(glideOption); } - /** +/** *马赛克 */ pixelHandlePixelMap(pixel: number) { @@ -813,11 +852,54 @@ struct TransformPixelMapPage { glideOption.load(mUrl) .addListener((err, data) => { let result = new PixelMapPack(); - result.pixelMap=data.glideValue as PixelMap; + result.pixelMap = data.glideValue as PixelMap; this.mPixelPixelMap = result; return false; }) - .setImageViewSize({ width: vp2px(100), height: vp2px(100) }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) + .skipMemoryCache(true) + .transform(transformation) + // .diskCacheStrategy(new NONE()) + Glide.call(glideOption); + + } + +/** + *扭曲 + */ + swirlHandlePixelMap() { + let glideOption = new RequestOption(); + var transformation = new SwirlFilterTransformation(80); + // glideOption.load($r('app.media.photo6')) + glideOption.load(mUrl) + .addListener((err, data) => { + let result = new PixelMapPack(); + result.pixelMap = data.glideValue as PixelMap; + this.mSwirlPixelMap = result; + return false; + }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) + .skipMemoryCache(true) + .transform(transformation) + // .diskCacheStrategy(new NONE()) + Glide.call(glideOption); + + } +/** + *遮罩 + */ + maskHandlePixelMap(maskResource: Resource) { + let glideOption = new RequestOption(); + var transformation = new MaskTransformation(maskResource); + // glideOption.load($r('app.media.photo6')) + glideOption.load(mUrl) + .addListener((err, data) => { + let result = new PixelMapPack(); + result.pixelMap = data.glideValue as PixelMap; + this.mMaskPixelMap = result; + return false; + }) + .setImageViewSize({ width: vp2px(200), height: vp2px(200) }) .skipMemoryCache(true) .transform(transformation) // .diskCacheStrategy(new NONE()) diff --git a/entry/src/main/resources/base/media/mask_starfish.png b/entry/src/main/resources/base/media/mask_starfish.png new file mode 100644 index 0000000..3cb4bcc Binary files /dev/null and b/entry/src/main/resources/base/media/mask_starfish.png differ