ofImage setBrightness() & setContrast()

Hi, I would like to share 2 methods that I just implemented which can change brightness and contrast of an image.
I found algorithms from these links.

I’m not sure if this is the best(most efficient) way to do it but it seems like it’s working well. If anyone knows a better solution, please let me know.

void ofApp::setBrightness(ofImage &image, const int brightness) {
    
    int numChannels;
    
    switch (image.getImageType()) {
            
        case OF_IMAGE_GRAYSCALE:
            numChannels = 1;
            break;
        case OF_IMAGE_COLOR:
            numChannels = 3;
            break;
        case OF_IMAGE_COLOR_ALPHA:
            numChannels = 4;
            break;
        default:
            break;
    }
    ofPixels &pix = image.getPixels();
    const size_t pixSize = static_cast<size_t>(image.getWidth() * image.getHeight() * numChannels);
    
    if (numChannels == 1) {
        
        for (size_t i=0; i<pixSize; ++i) {
            
            const int g = pix[i] + brightness;
            pix[i] = static_cast<unsigned char>(g < 0 ? 0 : g > 255 ? 255 : g);
        }
    }
    else {
        
        for (size_t i=0; i<pixSize; i+=numChannels) {
            
            const int r = pix[i] + brightness;
            const int g = pix[i+1] + brightness;
            const int b = pix[i+2] + brightness;
            pix[i] = static_cast<unsigned char>(r < 0 ? 0 : r > 255 ? 255 : r);
            pix[i+1] = static_cast<unsigned char>(g < 0 ? 0 : g > 255 ? 255 : g);
            pix[i+2] = static_cast<unsigned char>(b < 0 ? 0 : b > 255 ? 255 : b);
        }
    }
    image.update();
}

void ofApp::setContrast(ofImage &image, const int contrast) {
    
    int numChannels;
    
    switch (image.getImageType()) {
            
        case OF_IMAGE_GRAYSCALE:
            numChannels = 1;
            break;
        case OF_IMAGE_COLOR:
            numChannels = 3;
            break;
        case OF_IMAGE_COLOR_ALPHA:
            numChannels = 4;
            break;
        default:
            break;
    }
    ofPixels &pix = image.getPixels();
    const size_t pixSize = static_cast<size_t>(image.getWidth() * image.getHeight() * numChannels);
    const float factor = (259.0f * (contrast + 255)) / (255.0f * (259 - contrast));
    
    if (numChannels == 1) {
        
        for (size_t i=0; i<pixSize; ++i) {
            
            const int g = static_cast<int>(factor * (pix[i] - 128) + 128);
            pix[i] = static_cast<unsigned char>(g < 0 ? 0 : g > 255 ? 255 : g);
        }
    }
    else {
        
        for (size_t i=0; i<pixSize; i+=numChannels) {
            
            const int r = static_cast<int>(factor * (pix[i] - 128) + 128);
            const int g = static_cast<int>(factor * (pix[i+1] - 128) + 128);
            const int b = static_cast<int>(factor * (pix[i+2] - 128) + 128);
            pix[i] = static_cast<unsigned char>(r < 0 ? 0 : r > 255 ? 255 : r);
            pix[i+1] = static_cast<unsigned char>(g < 0 ? 0 : g > 255 ? 255 : g);
            pix[i+2] = static_cast<unsigned char>(b < 0 ? 0 : b > 255 ? 255 : b);
        }
    }
    image.update();
}

And if you want to quickly test using mouse movement, just copy this code.

ofImage image1, image2;

//--------------------------------------------------------------
void ofApp::setup(){

image1.load("image.jpg");
}

//--------------------------------------------------------------
void ofApp::update(){

image2.clone(image1);

//brightness
const int brightness = static_cast<int>(static_cast<float>(mouseX) /
                                        static_cast<float>(ofGetWidth()) * 256.0f - 128.0f);
cout << "brightness : " <<  brightness << endl;

setBrightness(image2, brightness);

//contrast
const int contrast = static_cast<int>(static_cast<float>(mouseY) /
                                      static_cast<float>(ofGetHeight()) * 256.0f - 128.0f);
cout << "contrast : " <<  contrast << endl;

setContrast(image2, contrast);
}


//--------------------------------------------------------------
void ofApp::draw(){

image2.draw(0, 0, ofGetWidth(), ofGetHeight());
}