SOL9 2.0 Class: GrayScaleFilter

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * GrayScaleFilter.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2000.07.07
#pragma once


#include <sol\Object.h>

#include <sol\ImageFilter.h>


namespace SOL {

class GrayScaleFilter :public ImageFilter {

public:
    GrayScaleFilter() { }

public:

    DIBSection* filterRGB(DC& dc, DIBSection& image)
    {
        int w = image.getWidth();
        int h = image.getHeight();
        DIBSection* grayImage = new DIBSection(dc, w, h);
    
        if (grayImage == NULL) {
            return NULL;
        }

        for(int y = 0; y < h; y++) {

            for (int x = 0; x < w; x++) {
                int r = 0;
                int g = 0;
                int b = 0;
                image.getPixel(x, y, r, g, b);

            /*
            int v = (int) (0.298912*(double)r + 0.586611*(double)g + 0.114478*(double)b);
                
            int    v = (2 * r + 4 * g + b)/7; 
            */

                // Much faster way.
                int    v = ((r<<1) + (g<<2) + b)/7; 

                grayImage->setPixel(x, y, RGB(v, v, v));
            }
        }
        return grayImage;
    }

};

}




Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.