From 1b812f84318fd140414437cd5df20ce0bf2825a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E5=8F=8C=E6=98=8E?= Date: Wed, 9 Oct 2024 08:40:06 +0000 Subject: [PATCH] update library/src/main/ets/downsampling/Downsampler.ets. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 田双明 --- .../src/main/ets/downsampling/Downsampler.ets | 61 ++++++++----------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/library/src/main/ets/downsampling/Downsampler.ets b/library/src/main/ets/downsampling/Downsampler.ets index a1bb180..1b7cdeb 100644 --- a/library/src/main/ets/downsampling/Downsampler.ets +++ b/library/src/main/ets/downsampling/Downsampler.ets @@ -20,12 +20,7 @@ import { FitCenter, None, } from './DownsampleStartegy'; -import { highestOneBit, SampleSizeRounding } from './DownsampleUtils'; - -export interface calculateScaleType { - targetWidth: number, - targetHeight: number -} +import { highestOneBit, round, SampleSizeRounding } from './DownsampleUtils'; export class Downsampler { @@ -36,43 +31,45 @@ export class Downsampler { requestWidth: number, //请求宽高 requestHeight: number, //请求宽高 downsampType: DownsampleStrategy, - ): calculateScaleType { + ): Size { if (sourceHeight <= 0 || sourceWidth <= 0) { - throw new Error(`Invalid width and height, sourceHeight:${sourceHeight}+ sourceWidth:${sourceWidth}`) + throw new Error(`Invalid width and height, sourceHeight:${sourceHeight}+ sourceWidth:${sourceWidth}`) } let downsampler = this.getDownsampler(downsampType); - let exactScaleFactor: number = - downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType); - let rounding: SampleSizeRounding = - downsampler.getSampleSizeRounding(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType); //采样类型 - //原始宽高和缩放系数的乘积 - let outSize:Size = { - width: this.round(exactScaleFactor * sourceWidth), - height: this.round(exactScaleFactor * sourceHeight) - } - let scaleFactor = - rounding == SampleSizeRounding.QUALITY ? Math.max(sourceWidth / outSize.width, sourceHeight / outSize.height) : - Math.min(sourceWidth / outSize.width, sourceHeight / outSize.height) //将整型的缩放因子转换为2的次幂采样大小 - scaleFactor = Math.max(1, highestOneBit(scaleFactor)) - if (rounding == 0 && (scaleFactor < (1 / exactScaleFactor))) { - scaleFactor = scaleFactor << 1; - } + let scaleFactor: number = + downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);//缩放比 + // let rounding: SampleSizeRounding = + // downsampler.getSampleSizeType(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType); //采样类型 + //根据缩放比重新计算宽高 + // let outSize: Size = { + // width: round(exactScaleFactor * sourceWidth), + // height:round(exactScaleFactor * sourceHeight) + // } + // let scaleFactor = + // rounding == SampleSizeRounding.QUALITY ? Math.max(sourceWidth / exactScaleFactor.width, sourceHeight / exactScaleFactor.height) : + // Math.min(sourceWidth / exactScaleFactor.width, sourceHeight / exactScaleFactor.height) //重新计算宽高比 + + // scaleFactor = Math.max(1, highestOneBit(scaleFactor))//将整型的缩放因子转换为2的次幂采样大小 + // + // if (rounding == 0 && (scaleFactor < (1 / exactScaleFactor))) { + // scaleFactor = scaleFactor << 1; + // } //基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸 if (typeValue === "png") { return { - targetWidth: Math.floor(sourceWidth / scaleFactor), - targetHeight: Math.floor(sourceHeight / scaleFactor) + width: Math.floor(sourceWidth / scaleFactor), + height: Math.floor(sourceHeight / scaleFactor) } } else if (typeValue === "webp") { return { - targetWidth: Math.round(sourceWidth / scaleFactor), - targetHeight: Math.round(sourceHeight / scaleFactor) + width: Math.round(sourceWidth / scaleFactor), + height: Math.round(sourceHeight / scaleFactor) } } else { return { - targetWidth: sourceWidth / scaleFactor, - targetHeight: sourceHeight / scaleFactor + width: sourceWidth / scaleFactor, + height: sourceHeight / scaleFactor } } @@ -96,8 +93,4 @@ export class Downsampler { throw new Error('Unsupported downsampling strategy'); } } - - round(value: number): number { - return Math.floor(value + 0.5); - } }