Comments (6)
malloc是分配内存,free是清除内存
from pngtiny.
跟C里面的用法一样的
from pngtiny.
pngtiny.js那个代码是emsdk生成的,不需要看pngtiny.js里面的代码
from pngtiny.
pngtiny.js那个代码是emsdk生成的,不需要看pngtiny.js里面的代码
感动 😙 回复好快。我不会C语言,脑子不太灵光,不过还是谢谢大佬 🌹 🌹 🌹。那如果要查pngtiny这个api的话,是去查那个库的文档,我在你的md文档里看到你用了3个库😰
from pngtiny.
pngtiny.js这个看tiny.c文件,tiny.c只开放了一个_tiny函数。该函数的传入一个图片buffer,一个图片大小,一个状态。使用方式看那个uploadFile函数。
function uploadFile(file, ufid) {
$('#conli'+ufid+' .status').html('正在处理');
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function(e) {
var fcont = new Uint8Array(e.target.result);//读取图片内容
var fsize = fcont.byteLength;//获取图片大小
var dataptr = pngtiny._malloc(fsize);//申请图片大小的内存
var retdata = pngtiny._malloc(4);//申请状态位内存
pngtiny.HEAPU8.set(fcont,dataptr);//给图片内存设置数据,以便webassembly读取
pngtiny._tiny(dataptr,fsize,retdata);//传入tiny函数
var rdata = new Int32Array(pngtiny.HEAPU8.buffer, retdata, 1);//获取图片处理结果
var size = rdata[0];//如果图片正常处理了,会返回处理后的图片大小,否则置0
var blob=null;
if(size>0) {//图片大于0,代表正常处理图片了
var pic = new Uint8Array(size);//初始化一个处理后图片大小的变量
var rdata = new Uint8Array(pngtiny.HEAPU8.buffer, dataptr, size);//获取处理后的图片内容,tiny.c里面如果正常处理了图片,会把图片内容保存到原来的图片内存地址里面,所以获取时候就从原来地址内存获取就好
pic.set(rdata);//把图片内容赋值给js变量
blob = new Blob([pic.buffer], {type: "image/png"});//把buffer转为blob
pic=null;
} else {//不正常处理图片,返回原图
blob = new Blob([fcont.buffer], {type: "image/png"});
size=fsize;
}
fcont=null;
var url = (URL || webkitURL).createObjectURL(blob);//从blob获取图片链接
imgList.push({name:file.name,data:blob});
var dr = ((fsize-size)*100/fsize|0)+'%';
$('#conli'+ufid+' .status').html('完成');
$('#conli'+ufid+' .after').html(getSizeTrans(size));
$('#conli'+ufid+' .deal').html(dr);
$('#conli'+ufid+' .down').html('<a href="'+url+'" download="'+file.name+'">下载</a>');
pngtiny._free(dataptr);//清除释放申请的图片内存,防止内存泄露
pngtiny._free(retdata);//清除释放申请的状态位内存,防止内存泄露
};
}
这里详细注释了使用方式
from pngtiny.
pngtiny.js这个看tiny.c文件,tiny.c只开放了一个_tiny函数。该函数的传入一个图片buffer,一个图片大小,一个状态。使用方式看那个uploadFile函数。
function uploadFile(file, ufid) { $('#conli'+ufid+' .status').html('正在处理'); var reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = function(e) { var fcont = new Uint8Array(e.target.result);//读取图片内容 var fsize = fcont.byteLength;//获取图片大小 var dataptr = pngtiny._malloc(fsize);//申请图片大小的内存 var retdata = pngtiny._malloc(4);//申请状态位内存 pngtiny.HEAPU8.set(fcont,dataptr);//给图片内存设置数据,以便webassembly读取 pngtiny._tiny(dataptr,fsize,retdata);//传入tiny函数 var rdata = new Int32Array(pngtiny.HEAPU8.buffer, retdata, 1);//获取图片处理结果 var size = rdata[0];//如果图片正常处理了,会返回处理后的图片大小,否则置0 var blob=null; if(size>0) {//图片大于0,代表正常处理图片了 var pic = new Uint8Array(size);//初始化一个处理后图片大小的变量 var rdata = new Uint8Array(pngtiny.HEAPU8.buffer, dataptr, size);//获取处理后的图片内容,tiny.c里面如果正常处理了图片,会把图片内容保存到原来的图片内存地址里面,所以获取时候就从原来地址内存获取就好 pic.set(rdata);//把图片内容赋值给js变量 blob = new Blob([pic.buffer], {type: "image/png"});//把buffer转为blob pic=null; } else {//不正常处理图片,返回原图 blob = new Blob([fcont.buffer], {type: "image/png"}); size=fsize; } fcont=null; var url = (URL || webkitURL).createObjectURL(blob);//从blob获取图片链接 imgList.push({name:file.name,data:blob}); var dr = ((fsize-size)*100/fsize|0)+'%'; $('#conli'+ufid+' .status').html('完成'); $('#conli'+ufid+' .after').html(getSizeTrans(size)); $('#conli'+ufid+' .deal').html(dr); $('#conli'+ufid+' .down').html('<a href="'+url+'" download="'+file.name+'">下载</a>'); pngtiny._free(dataptr);//清除释放申请的图片内存,防止内存泄露 pngtiny._free(retdata);//清除释放申请的状态位内存,防止内存泄露 }; }这里详细注释了使用方式
😃 ok了解了。我这就去看看tiny.c 谢谢贴心大佬还写了这么详细的注释。我与大佬还有不小的差距😅
from pngtiny.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pngtiny.