gm_image - Basic image manipulation

This will let you save/load images to and from files. At the moment it only saves bitmap images, but I’m going to add support for Portable Network Graphics as well.

[img_thumb]http://cdbarrett.com/dump/screenshots/garrysmod/gm_flatgrass0036.jpg[/img_thumb] [img_thumb]http://cdbarrett.com/dump/screenshots/garrysmod/gm_flatgrass0037.jpg[/img_thumb] [img_thumb]http://cdbarrett.com/dump/screenshots/garrysmod/gm_flatgrass0035.jpg[/img_thumb]
It’s not viable to use this in realtime, above as an example, that takes too long to generate to be done in realtime.

However, since you can also copy the active rendertarget to an image and save it, you can create your own spawn icons for example. This is a multi-model icon I made and saved to file.

http://cdbarrett.com/dump/screenshots/garrysmod/icon.bmp

Functions:
[ul]
[li]image.CreateImage( width, height )[/li][li]image.CreateProceduralTexture()[/li][li]Image:GetWidth()[/li][li]Image:GetHeight()[/li][li]Image:CopyRT( x, y, width, height )[/li][li]Image:Load( filename )[/li][li]Image:Save( filename )[/li][li]Image:GetPixel( x, y )[/li][li]Image:SetPixel( x, y, color )[/li][/ul]

Binary: http://dl.dropbox.com/u/4266088/facepunch/gm_image.zip
Source: http://dl.dropbox.com/u/4266088/facepunch/gm_image_src.zip

Examples:
[lua]require( “image” );

// capture and save the current image on the screen to file.
local screenImage = image.CreateImage( 0, 0 );
screenImage:CopyRT( 0, 0, ScrW(), ScrH() );
screenImage:Save( “./garrysmod/screen.bmp” );
[/lua]

[lua]require( “image” );

local gradient = image.CreateImage( 128, 128 );

// write a gradient
for x = 0, 127 do

for y = 0, 127 do

	gradient:SetPixel( x, y, Color( ( 255 / 128 ) * i, 0, 0, 255 ) );

end

end

// commit the image to a texture
local texture = image.CreateProceduralTexture();
gradient:Commit( texture );

// draw it
surface.SetTexture( texture );
surface.SetDrawColor( 255, 255, 255, 255 );
surface.DrawTexturedRect( 0, 0, 128, 128 );[/lua]

[lua]require( “image” );

local icon = image.CreateImage( 0, 0 );
icon:Load( “./garrysmod/icon.bmp” );

// commit the image to a texture
local texture = image.CreateProceduralTexture();
icon:Commit( texture );

// draw it
surface.SetTexture( texture );
surface.SetDrawColor( 255, 255, 255, 255 );
surface.DrawTexturedRect( 0, 0, 128, 128 );[/lua]

Woah you started this like a few hours ago

Looks awesome. I’ll be sure to try it with a wire scanner.

If only this worked server to client… then I could take a picture of their screen and send it to a storage place so admins can look for lua hacks.

Well, you could, but it’d be insanely slow and I wouldn’t advise it.
The server can read/write bitmap images. The client just has to send the enormous amount of image data to the server and have it write it. The module is loadable on client and server, it just has extra features on the client.

Very cool Jinto. Would it ever be possible to make it efficient enough to use it in the way Jonney described?

So awesome that you deserve a smiley face.

Would we be able to get a function to create, load and save images to and from a string? I can’t remember which program did it, I think it was burning sand or something, but you could generate icons from a length of hexadecimal numbers.

Although, I can’t think of a good use for it… More of a novelty thing…

It would make it easier to send the picture of the network.

Just encode the image data to whatever you want, then decode and use SetPixel to write it out.

Well if it was in PNG format, it actually wouldn’t be as big but still atleast 512 sends if every send is 255 bytes.

Oh god I love you so much Jinto

Awesome.

Out of curiosity, does this let you save bitmaps with alpha?

god damn, Jinto, amazing work. You always pump out the best modules.

Bitmaps don’t support alpha as far as I know.


This is one of those modules that’s cool, but I just can’t think of any feasible, reasonable usage for it beyond SetPixel :S in the long run. Any good examples aside from the spawnicon?

Windows Bitmaps do support alpha, although the images themselves aren’t that well supported (for example, OS X handles them better than Windows does)

The module looks cool, if I ever had any good ideas for things I’d use it.

I’m going to remove bitmap support entirely and switch over to png.

Will it support animated PNG?

Shit is still newer than new. Although animated PNG support would be pretty sweet. The animations of a GIF and the opacity of a PNG.