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