/* * * BOOH * * * A.k.a `Best web-album Of the world, Or your money back, Humerus'. * * The acronyn sucks, however this is a tribute to Dragon Ball by * Akira Toriyama, where the last enemy beaten by heroes of Dragon * Ball is named "Boo". But there was already a free software project * called Boo, so this one will be it "Booh". Or whatever. * * * Copyright (c) 2005 Guillaume Cottenceau * * This software may be freely redistributed under the terms of the GNU * public license version 2. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ #include #define GDK_PIXBUF_ENABLE_BACKEND #include #include #include "rbgobject.h" #define _SELF(s) GDK_PIXBUF(RVAL2GOBJ(s)) static VALUE whitebalance(self, level) VALUE self, level; { double red_filter[256], blue_filter[256]; int i, x, y; guchar* pixels = gdk_pixbuf_get_pixels(_SELF(self)); int rowstride = gdk_pixbuf_get_rowstride(_SELF(self)); double factor = 1 + fabs(NUM2DBL(level))/100; if (NUM2DBL(level) < 0) { factor = 1/factor; } for (i = 0; i < 256; i++) { red_filter[i] = pow(((double)i)/255, 1/factor) * 255; blue_filter[i] = pow(((double)i)/255, factor) * 255; } for (y = 0; y < gdk_pixbuf_get_height(_SELF(self)); y++) { guchar* pixline = &(pixels[rowstride*y]); for (x = 0; x < gdk_pixbuf_get_width(_SELF(self)); x++) { pixline[x*3] = red_filter[pixline[x*3]]; pixline[x*3+2] = blue_filter[pixline[x*3+2]]; } } return self; } static VALUE gammacorrect(self, level) VALUE self, level; { double filter[256]; int i, x, y; guchar* pixels = gdk_pixbuf_get_pixels(_SELF(self)); int rowstride = gdk_pixbuf_get_rowstride(_SELF(self)); double factor = 1 + fabs(NUM2DBL(level))/100; if (NUM2DBL(level) > 0) { factor = 1/factor; } for (i = 0; i < 256; i++) { filter[i] = pow(((double)i)/255, factor) * 255; } for (y = 0; y < gdk_pixbuf_get_height(_SELF(self)); y++) { guchar* pixline = &(pixels[rowstride*y]); for (x = 0; x < gdk_pixbuf_get_width(_SELF(self)); x++) { pixline[x*3] = filter[pixline[x*3]]; pixline[x*3+1] = filter[pixline[x*3+1]]; pixline[x*3+2] = filter[pixline[x*3+2]]; } } return self; } void Init_gtkadds() { RGObjClassInfo* cinfo = (RGObjClassInfo*)rbgobj_lookup_class_by_gtype(GDK_TYPE_PIXBUF, Qnil); rb_define_method(cinfo->klass, "whitebalance!", whitebalance, 1); rb_define_method(cinfo->klass, "gammacorrect!", gammacorrect, 1); }