{"size": 2584, "ext": "js", "max_stars_repo_path": "default.js", "max_stars_repo_name": "hellifiknow/image-web-workers", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "default.js", "max_issues_repo_name": "hellifiknow/image-web-workers", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "default.js", "max_forks_repo_name": "hellifiknow/image-web-workers", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 34.0, "max_line_length": 102, "alphanum_fraction": 0.5460526316}
﻿/* "use strict"; */

(function () {

    var source = document.getElementById("source");

    source.onload = function () {
        var start = new Date();

        var canvas = document.getElementById("target");
        canvas.width = source.clientWidth;
        canvas.height = source.clientHeight;

        if (!canvas.getContext) {
            log.innerHTML = "Canvas not supported. Please install an HTML5 compatible browser.";
            return;
        }

        var tempContext = canvas.getContext("2d");
        var len = canvas.width * canvas.height * 4;

        tempContext.drawImage(source, 0, 0, canvas.width, canvas.height);

        if (!window.Worker) {

            var canvasData = tempContext.getImageData(0, 0, canvas.width, canvas.height);
            var binaryData = canvasData.data;
            processSepia(binaryData, len);

            tempContext.putImageData(canvasData, 0, 0);
            var diff = new Date() - start;
            log.innerHTML = "Process done in " + diff + " ms (no web workers)";

            return;
        }

        var workersCount = 4;
        var finished = 0;
        var segmentLength = len / workersCount;
        var blockSize = canvas.height / workersCount;

        var onWorkEnded = function (e) {
            var canvasData = e.data.result;
            var index = e.data.index;

            tempContext.putImageData(canvasData, 0, blockSize * index);

            finished++;

            if (finished == workersCount) {
                var diff = new Date() - start;
                log.innerHTML = "Process done in " + diff + " ms using " + workersCount + " workers";
            }
        };

        for (var index = 0; index < workersCount; index++) {
            var worker = new Worker("pictureProcessor.js");
            worker.onmessage = onWorkEnded;

            var canvasData = tempContext.getImageData(0, blockSize * index, canvas.width, blockSize);
            
/*             worker.postMessage({ data: canvasData, index: index, length: segmentLength }); */
                        
            var objectData = { data: canvasData, index: index, length: segmentLength };
            worker.postMessage = worker.webkitPostMessage || worker.postMessage;
            worker.postMessage(objectData, [objectData.data.data.buffer]);
            if(objectData.data.data.byteLength)
            {
	            console.log("transferrable objects not supported");
            }
        }
    };

    source.src = "sample.png";
})();
