Class: Pxlsrt::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/pxlsrt/image.rb

Overview

Image class for handling ChunkyPNG images.

Instance Method Summary collapse

Constructor Details

#initialize(png) ⇒ Image

Returns a new instance of Image.



5
6
7
8
9
# 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

Instance Method Details

#[](x, y) ⇒ Object

Retrieve the color of a pixel.



152
153
154
# File 'lib/pxlsrt/image.rb', line 152

def [](x, y)
	return @modified[x, y]
end

#[]=(x, y, color) ⇒ Object

Set the color of a pixel.



157
158
159
# File 'lib/pxlsrt/image.rb', line 157

def []=(x, y, color)
	@modified[x, y] = color
end

#diagonalColumnRow(d, i) ⇒ Object

Get the column and row based on the diagonal hash created using diagonalLines.



60
61
62
63
64
65
# 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

#diagonalLinesObject

Retrieve a hash consisting of the diagonal lines (top left to bottom right) of the image.



50
51
52
# File 'lib/pxlsrt/image.rb', line 50

def diagonalLines
	return Pxlsrt::Lines.getDiagonals(self.horizontalLines.flatten(1), @width, @height)
end

#diagonalXY(d, i) ⇒ Object

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.



86
87
88
89
90
91
92
# File 'lib/pxlsrt/image.rb', line 86

def diagonalXY(d, i)
	cr = self.diagonalColumnRow(d, i)
	return {
		"x" => cr["column"],
		"y" => cr["row"]
	}
end

#getHeightObject



183
184
185
# File 'lib/pxlsrt/image.rb', line 183

def getHeight
	return @height
end

#getSobel(x, y) ⇒ Object

Retrieve Sobel value for a given pixel.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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

#getSobelAndColor(x, y) ⇒ Object

Retrieve the Sobel value and color of a pixel.



144
145
146
147
148
149
# File 'lib/pxlsrt/image.rb', line 144

def getSobelAndColor(x, y)
	return {
		"sobel" => self.getSobel(x, y),
		"color" => self[x, y]
	}
end

#getSobelsObject

Retrieve the Sobel values for every pixel and set it as @sobel.



131
132
133
134
135
136
137
138
139
140
141
# 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

#getWidthObject



180
181
182
# File 'lib/pxlsrt/image.rb', line 180

def getWidth
	return @width
end

#horizontalLinesObject

Retrieve a multidimensional array consisting of the horizontal lines (row) of the image.



12
13
14
# File 'lib/pxlsrt/image.rb', line 12

def horizontalLines
	return (0...@height).inject([]) { | arr, row | arr << @modified.row(row) }
end

#horizontalXY(horizontal, index) ⇒ Object

Retrieve the x and y coordinates of a pixel based on the multidimensional array created using the horizontalLines method.



23
24
25
26
27
28
# File 'lib/pxlsrt/image.rb', line 23

def horizontalXY(horizontal, index)
	return {
		"x" => index.to_i,
		"y" => horizontal.to_i
	}
end

#i(i) ⇒ Object



160
161
162
163
164
# File 'lib/pxlsrt/image.rb', line 160

def i(i)
	x = i % @width
	y = (i / @width).floor
	return self[x, y]
end

#i=(i, color) ⇒ Object



165
166
167
168
169
# File 'lib/pxlsrt/image.rb', line 165

def i=(i, color)
	x = i % @width
	y = (i / @width).floor
	self[x, y] = color
end

#rDiagonalLinesObject

Retrieve a hash consisting of the diagonal lines (bottom left to top right) of the image.



55
56
57
# File 'lib/pxlsrt/image.rb', line 55

def rDiagonalLines
	return Pxlsrt::Lines.getDiagonals(self.horizontalLines.reverse.flatten(1).reverse, @width, @height)
end

#rDiagonalXY(d, i) ⇒ Object

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.



95
96
97
98
99
100
101
# 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

#replaceDiagonal(d, arr) ⇒ Object

Replace a diagonal line (top left to bottom right) of the image.



68
69
70
71
72
73
74
# 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

#replaceHorizontal(y, arr) ⇒ Object

Replace a horizontal line (row) of the image.



17
18
19
20
# File 'lib/pxlsrt/image.rb', line 17

def replaceHorizontal(y, arr)
	@modified.replace_row!(y, arr)
	return @modified
end

#replaceRDiagonal(d, arr) ⇒ Object

Replace a diagonal line (bottom left to top right) of the image.



77
78
79
80
81
82
83
# 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

#replaceVertical(y, arr) ⇒ Object

Replace a vertical line (column) of the image.



36
37
38
39
# File 'lib/pxlsrt/image.rb', line 36

def replaceVertical(y, arr)
	@modified.replace_column!(y, arr)
	return @modified
end

#returnModifiedObject

Return the modified image.



177
178
179
# File 'lib/pxlsrt/image.rb', line 177

def returnModified
	return @modified
end

#returnOriginalObject

Return the original, unmodified image.



172
173
174
# File 'lib/pxlsrt/image.rb', line 172

def returnOriginal
	return @original
end

#verticalLinesObject

Retrieve a multidimensional array consisting of the vertical lines of the image.



31
32
33
# File 'lib/pxlsrt/image.rb', line 31

def verticalLines
	return (0...@width).inject([]) { | arr, column | arr << @modified.column(column) }
end

#verticalXY(vertical, index) ⇒ Object

Retrieve the x and y coordinates of a pixel based on the multidimensional array created using the verticalLines method.



42
43
44
45
46
47
# File 'lib/pxlsrt/image.rb', line 42

def verticalXY(vertical, index)
	return {
		"x" => vertical.to_i,
		"y" => index.to_i
	}
end