pixbufutils 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,24 +19,69 @@ end
19
19
  # Look for headers in {gem_root}/ext/{package}
20
20
  if use_gems
21
21
  %w[
22
- glib2 ].each do |package|
23
- require package
24
- $CFLAGS += " -I"+Gem.loaded_specs[package].full_gem_path+"/ext/"+package
22
+ glib2].each do |package|
23
+ require package
24
+ if Gem.loaded_specs[package]
25
+ $CFLAGS += " -I" + Gem.loaded_specs[package].full_gem_path + "/ext/" + package
26
+ else
27
+ if fn = $".find { |n| n.sub(/[.](so|rb)$/,'') == package }
28
+ dr = $:.find { |d| File.exist?(File.join(d, fn)) }
29
+ pt = File.join(dr,fn) if dr && fn
30
+ else
31
+ pt = "??"
32
+ end
33
+ STDERR.puts "require '" + package + "' loaded '"+pt+"' instead of the gem - trying to continue, but build may fail"
34
+ end
25
35
  end
26
36
  end
27
37
  if RbConfig::CONFIG.has_key?('rubyhdrdir')
28
- $CFLAGS += " -I" + RbConfig::CONFIG['rubyhdrdir']+'/ruby'
38
+ $CFLAGS += " -I" + RbConfig::CONFIG['rubyhdrdir']+'/ruby'
29
39
  end
30
40
 
31
41
  $CFLAGS += " -I."
32
42
  have_func("rb_errinfo")
33
43
  PKGConfig.have_package("gdk-pixbuf-2.0") or exit(-1)
34
44
  PKGConfig.have_package("gdk-2.0") or exit(-1)
35
- have_header("gdk-pixbuf/gdk-pixbuf.h") or exit(-1)
36
- have_header("rbglib.h") or exit(-1)
37
- have_header("rbgobject.h") or exit(-1)
38
- have_header("tiffio.h") or exit(-1)
39
- have_header("gdk/gdk.h") or exit(-1)
45
+
46
+ unless have_header("gdk-pixbuf/gdk-pixbuf.h")
47
+ paths = Gem.find_files("gdk-pixbuf/gdk-pixbuf.h")
48
+ paths.each do |path|
49
+ $CFLAGS += " '-I#{File.dirname(path)}'"
50
+ end
51
+ have_header("gdk-pixbuf/gdk-pixbuf.h") or exit -1
52
+ end
53
+
54
+ unless have_header("rbglib.h")
55
+ paths = Gem.find_files("rbglib.h")
56
+ paths.each do |path|
57
+ $CFLAGS += " '-I#{File.dirname(path)}'"
58
+ end
59
+ have_header("rbglib.h") or exit -1
60
+ end
61
+
62
+ unless have_header("rbgobject.h")
63
+ paths = Gem.find_files("rbgobject.h")
64
+ paths.each do |path|
65
+ $CFLAGS += " '-I#{File.dirname(path)}'"
66
+ end
67
+ have_header("rbgobject.h") or exit -1
68
+ end
69
+
70
+ unless have_header("tiffio.h")
71
+ paths = Gem.find_files("tiffio.h")
72
+ paths.each do |path|
73
+ $CFLAGS += " '-I#{File.dirname(path)}'"
74
+ end
75
+ have_header("tiffio.h") or exit -1
76
+ end
77
+
78
+ unless have_header("gdk/gdk.h")
79
+ paths = Gem.find_files("gdk/gdk.h")
80
+ paths.each do |path|
81
+ $CFLAGS += " '-I#{File.dirname(path)}'"
82
+ end
83
+ have_header("gdk/gdk.h") or exit -1
84
+ end
40
85
  have_library("tiff") or exit(-1)
41
86
  $LIBS += " -ltiff"
42
87
 
