View difference between Paste ID: RFPK8BWf and 7XAmsAbX
SHOW: | | - or go back to the newest paste.
1
--[[
2
 paintutils extra functions
3
 dis is a fuckin' beta you twat
4
 gonna add:
5
	rotation
6
	pixelation
7
8
 pastebin get 7XAmsAbX pue 
9
 pastebin get RFPK8BWf puebeta 
10
11
list of extra functions:
12
 paintutils.flipY(table image) 
13
	returns 'image' stretched vertically
14
 paintutils.flipX(table image)
15
	returns 'image' stretched horizontally
16
 paintutils.grayOut(table image)
17
	converts 'image' to grayscale
18
 paintutils.lighten(table image)
19
	returns 'image' but with brighter colors, suitable for transitions
20
 paintutils.darken(table image)
21
	returns 'image' but with darker colors, suitable for transitions
22
 paintutils.getSize(table image)
23
	returns the x and y sizes of 'image'
24
 paintutils.stretchImage(table image, number stretchedX, number stretchedY)
25
	returns 'image', but stretched to fit sizes 'stretchedX' and 'stretchedY'.
26
 paintutils.merge(table {image1,x,y}, table {image2,x,y}, ...)
27
	returns an image composed of image1 layered upon image2 layered upon image3 and so on.
28
 paintutils.fullScreen(number color, terminal object)
29
	returns a fullscreen picture made solidly of 'color'. sets size to current terminal or 'object' if defined.
30
 paintutils.unloadImage(table image)
31
	returns a string NFP picture made from 'image'.
32
 paintutils.autocrop(table image)
33
	returns 'image' but with the blank space cut off from the left and top.
34
 paintutils.drawImageBlit(table image, number x, number y)
35
	draws 'image' at 'x' and 'y' using term.blit. As such, it does not support transparency. You will want to merge images together before rendering.
36
 paintutils.drawImageBlitCenter(table image, number x, number y, terminal object)
37
 	draws 'image' centered around 'x' and 'y'. if 'x' and 'y' aren't defined, they default to half the screen width and height respectively. 'object' can be defined to use its screen size instead.
38
 paintutils.drawImageCenter(table image, number x, number y, terminal object)
39
	same as drawImageBlitCenter, but with the normal paintutils.drawImage. 'object' can be defined to use its screen size instead.
40
 paintutils.centerWithBlankSpace(table image, number x, number y, terminal object)
41
	returns 'image', but with extra blank space to center it to 'x' and 'y'. if 'x' and 'y' aren't defined, they default to half the screen width and height respectively. 'object' can be defined to use its screen size instead.
42
--]]
43
44
local round = function(x) return x + 0.5 - (x + 0.5) % 1 end
45
local deepCopy = function(tbl) return {table.unpack(tbl)} end
46
local bcol, ccol = {
47
[0]=" ",[colors.white]="0",[colors.orange]="1",[colors.magenta]="2",[colors.lightBlue]="3",[colors.yellow]="4",[colors.lime]="5",[colors.pink]="6",[colors.gray]="7",[colors.lightGray]="8",[colors.cyan]="9",[colors.purple]="a",[colors.blue]="b",[colors.brown]="c",[colors.green]="d",[colors.red]="e",[colors.black]="f"}, {}
48
for k,v in pairs(bcol) do ccol[v] = k end
49
50
paintutils.flipY = function(image)
51
	local output = {}
52
	for y = #image, 1, -1 do output[#output+1] = image[y] end
53
	return output
54
end
55
56
paintutils.flipX = function(image)
57
	local output,sizeX = {}, 0
58
	for y = 1, #image do sizeX = math.max(sizeX,#image[y]) end
59
	for y = 1, #image do
60
		output[y] = {}
61
		for x = 1, sizeX do output[y][x] = deepCopy(image)[y][sizeX-(x-1)] or 0 end
62
	end
63
	return output
