1.ArkTs整改12 整改imageknife->transform

Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
zhoulisheng1 2023-09-19 19:57:46 +08:00
parent 3595190083
commit ca0e3fe968
27 changed files with 614 additions and 589 deletions

View File

@ -14,5 +14,5 @@
*/ */
import { BusinessError } from '@ohos.base' import { BusinessError } from '@ohos.base'
export interface AsyncTransform<T> { export interface AsyncTransform<T> {
asyncTransform:(err:BusinessError|string, data: T)=>void asyncTransform:(err:BusinessError|string, data: T | null)=>void
} }

View File

@ -21,7 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils"
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { fastBlur } from "../utils/FastBlur" import { fastBlur } from "../utils/FastBlur"
import {LogUtil} from '../../imageknife/utils/LogUtil' import {LogUtil} from '../../imageknife/utils/LogUtil'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class BlurTransformation implements BaseTransform<PixelMap> { export class BlurTransformation implements BaseTransform<PixelMap> {
private _mRadius: number; private _mRadius: number;
@ -38,23 +39,20 @@ export class BlurTransformation implements BaseTransform<PixelMap> {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";BlurTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";BlurTransformation buf is empty");
if (func) { if (func) {
func(Constants.PROJECT_TAG + ";BlurTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";BlurTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
TransformUtils.getPixelMapSize(imageSource, (error, size: { TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth = size.width;
var pixelMapHeight = size.height; let pixelMapHeight = size.height;
var targetWidth = request.size.width; let targetWidth = request.size.width;
var targetHeight = request.size.height; let targetHeight = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -62,7 +60,7 @@ export class BlurTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -77,10 +75,10 @@ export class BlurTransformation implements BaseTransform<PixelMap> {
fastBlur.blur(data, this._mRadius, true, func); fastBlur.blur(data, this._mRadius, true, func);
} }
}) })
.catch((e) => { .catch((e:BusinessError) => {
LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); LogUtil.log(Constants.PROJECT_TAG + ";error:" + e);
func(e, null); func?.asyncTransform(e, null);
}) })
}) }})
} }
} }

View File

