class Pxlsrt::Image
Image
class for handling ChunkyPNG images.
Public Class Methods
# File lib/pxlsrt/image.rb, line 5 def initialize(png) @original = png @modified = ChunkyPNG::Image.from_canvas(png) @width, @height = png.width, png.height end
Public Instance Methods
Retrieve the color of a pixel.
# File lib/pxlsrt/image.rb, line 152 def [](x, y) return @modified[x, y] end
Set the color of a pixel.
# File lib/pxlsrt/image.rb, line 157 def []=(x, y, color) @modified[x, y] = color end
Get the column and row based on the diagonal hash created using diagonalLines.
# File lib/pxlsrt/image.rb, line 60 def diagonalColumnRow(d, i) return { "column" => ((d.to_i < 0) ? i : d.to_i + i).to_i, "row" => ((d.to_i < 0) ? d.to_i.abs + i : i).to_i } end
Retrieve a hash consisting of the diagonal lines (top left to bottom right) of the image.
# File lib/pxlsrt/image.rb, line 50 def diagonalLines return Pxlsrt::Lines.getDiagonals(self.horizontalLines.flatten(1), @width, @height) end
Retrieve the x and y coordinates of a pixel based on the hash created using the diagonalLines method and the column and row of the diagonalColumnRow method.
# File lib/pxlsrt/image.rb, line 86 def diagonalXY(d, i) cr = self.diagonalColumnRow(d, i) return { "x" => cr["column"], "y" => cr["row"] } end
# File lib/pxlsrt/image.rb, line 183 def getHeight return @height end
Retrieve Sobel value for a given pixel.
# File lib/pxlsrt/image.rb, line 104 def getSobel(x, y) if !defined?(@sobels) @grey ||= @original.grayscale @sobel_x ||= [[-1,0,1], [-2,0,2], [-1,0,1]] @sobel_y ||= [[-1,-2,-1], [ 0, 0, 0], [ 1, 2, 1]] if x != 0 and x != (@width-1) and y != 0 and y != (@height-1) t1=ChunkyPNG::Color.r(@grey[x-1,y-1]) t2=ChunkyPNG::Color.r(@grey[x,y-1]) t3=ChunkyPNG::Color.r(@grey[x+1,y-1]) t4=ChunkyPNG::Color.r(@grey[x-1,y]) t5=ChunkyPNG::Color.r(@grey[x,y]) t6=ChunkyPNG::Color.r(@grey[x+1,y]) t7=ChunkyPNG::Color.r(@grey[x-1,y+1]) t8=ChunkyPNG::Color.r(@grey[x,y+1]) t9=ChunkyPNG::Color.r(@grey[x+1,y+1]) pixel_x=(@sobel_x[0][0]*t1)+(@sobel_x[0][1]*t2)+(@sobel_x[0][2]*t3)+(@sobel_x[1][0]*t4)+(@sobel_x[1][1]*t5)+(@sobel_x[1][2]*t6)+(@sobel_x[2][0]*t7)+(@sobel_x[2][1]*t8)+(@sobel_x[2][2]*t9) pixel_y=(@sobel_y[0][0]*t1)+(@sobel_y[0][1]*t2)+(@sobel_y[0][2]*t3)+(@sobel_y[1][0]*t4)+(@sobel_y[1][1]*t5)+(@sobel_y[1][2]*t6)+(@sobel_y[2][0]*t7)+(@sobel_y[2][1]*t8)+(@sobel_y[2][2]*t9) return Math.sqrt(pixel_x * pixel_x + pixel_y * pixel_y).ceil else return 0 end else return @sobels[y * @width + x] end end
Retrieve the Sobel value and color of a pixel.
# File lib/pxlsrt/image.rb, line 144 def getSobelAndColor(x, y) return { "sobel" => self.getSobel(x, y), "color" => self[x, y] } end
Retrieve the Sobel values for every pixel and set it as @sobel.
# File lib/pxlsrt/image.rb, line 131 def getSobels if !defined?(@sobels) l = [] for xy in 0...(@width * @height) s = self.getSobel(xy % @width, (xy/@width).floor) l.push(s) end @sobels = l end return @sobels end
# File lib/pxlsrt/image.rb, line 180 def getWidth return @width end
Retrieve a multidimensional array consisting of the horizontal lines (row) of the image.
# File lib/pxlsrt/image.rb, line 12 def horizontalLines return (0...@height).inject([]) { | arr, row | arr << @modified.row(row) } end
Retrieve the x and y coordinates of a pixel based on the multidimensional array created using the horizontalLines method.
# File lib/pxlsrt/image.rb, line 23 def horizontalXY(horizontal, index) return { "x" => index.to_i, "y" => horizontal.to_i } end
# File lib/pxlsrt/image.rb, line 160 def i(i) x = i % @width y = (i / @width).floor return self[x, y] end
# File lib/pxlsrt/image.rb, line 165 def i=(i, color) x = i % @width y = (i / @width).floor self[x, y] = color end
Retrieve a hash consisting of the diagonal lines (bottom left to top right) of the image.
# File lib/pxlsrt/image.rb, line 55 def rDiagonalLines return Pxlsrt::Lines.getDiagonals(self.horizontalLines.reverse.flatten(1).reverse, @width, @height) end
Retrieve the x and y coordinates of a pixel based on the hash created using the rDiagonalLines method and the column and row of the diagonalColumnRow method.
# File lib/pxlsrt/image.rb, line 95 def rDiagonalXY(d, i) cr = self.diagonalColumnRow(d, i) return { "x" => @width - 1 - cr["column"], "y" => cr["row"] } end
Replace a diagonal line (top left to bottom right) of the image.
# File lib/pxlsrt/image.rb, line 68 def replaceDiagonal(d, arr) d = d.to_i for i in 0...arr.length xy = self.diagonalXY(d, i) self[xy["x"], xy["y"]] = arr[i] end end
Replace a horizontal line (row) of the image.
# File lib/pxlsrt/image.rb, line 17 def replaceHorizontal(y, arr) @modified.replace_row!(y, arr) return @modified end
Replace a diagonal line (bottom left to top right) of the image.
# File lib/pxlsrt/image.rb, line 77 def replaceRDiagonal(d, arr) d = d.to_i for i in 0...arr.length xy = self.rDiagonalXY(d, i) self[xy["x"], xy["y"]] = arr[i] end end
Replace a vertical line (column) of the image.
# File lib/pxlsrt/image.rb, line 36 def replaceVertical(y, arr) @modified.replace_column!(y, arr) return @modified end
Return the modified image.
# File lib/pxlsrt/image.rb, line 177 def returnModified return @modified end
Return the original, unmodified image.
# File lib/pxlsrt/image.rb, line 172 def returnOriginal return @original end
Retrieve a multidimensional array consisting of the vertical lines of the image.
# File lib/pxlsrt/image.rb, line 31 def verticalLines return (0...@width).inject([]) { | arr, column | arr << @modified.column(column) } end
Retrieve the x and y coordinates of a pixel based on the multidimensional array created using the verticalLines method.
# File lib/pxlsrt/image.rb, line 42 def verticalXY(vertical, index) return { "x" => vertical.to_i, "y" => index.to_i } end