First commit; calling this version 0.1
[imgurfusker.git] / nude.js
blob098278228b1f5afec96cfe18fc9b08a43d6eefa1
1 /*
2  * Nude.js - Nudity detection with Javascript and HTMLCanvas
3  * 
4  * Author: Patrick Wied ( http://www.patrick-wied.at )
5  * Version: 0.1  (2010-11-21)
6  * License: MIT License
7  */
8 (function(){
10         var nude = (function(){
11                 // private var definition
12                 var canvas = null,
13                 ctx = null,
14                 resultFn = null,
15                 // private functions
16                 initCanvas = function(){
17                         canvas = document.createElement("canvas");
18                         // the canvas should not be visible
19                         canvas.style.display = "none";
20                         var b = document.getElementsByTagName("body")[0];
21                         b.appendChild(canvas);
22                         ctx = canvas.getContext("2d");
23                 },
24                 loadImageById = function(id){
25                         // get the image
26                         var img = document.getElementById(id);
27                         // apply the width and height to the canvas element
28                         canvas.width = img.width;
29                         canvas.height = img.height;
30                         // reset the result function
31                         resultFn = null;
32                         // draw the image into the canvas element
33                         ctx.drawImage(img, 0, 0);
35                 },
36                 loadImageByElement = function(element){
37                         // apply width and height to the canvas element
38                         // make sure you set width and height at the element
39                         canvas.width = element.width;
40                         canvas.height = element.height;
41                         // reset result function
42                         resultFn = null;
43                         // draw the image/video element into the canvas
44                         ctx.drawImage(element, 0, 0);
45                 },
46                 scanImage = function(){
47                         // get the image data
48                         var image = ctx.getImageData(0, 0, canvas.width, canvas.height),
49                         imageData = image.data;
51                         var myWorker = new Worker('worker.nude.js'),
52                         message = [imageData, canvas.width, canvas.height];
53                         myWorker.postMessage(message);
54                         myWorker.onmessage = function(event){
55                                 resultHandler(event.data);
56                         }
57                 },
58                 // the result handler will be executed when the analysing process is done
59                 // the result contains true (it is nude) or false (it is not nude)
60                 // if the user passed an result function to the scan function, the result function will be executed
61                 resultHandler = function(result){
62                         
63                         if(resultFn){
64                                 resultFn(result);
65                         }else{
66                                 if(result)
67                                         console.log("the picture contains nudity");
68                         }
69                         
70                 }
71                 // public interface
72                 return {
73                         init: function(){
74                                 initCanvas();
75                                 // if web worker are not supported, append the noworker script
76                                 if(!!!window.Worker){
77                                         document.write(unescape("%3Cscript src='noworker.nude.js' type='text/javascript'%3E%3C/script%3E"));
78                                 }
79                                         
80                         },
81                         load: function(param){
82                                 if(typeof(param) == "string"){
83                                         loadImageById(param);
84                                 }else{
85                                         loadImageByElement(param);
86                                 }
87                         },
88                         scan: function(fn){
89                                 if(arguments.length>0 && typeof(arguments[0]) == "function"){
90                                         resultFn = fn;
91                                 }
92                                 scanImage();
93                         }
94                 };
95         })();
96         // register nude at window object
97         if(!window.nude)
98                 window.nude = nude;
99         // and initialize it
100         nude.init();
101 })();