Any way to limit values without the use of if statements?

João Simões

I'm working on a program that works with images. I'm having trouble on a function that adjusts contrast and brightness. I need to calculate a value for each RGB component based on what input I recieve from the user. The problem is, I need to make sure the final value after said calculation isn't greater than 255 or less than 0. So it can fit inside a byte.

temp = c * dataPtr[0] + b; //variable temp is of type double.

if (temp > 255)
{
    temp = 255;
}
else if (temp < 0)
{
    temp = 0.0;
}

dataPtr[0] = (byte)(Math.Round(temp)); 

I'm repeating this for every RGB component of every pixel, so the ifs are getting executed a million times, most of the times needlessly.

I thought about just casting a double back to byte but it just reads the first byte of the double and doesn't max out the value if it's greater than what a byte can handle. Is there any obvious way to optmize this this range-check that I'm just missing? Thank you.

John3136

No. if is the "gold standard" way of comparing values. If you need to make sure the the value is in a range if is the way to do it. If you must have an alternative: use an existing type that can only handle values 0-255, but then "overflow" behavior is "less defined". If you make your own type it probably uses if inside it anyway.

Your reasons "lots of needless ifs" is nothing to worry about.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Any way to have a string selected based on an int without a bunch of if statements?

Any way to use the `@Procedure` annotation without an entity?

Is there any way to use QtWebEngine without OpenGL?

is there any way to use onkey without a function?

Any way to use variable values in a string literal?

Any possible way to check for a condition without using any of control statements in R

Is there any way to use the Room Finder without any Room Lists?

Is there any way to show the output in pretty way without 7, -48 .... etc in print statements?

Is there any way to reduce number of if statements?

Any way to limit border length?

tkinter mainloop - is there any way to use button or checkbutton without mainloop?

Is there any way to use youtubePlayerView in application without extending YoutubeBaseActivity, YoutubePlayerSupportfragment and intent

Is this the right way to use MVVM structure with ViewModel and RecyclerView without any issues?

Any way to use Google Api without every-time authentication?

Any way to use Skype without random crashes? Like another gui?

Is that any way to use xdebug without compiling the source in linux?

is there any way to use AsyncTask without memory leak in android using java

Is there any way to use polymorphic relation, without the namespace FQDN

Is there any limit in storing values in NSUserDefaults?

Is there any way to use values from a JSON object in a SQL Select statement?

Is there any way to use global like values in a nodejs app?

is there any way to use the values from a list in an sql lite database

Selecting top values without limit

Is there any way for grouping cases in MySQL CASE statements?

Try block without any catch statements

How can I use if-else statements (or a better way) to assign absolute values to days in a year (using R)?

Any way to use EmbeddedNativeLibrary?

Is it safe to use LIMIT without ORDER BY

Is there a way of writing this in XCode without 2000 if else statements?