Notifications
Clear all

[Closed] DotNet ColorMatrix Struct

Hello Peeps,

just a pointer to a new bit of research on my site about Image adjustment within Max – Hope you find it useful.

8 Replies

Thanks for this, LR, your articles are great little gateways into using .net and 3ds – (download link broken atm?)

When you were having trouble using lockbits, were you using the facilities provided in System.Runtime.InteropServices to handle copying of fixed allocated areas of memory, etc?

Hey Dr, Thanks! (link now working, always the bit i forget!)

this was how far i got with lockbits, just trying to convert a method i had seen of codeproject into max, using the marshall class as im using VB –


 	imgobj =  (dotNetClass "System.Drawing.Image").fromfile ("c:\_output.png")
 	bm = dotnetobject "system.drawing.bitmap" imgobj.width imgobj.height
 	g = (dotnetclass "system.drawing.Graphics").FromImage bm
 	g.drawimage imgobj 0 0
 	imgobj.dispose()
 	
 	PixelSize = 4
 	rect = dotNetObject "System.Drawing.Rectangle" 0 0 10 10 
 	bmd  = bm.LockBits rect ((dotnetclass "System.Drawing.Imaging.ImageLockMode").Readonly) bm.PixelFormat
 
 For y = 0 To bmd.Height - 1 do
 (
 	For x = 0 To bmd.Width - 1 do
 	(
 			(dotnetclass "System.Runtime.InteropServices.Marshal").writeByte bmd.Scan0 ((bmd.Stride * y) + (4 * x)) 255
 	)
 	
 	bm.UnlockBits bmd
 )
 
 -- Runtime error: dotNet runtime exception: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
 --OK

dude what a coincidence – I recently wrote a dotnet dll for a client to do some image processing and avoid photoshop. The idea was that the maxscript part would get all the bitmaps in the scene and pass their filenames to dotnet where they would be processed, doing some basic stuff like resize, desaturate and output levels. I intend to expand it and make it more generic, adding more filters / adjustments ala photoshop, possibly some other stuff like multithreading. This colormatrix info looks like it would be super useful. Thanks heaps!

On a similar note, do you know how one would go about writing a new dotnet compatable image codec? I havn’t been able to find any info or reference…

Hi Joel,

nice work! it’s the sort of thing i’ve been fiddling around with also, but probably not to the same level as you.

I would think that much of it would be in getting the unmanaged lockbits method (from the bitmapdata class)to work in a MXS context – the stuff on codeproject for convolution matrix operations uses the C# unsafe command but I have been trying to do the same as VB which has the marshal class.

This would make it fast to perform something more involved like gaussian blur, edge detection etc. The colormatrix stuff is just about quick enough for these basic color transforms, but anything more would need a different approach. At this stage i’d be happy to just port something into a MXS compatible dotnet approach, but im not getting any further than what i’ve just posted.

C# has the marshal class, too, which brings the unmanaged stuff into the managed domain AFAIK. The unsafe context in C# allows the direct use of pointers, but can scramble you up in the security system, needing permissions.

As for your code example, at first sight, it does look a bit funny, to lock the bits readonly, and then write to them in the next loop, starting at the same address? And then unlock inside the loop? I may be misunderstanding it, (sorry if so,) and haven’t had time to try. But the more typical usage I’ve seen, is like this example from MSDN, using a separate buffer.

cheers Dr,

you’re right it is a bit bonkers, the readonly bit was changed from ‘readwrite’ in a version where i vainly tried altering things to make it work. but it was originally from an example in vb that i was trying to convert. I’ll take a look at your link, thanks!

That looks very interesting and promising,

The .NET framework and GDI+ classes are very powerful. But Marshalling and doing heavy bitmap stuff directly in MAXScript is slow and limited due to MAXScript language and its memory.

Hi yannick,

I guess it’s better to do more involved image work in an outside assembly then, my brain wont explode either.