@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption"
import { LogUtil } from '../../imageknife/utils/LogUtil' import { LogUtil } from '../../imageknife/utils/LogUtil'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { GPUImageBrightnessFilter } from '@ohos/gpu_transform' 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 * 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<PixelMap> {
async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
let imageInfo = await imageSource.getImageInfo(); let imageInfo:image.ImageInfo = await imageSource.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("GrayscaleTransformation The image size does not exist."), null) func?.asyncTransform("GrayscaleTransformation The image size does not exist.", null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -66,7 +68,7 @@ export class BrightnessFilterTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -85,12 +87,12 @@ export class BrightnessFilterTransformation implements BaseTransform<PixelMap> {
let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight);
data.writeBufferToPixels(buf); data.writeBufferToPixels(buf);
if (func) { if (func) {
func("success", data); func?.asyncTransform("success", data);
} }
return; return;
} }
var dataArray = new Uint8Array(bufferData); let dataArray = new Uint8Array(bufferData);
for (let index = 0; index < dataArray.length; index += 4) { for (let index = 0; index < dataArray.length; index += 4) {
dataArray[index] = this.checkVisAble(dataArray[index] * this._mBrightness + dataArray[index]); dataArray[index] = this.checkVisAble(dataArray[index] * this._mBrightness + dataArray[index]);
@ -102,7 +104,7 @@ export class BrightnessFilterTransformation implements BaseTransform<PixelMap> {
await data.writeBufferToPixels(bufferData); await data.writeBufferToPixels(bufferData);
if (func) { if (func) {
func("", data); func?.asyncTransform("", data);
} }
} }

View File

@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption"
import { LogUtil } from '../../imageknife/utils/LogUtil' import { LogUtil } from '../../imageknife/utils/LogUtil'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { GPUImageContrastFilter } from '@ohos/gpu_transform' import { GPUImageContrastFilter } from '@ohos/gpu_transform'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
/** /**
* 以24位色图像为例子每种色彩都可以用0-255 * 以24位色图像为例子每种色彩都可以用0-255
@ -50,27 +52,27 @@ export class ContrastFilterTransformation implements BaseTransform<PixelMap> {
async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty");
if (func) { if (func!=undefined) {
func(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
let imageInfo = await imageSource.getImageInfo(); let imageInfo:image.ImageInfo = await imageSource.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("ContrastFilterTransformation The image size does not exist."), null) func?.asyncTransform("ContrastFilterTransformation The image size does not exist.", null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -78,7 +80,7 @@ export class ContrastFilterTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -97,13 +99,13 @@ export class ContrastFilterTransformation implements BaseTransform<PixelMap> {
filter.setContrast(this._mContrast) filter.setContrast(this._mContrast)
let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight);
data.writeBufferToPixels(buf); data.writeBufferToPixels(buf);
if (func) { if (func != undefined) {
func("success", data); func?.asyncTransform("success", data);
} }
return; return;
} }
var dataArray = new Uint8Array(bufferData); let dataArray = new Uint8Array(bufferData);
let brightness = 0; //亮度的偏移量可以默认0 let brightness = 0; //亮度的偏移量可以默认0
for (let index = 0; index < dataArray.length; index += 4) { for (let index = 0; index < dataArray.length; index += 4) {
@ -114,8 +116,8 @@ export class ContrastFilterTransformation implements BaseTransform<PixelMap> {
} }
await data.writeBufferToPixels(bufferData); await data.writeBufferToPixels(bufferData);
if (func) { if (func != undefined) {
func("", data); func?.asyncTransform("", data);
} }
} }

View File

@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption"
import { TransformUtils } from "../transform/TransformUtils" import { TransformUtils } from "../transform/TransformUtils"
import {LogUtil} from '../../imageknife/utils/LogUtil' import {LogUtil} from '../../imageknife/utils/LogUtil'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class CropCircleTransformation implements BaseTransform<PixelMap> { export class CropCircleTransformation implements BaseTransform<PixelMap> {
private static TAG: string = "CropCircleTransformation"; private static TAG: string = "CropCircleTransformation";
@ -36,37 +38,34 @@ export class CropCircleTransformation implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty"); LogUtil.log(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty");
if (func) { if (func!=undefined) {
func(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
var that = this;
TransformUtils.getPixelMapSize(imageSource, (error, size: { TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size | null) => {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
if (pixelMapHeight < targetHeight) { if (pixelMapHeight < targetHeight) {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
that.updatePixelMapSize(imageSource, targetWidth, targetHeight, func); this.updatePixelMapSize(imageSource, targetWidth, targetHeight, func);
}) }})
} }
private updatePixelMapSize(imageSource: any, outWith: number, outHeight: number, func?: AsyncTransform<PixelMap>) { private updatePixelMapSize(imageSource: image.ImageSource, outWith: number, outHeight: number, func?: AsyncTransform<PixelMap>) {
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: outWith, width: outWith,
@ -74,30 +73,30 @@ export class CropCircleTransformation implements BaseTransform<PixelMap> {
} }
} }
imageSource.createPixelMap(options) imageSource.createPixelMap(options)
.then(p => { .then((p:PixelMap) => {
this.transformCircle(p, func); this.transformCircle(p, func);
}) })
.catch(e => { .catch((e:BusinessError) => {
LogUtil.log(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " transform e:" + e); LogUtil.log(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " transform e:" + e);
if (func) { if (func!=undefined) {
func(Constants.PROJECT_TAG + CropCircleTransformation.TAG + "e" + e, null); func?.asyncTransform(Constants.PROJECT_TAG + CropCircleTransformation.TAG + "e" + e, null);
} }
}) })
} }
private async transformCircle(data: any, func?: AsyncTransform<PixelMap>) { private async transformCircle(data: PixelMap, func?: AsyncTransform<PixelMap>) {
let imageInfo = await data.getImageInfo(); let imageInfo:image.ImageInfo = await data.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("CropCircleTransformation The image size does not exist."), null) func?.asyncTransform("CropCircleTransformation The image size does not exist.", null)
return; return;
} }
var height = size.height; let height:number = size.height;
var width = size.width; let width:number = size.width;
this.mRadius = 0; this.mRadius = 0;
if (width > height) { if (width > height) {
this.mRadius = height / 2; this.mRadius = height / 2;
@ -107,13 +106,13 @@ export class CropCircleTransformation implements BaseTransform<PixelMap> {
this.mCenterX = width / 2; this.mCenterX = width / 2;
this.mCenterY = height / 2; this.mCenterY = height / 2;
let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); let bufferData:ArrayBuffer = new ArrayBuffer(data.getPixelBytesNumber());
await data.readPixelsToBuffer(bufferData); await data.readPixelsToBuffer(bufferData);
var dataArray = new Uint8Array(bufferData); let dataArray = new Uint8Array(bufferData);
for (var h = 0;h <= height; h++) { for (let h = 0;h <= height; h++) {
for (var w = 0;w <= width; w++) { for (let w = 0;w <= width; w++) {
if (this.isContainsCircle(w, h)) { if (this.isContainsCircle(w, h)) {
continue; continue;
} }
@ -126,15 +125,15 @@ export class CropCircleTransformation implements BaseTransform<PixelMap> {
} }
} }
await data.writeBufferToPixels(bufferData); await data.writeBufferToPixels(bufferData);
if (func) { if (func != undefined) {
func("", data); func?.asyncTransform("", data);
} }
} }
isContainsCircle(x: number, y: number): boolean { isContainsCircle(x: number, y: number): boolean {
var a = Math.pow((this.mCenterX - x), 2); let a = Math.pow((this.mCenterX - x), 2);
var b = Math.pow((this.mCenterY - y), 2); let b = Math.pow((this.mCenterY - y), 2);
var c = Math.sqrt((a + b)); let c = Math.sqrt((a + b));
return c <= this.mRadius; return c <= this.mRadius;
} }
} }

View File

@ -20,7 +20,13 @@ import { RequestOption } from "../../imageknife/RequestOption"
import { TransformUtils } from "../transform/TransformUtils" import { TransformUtils } from "../transform/TransformUtils"
import {LogUtil} from '../../imageknife/utils/LogUtil' import {LogUtil} from '../../imageknife/utils/LogUtil'
import image from "@ohos.multimedia.image" 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<PixelMap> { export class CropCircleWithBorderTransformation implements BaseTransform<PixelMap> {
private static TAG: string = "CropCircleTransformation"; private static TAG: string = "CropCircleTransformation";
private mBorderSize: number = 5; private mBorderSize: number = 5;
@ -31,11 +37,7 @@ export class CropCircleWithBorderTransformation implements BaseTransform<PixelMa
private mGColor: number = 0; private mGColor: number = 0;
private mBColor: number= 0; private mBColor: number= 0;
constructor(border_size: number, value: { constructor(border_size: number, value: rgbColor) {
r_color?: number,
g_color?: number,
b_color?: number,
}) {
this.mRColor = value.g_color; this.mRColor = value.g_color;
this.mGColor = value.g_color; this.mGColor = value.g_color;
this.mBColor = value.b_color; this.mBColor = value.b_color;
@ -55,37 +57,33 @@ export class CropCircleWithBorderTransformation implements BaseTransform<PixelMa
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
var that = this; TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size:Size|null) => {
TransformUtils.getPixelMapSize(imageSource, (error, size: {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth = size.width;
var pixelMapHeight = size.height; let pixelMapHeight = size.height;
var targetWidth = request.size.width; let targetWidth = request.size.width;
var targetHeight = request.size.height; let targetHeight = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
if (pixelMapHeight < targetHeight) { if (pixelMapHeight < targetHeight) {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
that.updatePixelMapSize(imageSource, targetWidth, targetHeight, func); this.updatePixelMapSize(imageSource, targetWidth, targetHeight, func);
}) }})
} }
private updatePixelMapSize(imageSource: any, outWith: number, outHeight: number, func?: AsyncTransform<PixelMap>) { private updatePixelMapSize(imageSource: image.ImageSource, outWith: number, outHeight: number, func?: AsyncTransform<PixelMap>) {
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: outWith, width: outWith,
@ -93,18 +91,18 @@ export class CropCircleWithBorderTransformation implements BaseTransform<PixelMa
} }
} }
imageSource.createPixelMap(options) imageSource.createPixelMap(options)
.then((pixelMap) => { .then((pixelMap:PixelMap) => {
this.transformPixelMap(pixelMap, outWith, outHeight, func); this.transformPixelMap(pixelMap, outWith, outHeight, func);
}) })
.catch(e => { .catch((e:BusinessError) => {
LogUtil.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e); LogUtil.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e);
if (func) { if (func!=undefined) {
func(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e, null); func?.asyncTransform(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e, null);
} }
}) })
} }
private async transformPixelMap(pixelMap: any, width: number, height: number, func?: AsyncTransform<PixelMap>) { private async transformPixelMap(pixelMap: PixelMap, width: number, height: number, func?: AsyncTransform<PixelMap>) {
this.mRadius = 0; this.mRadius = 0;
if (width > height) { if (width > height) {
this.mRadius = height / 2; this.mRadius = height / 2;
@ -118,7 +116,7 @@ export class CropCircleWithBorderTransformation implements BaseTransform<PixelMa
let bufferData = new ArrayBuffer(pixelMap.getPixelBytesNumber()); let bufferData = new ArrayBuffer(pixelMap.getPixelBytesNumber());
await pixelMap.readPixelsToBuffer(bufferData); await pixelMap.readPixelsToBuffer(bufferData);
var dataArray = new Uint8Array(bufferData); let dataArray = new Uint8Array(bufferData);
for (let h = 0;h <= height; h++) { for (let h = 0;h <= height; h++) {
for (let w = 0;w <= width; w++) { for (let w = 0;w <= width; w++) {
@ -148,22 +146,22 @@ export class CropCircleWithBorderTransformation implements BaseTransform<PixelMa
} }
await pixelMap.writeBufferToPixels(bufferData); await pixelMap.writeBufferToPixels(bufferData);
if (func) { if (func != undefined) {
func("", pixelMap); func?.asyncTransform("", pixelMap);
} }
} }
isContainsCircle(x: number, y: number): boolean { isContainsCircle(x: number, y: number): boolean {
var a = Math.pow((this.mCenterX - x), 2); let a:number = Math.pow((this.mCenterX - x), 2);
var b = Math.pow((this.mCenterY - y), 2); let b:number = Math.pow((this.mCenterY - y), 2);
var c = Math.sqrt((a + b)); let c:number = Math.sqrt((a + b));
return c <= this.mRadius; return c <= this.mRadius;
} }
isContainsSmallCircle(x: number, y: number): boolean { isContainsSmallCircle(x: number, y: number): boolean {
var a = Math.pow((this.mCenterX - x), 2); let a:number = Math.pow((this.mCenterX - x), 2);
var b = Math.pow((this.mCenterY - y), 2); let b:number = Math.pow((this.mCenterY - y), 2);
var c = Math.sqrt((a + b)); let c:number = Math.sqrt((a + b));
return c <= (this.mRadius - this.mBorderSize); return c <= (this.mRadius - this.mBorderSize);
} }
} }

View File

@ -18,6 +18,8 @@ import { AsyncTransform } from "../transform/AsyncTransform"
import { Constants } from "../constants/Constants" import { Constants } from "../constants/Constants"
import { RequestOption } from "../../imageknife/RequestOption" import { RequestOption } from "../../imageknife/RequestOption"
import {LogUtil} from '../../imageknife/utils/LogUtil' import {LogUtil} from '../../imageknife/utils/LogUtil'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
@ -31,8 +33,8 @@ export class CropSquareTransformation implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty", null);
} }
return; return;
} }
@ -40,21 +42,21 @@ export class CropSquareTransformation implements BaseTransform<PixelMap> {
} }
squareCrop(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { squareCrop(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
imageSource.getImageInfo() imageSource.getImageInfo()
.then((p) => { .then((p:image.ImageInfo) => {
var pw = p.size.width; let pw:number = p.size.width;
var ph = p.size.height; let ph:number = p.size.height;
var outWidth = request.size.width; let outWidth:number = request.size.width;
var outHeight = request.size.height; let outHeight:number = request.size.height;
if (pw < outWidth) { if (pw < outWidth) {
outWidth = pw; outWidth = pw;
} }
if (ph < outHeight) { if (ph < outHeight) {
outHeight = ph; outHeight = ph;
} }
var targetSize = outWidth > outHeight ? outHeight : outWidth; let targetSize:number = outWidth > outHeight ? outHeight : outWidth;
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
rotate: 0, rotate: 0,
desiredSize: { desiredSize: {
@ -68,19 +70,19 @@ export class CropSquareTransformation implements BaseTransform<PixelMap> {
} }
imageSource.createPixelMap(options) imageSource.createPixelMap(options)
.then(data => { .then(data => {
if (func) { if (func != undefined) {
func("", data); func?.asyncTransform("", data);
} }
}) })
.catch(e => { .catch((e:BusinessError) => {
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";CropSquareTransformation e:" + e, null); func?.asyncTransform(Constants.PROJECT_TAG + ";CropSquareTransformation e:" + e, null);
} }
}) })
}) })
.catch((error) => { .catch((error:BusinessError) => {
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";CropSquareTransformation error:" + error, null); func?.asyncTransform(Constants.PROJECT_TAG + ";CropSquareTransformation error:" + error, null);
} }
}) })
} }

View File

@ -20,14 +20,16 @@ import { RequestOption } from "../../imageknife/RequestOption"
import { TransformUtils } from "../transform/TransformUtils" import { TransformUtils } from "../transform/TransformUtils"
import {LogUtil} from '../../imageknife/utils/LogUtil' import {LogUtil} from '../../imageknife/utils/LogUtil'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class CropTransformation implements BaseTransform<PixelMap> { export class CropTransformation implements BaseTransform<PixelMap> {
private static TAG: string = "CropCircleTransformation"; private static TAG: string = "CropCircleTransformation";
private mWidth: number; private mWidth: number = 0;
private mHeight: number; private mHeight: number = 0;
private mCropType: CropType = CropType.CENTER; private mCropType: CropType = CropType.CENTER;
constructor(width: number, height: number, cropType?: CropType) { constructor(width: number, height: number, cropType: CropType) {
this.mWidth = width; this.mWidth = width;
this.mHeight = height; this.mHeight = height;
this.mCropType = cropType; this.mCropType = cropType;
@ -42,35 +44,32 @@ export class CropTransformation implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";CropTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";CropTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";CropTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";CropTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
TransformUtils.getPixelMapSize(imageSource, (error, size: { TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
this.mWidth = this.mWidth == 0 ? pixelMapWidth : this.mWidth; this.mWidth = this.mWidth == 0 ? pixelMapWidth : this.mWidth;
this.mHeight = this.mHeight == 0 ? pixelMapHeight : this.mHeight; this.mHeight = this.mHeight == 0 ? pixelMapHeight : this.mHeight;
var scaleX = this.mWidth / pixelMapWidth; let scaleX:number = this.mWidth / pixelMapWidth;
var scaleY = this.mHeight / pixelMapHeight; let scaleY:number = this.mHeight / pixelMapHeight;
var scale = Math.max(scaleX, scaleY); let scale:number = Math.max(scaleX, scaleY);
var scaledWidth = scale * pixelMapWidth; let scaledWidth:number = scale * pixelMapWidth;
var scaledHeight = scale * pixelMapHeight; let scaledHeight:number = scale * pixelMapHeight;
var left = (this.mWidth - scaledWidth) / 2; let left:number = (this.mWidth - scaledWidth) / 2;
var top = Math.abs(this.getTop(pixelMapHeight)); let top:number = Math.abs(this.getTop(pixelMapHeight));
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredRegion: { desiredRegion: {
size: { size: {
@ -82,23 +81,23 @@ export class CropTransformation implements BaseTransform<PixelMap> {
}, },
} }
imageSource.createPixelMap(options) imageSource.createPixelMap(options)
.then((data) => { .then((data:PixelMap) => {
func("", data); func?.asyncTransform("", data);
}) })
.catch((e) => { .catch((e:BusinessError) => {
LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); LogUtil.log(Constants.PROJECT_TAG + ";error:" + e);
func(e, null); func?.asyncTransform(e, null);
}) })
}) }})
} }
private getTop(scaledHeight: number): number{ private getTop(scaledHeight: number): number{
switch (this.mCropType.valueOf()) { switch (this.mCropType) {
case CropType.TOP.valueOf(): case CropType.TOP:
return 0; return 0;
case CropType.CENTER.valueOf(): case CropType.CENTER:
return (this.mHeight - scaledHeight) / 2; return (this.mHeight - scaledHeight) / 2;
case CropType.BOTTOM.valueOf(): case CropType.BOTTOM:
return this.mHeight - scaledHeight; return this.mHeight - scaledHeight;
default: default:
return 0; return 0;
@ -107,7 +106,7 @@ export class CropTransformation implements BaseTransform<PixelMap> {
} }
export enum CropType { export enum CropType {
TOP, TOP = 0,
CENTER, CENTER = 1,
BOTTOM BOTTOM = 2
} }

View File

@ -21,6 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils"
import {LogUtil} from '../../imageknife/utils/LogUtil' import {LogUtil} from '../../imageknife/utils/LogUtil'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { GPUImageGrayscaleFilter } from '@ohos/gpu_transform' import { GPUImageGrayscaleFilter } from '@ohos/gpu_transform'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class GrayscaleTransformation implements BaseTransform<PixelMap> { export class GrayscaleTransformation implements BaseTransform<PixelMap> {
getName() { getName() {
@ -30,27 +32,27 @@ export class GrayscaleTransformation implements BaseTransform<PixelMap> {
async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
let imageInfo = await imageSource.getImageInfo(); let imageInfo = await imageSource.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("GrayscaleTransformation The image size does not exist."), null) func?.asyncTransform("GrayscaleTransformation The image size does not exist.", null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -58,14 +60,14 @@ export class GrayscaleTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
height: targetHeight height: targetHeight
} }
} }
let data = await imageSource.createPixelMap(options); let data:PixelMap= await imageSource.createPixelMap(options);
let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); let bufferData = new ArrayBuffer(data.getPixelBytesNumber());
let bufferNewData = new ArrayBuffer(data.getPixelBytesNumber()); let bufferNewData = new ArrayBuffer(data.getPixelBytesNumber());
@ -76,15 +78,15 @@ export class GrayscaleTransformation implements BaseTransform<PixelMap> {
filter.setImageData(bufferData, targetWidth, targetHeight); filter.setImageData(bufferData, targetWidth, targetHeight);
let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight);
data.writeBufferToPixels(buf); data.writeBufferToPixels(buf);
if (func) { if (func != undefined) {
func("success", data); func?.asyncTransform("success", data);
} }
return; return;
} }
var dataArray = new Uint8Array(bufferData); let dataArray = new Uint8Array(bufferData);
var dataNewArray = new Uint8Array(bufferNewData); let dataNewArray = new Uint8Array(bufferNewData);
for (let index = 0; index < dataArray.length; index += 4) { for (let index = 0; index < dataArray.length; index += 4) {
const r = dataArray[index]; const r = dataArray[index];
@ -99,8 +101,8 @@ export class GrayscaleTransformation implements BaseTransform<PixelMap> {
} }
await data.writeBufferToPixels(bufferNewData); await data.writeBufferToPixels(bufferNewData);
if (func) { if (func != undefined) {
func('', data); func?.asyncTransform('', data);
} }
} }

View File

@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption"
import {LogUtil} from '../../imageknife/utils/LogUtil' import {LogUtil} from '../../imageknife/utils/LogUtil'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { GPUImageColorInvertFilter } from '@ohos/gpu_transform' 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 ** Image inversion is particularly useful for enhancing white or gray detail in
@ -37,28 +39,28 @@ export class InvertFilterTransformation implements BaseTransform<PixelMap> {
async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
let imageInfo = await imageSource.getImageInfo(); let imageInfo:image.ImageInfo = await imageSource.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("InvertFilterTransformation The image size does not exist."), null) func?.asyncTransform("InvertFilterTransformation The image size does not exist.", null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -66,7 +68,7 @@ export class InvertFilterTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -74,7 +76,7 @@ export class InvertFilterTransformation implements BaseTransform<PixelMap> {
} }
} }
let data = await imageSource.createPixelMap(options); let data:PixelMap = await imageSource.createPixelMap(options);
let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); let bufferData = new ArrayBuffer(data.getPixelBytesNumber());
await data.readPixelsToBuffer(bufferData); await data.readPixelsToBuffer(bufferData);
@ -84,14 +86,14 @@ export class InvertFilterTransformation implements BaseTransform<PixelMap> {
filter.setImageData(bufferData, targetWidth, targetHeight); filter.setImageData(bufferData, targetWidth, targetHeight);
let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight);
data.writeBufferToPixels(buf); data.writeBufferToPixels(buf);
if (func) { if (func != undefined) {
func("success", data); func?.asyncTransform("success", data);
} }
return; return;
} }
var dataArray = new Uint8Array(bufferData); let dataArray = new Uint8Array(bufferData);
for (let index = 0; index < dataArray.length; index += 4) { for (let index = 0; index < dataArray.length; index += 4) {
dataArray[index] = this.checkVisAble(255 - dataArray[index]); dataArray[index] = this.checkVisAble(255 - dataArray[index]);
@ -99,8 +101,8 @@ export class InvertFilterTransformation implements BaseTransform<PixelMap> {
dataArray[index+2] = this.checkVisAble(255 - dataArray[index+2]); dataArray[index+2] = this.checkVisAble(255 - dataArray[index+2]);
} }
await data.writeBufferToPixels(bufferData); await data.writeBufferToPixels(bufferData);
if (func) { if (func != undefined) {
func('', data); func?.asyncTransform('', data);
} }
} }

View File

@ -21,10 +21,12 @@ import { TransformUtils } from "../transform/TransformUtils"
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { LogUtil } from '../../imageknife/utils/LogUtil' import { LogUtil } from '../../imageknife/utils/LogUtil'
import { GPUImageKuwaharaFilter } from '@ohos/gpu_transform' import { GPUImageKuwaharaFilter } from '@ohos/gpu_transform'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class KuwaharaFilterTransform implements BaseTransform<PixelMap> { export class KuwaharaFilterTransform implements BaseTransform<PixelMap> {
private _mRadius: number; private _mRadius: number = 0;
constructor(radius: number) { constructor(radius: number) {
this._mRadius = radius; this._mRadius = radius;
@ -37,32 +39,29 @@ export class KuwaharaFilterTransform implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";KuwaharaFilterTransform buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";KuwaharaFilterTransform buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";KuwaharaFilterTransform buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";KuwaharaFilterTransform buf is empty", null);
} }
return; return;
} }
if (!request.gpuEnabled) { if (!request.gpuEnabled) {
LogUtil.error(Constants.PROJECT_TAG + ";the KuwaharaFilterTransform supported only in GPU mode"); LogUtil.error(Constants.PROJECT_TAG + ";the KuwaharaFilterTransform supported only in GPU mode");
if (func) { 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; return;
} }
var that = this;
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
TransformUtils.getPixelMapSize(imageSource, (error, size: { TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -70,7 +69,7 @@ export class KuwaharaFilterTransform implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -79,16 +78,16 @@ export class KuwaharaFilterTransform implements BaseTransform<PixelMap> {
} }
imageSource.createPixelMap(options) imageSource.createPixelMap(options)
.then((data) => { .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); 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<PixelMap>) { private async kuwahara(bitmap: image.PixelMap, targetWidth: number, targetHeight: number, func?: AsyncTransform<PixelMap>) {
let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber());
await bitmap.readPixelsToBuffer(bufferData); await bitmap.readPixelsToBuffer(bufferData);
let filter = new GPUImageKuwaharaFilter(); let filter = new GPUImageKuwaharaFilter();
@ -96,8 +95,8 @@ export class KuwaharaFilterTransform implements BaseTransform<PixelMap> {
filter.setRadius(this._mRadius); filter.setRadius(this._mRadius);
let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight) let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight)
bitmap.writeBufferToPixels(buf); bitmap.writeBufferToPixels(buf);
if (func) { if (func != undefined) {
func("success", bitmap); func?.asyncTransform("success", bitmap);
} }
} }
} }

View File

@ -23,9 +23,11 @@ import image from "@ohos.multimedia.image"
import resmgr from "@ohos.resourceManager" import resmgr from "@ohos.resourceManager"
import { ImageKnifeGlobal } from '../ImageKnifeGlobal' import { ImageKnifeGlobal } from '../ImageKnifeGlobal'
import resourceManager from '@ohos.resourceManager' import resourceManager from '@ohos.resourceManager'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class MaskTransformation implements BaseTransform<PixelMap> { export class MaskTransformation implements BaseTransform<PixelMap> {
private _mResourceData: Resource; private _mResourceData:Resource|undefined = undefined;
constructor(maskBitmap: Resource) { constructor(maskBitmap: Resource) {
this._mResourceData = maskBitmap; this._mResourceData = maskBitmap;
@ -38,27 +40,27 @@ export class MaskTransformation implements BaseTransform<PixelMap> {
async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";MaskTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";MaskTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";MaskTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";MaskTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
let imageInfo = await imageSource.getImageInfo(); let imageInfo = await imageSource.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("MaskTransformation The image size does not exist."), null) func?.asyncTransform("MaskTransformation The image size does not exist.", null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -66,7 +68,7 @@ export class MaskTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -77,33 +79,40 @@ export class MaskTransformation implements BaseTransform<PixelMap> {
.then(data => { .then(data => {
this.openInternal(data, targetWidth, targetHeight, func) this.openInternal(data, targetWidth, targetHeight, func)
}) })
.catch(e => { .catch((e:BusinessError )=> {
func(e, null); func?.asyncTransform(e, null);
}) })
} }
private openInternal(bitmap: any, width: number, height: number, func: AsyncTransform<PixelMap>) { private openInternal(bitmap: PixelMap, width: number, height: number, func?: AsyncTransform<PixelMap>) {
if (!this._mResourceData) { 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<string,Object>).resourceManager as resourceManager.ResourceManager).getMediaContent(this._mResourceData.id) let context = (ImageKnifeGlobal.getInstance().getHapContext() as Record<string,Object>)
.then(array => { if(context != undefined){
let buffer = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset); let resourceManager = context.resourceManager as resourceManager.ResourceManager
var imageSource = image.createImageSource(buffer as any); if(resourceManager != undefined && this._mResourceData != undefined)
var options = { resourceManager.getMediaContent(this._mResourceData?.id)
editable: true, .then(array => {
desiredSize: { let buffer = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset);
width: width, let imageSource:image.ImageSource = image.createImageSource(buffer);
height: height let options:image.DecodingOptions = {
} editable: true,
desiredSize: {
width: width,
height: height
} }
imageSource.createPixelMap(options) }
.then(maskBitmap => { imageSource.createPixelMap(options)
MaskUtils.mask(bitmap, maskBitmap, func) .then(maskBitmap => {
}) MaskUtils.mask(bitmap, maskBitmap, func)
}) })
.catch(err => { })
func("MaskTransformation openInternal error" + err, null); .catch((err:BusinessError) => {
}) func?.asyncTransform("MaskTransformation openInternal error" + err, null);
})
}
} }
} }

View File

@ -21,6 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils"
import { LogUtil } from '../../imageknife/utils/LogUtil' import { LogUtil } from '../../imageknife/utils/LogUtil'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { pixelUtils } from "../utils/PixelUtils" import { pixelUtils } from "../utils/PixelUtils"
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
/** /**
* Applies a Pixelation effect to the image. * Applies a Pixelation effect to the image.
@ -42,24 +44,21 @@ export class PixelationFilterTransformation implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
TransformUtils.getPixelMapSize(imageSource, (error, size: { TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size:Size|null) => {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -67,7 +66,7 @@ export class PixelationFilterTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -82,10 +81,10 @@ export class PixelationFilterTransformation implements BaseTransform<PixelMap> {
pixelUtils.pixel(data, this._mPixel, func); pixelUtils.pixel(data, this._mPixel, func);
} }
}) })
.catch((e) => { .catch((e:BusinessError) => {
LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); LogUtil.log(Constants.PROJECT_TAG + ";error:" + e);
func(e, null); func?.asyncTransform(e, null);
}) })
}) }})
} }
} }

View File

@ -18,11 +18,13 @@ import { Constants } from "../constants/Constants"
import { RequestOption } from "../../imageknife/RequestOption" import { RequestOption } from "../../imageknife/RequestOption"
import { TransformUtils } from "../transform/TransformUtils" import { TransformUtils } from "../transform/TransformUtils"
import {LogUtil} from '../../imageknife/utils/LogUtil' import {LogUtil} from '../../imageknife/utils/LogUtil'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
export class RotateImageTransformation implements BaseTransform<PixelMap> { export class RotateImageTransformation implements BaseTransform<PixelMap> {
private mDegreesToRotate: number; private mDegreesToRotate: number = 0;
constructor(degreesToRotate: number) { constructor(degreesToRotate: number) {
this.mDegreesToRotate = degreesToRotate; this.mDegreesToRotate = degreesToRotate;
@ -35,24 +37,21 @@ export class RotateImageTransformation implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
TransformUtils.getPixelMapSize(imageSource, (error, size: { TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -60,7 +59,7 @@ export class RotateImageTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
rotate: this.mDegreesToRotate%360, rotate: this.mDegreesToRotate%360,
desiredSize: { desiredSize: {
@ -70,12 +69,12 @@ export class RotateImageTransformation implements BaseTransform<PixelMap> {
} }
imageSource.createPixelMap(options) imageSource.createPixelMap(options)
.then((data) => { .then((data) => {
func("", data); func?.asyncTransform("", data);
}) })
.catch((e) => { .catch((e:BusinessError) => {
LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); LogUtil.log(Constants.PROJECT_TAG + ";error:" + e);
func(e, null); func?.asyncTransform(e, null);
}) })
}) }})
} }
} }

View File

@ -19,23 +19,27 @@ import { Constants } from "../constants/Constants"
import { RequestOption } from "../../imageknife/RequestOption" import { RequestOption } from "../../imageknife/RequestOption"
import { TransformUtils } from "../transform/TransformUtils" import { TransformUtils } from "../transform/TransformUtils"
import {LogUtil} from '../../imageknife/utils/LogUtil' import {LogUtil} from '../../imageknife/utils/LogUtil'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
import image from "@ohos.multimedia.image" 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<PixelMap> { export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
private mTop_left: number = 0; private mTop_left: number = 0;
private mTop_right: number = 0; private mTop_right: number = 0;
private mBottom_left: number = 0; private mBottom_left: number = 0;
private mBottom_right: number = 0; private mBottom_right: number = 0;
private mTransform_pixelMap: any; private mTransform_pixelMap: PixelMap|undefined = undefined;
private mPoint: ArcPoint[]; private mPoint: ArcPoint[] = new Array();
constructor(value: { constructor(value:RoundCorner) {
top_left?: number,
top_right?: number,
bottom_left?: number,
bottom_right?: number
}) {
this.mTop_left = value.top_left; this.mTop_left = value.top_left;
this.mTop_right = value.top_right; this.mTop_right = value.top_right;
this.mBottom_left = value.bottom_left; this.mBottom_left = value.bottom_left;
@ -54,37 +58,33 @@ export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
+ 'buf is null ='+ (buf == null)); + 'buf is null ='+ (buf == null));
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
var that = this; TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => {
TransformUtils.getPixelMapSize(imageSource, (error, size: {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
if (pixelMapHeight < targetHeight) { if (pixelMapHeight < targetHeight) {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
that.transformPixelMap(imageSource, targetWidth, targetHeight, func); this.transformPixelMap(imageSource, targetWidth, targetHeight, func);
}) }})
} }
private transformPixelMap(imageSource: any, outWith: number, outHeight: number, func?: AsyncTransform<PixelMap>) { private transformPixelMap(imageSource: image.ImageSource, outWith: number, outHeight: number, func?: AsyncTransform<PixelMap>) {
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: outWith, width: outWith,
@ -96,8 +96,8 @@ export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
this.mTransform_pixelMap = pixelMap; this.mTransform_pixelMap = pixelMap;
this.mTransform_pixelMap.getImageInfo() this.mTransform_pixelMap.getImageInfo()
.then((data) => { .then((data) => {
let width = data.size.width; let width:number = data.size.width;
let height = data.size.height; let height:number = data.size.height;
if (this.mTop_left > 0) { if (this.mTop_left > 0) {
this.top_left_corners(); this.top_left_corners();
} }
@ -110,18 +110,18 @@ export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
if (this.mBottom_right > 0) { if (this.mBottom_right > 0) {
this.bottom_right_corners(width, height); this.bottom_right_corners(width, height);
} }
if (func) { if (func != undefined && this.mTransform_pixelMap != undefined) {
func("", this.mTransform_pixelMap); func?.asyncTransform("", this.mTransform_pixelMap);
} }
}) })
.catch((error) => { .catch((error:BusinessError) => {
LogUtil.log(Constants.PROJECT_TAG + "RoundedCornersTransformation error:" + error); LogUtil.log(Constants.PROJECT_TAG + "RoundedCornersTransformation error:" + error);
}); });
}) })
.catch((e) => { .catch((e:BusinessError) => {
LogUtil.log(Constants.PROJECT_TAG + ";error:" + e); LogUtil.log(Constants.PROJECT_TAG + ";error:" + e);
if (func) { if (func != undefined) {
func(e, null); func?.asyncTransform(e, null);
} }
}) })
@ -129,10 +129,10 @@ export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
private top_left_corners() { private top_left_corners() {
this.mPoint = new Array<ArcPoint>(); this.mPoint = new Array<ArcPoint>();
for (var time = 180; time < 270; time++) { for (let time = 180; time < 270; time++) {
var hudu = (2 * Math.PI / 360) * time; let hudu:number = (2 * Math.PI / 360) * time;
var x = this.mTop_left + Math.sin(hudu) * this.mTop_left; let x:number= this.mTop_left + Math.sin(hudu) * this.mTop_left;
var y = this.mTop_left + Math.cos(hudu) * this.mTop_left; let y:number = this.mTop_left + Math.cos(hudu) * this.mTop_left;
this.mPoint.push(new ArcPoint(x, y)); this.mPoint.push(new ArcPoint(x, y));
} }
this.hand_pixel_point(this.mPoint); this.hand_pixel_point(this.mPoint);
@ -140,10 +140,10 @@ export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
private top_right_corners(width: number) { private top_right_corners(width: number) {
this.mPoint = new Array<ArcPoint>(); this.mPoint = new Array<ArcPoint>();
for (var time = 0; time < 90; time++) { for (let time = 0; time < 90; time++) {
var hudu = (2 * Math.PI / 360) * time; let hudu:number = (2 * Math.PI / 360) * time;
var x = (width - this.mTop_right) + Math.cos(hudu) * this.mTop_right; let x:number = (width - this.mTop_right) + Math.cos(hudu) * this.mTop_right;
var y = this.mTop_right - Math.sin(hudu) * this.mTop_right; let y:number = this.mTop_right - Math.sin(hudu) * this.mTop_right;
this.mPoint.push(new ArcPoint(x, y)); this.mPoint.push(new ArcPoint(x, y));
} }
@ -152,10 +152,10 @@ export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
private bottom_left_corners(width: number, height: number) { private bottom_left_corners(width: number, height: number) {
this.mPoint = new Array<ArcPoint>(); this.mPoint = new Array<ArcPoint>();
for (var time = 90; time < 180; time++) { for (let time = 90; time < 180; time++) {
var hudu = (2 * Math.PI / 360) * time; let hudu = (2 * Math.PI / 360) * time;
var x = this.mBottom_left + Math.cos(hudu) * this.mBottom_left; let x = this.mBottom_left + Math.cos(hudu) * this.mBottom_left;
var y = height - this.mBottom_left + Math.sin(hudu) * this.mBottom_left; let y = height - this.mBottom_left + Math.sin(hudu) * this.mBottom_left;
this.mPoint.push(new ArcPoint(x, y)); this.mPoint.push(new ArcPoint(x, y));
} }
this.hand_pixel_point(this.mPoint); this.hand_pixel_point(this.mPoint);
@ -163,25 +163,25 @@ export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
private bottom_right_corners(width: number, height: number) { private bottom_right_corners(width: number, height: number) {
this.mPoint = new Array<ArcPoint>(); this.mPoint = new Array<ArcPoint>();
for (var time = 0; time < 90; time++) { for (let time = 0; time < 90; time++) {
var hudu = (2 * Math.PI / 360) * time; let hudu = (2 * Math.PI / 360) * time;
var x = width - this.mBottom_right + Math.cos(hudu) * this.mBottom_right; let x = width - this.mBottom_right + Math.cos(hudu) * this.mBottom_right;
var y = height - this.mBottom_right + Math.sin(hudu) * this.mBottom_right; let y = height - this.mBottom_right + Math.sin(hudu) * this.mBottom_right;
this.mPoint.push(new ArcPoint(x, y)); this.mPoint.push(new ArcPoint(x, y));
} }
this.hand_right_pixel_point(this.mPoint, width); this.hand_right_pixel_point(this.mPoint, width);
} }
private hand_pixel_point(points: ArcPoint[]) { 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]; const element = points[index];
let x = element.getX(); let x = element.getX();
let y = element.getY(); let y = element.getY();
for (var i = 0; i <= x; i++) { for (let i = 0; i <= x; i++) {
var buffer1 = new ArrayBuffer(5); let buffer1 = new ArrayBuffer(5);
var bytes1 = new Uint8Array(buffer1); let bytes1 = new Uint8Array(buffer1);
var writePositionRen = { let writePositionRen:image.PositionArea = {
pixels: buffer1, pixels: buffer1,
offset: 1, offset: 1,
stride: 1024, stride: 1024,
@ -193,21 +193,23 @@ export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
for (let j = 0;j < 5; j++) { for (let j = 0;j < 5; j++) {
bytes1[j] = 0; 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) { 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]; const element = points[index];
let x = element.getX(); let x = element.getX();
let y = element.getY(); let y = element.getY();
for (var i = x; i <= width; i++) { for (let i = x; i <= width; i++) {
var buffer1 = new ArrayBuffer(5); let buffer1 = new ArrayBuffer(5);
var bytes1 = new Uint8Array(buffer1); let bytes1 = new Uint8Array(buffer1);
var writePositionRen = { let writePositionRen:image.PositionArea = {
pixels: buffer1, pixels: buffer1,
offset: 0, offset: 0,
stride: 4, stride: 4,
@ -219,7 +221,9 @@ export class RoundedCornersTransformation implements BaseTransform<PixelMap> {
for (let j = 0;j < 5; j++) { for (let j = 0;j < 5; j++) {
bytes1[j] = 0; bytes1[j] = 0;
} }
this.mTransform_pixelMap.writePixels(writePositionRen); if(this.mTransform_pixelMap != undefined) {
this.mTransform_pixelMap.writePixels(writePositionRen);
}
} }
} }
} }

View File

@ -20,6 +20,8 @@ import { RequestOption } from "../../imageknife/RequestOption"
import { LogUtil } from '../../imageknife/utils/LogUtil' import { LogUtil } from '../../imageknife/utils/LogUtil'
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { GPUImageSepiaToneFilter } from '@ohos/gpu_transform' import { GPUImageSepiaToneFilter } from '@ohos/gpu_transform'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
/** /**
* Applies a simple sepia effect. * Applies a simple sepia effect.
@ -34,28 +36,28 @@ export class SepiaFilterTransformation implements BaseTransform<PixelMap> {
async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty");
if (func) { if (func!=undefined) {
func(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty", null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
let imageInfo = await imageSource.getImageInfo(); let imageInfo:image.ImageInfo = await imageSource.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("SepiaFilterTransformation The image size does not exist."), null) func?.asyncTransform("SepiaFilterTransformation The image size does not exist.", null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -63,14 +65,14 @@ export class SepiaFilterTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
height: targetHeight height: targetHeight
} }
} }
let data = await imageSource.createPixelMap(options); let data:PixelMap = await imageSource.createPixelMap(options);
let bufferData = new ArrayBuffer(data.getPixelBytesNumber()); let bufferData = new ArrayBuffer(data.getPixelBytesNumber());
await data.readPixelsToBuffer(bufferData); await data.readPixelsToBuffer(bufferData);
@ -80,21 +82,21 @@ export class SepiaFilterTransformation implements BaseTransform<PixelMap> {
filter.setImageData(bufferData, targetWidth, targetHeight); filter.setImageData(bufferData, targetWidth, targetHeight);
let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight); let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight);
data.writeBufferToPixels(buf); data.writeBufferToPixels(buf);
if (func) { if (func != undefined) {
func("success", data); func?.asyncTransform("success", data);
} }
return; return;
} }
let bufferNewData = new ArrayBuffer(data.getPixelBytesNumber()); let bufferNewData = new ArrayBuffer(data.getPixelBytesNumber());
var dataArray = new Uint8Array(bufferData); let dataArray = new Uint8Array(bufferData);
var dataNewArray = new Uint8Array(bufferNewData); let dataNewArray = new Uint8Array(bufferNewData);
for (let index = 0; index < dataArray.length; index += 4) { for (let index = 0; index < dataArray.length; index += 4) {
const r = dataArray[index]; const r:number = dataArray[index];
const g = dataArray[index+1]; const g:number = dataArray[index+1];
const b = dataArray[index+2]; const b:number = dataArray[index+2];
const f = dataArray[index+3]; const f:number = dataArray[index+3];
dataNewArray[index+2] = this.checkVisAble(this.colorBlend(this.noise() dataNewArray[index+2] = this.checkVisAble(this.colorBlend(this.noise()
, (r * 0.272) + (g * 0.534) + (b * 0.131) , (r * 0.272) + (g * 0.534) + (b * 0.131)
@ -109,8 +111,8 @@ export class SepiaFilterTransformation implements BaseTransform<PixelMap> {
} }
await data.writeBufferToPixels(bufferNewData); await data.writeBufferToPixels(bufferNewData);
if (func) { if (func != undefined) {
func("", data); func?.asyncTransform("", data);
} }
} }

View File

@ -20,6 +20,8 @@ import { RequestOption } from '../../imageknife/RequestOption'
import { TransformUtils } from '../transform/TransformUtils' import { TransformUtils } from '../transform/TransformUtils'
import image from '@ohos.multimedia.image' import image from '@ohos.multimedia.image'
import { CalculatePixelUtils } from '../utils/CalculatePixelUtils' import { CalculatePixelUtils } from '../utils/CalculatePixelUtils'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class SketchFilterTransformation implements BaseTransform<PixelMap> { export class SketchFilterTransformation implements BaseTransform<PixelMap> {
getName() { getName() {
@ -28,25 +30,21 @@ export class SketchFilterTransformation implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
throw new Error(Constants.PROJECT_TAG + ';SketchFilterTransformation buf is empty'); if (func != undefined) {
if (func) { func?.asyncTransform(Constants.PROJECT_TAG + ';SketchFilterTransformation buf is empty', null);
func(Constants.PROJECT_TAG + ';SketchFilterTransformation buf is empty', null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
TransformUtils.getPixelMapSize(imageSource, (error, size: { TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth: number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight: number = size.height;
var targetWidth = request.size.width; let targetWidth: number = request.size.width;
var targetHeight = request.size.height; let targetHeight: number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -54,7 +52,7 @@ export class SketchFilterTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options: image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -69,9 +67,9 @@ export class SketchFilterTransformation implements BaseTransform<PixelMap> {
CalculatePixelUtils.sketch(data, func); CalculatePixelUtils.sketch(data, func);
} }
}) })
.catch((e) => { .catch((e:BusinessError) => {
func(e, null); func?.asyncTransform(e, null);
}) })
}) }})
} }
} }

View File

@ -23,6 +23,8 @@ import { PixelEntry } from '../entry/PixelEntry'
import { ColorUtils } from '../utils/ColorUtils' import { ColorUtils } from '../utils/ColorUtils'
import { CalculatePixelUtils } from '../utils/CalculatePixelUtils' import { CalculatePixelUtils } from '../utils/CalculatePixelUtils'
import { GPUImageSwirlFilter } from '@ohos/gpu_transform' import { GPUImageSwirlFilter } from '@ohos/gpu_transform'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class SwirlFilterTransformation implements BaseTransform<PixelMap> { export class SwirlFilterTransformation implements BaseTransform<PixelMap> {
private radius: number = 0; private radius: number = 0;
@ -47,25 +49,21 @@ export class SwirlFilterTransformation implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
throw new Error(Constants.PROJECT_TAG + ';SwirlFilterTransformation buf is empty'); if (func != undefined) {
if (func) { func?.asyncTransform(Constants.PROJECT_TAG + ';SwirlFilterTransformation buf is empty', null);
func(Constants.PROJECT_TAG + ';SwirlFilterTransformation buf is empty', null);
} }
return; return;
} }
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
TransformUtils.getPixelMapSize(imageSource, (error, size: { TransformUtils.getPixelMapSize(imageSource,{asyncTransform: (error:BusinessError|string, size:Size|null) => {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -73,7 +71,7 @@ export class SwirlFilterTransformation implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -84,23 +82,23 @@ export class SwirlFilterTransformation implements BaseTransform<PixelMap> {
.then((data) => { .then((data) => {
this.swirl(data, this.radius, request, func); this.swirl(data, this.radius, request, func);
}) })
.catch((e) => { .catch((e:BusinessError) => {
func(e, null); func?.asyncTransform(e, null);
}) })
}) }})
} }
private async swirl(bitmap: image.PixelMap, degree: number, request: RequestOption, func?: AsyncTransform<PixelMap>) { private async swirl(bitmap: image.PixelMap, degree: number, request: RequestOption, func?: AsyncTransform<PixelMap>) {
let imageInfo = await bitmap.getImageInfo(); let imageInfo:image.ImageInfo = await bitmap.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
return; return;
} }
let width = size.width; let width:number = size.width;
let height = size.height; let height:number = size.height;
let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber());
@ -113,8 +111,8 @@ export class SwirlFilterTransformation implements BaseTransform<PixelMap> {
filter.setCenter(this._xCenter, this._yCenter) filter.setCenter(this._xCenter, this._yCenter)
let buf = await filter.getPixelMapBuf(0, 0, width, height); let buf = await filter.getPixelMapBuf(0, 0, width, height);
bitmap.writeBufferToPixels(buf); bitmap.writeBufferToPixels(buf);
if (func) { if (func != undefined) {
func("success", bitmap); func?.asyncTransform("success", bitmap);
} }
return; return;
} }
@ -125,13 +123,13 @@ export class SwirlFilterTransformation implements BaseTransform<PixelMap> {
let dataArray = new Uint8Array(bufferData); let dataArray = new Uint8Array(bufferData);
let ph = 0; let ph:number = 0;
let pw = 0; let pw:number = 0;
for (let index = 0; index < dataArray.length; index += 4) { for (let index = 0; index < dataArray.length; index += 4) {
const r = dataArray[index]; const r:number = dataArray[index];
const g = dataArray[index+1]; const g:number = dataArray[index+1];
const b = dataArray[index+2]; const b:number = dataArray[index+2];
const f = dataArray[index+3]; const f:number = dataArray[index+3];
let entry = new PixelEntry(); let entry = new PixelEntry();
entry.a = 0; entry.a = 0;
@ -201,8 +199,8 @@ export class SwirlFilterTransformation implements BaseTransform<PixelMap> {
index++; index++;
} }
await bitmap.writeBufferToPixels(bufferNewData); await bitmap.writeBufferToPixels(bufferNewData);
if (func) { if (func != undefined) {
func("", bitmap); func?.asyncTransform("", bitmap);
} }
} }
} }

View File

@ -21,7 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils"
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { LogUtil } from '../../imageknife/utils/LogUtil' import { LogUtil } from '../../imageknife/utils/LogUtil'
import { GPUImageToonFilter } from '@ohos/gpu_transform' import { GPUImageToonFilter } from '@ohos/gpu_transform'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class ToonFilterTransform implements BaseTransform<PixelMap> { export class ToonFilterTransform implements BaseTransform<PixelMap> {
private threshold: number = 0.2; private threshold: number = 0.2;
@ -43,32 +44,29 @@ export class ToonFilterTransform implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";ToonFilterTransform buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";ToonFilterTransform buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";ToonFilterTransform buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";ToonFilterTransform buf is empty", null);
} }
return; return;
} }
if (!request.gpuEnabled) { if (!request.gpuEnabled) {
LogUtil.error(Constants.PROJECT_TAG + ";the ToonFilterTransform supported only in GPU mode"); LogUtil.error(Constants.PROJECT_TAG + ";the ToonFilterTransform supported only in GPU mode");
if (func) { 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; return;
} }
var that = this;
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
TransformUtils.getPixelMapSize(imageSource, (error, size: { TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -76,7 +74,7 @@ export class ToonFilterTransform implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -85,16 +83,16 @@ export class ToonFilterTransform implements BaseTransform<PixelMap> {
} }
imageSource.createPixelMap(options) imageSource.createPixelMap(options)
.then((data) => { .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); 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<PixelMap>) { private async toon(bitmap: image.PixelMap, targetWidth: number, targetHeight: number, func?: AsyncTransform<PixelMap>) {
let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber());
await bitmap.readPixelsToBuffer(bufferData); await bitmap.readPixelsToBuffer(bufferData);
let filter = new GPUImageToonFilter(); let filter = new GPUImageToonFilter();
@ -103,8 +101,8 @@ export class ToonFilterTransform implements BaseTransform<PixelMap> {
filter.setQuantizationLevels(this.quantizationLevels); filter.setQuantizationLevels(this.quantizationLevels);
let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight) let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight)
bitmap.writeBufferToPixels(buf); bitmap.writeBufferToPixels(buf);
if (func) { if (func != undefined) {
func("success", bitmap); func?.asyncTransform("success", bitmap);
} }
} }
} }

View File

@ -14,18 +14,19 @@
*/ */
import {AsyncTransform} from '../transform/AsyncTransform' import {AsyncTransform} from '../transform/AsyncTransform'
import image from '@ohos.multimedia.image' import image from '@ohos.multimedia.image'
import {Size} from '../../imageknife/RequestOption'
import { BusinessError } from '@ohos.base'
export class TransformUtils { export class TransformUtils {
static centerCrop(buf: ArrayBuffer, outWidth: number, outHeihgt: number, static centerCrop(buf: ArrayBuffer, outWidth: number, outHeihgt: number,
callback?: AsyncTransform<Promise<PixelMap>>) { callback?: AsyncTransform<Promise<PixelMap>>) {
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
imageSource.getImageInfo() imageSource.getImageInfo()
.then((p) => { .then((p) => {
var sw; let sw:number=0;
var sh; let sh:number=0;
var scale; let scale:number=1;
var pw = p.size.width; let pw:number = p.size.width;
var ph = p.size.height; let ph:number = p.size.height;
if (pw == outWidth && ph == outHeihgt) { if (pw == outWidth && ph == outHeihgt) {
sw = outWidth; sw = outWidth;
sh = outHeihgt; sh = outHeihgt;
@ -38,7 +39,7 @@ export class TransformUtils {
sw = pw * scale; sw = pw * scale;
sh = ph * scale; sh = ph * scale;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
rotate: 0, rotate: 0,
desiredRegion: { size: { width: sw, height: sh }, desiredRegion: { size: { width: sw, height: sh },
@ -47,18 +48,18 @@ export class TransformUtils {
}, },
} }
if (callback) { if (callback) {
callback('', imageSource.createPixelMap(options)); callback.asyncTransform('', imageSource.createPixelMap(options));
} }
}) })
.catch((error) => { .catch((error:BusinessError) => {
callback(error, null); callback?.asyncTransform(error, null);
}) })
} }
static rotateImage(buf: ArrayBuffer, degreesToRotate: number): Promise<PixelMap>{ static rotateImage(buf: ArrayBuffer, degreesToRotate: number): Promise<PixelMap>{
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
rotate: degreesToRotate rotate: degreesToRotate
} }
@ -67,36 +68,36 @@ export class TransformUtils {
static centerInside(buf: ArrayBuffer, outWidth: number, outHeihgt: number, static centerInside(buf: ArrayBuffer, outWidth: number, outHeihgt: number,
callback?: AsyncTransform<Promise<PixelMap>>) { callback?: AsyncTransform<Promise<PixelMap>>) {
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
imageSource.getImageInfo() imageSource.getImageInfo()
.then((p) => { .then((p:image.ImageInfo) => {
var pw = p.size.width; let pw = p.size.width;
var ph = p.size.height; let ph = p.size.height;
if (pw <= outWidth && ph <= outHeihgt) { if (pw <= outWidth && ph <= outHeihgt) {
callback('', imageSource.createPixelMap()); callback?.asyncTransform('', imageSource.createPixelMap());
} else { } else {
this.fitCenter(buf, outWidth, outHeihgt, callback); TransformUtils.fitCenter(buf, outWidth, outHeihgt, callback);
} }
}) })
.catch((error) => { .catch((error:BusinessError) => {
callback(error, null); callback?.asyncTransform(error, null);
}) })
} }
static fitCenter(buf: ArrayBuffer, outWidth: number, outHeihgt: number static fitCenter(buf: ArrayBuffer, outWidth: number, outHeihgt: number
, callback?: AsyncTransform<Promise<PixelMap>>) { , callback?: AsyncTransform<Promise<PixelMap>>) {
var imageSource = image.createImageSource(buf as any); let imageSource:image.ImageSource = image.createImageSource(buf);
imageSource.getImageInfo() imageSource.getImageInfo()
.then((p) => { .then((p:image.ImageInfo) => {
var pw = p.size.width; let pw:number = p.size.width;
var ph = p.size.height; let ph:number = p.size.height;
var widthPercentage = outWidth / pw; let widthPercentage:number = outWidth / pw;
var heightPercentage = outHeihgt / ph; let heightPercentage:number = outHeihgt / ph;
var minPercentage = Math.min(widthPercentage, heightPercentage); let minPercentage:number = Math.min(widthPercentage, heightPercentage);
var targetWidth = Math.round(minPercentage * pw); let targetWidth:number = Math.round(minPercentage * pw);
var targetHeight = Math.round(minPercentage * ph); let targetHeight:number = Math.round(minPercentage * ph);
if (pw == targetWidth && ph == targetHeight) { if (pw == targetWidth && ph == targetHeight) {
targetWidth = pw; targetWidth = pw;
@ -105,37 +106,34 @@ export class TransformUtils {
targetWidth = minPercentage * pw; targetWidth = minPercentage * pw;
targetHeight = minPercentage * ph; targetHeight = minPercentage * ph;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
rotate: 0, rotate: 0,
desiredSize: { width: targetWidth, height: targetHeight } desiredSize: { width: targetWidth, height: targetHeight }
} }
if (callback) { if (callback) {
callback('', imageSource.createPixelMap(options)); callback.asyncTransform('', imageSource.createPixelMap(options));
} }
}) })
.catch(e => { .catch((e:BusinessError) => {
if (callback) { if (callback) {
callback(e, null); callback.asyncTransform(e, null);
} }
}) })
} }
static getPixelMapSize(imageSource: any, func: AsyncTransform<{ static getPixelMapSize(imageSource: image.ImageSource, func: AsyncTransform<Size>) {
width: number,
height: number
}>) {
if (!imageSource) { if (!imageSource) {
return; return;
} }
imageSource.getImageInfo((err, value) => { imageSource.getImageInfo((err:BusinessError, value:image.ImageInfo) => {
if (err) { if (err) {
func(err, null) func?.asyncTransform(err, null)
return; return;
} }
var pWidth = value.size.width; let pWidth:number = value.size.width;
var pHeight = value.size.height; let pHeight:number = value.size.height;
func('', { width: pWidth, height: pHeight }); func?.asyncTransform('', { width: pWidth, height: pHeight });
}) })
} }
} }

View File

@ -21,6 +21,8 @@ import { TransformUtils } from "../transform/TransformUtils"
import image from "@ohos.multimedia.image" import image from "@ohos.multimedia.image"
import { LogUtil } from '../../imageknife/utils/LogUtil' import { LogUtil } from '../../imageknife/utils/LogUtil'
import { GPUImageVignetterFilter } from '@ohos/gpu_transform' import { GPUImageVignetterFilter } from '@ohos/gpu_transform'
import { BusinessError } from '@ohos.base'
import {Size} from '../../imageknife/RequestOption'
export class VignetteFilterTransform implements BaseTransform<PixelMap> { export class VignetteFilterTransform implements BaseTransform<PixelMap> {
@ -47,32 +49,28 @@ export class VignetteFilterTransform implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
LogUtil.log(Constants.PROJECT_TAG + ";VignetteFilterTransform buf is empty"); LogUtil.log(Constants.PROJECT_TAG + ";VignetteFilterTransform buf is empty");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";VignetteFilterTransform buf is empty", null); func?.asyncTransform(Constants.PROJECT_TAG + ";VignetteFilterTransform buf is empty", null);
} }
return; return;
} }
if (!request.gpuEnabled) { if (!request.gpuEnabled) {
LogUtil.error(Constants.PROJECT_TAG + ";the VignetteFilterTransform supported only in GPU mode"); LogUtil.error(Constants.PROJECT_TAG + ";the VignetteFilterTransform supported only in GPU mode");
if (func) { if (func != undefined) {
func(Constants.PROJECT_TAG + ";the VignetteFilterTransform supported only in GPU mode", null); func?.asyncTransform(Constants.PROJECT_TAG + ";the VignetteFilterTransform supported only in GPU mode", null);
} }
return; return;
} }
var that = this; let imageSource:image.ImageSource = image.createImageSource(buf);
var imageSource = image.createImageSource(buf as any); TransformUtils.getPixelMapSize(imageSource, {asyncTransform:(error:BusinessError|string, size: Size|null) => {
TransformUtils.getPixelMapSize(imageSource, (error, size: {
width: number,
height: number
}) => {
if (!size) { if (!size) {
func(error, null) func?.asyncTransform(error, null)
return; return;
} }
var pixelMapWidth = size.width; let pixelMapWidth:number = size.width;
var pixelMapHeight = size.height; let pixelMapHeight:number = size.height;
var targetWidth = request.size.width; let targetWidth:number = request.size.width;
var targetHeight = request.size.height; let targetHeight:number = request.size.height;
if (pixelMapWidth < targetWidth) { if (pixelMapWidth < targetWidth) {
targetWidth = pixelMapWidth; targetWidth = pixelMapWidth;
} }
@ -80,7 +78,7 @@ export class VignetteFilterTransform implements BaseTransform<PixelMap> {
targetHeight = pixelMapHeight; targetHeight = pixelMapHeight;
} }
var options = { let options:image.DecodingOptions = {
editable: true, editable: true,
desiredSize: { desiredSize: {
width: targetWidth, width: targetWidth,
@ -89,16 +87,16 @@ export class VignetteFilterTransform implements BaseTransform<PixelMap> {
} }
imageSource.createPixelMap(options) imageSource.createPixelMap(options)
.then((data) => { .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); 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<PixelMap>) { private async vignette(bitmap: image.PixelMap, targetWidth: number, targetHeight: number, func?: AsyncTransform<PixelMap>) {
let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber());
await bitmap.readPixelsToBuffer(bufferData); await bitmap.readPixelsToBuffer(bufferData);
let filter = new GPUImageVignetterFilter(); let filter = new GPUImageVignetterFilter();
@ -109,8 +107,8 @@ export class VignetteFilterTransform implements BaseTransform<PixelMap> {
filter.setVignetteEnd(this.vignetteSpace[1]); filter.setVignetteEnd(this.vignetteSpace[1]);
let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight) let buf = await filter.getPixelMapBuf(0, 0, targetWidth, targetHeight)
bitmap.writeBufferToPixels(buf); bitmap.writeBufferToPixels(buf);
if (func) { if (func != undefined) {
func("success", bitmap); func?.asyncTransform("success", bitmap);
} }
} }
} }

View File

@ -18,7 +18,7 @@ import {AsyncTransform} from '../AsyncTransform'
import {Constants} from '../../constants/Constants' import {Constants} from '../../constants/Constants'
import {TransformUtils} from '../TransformUtils' import {TransformUtils} from '../TransformUtils'
import {RequestOption} from '../../../imageknife/RequestOption' import {RequestOption} from '../../../imageknife/RequestOption'
import { BusinessError } from '@ohos.base'
export class CenterCrop implements BaseTransform<PixelMap> { export class CenterCrop implements BaseTransform<PixelMap> {
getName() { getName() {
return 'CenterCrop:' + this; return 'CenterCrop:' + this;
@ -26,18 +26,17 @@ export class CenterCrop implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
throw new Error(Constants.PROJECT_TAG + ';CenterCrop buf is empty'); if (func != undefined) {
if (func) { func?.asyncTransform(Constants.PROJECT_TAG + ';CenterCrop buf is empty', null);
func(Constants.PROJECT_TAG + ';CenterCrop buf is empty', null);
} }
return; return;
} }
TransformUtils.centerCrop(buf, request.size.width, request.size.height, (error, data) => { TransformUtils.centerCrop(buf, request.size.width, request.size.height, {asyncTransform:(error:BusinessError|string, data:Promise<PixelMap>|null) => {
data.then(p => { data?.then(p => {
func('', p); func?.asyncTransform('', p);
}).catch(e => { }).catch((e:BusinessError) => {
func(Constants.PROJECT_TAG + ';CenterCrop error:' + e, null); func?.asyncTransform(Constants.PROJECT_TAG + ';CenterCrop error:' + e, null);
}) })
}) }})
} }
} }

View File

@ -13,12 +13,12 @@
* limitations under the License. * limitations under the License.
*/ */
import {BaseTransform} from '../BaseTransform' import {BaseTransform} from '../BaseTransform'
import {AsyncTransform} from '../AsyncTransform' import {AsyncTransform} from '../AsyncTransform'
import {Constants} from '../../constants/Constants' import {Constants} from '../../constants/Constants'
import {TransformUtils} from '../TransformUtils' import {TransformUtils} from '../TransformUtils'
import {RequestOption} from '../../../imageknife/RequestOption' import {RequestOption} from '../../../imageknife/RequestOption'
import { BusinessError } from '@ohos.base'
export class CenterInside implements BaseTransform<PixelMap> { export class CenterInside implements BaseTransform<PixelMap> {
getName() { getName() {
return 'CenterInside:' + this; return 'CenterInside:' + this;
@ -26,18 +26,17 @@ export class CenterInside implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
throw new Error(Constants.PROJECT_TAG + ';CenterInside buf is empty'); if (func != undefined) {
if (func) { func?.asyncTransform(Constants.PROJECT_TAG + ';CenterInside buf is empty', null);
func(Constants.PROJECT_TAG + ';CenterInside buf is empty', null);
} }
return; return;
} }
TransformUtils.centerInside(buf, request.size.width, request.size.height, (error, data) => { TransformUtils.centerInside(buf, request.size.width, request.size.height, {asyncTransform:(error:BusinessError|string, data:Promise<PixelMap>|null) => {
data.then(p => { data?.then(p => {
func('', p); func?.asyncTransform('', p);
}).catch(e => { }).catch((e:BusinessError) => {
func(Constants.PROJECT_TAG + ';CenterInside error:' + e, null); func?.asyncTransform(Constants.PROJECT_TAG + ';CenterInside error:' + e, null);
}) })
}) }})
} }
} }

View File

@ -18,7 +18,7 @@ import {AsyncTransform} from '../AsyncTransform'
import {Constants} from '../../constants/Constants' import {Constants} from '../../constants/Constants'
import {TransformUtils} from '../TransformUtils' import {TransformUtils} from '../TransformUtils'
import {RequestOption} from '../../../imageknife/RequestOption' import {RequestOption} from '../../../imageknife/RequestOption'
import { BusinessError } from '@ohos.base'
export class FitCenter implements BaseTransform<PixelMap> { export class FitCenter implements BaseTransform<PixelMap> {
getName() { getName() {
return 'FitCenter:' + this; return 'FitCenter:' + this;
@ -26,18 +26,17 @@ export class FitCenter implements BaseTransform<PixelMap> {
transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform<PixelMap>) {
if (!buf || buf.byteLength <= 0) { if (!buf || buf.byteLength <= 0) {
throw new Error(Constants.PROJECT_TAG + ';FitCenter buf is empty'); if (func != undefined) {
if (func) { func?.asyncTransform(Constants.PROJECT_TAG + ';FitCenter buf is empty', null);
func(Constants.PROJECT_TAG + ';FitCenter buf is empty', null);
} }
return; return;
} }
TransformUtils.fitCenter(buf, request.size.width, request.size.height, (error, data) => { TransformUtils.fitCenter(buf, request.size.width, request.size.height, {asyncTransform:(error:BusinessError|string, data:Promise<PixelMap>|null) => {
data.then(p => { data?.then(p => {
func('', p); func?.asyncTransform('', p);
}).catch(e => { }).catch((e:BusinessError) => {
func(Constants.PROJECT_TAG + ';FitCenter error:' + e, null); func?.asyncTransform(Constants.PROJECT_TAG + ';FitCenter error:' + e, null);
}) })
}) }})
} }
} }

View File

@ -16,16 +16,17 @@ import { PixelEntry } from "../entry/PixelEntry"
import { AsyncTransform } from "../transform/AsyncTransform" import { AsyncTransform } from "../transform/AsyncTransform"
import { ColorUtils } from "./ColorUtils" import { ColorUtils } from "./ColorUtils"
import { GPUImageSketchFilter } from '@ohos/gpu_transform' import { GPUImageSketchFilter } from '@ohos/gpu_transform'
import image from '@ohos.multimedia.image'
export namespace CalculatePixelUtils { export namespace CalculatePixelUtils {
export async function sketch(p: any, func: AsyncTransform<PixelMap>) { export async function sketch(p: PixelMap, func?: AsyncTransform<PixelMap>) {
let imageInfo = await p.getImageInfo(); let imageInfo = await p.getImageInfo();
let size = { let size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("sketch The image size does not exist."), null) func.asyncTransform("sketch The image size does not exist.", null)
return; return;
} }
@ -91,8 +92,8 @@ export namespace CalculatePixelUtils {
} }
await p.writeBufferToPixels(bufferNewData); await p.writeBufferToPixels(bufferNewData);
if (func) { if (func != undefined) {
func("success", p); func.asyncTransform("success", p);
} }
} }
@ -298,14 +299,14 @@ export namespace CalculatePixelUtils {
return array; return array;
} }
export async function sketchGpu(p: any, func: AsyncTransform<PixelMap>) { export async function sketchGpu(p: PixelMap, func?: AsyncTransform<PixelMap>) {
let imageInfo = await p.getImageInfo(); let imageInfo:image.ImageInfo = await p.getImageInfo();
let size = { let size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("sketch The image size does not exist."), null) func?.asyncTransform("sketch The image size does not exist.", null)
return; return;
} }
@ -318,8 +319,8 @@ export namespace CalculatePixelUtils {
filter.setImageData(bufferData, w, h); filter.setImageData(bufferData, w, h);
filter.getPixelMapBuf(0, 0, w, h).then((buf) => { filter.getPixelMapBuf(0, 0, w, h).then((buf) => {
p.writeBufferToPixels(buf); p.writeBufferToPixels(buf);
if (func) { if (func!=undefined) {
func("success", p); func.asyncTransform("success", p);
} }
}) })
} }

