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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 222k
  • Answers 222k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See the section Adding dynamic content via portlets in the… May 13, 2026 at 12:24 am
  • Editorial Team
    Editorial Team added an answer Many users deactivate their referer, mostly not by choice. But… May 13, 2026 at 12:24 am
  • Editorial Team
    Editorial Team added an answer I've replaced the .click with mousedown, and inserted the script… May 13, 2026 at 12:24 am

Related Questions

I have an AppleScript program which creates XML tags and elements within an Adobe
In trying to figure out whether or not my code's inner loop is hitting
I have a following code in a most inner loop of my program struct
I'm writing an inner loop that needs to place struct s in contiguous storage.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.