+ Reply to Thread
Results 1 to 2 of 2

Thread: bitmap contrasting

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    13

    bitmap contrasting

    I have a method to adjust an image's brightness, contrast, gamma, etc and i am trying to get real-time updates to my image while the user drags the slider. The brightness and gamma algorithms are an acceptable speed (around 170ms). But the contrast algorithm is about 380ms.
    Basically my form is a tool window with sliders. Each time the image is updated, it sends an event to the parent which redraws the new image. The tool window keeps the original unmodified image locked in memory so I always have access to the bytes of it.
    Code:
    double value = 0;
    double c = (100.0 + contrast) / 100.0;
    
    c *= c;
    
    for (int i = 0; i < sourcePixels.Length; i++)
    {
        value = sourcePixels[i];
    
        value /= 255.0;
        value -= 0.5;
        value *= c;
        value += 0.5;
        value *= 255;
    
        if (value < 0)
            value = 0;
        if (value > 255)
            value = 255;
    
        destPixels[i] = (byte)value;
    }

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Depending on the machine you're running this on, your technique could be quite slow. If you're using an ARM system without an FPU, each of those operations would take quite a while. Since you're applying the same operation to every byte, a faster technique would be to create a 256-entry lookup table for the contrast level and then translate each image byte through the table. Your loop would then look like:
    Code:
    byte contrast_lookup[256];
    double value = 0;
    double c = (100.0 + contrast) / 100.0;
    
    c *= c;
    
    for (int i = 0; i < 256; i++)
    {
        value = (double)i;
        value /= 255.0;
        value -= 0.5;
        value *= c;
        value += 0.5;
        value *= 255;
    
        if (value < 0)
            value = 0;
        if (value > 255)
            value = 255;
        contrast_lookup[i] = (byte)value;
    }
    
    for (int i = 0; i < sourcePixels.Length; i++)
    {
        destPixels[i] = contrast_lookup[sourcePixels[i]];
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts