Compare commits
No commits in common. "e086bb8630188b386c6b854dbcf707159c800c5c" and "b2af4e1958f6c736a9a6a4544ce7d5bc410cf6a9" have entirely different histories.
e086bb8630
...
b2af4e1958
|
@ -39,16 +39,6 @@ class RequestData {
|
|||
/**
|
||||
* ImageKnifeDispatcher 抽取出来的方法,因@Concurrent只能import方法,故抽取到另一个类
|
||||
*/
|
||||
interface GeneratedObjectLiteralInterface_1 {
|
||||
height: number;
|
||||
width: number;
|
||||
}
|
||||
|
||||
interface Generated {
|
||||
editable: boolean;
|
||||
desiredSize: Size;
|
||||
}
|
||||
|
||||
export class ImageKnifeLoader {
|
||||
static async parseImage(resBuf: ArrayBuffer, typeValue: string, fileKey: string,
|
||||
request: RequestJobRequest): Promise<RequestJobResult> {
|
||||
|
@ -85,15 +75,19 @@ export class ImageKnifeLoader {
|
|||
}
|
||||
|
||||
let size = (await imageSource.getImageInfo()).size
|
||||
try{
|
||||
if ((request.downsampType !== DownsampleStrategy.NONE) &&
|
||||
request.requestSource == ImageKnifeRequestSource.SRC ) {
|
||||
decodingOptions =await ImageKnifeLoader.downsamplerReqSize(typeValue,request,size,ImageKnifeRequestSource.SRC)
|
||||
if ((request.downsampType !== undefined && request.downsampType !== DownsampleStrategy.NONE) &&
|
||||
request.requestSource == ImageKnifeRequestSource.SRC) {
|
||||
let reqSize =
|
||||
new Downsampler().calculateScaling(typeValue, size.width, size.height, request.targetWidth,
|
||||
request.targetHeight, request.downsampType)
|
||||
decodingOptions = {
|
||||
editable: request.requestSource === ImageKnifeRequestSource.SRC && request.transformation !== undefined ? true : false,
|
||||
desiredSize: {
|
||||
width: reqSize.targetWidth,
|
||||
height: reqSize.targetHeight
|
||||
}
|
||||
}
|
||||
}catch(err){
|
||||
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
||||
}
|
||||
|
||||
await imageSource.createPixelMap(decodingOptions)
|
||||
.then((pixelmap: PixelMap) => {
|
||||
resPixelmap = pixelmap
|
||||
|
@ -131,13 +125,18 @@ export class ImageKnifeLoader {
|
|||
editable: true,
|
||||
desiredSize: defaultSize
|
||||
};
|
||||
try{
|
||||
if ((request.downsampType !== DownsampleStrategy.NONE) &&
|
||||
request.requestSource == ImageKnifeRequestSource.SRC ) {
|
||||
opts =await ImageKnifeLoader.downsamplerReqSize(typeValue,request,size)
|
||||
if ((request.downsampType !== DownsampleStrategy.NONE && request.downsampType !== undefined) &&
|
||||
request.requestSource == ImageKnifeRequestSource.SRC) {
|
||||
let reqSize =
|
||||
new Downsampler().calculateScaling(typeValue, size.width, size.height, request.targetWidth,
|
||||
request.targetHeight, request.downsampType)
|
||||
opts = {
|
||||
editable: true,
|
||||
desiredSize: {
|
||||
height: vp2px(reqSize.targetHeight),
|
||||
width: vp2px(reqSize.targetWidth)
|
||||
}
|
||||
}
|
||||
}catch(err){
|
||||
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
||||
}
|
||||
await imageSource.createPixelMap(opts)
|
||||
.then((pixelmap: PixelMap) => {
|
||||
|
@ -383,25 +382,4 @@ export class ImageKnifeLoader {
|
|||
}
|
||||
return resBuf
|
||||
}
|
||||
static async downsamplerReqSize(typeValue:string,request:RequestJobRequest ,size:Size,SRC?:ImageKnifeRequestSource): Promise<image.DecodingOptions>{
|
||||
let reqSize = new Downsampler().calculateScaling(typeValue, size.width, size.height, request.targetWidth, request.targetHeight, request.downsampType)
|
||||
if(typeValue=="svg") {
|
||||
return ({
|
||||
editable: true,
|
||||
desiredSize: ({
|
||||
height: vp2px(reqSize.targetHeight),
|
||||
width: vp2px(reqSize.targetWidth)
|
||||
} as Size)
|
||||
} as image.DecodingOptions )
|
||||
}else {
|
||||
return( {
|
||||
editable: request.requestSource ===SRC && request.transformation !== undefined ? true : false,
|
||||
desiredSize: ({
|
||||
width: reqSize.targetWidth,
|
||||
height: reqSize.targetHeight
|
||||
}as Size)
|
||||
}as image.DecodingOptions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -27,58 +27,55 @@ export interface calculateScaleType {
|
|||
targetHeight: number
|
||||
}
|
||||
|
||||
|
||||
export class Downsampler {
|
||||
calculateScaling(
|
||||
typeValue: string |null,
|
||||
sourceWidth: number, //原始宽高
|
||||
typeValue: string | null,
|
||||
sourceWidth: number | undefined, //原始宽高
|
||||
sourceHeight: number, //原始宽高
|
||||
requestWidth: number, //请求宽高
|
||||
requestHeight: number, //请求宽高
|
||||
downsampType: DownsampleStrategy,
|
||||
downsampType: DownsampleStrategy | undefined,
|
||||
): calculateScaleType {
|
||||
|
||||
if (sourceHeight <= 0 || sourceWidth <= 0) {
|
||||
throw new Error(`Invalid width and height, sourceHeight:${sourceHeight}+ sourceWidth:${sourceWidth}`)
|
||||
const fileType = typeValue //获取图片类型
|
||||
let targetWidth: number | null = null;//降采样之后的宽
|
||||
let targetHeight: number | null = null;//降采样之后的高
|
||||
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 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 exactScaleFactor : number = downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);
|
||||
let rounding: SampleSizeRounding= downsampler.getSampleSizeRounding(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);//采样类型
|
||||
if (exactScaleFactor==undefined|| exactScaleFactor <= 0 ) {
|
||||
throw new Error("Cannot round with exactScaleFactor");
|
||||
}
|
||||
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 outWidth: number = this.round(exactScaleFactor * sourceWidth);//原始宽和缩放系数的乘积
|
||||
let outHeight: number = this.round(exactScaleFactor * sourceHeight);//原始高和缩放系数的乘积
|
||||
let widthScaleFactor = sourceWidth / outWidth;//计算的缩放因子
|
||||
let heightScaleFactor = sourceHeight / outHeight;//计算的缩放因子
|
||||
let scaleFactor = rounding ==SampleSizeRounding.QUALITY ? Math.max(widthScaleFactor, heightScaleFactor) :
|
||||
Math.min(widthScaleFactor, heightScaleFactor) //将整型的缩放因子转换为2的次幂采样大小
|
||||
let powerOfTwoSampleSize: number = scaleFactor;
|
||||
powerOfTwoSampleSize = Math.max(1, highestOneBit(scaleFactor))
|
||||
if (rounding == 0 && (powerOfTwoSampleSize < (1 / exactScaleFactor))) {
|
||||
powerOfTwoSampleSize = powerOfTwoSampleSize << 1;
|
||||
}
|
||||
//基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸
|
||||
if (typeValue === "png") {
|
||||
return {
|
||||
targetWidth: Math.floor(sourceWidth / scaleFactor),
|
||||
targetHeight: Math.floor(sourceHeight / scaleFactor)
|
||||
}
|
||||
} else if (typeValue === "webp") {
|
||||
return {
|
||||
targetWidth: Math.round(sourceWidth / scaleFactor),
|
||||
targetHeight: Math.round(sourceHeight / scaleFactor)
|
||||
}
|
||||
if (fileType === "png") {
|
||||
targetWidth = Math.floor(sourceWidth / powerOfTwoSampleSize);
|
||||
targetHeight = Math.floor(sourceHeight / powerOfTwoSampleSize);
|
||||
} else if (fileType === "webp") {
|
||||
targetWidth = Math.round(sourceWidth / powerOfTwoSampleSize);
|
||||
targetHeight = Math.round(sourceHeight / powerOfTwoSampleSize);
|
||||
} else {
|
||||
return {
|
||||
targetWidth: sourceWidth / scaleFactor,
|
||||
targetHeight: sourceHeight / scaleFactor
|
||||
|
||||
}
|
||||
targetWidth = sourceWidth / powerOfTwoSampleSize;
|
||||
targetHeight = sourceHeight / powerOfTwoSampleSize;
|
||||
}
|
||||
return { targetWidth, targetHeight }
|
||||
}
|
||||
|
||||
getDownsampler(downsampType: DownsampleStrategy) {
|
||||
getDownsampler(downsampType:DownsampleStrategy) {
|
||||
switch (downsampType) {
|
||||
case DownsampleStrategy.FIT_CENTER_MEMORY:
|
||||
case DownsampleStrategy.FIT_CENTER_QUALITY:
|
||||
|
@ -96,7 +93,6 @@ export class Downsampler {
|
|||
throw new Error('Unsupported downsampling strategy');
|
||||
}
|
||||
}
|
||||
|
||||
round(value: number): number {
|
||||
return Math.floor(value + 0.5);
|
||||
}
|
||||
|
|
|
@ -124,7 +124,7 @@ export class ImageKnifeOption {
|
|||
onComplete?:(event:EventImage | undefined) => void
|
||||
drawingColorFilter?: ColorFilter | drawing.ColorFilter
|
||||
// 下采样
|
||||
@Trace downsampleOf: DownsampleStrategy = DownsampleStrategy.NONE
|
||||
@Trace downsampleOf?: DownsampleStrategy = DownsampleStrategy.NONE
|
||||
constructor(option?:ImageOption) {
|
||||
this.loadSrc = option?.loadSrc == undefined ? "" : option?.loadSrc
|
||||
this.placeholderSrc = option?.placeholderSrc
|
||||
|
@ -145,7 +145,7 @@ export class ImageKnifeOption {
|
|||
this.onLoadListener = option?.onLoadListener
|
||||
this.onComplete = option?.onComplete
|
||||
this.drawingColorFilter = option?.drawingColorFilter
|
||||
this.downsampleOf = option?.downsampleOf==undefined?DownsampleStrategy.NONE:option?.downsampleOf
|
||||
this.downsampleOf = option?.downsampleOf
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue