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,
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);
}
}