@@ -0,0 +1,66 @@
1
+ static GdkPixbuf *pixbuf_extract_alpha(GdkPixbuf *src, int cutoff, int force_2bit)
2
+ {
3
+ int s_has_alpha, d_has_alpha;
4
+ int s_width, s_height, s_rowstride;
5
+ int d_width, d_height, d_rowstride;
6
+ GdkPixbuf *dest;
7
+ guchar *s_pix, *sp;
8
+ guchar *d_pix, *dp;
9
+ int i, j, pix_width, grey;
10
+
11
+ g_return_val_if_fail(src != NULL, NULL);
12
+
13
+ s_width = gdk_pixbuf_get_width(src);
14
+ s_height = gdk_pixbuf_get_height(src);
15
+ s_has_alpha = gdk_pixbuf_get_has_alpha(src);
16
+ s_rowstride = gdk_pixbuf_get_rowstride(src);
17
+ s_pix = gdk_pixbuf_get_pixels(src);
18
+ g_return_val_if_fail(s_has_alpha, NULL);
19
+
20
+ dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, s_width, s_height);
21
+
22
+ g_return_val_if_fail(dest != NULL, NULL);
23
+
24
+ d_rowstride = gdk_pixbuf_get_rowstride(dest);
25
+ d_pix = gdk_pixbuf_get_pixels(dest);
26
+
27
+ d_width = gdk_pixbuf_get_width(dest);
28
+ d_height = gdk_pixbuf_get_height(dest);
29
+ d_has_alpha = gdk_pixbuf_get_has_alpha(dest);
30
+ d_rowstride = gdk_pixbuf_get_rowstride(dest);
31
+ d_pix = gdk_pixbuf_get_pixels(dest);
32
+
33
+ g_return_val_if_fail(d_width == s_width, NULL);
34
+ g_return_val_if_fail(d_height == s_height, NULL);
35
+
36
+ pix_width = (s_has_alpha ? 4 : 3);
37
+
38
+ for (i = 0; i < s_height; i++) {
39
+ sp = s_pix;
40
+ dp = d_pix;
41
+
42
+ for (j = 0; j < s_width; j++) {
43
+ grey = sp[3];
44
+
45
+ if (grey < cutoff)
46
+ grey = 0;
47
+
48
+ if (force_2bit)
49
+ grey = (grey >= cutoff ? 255 : 0);
50
+
51
+ dp[0] = grey; /* red */
52
+ dp[1] = grey; /* green */
53
+ dp[2] = grey; /* blue */
54
+
55
+ dp += 3;
56
+ sp += 4;
57
+ }
58
+
59
+ d_pix += d_rowstride;
60
+ s_pix += s_rowstride;
61
+ }
62
+
63
+ return dest;
64
+ }
65
+
66
+
@@ -0,0 +1,376 @@
1
+ #include <math.h>
2
+
3
+ static GdkPixbuf *pixbuf_adjust_brightness(GdkPixbuf *src, GdkPixbuf *dest, int adjust) {
4
+ int s_has_alpha, d_has_alpha;
5
+ int s_width, s_height, s_rowstride;
6
+ int d_width, d_height, d_rowstride;
7
+ guchar *s_pix, *sp;
8
+ guchar *d_pix, *dp;
9
+ int i, j, pix_width;
10
+ int mod = (int) floor(255 * (adjust/100));
11
+
12
+
13
+ g_return_val_if_fail(src != NULL, NULL);
14
+ g_return_val_if_fail(dest != NULL, NULL);
15
+
16
+ s_width = gdk_pixbuf_get_width(src);
17
+ s_height = gdk_pixbuf_get_height(src);
18
+ s_has_alpha = gdk_pixbuf_get_has_alpha(src);
19
+ s_rowstride = gdk_pixbuf_get_rowstride(src);
20
+ s_pix = gdk_pixbuf_get_pixels(src);
21
+
22
+ d_width = gdk_pixbuf_get_width(dest);
23
+ d_height = gdk_pixbuf_get_height(dest);
24
+ d_has_alpha = gdk_pixbuf_get_has_alpha(dest);
25
+ d_rowstride = gdk_pixbuf_get_rowstride(dest);
26
+ d_pix = gdk_pixbuf_get_pixels(dest);
27
+
28
+ g_return_val_if_fail(d_width == s_width, NULL);
29
+ g_return_val_if_fail(d_height == s_height, NULL);
30
+ g_return_val_if_fail(d_has_alpha == s_has_alpha, NULL);
31
+
32
+ pix_width = (s_has_alpha ? 4 : 3);
33
+
34
+
35
+
36
+ for (i = 0; i < s_height; i++) {
37
+ sp = s_pix;
38
+ dp = d_pix;
39
+
40
+ for (j = 0; j < s_width; j++) {
41
+
42
+ dp[0] = (unsigned char) MAX(MIN(mod + sp[0], 255), 0);
43
+ dp[1] = (unsigned char) MAX(MIN(mod + sp[1], 255), 0);
44
+ dp[2] = (unsigned char) MAX(MIN(mod + sp[2], 255), 0);
45
+
46
+ if (s_has_alpha)
47
+ {
48
+ dp[3] = sp[3]; /* alpha */
49
+ }
50
+
51
+ dp += pix_width;
52
+ sp += pix_width;
53
+ }
54
+
55
+ d_pix += d_rowstride;
56
+ s_pix += s_rowstride;
57
+ }
58
+
59
+ return dest;
60
+ }
61
+
62
+ inline unsigned char pix_value(int value)
63
+ {
64
+ if (value < 0)
65
+ return 0;
66
+ if (value > 255)
67
+ return 255;
68
+ return (unsigned char) value;
69
+ }
70
+
71
+ static GdkPixbuf *pixbuf_adjust_saturation(GdkPixbuf *src, GdkPixbuf *dest, int adjust) {
72
+ int s_has_alpha, d_has_alpha;
73
+ int s_width, s_height, s_rowstride;
74
+ int d_width, d_height, d_rowstride;
75
+ guchar *s_pix, *sp;
76
+ guchar *d_pix, *dp, max;
77
+ int i, j, pix_width;
78
+ double mod = adjust * -0.01;
79
+
80
+
81
+ g_return_val_if_fail(src != NULL, NULL);
82
+ g_return_val_if_fail(dest != NULL, NULL);
83
+
84
+ s_width = gdk_pixbuf_get_width(src);
85
+ s_height = gdk_pixbuf_get_height(src);
86
+ s_has_alpha = gdk_pixbuf_get_has_alpha(src);
87
+ s_rowstride = gdk_pixbuf_get_rowstride(src);
88
+ s_pix = gdk_pixbuf_get_pixels(src);
89
+
90
+ d_width = gdk_pixbuf_get_width(dest);
91
+ d_height = gdk_pixbuf_get_height(dest);
92
+ d_has_alpha = gdk_pixbuf_get_has_alpha(dest);
93
+ d_rowstride = gdk_pixbuf_get_rowstride(dest);
94
+ d_pix = gdk_pixbuf_get_pixels(dest);
95
+
96
+ g_return_val_if_fail(d_width == s_width, NULL);
97
+ g_return_val_if_fail(d_height == s_height, NULL);
98
+ g_return_val_if_fail(d_has_alpha == s_has_alpha, NULL);
99
+
100
+ pix_width = (s_has_alpha ? 4 : 3);
101
+
102
+ for (i = 0; i < s_height; i++) {
103
+ sp = s_pix;
104
+ dp = d_pix;
105
+
106
+ for (j = 0; j < s_width; j++) {
107
+ max = sp[0];
108
+ if (sp[1] > max)
109
+ max = sp[1];
110
+ if (sp[2] > max)
111
+ max = sp[2];
112
+
113
+
114
+ if (max != sp[0])
115
+ dp[0] = pix_value(sp[0] + ((double)(max - sp[0])) * mod);
116
+ else
117
+ dp[0] = sp[0];
118
+
119
+ if (max != sp[1])
120
+ dp[1] = pix_value(sp[1] + ((double)(max - sp[1])) * mod);
121
+ else
122
+ dp[1] = sp[1];
123
+
124
+ if (max != sp[2])
125
+ dp[2] = pix_value(sp[2] + ((double)(max - sp[2])) * mod);
126
+ else
127
+ dp[2] = sp[2];
128
+
129
+ if (s_has_alpha)
130
+ {
131
+ dp[3] = sp[3]; /* alpha */
132
+ }
133
+
134
+ dp += pix_width;
135
+ sp += pix_width;
136
+ }
137
+
138
+ d_pix += d_rowstride;
139
+ s_pix += s_rowstride;
140
+ }
141
+
142
+ return dest;
143
+ }
144
+
145
+
146
+ static GdkPixbuf *pixbuf_adjust_vibrance(GdkPixbuf *src, GdkPixbuf *dest, int adjust) {
147
+ int s_has_alpha, d_has_alpha;
148
+ int s_width, s_height, s_rowstride;
149
+ int d_width, d_height, d_rowstride;
150
+ guchar *s_pix, *sp;
151
+ guchar *d_pix, *dp;
152
+ int i, j, pix_width, avg, max;
153
+ double mod = adjust * -1;
154
+
155
+
156
+ g_return_val_if_fail(src != NULL, NULL);
157
+ g_return_val_if_fail(dest != NULL, NULL);
158
+
159
+ s_width = gdk_pixbuf_get_width(src);
160
+ s_height = gdk_pixbuf_get_height(src);
161
+ s_has_alpha = gdk_pixbuf_get_has_alpha(src);
162
+ s_rowstride = gdk_pixbuf_get_rowstride(src);
163
+ s_pix = gdk_pixbuf_get_pixels(src);
164
+
165
+ d_width = gdk_pixbuf_get_width(dest);
166
+ d_height = gdk_pixbuf_get_height(dest);
167
+ d_has_alpha = gdk_pixbuf_get_has_alpha(dest);
168
+ d_rowstride = gdk_pixbuf_get_rowstride(dest);
169
+ d_pix = gdk_pixbuf_get_pixels(dest);
170
+
171
+ g_return_val_if_fail(d_width == s_width, NULL);
172
+ g_return_val_if_fail(d_height == s_height, NULL);
173
+ g_return_val_if_fail(d_has_alpha == s_has_alpha, NULL);
174
+
175
+ pix_width = (s_has_alpha ? 4 : 3);
176
+
177
+ for (i = 0; i < s_height; i++) {
178
+ sp = s_pix;
179
+ dp = d_pix;
180
+
181
+ for (j = 0; j < s_width; j++) {
182
+ max = sp[0];
183
+ if (sp[1] > max)
184
+ max = sp[1];
185
+ if (sp[2] > max)
186
+ max = sp[2];
187
+
188
+ avg = (sp[0] + sp[1] + sp[2])/3;
189
+ mod = ((abs(max - avg) * 2/255) * -1 * adjust) / 100.0;
190
+
191
+ if (max != sp[0])
192
+ dp[0] = (unsigned char) MAX(MIN(sp[0] + ( max - sp[0] ) * mod , 255), 0);
193
+ if (max != sp[1])
194
+ dp[1] = (unsigned char) MAX(MIN(sp[1] + ( max - sp[1] ) * mod , 255), 0);
195
+ if (max != sp[2])
196
+ dp[2] = (unsigned char) MAX(MIN(sp[2] + ( max - sp[2] ) * mod , 255), 0);
197
+
198
+ if (s_has_alpha)
199
+ {
200
+ dp[3] = sp[3]; /* alpha */
201
+ }
202
+
203
+ dp += pix_width;
204
+ sp += pix_width;
205
+ }
206
+
207
+ d_pix += d_rowstride;
208
+ s_pix += s_rowstride;
209
+ }
210
+
211
+ return dest;
212
+ }
213
+ static GdkPixbuf *pixbuf_adjust_contrast(GdkPixbuf *src, GdkPixbuf *dest, int adjust) {
214
+ int s_has_alpha, d_has_alpha;
215
+ int s_width, s_height, s_rowstride;
216
+ int d_width, d_height, d_rowstride;
217
+ guchar *s_pix, *sp;
218
+ guchar *d_pix, *dp;
219
+ int i, j, pix_width;
220
+ double mod = pow(((double)adjust + 100.0)/100.0, 2);
221
+
222
+
223
+ g_return_val_if_fail(src != NULL, NULL);
224
+ g_return_val_if_fail(dest != NULL, NULL);
225
+
226
+ s_width = gdk_pixbuf_get_width(src);
227
+ s_height = gdk_pixbuf_get_height(src);
228
+ s_has_alpha = gdk_pixbuf_get_has_alpha(src);
229
+ s_rowstride = gdk_pixbuf_get_rowstride(src);
230
+ s_pix = gdk_pixbuf_get_pixels(src);
231
+
232
+ d_width = gdk_pixbuf_get_width(dest);
233
+ d_height = gdk_pixbuf_get_height(dest);
234
+ d_has_alpha = gdk_pixbuf_get_has_alpha(dest);
235
+ d_rowstride = gdk_pixbuf_get_rowstride(dest);
236
+ d_pix = gdk_pixbuf_get_pixels(dest);
237
+
238
+ g_return_val_if_fail(d_width == s_width, NULL);
239
+ g_return_val_if_fail(d_height == s_height, NULL);
240
+ g_return_val_if_fail(d_has_alpha == s_has_alpha, NULL);
241
+
242
+ pix_width = (s_has_alpha ? 4 : 3);
243
+
244
+ for (i = 0; i < s_height; i++) {
245
+ sp = s_pix;
246
+ dp = d_pix;
247
+
248
+ for (j = 0; j < s_width; j++) {
249
+
250
+ dp[0] = (unsigned char) MAX(MIN(127 + ( (((double)sp[0]) - 127) * mod ), 255), 0);
251
+ dp[1] = (unsigned char) MAX(MIN(127 + ( (((double)sp[1]) - 127) * mod ), 255), 0);
252
+ dp[2] = (unsigned char) MAX(MIN(127 + ( (((double)sp[2]) - 127) * mod ), 255), 0);
253
+
254
+ if (s_has_alpha)
255
+ {
256
+ dp[3] = sp[3]; /* alpha */
257
+ }
258
+
259
+ dp += pix_width;
260
+ sp += pix_width;
261
+ }
262
+
263
+ d_pix += d_rowstride;
264
+ s_pix += s_rowstride;
265
+ }
266
+
267
+ return dest;
268
+ }
269
+
270
+
271
+ static GdkPixbuf *pixbuf_convolution_matrix(GdkPixbuf *src, GdkPixbuf *dest, int matrix_size, double *matrix, double divisor)
272
+ {
273
+ int s_has_alpha, d_has_alpha;
274
+ int s_width, s_height, s_rowstride;
275
+ int d_width, d_height, d_rowstride;
276
+ guchar *s_pix, *sp, *cp;
277
+ guchar *d_pix, *dp;
278
+ int i, j, pix_width;
279
+
280
+ int xx, yy, mp;
281
+ int matrix_from, matrix_to;
282
+ double sum_red, sum_green, sum_blue;
283
+
284
+ g_return_val_if_fail(src != NULL, NULL);
285
+ g_return_val_if_fail(dest != NULL, NULL);
286
+
287
+ s_width = gdk_pixbuf_get_width(src);
288
+ s_height = gdk_pixbuf_get_height(src);
289
+ s_has_alpha = gdk_pixbuf_get_has_alpha(src);
290
+ s_rowstride = gdk_pixbuf_get_rowstride(src);
291
+ s_pix = gdk_pixbuf_get_pixels(src);
292
+
293
+ d_width = gdk_pixbuf_get_width(dest);
294
+ d_height = gdk_pixbuf_get_height(dest);
295
+ d_has_alpha = gdk_pixbuf_get_has_alpha(dest);
296
+ d_rowstride = gdk_pixbuf_get_rowstride(dest);
297
+ d_pix = gdk_pixbuf_get_pixels(dest);
298
+
299
+ g_return_val_if_fail(d_width == s_width, NULL);
300
+ g_return_val_if_fail(d_height == s_height, NULL);
301
+ g_return_val_if_fail(d_has_alpha == s_has_alpha, NULL);
302
+
303
+ pix_width = (s_has_alpha ? 4 : 3);
304
+
305
+ mp = matrix_size >> 1;
306
+ matrix_from = -1 * mp;
307
+ matrix_to = mp;
308
+
309
+ /*
310
+ fprintf(stderr, "FILTER: %i -> %i", matrix_from, matrix_to);
311
+ for (yy = matrix_from; yy <= matrix_to; yy ++) {
312
+ for (xx = matrix_from; xx <= matrix_to; xx ++) {
313
+ int index = ((mp + yy) * matrix_size) + (mp + xx);
314
+ double multiplier = matrix[ index ];
315
+ if ((xx == matrix_from)) {
316
+ fprintf(stderr, "\n");
317
+ }
318
+ fprintf(stderr, "(%i,%i) [%i] : %5.3f ", xx, yy, index, multiplier);
319
+ }
320
+ }
321
+ fprintf(stderr, "\n\n");
322
+ // */
323
+
324
+ for (i = 0; i < s_height; i++) {
325
+ sp = s_pix;
326
+ dp = d_pix;
327
+
328
+ for (j = 0; j < s_width; j++) {
329
+ sum_red = 0.0;
330
+ sum_green = 0.0;
331
+ sum_blue = 0.0;
332
+
333
+ for (yy = matrix_from; yy <= matrix_to; yy ++) {
334
+ for (xx = matrix_from; xx <= matrix_to; xx ++) {
335
+
336
+ int index = ((mp + yy) * matrix_size) + (mp + xx);
337
+ double multiplier = matrix[ index ];
338
+
339
+ if ( ((j + xx) < 0) || ((j + xx) >= s_width) ||
340
+ ((i + yy) < 0) || ((i + yy) >= s_height) ) {
341
+ sum_red += multiplier;
342
+ sum_green += multiplier;
343
+ sum_blue += multiplier;
344
+ continue;
345
+ }
346
+
347
+ cp = sp + (yy * s_rowstride) + (xx * pix_width);
348
+ sum_red += (multiplier * (double)cp[0]);
349
+ sum_green += (multiplier * (double)cp[1]);
350
+ sum_blue += (multiplier * (double)cp[2]);
351
+ }
352
+ }
353
+ sum_red /= divisor;
354
+ sum_green /= divisor;
355
+ sum_blue /= divisor;
356
+
357
+ dp[0] = (unsigned char)MIN(MAX(sum_red, 0.0), 255.0); /* red */
358
+ dp[1] = (unsigned char)MIN(MAX(sum_green, 0.0), 255.0); /* green */
359
+ dp[2] = (unsigned char)MIN(MAX(sum_blue, 0.0), 255.0); /* blue */
360
+
361
+ if (s_has_alpha)
362
+ {
363
+ dp[3] = sp[3]; /* alpha */
364
+ }
365
+
366
+ dp += pix_width;
367
+ sp += pix_width;
368
+ }
369
+
370
+ d_pix += d_rowstride;
371
+ s_pix += s_rowstride;
372
+ }
373
+
374
+ return dest;
375
+ }
376
+