override rg2's gdkpixbuf.rotate with a noleak method instead of testing for the fix
[booh] / ext / rbbooh.c
1 /*
2  *                         *  BOOH  *
3  *
4  * A.k.a `Best web-album Of the world, Or your money back, Humerus'.
5  *
6  * The acronyn sucks, however this is a tribute to Dragon Ball by
7  * Akira Toriyama, where the last enemy beaten by heroes of Dragon
8  * Ball is named "Boo". But there was already a free software project
9  * called Boo, so this one will be it "Booh". Or whatever.
10  *
11  *
12  * Copyright (c) 2005 Guillaume Cottenceau
13  *
14  * This software may be freely redistributed under the terms of the GNU
15  * public license version 2.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <math.h>
24
25 #define GDK_PIXBUF_ENABLE_BACKEND
26 #include <gtk/gtk.h>
27 #include "rbgobject.h"
28
29 #define _SELF(s) GDK_PIXBUF(RVAL2GOBJ(s)) 
30
31 #include <libexif/exif-data.h>
32
33 static VALUE
34 whitebalance(self, level)
35     VALUE self, level; 
36 {
37         double red_filter[256], blue_filter[256];
38         int i, x, y;
39         guchar* pixels = gdk_pixbuf_get_pixels(_SELF(self));
40         int rowstride = gdk_pixbuf_get_rowstride(_SELF(self));
41
42         double factor = 1 + fabs(NUM2DBL(level))/100;
43         if (NUM2DBL(level) < 0) {
44                 factor = 1/factor;
45         }
46
47         for (i = 0; i < 256; i++) {
48                 red_filter[i]  = pow(((double)i)/255, 1/factor) * 255;
49                 blue_filter[i] = pow(((double)i)/255, factor) * 255;
50         }
51     
52         for (y = 0; y < gdk_pixbuf_get_height(_SELF(self)); y++) {
53                 guchar* pixline = &(pixels[rowstride*y]);
54                 for (x = 0; x < gdk_pixbuf_get_width(_SELF(self)); x++) {
55                         pixline[x*3]   = red_filter[pixline[x*3]];
56                         pixline[x*3+2] = blue_filter[pixline[x*3+2]];
57                 }
58         }
59
60         return self;
61 }
62
63 static VALUE
64 gammacorrect(self, level)
65     VALUE self, level; 
66 {
67         double filter[256];
68         int i, x, y;
69         guchar* pixels = gdk_pixbuf_get_pixels(_SELF(self));
70         int rowstride = gdk_pixbuf_get_rowstride(_SELF(self));
71
72         double factor = 1 + fabs(NUM2DBL(level))/100;
73         if (NUM2DBL(level) > 0) {
74                 factor = 1/factor;
75         }
76
77         for (i = 0; i < 256; i++) {
78                 filter[i] = pow(((double)i)/255, factor) * 255;
79         }
80     
81         for (y = 0; y < gdk_pixbuf_get_height(_SELF(self)); y++) {
82                 guchar* pixline = &(pixels[rowstride*y]);
83                 for (x = 0; x < gdk_pixbuf_get_width(_SELF(self)); x++) {
84                         pixline[x*3]   = filter[pixline[x*3]];
85                         pixline[x*3+1] = filter[pixline[x*3+1]];
86                         pixline[x*3+2] = filter[pixline[x*3+2]];
87                 }
88         }
89
90         return self;
91 }
92
93 static ExifEntry* exif_entry(VALUE filename, ExifTag tag, ExifData** data) {
94         unsigned int i;
95
96         *data = exif_data_new_from_file(StringValuePtr(filename));
97         if (*data == NULL) {
98                 fprintf(stderr, "libexif failed loading file '%s'\n", StringValuePtr(filename));
99                 return NULL;
100         }
101
102         for (i = 0; i < EXIF_IFD_COUNT; i++) {
103                 ExifEntry* entry = exif_content_get_entry((*data)->ifd[i], tag);
104                 if (entry)
105                         return entry;
106         }
107
108         return NULL;
109 }
110
111 static VALUE exif_orientation(VALUE module, VALUE filename) {
112         ExifData* data = NULL;
113         ExifEntry* entry;
114
115         entry = exif_entry(filename, EXIF_TAG_ORIENTATION, &data);
116
117         if (entry != NULL) {
118                 VALUE ret;
119                 ExifByteOrder o;
120                 ExifShort v_short;
121                 if (!entry || !entry->parent || !entry->parent->parent)
122                         return Qnil;
123                 o = exif_data_get_byte_order(entry->parent->parent);
124                 v_short = exif_get_short(entry->data, o);
125                 ret = INT2NUM(v_short);
126                 exif_data_free(data);
127                 return ret;
128         }
129         
130         if (data)
131                 exif_data_free(data);
132         return Qnil;
133 }
134
135 static VALUE exif_datetimeoriginal(VALUE module, VALUE filename) {
136         ExifData* data = NULL;
137         ExifEntry* entry;
138
139         entry = exif_entry(filename, EXIF_TAG_DATE_TIME_ORIGINAL, &data);
140
141         if (entry != NULL) {
142                 VALUE ret = rb_str_new((const char*) entry->data, entry->size);
143                 exif_data_free(data);
144                 return ret;
145         }
146
147         if (data)
148                 exif_data_free(data);
149         return Qnil;
150 }
151
152 static VALUE draw_borders(VALUE self, VALUE pixbuf, VALUE x1, VALUE x2, VALUE ystart, VALUE yend) {
153         GdkDrawable* drawable = GDK_DRAWABLE(RVAL2GOBJ(self));
154         int y = NUM2INT(ystart);
155         int yend_ = NUM2INT(yend);
156         GdkPixbuf* pb = GDK_PIXBUF(RVAL2GOBJ(pixbuf));
157         int height = gdk_pixbuf_get_height(pb);
158         while (y < yend_) {
159                 int render_height = MIN(height, yend_ - y);
160                 gdk_draw_pixbuf(drawable, NULL, pb, 0, 0, NUM2INT(x1), y, -1, render_height, GDK_RGB_DITHER_NONE, -1, -1);
161                 gdk_draw_pixbuf(drawable, NULL, pb, 0, 0, NUM2INT(x2), y, -1, render_height, GDK_RGB_DITHER_NONE, -1, -1);
162                 y += height;
163         }
164         return self;
165 }
166
167 static VALUE rotate_noleak(VALUE self, VALUE angle) {
168         VALUE ret;
169         GdkPixbuf* dest = gdk_pixbuf_rotate_simple(_SELF(self), RVAL2GENUM(angle, GDK_TYPE_PIXBUF_ROTATION));
170         if (dest == NULL)
171                 return Qnil;
172         ret = GOBJ2RVAL(dest);
173         g_object_unref(dest);
174         return ret;
175 }
176
177 void 
178 Init_libadds()
179 {
180     RGObjClassInfo* cinfo = (RGObjClassInfo*)rbgobj_lookup_class_by_gtype(GDK_TYPE_PIXBUF, Qnil);
181     rb_define_method(cinfo->klass, "whitebalance!", whitebalance, 1); 
182     rb_define_method(cinfo->klass, "gammacorrect!", gammacorrect, 1); 
183     rb_define_method(cinfo->klass, "rotate", rotate_noleak, 1); 
184
185     cinfo = (RGObjClassInfo*)rbgobj_lookup_class_by_gtype(GDK_TYPE_DRAWABLE, Qnil);
186     rb_define_method(cinfo->klass, "draw_borders", draw_borders, 5);
187
188     VALUE exif = rb_define_module("Exif");
189     rb_define_module_function(exif, "orientation", exif_orientation, 1);
190     rb_define_module_function(exif, "datetimeoriginal", exif_datetimeoriginal, 1);
191 }