浏览代码

check for transparency in hat transparency-bounding-box for avatars, fixes #117

jomo 9 年之前
父节点
当前提交
f2dda3b939
共有 1 个文件被更改,包括 47 次插入23 次删除
  1. 47 23
      lib/skins.js

+ 47 - 23
lib/skins.js

@@ -43,37 +43,61 @@ exp.extract_helm = function(rid, facefile, buffer, outname, callback) {
             if (buf_err) {
               callback(buf_err);
             } else {
-              skin_img.crop(40, 8, 47, 15, function(crop_err, helm_img) {
-              if (crop_err) {
-                callback(crop_err);
-              } else {
-                face_img.paste(0, 0, helm_img, function(img_err, face_helm_img) {
-                  if (img_err) {
-                    callback(img_err);
+              // crop to hat transparency-bounding-box
+              skin_img.crop(32, 0, 63, 31, function(area_err, helm_area) {
+                if (area_err) {
+                  callback(area_err);
+                } else {
+                  /* eslint-disable no-labels */
+                  var is_opaque = true;
+                  if (skin_img.__trans) { // eslint-disable-line no-underscore-dangle
+                    xloop:
+                    for (var x = 0; x < helm_area.width(); x++) {
+                      for (var y = 0; y < helm_area.height(); y++) {
+                        // check if transparency-bounding-box has transparency
+                        if (helm_area.getPixel(x, y).a !== 100) {
+                          is_opaque = false;
+                          break xloop;
+                        }
+                      }
+                    }
+                    /* eslint-enable no-labels */
                   } else {
-                    if (!skin_img.__trans) {
-                      logging.debug(rid, "Skin is not transparent, skipping helm!");
-                      callback(null);
+                    is_opaque = true;
+                  }
+                  skin_img.crop(8, 8, 15, 15, function(crop_err, helm_img) {
+                    if (crop_err) {
+                      callback(crop_err);
                     } else {
-                      face_helm_img.toBuffer("png", {compression: "none"}, function(buf_err2, face_helm_buffer) {
-                        if (buf_err2) {
-                          callback(buf_err2);
+                      face_img.paste(0, 0, helm_img, function(img_err, face_helm_img) {
+                        if (img_err) {
+                          callback(img_err);
                         } else {
-                          if (face_helm_buffer.toString() !== face_buffer.toString()) {
-                            face_helm_img.writeFile(outname, function(write_err) {
-                              callback(write_err);
-                            });
-                          } else {
-                            logging.debug(rid, "helm img == face img, not storing!");
+                          if (is_opaque) {
+                            logging.debug(rid, "Skin is not transparent, skipping helm!");
                             callback(null);
+                          } else {
+                            face_helm_img.toBuffer("png", {compression: "none"}, function(buf_err2, face_helm_buffer) {
+                              if (buf_err2) {
+                                callback(buf_err2);
+                              } else {
+                                if (face_helm_buffer.toString() !== face_buffer.toString()) {
+                                  face_helm_img.writeFile(outname, function(write_err) {
+                                    callback(write_err);
+                                  });
+                                } else {
+                                  logging.debug(rid, "helm img == face img, not storing!");
+                                  callback(null);
+                                }
+                              }
+                            });
                           }
                         }
                       });
                     }
-                  }
-                });
-              }
-            });
+                  });
+                }
+              });
             }
           });
         }