64
end
65
66
paintutils.grayOut = function(image)
67
	local output,grays = {},{[0] = 0,[colors.white]=colors.white,[colors.orange]=colors.lightGray,[colors.magenta]=colors.lightGray,[colors.lightBlue]=colors.lightGray,[colors.yellow]=colors.white,[colors.lime]=colors.lightGray,[colors.pink]=colors.lightGray,[colors.gray]=colors.gray,[colors.lightGray]=colors.lightGray,[colors.cyan]=colors.lightGray,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.gray,[colors.green]=colors.lightGray,[colors.red]=colors.gray,[colors.black]=colors.black}
68
	for y = 1, #image do
69
		output[y] = {}
70
		for x = 1, #image[y] do output[y][x] = grays[image[y][x]] or image[y][x] end
71
	end
72
	return output
73
end
74
75
paintutils.lighten = function(image)
76
	local output,lighters = {},{[0] = 0,[colors.white] = colors.white,[colors.orange] = colors.yellow,[colors.magenta] = colors.pink,[colors.lightBlue] = colors.white,[colors.yellow] = colors.white,[colors.lime] = colors.white,[colors.pink] = colors.white,[colors.gray] = colors.lightGray,[colors.lightGray] = colors.white,[colors.cyan] = colors.lightBlue,[colors.purple] = colors.magenta,[colors.blue] = colors.lightBlue,[colors.brown] = colors.orange,[colors.green] = colors.lime,[colors.red] = colors.magenta,[colors.black] = colors.gray}
77
	for y = 1, #image do
78
		output[y] = {}
79
		for x = 1, #image[y] do output[y][x] = lighters[image[y][x]] or image[y][x] end
80
	end
81
	return output
82
end
83
84
paintutils.darken = function(image)
85
	local output, darkers = {},{[0]=0,[colors.white]=colors.lightGray,[colors.orange]=colors.brown,[colors.magenta]=colors.purple,[colors.lightBlue]=colors.cyan,[colors.yellow]=colors.orange,[colors.lime]=colors.green,[colors.pink]=colors.magenta,[colors.gray]=colors.black,[colors.lightGray]=colors.gray,[colors.cyan]=colors.blue,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.black,[colors.green]=colors.gray,[colors.red]=colors.gray,[colors.black]=colors.black}
86
	for y = 1, #image do
87
		output[y] = {}
88
		for x = 1, #image[y] do output[y][x] = darkers[image[y][x]] or image[y][x] end
89
	end
90
	return output
91
end
92
93
paintutils.getSize = function(image)
94
	local xsize = 0
95
	if type(image) ~= "table" then return 0,0 end
96
	for y = 1, #image do xsize = math.max(xsize,#image[y]) end
97
	return xsize, #image
98
end
99
100
paintutils.stretchImage = function(_image,sx,sy)
101
	local image,output = deepCopy(_image), {}
102
	if sx == 0 or sy == 0 then return {{}} end
103
	if sx < 0 then image = paintutils.flipX(image) end
104
	if sy < 0 then image = paintutils.flipY(image) end
105
	sx,sy = round(math.abs(sx)), round(math.abs(sy))
106
	if sx == 0 or sy == 0 then return {{}} end
107
	local imX,imY = paintutils.getSize(image)
108
	local xcur,ycur
109
	for y = 1, sy do
110
		output[y] = {}
111
		for x = 1, sx do
112
			output[y][x] = image[math.max(1,round((y/sy)*imY))][math.max(1,round((x/sx)*imX))] or nil
113
		end
114
	end
115
	return output
116
end
117
118
paintutils.drawImageBlit = function(image,ix,iy,object)
119
	if (type(image) ~= "table") or (type(ix) ~= "number") or (type(iy) ~= "number") then
120
		error("Expected image, x, y", 2)
121
	else
122
		object = object or term
123
		ix, iy = round(ix), round(iy)
124
		local buff
125
		for y = 1, #image do
126
			buff = ""
