UNPKG

barcode-detector-polyfill

Version:
84 lines (83 loc) 84.4 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define("BarcodeDetector", [], factory); else if(typeof exports === 'object') exports["BarcodeDetector"] = factory(); else root["BarcodeDetector"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 1); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { module.exports = function() { return __webpack_require__(2)("/******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId]) {\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/ \t\t}\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\ti: moduleId,\n/******/ \t\t\tl: false,\n/******/ \t\t\texports: {}\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.l = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// identity function for calling harmony imports with the correct context\n/******/ \t__webpack_require__.i = function(value) { return value; };\n/******/\n/******/ \t// define getter function for harmony exports\n/******/ \t__webpack_require__.d = function(exports, name, getter) {\n/******/ \t\tif(!__webpack_require__.o(exports, name)) {\n/******/ \t\t\tObject.defineProperty(exports, name, {\n/******/ \t\t\t\tconfigurable: false,\n/******/ \t\t\t\tenumerable: true,\n/******/ \t\t\t\tget: getter\n/******/ \t\t\t});\n/******/ \t\t}\n/******/ \t};\n/******/\n/******/ \t// getDefaultExport function for compatibility with non-harmony modules\n/******/ \t__webpack_require__.n = function(module) {\n/******/ \t\tvar getter = module && module.__esModule ?\n/******/ \t\t\tfunction getDefault() { return module['default']; } :\n/******/ \t\t\tfunction getModuleExports() { return module; };\n/******/ \t\t__webpack_require__.d(getter, 'a', getter);\n/******/ \t\treturn getter;\n/******/ \t};\n/******/\n/******/ \t// Object.prototype.hasOwnProperty.call\n/******/ \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(__webpack_require__.s = 0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ (function(module, exports, __webpack_require__) {\n\n\"use strict\";\n\n\n/* eslint-disable */\n\n/* --------------------------------------------------\nJavascript Only Barcode_Reader (BarcodeReader) V1.6 by Eddie Larsson <https://github.com/EddieLa/BarcodeReader>\n\nThis software is provided under the MIT license, http://opensource.org/licenses/MIT.\nAll use of this software must include this\ntext, including the reference to the creator of the original source code. The\noriginator accepts no responsibility of any kind pertaining to\nuse of this software.\n\nCopyright (c) 2013 Eddie Larsson\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n------------------------ */\n\nvar availableFormats = [\"Code128\", \"Code93\", \"Code39\", \"EAN-13\", \"2Of5\", \"Inter2Of5\", \"Codabar\"];\nvar SecureCodabar = true;\nvar Secure2Of5 = true;\nvar Image = void 0,\n ScanImage = void 0,\n allTables = void 0,\n FormatPriority = void 0,\n Multiple = void 0,\n Locations = void 0;\n\nfunction Rotate(data, width, height, rotation) {\n var newData = [];\n var x, y;\n switch (rotation) {\n case 90:\n for (x = 0; x < width * 4; x += 4) {\n for (y = width * 4 * (height - 1); y >= 0; y -= width * 4) {\n newData.push(data[x + y]);\n newData.push(data[x + y + 1]);\n newData.push(data[x + y + 2]);\n newData.push(data[x + y + 3]);\n }\n }\n break;\n case -90:\n for (x = width * 4 - 4; x >= 0; x -= 4) {\n for (y = 0; y < data.length; y += width * 4) {\n newData.push(data[x + y]);\n newData.push(data[x + y + 1]);\n newData.push(data[x + y + 2]);\n newData.push(data[x + y + 3]);\n }\n }\n break;\n case 180:\n for (y = width * 4 * (height - 1); y >= 0; y -= width * 4) {\n for (x = width * 4 - 4; x >= 0; x -= 4) {\n newData.push(data[x + y]);\n newData.push(data[x + y + 1]);\n newData.push(data[x + y + 2]);\n newData.push(data[x + y + 3]);\n }\n }\n }\n return new Uint8ClampedArray(newData);\n}\n\nfunction BoxFilter(data, width, radius) {\n var elements = [];\n var sum = [];\n var val;\n var x, y, i;\n for (x = 0; x < width; x++) {\n elements.push([]);\n sum.push(0);\n for (y = 0; y < (radius + 1) * width; y += width) {\n elements[elements.length - 1].push(data[x + y]);\n sum[sum.length - 1] = sum[sum.length - 1] + data[x + y];\n }\n }\n var newData = [];\n for (y = 0; y < data.length; y += width) {\n for (x = 0; x < width; x++) {\n var newVal = 0;\n var length = 0;\n for (i = x; i >= 0; i--) {\n newVal += sum[i];\n length++;\n if (length === radius + 1) break;\n }\n var tempLength = 0;\n for (i = x + 1; i < width; i++) {\n newVal += sum[i];\n length++;\n tempLength++;\n if (tempLength === radius) break;\n }\n length *= elements[0].length;\n newVal /= length;\n newData.push(newVal);\n }\n if (y - radius * width >= 0) {\n for (i = 0; i < elements.length; i++) {\n val = elements[i].shift();\n sum[i] = sum[i] - val;\n }\n }\n if (y + (radius + 1) * width < data.length) {\n for (i = 0; i < elements.length; i++) {\n val = data[i + y + (radius + 1) * width];\n elements[i].push(val);\n sum[i] = sum[i] + val;\n }\n }\n }\n return newData;\n}\n\nfunction Scale(data, width, height) {\n var newData = [];\n var x, y;\n for (y = 0; y < data.length; y += width * 8) {\n for (x = 0; x < width * 4; x += 8) {\n var r = (data[y + x] + data[y + x + 4] + data[y + width * 4 + x] + data[y + width * 4 + x + 4]) / 4;\n newData.push(r);\n var g = (data[y + x + 1] + data[y + x + 4 + 1] + data[y + width * 4 + x + 1] + data[y + width * 4 + x + 4 + 1]) / 4;\n newData.push(g);\n var b = (data[y + x + 2] + data[y + x + 4 + 2] + data[y + width * 4 + x + 2] + data[y + width * 4 + x + 4 + 2]) / 4;\n newData.push(b);\n newData.push(255);\n }\n }\n return new Uint8ClampedArray(newData);\n}\n\nfunction IntensityGradient(data, width) {\n var newData = [];\n var max = Number.MIN_VALUE;\n var min = Number.MAX_VALUE;\n var x, y, i;\n for (y = 0; y < data.length; y += width * 4) {\n for (x = 0; x < width * 4; x += 4) {\n var horizontalDiff = 0;\n var verticalDiff = 0;\n for (i = 1; i < 2; i++) {\n if (x + i * 4 < width * 4) {\n horizontalDiff = horizontalDiff + Math.abs(data[y + x] - data[y + x + i * 4]);\n }\n if (y + width * 4 * i < data.length) {\n verticalDiff += verticalDiff + Math.abs(data[y + x] - data[y + x + width * 4 * i]);\n }\n }\n var diff = horizontalDiff - verticalDiff;\n max = diff > max ? diff : max;\n min = diff < min ? diff : min;\n newData.push(diff);\n }\n }\n if (min < 0) {\n for (i = 0; i < newData.length; i++) {\n newData[i] = newData[i] - min;\n }\n min = 0;\n }\n return newData;\n}\n\nfunction greyScale(data) {\n var i;\n for (i = 0; i < data.length; i += 4) {\n var max = 0;\n var min = 255;\n max = data[i] > max ? data[i] : max;\n max = data[i + 1] > max ? data[i + 1] : max;\n max = data[i + 2] > max ? data[i + 2] : max;\n min = data[i] < min ? data[i] : min;\n min = data[i + 1] < min ? data[i + 1] : min;\n min = data[i + 2] < min ? data[i + 2] : min;\n data[i] = data[i + 1] = data[i + 2] = (max + min) / 2;\n }\n}\n\nfunction histogram(data) {\n var i;\n var hist = [];\n for (i = 0; i < 256; i++) {\n hist[i] = 0;\n }\n for (i = 0; i < data.length; i += 4) {\n hist[data[i]] = hist[data[i]] + 1;\n }\n return hist;\n}\n\nfunction otsu(histogram, total) {\n var i;\n var sum = 0;\n for (i = 1; i < histogram.length; ++i) {\n sum += i * histogram[i];\n }var sumB = 0;\n var wB = 0;\n var wF = 0;\n var mB;\n var mF;\n var max = 0.0;\n var between = 0.0;\n var threshold1 = 0.0;\n var threshold2 = 0.0;\n for (i = 0; i < histogram.length; ++i) {\n wB += histogram[i];\n if (wB === 0) continue;\n wF = total - wB;\n if (wF === 0) break;\n sumB += i * histogram[i];\n mB = sumB / wB;\n mF = (sum - sumB) / wF;\n between = wB * wF * Math.pow(mB - mF, 2);\n if (between >= max) {\n threshold1 = i;\n if (between > max) {\n threshold2 = i;\n }\n max = between;\n }\n }\n return (threshold1 + threshold2) / 2.0;\n}\n\nfunction CreateImageData() {\n Image.data = new Uint8ClampedArray(Image.width * Image.height * 4);\n var Converter;\n var x, y;\n for (y = 0; y < Image.height; y++) {\n for (x = 0; x < Image.width; x++) {\n Converter = y * 4 * Image.width;\n Image.data[Converter + x * 4] = Image.table[x][y][0];\n Image.data[Converter + x * 4 + 1] = Image.table[x][y][1];\n Image.data[Converter + x * 4 + 2] = Image.table[x][y][2];\n Image.data[Converter + x * 4 + 3] = Image.table[x][y][3];\n }\n }\n}\n\nfunction CreateScanImageData() {\n ScanImage.data = new Uint8ClampedArray(ScanImage.width * ScanImage.height * 4);\n var Converter;\n var x, y;\n for (y = 0; y < ScanImage.height; y++) {\n for (x = 0; x < ScanImage.width; x++) {\n Converter = y * 4 * ScanImage.width;\n ScanImage.data[Converter + x * 4] = ScanImage.table[x][y][0];\n ScanImage.data[Converter + x * 4 + 1] = ScanImage.table[x][y][1];\n ScanImage.data[Converter + x * 4 + 2] = ScanImage.table[x][y][2];\n ScanImage.data[Converter + x * 4 + 3] = ScanImage.table[x][y][3];\n }\n }\n}\n\nfunction CreateTable() {\n Image.table = [];\n var tempArray = [];\n var i, j;\n for (i = 0; i < Image.width * 4; i += 4) {\n tempArray = [];\n for (j = i; j < Image.data.length; j += Image.width * 4) {\n tempArray.push([Image.data[j], Image.data[j + 1], Image.data[j + 2], Image.data[j + 3]]);\n }\n Image.table.push(tempArray);\n }\n}\n\nfunction CreateScanTable() {\n ScanImage.table = [];\n var tempArray = [];\n var i, j;\n for (i = 0; i < ScanImage.width * 4; i += 4) {\n tempArray = [];\n for (j = i; j < ScanImage.data.length; j += ScanImage.width * 4) {\n tempArray.push([ScanImage.data[j], ScanImage.data[j + 1], ScanImage.data[j + 2], ScanImage.data[j + 3]]);\n }\n ScanImage.table.push(tempArray);\n }\n}\n\nfunction EnlargeTable(h, w) {\n var TempArray = [];\n var x, y, i;\n for (x = 0; x < Image.width; x++) {\n TempArray = [];\n for (y = 0; y < Image.height; y++) {\n for (i = 0; i < h; i++) {\n TempArray.push(Image.table[x][y]);\n }\n }\n Image.table[x] = TempArray.slice();\n }\n TempArray = Image.table.slice();\n for (x = 0; x < Image.width; x++) {\n for (i = 0; i < w; i++) {\n Image.table[x * w + i] = TempArray[x].slice();\n }\n }\n Image.width = Image.table.length;\n Image.height = Image.table[0].length;\n CreateImageData();\n}\n\nfunction ScaleHeight(scale) {\n var tempArray = [];\n var avrgRed = 0;\n var avrgGreen = 0;\n var avrgBlue = 0;\n var i, j, k;\n for (i = 0; i < Image.height - scale; i += scale) {\n for (j = 0; j < Image.width; j++) {\n avrgRed = 0;\n avrgGreen = 0;\n avrgBlue = 0;\n for (k = i; k < i + scale; k++) {\n avrgRed += Image.table[j][k][0];\n avrgGreen += Image.table[j][k][1];\n avrgBlue += Image.table[j][k][2];\n }\n tempArray.push(avrgRed / scale);\n tempArray.push(avrgGreen / scale);\n tempArray.push(avrgBlue / scale);\n tempArray.push(255);\n }\n }\n return new Uint8ClampedArray(tempArray);\n}\n\nfunction Intersects(rectOne, rectTwo) {\n return rectOne[0][0] <= rectTwo[0][1] && rectTwo[0][0] <= rectOne[0][1] && rectOne[1][0] <= rectTwo[1][1] && rectTwo[1][0] <= rectOne[1][1];\n}\n\nfunction maxLocalization(max, maxPos, data) {\n var originalMax = max;\n var rects = [];\n var x, y, i;\n do {\n var startX = maxPos % Image.width;\n var startY = (maxPos - startX) / Image.width;\n var minY = 0;\n var maxY = Image.height;\n var minX = 0;\n var maxX = Image.width - 1;\n for (y = startY; y < Image.height - 1; y++) {\n if (Image.table[startX][y + 1][0] === 0) {\n maxY = y;\n break;\n }\n }\n for (y = startY; y > 0; y--) {\n if (Image.table[startX][y - 1][0] === 0) {\n minY = y;\n break;\n }\n }\n for (x = startX; x < Image.width - 1; x++) {\n if (Image.table[x + 1][startY][0] === 0) {\n maxX = x;\n break;\n }\n }\n for (x = startX; x > 0; x--) {\n if (Image.table[x - 1][startY][0] === 0) {\n minX = x;\n break;\n }\n }\n for (y = minY * Image.width; y <= maxY * Image.width; y += Image.width) {\n for (x = minX; x <= maxX; x++) {\n data[y + x] = 0;\n }\n }\n var newRect = [[minX, maxX], [minY, maxY]];\n for (i = 0; i < rects.length; i++) {\n if (Intersects(newRect, rects[i])) {\n if (rects[i][0][1] - rects[i][0][0] > newRect[0][1] - newRect[0][0]) {\n rects[i][0][0] = rects[i][0][0] < newRect[0][0] ? rects[i][0][0] : newRect[0][0];\n rects[i][0][1] = rects[i][0][1] > newRect[0][1] ? rects[i][0][1] : newRect[0][1];\n newRect = [];\n break;\n } else {\n rects[i][0][0] = rects[i][0][0] < newRect[0][0] ? rects[i][0][0] : newRect[0][0];\n rects[i][0][1] = rects[i][0][1] > newRect[0][1] ? rects[i][0][1] : newRect[0][1];\n rects[i][1][0] = newRect[1][0];\n rects[i][1][1] = newRect[1][1];\n newRect = [];\n break;\n }\n }\n }\n if (newRect.length > 0) {\n rects.push(newRect);\n }\n max = 0;\n maxPos = 0;\n var newMaxPos = 0;\n for (i = 0; i < data.length; i++) {\n if (data[i] > max) {\n max = data[i];\n maxPos = i;\n }\n }\n } while (max > originalMax * 0.70);\n return rects;\n}\n\nfunction ImgProcessing() {\n greyScale(Image.data);\n var newData = IntensityGradient(Image.data, Image.width);\n newData = BoxFilter(newData, Image.width, 15);\n var min = newData[0];\n var i, x, y;\n for (i = 1; i < newData.length; i++) {\n min = min > newData[i] ? newData[i] : min;\n }\n var max = 0;\n var maxPos = 0;\n var avrgLight = 0;\n for (i = 0; i < newData.length; i++) {\n newData[i] = Math.round(newData[i] - min);\n avrgLight += newData[i];\n if (max < newData[i]) {\n max = newData[i];\n maxPos = i;\n }\n }\n avrgLight /= newData.length;\n if (avrgLight < 15) {\n newData = BoxFilter(newData, Image.width, 8);\n min = newData[0];\n for (i = 1; i < newData.length; i++) {\n min = min > newData[i] ? newData[i] : min;\n }\n max = 0;\n maxPos = 0;\n for (i = 0; i < newData.length; i++) {\n newData[i] = Math.round(newData[i] - min);\n if (max < newData[i]) {\n max = newData[i];\n maxPos = i;\n }\n }\n }\n var hist = [];\n for (i = 0; i <= max; i++) {\n hist[i] = 0;\n }\n for (i = 0; i < newData.length; i++) {\n hist[newData[i]] = hist[newData[i]] + 1;\n }\n var thresh = otsu(hist, newData.length);\n for (i = 0; i < newData.length; i++) {\n if (newData[i] < thresh) {\n Image.data[i * 4] = Image.data[i * 4 + 1] = Image.data[i * 4 + 2] = 0;\n } else {\n Image.data[i * 4] = Image.data[i * 4 + 1] = Image.data[i * 4 + 2] = 255;\n }\n }\n CreateTable();\n var rects = maxLocalization(max, maxPos, newData);\n var feedBack = [];\n for (i = 0; i < rects.length; i++) {\n feedBack.push({\n x: rects[i][0][0],\n y: rects[i][1][0],\n width: rects[i][0][1] - rects[i][0][0],\n height: rects[i][1][1] - rects[i][1][0]\n });\n }\n if (feedBack.length > 0) postMessage({\n result: feedBack,\n success: \"localization\"\n });\n Locations = feedBack;\n allTables = [];\n for (i = 0; i < rects.length; i++) {\n var newTable = [];\n for (x = rects[i][0][0] * 2; x < rects[i][0][1] * 2; x++) {\n var tempArray = [];\n for (y = rects[i][1][0] * 2; y < rects[i][1][1] * 2; y++) {\n tempArray.push([ScanImage.table[x][y][0], ScanImage.table[x][y][1], ScanImage.table[x][y][2], 255]);\n }\n newTable.push(tempArray);\n }\n if (newTable.length < 1) continue;\n Image.table = newTable;\n Image.width = newTable.length;\n Image.height = newTable[0].length;\n CreateImageData();\n allTables.push({\n table: newTable,\n data: new Uint8ClampedArray(Image.data),\n width: Image.width,\n height: Image.height\n });\n }\n}\n\nfunction showImage(data, width, height) {\n postMessage({\n result: data,\n width: width,\n height: height,\n success: \"image\"\n });\n}\n\nfunction Main() {\n ImgProcessing();\n var allResults = [];\n var tempObj;\n var tempData;\n var hist;\n var val;\n var thresh;\n var start;\n var end;\n var z, i;\n for (z = 0; z < allTables.length; z++) {\n Image = allTables[z];\n var scaled = ScaleHeight(30);\n var variationData;\n var incrmt = 0;\n var format = \"\";\n var first = true;\n var eanStatistics = {};\n var eanOrder = [];\n var Selection = false;\n do {\n tempData = scaled.subarray(incrmt, incrmt + Image.width * 4);\n hist = [];\n for (i = 0; i < 256; i++) {\n hist[i] = 0;\n }\n for (i = 0; i < tempData.length; i += 4) {\n val = Math.round((tempData[i] + tempData[i + 1] + tempData[i + 2]) / 3);\n hist[val] = hist[val] + 1;\n }\n thresh = otsu(hist, tempData.length / 4);\n start = thresh < 41 ? 1 : thresh - 40;\n end = thresh > 254 - 40 ? 254 : thresh + 40;\n variationData = yStraighten(tempData, start, end);\n Selection = BinaryString(variationData);\n if (Selection.string) {\n format = Selection.format;\n tempObj = Selection;\n Selection = Selection.string;\n if (format === \"EAN-13\") {\n if (typeof eanStatistics[Selection] === 'undefined') {\n eanStatistics[Selection] = {\n count: 1,\n correction: tempObj.correction\n };\n eanOrder.push(Selection);\n } else {\n eanStatistics[Selection].count = eanStatistics[Selection].count + 1;\n eanStatistics[Selection].correction = eanStatistics[Selection].correction + tempObj.correction;\n }\n Selection = false;\n }\n } else {\n Selection = false;\n }\n incrmt += Image.width * 4;\n } while (!Selection && incrmt < scaled.length);\n if (Selection && format !== \"EAN-13\") allResults.push({\n Format: format,\n Value: Selection\n });\n if (format === \"EAN-13\") Selection = false;\n if (!Selection) {\n EnlargeTable(4, 2);\n incrmt = 0;\n scaled = ScaleHeight(20);\n do {\n tempData = scaled.subarray(incrmt, incrmt + Image.width * 4);\n hist = [];\n for (i = 0; i < 256; i++) {\n hist[i] = 0;\n }\n for (i = 0; i < tempData.length; i += 4) {\n val = Math.round((tempData[i] + tempData[i + 1] + tempData[i + 2]) / 3);\n hist[val] = hist[val] + 1;\n }\n thresh = otsu(hist, tempData.length / 4);\n start = thresh < 40 ? 0 : thresh - 40;\n end = thresh > 255 - 40 ? 255 : thresh + 40;\n variationData = yStraighten(tempData, start, end);\n Selection = BinaryString(variationData);\n if (Selection.string) {\n format = Selection.format;\n tempObj = Selection;\n Selection = Selection.string;\n if (format === \"EAN-13\") {\n if (typeof eanStatistics[Selection] === 'undefined') {\n eanStatistics[Selection] = {\n count: 1,\n correction: tempObj.correction\n };\n eanOrder.push(Selection);\n } else {\n eanStatistics[Selection].count = eanStatistics[Selection].count + 1;\n eanStatistics[Selection].correction = eanStatistics[Selection].correction + tempObj.correction;\n }\n Selection = false;\n }\n } else {\n Selection = false;\n }\n incrmt += Image.width * 4;\n } while (!Selection && incrmt < scaled.length);\n if (format === \"EAN-13\") {\n var points = {};\n for (var key in eanStatistics) {\n eanStatistics[key].correction = eanStatistics[key].correction / eanStatistics[key].count;\n var pointTemp = eanStatistics[key].correction;\n pointTemp -= eanStatistics[key].count;\n pointTemp += eanOrder.indexOf(key);\n points[key] = pointTemp;\n }\n var minPoints = Number.POSITIVE_INFINITY;\n var tempString = \"\";\n for (var point in points) {\n if (points[point] < minPoints) {\n minPoints = points[point];\n tempString = key;\n }\n }\n if (minPoints < 11) {\n Selection = tempString;\n } else {\n Selection = false;\n }\n }\n if (Selection) allResults.push({\n Format: format,\n Value: Selection,\n bBox: Locations[z]\n });\n }\n if (allResults.length > 0 && !Multiple) break;\n }\n return allResults;\n}\n\nfunction yStraighten(img, start, end) {\n var average = 0;\n var threshold;\n var newImg = new Uint8ClampedArray(Image.width * (end - start + 1) * 4);\n var i, j;\n for (i = 0; i < newImg.length; i++) {\n newImg[i] = 255;\n }\n for (i = 0; i < Image.width * 4; i += 4) {\n threshold = end;\n average = (img[i] + img[i + 1] + img[i + 2]) / 3;\n if (i < Image.width * 4 - 4) {\n average += (img[i + 4] + img[i + 5] + img[i + 6]) / 3;\n average /= 2;\n }\n for (j = i; j < newImg.length; j += Image.width * 4) {\n if (average < threshold) {\n newImg[j] = newImg[j + 1] = newImg[j + 2] = 0;\n }\n threshold--;\n }\n }\n return newImg;\n}\n\nfunction CheckEan13(values, middle) {\n if (middle) {\n if (values.length !== 5) return false;\n } else {\n if (values.length !== 3) return false;\n }\n var avrg = 0;\n var i;\n for (i = 0; i < values.length; i++) {\n avrg += values[i];\n }\n avrg /= values.length;\n for (i = 0; i < values.length; i++) {\n if (values[i] / avrg < 0.5 || values[i] / avrg > 1.5) return false;\n }\n return true;\n}\n\nfunction TwoOfFiveStartEnd(values, start) {\n if (values.length < 5 || values.length > 6) return false;\n var maximum = 0;\n var TwoOfFiveMax = [0, 0];\n var u;\n for (u = 0; u < values.length; u++) {\n if (values[u] > maximum) {\n maximum = values[u];\n TwoOfFiveMax[0] = u;\n }\n }\n maximum = 0;\n for (u = 0; u < values.length; u++) {\n if (u === TwoOfFiveMax[0]) continue;\n if (values[u] > maximum) {\n maximum = values[u];\n TwoOfFiveMax[1] = u;\n }\n }\n if (start) {\n return TwoOfFiveMax[0] + TwoOfFiveMax[1] === 2;\n } else {\n return TwoOfFiveMax[0] + TwoOfFiveMax[1] === 2;\n }\n}\n\nfunction CheckInterleaved(values, start) {\n var average = 0;\n var i;\n for (i = 0; i < values.length; i++) {\n average += values[i];\n }\n average /= 4;\n if (start) {\n if (values.length !== 4) return false;\n for (i = 0; i < values.length; i++) {\n if (values[i] / average < 0.5 || values[i] / average > 1.5) return false;\n }\n return true;\n } else {\n if (values.length !== 3) return false;\n var max = 0;\n var pos;\n for (i = 0; i < values.length; i++) {\n if (values[i] > max) {\n max = values[i];\n pos = i;\n }\n }\n if (pos !== 0) return false;\n if (values[0] / average < 1.5 || values[0] / average > 2.5) return false;\n for (i = 1; i < values.length; i++) {\n if (values[i] / average < 0.5 || values[i] / average > 1.5) return false;\n }\n return true;\n }\n}\n\nfunction BinaryConfiguration(binaryString, type) {\n var result = [];\n var binTemp = [];\n var count = 0;\n var bars;\n var len;\n var totalBars;\n var i;\n if (type === \"Code128\" || type === \"Code93\") {\n totalBars = 6;\n len = binaryString[0];\n if (type === \"Code128\") len /= 2;\n for (i = 0; i < binaryString.length; i++) {\n if (binaryString[i] > len * 6) {\n binaryString.splice(i, binaryString.length);\n break;\n }\n }\n do {\n if (binaryString.length === 7 && type === \"Code128\") {\n result.push(binaryString.splice(0, binaryString.length));\n } else {\n result.push(binaryString.splice(0, totalBars));\n }\n if (type === \"Code93\" && binaryString.length < 6) binaryString.splice(0, totalBars);\n } while (binaryString.length > 0);\n }\n if (type === \"Code39\") {\n totalBars = 9;\n len = binaryString[0];\n for (i = 0; i < binaryString.length; i++) {\n if (binaryString[i] > len * 5) {\n binaryString.splice(i, binaryString.length);\n break;\n }\n }\n do {\n result.push(binaryString.splice(0, totalBars));\n binaryString.splice(0, 1);\n } while (binaryString.length > 0);\n }\n if (type === \"EAN-13\") {\n totalBars = 4;\n len = binaryString[0];\n var secureCount = 0;\n for (i = 0; i < binaryString.length; i++) {\n if (binaryString[i] > len * 6) {\n binaryString.splice(i, binaryString.length);\n break;\n }\n }\n if (CheckEan13(binaryString.splice(0, 3), false)) secureCount++;\n count = 0;\n do {\n result.push(binaryString.splice(0, totalBars));\n count++;\n if (count === 6) if (CheckEan13(binaryString.splice(0, 5), true)) secureCount++;\n } while (result.length < 12 && binaryString.length > 0);\n if (CheckEan13(binaryString.splice(0, 3), false)) secureCount++;\n if (secureCount < 2) return [];\n }\n if (type === \"2Of5\") {\n totalBars = 5;\n len = binaryString[0] / 2;\n for (i = 0; i < binaryString.length; i++) {\n if (binaryString[i] > len * 5) {\n binaryString.splice(i, binaryString.length);\n break;\n }\n }\n var temp = binaryString.splice(0, 6);\n result.push(temp);\n do {\n binTemp = [];\n for (i = 0; i < totalBars; i++) {\n binTemp.push(binaryString.splice(0, 1)[0]);\n // binaryString.splice(0, 1)[0];\n }\n result.push(binTemp);\n if (binaryString.length === 5) result.push(binaryString.splice(0, 5));\n } while (binaryString.length > 0);\n }\n if (type === \"Inter2Of5\") {\n totalBars = 5;\n len = binaryString[0];\n for (i = 0; i < binaryString.length; i++) {\n if (binaryString[i] > len * 5) {\n binaryString.splice(i, binaryString.length);\n break;\n }\n }\n result.push(binaryString.splice(0, 4));\n var binTempWhite = [];\n do {\n binTemp = [];\n binTempWhite = [];\n for (i = 0; i < totalBars; i++) {\n binTemp.push(binaryString.splice(0, 1)[0]);\n binTempWhite.push(binaryString.splice(0, 1)[0]);\n }\n result.push(binTemp);\n result.push(binTempWhite);\n if (binaryString.length === 3) result.push(binaryString.splice(0, 3));\n } while (binaryString.length > 0);\n }\n if (type === \"Codabar\") {\n totalBars = 7;\n len = binaryString[0];\n for (i = 0; i < binaryString.length; i++) {\n if (binaryString[i] > len * 5) {\n binaryString.splice(i, binaryString.length);\n break;\n }\n }\n do {\n result.push(binaryString.splice(0, totalBars));\n binaryString.splice(0, 1);\n } while (binaryString.length > 0);\n }\n return result;\n}\n\nfunction BinaryString(img, type) {\n var binaryString = [];\n var binTemp = [];\n var container = 255;\n var count = 0;\n var format;\n var tempString;\n var j, i;\n var corrections;\n for (j = 0; j < img.length - Image.width * 4; j += Image.width * 4) {\n var SlicedArray = img.subarray(j, j + Image.width * 4);\n binaryString = [];\n i = 0;\n while (SlicedArray[i] === 255) {\n i += 4;\n }\n while (i < SlicedArray.length) {\n count = 0;\n container = SlicedArray[i];\n while (SlicedArray[i] === container && i < SlicedArray.length) {\n count++;\n i += 4;\n }\n binaryString.push(count);\n }\n if (binaryString.length > 2 && binaryString[0] <= binaryString[1] / 10) {\n binaryString.splice(0, 2);\n }\n var binaryHolder = binaryString.slice();\n var success = false;\n for (i = 0; i < FormatPriority.length; i++) {\n binaryString = binaryHolder.slice();\n var first;\n var second;\n binaryString = BinaryConfiguration(binaryString, FormatPriority[i]);\n if (FormatPriority[i] === \"2Of5\" || FormatPriority[i] === \"Inter2Of5\") {\n first = binaryString.splice(0, 1)[0];\n second = binaryString.splice(binaryString.length - 1, 1)[0];\n }\n binTemp = Distribution(binaryString, FormatPriority[i]);\n if (FormatPriority[i] === \"EAN-13\") {\n binaryString = binTemp.data;\n corrections = binTemp.correction;\n } else {\n binaryString = binTemp;\n }\n if (typeof binaryString === 'undefined') continue;\n if (binaryString.length > 4 || FormatPriority[i] === \"Code39\" && binaryString.length > 2) {\n if (FormatPriority[i] === \"Code128\") {\n if (CheckCode128(binaryString)) {\n binaryString = DecodeCode128(binaryString);\n success = true;\n }\n } else if (FormatPriority[i] === \"Code93\") {\n if (CheckCode93(binaryString)) {\n binaryString = DecodeCode93(binaryString);\n success = true;\n }\n } else if (FormatPriority[i] === \"Code39\") {\n if (CheckCode39(binaryString)) {\n binaryString = DecodeCode39(binaryString);\n success = true;\n }\n } else if (FormatPriority[i] === \"EAN-13\") {\n tempString = DecodeEAN13(binaryString);\n if (tempString) {\n if (tempString.length === 13) {\n binaryString = tempString;\n success = true;\n }\n }\n } else if (FormatPriority[i] === \"2Of5\" || FormatPriority[i] === \"Inter2Of5\") {\n if (FormatPriority[i] === \"2Of5\") {\n if (typeof first !== 'undefined') if (!TwoOfFiveStartEnd(first, true)) continue;\n if (typeof second !== 'undefined') if (!TwoOfFiveStartEnd(second, false)) continue;\n }\n if (FormatPriority[i] === \"Inter2Of5\") {\n if (typeof first !== 'undefined') if (!CheckInterleaved(first, true)) continue;\n if (typeof second !== 'undefined') if (!CheckInterleaved(second, false)) continue;\n }\n tempString = Decode2Of5(binaryString);\n if (tempString) {\n binaryString = tempString;\n success = true;\n }\n } else if (FormatPriority[i] === \"Codabar\") {\n tempString = DecodeCodaBar(binaryString);\n if (tempString) {\n binaryString = tempString;\n success = true;\n }\n }\n }\n if (success) {\n format = FormatPriority[i];\n if (format === \"Inter2Of5\") format = \"Interleaved 2 of 5\";\n if (format === \"2Of5\") format = \"Standard 2 of 5\";\n break;\n }\n }\n if (success) break;\n }\n if (format === \"Code128\") {\n if (typeof binaryString.string === 'string') {\n return binaryString;\n } else {\n return {\n string: false\n };\n }\n }\n if (typeof binaryString === 'string') {\n if (format === \"EAN-13\") {\n return {\n string: binaryString,\n format: format,\n correction: corrections\n };\n } else {\n return {\n string: binaryString,\n format: format\n };\n }\n } else {\n return {\n string: false\n };\n }\n}\n\nfunction Distribution(totalBinArray, type) {\n var testData = 0;\n var result = [];\n var totalBars;\n var total;\n var maxLength;\n var k, i, j;\n var blackMax;\n var whiteMax;\n var wideAvrg;\n var narrowAvrg;\n var prevPos;\n var wideValues;\n var max;\n\n type = availableFormats.indexOf(type);\n\n if (type === 0) {\n total = 11;\n totalBars = 6;\n maxLength = 4;\n } else if (type === 1) {\n total = 9;\n totalBars = 6;\n maxLength = 4;\n } else if (type === 2) {\n total = 12;\n totalBars = 9;\n } else if (type === 3) {\n total = 7;\n totalBars = 4;\n maxLength = 4;\n } else if (type === 6) {\n totalBars = 7;\n }\n for (k = 0; k < totalBinArray.length; k++) {\n var BinArray = totalBinArray[k];\n var sum = 0;\n var counter = 0;\n var tempBin = [];\n var narrowArr = [];\n var wideArr = [];\n if (type === 6) {\n var upperTolerance = 1.5;\n var lowerTolerance = 1 / 2;\n if (BinArray.length !== 7) return [];\n if (k === 0 || k === totalBinArray.length - 1) {\n whiteMax = [[0, 0], [0, 0]];\n blackMax = [0, 0];\n for (i = 0; i < BinArray.length; i++) {\n if (i % 2 === 0) {\n if (BinArray[i] > blackMax[0]) {\n blackMax[0] = BinArray[i];\n blackMax[1] = i;\n }\n } else {\n if (BinArray[i] > whiteMax[0][0]) {\n whiteMax[0][0] = BinArray[i];\n prevPos = whiteMax[0][1];\n whiteMax[0][1] = i;\n i = prevPos - 1;\n continue;\n }\n if (BinArray[i] > whiteMax[1][0] && i !== whiteMax[0][1]) {\n whiteMax[1][0] = BinArray[i];\n whiteMax[1][1] = i;\n }\n }\n }\n if (SecureCodabar) {\n wideAvrg = whiteMax[0][0] + whiteMax[1][0] + blackMax[0];\n wideAvrg /= 3;\n wideValues = [whiteMax[0][0], whiteMax[1][0], blackMax[0]];\n for (i = 0; i < wideValues.length; i++) {\n if (wideValues[i] / wideAvrg > upperTolerance || wideValues[i] / wideAvrg < lowerTolerance) return [];\n }\n narrowAvrg = 0;\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[1] || i === whiteMax[0][1] || i === whiteMax[1][1]) continue;\n narrowAvrg += BinArray[i];\n }\n narrowAvrg /= 4;\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[1] || i === whiteMax[0][1] || i === whiteMax[1][1]) continue;\n if (BinArray[i] / narrowAvrg > upperTolerance || BinArray[i] / narrowAvrg < lowerTolerance) return [];\n }\n }\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[1] || i === whiteMax[0][1] || i === whiteMax[1][1]) {\n tempBin.push(1);\n } else {\n tempBin.push(0);\n }\n }\n } else {\n blackMax = [0, 0];\n whiteMax = [0, 0];\n for (i = 0; i < BinArray.length; i++) {\n if (i % 2 === 0) {\n if (BinArray[i] > blackMax[0]) {\n blackMax[0] = BinArray[i];\n blackMax[1] = i;\n }\n } else {\n if (BinArray[i] > whiteMax[0]) {\n whiteMax[0] = BinArray[i];\n whiteMax[1] = i;\n }\n }\n }\n if (blackMax[0] / whiteMax[0] > 1.55) {\n var tempArray = blackMax;\n blackMax = [tempArray, [0, 0], [0, 0]];\n for (i = 0; i < BinArray.length; i++) {\n if (i % 2 === 0) {\n if (BinArray[i] > blackMax[1][0] && i !== blackMax[0][1]) {\n blackMax[1][0] = BinArray[i];\n prevPos = blackMax[1][1];\n blackMax[1][1] = i;\n i = prevPos - 1;\n continue;\n }\n if (BinArray[i] > blackMax[2][0] && i !== blackMax[0][1] && i !== blackMax[1][1]) {\n blackMax[2][0] = BinArray[i];\n blackMax[2][1] = i;\n }\n }\n }\n if (SecureCodabar) {\n wideAvrg = blackMax[0][0] + blackMax[1][0] + blackMax[2][0];\n wideAvrg /= 3;\n for (i = 0; i < blackMax.length; i++) {\n if (blackMax[i][0] / wideAvrg > upperTolerance || blackMax[i][0] / wideAvrg < lowerTolerance) return [];\n }\n narrowAvrg = 0;\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[0][1] || i === blackMax[1][1] || i === blackMax[2][1]) continue;\n narrowAvrg += BinArray[i];\n }\n narrowAvrg /= 4;\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[0][1] || i === blackMax[1][1] || i === blackMax[2][1]) continue;\n if (BinArray[i] / narrowAvrg > upperTolerance || BinArray[i] / narrowAvrg < lowerTolerance) return [];\n }\n }\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[0][1] || i === blackMax[1][1] || i === blackMax[2][1]) {\n tempBin.push(1);\n } else {\n tempBin.push(0);\n }\n }\n } else {\n if (SecureCodabar) {\n wideAvrg = blackMax[0] + whiteMax[0];\n wideAvrg /= 2;\n if (blackMax[0] / wideAvrg > 1.5 || blackMax[0] / wideAvrg < 0.5) return [];\n if (whiteMax[0] / wideAvrg > 1.5 || whiteMax[0] / wideAvrg < 0.5) return [];\n narrowAvrg = 0;\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[1] || i === whiteMax[1]) continue;\n narrowAvrg += BinArray[i];\n }\n narrowAvrg /= 5;\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[1] || i === whiteMax[1]) continue;\n if (BinArray[i] / narrowAvrg > upperTolerance || BinArray[i] / narrowAvrg < lowerTolerance) return [];\n }\n }\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[1] || i === whiteMax[1]) {\n tempBin.push(1);\n } else {\n tempBin.push(0);\n }\n }\n }\n }\n result.push(tempBin);\n continue;\n }\n if (type === 4 || type === 5) {\n max = [[0, 0], [0, 0]];\n for (i = 0; i < BinArray.length; i++) {\n if (!isFinite(BinArray[i])) return [];\n if (BinArray[i] > max[0][0]) {\n max[0][0] = BinArray[i];\n prevPos = max[0][1];\n max[0][1] = i;\n i = prevPos - 1;\n }\n if (BinArray[i] > max[1][0] && i !== max[0][1]) {\n max[1][0] = BinArray[i];\n max[1][1] = i;\n }\n }\n if (Secure2Of5) {\n wideAvrg = max[0][0] + max[1][0];\n wideAvrg /= 2;\n if (max[0][0] / wideAvrg > 1.3 || max[0][0] / wideAvrg < 0.7) return [];\n if (max[1][0] / wideAvrg > 1.3 || max[1][0] / wideAvrg < 0.7) return [];\n narrowAvrg = 0;\n for (i = 0; i < BinArray.length; i++) {\n if (i === max[0][1] || i === max[1][1]) continue;\n narrowAvrg += BinArray[i];\n }\n narrowAvrg /= 3;\n for (i = 0; i < BinArray.length; i++) {\n if (i === max[0][1] || i === max[1][1]) continue;\n if (BinArray[i] / narrowAvrg > 1.3 || BinArray[i] / narrowAvrg < 0.7) return [];\n }\n }\n for (i = 0; i < BinArray.length; i++) {\n if (i === max[0][1] || i === max[1][1]) {\n tempBin.push(1);\n continue;\n }\n tempBin.push(0);\n }\n result.push(tempBin);\n continue;\n }\n while (counter < totalBars) {\n sum += BinArray[counter];\n counter++;\n }\n if (type === 2) {\n var indexCount = [];\n blackMax = [[0, 0], [0, 0]];\n whiteMax = [0, 0];\n for (j = 0; j < BinArray.length; j++) {\n if (j % 2 === 0) {\n if (BinArray[j] > blackMax[0][0]) {\n blackMax[0][0] = BinArray[j];\n prevPos = blackMax[0][1];\n blackMax[0][1] = j;\n j = prevPos;\n }\n if (BinArray[j] > blackMax[1][0] && j !== blackMax[0][1]) {\n blackMax[1][0] = BinArray[j];\n blackMax[1][1] = j;\n }\n } else {\n if (BinArray[j] > whiteMax[0]) {\n whiteMax[0] = BinArray[j];\n whiteMax[1] = j;\n }\n }\n }\n if (whiteMax[0] / blackMax[0][0] > 1.5 && whiteMax[0] / blackMax[1][0] > 1.5) {\n blackMax = [[0, 0], [0, 0]];\n for (j = 0; j < BinArray.length; j++) {\n if (j % 2 !== 0) {\n if (BinArray[j] > blackMax[0][0] && j !== whiteMax[1]) {\n blackMax[0][0] = BinArray[j];\n prevPos = blackMax[0][1];\n blackMax[0][1] = j;\n j = prevPos;\n }\n if (BinArray[j] > blackMax[1][0] && j !== blackMax[0][1] && j !== whiteMax[1]) {\n blackMax[1][0] = BinArray[j];\n blackMax[1][1] = j;\n }\n }\n }\n }\n wideAvrg = blackMax[0][0] + blackMax[1][0] + whiteMax[0];\n wideAvrg /= 3;\n if (blackMax[0][0] / wideAvrg > 1.6 || blackMax[0][0] / wideAvrg < 0.4) return [];\n if (blackMax[1][0] / wideAvrg > 1.6 || blackMax[1][0] / wideAvrg < 0.4) return [];\n if (whiteMax[0] / wideAvrg > 1.6 || whiteMax[0] / wideAvrg < 0.4) return [];\n narrowAvrg = 0;\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[0][1] || i === blackMax[1][1] || i === whiteMax[1]) continue;\n narrowAvrg += BinArray[i];\n }\n narrowAvrg /= 6;\n for (i = 0; i < BinArray.length; i++) {\n if (i === blackMax[0][1] || i === blackMax[1][1] || i === whiteMax[1]) continue;\n if (BinArray[i] / narrowAvrg > 1.6 || BinArray[i] / narrowAvrg < 0.4) return [];\n }\n for (j = 0; j < BinArray.length; j++) {\n if (j === blackMax[0][1] || j === blackMax[1][1] || j === whiteMax[1]) {\n tempBin.push(2);\n } else {\n tempBin.push(1);\n }\n }\n result.push(tempBin);\n continue;\n }\n if (type === 3) {\n max = [[0, 0], [0, 0], [0, 0]];\n for (j = 0; j < BinArray.length; j++) {\n if (BinArray[j] > max[0][0]) {\n max[0][0] = BinArray[j];\n prevPos = max[0][1];\n max[0][1] = j;\n j = prevPos;\n }\n if (BinArray[j] > max[1][0] && j !== max[0][1]) {\n max[1][0] = BinArray[j];\n prevPos = max[1][1];\n max[1][1] = j;\n j = prevPos;\n }\n if (BinArray[j] > max[2][0] && j !== max[0][1] && j !== max[1][1]) {\n max[2][0] = BinArray[j];\n max[2][1] = j;\n }\n }\n if (max[0][0] / max[1][0] >= 3) {\n narrowAvrg = 0;\n for (j = 0; j < BinArray.length; j++) {\n if (j === max[0][1]) continue;\n narrowAvrg += BinArray[j];\n }\n narrowAvrg /= 3;\n for (j = 0; j < BinArray.length; j++) {\n if (j === max[0][1]) continue;\n if (BinArray[j] / narrowAvrg < 0.02 || BinArray[j] / narrowAvrg > 3) return {\n data: [],\n correction: 0\n };\n }\n if (max[0][0] / narrowAvrg < 2.2 || max[0][0] / narrowAvrg > 6) return {\n data: [],\n correction: 0\n };\n for (j = 0; j < BinArray.length; j++) {\n if (j === max[0][1]) {\n tempBin.push(4);\n } else {\n tempBin.push(1);\n }\n }\n result.push(tempBin);\n } else if (max[0][0] / max[2][0] > 2) {\n wideAvrg = max[0][0] + max[1][0];\n wideAvrg /= 5;\n if (max[0][0] / (wideAvrg * 3) < 0.02 || max[0][0] / (wideAvrg * 3) > 3) return {\n data: [],\n correction: 0\n };\n if (max[1][0] / (wideAvrg * 2) < 0.02 || max[1][0] / (wideAvrg * 2) > 3) return {\n data: [],\n correction: 0\n };\n narrowAvrg = 0;\n for (j = 0; j < BinArray.length; j++) {\n if (j === max[0][1] || j === max[1][1]) continue;\n narrowAvrg += BinArray[j];\n }\n narrowAvrg /= 2;\n for (j = 0; j < BinArray.length; j++) {\n if (j === max[0][1] || j === max[1][1]) continue;\n if (BinArray[j] / narrowAvrg < 0.02 || BinArray[j] / narrowAvrg > 3) return {\n data: [],\n correction: 0\n };\n }\n for (j = 0; j < BinArray.length; j++) {\n if (j === max[0][1]) {\n tempBin.push(3);\n } else if (j === max