@quartic/bokehjs
Version:
Interactive, novel data visualization
128 lines (105 loc) • 3.82 kB
text/coffeescript
import {XYGlyph, XYGlyphView} from "./xy_glyph"
import * as p from "core/properties"
import {max, concat} from "core/util/array"
export class ImageRGBAView extends XYGlyphView
# TODO (bev) to improve. Currently, if only one image has changed, can
# pass index as "arg" to prevent full re-preocessing (useful for streaming)
_set_data: (source, arg) ->
if not ? or .length != .length
= new Array(.length)
if not ? or .length != .length
= new Array(.length)
if not ? or .length != .length
= new Array(.length)
for i in [0....length]
if arg?
if i != arg
continue
shape = []
if ?
shape = [i]
# Note specifying rows and cols is deprecated and this can soon
# be removed.
if ?
[i] = [i]
[i] = [i]
if shape.length > 0
buf = [i].buffer
else
flat = [i]
buf = new ArrayBuffer(flat.length * 4)
color = new Uint32Array(buf)
for j in [0...flat.length]
color[j] = flat[j]
else if shape.length > 0
buf = [i].buffer
[i] = shape[0]
[i] = shape[1]
else
flat = concat([i])
buf = new ArrayBuffer(flat.length * 4)
color = new Uint32Array(buf)
for j in [0...flat.length]
color[j] = flat[j]
[i] = [i].length
[i] = [i][0].length
if [i]? and [i].width == [i] and [i].height == [i]
canvas = [i]
else
canvas = document.createElement('canvas')
canvas.width = [i]
canvas.height = [i]
ctx = canvas.getContext('2d')
image_data = ctx.getImageData(0, 0, [i], [i])
buf8 = new Uint8Array(buf)
image_data.data.set(buf8)
ctx.putImageData(image_data, 0, 0)
[i] = canvas
= 0
if .units == "data"
= max()
= 0
if .units == "data"
= max()
_map_data: () ->
switch .properties.dw.units
when "data" then =
when "screen" then =
switch .properties.dh.units
when "data" then =
when "screen" then =
_render: (ctx, indices, {image_data, sx, sy, sw, sh}) ->
old_smoothing = ctx.getImageSmoothingEnabled()
ctx.setImageSmoothingEnabled(false)
for i in indices
if isNaN(sx[i]+sy[i]+sw[i]+sh[i])
continue
y_offset = sy[i]
ctx.translate(0, y_offset)
ctx.scale(1, -1)
ctx.translate(0, -y_offset)
ctx.drawImage(image_data[i], sx[i]|0, sy[i]|0, sw[i], sh[i])
ctx.translate(0, y_offset)
ctx.scale(1, -1)
ctx.translate(0, -y_offset)
ctx.setImageSmoothingEnabled(old_smoothing)
bounds: () ->
bbox = .bbox
bbox.maxX +=
bbox.maxY +=
return bbox
export class ImageRGBA extends XYGlyph
default_view: ImageRGBAView
type: 'ImageRGBA'
{
image: [ p.NumberSpec ] # TODO (bev) array spec?
rows: [ p.NumberSpec ]
cols: [ p.NumberSpec ]
dw: [ p.DistanceSpec ]
dh: [ p.DistanceSpec ]
dilate: [ p.Bool, false ]
}
initialize: (attrs, options) ->
super(attrs, options)
.rows.optional = true
.cols.optional = true