
A BitmapManip class I put together since I was trying to do 2D sprites quite a bit.
Includes common manipulations like:
Changing opacity w/ alpha channel options
Rescaling w/o aspect ratio restrictions
Cropping
Checking for power-of-two dimensions
Specific color replacement
Rotate around center of image
Blitting two images together
Flipping
Bitmap to Texture
Texture to Bitmap
Byte[,] to Bitmap
Bitmap to Byte[,]
With the latter two, you could probably use those C# GDI+ bitmap manipulation classes on the Web to do weird stuff like Perlin noise, etc.
The script is located here: http://cardinal4.phpnet.us/BitmapManip.cs.
Set BitmapManip.Device to the IrrlichtDevice first before using it.
Don't forget to include a reference to the System.Drawing library to use Bitmaps.
This uses alias directives... , I'm not too sure if it's a .NET1.0 feature.
Do not load non-power-of-two Textures into memory via VideoDriver.GetTexture(). Load them in as Bitmaps instead. It is perfectly fine however, to change non-power-of-two Bitmaps to Textures.
Should be able to handle most images. .png, .bmp, and .jpg all work with transparency functions since I used Format32bppArgb, ColorFormat.A8R8G8B8 and .pngs internally.
Potential conflict with filename "I1r2r3C4P5.png" and textures that use the name "I1r2r3C4P5", so don't use those names!
Example: how to do an overlay

Code:
BillboardSceneNode bbsn = _scene.AddBillboardSceneNode(null, new Dimension2Df(100,100), 456);
BitmapManip.Device = device;
//I just used the textures sitting on my drive so I have to resize them. They don't need to be of the same size for Blit() to work.
Bitmap bmp1 = new Bitmap("umagon_shocked.jpg");
Bitmap bmp2 = new Bitmap("2ddemo.bmp");
bmp1 = BitmapManip.Scale(bmp1, 256,256);
bmp2 = BitmapManip.Scale(bmp2, 256,256);
bmp2 = BitmapManip.ChangeAlpha(bmp2, 50);
Bitmap bmp = BitmapManip.Blit(bmp1, bmp2, new Vector2D(0,0));
Texture text = BitmapManip.BitmapToTextureHigh(bmp);
bbsn.SetMaterialTexture(0, text);
bbsn.SetMaterialFlag(MaterialFlag.Lighting,false);
bbsn.SetMaterialType(MaterialType.TransparentAlphaChannel);
bbsn.GetMaterial(0).MaterialTypeParam = 0.3f;
Starting off with a 100x100 .jpg, then changing it to a Texture, then back to a Bitmap, then scaling to 128x128, then changing back to a Texture, will work just fine, since the image was loaded into memory first as a Bitmap, not a Texture.
Code:
BitmapManip.Device = device;
//a 100x100 bitmap
Bitmap bmp = new Bitmap("umagon_shocked.jpg");
Texture text = BitmapManip.BitmapToTextureHigh(bmp);
text = BitmapManip.ChangeAlpha(text, 40);
bmp = BitmapManip.TextureToBitmapHigh(text);
//A pretty round-about way, but just to show that a Texture, so long
//as it was not loaded in via IrrlichtDevice.VideoDriver.GetTexture(),
//is perfectly fine to mess around with despite being 100x100.
bmp = BitmapManip.Scale(bmp, 128, 128);
text = BitmapManip.BitmapToTextureHigh(bmp);
I worked on this for a long long time... But only when into hard-core programming mode last Friday till today. Waking up on a sch holiday, full of ideas and programming is very very enjoyable. But then you'd kinda feel drained afterwards during the afternoon and just wanna take a nap, barring which can always play Tiberium Wars. =D
This is like my second contribution back to a community. First was a Gash's Chi-Eng translation. Now it's this BitmapManip library. It was about time I started reciprocating all the free help I've gotten this few years.
I hope I don't get shot down by the pros about the code. In either case, even if they shot it down and wrote their own better improved code, it's a win-win situation.
One, my forum reputation will increase slightly. That way I'll possibly get more help next time I have a problem.
Two, I'll still use this code even if they shot it down and rejected it. I seriously need Bitmap manipulation libraries.
Three, it's also possible that they try to optimize my code to make it run faster, or offer alternative suggestions to my method. In that case, I'll have even faster code!
=D Right, now to play some Tiberium Wars. I always have a habit of hanging around forums especially after I posted something up, waiting for a response. That is soooo unproductive. Gaming is making better use of that time. =D
EDIT: Deleted a replicated Bitmap function that made a specific color transparent.
EDIT: Used Texture.Modify() instead of Texture.SetPixel() for pixel-by-pixel Texture modification.
EDIT: Officially it's using unmanaged code now. Has direct access to byte array of Bitmaps => Speed improvements. Also comes with a Bitmap quick accessor class now.
I was right. Suggestions offered by other forum members have led to cleaner code. And they got me thinking about how to use a byte array to construct Bitmaps.
![]()
Konjiki no Gash Bell rocks!! Don't say it ain't.
1. Konjiki no Gash Bell OP3 [Mienai Tsubasa] - Tanimoto Takayashi
2. Bad Day - David Powter
3. Bleach OP1 [Alones] - Aqua Timerz

No comments:
Post a Comment