lkarch.org/tools/hexgl/bkcore.coffee/ImageData.coffee
Lucas Kent e39465ad2f Changes to be committed:
new file:   Files/flashplayer_32_sa.exe
	new file:   favicon.ico
	new file:   globe.gif
	new file:   imgs/download.png
	new file:   imgs/zuck.jpg
	new file:   index.html
	new file:   other.ico
	new file:   script.js
	new file:   site.webmanifest
	new file:   sitemap.html
	new file:   styles/backround.css
	new file:   styles/border.css
	new file:   styles/fonts/Titillium_Web/OFL.txt
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-Black.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-Bold.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-BoldItalic.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-ExtraLight.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-ExtraLightItalic.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-Italic.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-Light.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-LightItalic.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-Regular.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-SemiBold.ttf
	new file:   styles/fonts/Titillium_Web/TitilliumWeb-SemiBoldItalic.ttf
	new file:   styles/fonts/webfontkit-20221027-163353/generator_config.txt
	new file:   styles/fonts/webfontkit-20221027-163353/specimen_files/grid_12-825-55-15.css
	new file:   styles/fonts/webfontkit-20221027-163353/specimen_files/specimen_stylesheet.css
	new file:   styles/fonts/webfontkit-20221027-163353/stylesheet.css
	new file:   styles/fonts/webfontkit-20221027-163353/titilliumweb-extralight-demo.html
	new file:   styles/fonts/webfontkit-20221027-163353/titilliumweb-extralight-webfont.woff
	new file:   styles/fonts/webfontkit-20221027-163353/titilliumweb-extralight-webfont.woff2
	new file:   styles/fonts/webfontkit-20221027-165950/generator_config.txt
	new file:   styles/fonts/webfontkit-20221027-165950/specimen_files/grid_12-825-55-15.css
	new file:   styles/fonts/webfontkit-20221027-165950/specimen_files/specimen_stylesheet.css
	new file:   styles/fonts/webfontkit-20221027-165950/stylesheet.css
	new file:   styles/fonts/webfontkit-20221027-165950/titilliumweb-bold-demo.html
	new file:   styles/fonts/webfontkit-20221027-165950/titilliumweb-bold-webfont.woff
	new file:   styles/fonts/webfontkit-20221027-165950/titilliumweb-bold-webfont.woff2
	new file:   styles/style.css
	new file:   tools/2048/.gitignore
	new file:   tools/2048/.jshintrc
	new file:   tools/2048/CONTRIBUTING.md
	new file:   tools/2048/LICENSE.txt
	new file:   tools/2048/README.md
	new file:   tools/2048/Rakefile
	new file:   tools/2048/favicon.ico
	new file:   tools/2048/index.html
	new file:   tools/2048/js/animframe_polyfill.js
	new file:   tools/2048/js/application.js
	new file:   tools/2048/js/bind_polyfill.js
	new file:   tools/2048/js/classlist_polyfill.js
	new file:   tools/2048/js/game_manager.js
	new file:   tools/2048/js/grid.js
	new file:   tools/2048/js/html_actuator.js
	new file:   tools/2048/js/keyboard_input_manager.js
	new file:   tools/2048/js/local_storage_manager.js
	new file:   tools/2048/js/tile.js
    new file:   tools/2048/meta/apple-touch-icon.png
	new file:   tools/webretro/cores/neocd_libretro.js
	new file:   tools/webretro/cores/neocd_libretro.wasm
	new file:   tools/webretro/cores/nestopia_libretro.js
	new file:   tools/webretro/cores/nestopia_libretro.wasm
	new file:   tools/webretro/cores/o2em_libretro.js
	new file:   tools/webretro/cores/o2em_libretro.wasm
	new file:   tools/webretro/cores/opera_libretro.js
	new file:   tools/webretro/cores/opera_libretro.wasm
2022-11-02 08:40:01 -04:00

141 lines
3.1 KiB
CoffeeScript

###
Loads an image and gives access to pixel data.
@class bkcore.ImageData
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
###
class ImageData
###
Creates a new ImageData object
@param path string The path of the image
@param callback function A callback function to be called
once th eimage is loaded
###
constructor: (path, callback)->
@image = new Image
@pixels = null
@canvas = null
@loaded = false
@image.onload = ()=>
@canvas = document.createElement('canvas')
@canvas.width = @image.width
@canvas.height = @image.height
context = @canvas.getContext('2d')
context.drawImage(@image, 0, 0)
@pixels = context.getImageData(0,0, @canvas.width, @canvas.height)
@loaded = true
context = null
@canvas = null
@image = null
callback?.call(@)
@image.crossOrigin = "anonymous"
@image.src = path
###
Gets pixel RGBA data at given index
@param x int In pixels
@param y int In pixels
@return Object{r,g,b,a}
###
getPixel: (x, y)->
if !@pixels? or x < 0 or y < 0 or x >= @pixels.width or y >= @pixels.height
return {r: 0, g: 0, b:0, a:0}
i = (y*@pixels.width + x) * 4
return {
r: @pixels.data[i]
g: @pixels.data[i+1]
b: @pixels.data[i+2]
a: @pixels.data[i+3]
}
###
Gets pixel RGBA data at given float index using bilinear interpolation
@param x float In subpixels
@param y float In subpixels
@return Object{r,g,b,a}
###
getPixelBilinear: (fx, fy)->
x = Math.floor(fx)
y = Math.floor(fy)
rx = fx - x - .5
ry = fy - y - .5
ax = Math.abs(rx)
ay = Math.abs(ry)
dx = if rx < 0 then -1 else 1
dy = if ry < 0 then -1 else 1
c = @getPixel(x, y)
cx = @getPixel(x+dx, y)
cy = @getPixel(x, y+dy)
cxy = @getPixel(x+dx, y+dy)
cf1 = [
(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
]
cf2 = [
(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
]
return {
r: (1-ay) * cf1[0] + ay * cf2[0]
g: (1-ay) * cf1[1] + ay * cf2[1]
b: (1-ay) * cf1[2] + ay * cf2[2]
a: (1-ay) * cf1[3] + ay * cf2[3]
}
###
Gets pixel data at given index
as 3-bytes integer (for floating-point textures erzats, from RGB values)
@param x int In pixels
@param y int In pixels
@return int (R + G*255 + B*255*255)
###
getPixelF: (x, y)->
c = @getPixel(x, y)
return c.r + c.g * 255 + c.b * 255 * 255
###
Gets pixel data at given float index using bilinear interpolationas
as 3-bytes integer (for floating-point textures erzats, from RGB values)
@param x float In subpixels
@param y float In subpixels
@return Object{r,g,b,a}
###
getPixelFBilinear: (fx, fy)->
c = @getPixelBilinear(fx, fy)
return c.r + c.g * 255 + c.b * 255 * 255
###
Exports
@package bkcore
###
exports = exports ? @
exports.bkcore ||= {}
exports.bkcore.ImageData = ImageData