127
			for x = 1, #image[y] do
128
				buff = buff..bcol[image[y][x]]
129
			end
130
			object.setCursorPos(ix,iy+(y-1))
131
			object.blit(buff,buff,buff)
132
		end
133
	end
134
end
135
136
paintutils.drawImage = function(image,ix,iy,object)
137
	if (type(image) ~= "table") or (type(ix) ~= "number") or (type(iy) ~= "number") then
138
		error("Expected image, x, y", 2)
139
	else
140
		object = object or term
141
		for y = 1, #image do
142
			object.setCursorPos(ix,iy+(y-1))
143
			for x = 1, #image[y] do
144
				if image[y][x] == 0 then
145
					object.setCursorPos(ix+x,iy+(y-1))
146
				else
147
					object.blit(" ","0",bcol[image[y][x]])
148
					if x == #image[y] and y == #image then --gotta have that backwards compatibility
149
						object.setBackgroundColor(image[y][x])
150
					end
151
				end
152
			end
153
		end
154
	end
155
end
156
157
paintutils.centerWithBlankSpace = function(_image,ix,iy,object)
158
	local image,scX,scY = deepCopy(_image)
159
	if object then scX,scY = object.getSize() else scX,scY = term.getSize() end
160
	local imgXsize,imgYsize = paintutils.getSize(image)
161
	if imgXsize == 0 or imgYsize == 0 then return {{}} end
162
	local incX,incY = math.floor((ix or (scX/2)) - (imgXsize/2)), math.floor((iy or (scY/2)) - (imgYsize/2))
163
	local output = {}
164
	for y = 1, imgYsize + incY do
165
		output[y] = {}
166
		for x = 1, imgXsize + incX do
167
			if (x > incX) and (y > incY) then
168
				output[y][x] = image[y-incY][x-incX] or 0
169
			else
170
				output[y][x] = 0
171
			end
172
		end
173
	end
174
	return output
175
end
176
177
paintutils.drawImageBlitCenter = function(image,ix,iy,object)
178
	local scX,scY
179
	object = object or term
180
	if object then scX,scY = object.getSize() else scX,scY = term.getSize() end
181
	local imgXsize,imgYsize = paintutils.getSize(image)
182
	return paintutils.drawImageBlit(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2), object)
183
end
184
185
paintutils.drawImageCenter = function(image,ix,iy,object)
186
	local scX,scY
187
	object = object or term
188
	scX,scY = object.getSize()
189
	local imgXsize,imgYsize = paintutils.getSize(image)
190
	return paintutils.drawImage(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2), object)
191
end
192
193
paintutils.merge = function(...)
194
	local output,arg,imgXsize,imgYsize,image,xdiff,ydiff = {}, table.pack(...), 0, 0
195
	for a = 1, #arg do
196
		local x,y = paintutils.getSize(arg[a][1])
197
		if not (x == 0 or y == 0) then
198
			x, y = x+arg[a][2], y+arg[a][3]
199
			imgXsize = math.max(imgXsize,x)
200
			imgYsize = math.max(imgYsize,y)
201
		end
202
	end
203
	for a = #arg, 1, -1 do
204
		image,xdiff,ydiff = table.unpack(arg[a])
205
		xdiff, ydiff = xdiff - 1, ydiff - 1
206
		for y = 1, imgYsize do
207
			output[y] = output[y] or {}
208
			for x = 1, imgXsize do
209
				output[y][x] = output[y][x] or 0
210
				if image[y-ydiff] then
211
					if image[y-ydiff][x-xdiff] and (image[y-ydiff][x-xdiff] ~= 0) then
212
						output[y][x] = image[y-ydiff][x-xdiff]
213
					end
214
				end
215
			end
216
		end
217
	end
218
	return output
219
end
220
221
paintutils.fullScreen = function(bgcol,object)
222
	local scr_x,scr_y = (object or term).getSize()
223
	local output,l = {},{}
224
	bgcol = bgcol or colors.black
