Auto adjust for pure black

This commit is contained in:
oc 2018-12-07 01:08:01 +08:00
parent 75bff6bcd6
commit 15b71dc60c
2 changed files with 16 additions and 2 deletions

View File

@ -587,15 +587,16 @@ editor_mode = function (editor) {
tempCanvas.imageSmoothingEnabled = false; tempCanvas.imageSmoothingEnabled = false;
tempCanvas.drawImage(image, 0, 0); tempCanvas.drawImage(image, 0, 0);
var imgData = tempCanvas.getImageData(0, 0, image.width, image.height); var imgData = tempCanvas.getImageData(0, 0, image.width, image.height);
var trans = 0, white = 0; var trans = 0, white = 0, black=0;
for (var i=0;i<image.width;i++) { for (var i=0;i<image.width;i++) {
for (var j=0;j<image.height;j++) { for (var j=0;j<image.height;j++) {
var pixel = getPixel(imgData, i, j); var pixel = getPixel(imgData, i, j);
if (pixel[3]==0) trans++; if (pixel[3]==0) trans++;
if (pixel[0]==255 && pixel[1]==255 && pixel[2]==255 && pixel[3]==255) white++; if (pixel[0]==255 && pixel[1]==255 && pixel[2]==255 && pixel[3]==255) white++;
if (pixel[0]==0 && pixel[1]==0 && pixel[2]==0 && pixel[3]==255) black++;
} }
} }
if (white>trans*10 && confirm("看起来这张图片是以为底色,是否自动调整为透明底色?")) { if (white>black && white>trans*10 && confirm("看起来这张图片是以白为底色,是否自动调整为透明底色?")) {
for (var i=0;i<image.width;i++) { for (var i=0;i<image.width;i++) {
for (var j=0;j<image.height;j++) { for (var j=0;j<image.height;j++) {
var pixel = getPixel(imgData, i, j); var pixel = getPixel(imgData, i, j);
@ -608,6 +609,19 @@ editor_mode = function (editor) {
tempCanvas.putImageData(imgData, 0, 0); tempCanvas.putImageData(imgData, 0, 0);
changed = true; changed = true;
} }
if (black>white && black>trans*10 && confirm("看起来这张图片是以纯黑为底色,是否自动调整为透明底色?")) {
for (var i=0;i<image.width;i++) {
for (var j=0;j<image.height;j++) {
var pixel = getPixel(imgData, i, j);
if (pixel[0]==0 && pixel[1]==0 && pixel[2]==0 && pixel[3]==255) {
setPixel(imgData, i, j, [0,0,0,0]);
}
}
}
tempCanvas.clearRect(0, 0, image.width, image.height);
tempCanvas.putImageData(imgData, 0, 0);
changed = true;
}
// Step 2: 检测长宽比 // Step 2: 检测长宽比
var ysize = selectAppend.value.indexOf('48') === -1 ? 32 : 48; var ysize = selectAppend.value.indexOf('48') === -1 ? 32 : 48;

Binary file not shown.