<commit_before>_ = require "underscore"

Glyph = require "../glyphs/glyph"
LinearColorMapper = require "../mappers/linear_color_mapper"
p = require "../../core/properties"
{Greys9} = require '../../palettes/palettes'

class ImageView extends Glyph.View

  _index_data: () ->
    @_xy_index()

  _set_data: () ->
    if not @image_data? or @image_data.length != @_image.length
      @image_data = new Array(@_image.length)

    if not @_width? or @_width.length != @_image.length
      @_width = new Array(@_image.length)

    if not @_height? or @_height.length != @_image.length
      @_height = new Array(@_image.length)

    for i in [0...@_image.length]
      if @_rows?
        @_height[i] = @_rows[i]
        @_width[i] = @_cols[i]
      else
        @_height[i] = @_image[i].length
        @_width[i] = @_image[i][0].length
      canvas = document.createElement('canvas')
      canvas.width = @_width[i]
      canvas.height = @_height[i]
      ctx = canvas.getContext('2d')
      image_data = ctx.getImageData(0, 0, @_width[i], @_height[i])
      cmap = @mget('color_mapper')
      if @_rows?
        img = @_image[i]
      else
        img = _.flatten(@_image[i])
      buf = cmap.v_map_screen(img)
      buf8 = new Uint8ClampedArray(buf)
      image_data.data.set(buf8)
      ctx.putImageData(image_data, 0, 0)
      @image_data[i] = canvas

      @max_dw = 0
      if @_dw.units == "data"
        @max_dw = _.max(@_dw)
      @max_dh = 0
      if @_dh.units == "data"
        @max_dh = _.max(@_dh)
      @_xy_index()

  _map_data: () ->
    @sw = @sdist(@renderer.xmapper, @_x, @_dw, 'edge', @mget('dilate'))
    @sh = @sdist(@renderer.ymapper, @_y, @_dh, 'edge', @mget('dilate'))

  _render: (ctx, indices, {image_data, sx, sy, sw, sh}) ->
    old_smoothing = ctx.getImageSmoothingEnabled()
    ctx.setImageSmoothingEnabled(false)

    for i in indices
      if not image_data[i]?
        continue
      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: () ->
    bb = @index.data.bbox
    return [
      [bb[0], bb[2]+@max_dw],
      [bb[1], bb[3]+@max_dh]
    ]

class Image extends Glyph.Model
  default_view: ImageView

  type: 'Image'

  @coords [['x', 'y']]
  @mixins []
  @define {
      image:        [ p.NumberSpec       ] # TODO (bev) array spec?
      dw:           [ p.NumberSpec       ]
      dh:           [ p.NumberSpec       ]
      dilate:       [ p.Bool,      false ]
      color_mapper: [ p.Instance,  new LinearColorMapper.Model(palette: Greys9) ]
  }

  initialize: (attrs, options) ->
    super(attrs, options)
    @properties.rows.optional = true
    @properties.cols.optional = true


module.exports =
  Model: Image
  View: ImageView
<commit_msg>Remove initializer from Image glyph<commit_after>_ = require "underscore"

Glyph = require "../glyphs/glyph"
LinearColorMapper = require "../mappers/linear_color_mapper"
p = require "../../core/properties"
{Greys9} = require '../../palettes/palettes'

class ImageView extends Glyph.View

  _index_data: () ->
    @_xy_index()

  _set_data: () ->
    if not @image_data? or @image_data.length != @_image.length
      @image_data = new Array(@_image.length)

    if not @_width? or @_width.length != @_image.length
      @_width = new Array(@_image.length)

    if not @_height? or @_height.length != @_image.length
      @_height = new Array(@_image.length)

    for i in [0...@_image.length]
      if @_rows?
        @_height[i] = @_rows[i]
        @_width[i] = @_cols[i]
      else
        @_height[i] = @_image[i].length
        @_width[i] = @_image[i][0].length
      canvas = document.createElement('canvas')
      canvas.width = @_width[i]
      canvas.height = @_height[i]
      ctx = canvas.getContext('2d')
      image_data = ctx.getImageData(0, 0, @_width[i], @_height[i])
      cmap = @mget('color_mapper')
      if @_rows?
        img = @_image[i]
      else
        img = _.flatten(@_image[i])
      buf = cmap.v_map_screen(img)
      buf8 = new Uint8ClampedArray(buf)
      image_data.data.set(buf8)
      ctx.putImageData(image_data, 0, 0)
      @image_data[i] = canvas

      @max_dw = 0
      if @_dw.units == "data"
        @max_dw = _.max(@_dw)
      @max_dh = 0
      if @_dh.units == "data"
        @max_dh = _.max(@_dh)
      @_xy_index()

  _map_data: () ->
    @sw = @sdist(@renderer.xmapper, @_x, @_dw, 'edge', @mget('dilate'))
    @sh = @sdist(@renderer.ymapper, @_y, @_dh, 'edge', @mget('dilate'))

  _render: (ctx, indices, {image_data, sx, sy, sw, sh}) ->
    old_smoothing = ctx.getImageSmoothingEnabled()
    ctx.setImageSmoothingEnabled(false)

    for i in indices
      if not image_data[i]?
        continue
      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: () ->
    bb = @index.data.bbox
    return [
      [bb[0], bb[2]+@max_dw],
      [bb[1], bb[3]+@max_dh]
    ]

class Image extends Glyph.Model
  default_view: ImageView

  type: 'Image'

  @coords [['x', 'y']]
  @mixins []
  @define {
      image:        [ p.NumberSpec       ] # TODO (bev) array spec?
      dw:           [ p.NumberSpec       ]
      dh:           [ p.NumberSpec       ]
      dilate:       [ p.Bool,      false ]
      color_mapper: [ p.Instance,  new LinearColorMapper.Model(palette: Greys9) ]
  }

module.exports =
  Model: Image
  View: ImageView

{'fraction_non_alphanumeric': 0.15945214744673655, 'fraction_numerical': 0.009469056476158269, 'mean_word_length': 2.4631147540983607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2392658', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2213, 'n_words': 435}