View File

@ -18,11 +18,12 @@ import {PixelEntry} from "../entry/PixelEntry"
import {AsyncTransform} from "../transform/AsyncTransform" import {AsyncTransform} from "../transform/AsyncTransform"
import {ColorUtils} from "./ColorUtils" import {ColorUtils} from "./ColorUtils"
import { GPUImageBlurFilter } from '@ohos/gpu_transform' import { GPUImageBlurFilter } from '@ohos/gpu_transform'
import {Size} from '../../imageknife/RequestOption'
import image from '@ohos.multimedia.image';
export namespace fastBlur { export namespace fastBlur {
export async function blur(bitmap: any, radius: number, canReuseInBitmap: boolean, func: AsyncTransform<PixelMap>) { export async function blur(bitmap: PixelMap, radius: number, canReuseInBitmap: boolean, func?: AsyncTransform<PixelMap>) {
// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html // http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
@ -52,25 +53,25 @@ export namespace fastBlur {
// //
if (radius < 1) { if (radius < 1) {
func("error,radius must be greater than 1 ", null); func?.asyncTransform("error,radius must be greater than 1 ", null);
return; return;
} }
let imageInfo = await bitmap.getImageInfo(); let imageInfo = await bitmap.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("fastBlur The image size does not exist."), null) func?.asyncTransform("fastBlur The image size does not exist.", null)
return; return;
} }
let w = size.width; let w = size.width;
let h = size.height; let h = size.height;
var pixEntry: Array<PixelEntry> = new Array() let pixEntry: Array<PixelEntry> = new Array()
var pix: Array<number> = new Array() let pix: Array<number> = new Array()
let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber());
@ -103,7 +104,17 @@ export namespace fastBlur {
let g = CalculatePixelUtils.createIntArray(wh); let g = CalculatePixelUtils.createIntArray(wh);
let b = 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 vmin = CalculatePixelUtils.createIntArray(Math.max(w, h));
let divsum = (div + 1) >> 1; let divsum = (div + 1) >> 1;
@ -115,7 +126,15 @@ export namespace fastBlur {
yw = yi = 0; yw = yi = 0;
let stack = CalculatePixelUtils.createInt2DArray(div, 3); 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<number>; let sir: Array<number>;
let r1 = radius + 1; let r1 = radius + 1;
for (y = 0; y < h; y++) { for (y = 0; y < h; y++) {
@ -286,29 +305,29 @@ export namespace fastBlur {
index++; index++;
} }
await bitmap.writeBufferToPixels(bufferNewData); await bitmap.writeBufferToPixels(bufferNewData);
if (func) { if (func != undefined) {
func("success", bitmap); func?s.asyncTransform("success", bitmap);
} }
} }
export async function blurGPU(bitmap: any, radius: number, canReuseInBitmap: boolean, func: AsyncTransform<PixelMap>) { export async function blurGPU(bitmap: PixelMap, radius: number, canReuseInBitmap: boolean, func?: AsyncTransform<PixelMap>) {
if (radius < 1) { if (radius < 1) {
func("error,radius must be greater than 1 ", null); func?.asyncTransform("error,radius must be greater than 1 ", null);
return; return;
} }
let imageInfo = await bitmap.getImageInfo(); let imageInfo:image.ImageInfo = await bitmap.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("fastBlur The image size does not exist."), null) func?.asyncTransform("fastBlur The image size does not exist.", null)
return; return;
} }
let w = size.width; let w:number = size.width;
let h = size.height; let h:number = size.height;
let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber()); let bufferData = new ArrayBuffer(bitmap.getPixelBytesNumber());
await bitmap.readPixelsToBuffer(bufferData); await bitmap.readPixelsToBuffer(bufferData);
@ -318,8 +337,8 @@ export namespace fastBlur {
filter.setBlurOffset([1.0, 1.0]) filter.setBlurOffset([1.0, 1.0])
filter.getPixelMapBuf(0, 0, w, h).then((buf) => { filter.getPixelMapBuf(0, 0, w, h).then((buf) => {
bitmap.writeBufferToPixels(buf); bitmap.writeBufferToPixels(buf);
if (func) { if (func != undefined) {
func("success", bitmap); func?.asyncTransform("success", bitmap);
} }
}) })
} }

