• Jump To … +
    analyze.coffee autoload.coffee blender.coffee calculate.coffee caman.coffee convert.coffee event.coffee filter.coffee io.coffee layer.coffee logger.coffee pixelinfo.coffee plugin.coffee renderer.coffee store.coffee util.coffee blenders.coffee filters.coffee size.coffee blur.coffee camera.coffee compoundBlur.coffee edges.coffee posterize.coffee presets.coffee stackBlur.coffee threshold.coffee
  • pixelinfo.coffee

  • ¶

    This object is available inside of the process() loop, and it lets filter developers have simple access to any arbitrary pixel in the image, as well as information about the current pixel in the loop.

    class PixelInfo
      @coordinatesToLocation: (x, y, width) ->
        (y * width + x) * 4
    
      @locationToCoordinates: (loc, width) ->
        y = Math.floor(loc / (width * 4))
        x = (loc % (width * 4)) / 4
    
        return x: x, y: y
    
      constructor: (@c) -> @loc = 0
  • ¶

    Retrieves the X, Y location of the current pixel. The origin is at the bottom left corner of the image, like a normal coordinate system.

      locationXY: ->
        y = @c.dimensions.height - Math.floor(@loc / (@c.dimensions.width * 4))
        x = (@loc % (@c.dimensions.width * 4)) / 4
    
        return x: x, y: y
  • ¶

    Returns an RGBA object for a pixel whose location is specified in relation to the current pixel.

      getPixelRelative: (horiz, vert) ->
  • ¶

    We invert the vert_offset in order to make the coordinate system non-inverted. In laymans terms: -1 means down and +1 means up.

        newLoc = @loc + (@c.dimensions.width * 4 * (vert * -1)) + (4 * horiz)
    
        if newLoc > @c.pixelData.length or newLoc < 0
          return r: 0, g: 0, b: 0, a: 0
    
        return {
          r: @c.pixelData[newLoc]
          g: @c.pixelData[newLoc + 1]
          b: @c.pixelData[newLoc + 2]
          a: @c.pixelData[newLoc + 3]
        }
  • ¶

    The counterpart to getPixelRelative, this updates the value of a pixel whose location is specified in relation to the current pixel.

      putPixelRelative: (horiz, vert, rgba) ->
        nowLoc = @loc + (@c.dimensions.width * 4 * (vert * -1)) + (4 * horiz)
    
        return if newLoc > @c.pixelData.length or newLoc < 0
    
        @c.pixelData[newLoc] = rgba.r
        @c.pixelData[newLoc + 1] = rgba.g
        @c.pixelData[newLoc + 2] = rgba.b
        @c.pixelData[newLoc + 3] = rgba.a
    
        return true
  • ¶

    Gets an RGBA object for an arbitrary pixel in the canvas specified by absolute X, Y coordinates

      getPixel: (x, y) ->
        loc = @coordinatesToLocation(x, y, @width)
    
        return {
          r: @c.pixelData[loc]
          g: @c.pixelData[loc + 1]
          b: @c.pixelData[loc + 2]
          a: @c.pixelData[loc + 3]
        }
  • ¶

    Updates the pixel at the given X, Y coordinate

      putPixel: (x, y, rgba) ->
        loc = @coordinatesToLocation(x, y, @width)
    
        @c.pixelData[loc] = rgba.r
        @c.pixelData[loc + 1] = rgba.g
        @c.pixelData[loc + 2] = rgba.b
        @c.pixelData[loc + 3] = rgba.a