image filters library with pics

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
flattspott
Posts: 22
Joined: Mon Aug 22, 2005 4:06 am

image filters library with pics

Post by flattspott »

I wanted to contribute a little something. It may be a bit slow but it's not too bad and it/they work(s). Perhaps someone can get some use from them.

Code: Select all

function brightness(img, amount)	--must be between 100 and -100
	output = Image.createEmpty(img:height(), img:width())
	for x = 1, img:width()-1 do
		for y = 1, img:height()-1 do
			p = img:pixel(y, x)
			r = p:colors().r+amount
			if r < 0 then r = 0 end
			if r > 255 then r = 255 end
			g = p&#58;colors&#40;&#41;.g+amount
			if g < 0 then g = 0 end
			if g > 255 then g = 255 end
			b = p&#58;colors&#40;&#41;.b+amount
			if b < 0 then b = 0 end
			if b > 255 then b = 255 end
			c = Color.new&#40;r,g,b&#41;
			output&#58;pixel&#40;y, x, c&#41;
		end
	end
	return output
end

function invert&#40;img&#41;
	output = Image.createEmpty&#40;img&#58;height&#40;&#41;, img&#58;width&#40;&#41;&#41;
	for x = 1, img&#58;width&#40;&#41;-1 do
		for y = 1, img&#58;height&#40;&#41;-1 do
			p = img&#58;pixel&#40;y, x&#41;
			c = Color.new&#40;255-p&#58;colors&#40;&#41;.r,255-p&#58;colors&#40;&#41;.g,255-p&#58;colors&#40;&#41;.b&#41;
			output&#58;pixel&#40;y, x, c&#41;
		end
	end
	return output
end

function contrast&#40;img, amount&#41; -- must be between 100 and -100
	if amount < -100 then amount = -100 end
	if amount > 100 then amount = 100 end
	amount = amount + 100
	amount = amount / 100
	amount = amount * amount
	output = Image.createEmpty&#40;img&#58;height&#40;&#41;, img&#58;width&#40;&#41;&#41;
	for x = 1, img&#58;width&#40;&#41;-1 do
		for y = 1, img&#58;height&#40;&#41;-1 do
			p = img&#58;pixel&#40;y, x&#41;
			r = p&#58;colors&#40;&#41;.r
			r = r / 255
			r = r - 0.5
			r = r * amount
			r = r + 0.5
			r = r * 255
			if r < 0 then r = 0 end
			if r > 255 then r = 255 end
			g = p&#58;colors&#40;&#41;.g
			g = g / 255
			g = g - 0.5
			g = g * amount
			g = g + 0.5
			g = g * 255
			if g < 0 then g = 0 end
			if g > 255 then g = 255 end
			b = p&#58;colors&#40;&#41;.b
			b = b / 255
			b = b - 0.5
			b = b * amount
			b = b + 0.5
			b = b * 255
			if b < 0 then b = 0 end
			if b > 255 then b = 255 end
			c = Color.new&#40;r,g,b&#41;
			output&#58;pixel&#40;y, x, c&#41;
		end
	end
	return output
end

function stripcolor&#40;img, red, green, blue&#41; -- 1 mean strip that color, 0 means leave it
	output = Image.createEmpty&#40;img&#58;height&#40;&#41;, img&#58;width&#40;&#41;&#41;
	for x = 1, img&#58;width&#40;&#41;-1 do
		for y = 1, img&#58;height&#40;&#41;-1 do
			p = img&#58;pixel&#40;y, x&#41;
			r = p&#58;colors&#40;&#41;.r
			g = p&#58;colors&#40;&#41;.g
			b = p&#58;colors&#40;&#41;.b
			if red == 0 then r = r end
			if red == 1 then r = -255 end
			if green == 0 then g = g end
			if green == 1 then g = -255 end
			if blue == 0 then b = b end
			if blue == 1 then b = -255 end
			c = Color.new&#40;r,g,b&#41;
			output&#58;pixel&#40;y, x, c&#41;
		end
	end
	return output
end

test = Image.load&#40;"64.png"&#41;
screen&#58;blit&#40;30, 26, test&#41; -- the original image
screen&#58;blit&#40;106, 36, invert&#40;test&#41;&#41;
screen&#58;blit&#40;40, 102, stripcolor&#40;test, 1, 1, 0&#41;&#41; -- make it blue
screen&#58;blit&#40;106, 102, stripcolor&#40;test, 0, 1, 1&#41;&#41; -- make it red
screen&#58;blit&#40;172, 102, stripcolor&#40;test, 1, 0, 1&#41;&#41; -- make it green
screen&#58;blit&#40;40, 168, contrast&#40;test, 50&#41;&#41; -- increase by 50
screen&#58;blit&#40;106, 168, contrast&#40;test, -50&#41;&#41; -- decrease by -50
screen&#58;blit&#40;172, 168, brightness&#40;test, 50&#41;&#41; -- increase by 50
screen&#58;blit&#40;238, 168, brightness&#40;test, -50&#41;&#41; -- decrease by -50
screen&#58;flip&#40;&#41;
screen.waitVblankStart&#40;600&#41;
--screen&#58;save&#40;"filters.png"&#41;
The source image i used
Image

and the various things you can do
Image
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

hehe i already inplemented those functions in C
(to be compiled with luaplayer) which should be way faster than the lua code :)
current status is up on my page
also have blur and so on online
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Post Reply