225
	for x = 1, scr_x do l[x] = bgcol end
226
	for y = 1, scr_y do output[y] = l end
227
	return output
228
end
229
230
paintutils.unloadImage = function(image)
231
	local output = ""
232
	for y = 1, #image do
233
		for x = 1, #image[y] do output = output..bcol[image[y][x]] end
234
		if y < #image then output = output.."\n" end
235
	end
236
	return output
237
end
238
239
paintutils.autocrop = function(image)
240
	local output,top,leftings = {},1,{}
241
	local isTopped = false
242
	for a = 1, #image do
243
		if (not isTopped) and (#image[a] > 0) then
244
			top = a
245
			isTopped = true
246
		end
247
		if isTopped then
248
			output[#output+1] = deepCopy(image[a])
249
		end
250
	end
251
	for y = 1, #output do
252
		leftings[y] = 0
253
		for x = 1, #output[y] do
254
			if (output[y][x] == 0) or (output[y][x] == nil) then
255
				leftings[y] = leftings[y] + 1
256
			else
257
				break
258
			end
259
		end
260
	end
261
	local left = math.min(table.unpack(leftings))
262
	for y = 1, #output do
263
		for l = 1, left do table.remove(output[y],1) end
264
	end
265
	return output
266
end
267
268
paintutils.rotateImage = function(image,degrees,centerX,centerY)
269
	local imgXsize, imgYsize = paintutils.getSize(image)
270
	centerX, centerY = centerX or imgXsize/2, centerY or imgYsize/2
271
	degrees = math.rad(degrees)
272
	local rotatePoint = function(x,y,degrees,centerX,centerY)
273
		return ((x-centerX)*math.cos(degrees)) + ((y-centerY)*math.sin(degrees)), ((y-centerY)*math.cos(degrees)) - ((x-centerX)*math.sin(degrees))
274
	end
275
	local output = {}
276
	local newXsize, newYsize = 0, 0
277
	local minXsize, minYsize = 1, 1
278
	local points = {
279
		{rotatePoint(1,			1,			degrees,centerX,centerY)},
280
		{rotatePoint(imgXsize,	1,			degrees,centerX,centerY)},
281
		{rotatePoint(1,			imgYsize,	degrees,centerX,centerY)},
282
		{rotatePoint(imgXsize,	imgYsize,	degrees,centerX,centerY)}
283
	}
284
	for a = 1, #points do
285
		newXsize = math.ceil( math.max(newXsize,points[a][1]))
286
		newYsize = math.ceil( math.max(newYsize,points[a][2]))
287
		minXsize = math.floor(math.min(minXsize,points[a][1]))
288
		minYsize = math.floor(math.min(minYsize,points[a][2]))
289
	end
290
	for y = 1, newYsize - (minYsize - 1) do
291
		output[y] = {}
292
		for x = 1, newXsize - (minXsize - 1) do
293
			local rx,ry = rotatePoint(x + (minXsize - 1),y + (minYsize - 1),degrees,centerX,centerY)
294
			rx, ry = round(rx), round(ry)
295
			if image[ry] then
296
				output[y][x] = image[ry][rx] or 0
297
			end
298
		end
299
	end
300
	return output
301
end
302
303
paintutils.pixelateImage = function(image,intensity)
304
	local imgXsize, imgYsize = paintutils.getSize(image)
305
	local output = {}
306
	for y = 1, imgYsize do
307
		output[y] = {}
308
		for x = 1, imgXsize do
309
			local vx, vy = math.max(math.min(round(x/intensity)*intensity,imgXsize),1), math.max(math.min(round(y/intensity)*intensity,imgYsize),1)
310
			if image[vy] then
311
				if image[vy][vx] then
312
					output[y][x] = image[vy][vx]
313
				else
314
					output[y][x] = 0
315
				end
316
			else
317
				output[y][x] = 0
318
			end
319
		end
320
	end
321
	return output
322
end