View File

@ -17,23 +17,25 @@ import {CalculatePixelUtils} from "./CalculatePixelUtils"
import {PixelEntry} from "../entry/PixelEntry" import {PixelEntry} from "../entry/PixelEntry"
import {AsyncTransform} from "../transform/AsyncTransform" import {AsyncTransform} from "../transform/AsyncTransform"
import {ColorUtils} from "./ColorUtils" import {ColorUtils} from "./ColorUtils"
import {Size} from "../../imageknife/RequestOption"
import {GPUImagePixelationFilter} from '@ohos/gpu_transform' import {GPUImagePixelationFilter} from '@ohos/gpu_transform'
import image from '@ohos.multimedia.image';
export namespace pixelUtils { export namespace pixelUtils {
export async function pixel(bitmap: any, pixel: number, func: AsyncTransform<PixelMap>) { export async function pixel(bitmap: PixelMap, pixel: number, func?: AsyncTransform<PixelMap>) {
let imageInfo = await bitmap.getImageInfo(); let imageInfo:image.ImageInfo= await bitmap.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("GrayscaleTransformation The image size does not exist."), null) func.asyncTransform("GrayscaleTransformation The image size does not exist.", null)
return; return;
} }
let targetWidth = size.width; let targetWidth:number = size.width;
let targetHeight = size.height; let targetHeight:number = size.height;
var pixEntry: Array<PixelEntry> = new Array() var pixEntry: Array<PixelEntry> = new Array()
let inPixels: Array<Array<number>> = CalculatePixelUtils.createInt2DArray(targetHeight, targetWidth); let inPixels: Array<Array<number>> = CalculatePixelUtils.createInt2DArray(targetHeight, targetWidth);
@ -42,8 +44,8 @@ export namespace pixelUtils {
await bitmap.readPixelsToBuffer(bufferData); await bitmap.readPixelsToBuffer(bufferData);
let dataArray = new Uint8Array(bufferData); let dataArray = new Uint8Array(bufferData);
let ph = 0; let ph:number = 0;
let pw = 0; let pw:number = 0;
for (let index = 0; index < dataArray.length; index += 4) { for (let index = 0; index < dataArray.length; index += 4) {
@ -127,18 +129,18 @@ export namespace pixelUtils {
} }
await bitmap.writeBufferToPixels(bufferNewData); await bitmap.writeBufferToPixels(bufferNewData);
if (func) { if (func) {
func("success", bitmap); func?.asyncTransform("success", bitmap);
} }
} }
export async function pixelGPU(bitmap: any, pixel: number, func: AsyncTransform<PixelMap>) { export async function pixelGPU(bitmap: any, pixel: number, func?: AsyncTransform<PixelMap>) {
let imageInfo = await bitmap.getImageInfo(); let imageInfo = await bitmap.getImageInfo();
let size = { let size:Size = {
width: imageInfo.size.width, width: imageInfo.size.width,
height: imageInfo.size.height height: imageInfo.size.height
} }
if (!size) { if (!size) {
func(new Error("GrayscaleTransformation The image size does not exist."), null) func?.asyncTransform("GrayscaleTransformation The image size does not exist.", null)
return; return;
} }
let w = size.width; let w = size.width;
@ -151,8 +153,8 @@ export namespace pixelUtils {
filter.setPixel(pixel) filter.setPixel(pixel)
filter.getPixelMapBuf(0, 0, w, h).then((buf) => { filter.getPixelMapBuf(0, 0, w, h).then((buf) => {
bitmap.writeBufferToPixels(buf); bitmap.writeBufferToPixels(buf);
if (func) { if (func!=undefined) {
func("success", bitmap); func?.asyncTransform("success", bitmap);
} }
}) })
} }