From ca0e3fe968485320bdd92d79c95964920c7a9e2c Mon Sep 17 00:00:00 2001 From: zhoulisheng1 Date: Tue, 19 Sep 2023 19:57:46 +0800 Subject: [PATCH] =?UTF-8?q?1.ArkTs=E6=95=B4=E6=94=B912=20=E6=95=B4?= =?UTF-8?q?=E6=94=B9imageknife->transform?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhoulisheng1 --- .../imageknife/transform/AsyncTransform.ets | 2 +- .../transform/BlurTransformation.ets | 30 ++--- .../BrightnessFilterTransformation.ets | 30 +++-- .../ContrastFilterTransformation.ets | 34 ++--- .../transform/CropCircleTransformation.ets | 71 +++++----- .../CropCircleWithBorderTransformation.ets | 72 +++++----- .../transform/CropSquareTransformation.ets | 38 +++--- .../transform/CropTransformation.ets | 65 +++++---- .../transform/GrayscaleTransformation.ets | 36 ++--- .../transform/InvertFilterTransformation.ets | 36 ++--- .../transform/KuwaharaFilterTransform.ets | 45 +++---- .../transform/MaskTransformation.ets | 75 ++++++----- .../PixelationFilterTransformation.ets | 31 +++-- .../transform/RotateImageTransformation.ets | 35 +++-- .../RoundedCornersTransformation.ets | 126 +++++++++--------- .../transform/SepiaFilterTransformation.ets | 46 ++++--- .../transform/SketchFilterTransformation.ets | 32 +++-- .../transform/SwirlFilterTransformation.ets | 60 ++++----- .../transform/ToonFilterTransform.ets | 44 +++--- .../imageknife/transform/TransformUtils.ets | 84 ++++++------ .../transform/VignetteFilterTransform.ets | 44 +++--- .../transform/pixelmap/CenterCrop.ets | 19 ++- .../transform/pixelmap/CenterInside.ets | 21 ++- .../transform/pixelmap/FitCenter.ets | 19 ++- .../imageknife/utils/CalculatePixelUtils.ets | 19 +-- .../components/imageknife/utils/FastBlur.ets | 59 +++++--- .../imageknife/utils/PixelUtils.ets | 30 +++-- 27 files changed, 614 insertions(+), 589 deletions(-) diff --git a/imageknife/src/main/ets/components/imageknife/transform/AsyncTransform.ets b/imageknife/src/main/ets/components/imageknife/transform/AsyncTransform.ets index a868d1f..d267bc4 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/AsyncTransform.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/AsyncTransform.ets @@ -14,5 +14,5 @@ */ import { BusinessError } from '@ohos.base' export interface AsyncTransform { - asyncTransform:(err:BusinessError|string, data: T)=>void + asyncTransform:(err:BusinessError|string, data: T | null)=>void } diff --git a/imageknife/src/main/ets/components/imageknife/transform/BlurTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/BlurTransformation.ets index 70a479c..b309333 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/BlurTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/BlurTransformation.ets @@ -21,7 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils" import image from "@ohos.multimedia.image" import { fastBlur } from "../utils/FastBlur" import {LogUtil} from '../../imageknife/utils/LogUtil' - +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class BlurTransformation implements BaseTransform { private _mRadius: number; @@ -38,23 +39,20 @@ export class BlurTransformation implements BaseTransform { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";BlurTransformation buf is empty"); if (func) { - func(Constants.PROJECT_TAG + ";BlurTransformation buf is empty", null); + func?.asyncTransform(Constants.PROJECT_TAG + ";BlurTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth = size.width; + let pixelMapHeight = size.height; + let targetWidth = request.size.width; + let targetHeight = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -62,7 +60,7 @@ export class BlurTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -77,10 +75,10 @@ export class BlurTransformation implements BaseTransform { fastBlur.blur(data, this._mRadius, true, func); } }) - .catch((e) => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); - func(e, null); + func?.asyncTransform(e, null); }) - }) + }}) } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/BrightnessFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/BrightnessFilterTransformation.ets index d099976..fd5d58c 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/BrightnessFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/BrightnessFilterTransformation.ets @@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption" import { LogUtil } from '../../imageknife/utils/LogUtil' import image from "@ohos.multimedia.image" import { GPUImageBrightnessFilter } from '@ohos/gpu_transform' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' /** * brightness value ranges from -1.0 to 1.0, with 0.0 as the normal level @@ -38,27 +40,27 @@ export class BrightnessFilterTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); - let imageInfo = await imageSource.getImageInfo(); - let size = { + let imageInfo:image.ImageInfo = await imageSource.getImageInfo(); + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("GrayscaleTransformation The image size does not exist."), null) + func?.asyncTransform("GrayscaleTransformation 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; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -66,7 +68,7 @@ export class BrightnessFilterTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -85,12 +87,12 @@ export class BrightnessFilterTransformation implements BaseTransform { let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); data.writeBufferToPixels(buf); if (func) { - func("success", data); + func?.asyncTransform("success", data); } return; } - var dataArray = new Uint8Array(bufferData); + let dataArray = new Uint8Array(bufferData); for (let index = 0; index < dataArray.length; index += 4) { dataArray[index] = this.checkVisAble(dataArray[index] * this._mBrightness + dataArray[index]); @@ -102,7 +104,7 @@ export class BrightnessFilterTransformation implements BaseTransform { await data.writeBufferToPixels(bufferData); if (func) { - func("", data); + func?.asyncTransform("", data); } } diff --git a/imageknife/src/main/ets/components/imageknife/transform/ContrastFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/ContrastFilterTransformation.ets index 57d46c4..11d9946 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/ContrastFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/ContrastFilterTransformation.ets @@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption" import { LogUtil } from '../../imageknife/utils/LogUtil' import image from "@ohos.multimedia.image" import { GPUImageContrastFilter } from '@ohos/gpu_transform' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' /** * 以24位色图像为例子,每种色彩都可以用0-255, @@ -50,27 +52,27 @@ export class ContrastFilterTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty", null); + if (func!=undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); - let imageInfo = await imageSource.getImageInfo(); - let size = { + let imageInfo:image.ImageInfo = await imageSource.getImageInfo(); + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("ContrastFilterTransformation The image size does not exist."), null) + func?.asyncTransform("ContrastFilterTransformation 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; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -78,7 +80,7 @@ export class ContrastFilterTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -97,13 +99,13 @@ export class ContrastFilterTransformation implements BaseTransform { filter.setContrast(this._mContrast) let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); data.writeBufferToPixels(buf); - if (func) { - func("success", data); + if (func != undefined) { + func?.asyncTransform("success", data); } return; } - var dataArray = new Uint8Array(bufferData); + let dataArray = new Uint8Array(bufferData); let brightness = 0; //亮度的偏移量,可以默认0 for (let index = 0; index < dataArray.length; index += 4) { @@ -114,8 +116,8 @@ export class ContrastFilterTransformation implements BaseTransform { } await data.writeBufferToPixels(bufferData); - if (func) { - func("", data); + if (func != undefined) { + func?.asyncTransform("", data); } } diff --git a/imageknife/src/main/ets/components/imageknife/transform/CropCircleTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/CropCircleTransformation.ets index d526c40..239810d 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/CropCircleTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/CropCircleTransformation.ets @@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption" import { TransformUtils } from "../transform/TransformUtils" import {LogUtil} from '../../imageknife/utils/LogUtil' import image from "@ohos.multimedia.image" +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class CropCircleTransformation implements BaseTransform { private static TAG: string = "CropCircleTransformation"; @@ -36,37 +38,34 @@ export class CropCircleTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty", null); + if (func!=undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); - var that = this; - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size | null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } if (pixelMapHeight < targetHeight) { targetHeight = pixelMapHeight; } - that.updatePixelMapSize(imageSource, targetWidth, targetHeight, func); - }) + this.updatePixelMapSize(imageSource, targetWidth, targetHeight, func); + }}) } - private updatePixelMapSize(imageSource: any, outWith: number, outHeight: number, func?: AsyncTransform) { - var options = { + private updatePixelMapSize(imageSource: image.ImageSource, outWith: number, outHeight: number, func?: AsyncTransform) { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: outWith, @@ -74,30 +73,30 @@ export class CropCircleTransformation implements BaseTransform { } } imageSource.createPixelMap(options) - .then(p => { + .then((p:PixelMap) => { this.transformCircle(p, func); }) - .catch(e => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " transform e:" + e); - if (func) { - func(Constants.PROJECT_TAG + CropCircleTransformation.TAG + "e" + e, null); + if (func!=undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + CropCircleTransformation.TAG + "e" + e, null); } }) } - private async transformCircle(data: any, func?: AsyncTransform) { - let imageInfo = await data.getImageInfo(); - let size = { + private async transformCircle(data: PixelMap, func?: AsyncTransform) { + let imageInfo:image.ImageInfo = await data.getImageInfo(); + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("CropCircleTransformation The image size does not exist."), null) + func?.asyncTransform("CropCircleTransformation The image size does not exist.", null) return; } - var height = size.height; - var width = size.width; + let height:number = size.height; + let width:number = size.width; this.mRadius = 0; if (width > height) { this.mRadius = height / 2; @@ -107,13 +106,13 @@ export class CropCircleTransformation implements BaseTransform { this.mCenterX = width / 2; this.mCenterY = height / 2; - let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); + let bufferData:ArrayBuffer = new ArrayBuffer(data.getPixelBytesNumber()); await data.readPixelsToBuffer(bufferData); - var dataArray = new Uint8Array(bufferData); + let dataArray = new Uint8Array(bufferData); - for (var h = 0;h <= height; h++) { - for (var w = 0;w <= width; w++) { + for (let h = 0;h <= height; h++) { + for (let w = 0;w <= width; w++) { if (this.isContainsCircle(w, h)) { continue; } @@ -126,15 +125,15 @@ export class CropCircleTransformation implements BaseTransform { } } await data.writeBufferToPixels(bufferData); - if (func) { - func("", data); + if (func != undefined) { + func?.asyncTransform("", data); } } isContainsCircle(x: number, y: number): boolean { - var a = Math.pow((this.mCenterX - x), 2); - var b = Math.pow((this.mCenterY - y), 2); - var c = Math.sqrt((a + b)); + let a = Math.pow((this.mCenterX - x), 2); + let b = Math.pow((this.mCenterY - y), 2); + let c = Math.sqrt((a + b)); return c <= this.mRadius; } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/CropCircleWithBorderTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/CropCircleWithBorderTransformation.ets index 3cd5843..183c47e 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/CropCircleWithBorderTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/CropCircleWithBorderTransformation.ets @@ -20,7 +20,13 @@ import { RequestOption } from "../../imageknife/RequestOption" import { TransformUtils } from "../transform/TransformUtils" import {LogUtil} from '../../imageknife/utils/LogUtil' import image from "@ohos.multimedia.image" - +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' +export interface rgbColor{ + r_color: number, + g_color: number, + b_color: number, +} export class CropCircleWithBorderTransformation implements BaseTransform { private static TAG: string = "CropCircleTransformation"; private mBorderSize: number = 5; @@ -31,11 +37,7 @@ export class CropCircleWithBorderTransformation implements BaseTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); - var that = this; - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size:Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth = size.width; + let pixelMapHeight = size.height; + let targetWidth = request.size.width; + let targetHeight = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } if (pixelMapHeight < targetHeight) { targetHeight = pixelMapHeight; } - that.updatePixelMapSize(imageSource, targetWidth, targetHeight, func); - }) + this.updatePixelMapSize(imageSource, targetWidth, targetHeight, func); + }}) } - private updatePixelMapSize(imageSource: any, outWith: number, outHeight: number, func?: AsyncTransform) { - var options = { + private updatePixelMapSize(imageSource: image.ImageSource, outWith: number, outHeight: number, func?: AsyncTransform) { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: outWith, @@ -93,18 +91,18 @@ export class CropCircleWithBorderTransformation implements BaseTransform { + .then((pixelMap:PixelMap) => { this.transformPixelMap(pixelMap, outWith, outHeight, func); }) - .catch(e => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e); - if (func) { - func(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e, null); + if (func!=undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e, null); } }) } - private async transformPixelMap(pixelMap: any, width: number, height: number, func?: AsyncTransform) { + private async transformPixelMap(pixelMap: PixelMap, width: number, height: number, func?: AsyncTransform) { this.mRadius = 0; if (width > height) { this.mRadius = height / 2; @@ -118,7 +116,7 @@ export class CropCircleWithBorderTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty", null); } return; } @@ -40,21 +42,21 @@ export class CropSquareTransformation implements BaseTransform { } squareCrop(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); imageSource.getImageInfo() - .then((p) => { - var pw = p.size.width; - var ph = p.size.height; - var outWidth = request.size.width; - var outHeight = request.size.height; + .then((p:image.ImageInfo) => { + let pw:number = p.size.width; + let ph:number = p.size.height; + let outWidth:number = request.size.width; + let outHeight:number = request.size.height; if (pw < outWidth) { outWidth = pw; } if (ph < outHeight) { outHeight = ph; } - var targetSize = outWidth > outHeight ? outHeight : outWidth; - var options = { + let targetSize:number = outWidth > outHeight ? outHeight : outWidth; + let options:image.DecodingOptions = { editable: true, rotate: 0, desiredSize: { @@ -68,19 +70,19 @@ export class CropSquareTransformation implements BaseTransform { } imageSource.createPixelMap(options) .then(data => { - if (func) { - func("", data); + if (func != undefined) { + func?.asyncTransform("", data); } }) - .catch(e => { - if (func) { - func(Constants.PROJECT_TAG + ";CropSquareTransformation e:" + e, null); + .catch((e:BusinessError) => { + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";CropSquareTransformation e:" + e, null); } }) }) - .catch((error) => { - if (func) { - func(Constants.PROJECT_TAG + ";CropSquareTransformation error:" + error, null); + .catch((error:BusinessError) => { + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";CropSquareTransformation error:" + error, null); } }) } diff --git a/imageknife/src/main/ets/components/imageknife/transform/CropTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/CropTransformation.ets index c8af2ce..d98f56f 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/CropTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/CropTransformation.ets @@ -20,14 +20,16 @@ import { RequestOption } from "../../imageknife/RequestOption" import { TransformUtils } from "../transform/TransformUtils" import {LogUtil} from '../../imageknife/utils/LogUtil' import image from "@ohos.multimedia.image" +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class CropTransformation implements BaseTransform { private static TAG: string = "CropCircleTransformation"; - private mWidth: number; - private mHeight: number; + private mWidth: number = 0; + private mHeight: number = 0; private mCropType: CropType = CropType.CENTER; - constructor(width: number, height: number, cropType?: CropType) { + constructor(width: number, height: number, cropType: CropType) { this.mWidth = width; this.mHeight = height; this.mCropType = cropType; @@ -42,35 +44,32 @@ export class CropTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";CropTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";CropTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";CropTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; this.mWidth = this.mWidth == 0 ? pixelMapWidth : this.mWidth; this.mHeight = this.mHeight == 0 ? pixelMapHeight : this.mHeight; - var scaleX = this.mWidth / pixelMapWidth; - var scaleY = this.mHeight / pixelMapHeight; - var scale = Math.max(scaleX, scaleY); + let scaleX:number = this.mWidth / pixelMapWidth; + let scaleY:number = this.mHeight / pixelMapHeight; + let scale:number = Math.max(scaleX, scaleY); - var scaledWidth = scale * pixelMapWidth; - var scaledHeight = scale * pixelMapHeight; - var left = (this.mWidth - scaledWidth) / 2; - var top = Math.abs(this.getTop(pixelMapHeight)); - var options = { + let scaledWidth:number = scale * pixelMapWidth; + let scaledHeight:number = scale * pixelMapHeight; + let left:number = (this.mWidth - scaledWidth) / 2; + let top:number = Math.abs(this.getTop(pixelMapHeight)); + let options:image.DecodingOptions = { editable: true, desiredRegion: { size: { @@ -82,23 +81,23 @@ export class CropTransformation implements BaseTransform { }, } imageSource.createPixelMap(options) - .then((data) => { - func("", data); + .then((data:PixelMap) => { + func?.asyncTransform("", data); }) - .catch((e) => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); - func(e, null); + func?.asyncTransform(e, null); }) - }) + }}) } private getTop(scaledHeight: number): number{ - switch (this.mCropType.valueOf()) { - case CropType.TOP.valueOf(): + switch (this.mCropType) { + case CropType.TOP: return 0; - case CropType.CENTER.valueOf(): + case CropType.CENTER: return (this.mHeight - scaledHeight) / 2; - case CropType.BOTTOM.valueOf(): + case CropType.BOTTOM: return this.mHeight - scaledHeight; default: return 0; @@ -107,7 +106,7 @@ export class CropTransformation implements BaseTransform { } export enum CropType { - TOP, - CENTER, - BOTTOM + TOP = 0, + CENTER = 1, + BOTTOM = 2 } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/GrayscaleTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/GrayscaleTransformation.ets index e092312..fc23f74 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/GrayscaleTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/GrayscaleTransformation.ets @@ -21,6 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils" import {LogUtil} from '../../imageknife/utils/LogUtil' import image from "@ohos.multimedia.image" import { GPUImageGrayscaleFilter } from '@ohos/gpu_transform' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class GrayscaleTransformation implements BaseTransform { getName() { @@ -30,27 +32,27 @@ export class GrayscaleTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); let imageInfo = await imageSource.getImageInfo(); - let size = { + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("GrayscaleTransformation The image size does not exist."), null) + func?.asyncTransform("GrayscaleTransformation 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; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -58,14 +60,14 @@ export class GrayscaleTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, height: targetHeight } } - let data = await imageSource.createPixelMap(options); + let data:PixelMap= await imageSource.createPixelMap(options); let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); let bufferNewData = new ArrayBuffer(data.getPixelBytesNumber()); @@ -76,15 +78,15 @@ export class GrayscaleTransformation implements BaseTransform { filter.setImageData(bufferData, targetWidth, targetHeight); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); data.writeBufferToPixels(buf); - if (func) { - func("success", data); + if (func != undefined) { + func?.asyncTransform("success", data); } return; } - var dataArray = new Uint8Array(bufferData); - var dataNewArray = new Uint8Array(bufferNewData); + let dataArray = new Uint8Array(bufferData); + let dataNewArray = new Uint8Array(bufferNewData); for (let index = 0; index < dataArray.length; index += 4) { const r = dataArray[index]; @@ -99,8 +101,8 @@ export class GrayscaleTransformation implements BaseTransform { } await data.writeBufferToPixels(bufferNewData); - if (func) { - func('', data); + if (func != undefined) { + func?.asyncTransform('', data); } } diff --git a/imageknife/src/main/ets/components/imageknife/transform/InvertFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/InvertFilterTransformation.ets index ffd131c..7fcefc0 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/InvertFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/InvertFilterTransformation.ets @@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption" import {LogUtil} from '../../imageknife/utils/LogUtil' import image from "@ohos.multimedia.image" import { GPUImageColorInvertFilter } from '@ohos/gpu_transform' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' /** ** Image inversion is particularly useful for enhancing white or gray detail in @@ -37,28 +39,28 @@ export class InvertFilterTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); - let imageInfo = await imageSource.getImageInfo(); - let size = { + let imageInfo:image.ImageInfo = await imageSource.getImageInfo(); + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("InvertFilterTransformation The image size does not exist."), null) + func?.asyncTransform("InvertFilterTransformation 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; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -66,7 +68,7 @@ export class InvertFilterTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -74,7 +76,7 @@ export class InvertFilterTransformation implements BaseTransform { } } - let data = await imageSource.createPixelMap(options); + let data:PixelMap = await imageSource.createPixelMap(options); let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); await data.readPixelsToBuffer(bufferData); @@ -84,14 +86,14 @@ export class InvertFilterTransformation implements BaseTransform { filter.setImageData(bufferData, targetWidth, targetHeight); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); data.writeBufferToPixels(buf); - if (func) { - func("success", data); + if (func != undefined) { + func?.asyncTransform("success", data); } return; } - var dataArray = new Uint8Array(bufferData); + let dataArray = new Uint8Array(bufferData); for (let index = 0; index < dataArray.length; index += 4) { dataArray[index] = this.checkVisAble(255 - dataArray[index]); @@ -99,8 +101,8 @@ export class InvertFilterTransformation implements BaseTransform { dataArray[index+2] = this.checkVisAble(255 - dataArray[index+2]); } await data.writeBufferToPixels(bufferData); - if (func) { - func('', data); + if (func != undefined) { + func?.asyncTransform('', data); } } diff --git a/imageknife/src/main/ets/components/imageknife/transform/KuwaharaFilterTransform.ets b/imageknife/src/main/ets/components/imageknife/transform/KuwaharaFilterTransform.ets index 5ce2725..edc89a7 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/KuwaharaFilterTransform.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/KuwaharaFilterTransform.ets @@ -21,10 +21,12 @@ import { TransformUtils } from "../transform/TransformUtils" import image from "@ohos.multimedia.image" import { LogUtil } from '../../imageknife/utils/LogUtil' import { GPUImageKuwaharaFilter } from '@ohos/gpu_transform' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class KuwaharaFilterTransform implements BaseTransform { - private _mRadius: number; + private _mRadius: number = 0; constructor(radius: number) { this._mRadius = radius; @@ -37,32 +39,29 @@ export class KuwaharaFilterTransform implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";KuwaharaFilterTransform buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";KuwaharaFilterTransform buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";KuwaharaFilterTransform buf is empty", null); } return; } if (!request.gpuEnabled) { LogUtil.error(Constants.PROJECT_TAG + ";the KuwaharaFilterTransform supported only in GPU mode"); if (func) { - func(Constants.PROJECT_TAG + ";;the KuwaharaFilterTransform supported only in GPU mode", null); + func?.asyncTransform(Constants.PROJECT_TAG + ";;the KuwaharaFilterTransform supported only in GPU mode", null); } return; } - var that = this; - var imageSource = image.createImageSource(buf as any); - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -70,7 +69,7 @@ export class KuwaharaFilterTransform implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -79,16 +78,16 @@ export class KuwaharaFilterTransform implements BaseTransform { } imageSource.createPixelMap(options) .then((data) => { - that.kuwahara(data, targetWidth, targetHeight, func); + this.kuwahara(data, targetWidth, targetHeight, func); }) - .catch((e) => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); - func(e, null); + func?.asyncTransform(e, null); }) - }) + }}) } - private async kuwahara(bitmap: image.PixelMap, targetWidth: number, targetHeight: number, func: AsyncTransform) { + private async kuwahara(bitmap: image.PixelMap, targetWidth: number, targetHeight: number, func?: AsyncTransform) { let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); await bitmap.readPixelsToBuffer(bufferData); let filter = new GPUImageKuwaharaFilter(); @@ -96,8 +95,8 @@ export class KuwaharaFilterTransform implements BaseTransform { filter.setRadius(this._mRadius); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight) bitmap.writeBufferToPixels(buf); - if (func) { - func("success", bitmap); + if (func != undefined) { + func?.asyncTransform("success", bitmap); } } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets index c9e8c98..40328f0 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets @@ -23,9 +23,11 @@ import image from "@ohos.multimedia.image" import resmgr from "@ohos.resourceManager" import { ImageKnifeGlobal } from '../ImageKnifeGlobal' import resourceManager from '@ohos.resourceManager' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class MaskTransformation implements BaseTransform { - private _mResourceData: Resource; + private _mResourceData:Resource|undefined = undefined; constructor(maskBitmap: Resource) { this._mResourceData = maskBitmap; @@ -38,27 +40,27 @@ export class MaskTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";MaskTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";MaskTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";MaskTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); let imageInfo = await imageSource.getImageInfo(); - let size = { + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("MaskTransformation The image size does not exist."), null) + func?.asyncTransform("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; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -66,7 +68,7 @@ export class MaskTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -77,33 +79,40 @@ export class MaskTransformation implements BaseTransform { .then(data => { this.openInternal(data, targetWidth, targetHeight, func) }) - .catch(e => { - func(e, null); + .catch((e:BusinessError )=> { + func?.asyncTransform(e, null); }) } - private openInternal(bitmap: any, width: number, height: number, func: AsyncTransform) { + private openInternal(bitmap: PixelMap, width: number, height: number, func?: AsyncTransform) { if (!this._mResourceData) { - throw new Error("MaskTransformation resource is empty"); + if(func != undefined){ + func.asyncTransform("MaskTransformation resource is empty", null) + } } - ((ImageKnifeGlobal.getInstance().getHapContext() as Record).resourceManager as resourceManager.ResourceManager).getMediaContent(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 - } + let context = (ImageKnifeGlobal.getInstance().getHapContext() as Record) + if(context != undefined){ + let resourceManager = context.resourceManager as resourceManager.ResourceManager + if(resourceManager != undefined && this._mResourceData != undefined) + resourceManager.getMediaContent(this._mResourceData?.id) + .then(array => { + let buffer = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset); + let imageSource:image.ImageSource = image.createImageSource(buffer); + let options:image.DecodingOptions = { + 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); - }) + } + imageSource.createPixelMap(options) + .then(maskBitmap => { + MaskUtils.mask(bitmap, maskBitmap, func) + }) + }) + .catch((err:BusinessError) => { + func?.asyncTransform("MaskTransformation openInternal error" + err, null); + }) + } } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/PixelationFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/PixelationFilterTransformation.ets index 32705e5..7c45c72 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/PixelationFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/PixelationFilterTransformation.ets @@ -21,6 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils" import { LogUtil } from '../../imageknife/utils/LogUtil' import image from "@ohos.multimedia.image" import { pixelUtils } from "../utils/PixelUtils" +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' /** * Applies a Pixelation effect to the image. @@ -42,24 +44,21 @@ export class PixelationFilterTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size:Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -67,7 +66,7 @@ export class PixelationFilterTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -82,10 +81,10 @@ export class PixelationFilterTransformation implements BaseTransform { pixelUtils.pixel(data, this._mPixel, func); } }) - .catch((e) => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); - func(e, null); + func?.asyncTransform(e, null); }) - }) + }}) } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/RotateImageTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/RotateImageTransformation.ets index 4fc0325..00f35a5 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/RotateImageTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/RotateImageTransformation.ets @@ -18,11 +18,13 @@ import { Constants } from "../constants/Constants" import { RequestOption } from "../../imageknife/RequestOption" import { TransformUtils } from "../transform/TransformUtils" import {LogUtil} from '../../imageknife/utils/LogUtil' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' import image from "@ohos.multimedia.image" export class RotateImageTransformation implements BaseTransform { - private mDegreesToRotate: number; + private mDegreesToRotate: number = 0; constructor(degreesToRotate: number) { this.mDegreesToRotate = degreesToRotate; @@ -35,24 +37,21 @@ export class RotateImageTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -60,7 +59,7 @@ export class RotateImageTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, rotate: this.mDegreesToRotate%360, desiredSize: { @@ -70,12 +69,12 @@ export class RotateImageTransformation implements BaseTransform { } imageSource.createPixelMap(options) .then((data) => { - func("", data); + func?.asyncTransform("", data); }) - .catch((e) => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); - func(e, null); + func?.asyncTransform(e, null); }) - }) + }}) } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/RoundedCornersTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/RoundedCornersTransformation.ets index dca0d44..097ab06 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/RoundedCornersTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/RoundedCornersTransformation.ets @@ -19,23 +19,27 @@ import { Constants } from "../constants/Constants" import { RequestOption } from "../../imageknife/RequestOption" import { TransformUtils } from "../transform/TransformUtils" import {LogUtil} from '../../imageknife/utils/LogUtil' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' import image from "@ohos.multimedia.image" +export interface RoundCorner{ + top_left: number, + top_right: number, + bottom_left: number, + bottom_right: number +} + export class RoundedCornersTransformation implements BaseTransform { private mTop_left: number = 0; private mTop_right: number = 0; private mBottom_left: number = 0; private mBottom_right: number = 0; - private mTransform_pixelMap: any; - private mPoint: ArcPoint[]; + private mTransform_pixelMap: PixelMap|undefined = undefined; + private mPoint: ArcPoint[] = new Array(); - constructor(value: { - top_left?: number, - top_right?: number, - bottom_left?: number, - bottom_right?: number - }) { + constructor(value:RoundCorner) { this.mTop_left = value.top_left; this.mTop_right = value.top_right; this.mBottom_left = value.bottom_left; @@ -54,37 +58,33 @@ export class RoundedCornersTransformation implements BaseTransform { + 'buf is null ='+ (buf == null)); if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); - var that = this; - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } if (pixelMapHeight < targetHeight) { targetHeight = pixelMapHeight; } - that.transformPixelMap(imageSource, targetWidth, targetHeight, func); - }) + this.transformPixelMap(imageSource, targetWidth, targetHeight, func); + }}) } - private transformPixelMap(imageSource: any, outWith: number, outHeight: number, func?: AsyncTransform) { - var options = { + private transformPixelMap(imageSource: image.ImageSource, outWith: number, outHeight: number, func?: AsyncTransform) { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: outWith, @@ -96,8 +96,8 @@ export class RoundedCornersTransformation implements BaseTransform { this.mTransform_pixelMap = pixelMap; this.mTransform_pixelMap.getImageInfo() .then((data) => { - let width = data.size.width; - let height = data.size.height; + let width:number = data.size.width; + let height:number = data.size.height; if (this.mTop_left > 0) { this.top_left_corners(); } @@ -110,18 +110,18 @@ export class RoundedCornersTransformation implements BaseTransform { if (this.mBottom_right > 0) { this.bottom_right_corners(width, height); } - if (func) { - func("", this.mTransform_pixelMap); + if (func != undefined && this.mTransform_pixelMap != undefined) { + func?.asyncTransform("", this.mTransform_pixelMap); } }) - .catch((error) => { + .catch((error:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + "RoundedCornersTransformation error:" + error); }); }) - .catch((e) => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); - if (func) { - func(e, null); + if (func != undefined) { + func?.asyncTransform(e, null); } }) @@ -129,10 +129,10 @@ export class RoundedCornersTransformation implements BaseTransform { private top_left_corners() { this.mPoint = new Array(); - for (var time = 180; time < 270; time++) { - var hudu = (2 * Math.PI / 360) * time; - var x = this.mTop_left + Math.sin(hudu) * this.mTop_left; - var y = this.mTop_left + Math.cos(hudu) * this.mTop_left; + for (let time = 180; time < 270; time++) { + let hudu:number = (2 * Math.PI / 360) * time; + let x:number= this.mTop_left + Math.sin(hudu) * this.mTop_left; + let y:number = this.mTop_left + Math.cos(hudu) * this.mTop_left; this.mPoint.push(new ArcPoint(x, y)); } this.hand_pixel_point(this.mPoint); @@ -140,10 +140,10 @@ export class RoundedCornersTransformation implements BaseTransform { private top_right_corners(width: number) { this.mPoint = new Array(); - for (var time = 0; time < 90; time++) { - var hudu = (2 * Math.PI / 360) * time; - var x = (width - this.mTop_right) + Math.cos(hudu) * this.mTop_right; - var y = this.mTop_right - Math.sin(hudu) * this.mTop_right; + for (let time = 0; time < 90; time++) { + let hudu:number = (2 * Math.PI / 360) * time; + let x:number = (width - this.mTop_right) + Math.cos(hudu) * this.mTop_right; + let y:number = this.mTop_right - Math.sin(hudu) * this.mTop_right; this.mPoint.push(new ArcPoint(x, y)); } @@ -152,10 +152,10 @@ export class RoundedCornersTransformation implements BaseTransform { private bottom_left_corners(width: number, height: number) { this.mPoint = new Array(); - for (var time = 90; time < 180; time++) { - var hudu = (2 * Math.PI / 360) * time; - var x = this.mBottom_left + Math.cos(hudu) * this.mBottom_left; - var y = height - this.mBottom_left + Math.sin(hudu) * this.mBottom_left; + for (let time = 90; time < 180; time++) { + let hudu = (2 * Math.PI / 360) * time; + let x = this.mBottom_left + Math.cos(hudu) * this.mBottom_left; + let y = height - this.mBottom_left + Math.sin(hudu) * this.mBottom_left; this.mPoint.push(new ArcPoint(x, y)); } this.hand_pixel_point(this.mPoint); @@ -163,25 +163,25 @@ export class RoundedCornersTransformation implements BaseTransform { private bottom_right_corners(width: number, height: number) { this.mPoint = new Array(); - for (var time = 0; time < 90; time++) { - var hudu = (2 * Math.PI / 360) * time; - var x = width - this.mBottom_right + Math.cos(hudu) * this.mBottom_right; - var y = height - this.mBottom_right + Math.sin(hudu) * this.mBottom_right; + for (let time = 0; time < 90; time++) { + let hudu = (2 * Math.PI / 360) * time; + let x = width - this.mBottom_right + Math.cos(hudu) * this.mBottom_right; + let y = height - this.mBottom_right + Math.sin(hudu) * this.mBottom_right; this.mPoint.push(new ArcPoint(x, y)); } this.hand_right_pixel_point(this.mPoint, width); } private hand_pixel_point(points: ArcPoint[]) { - for (var index = 0; index < points.length; index++) { + for (let index = 0; index < points.length; index++) { const element = points[index]; let x = element.getX(); let y = element.getY(); - for (var i = 0; i <= x; i++) { - var buffer1 = new ArrayBuffer(5); - var bytes1 = new Uint8Array(buffer1); - var writePositionRen = { + for (let i = 0; i <= x; i++) { + let buffer1 = new ArrayBuffer(5); + let bytes1 = new Uint8Array(buffer1); + let writePositionRen:image.PositionArea = { pixels: buffer1, offset: 1, stride: 1024, @@ -193,21 +193,23 @@ export class RoundedCornersTransformation implements BaseTransform { for (let j = 0;j < 5; j++) { bytes1[j] = 0; } - this.mTransform_pixelMap.writePixels(writePositionRen); + if(this.mTransform_pixelMap != undefined) { + this.mTransform_pixelMap.writePixels(writePositionRen); + } } } } private hand_right_pixel_point(points: ArcPoint[], width: number) { - for (var index = 0; index < points.length; index++) { + for (let index = 0; index < points.length; index++) { const element = points[index]; let x = element.getX(); let y = element.getY(); - for (var i = x; i <= width; i++) { - var buffer1 = new ArrayBuffer(5); - var bytes1 = new Uint8Array(buffer1); - var writePositionRen = { + for (let i = x; i <= width; i++) { + let buffer1 = new ArrayBuffer(5); + let bytes1 = new Uint8Array(buffer1); + let writePositionRen:image.PositionArea = { pixels: buffer1, offset: 0, stride: 4, @@ -219,7 +221,9 @@ export class RoundedCornersTransformation implements BaseTransform { for (let j = 0;j < 5; j++) { bytes1[j] = 0; } - this.mTransform_pixelMap.writePixels(writePositionRen); + if(this.mTransform_pixelMap != undefined) { + this.mTransform_pixelMap.writePixels(writePositionRen); + } } } } diff --git a/imageknife/src/main/ets/components/imageknife/transform/SepiaFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/SepiaFilterTransformation.ets index eee5d66..72fbe75 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/SepiaFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/SepiaFilterTransformation.ets @@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption" import { LogUtil } from '../../imageknife/utils/LogUtil' import image from "@ohos.multimedia.image" import { GPUImageSepiaToneFilter } from '@ohos/gpu_transform' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' /** * Applies a simple sepia effect. @@ -34,28 +36,28 @@ export class SepiaFilterTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty", null); + if (func!=undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty", null); } return; } - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); - let imageInfo = await imageSource.getImageInfo(); - let size = { + let imageInfo:image.ImageInfo = await imageSource.getImageInfo(); + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("SepiaFilterTransformation The image size does not exist."), null) + func?.asyncTransform("SepiaFilterTransformation 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; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -63,14 +65,14 @@ export class SepiaFilterTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, height: targetHeight } } - let data = await imageSource.createPixelMap(options); + let data:PixelMap = await imageSource.createPixelMap(options); let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); await data.readPixelsToBuffer(bufferData); @@ -80,21 +82,21 @@ export class SepiaFilterTransformation implements BaseTransform { filter.setImageData(bufferData, targetWidth, targetHeight); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); data.writeBufferToPixels(buf); - if (func) { - func("success", data); + if (func != undefined) { + func?.asyncTransform("success", data); } return; } let bufferNewData = new ArrayBuffer(data.getPixelBytesNumber()); - var dataArray = new Uint8Array(bufferData); - var dataNewArray = new Uint8Array(bufferNewData); + let dataArray = new Uint8Array(bufferData); + let 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]; + const r:number = dataArray[index]; + const g:number = dataArray[index+1]; + const b:number = dataArray[index+2]; + const f:number = dataArray[index+3]; dataNewArray[index+2] = this.checkVisAble(this.colorBlend(this.noise() , (r * 0.272) + (g * 0.534) + (b * 0.131) @@ -109,8 +111,8 @@ export class SepiaFilterTransformation implements BaseTransform { } await data.writeBufferToPixels(bufferNewData); - if (func) { - func("", data); + if (func != undefined) { + func?.asyncTransform("", data); } } diff --git a/imageknife/src/main/ets/components/imageknife/transform/SketchFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/SketchFilterTransformation.ets index aa3dbf7..1b3d72d 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/SketchFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/SketchFilterTransformation.ets @@ -20,6 +20,8 @@ import { RequestOption } from '../../imageknife/RequestOption' import { TransformUtils } from '../transform/TransformUtils' import image from '@ohos.multimedia.image' import { CalculatePixelUtils } from '../utils/CalculatePixelUtils' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class SketchFilterTransformation implements BaseTransform { getName() { @@ -28,25 +30,21 @@ export class SketchFilterTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - throw new Error(Constants.PROJECT_TAG + ';SketchFilterTransformation buf is empty'); - if (func) { - func(Constants.PROJECT_TAG + ';SketchFilterTransformation buf is empty', null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ';SketchFilterTransformation buf is empty', null); } return; } - var imageSource = image.createImageSource(buf as any); - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth: number = size.width; + let pixelMapHeight: number = size.height; + let targetWidth: number = request.size.width; + let targetHeight: number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -54,7 +52,7 @@ export class SketchFilterTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options: image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -69,9 +67,9 @@ export class SketchFilterTransformation implements BaseTransform { CalculatePixelUtils.sketch(data, func); } }) - .catch((e) => { - func(e, null); + .catch((e:BusinessError) => { + func?.asyncTransform(e, null); }) - }) + }}) } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/SwirlFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/SwirlFilterTransformation.ets index 891f315..bc2b174 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/SwirlFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/SwirlFilterTransformation.ets @@ -23,6 +23,8 @@ import { PixelEntry } from '../entry/PixelEntry' import { ColorUtils } from '../utils/ColorUtils' import { CalculatePixelUtils } from '../utils/CalculatePixelUtils' import { GPUImageSwirlFilter } from '@ohos/gpu_transform' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class SwirlFilterTransformation implements BaseTransform { private radius: number = 0; @@ -47,25 +49,21 @@ export class SwirlFilterTransformation implements BaseTransform { 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); + if (func != undefined) { + func?.asyncTransform(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 - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource,{asyncTransform: (error:BusinessError|string, size:Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -73,7 +71,7 @@ export class SwirlFilterTransformation implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -84,23 +82,23 @@ export class SwirlFilterTransformation implements BaseTransform { .then((data) => { this.swirl(data, this.radius, request, func); }) - .catch((e) => { - func(e, null); + .catch((e:BusinessError) => { + func?.asyncTransform(e, null); }) - }) + }}) } private async swirl(bitmap: image.PixelMap, degree: number, request: RequestOption, func?: AsyncTransform) { - let imageInfo = await bitmap.getImageInfo(); - let size = { + let imageInfo:image.ImageInfo = await bitmap.getImageInfo(); + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { return; } - let width = size.width; - let height = size.height; + let width:number = size.width; + let height:number = size.height; let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); @@ -113,8 +111,8 @@ export class SwirlFilterTransformation implements BaseTransform { filter.setCenter(this._xCenter, this._yCenter) let buf = await filter.getPixelMapBuf(0, 0, width, height); bitmap.writeBufferToPixels(buf); - if (func) { - func("success", bitmap); + if (func != undefined) { + func?.asyncTransform("success", bitmap); } return; } @@ -125,13 +123,13 @@ export class SwirlFilterTransformation implements BaseTransform { let dataArray = new Uint8Array(bufferData); - let ph = 0; - let pw = 0; + let ph:number = 0; + let pw:number = 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]; + const r:number = dataArray[index]; + const g:number = dataArray[index+1]; + const b:number = dataArray[index+2]; + const f:number = dataArray[index+3]; let entry = new PixelEntry(); entry.a = 0; @@ -201,8 +199,8 @@ export class SwirlFilterTransformation implements BaseTransform { index++; } await bitmap.writeBufferToPixels(bufferNewData); - if (func) { - func("", bitmap); + if (func != undefined) { + func?.asyncTransform("", bitmap); } } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/ToonFilterTransform.ets b/imageknife/src/main/ets/components/imageknife/transform/ToonFilterTransform.ets index 90e1647..7c7d83d 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/ToonFilterTransform.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/ToonFilterTransform.ets @@ -21,7 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils" import image from "@ohos.multimedia.image" import { LogUtil } from '../../imageknife/utils/LogUtil' import { GPUImageToonFilter } from '@ohos/gpu_transform' - +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class ToonFilterTransform implements BaseTransform { private threshold: number = 0.2; @@ -43,32 +44,29 @@ export class ToonFilterTransform implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";ToonFilterTransform buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";ToonFilterTransform buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";ToonFilterTransform buf is empty", null); } return; } if (!request.gpuEnabled) { LogUtil.error(Constants.PROJECT_TAG + ";the ToonFilterTransform supported only in GPU mode"); if (func) { - func(Constants.PROJECT_TAG + ";the ToonFilterTransform supported only in GPU mode", null); + func?.asyncTransform(Constants.PROJECT_TAG + ";the ToonFilterTransform supported only in GPU mode", null); } return; } - var that = this; - var imageSource = image.createImageSource(buf as any); - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -76,7 +74,7 @@ export class ToonFilterTransform implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -85,16 +83,16 @@ export class ToonFilterTransform implements BaseTransform { } imageSource.createPixelMap(options) .then((data) => { - that.toon(data, targetWidth, targetHeight, func); + this.toon(data, targetWidth, targetHeight, func); }) - .catch((e) => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); - func(e, null); + func?.asyncTransform(e, null); }) - }) + }}) } - private async toon(bitmap: image.PixelMap, targetWidth: number, targetHeight: number, func: AsyncTransform) { + private async toon(bitmap: image.PixelMap, targetWidth: number, targetHeight: number, func?: AsyncTransform) { let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); await bitmap.readPixelsToBuffer(bufferData); let filter = new GPUImageToonFilter(); @@ -103,8 +101,8 @@ export class ToonFilterTransform implements BaseTransform { filter.setQuantizationLevels(this.quantizationLevels); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight) bitmap.writeBufferToPixels(buf); - if (func) { - func("success", bitmap); + if (func != undefined) { + func?.asyncTransform("success", bitmap); } } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/TransformUtils.ets b/imageknife/src/main/ets/components/imageknife/transform/TransformUtils.ets index 14653b9..1cd859f 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/TransformUtils.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/TransformUtils.ets @@ -14,18 +14,19 @@ */ import {AsyncTransform} from '../transform/AsyncTransform' import image from '@ohos.multimedia.image' - +import {Size} from '../../imageknife/RequestOption' +import { BusinessError } from '@ohos.base' export class TransformUtils { static centerCrop(buf: ArrayBuffer, outWidth: number, outHeihgt: number, callback?: AsyncTransform>) { - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); imageSource.getImageInfo() .then((p) => { - var sw; - var sh; - var scale; - var pw = p.size.width; - var ph = p.size.height; + let sw:number=0; + let sh:number=0; + let scale:number=1; + let pw:number = p.size.width; + let ph:number = p.size.height; if (pw == outWidth && ph == outHeihgt) { sw = outWidth; sh = outHeihgt; @@ -38,7 +39,7 @@ export class TransformUtils { sw = pw * scale; sh = ph * scale; } - var options = { + let options:image.DecodingOptions = { editable: true, rotate: 0, desiredRegion: { size: { width: sw, height: sh }, @@ -47,18 +48,18 @@ export class TransformUtils { }, } if (callback) { - callback('', imageSource.createPixelMap(options)); + callback.asyncTransform('', imageSource.createPixelMap(options)); } }) - .catch((error) => { - callback(error, null); + .catch((error:BusinessError) => { + callback?.asyncTransform(error, null); }) } static rotateImage(buf: ArrayBuffer, degreesToRotate: number): Promise{ - var imageSource = image.createImageSource(buf as any); - var options = { + let imageSource:image.ImageSource = image.createImageSource(buf); + let options:image.DecodingOptions = { editable: true, rotate: degreesToRotate } @@ -67,36 +68,36 @@ export class TransformUtils { static centerInside(buf: ArrayBuffer, outWidth: number, outHeihgt: number, callback?: AsyncTransform>) { - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); imageSource.getImageInfo() - .then((p) => { - var pw = p.size.width; - var ph = p.size.height; + .then((p:image.ImageInfo) => { + let pw = p.size.width; + let ph = p.size.height; if (pw <= outWidth && ph <= outHeihgt) { - callback('', imageSource.createPixelMap()); + callback?.asyncTransform('', imageSource.createPixelMap()); } else { - this.fitCenter(buf, outWidth, outHeihgt, callback); + TransformUtils.fitCenter(buf, outWidth, outHeihgt, callback); } }) - .catch((error) => { - callback(error, null); + .catch((error:BusinessError) => { + callback?.asyncTransform(error, null); }) } static fitCenter(buf: ArrayBuffer, outWidth: number, outHeihgt: number , callback?: AsyncTransform>) { - var imageSource = image.createImageSource(buf as any); + let imageSource:image.ImageSource = image.createImageSource(buf); imageSource.getImageInfo() - .then((p) => { - var pw = p.size.width; - var ph = p.size.height; - var widthPercentage = outWidth / pw; - var heightPercentage = outHeihgt / ph; - var minPercentage = Math.min(widthPercentage, heightPercentage); + .then((p:image.ImageInfo) => { + let pw:number = p.size.width; + let ph:number = p.size.height; + let widthPercentage:number = outWidth / pw; + let heightPercentage:number = outHeihgt / ph; + let minPercentage:number = Math.min(widthPercentage, heightPercentage); - var targetWidth = Math.round(minPercentage * pw); - var targetHeight = Math.round(minPercentage * ph); + let targetWidth:number = Math.round(minPercentage * pw); + let targetHeight:number = Math.round(minPercentage * ph); if (pw == targetWidth && ph == targetHeight) { targetWidth = pw; @@ -105,37 +106,34 @@ export class TransformUtils { targetWidth = minPercentage * pw; targetHeight = minPercentage * ph; } - var options = { + let options:image.DecodingOptions = { editable: true, rotate: 0, desiredSize: { width: targetWidth, height: targetHeight } } if (callback) { - callback('', imageSource.createPixelMap(options)); + callback.asyncTransform('', imageSource.createPixelMap(options)); } }) - .catch(e => { + .catch((e:BusinessError) => { if (callback) { - callback(e, null); + callback.asyncTransform(e, null); } }) } - static getPixelMapSize(imageSource: any, func: AsyncTransform<{ - width: number, - height: number - }>) { + static getPixelMapSize(imageSource: image.ImageSource, func: AsyncTransform) { if (!imageSource) { return; } - imageSource.getImageInfo((err, value) => { + imageSource.getImageInfo((err:BusinessError, value:image.ImageInfo) => { if (err) { - func(err, null) + func?.asyncTransform(err, null) return; } - var pWidth = value.size.width; - var pHeight = value.size.height; - func('', { width: pWidth, height: pHeight }); + let pWidth:number = value.size.width; + let pHeight:number = value.size.height; + func?.asyncTransform('', { width: pWidth, height: pHeight }); }) } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/VignetteFilterTransform.ets b/imageknife/src/main/ets/components/imageknife/transform/VignetteFilterTransform.ets index 4f34dbc..c49897d 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/VignetteFilterTransform.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/VignetteFilterTransform.ets @@ -21,6 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils" import image from "@ohos.multimedia.image" import { LogUtil } from '../../imageknife/utils/LogUtil' import { GPUImageVignetterFilter } from '@ohos/gpu_transform' +import { BusinessError } from '@ohos.base' +import {Size} from '../../imageknife/RequestOption' export class VignetteFilterTransform implements BaseTransform { @@ -47,32 +49,28 @@ export class VignetteFilterTransform implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { LogUtil.log(Constants.PROJECT_TAG + ";VignetteFilterTransform buf is empty"); - if (func) { - func(Constants.PROJECT_TAG + ";VignetteFilterTransform buf is empty", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";VignetteFilterTransform buf is empty", null); } return; } if (!request.gpuEnabled) { LogUtil.error(Constants.PROJECT_TAG + ";the VignetteFilterTransform supported only in GPU mode"); - if (func) { - func(Constants.PROJECT_TAG + ";the VignetteFilterTransform supported only in GPU mode", null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ";the VignetteFilterTransform supported only in GPU mode", null); } return; } - var that = this; - var imageSource = image.createImageSource(buf as any); - TransformUtils.getPixelMapSize(imageSource, (error, size: { - width: number, - height: number - }) => { + let imageSource:image.ImageSource = image.createImageSource(buf); + TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => { if (!size) { - func(error, null) + func?.asyncTransform(error, null) return; } - var pixelMapWidth = size.width; - var pixelMapHeight = size.height; - var targetWidth = request.size.width; - var targetHeight = request.size.height; + let pixelMapWidth:number = size.width; + let pixelMapHeight:number = size.height; + let targetWidth:number = request.size.width; + let targetHeight:number = request.size.height; if (pixelMapWidth < targetWidth) { targetWidth = pixelMapWidth; } @@ -80,7 +78,7 @@ export class VignetteFilterTransform implements BaseTransform { targetHeight = pixelMapHeight; } - var options = { + let options:image.DecodingOptions = { editable: true, desiredSize: { width: targetWidth, @@ -89,16 +87,16 @@ export class VignetteFilterTransform implements BaseTransform { } imageSource.createPixelMap(options) .then((data) => { - that.vignette(data, targetWidth, targetHeight, func); + this.vignette(data, targetWidth, targetHeight, func); }) - .catch((e) => { + .catch((e:BusinessError) => { LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); - func(e, null); + func?.asyncTransform(e, null); }) - }) + }}) } - private async vignette(bitmap: image.PixelMap, targetWidth: number, targetHeight: number, func: AsyncTransform) { + private async vignette(bitmap: image.PixelMap, targetWidth: number, targetHeight: number, func?: AsyncTransform) { let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); await bitmap.readPixelsToBuffer(bufferData); let filter = new GPUImageVignetterFilter(); @@ -109,8 +107,8 @@ export class VignetteFilterTransform implements BaseTransform { filter.setVignetteEnd(this.vignetteSpace[1]); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight) bitmap.writeBufferToPixels(buf); - if (func) { - func("success", bitmap); + if (func != undefined) { + func?.asyncTransform("success", bitmap); } } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/pixelmap/CenterCrop.ets b/imageknife/src/main/ets/components/imageknife/transform/pixelmap/CenterCrop.ets index 28349dc..01b8b5d 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/pixelmap/CenterCrop.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/pixelmap/CenterCrop.ets @@ -18,7 +18,7 @@ import {AsyncTransform} from '../AsyncTransform' import {Constants} from '../../constants/Constants' import {TransformUtils} from '../TransformUtils' import {RequestOption} from '../../../imageknife/RequestOption' - +import { BusinessError } from '@ohos.base' export class CenterCrop implements BaseTransform { getName() { return 'CenterCrop:' + this; @@ -26,18 +26,17 @@ export class CenterCrop implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - throw new Error(Constants.PROJECT_TAG + ';CenterCrop buf is empty'); - if (func) { - func(Constants.PROJECT_TAG + ';CenterCrop buf is empty', null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ';CenterCrop buf is empty', null); } return; } - TransformUtils.centerCrop(buf, request.size.width, request.size.height, (error, data) => { - data.then(p => { - func('', p); - }).catch(e => { - func(Constants.PROJECT_TAG + ';CenterCrop error:' + e, null); + TransformUtils.centerCrop(buf, request.size.width, request.size.height, {asyncTransform:(error:BusinessError|string, data:Promise|null) => { + data?.then(p => { + func?.asyncTransform('', p); + }).catch((e:BusinessError) => { + func?.asyncTransform(Constants.PROJECT_TAG + ';CenterCrop error:' + e, null); }) - }) + }}) } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/pixelmap/CenterInside.ets b/imageknife/src/main/ets/components/imageknife/transform/pixelmap/CenterInside.ets index c3b8b43..016ee2d 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/pixelmap/CenterInside.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/pixelmap/CenterInside.ets @@ -13,12 +13,12 @@ * limitations under the License. */ -import {BaseTransform} from '../BaseTransform' +import {BaseTransform} from '../BaseTransform' import {AsyncTransform} from '../AsyncTransform' import {Constants} from '../../constants/Constants' import {TransformUtils} from '../TransformUtils' import {RequestOption} from '../../../imageknife/RequestOption' - +import { BusinessError } from '@ohos.base' export class CenterInside implements BaseTransform { getName() { return 'CenterInside:' + this; @@ -26,18 +26,17 @@ export class CenterInside implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - throw new Error(Constants.PROJECT_TAG + ';CenterInside buf is empty'); - if (func) { - func(Constants.PROJECT_TAG + ';CenterInside buf is empty', null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ';CenterInside buf is empty', null); } return; } - TransformUtils.centerInside(buf, request.size.width, request.size.height, (error, data) => { - data.then(p => { - func('', p); - }).catch(e => { - func(Constants.PROJECT_TAG + ';CenterInside error:' + e, null); + TransformUtils.centerInside(buf, request.size.width, request.size.height, {asyncTransform:(error:BusinessError|string, data:Promise|null) => { + data?.then(p => { + func?.asyncTransform('', p); + }).catch((e:BusinessError) => { + func?.asyncTransform(Constants.PROJECT_TAG + ';CenterInside error:' + e, null); }) - }) + }}) } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/transform/pixelmap/FitCenter.ets b/imageknife/src/main/ets/components/imageknife/transform/pixelmap/FitCenter.ets index 2330b0d..b820ba0 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/pixelmap/FitCenter.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/pixelmap/FitCenter.ets @@ -18,7 +18,7 @@ import {AsyncTransform} from '../AsyncTransform' import {Constants} from '../../constants/Constants' import {TransformUtils} from '../TransformUtils' import {RequestOption} from '../../../imageknife/RequestOption' - +import { BusinessError } from '@ohos.base' export class FitCenter implements BaseTransform { getName() { return 'FitCenter:' + this; @@ -26,18 +26,17 @@ export class FitCenter implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - throw new Error(Constants.PROJECT_TAG + ';FitCenter buf is empty'); - if (func) { - func(Constants.PROJECT_TAG + ';FitCenter buf is empty', null); + if (func != undefined) { + func?.asyncTransform(Constants.PROJECT_TAG + ';FitCenter buf is empty', null); } return; } - TransformUtils.fitCenter(buf, request.size.width, request.size.height, (error, data) => { - data.then(p => { - func('', p); - }).catch(e => { - func(Constants.PROJECT_TAG + ';FitCenter error:' + e, null); + TransformUtils.fitCenter(buf, request.size.width, request.size.height, {asyncTransform:(error:BusinessError|string, data:Promise|null) => { + data?.then(p => { + func?.asyncTransform('', p); + }).catch((e:BusinessError) => { + func?.asyncTransform(Constants.PROJECT_TAG + ';FitCenter error:' + e, null); }) - }) + }}) } } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/utils/CalculatePixelUtils.ets b/imageknife/src/main/ets/components/imageknife/utils/CalculatePixelUtils.ets index e9d5300..119bd3b 100644 --- a/imageknife/src/main/ets/components/imageknife/utils/CalculatePixelUtils.ets +++ b/imageknife/src/main/ets/components/imageknife/utils/CalculatePixelUtils.ets @@ -16,16 +16,17 @@ import { PixelEntry } from "../entry/PixelEntry" import { AsyncTransform } from "../transform/AsyncTransform" import { ColorUtils } from "./ColorUtils" import { GPUImageSketchFilter } from '@ohos/gpu_transform' +import image from '@ohos.multimedia.image' export namespace CalculatePixelUtils { - export async function sketch(p: any, func: AsyncTransform) { + export async function sketch(p: PixelMap, func?: AsyncTransform) { let imageInfo = await p.getImageInfo(); let size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("sketch The image size does not exist."), null) + func.asyncTransform("sketch The image size does not exist.", null) return; } @@ -91,8 +92,8 @@ export namespace CalculatePixelUtils { } await p.writeBufferToPixels(bufferNewData); - if (func) { - func("success", p); + if (func != undefined) { + func.asyncTransform("success", p); } } @@ -298,14 +299,14 @@ export namespace CalculatePixelUtils { return array; } - export async function sketchGpu(p: any, func: AsyncTransform) { - let imageInfo = await p.getImageInfo(); + export async function sketchGpu(p: PixelMap, func?: AsyncTransform) { + let imageInfo:image.ImageInfo = await p.getImageInfo(); let size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("sketch The image size does not exist."), null) + func?.asyncTransform("sketch The image size does not exist.", null) return; } @@ -318,8 +319,8 @@ export namespace CalculatePixelUtils { filter.setImageData(bufferData, w, h); filter.getPixelMapBuf(0, 0, w, h).then((buf) => { p.writeBufferToPixels(buf); - if (func) { - func("success", p); + if (func!=undefined) { + func.asyncTransform("success", p); } }) } diff --git a/imageknife/src/main/ets/components/imageknife/utils/FastBlur.ets b/imageknife/src/main/ets/components/imageknife/utils/FastBlur.ets index 568b5cc..d85e8bb 100644 --- a/imageknife/src/main/ets/components/imageknife/utils/FastBlur.ets +++ b/imageknife/src/main/ets/components/imageknife/utils/FastBlur.ets @@ -18,11 +18,12 @@ import {PixelEntry} from "../entry/PixelEntry" import {AsyncTransform} from "../transform/AsyncTransform" import {ColorUtils} from "./ColorUtils" import { GPUImageBlurFilter } from '@ohos/gpu_transform' - +import {Size} from '../../imageknife/RequestOption' +import image from '@ohos.multimedia.image'; export namespace fastBlur { - export async function blur(bitmap: any, radius: number, canReuseInBitmap: boolean, func: AsyncTransform) { + export async function blur(bitmap: PixelMap, radius: number, canReuseInBitmap: boolean, func?: AsyncTransform) { // http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html @@ -52,25 +53,25 @@ export namespace fastBlur { // if (radius < 1) { - func("error,radius must be greater than 1 ", null); + func?.asyncTransform("error,radius must be greater than 1 ", null); return; } let imageInfo = await bitmap.getImageInfo(); - let size = { + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("fastBlur The image size does not exist."), null) + func?.asyncTransform("fastBlur The image size does not exist.", null) return; } let w = size.width; let h = size.height; - var pixEntry: Array = new Array() - var pix: Array = new Array() + let pixEntry: Array = new Array() + let pix: Array = new Array() let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); @@ -103,7 +104,17 @@ export namespace fastBlur { let g = CalculatePixelUtils.createIntArray(wh); let b = CalculatePixelUtils.createIntArray(wh); - let rsum, gsum, bsum, x, y, i, p, yp, yi, yw: number; + let rsum:number = 0; + let gsum:number = 0; + let bsum:number = 0; + let x:number = 0; + let y:number = 0; + let i:number = 0; + let p:number = 0; + let yp:number = 0; + let yi:number = 0; + let yw: number = 0; + let vmin = CalculatePixelUtils.createIntArray(Math.max(w, h)); let divsum = (div + 1) >> 1; @@ -115,7 +126,15 @@ export namespace fastBlur { yw = yi = 0; let stack = CalculatePixelUtils.createInt2DArray(div, 3); - let stackpointer, stackstart, rbs, routsum, goutsum, boutsum, rinsum, ginsum, binsum: number; + let stackpointer:number=0; + let stackstart:number=0; + let rbs:number=0; + let routsum:number=0; + let goutsum:number=0; + let boutsum:number=0; + let rinsum:number=0; + let ginsum:number=0; + let binsum:number=0; let sir: Array; let r1 = radius + 1; for (y = 0; y < h; y++) { @@ -286,29 +305,29 @@ export namespace fastBlur { index++; } await bitmap.writeBufferToPixels(bufferNewData); - if (func) { - func("success", bitmap); + if (func != undefined) { + func?s.asyncTransform("success", bitmap); } } - export async function blurGPU(bitmap: any, radius: number, canReuseInBitmap: boolean, func: AsyncTransform) { + export async function blurGPU(bitmap: PixelMap, radius: number, canReuseInBitmap: boolean, func?: AsyncTransform) { if (radius < 1) { - func("error,radius must be greater than 1 ", null); + func?.asyncTransform("error,radius must be greater than 1 ", null); return; } - let imageInfo = await bitmap.getImageInfo(); - let size = { + let imageInfo:image.ImageInfo = await bitmap.getImageInfo(); + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("fastBlur The image size does not exist."), null) + func?.asyncTransform("fastBlur The image size does not exist.", null) return; } - let w = size.width; - let h = size.height; + let w:number = size.width; + let h:number = size.height; let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); await bitmap.readPixelsToBuffer(bufferData); @@ -318,8 +337,8 @@ export namespace fastBlur { filter.setBlurOffset([1.0, 1.0]) filter.getPixelMapBuf(0, 0, w, h).then((buf) => { bitmap.writeBufferToPixels(buf); - if (func) { - func("success", bitmap); + if (func != undefined) { + func?.asyncTransform("success", bitmap); } }) } diff --git a/imageknife/src/main/ets/components/imageknife/utils/PixelUtils.ets b/imageknife/src/main/ets/components/imageknife/utils/PixelUtils.ets index 2895fc0..65d5da6 100644 --- a/imageknife/src/main/ets/components/imageknife/utils/PixelUtils.ets +++ b/imageknife/src/main/ets/components/imageknife/utils/PixelUtils.ets @@ -17,23 +17,25 @@ import {CalculatePixelUtils} from "./CalculatePixelUtils" import {PixelEntry} from "../entry/PixelEntry" import {AsyncTransform} from "../transform/AsyncTransform" import {ColorUtils} from "./ColorUtils" +import {Size} from "../../imageknife/RequestOption" import {GPUImagePixelationFilter} from '@ohos/gpu_transform' +import image from '@ohos.multimedia.image'; export namespace pixelUtils { - export async function pixel(bitmap: any, pixel: number, func: AsyncTransform) { + export async function pixel(bitmap: PixelMap, pixel: number, func?: AsyncTransform) { - let imageInfo = await bitmap.getImageInfo(); - let size = { + let imageInfo:image.ImageInfo= await bitmap.getImageInfo(); + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("GrayscaleTransformation The image size does not exist."), null) + func.asyncTransform("GrayscaleTransformation The image size does not exist.", null) return; } - let targetWidth = size.width; - let targetHeight = size.height; + let targetWidth:number = size.width; + let targetHeight:number = size.height; var pixEntry: Array = new Array() let inPixels: Array> = CalculatePixelUtils.createInt2DArray(targetHeight, targetWidth); @@ -42,8 +44,8 @@ export namespace pixelUtils { await bitmap.readPixelsToBuffer(bufferData); let dataArray = new Uint8Array(bufferData); - let ph = 0; - let pw = 0; + let ph:number = 0; + let pw:number = 0; for (let index = 0; index < dataArray.length; index += 4) { @@ -127,18 +129,18 @@ export namespace pixelUtils { } await bitmap.writeBufferToPixels(bufferNewData); if (func) { - func("success", bitmap); + func?.asyncTransform("success", bitmap); } } - export async function pixelGPU(bitmap: any, pixel: number, func: AsyncTransform) { + export async function pixelGPU(bitmap: any, pixel: number, func?: AsyncTransform) { let imageInfo = await bitmap.getImageInfo(); - let size = { + let size:Size = { width: imageInfo.size.width, height: imageInfo.size.height } if (!size) { - func(new Error("GrayscaleTransformation The image size does not exist."), null) + func?.asyncTransform("GrayscaleTransformation The image size does not exist.", null) return; } let w = size.width; @@ -151,8 +153,8 @@ export namespace pixelUtils { filter.setPixel(pixel) filter.getPixelMapBuf(0, 0, w, h).then((buf) => { bitmap.writeBufferToPixels(buf); - if (func) { - func("success", bitmap); + if (func!=undefined) { + func?.asyncTransform("success", bitmap); } }) }