1.图片变换工具类,相关codecheck

Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
zhoulisheng1 2022-11-14 03:41:16 -08:00
parent 923d6bfbbe
commit 90a67926d5
4 changed files with 178 additions and 206 deletions

View File

@ -95,8 +95,8 @@ export namespace CalculatePixelUtils {
}
}
function gaussGray(psrc: Array<number>, horz: number, vert: number,
width: number, height: number): number {
var gaussGray = (psrc: Array<number>, horz: number, vert: number,
width: number, height: number): number=> {
let dst, src, n_p, n_m, d_p, d_m, bd_p, bd_m, val_p, val_m, initial_p, initial_m: Array<number>;
let i, j, t, k, row, col, terms, std_dev, sp_p_idx, sp_m_idx, vp_idx, vm_idx: number;
let row_stride = width;
@ -203,9 +203,9 @@ export namespace CalculatePixelUtils {
return 0;
}
function findConstants(n_p: Array<number>, n_m: Array<number>, d_p: Array<number>,
var findConstants = (n_p: Array<number>, n_m: Array<number>, d_p: Array<number>,
d_m: Array<number>, bd_p: Array<number>
, bd_m: Array<number>, std_dev: number) {
, bd_m: Array<number>, std_dev: number)=> {
let div = Math.sqrt(2 * 3.141593) * std_dev;
let x0 = -1.783 / std_dev;
let x1 = -1.723 / std_dev;
@ -262,8 +262,8 @@ export namespace CalculatePixelUtils {
}
}
function transferGaussPixels(src1: Array<number>, src2: Array<number>,
dest: Array<number>, bytes: number, width: number) {
var transferGaussPixels = (src1: Array<number>, src2: Array<number>,
dest: Array<number>, bytes: number, width: number)=> {
let i, j, k, b, sum: number;
let bend = bytes * width;
i = j = k = 0;

View File

@ -24,33 +24,33 @@ export namespace fastBlur {
export async function blur(bitmap: any, radius: number, canReuseInBitmap: boolean, func: AsyncTransform<PixelMap>) {
// Stack Blur v1.0 from
// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
//
// Java Author: Mario Klingemann <mario at quasimondo.com>
// http://incubator.quasimondo.com
// created Feburary 29, 2004
// port : Yahel Bouaziz <yahel at kayenko.com>
// http://www.kayenko.com
// ported april 5th, 2012
// This is a compromise between Gaussian Blur and Box blur
// It creates much better looking blurs than Box Blur, but is
// 7x faster than my Gaussian Blur implementation.
//
// I called it Stack Blur because this describes best how this
// filter works internally: it creates a kind of moving stack
// of colors whilst scanning through the image. Thereby it
// just has to add one new block of color to the right side
// of the stack and remove the leftmost color. The remaining
// colors on the topmost layer of the stack are either added on
// or reduced by one, depending on if they are on the right or
// on the left side of the stack.
//
// If you are using this algorithm in your code please add
// the following line:
//
// Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>
// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
//
// Java Author: Mario Klingemann <mario at quasimondo.com>
// http://incubator.quasimondo.com
// created Feburary 29, 2004
// port : Yahel Bouaziz <yahel at kayenko.com>
// http://www.kayenko.com
// ported april 5th, 2012
//
// This is a compromise between Gaussian Blur and Box blur
// It creates much better looking blurs than Box Blur, but is
// 7x faster than my Gaussian Blur implementation.
//
// I called it Stack Blur because this describes best how this
// filter works internally: it creates a kind of moving stack
// of colors whilst scanning through the image. Thereby it
// just has to add one new block of color to the right side
// of the stack and remove the leftmost color. The remaining
// colors on the topmost layer of the stack are either added on
// or reduced by one, depending on if they are on the right or
// on the left side of the stack.
//
// If you are using this algorithm in your code please add
// the following line:
//
if (radius < 1) {
func("error,radius must be greater than 1 ", null);
return;

View File

@ -15,171 +15,59 @@
export class FileTypeUtil {
private map = new Map();
private READ_MIN_LENGTH;
private SUPPORT_FORMATS = [
PhotoFormat.jpg,
PhotoFormat.png,
PhotoFormat.tiff,
PhotoFormat.bmp,
PhotoFormat.webp,
PhotoFormat.svg,
PhotoFormat.gif
]
constructor() {
this.initImageType();
this.initMediaType();
this.initOtherType();
this.initImageType();
}
private initImageType(){
this.map.set("FFD8FF", "JPG")
this.map.set("89504E47", "PNG")
this.map.set("47494638", "GIF")
this.map.set("49492A00", "TIF")
this.map.set("52494646", "RIFF")
this.map.set("57454250", "WEBP")
this.map.set("3C3F786D6C", "SVG")
this.map.set("424D", "BMP")
this.map.set("424D228C010000000000", "BMP") // 16色位图(bmp)
this.map.set("424D8240090000000000", "BMP") // 24位位图(bmp)
this.map.set("424D8E1B030000000000", "BMP") // 256色位图(bmp)
}
private initMediaType(){
this.map.set("57415645", "WAV")
this.map.set("41564920", "AVI")
this.map.set("00000020667479706D70", "MP4")
this.map.set("49443303000000002176", "MP3")
this.map.set("464C5601050000000900", "FLV")
}
private initOtherType(){
this.map.set("41433130", "DWG")
this.map.set("38425053", "PSD")
this.map.set("7B5C727466", "RTF")
/** XML */
// this.map.set("3C3F786D6C", "XML")
this.map.set("68746D6C3E", "HTML")
this.map.set("44656C69766572792D646174653A", "EML")
this.map.set("CFAD12FEC5FD746F", "DBX")
this.map.set("2142444E", "PST")
this.map.set("0xD0CF11E0A1B11AE1", "OLE2")
/** Microsoft Word/Excel 注意word 和 excel的文件头一样 */
this.map.set("D0CF11E0", "XLS")
// this.map.set("DOC", "D0CF11E0")
// this.map.set("504B0304", "DOCX")
this.map.set("504B0304", "XLSX")
this.map.set("5374616E64617264204A", "MDB")
this.map.set("FF575043", "WPB")
this.map.set("252150532D41646F6265", "EPS")
this.map.set("252150532D41646F6265", "PS")
this.map.set("255044462D312E", "PDF")
this.map.set("AC9EBD8F", "qdf")
this.map.set("458600000600", "qbb")
this.map.set("E3828596", "PWL")
this.map.set("504B0304", "ZIP")
this.map.set("52617221", "ARAR")
this.map.set("2E7261FD", "RAM")
this.map.set("2E524D46", "RM")
this.map.set("2E524D46000000120001", "RMVB")
this.map.set("000001BA", "MPG")
this.map.set("6D6F6F76", "MOV")
this.map.set("3026B2758E66CF11", "ASF")
this.map.set("60EA", "ARJ")
this.map.set("4D546864", "MID")
this.map.set("1F8B08", "GZ")
this.map.set("48544D4C207B0D0A0942", "CSS")
this.map.set("696B2E71623D696B2E71", "JS")
this.map.set("d0cf11e0a1b11ae10000", "VSD")
this.map.set("d0cf11e0a1b11ae10000", "WPS")
this.map.set("6431303A637265617465", "TORRENT")
this.map.set("3C2540207061676520", "JSP")
this.map.set("7061636B61676520", "JAVA")
this.map.set("CAFEBABE0000002E00", "CLASS")
this.map.set("504B03040A000000", "JAR")
this.map.set("4D616E69666573742D56", "MF")
this.map.set("4D5A9000030000000400", "EXE")
this.map.set("7F454C4601010100", "ELF")
this.map.set("2000604060", "WK1")
this.map.set("00001A0000100400", "WK3")
this.map.set("00001A0002100400", "WK4")
this.map.set("576F726450726F", "LWP")
this.map.set("53520100", "SLY")
private getDataViewAt(dataView:DataView,index:number): string{
return this.dec2Hex(dataView.getUint8(index))
}
getMaxKeyLength() {
let length = 0;
this.map.forEach((value, key, map) => {
let keyName = key;
length = Math.max(keyName.length, length);
})
return length;
}
isImage(arraybuffer: ArrayBuffer) {
let value = this.getFileType(arraybuffer)
if (
value == "jpg" ||
value == "png" ||
value == "riff" ||
value == "webp" ||
value == "bmp" ||
value == "gif" ||
value == "svg"
) {
return true;
}
return false;
}
isPixelMapType(arraybuffer: ArrayBuffer) {
let value = this.getFileType(arraybuffer)
if (
value == "jpg" ||
value == "png" ||
value == "riff" ||
value == "webp" ||
value == "bmp"
) {
return true;
}
return false;
}
isGif(arraybuffer: ArrayBuffer) {
let value = this.getFileType(arraybuffer)
if (
value == "gif"
) {
return true;
}
return false;
}
isSvg(arraybuffer: ArrayBuffer) {
let value = this.getFileType(arraybuffer)
if (
value == "svg"
) {
return true;
}
return false;
}
getFileType(arraybuffer: ArrayBuffer) {
if(arraybuffer == null || arraybuffer == undefined || arraybuffer.byteLength <= this.getMaxKeyLength()){
let fileType = undefined;
if (arraybuffer == null || arraybuffer == undefined || arraybuffer.byteLength <= this.READ_MIN_LENGTH) {
return undefined;
}
let maxlen = this.getMaxKeyLength() / 2;
let dataView = new DataView(arraybuffer);
let searchKey = "";
for (let i = 0; i < maxlen; i++) {
searchKey = searchKey + this.dec2Hex(dataView.getUint8(i));
let value = this.map.get(searchKey);
if (value != undefined) {
return (value as string).toLowerCase();
console.log('dataView +'+this.getDataViewAt(dataView,0)+this.getDataViewAt(dataView,1))
for(var [key,value] of this.map){
let keySplit = key.split(',')
if(keySplit.length == 2){
let offset = parseInt(keySplit[0])
let magicStringLength = keySplit[1].length;
let readLength = magicStringLength/2;
let start = 0;
let fileMagic = ''
while(start< readLength){
fileMagic+=this.getDataViewAt(dataView,offset+start)
start++;
}
// console.log('magic='+fileMagic+' keySplit[1]='+keySplit[1]+' fileMagic == keySplit[1] ='+(fileMagic == keySplit[1])+
// ' fileMagic type ='+typeof(fileMagic) + ' keySplit[1]='+typeof(keySplit[1]))
if(fileMagic == keySplit[1]){
console.log('匹配到了 fileType='+value)
fileType = value
break;
}
}
}
return fileType;
}
dec2Hex(uint8: number) {
@ -189,4 +77,88 @@ export class FileTypeUtil {
}
return hex.toUpperCase();
}
private initImageType() {
if(!this.map){
this.map = new Map()
}
this.SUPPORT_FORMATS.forEach((value,index,arrs)=>{
let values = value.split(',')
if(values.length == 3){
let magicSplits = values[2].split("|")
if(magicSplits.length == 1){
this.map.set(values[1]+','+values[2],values[0])
}else if(magicSplits.length > 1){
for(let i=0; i<magicSplits.length; i++){
let magicStr = magicSplits[i];
this.map.set(values[1]+','+magicStr,values[0])
}
}else{
// 文件魔数不存在,不处理
}
}
})
this.initReadMinLength();
this.printMapContent();
}
private printMapContent(){
for(var [key,value] of this.map){
console.log('key='+key+'---value='+value)
}
}
private initReadMinLength(){
let max = 0;
this.map.forEach((value,key,map)=>{
let keySplit = key.split(',');
if(keySplit.length == 2){
let offset = parseInt(keySplit[0])
let magicStringLength = keySplit[1].length;
let tempMax = offset + magicStringLength/2;
if(tempMax > max){
max = tempMax;
}
}
})
this.READ_MIN_LENGTH =max;
}
isImage(arraybuffer:ArrayBuffer){
let value = this.getFileType(arraybuffer);
if(
value == SupportFormat.jpg ||
value == SupportFormat.png ||
value == SupportFormat.tiff ||
value == SupportFormat.webp ||
value == SupportFormat.bmp ||
value == SupportFormat.gif ||
value == SupportFormat.svg
){
return true;
}
return false;
}
}
export enum PhotoFormat {
jpg = 'jpg,0,FFD8',
png = 'png,0,89504E470D0A1A0A',
bmp = 'bmp,0,424D',
gif = 'gif,0,474946383961',
svg = 'svg,0,3C3F786D6C',
webp = 'webp,0,52494646',
tiff = 'tiff,0,492049|49492A00|4D4D002A|4D4D002B'
}
export enum SupportFormat {
jpg = 'jpg',
png = 'png',
webp = 'webp',
bmp = 'bmp',
gif = 'gif',
svg = 'svg',
tiff = 'tiff'
}

View File

@ -74,18 +74,18 @@ export class MaskUtils {
let rgbDataMask = CalculatePixelUtils.createInt2DArray(heightMask, widthMask);
let pixEntry: Array<PixelEntry> = new Array();
let bufferData_m = new ArrayBuffer(maskBitmap.getPixelBytesNumber());
await maskBitmap.readPixelsToBuffer(bufferData_m);
let dataArray_m = new Uint8Array(bufferData_m);
let bufferDataM = new ArrayBuffer(maskBitmap.getPixelBytesNumber());
await maskBitmap.readPixelsToBuffer(bufferDataM);
let dataArrayM = new Uint8Array(bufferDataM);
let ph_m = 0;
let pw_m = 0;
let phM = 0;
let pwM = 0;
for (let index = 0; index < dataArray_m.length; index += 4) {
const r = dataArray_m[index];
const g = dataArray_m[index+1];
const b = dataArray_m[index+2];
const f = dataArray_m[index+3];
for (let index = 0; index < dataArrayM.length; index += 4) {
const r = dataArrayM[index];
const g = dataArrayM[index+1];
const b = dataArrayM[index+2];
const f = dataArrayM[index+3];
let entry = new PixelEntry();
entry.a = 0;
@ -96,16 +96,16 @@ export class MaskUtils {
entry.pixel = ColorUtils.rgb(entry.r, entry.g, entry.b);
pixEntry.push(entry);
if (entry.r == 0 && entry.g == 0 && entry.b == 0) {
rgbDataMask[ph_m][pw_m] = rgbData[ph_m][pw_m];
rgbDataMask[phM][pwM] = rgbData[phM][pwM];
} else {
rgbDataMask[ph_m][pw_m] = ColorUtils.rgb(entry.r, entry.g, entry.b);
rgbDataMask[phM][pwM] = ColorUtils.rgb(entry.r, entry.g, entry.b);
}
if (pw_m == widthMask - 1) {
pw_m = 0;
ph_m++;
if (pwM == widthMask - 1) {
pwM = 0;
phM++;
} else {
pw_m++;
pwM++;
}
}
@ -117,7 +117,7 @@ export class MaskUtils {
let nw = 0;
for (let i = 0; i < dataNewArray.length; i += 4) {
let pixel_1 = rgbDataMask[mh][nw];
let pixel1 = rgbDataMask[mh][nw];
if (nw == widthMask - 1) {
nw = 0;
@ -126,13 +126,13 @@ export class MaskUtils {
nw++;
}
let p_r = ColorUtils.red(pixel_1);
let p_g = ColorUtils.green(pixel_1);
let p_b = ColorUtils.blue(pixel_1);
let pR = ColorUtils.red(pixel1);
let pG = ColorUtils.green(pixel1);
let pB = ColorUtils.blue(pixel1);
dataNewArray[i] = p_r;
dataNewArray[i+1] = p_g;
dataNewArray[i+2] = p_b;
dataNewArray[i] = pR;
dataNewArray[i+1] = pG;
dataNewArray[i+2] = pB;
dataNewArray[i+3] = pixEntry[index].f;
index++;
}