update library/src/main/ets/downsampling/Downsampler.ets.
简化代码 Signed-off-by: 田双明 <tianshuangming@h-partners.com>
This commit is contained in:
parent
b2af4e1958
commit
fe9235d8b1
|
@ -29,53 +29,50 @@ export interface calculateScaleType {
|
||||||
|
|
||||||
export class Downsampler {
|
export class Downsampler {
|
||||||
calculateScaling(
|
calculateScaling(
|
||||||
typeValue: string | null,
|
typeValue: string,
|
||||||
sourceWidth: number | undefined, //原始宽高
|
sourceWidth: number, //原始宽高
|
||||||
sourceHeight: number, //原始宽高
|
sourceHeight: number, //原始宽高
|
||||||
requestWidth: number, //请求宽高
|
requestWidth: number, //请求宽高
|
||||||
requestHeight: number, //请求宽高
|
requestHeight: number, //请求宽高
|
||||||
downsampType: DownsampleStrategy | undefined,
|
downsampType: DownsampleStrategy,
|
||||||
): calculateScaleType {
|
): calculateScaleType {
|
||||||
const fileType = typeValue //获取图片类型
|
|
||||||
let targetWidth: number | null = null;//降采样之后的宽
|
if (sourceHeight <= 0 || sourceWidth <= 0) {
|
||||||
let targetHeight: number | null = null;//降采样之后的高
|
throw new Error("非法宽高 sourceHeight sourceWidth")
|
||||||
if (sourceHeight <= 0 || sourceWidth == undefined || sourceWidth == null || sourceWidth <= 0) {
|
|
||||||
throw new Error("Cannot found width or height")
|
|
||||||
}
|
|
||||||
if(downsampType===undefined){
|
|
||||||
throw new Error("Cannot found downsampType");
|
|
||||||
}
|
}
|
||||||
let downsampler = this.getDownsampler(downsampType);
|
let downsampler = this.getDownsampler(downsampType);
|
||||||
let exactScaleFactor : number = downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);
|
let exactScaleFactor: number =
|
||||||
let rounding: SampleSizeRounding= downsampler.getSampleSizeRounding(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);//采样类型
|
downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);
|
||||||
if (exactScaleFactor==undefined|| exactScaleFactor <= 0 ) {
|
let rounding: SampleSizeRounding =
|
||||||
throw new Error("Cannot round with exactScaleFactor");
|
downsampler.getSampleSizeRounding(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType); //采样类型
|
||||||
|
//原始宽高和缩放系数的乘积
|
||||||
|
let outSize = {
|
||||||
|
width: this.round(exactScaleFactor * sourceWidth),
|
||||||
|
height: this.round(exactScaleFactor * sourceHeight)
|
||||||
}
|
}
|
||||||
let outWidth: number = this.round(exactScaleFactor * sourceWidth);//原始宽和缩放系数的乘积
|
let scaleFactor =
|
||||||
let outHeight: number = this.round(exactScaleFactor * sourceHeight);//原始高和缩放系数的乘积
|
rounding == SampleSizeRounding.QUALITY ? Math.max(sourceWidth / outSize.width, sourceHeight / outSize.height) :
|
||||||
let widthScaleFactor = sourceWidth / outWidth;//计算的缩放因子
|
Math.min(sourceWidth / outSize.width, sourceHeight / outSize.height) //将整型的缩放因子转换为2的次幂采样大小
|
||||||
let heightScaleFactor = sourceHeight / outHeight;//计算的缩放因子
|
scaleFactor = Math.max(1, highestOneBit(scaleFactor))
|
||||||
let scaleFactor = rounding ==SampleSizeRounding.QUALITY ? Math.max(widthScaleFactor, heightScaleFactor) :
|
if (rounding == 0 && (scaleFactor < (1 / exactScaleFactor))) {
|
||||||
Math.min(widthScaleFactor, heightScaleFactor) //将整型的缩放因子转换为2的次幂采样大小
|
scaleFactor = scaleFactor << 1;
|
||||||
let powerOfTwoSampleSize: number = scaleFactor;
|
|
||||||
powerOfTwoSampleSize = Math.max(1, highestOneBit(scaleFactor))
|
|
||||||
if (rounding == 0 && (powerOfTwoSampleSize < (1 / exactScaleFactor))) {
|
|
||||||
powerOfTwoSampleSize = powerOfTwoSampleSize << 1;
|
|
||||||
}
|
}
|
||||||
//基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸
|
//基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸
|
||||||
if (fileType === "png") {
|
if (typeValue === "png") {
|
||||||
targetWidth = Math.floor(sourceWidth / powerOfTwoSampleSize);
|
return {
|
||||||
targetHeight = Math.floor(sourceHeight / powerOfTwoSampleSize);
|
targetWidth: Math.floor(sourceWidth / scaleFactor),
|
||||||
} else if (fileType === "webp") {
|
targetHeight: Math.floor(sourceHeight / scaleFactor)
|
||||||
targetWidth = Math.round(sourceWidth / powerOfTwoSampleSize);
|
}
|
||||||
targetHeight = Math.round(sourceHeight / powerOfTwoSampleSize);
|
} else if (typeValue === "webp") {
|
||||||
|
targetWidth = Math.round(sourceWidth / scaleFactor);
|
||||||
|
targetHeight = Math.round(sourceHeight / scaleFactor);
|
||||||
} else {
|
} else {
|
||||||
targetWidth = sourceWidth / powerOfTwoSampleSize;
|
targetWidth = sourceWidth / scaleFactor;
|
||||||
targetHeight = sourceHeight / powerOfTwoSampleSize;
|
targetHeight = sourceHeight / scaleFactor;
|
||||||
}
|
}
|
||||||
return { targetWidth, targetHeight }
|
|
||||||
}
|
}
|
||||||
getDownsampler(downsampType:DownsampleStrategy) {
|
|
||||||
|
getDownsampler(downsampType: DownsampleStrategy) {
|
||||||
switch (downsampType) {
|
switch (downsampType) {
|
||||||
case DownsampleStrategy.FIT_CENTER_MEMORY:
|
case DownsampleStrategy.FIT_CENTER_MEMORY:
|
||||||
case DownsampleStrategy.FIT_CENTER_QUALITY:
|
case DownsampleStrategy.FIT_CENTER_QUALITY:
|
||||||
|
@ -93,6 +90,7 @@ export class Downsampler {
|
||||||
throw new Error('Unsupported downsampling strategy');
|
throw new Error('Unsupported downsampling strategy');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
round(value: number): number {
|
round(value: number): number {
|
||||||
return Math.floor(value + 0.5);
|
return Math.floor(value + 0.5);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue