Easy Supersampling in VB.NET

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Here's an image resizer program, which uses supersampling, I coded in about half an hour. If you're unfamiliar with supersampling, basically it takes a large image and averages neighbor pixels to form one pixel on the smaller picture. This is good for antialiasing and it just makes the image look softer in general. Thought I'd share the code. One thing I need help with: eventually I want this algorithm to be real-time so I can process media files with it, and thus have a supersampling media player. I probably could do this with Direct3D, true, but if possible I'd like to learn something. So, I need some advice on how to optimize it.

I'll post a sample shortly.
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
Doesn't this loop just grab the pixels that are diagonal (up and towards the left) to the current pixel?

 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: igowerf
Doesn't this loop just grab the pixels that are diagonal (up and towards the left) to the current pixel?

Good catch. Fixed, I think. Updated the code. Know of any good 2048x1536 wallpapers I can try this on?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
It looks like you could combine your 2 (i,j) loops and use a single p variable instead of an array, though a decent compiler might do this for you behind the scenes.
 

kevinthenerd

Platinum Member
Jun 27, 2002
2,908
0
76
Originally posted by: DaveSimmons
It looks like you could combine your 2 (i,j) loops and use a single p variable instead of an array, though a decent compiler might do this for you behind the scenes.

This is VB we're talking about here. I don't know about that. VB6 booleans took two bytes! (Does VB.NET even support boolean data types?)
 

kevinthenerd

Platinum Member
Jun 27, 2002
2,908
0
76
Sorry, I'm an old hand at VB6. I haven't made the switch yet.

Does VB.NET support Option Explicit? Avoid variants like the plague.

The first thing I always do in code optimization is to tidy up my data types. Make sure you're using the smallest data types you can. If your image will never exceed 255 pixels, go with a Byte data type. (In your code, do you really need int32's? Maybe Int16's will work?)

Is there any way you can use pointers to directly access your data? Reading data structures is marginally slower than reading non-array, static variables.

Again, I don't know much about VB.NET, but I know in VB6, when you compile your code, you have quite a few options to optimize the compiler. Get rid of error checking, and make use of the processor's registers for fast calculation.

If you're feeling very adventurous, I know VB6 has ways to access assembly code if you're looking for the ultimate in speed, but at that point, you're using a lot of time learning and coding for a very marginal gain. It's like those Formula 1 teams who spend tens of millions of dollars for a 1% increase in dynamic performance. Nothing beats the feeling of perfection, though, and it makes some hobbies even that much more enjoyable when you know you've reached the highest goal you could ever attain with it.

I hope someone with more .NET experience can critique what I say before you waste your time applying it.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: kevinthenerd
If you're feeling very adventurous, I know VB6 has ways to access assembly code if you're looking for the ultimate in speed, but at that point, you're using a lot of time learning and coding for a very marginal gain. It's like those Formula 1 teams who spend tens of millions of dollars for a 1% increase in dynamic performance. Nothing beats the feeling of perfection, though, and it makes some hobbies even that much more enjoyable when you know you've reached the highest goal you could ever attain with it.

That's the next project. I have found some assembly code to multiply pixels with the IMUL x86 instruction. Thanks for the advice. How much does a 16-bit integer store? And what about a short? Which stores 256 values?

I found .NET was SO much faster with image processing. This would 5 minutes on VB6.

I'm going to multithread the program and maybe use it as a basis for a very simple freeware image editor. If I'm really lucky maybe I'll add SSE2.
 

kevinthenerd

Platinum Member
Jun 27, 2002
2,908
0
76
Originally posted by: xtknight
Originally posted by: kevinthenerd
If you're feeling very adventurous, I know VB6 has ways to access assembly code if you're looking for the ultimate in speed, but at that point, you're using a lot of time learning and coding for a very marginal gain. It's like those Formula 1 teams who spend tens of millions of dollars for a 1% increase in dynamic performance. Nothing beats the feeling of perfection, though, and it makes some hobbies even that much more enjoyable when you know you've reached the highest goal you could ever attain with it.

That's the next project. I have found some assembly code to multiply pixels with the IMUL x86 instruction. Thanks for the advice. How much does a 16-bit integer store? And what about a short? Which stores 256 values?

I found .NET was SO much faster with image processing. This would 5 minutes on VB6.

I'm going to multithread the program and maybe use it as a basis for a very simple freeware image editor. If I'm really lucky maybe I'll add SSE2.

Integer data types hold a range of values equivalent to their power of 2, according to simple binary arithmetic.

2 ^ 8 = 256 possible values for 8-bit integer
2 ^ 16 = 65536 possible values for 16-bit integer
2 ^ 24 = 16777216 possible values for 24-bit integer

The range, however, doesn't necessarily mean it's going to hold a number that high. In VB, I believe integers default to being able to hold negative values. A 16-bit integer, for example, will hold values from -32k to +32k. A short IS a 16-bit.

http://www.ondotnet.com/pub/a/dotnet/2001/07/30/vb7.html


In VB, I believe you have the option of using a Byte to store 8-bit values. From the days of QB, however, I've always used a String*1 to hold such information. I'm not sure how much faster of slower the Byte data type is; I'll have to write some code to test it.