• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lawnjelly

Integer scaling

7 posts in this topic

Hi, this is probably really easy, but..

 

how do I best do a fast, accurate integer scaling (0.0 - 1.0) of 2 unsigned chars (8 bit values)?

[Note: this is the same problem as doing accurate alpha-blending. But it is also useful in other areas, e.g. audio.]

 

e.g.

 

a = 100; // test number

b = 250; // scale factor 0 - 255, which should correspond to a 0.0 to 1.0 factor

 

result = (a * b) / 255;

 

However in this case the divide by a non-magic number (2, 4, 8, 16, etc) is presumably not as good as dividing by 256 which can be a bitshift? Although perhaps these days in most CPUs this is not an issue as memory bandwidth is more important?

 

And finally, what is the *best* way of doing this, allowing floating point stuff (and perhaps SIMD, maybe there is an instruction for it?).

 

*edit*

Variations I've seen:

 

1) Just divide by 256 (the bitshift)

 

(and ignore the fact that 255 x 255 will come out as 254 as the result)

 

Pros: Works fast

Cons: Might be ok in many uses but isn't accurate.

 

2) result = (a * (b+1)) / 256;

 

Pros: Should be almost as fast as the above.

when b = 0

e.g. 255 * 1 = 255,

255 / 256 = 0 CORRECT

when b = 255

255 * 256 = 65280

65280 / 256 = 255 CORRECT

 

Cons: Are there some cases where this gives different results than the 'true' result? Not sure

 

*/edit*

0

Share this post


Link to post
Share on other sites

I can propose

 

3) result = (a * (1 + 2*b) + 256) / 512

 

None of those proposals give you the true result in all situations.

 

Option 1 disagrees with the true result in 47056 cases. 

Option 2 disagrees with the true result in 18760 cases.

Option 3 disagrees with the true result in 7720 cases.

 

What I am using as the true result is this:

result = (2 * a * b + 255) / 510

 

I like options 2 and 3 because they do the right thing for b=0 and b=255.

 

However, you have to ask yourself how expensive the division by 510 really is. Is your program noticeably faster with one formula than with another? If not, stick to the true formula.

 

[EDIT: Note that the compiler can use some tricks to compute the division by 510 as a multiplication by some magic constant and a couple of bit shifts. A detailed explanation of how this would work can be found [url="http://reverseengineering.stackexchange.com/questions/1397/how-can-i-reverse-optimized-integer-division-modulo-by-constant-operations"]here[/url].]

Edited by Álvaro
1

Share this post


Link to post
Share on other sites

Wow thanks Alvaro. I'm sure a few of you have come across this exact issue.

 

I just was looking at it this morning, wondering at the best solution, as I'm doing an photoshop-like program at the moment so can sacrifice speed for getting the most accurate solution, but still want the 'fastest' accurate solution.

 

Must admit it makes my tiny brain go all fuzzy lol.

 

So is the

 

result = (2 * a * b + 255) / 510

 

more accurate than

 

result = (a * b) / 255

 

?

If so I'll change my code for this.

0

Share this post


Link to post
Share on other sites

The problem with (a*b)/255 is that it rounds down. If a is 89 and b is 209, a*b/255 is the integer 72, although the result of the division before truncation is actually 72.945098..., which is almost 73. I would say the correct value in that case is 73.

 

So the formula that I take to be `true' is

[b]int((a*b)/255.0+0.5)[/b]

 

However, that formula involves converting between integers and floating-point numbers, which can be slow. But it's easy to fix if we move the +0.5 into the fraction like this:

[b]int((a*b+127.5)/255.0) == int((2*a*b+255)/510.0) == (2*a*b+255)/510[/b]

 

Using (a*b)/255 results in an average darkening of the image by about half a level.

1

Share this post


Link to post
Share on other sites

(a*(b+1))/256 is wrong for numerous values, like a=185, b=186. Found by computer search.

Yes, and I posted above for how many values it is wrong: 18760 of the total 65536 possibilities. Of course I also found that with a little program.

0

Share this post


Link to post
Share on other sites

This is probably purely academic now, but it's kind of interesting lol smile.png

 

Alvaro, as your formula:

 

3) result = (a * (1 + 2*b) + 256) / 512

 

seems to essentially be 'diluting' the inaccuracy (as far as I can see), is it possible to use 'bigger numbers' to make the approximation method more accurate?

 

i.e. would using 1024 instead of 512 make it better? Essentially using whatever you could fit into say, a 32 bit unsigned int?

 

Haha my maths is clearly lost here lol biggrin.png

0

Share this post


Link to post
Share on other sites

The point of my formula (3) is that it is a proper scaling (i.e., of the form floor(a*fraction+0.5), where `fraction' is a real number between 0 and 1) and it does the right thing for b=0 and b=255.

 

But yes, you can get a more precise formula using larger numbers. This one seems to work perfectly:

[b](a*b*65793+8388608)/16777216[/b]
 

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0