Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 377409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:41:37+00:00 2026-05-12T14:41:37+00:00

My inner loop contains a calculation that profiling shows to be problematic. The idea

  • 0

My inner loop contains a calculation that profiling shows to be problematic.

The idea is to take a greyscale pixel x (0 <= x <= 1), and “increase its contrast”. My requirements are fairly loose, just the following:

  • for x < .5, 0 <= f(x) < x
  • for x > .5, x < f(x) <= 1
  • f(0) = 0
  • f(x) = 1 – f(1 – x), i.e. it should be “symmetric”
  • Preferably, the function should be smooth.

So the graph must look something like this:

Graph.

I have two implementations (their results differ but both are conformant):

float cosContrastize(float i) {
    return .5 - cos(x * pi) / 2;
}

float mulContrastize(float i) {
    if (i < .5) return i * i * 2;
    i = 1 - i;
    return 1 - i * i * 2;
}

So I request either a microoptimization for one of these implementations, or an original, faster formula of your own.

Maybe one of you can even twiddle the bits 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:41:38+00:00Added an answer on May 12, 2026 at 2:41 pm

    Trivially you could simply threshold, but I imagine this is too dumb:

    return i < 0.5 ? 0.0 : 1.0;
    

    Since you mention ‘increasing contrast’ I assume the input values are luminance values. If so, and they are discrete (perhaps it’s an 8-bit value), you could use a lookup table to do this quite quickly.

    Your ‘mulContrastize’ looks reasonably quick. One optimization would be to use integer math. Let’s say, again, your input values could actually be passed as an 8-bit unsigned value in [0..255]. (Again, possibly a fine assumption?) You could do something roughly like…

    int mulContrastize(int i) {
      if (i < 128) return (i * i) >> 7; 
      // The shift is really: * 2 / 256
      i = 255 - i;
      return 255 - ((i * i) >> 7);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an inner loop that needs to place struct s in contiguous storage.
I have a query that has 7 inner joins (because a lot of the
There is a div that has inner content, a div with a border that's
I have an array that contains an array of arrays if that makes any
Is there a language construct in C# that allows the abstraction of loop (or
If I wanted to enumerate the iterations of an inner loop in Scala, how
In this program, the inner loop generates random numbers out of 100 and then
EDIT: For the inner queries, there could be more than one match per inner
If I have an inner class, like this: public class Test { public class
What is the main difference between an inner class and a static nested class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.