<commit_before>/**
 * Image.js
 *
 * Copyright 2012, Moxiecode Systems AB
 * Released under GPL License.
 *
 * License: http://www.plupload.com/license
 * Contributing: http://www.plupload.com/contributing
 */

// JSLint defined globals
/*global window:false, escape:false */

/*
depends on:
- mOxie.js
- Exceptions.js
- Util.js
- I18N.js
- EventTarget.js
- Runtime.js
*/

;(function(window, document, o, undefined) {
	
var x = o.Exceptions;

o.Image = (function() {
	var dispatches = ['loadstart', 'progress', 'load', 'error', 'loadend', 'resize', 'embedded'];
	
	function Image() {
		var self = this;
			
		o.RuntimeClient.call(this);
		
		o.extend(this, {
			
			uid: o.guid('uid_'),

			ruid: null,
			
			name: "",
			
			size: 0,

			width: 0,

			height: 0,
			
			type: "",

			meta: {},

			clone: function() {
				this.load.apply(this, arguments);
			},

			load: function(src, options) {
				var el, url, urlp;

				this.convertEventPropsToHandlers(dispatches);	

				if (src instanceof o.Image) {
					if (!src.size) { // only preloaded image objects can be used as source
						throw new x.DOMException(x.DOMException.INVALID_STATE_ERR);	
					}
					_loadFromImage.apply(this, arguments);
				} 
				else if (src instanceof o.File || src instanceof o.Blob) {
					_loadFromBlob.apply(this, arguments);
				} 
				else if (o.typeOf(src) === 'string' && /^http:\/\//.test(src)) {
					_loadFromUrl.apply(this, arguments);
				} 
				else if (el = o(src) && el.nodeName === 'img') {
					urlp = o.parseUrl(el.src); // src can be relative

					// manually resolve the url
					url = urlp.scheme + '://' + urlp.host + (urlp.port !== 80 ? ':' + urlp.port : '') + urlp.path;

					_loadFromUrl.call(this, url, options);
				} 
				else {
					throw new x.DOMException(x.DOMException.TYPE_MISMATCH_ERR);	
				}
			},

			resize: function(width, height, crop) {
				var runtime;

				if (!this.size) { // only preloaded image objects can be used as source
					throw new x.DOMException(x.DOMException.INVALID_STATE_ERR);	
				}

				if (!width) {
					throw new x.DOMException(x.DOMException.SYNTAX_ERR);	
				}

				if (!height) {
					height = width;
				}

				runtime = this.connectRuntime(this.ruid);
				self.bind('Resize', function(e, info) {
					_updateInfo.call(this, info);
				}, 999);
				runtime.exec.call(self, 'Image', 'resize', width, height, (crop === undefined ? false : crop));
			},

			crop: function(width, height) {
				this.resize(width, height, true);
			},

			getAsCanvas: function() {
				if (!o.ua.can('create_canvas')) {
					throw new x.RuntimeError(x.RuntimeError.NOT_SUPPORTED_ERR);	
				}

				var runtime = this.connectRuntime(this.ruid);
				return runtime.exec.call(self, 'Image', 'getAsCanvas');
			},

			getAsImage: function() {
				var runtime = this.connectRuntime(this.ruid);
				return runtime.exec.call(self, 'Image', 'getAsImage');
			},

			getAsBlob: function(type, quality) {
				if (!type) {
					type = 'image/jpeg';
				}

				if (type === 'image/jpeg' && !quality) {
					quality = 90;
				}

				if (!this.ruid) {
					throw new x.RuntimeError(x.RuntimeError.NOT_INIT_ERR);	
				}

				runtime = this.connectRuntime(this.ruid);
				return runtime.exec.call(self, 'Image', 'getAsBlob', type, quality);
			},

			getAsDataURL: function(type, quality) {
				var runtime = this.connectRuntime(this.ruid);
				return runtime.exec.call(self, 'Image', 'getAsDataURL', type, quality);
			},


			getAsBinaryString: function(type, quality) {
				var blob, frs;
				
				blob = this.getAsBlob(type, quality);
				
				frs = new o.FileReaderSync;
				return frs.readAsBinaryString(blob);
			},

			embed: function(el) {<commit_msg>Image: Throw DOMException.INVALID_STATE_ERR when contents are requested on an empty image.<commit_after>/**
 * Image.js
 *
 * Copyright 2012, Moxiecode Systems AB
 * Released under GPL License.
 *
 * License: http://www.plupload.com/license
 * Contributing: http://www.plupload.com/contributing
 */

// JSLint defined globals
/*global window:false, escape:false */

/*
depends on:
- mOxie.js
- Exceptions.js
- Util.js
- I18N.js
- EventTarget.js
- Runtime.js
*/

;(function(window, document, o, undefined) {
	
var x = o.Exceptions;

o.Image = (function() {
	var dispatches = ['loadstart', 'progress', 'load', 'error', 'loadend', 'resize', 'embedded'];
	
	function Image() {
		var self = this;
			
		o.RuntimeClient.call(this);
		
		o.extend(this, {
			
			uid: o.guid('uid_'),

			ruid: null,
			
			name: "",
			
			size: 0,

			width: 0,

			height: 0,
			
			type: "",

			meta: {},

			clone: function() {
				this.load.apply(this, arguments);
			},

			load: function(src, options) {
				var el, url, urlp;

				this.convertEventPropsToHandlers(dispatches);	

				if (src instanceof o.Image) {
					if (!src.size) { // only preloaded image objects can be used as source
						throw new x.DOMException(x.DOMException.INVALID_STATE_ERR);	
					}
					_loadFromImage.apply(this, arguments);
				} 
				else if (src instanceof o.File || src instanceof o.Blob) {
					_loadFromBlob.apply(this, arguments);
				} 
				else if (o.typeOf(src) === 'string' && /^http:\/\//.test(src)) {
					_loadFromUrl.apply(this, arguments);
				} 
				else if (el = o(src) && el.nodeName === 'img') {
					urlp = o.parseUrl(el.src); // src can be relative

					// manually resolve the url
					url = urlp.scheme + '://' + urlp.host + (urlp.port !== 80 ? ':' + urlp.port : '') + urlp.path;

					_loadFromUrl.call(this, url, options);
				} 
				else {
					throw new x.DOMException(x.DOMException.TYPE_MISMATCH_ERR);	
				}
			},

			resize: function(width, height, crop) {
				var runtime;

				if (!this.size) { // only preloaded image objects can be used as source
					throw new x.DOMException(x.DOMException.INVALID_STATE_ERR);	
				}

				if (!width) {
					throw new x.DOMException(x.DOMException.SYNTAX_ERR);	
				}

				if (!height) {
					height = width;
				}

				runtime = this.connectRuntime(this.ruid);
				self.bind('Resize', function(e, info) {
					_updateInfo.call(this, info);
				}, 999);
				runtime.exec.call(self, 'Image', 'resize', width, height, (crop === undefined ? false : crop));
			},

			crop: function(width, height) {
				this.resize(width, height, true);
			},

			getAsCanvas: function() {
				if (!o.ua.can('create_canvas')) {
					throw new x.RuntimeError(x.RuntimeError.NOT_SUPPORTED_ERR);	
				}

				var runtime = this.connectRuntime(this.ruid);
				return runtime.exec.call(self, 'Image', 'getAsCanvas');
			},

			getAsImage: function() {
				var runtime = this.connectRuntime(this.ruid);
				return runtime.exec.call(self, 'Image', 'getAsImage');
			},

			getAsBlob: function(type, quality) {
				if (!this.size) {
					throw new x.DOMException(x.DOMException.INVALID_STATE_ERR);	
				}

				if (!type) {
					type = 'image/jpeg';
				}

				if (type === 'image/jpeg' && !quality) {
					quality = 90;
				}

				return this.connectRuntime(this.ruid).exec.call(self, 'Image', 'getAsBlob', type, quality);
			},

			getAsDataURL: function(type, quality) {
				if (!this.size) {
					throw new x.DOMException(x.DOMException.INVALID_STATE_ERR);	
				}
				return this.connectRuntime(this.ruid).exec.call(self, 'Image', 'getAsDataURL', type, quality);
			},


			getAsBinaryString: function(type, quality) {
				var blob, frs;
				blob = this.getAsBlob(type, quality);
				frs = new o.FileReaderSync;
				return frs.readAsBinaryString(blob);
			},

			embed: function(el) {
{'fraction_non_alphanumeric': 0.15605776945707409, 'fraction_numerical': 0.004279219042524739, 'mean_word_length': 2.849202264539372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4012460', 'n_tokens_mistral': 3228, 'n_tokens_neox': 2541, 'n_words': 560}