Path: blob/main/projects/HexGL/bkcore.coffee/ImageData.js
4626 views
// Generated by CoffeeScript 1.4.012/*3Loads an image and gives access to pixel data.45@class bkcore.ImageData6@author Thibaut 'BKcore' Despoulain <http://bkcore.com>7*/8910(function() {11var ImageData, exports;1213ImageData = (function() {14/*15Creates a new ImageData object1617@param path string The path of the image18@param callback function A callback function to be called19once th eimage is loaded20*/2122function ImageData(path, callback) {23var _this = this;24this.image = new Image;25this.pixels = null;26this.canvas = null;27this.loaded = false;28this.image.onload = function() {29var context;30_this.canvas = document.createElement('canvas');31_this.canvas.width = _this.image.width;32_this.canvas.height = _this.image.height;33context = _this.canvas.getContext('2d');34context.drawImage(_this.image, 0, 0);35_this.pixels = context.getImageData(0, 0, _this.canvas.width, _this.canvas.height);36_this.loaded = true;37context = null;38_this.canvas = null;39_this.image = null;40return callback != null ? callback.call(_this) : void 0;41};42this.image.crossOrigin = "anonymous";43this.image.src = path;44}4546/*47Gets pixel RGBA data at given index4849@param x int In pixels50@param y int In pixels51@return Object{r,g,b,a}52*/535455ImageData.prototype.getPixel = function(x, y) {56var i;57if (!(this.pixels != null) || x < 0 || y < 0 || x >= this.pixels.width || y >= this.pixels.height) {58return {59r: 0,60g: 0,61b: 0,62a: 063};64}65i = (y * this.pixels.width + x) * 4;66return {67r: this.pixels.data[i],68g: this.pixels.data[i + 1],69b: this.pixels.data[i + 2],70a: this.pixels.data[i + 3]71};72};7374/*75Gets pixel RGBA data at given float index using bilinear interpolation7677@param x float In subpixels78@param y float In subpixels79@return Object{r,g,b,a}80*/818283ImageData.prototype.getPixelBilinear = function(fx, fy) {84var ax, ay, c, cf1, cf2, cx, cxy, cy, dx, dy, rx, ry, x, y;85x = Math.floor(fx);86y = Math.floor(fy);87rx = fx - x - .5;88ry = fy - y - .5;89ax = Math.abs(rx);90ay = Math.abs(ry);91dx = rx < 0 ? -1 : 1;92dy = ry < 0 ? -1 : 1;93c = this.getPixel(x, y);94cx = this.getPixel(x + dx, y);95cy = this.getPixel(x, y + dy);96cxy = this.getPixel(x + dx, y + dy);97cf1 = [(1 - ax) * c.r + ax * cx.r, (1 - ax) * c.g + ax * cx.g, (1 - ax) * c.b + ax * cx.b, (1 - ax) * c.a + ax * cx.a];98cf2 = [(1 - ax) * cy.r + ax * cxy.r, (1 - ax) * cy.g + ax * cxy.g, (1 - ax) * cy.b + ax * cxy.b, (1 - ax) * cy.a + ax * cxy.a];99return {100r: (1 - ay) * cf1[0] + ay * cf2[0],101g: (1 - ay) * cf1[1] + ay * cf2[1],102b: (1 - ay) * cf1[2] + ay * cf2[2],103a: (1 - ay) * cf1[3] + ay * cf2[3]104};105};106107/*108Gets pixel data at given index109as 3-bytes integer (for floating-point textures erzats, from RGB values)110111@param x int In pixels112@param y int In pixels113@return int (R + G*255 + B*255*255)114*/115116117ImageData.prototype.getPixelF = function(x, y) {118var c;119c = this.getPixel(x, y);120return c.r + c.g * 255 + c.b * 255 * 255;121};122123/*124Gets pixel data at given float index using bilinear interpolationas125as 3-bytes integer (for floating-point textures erzats, from RGB values)126127@param x float In subpixels128@param y float In subpixels129@return Object{r,g,b,a}130*/131132133ImageData.prototype.getPixelFBilinear = function(fx, fy) {134var c;135c = this.getPixelBilinear(fx, fy);136return c.r + c.g * 255 + c.b * 255 * 255;137};138139return ImageData;140141})();142143/*144Exports145@package bkcore146*/147148149exports = exports != null ? exports : this;150151exports.bkcore || (exports.bkcore = {});152153exports.bkcore.ImageData = ImageData;154155}).call(this);156157158