update library/src/main/ets/downsampling/Downsampler.ets.

Signed-off-by: 田双明 <tianshuangming@h-partners.com>
This commit is contained in:
田双明 2024-10-09 08:40:06 +00:00 committed by Gitee
parent 6e708e1c17
commit 1b812f8431
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 27 additions and 34 deletions

View File

@ -20,12 +20,7 @@ import {
FitCenter, FitCenter,
None, None,
} from './DownsampleStartegy'; } from './DownsampleStartegy';
import { highestOneBit, SampleSizeRounding } from './DownsampleUtils'; import { highestOneBit, round, SampleSizeRounding } from './DownsampleUtils';
export interface calculateScaleType {
targetWidth: number,
targetHeight: number
}
export class Downsampler { export class Downsampler {
@ -36,43 +31,45 @@ export class Downsampler {
requestWidth: number, //请求宽高 requestWidth: number, //请求宽高
requestHeight: number, //请求宽高 requestHeight: number, //请求宽高
downsampType: DownsampleStrategy, downsampType: DownsampleStrategy,
): calculateScaleType { ): Size {
if (sourceHeight <= 0 || sourceWidth <= 0) { 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 downsampler = this.getDownsampler(downsampType);
let exactScaleFactor: number = let scaleFactor: number =
downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType); downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);//缩放比
let rounding: SampleSizeRounding = // let rounding: SampleSizeRounding =
downsampler.getSampleSizeRounding(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType); //采样类型 // downsampler.getSampleSizeType(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType); //采样类型
//原始宽高和缩放系数的乘积 //根据缩放比重新计算宽高
let outSize:Size = { // let outSize: Size = {
width: this.round(exactScaleFactor * sourceWidth), // width: round(exactScaleFactor * sourceWidth),
height: this.round(exactScaleFactor * sourceHeight) // height:round(exactScaleFactor * sourceHeight)
} // }
let scaleFactor = // let scaleFactor =
rounding == SampleSizeRounding.QUALITY ? Math.max(sourceWidth / outSize.width, sourceHeight / outSize.height) : // rounding == SampleSizeRounding.QUALITY ? Math.max(sourceWidth / exactScaleFactor.width, sourceHeight / exactScaleFactor.height) :
Math.min(sourceWidth / outSize.width, sourceHeight / outSize.height) //将整型的缩放因子转换为2的次幂采样大小 // Math.min(sourceWidth / exactScaleFactor.width, sourceHeight / exactScaleFactor.height) //重新计算宽高比
scaleFactor = Math.max(1, highestOneBit(scaleFactor))
if (rounding == 0 && (scaleFactor < (1 / exactScaleFactor))) { // scaleFactor = Math.max(1, highestOneBit(scaleFactor))//将整型的缩放因子转换为2的次幂采样大小
scaleFactor = scaleFactor << 1; //
} // if (rounding == 0 && (scaleFactor < (1 / exactScaleFactor))) {
// scaleFactor = scaleFactor << 1;
// }
//基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸 //基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸
if (typeValue === "png") { if (typeValue === "png") {
return { return {
targetWidth: Math.floor(sourceWidth / scaleFactor), width: Math.floor(sourceWidth / scaleFactor),
targetHeight: Math.floor(sourceHeight / scaleFactor) height: Math.floor(sourceHeight / scaleFactor)
} }
} else if (typeValue === "webp") { } else if (typeValue === "webp") {
return { return {
targetWidth: Math.round(sourceWidth / scaleFactor), width: Math.round(sourceWidth / scaleFactor),
targetHeight: Math.round(sourceHeight / scaleFactor) height: Math.round(sourceHeight / scaleFactor)
} }
} else { } else {
return { return {
targetWidth: sourceWidth / scaleFactor, width: sourceWidth / scaleFactor,
targetHeight: sourceHeight / scaleFactor height: sourceHeight / scaleFactor
} }
} }
@ -96,8 +93,4 @@ export class Downsampler {
throw new Error('Unsupported downsampling strategy'); throw new Error('Unsupported downsampling strategy');
} }
} }
round(value: number): number {
return Math.floor(value + 0.5);
}
} }