aboutsummaryrefslogtreecommitdiffstats
path: root/src/filter.c
blob: c55e9b8de2c3f9f61bcc5520e95e5225e5056cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include "filter.h"

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <glib/gi18n.h>

gchar const*
camorama_filter_get_name(CamoramaFilter* self) {
	gchar const* name = CAMORAMA_FILTER_GET_CLASS(self)->name;
	g_return_val_if_fail(name, G_OBJECT_TYPE_NAME(self));
	return _(name);
}

void
camorama_filter_apply(CamoramaFilter* self, guchar* image, gint width, gint height, gint depth) {
	g_return_if_fail(CAMORAMA_FILTER_GET_CLASS(self)->filter);

	CAMORAMA_FILTER_GET_CLASS(self)->filter(self, image, width, height, depth);
}

/* GType stuff ifor CamoramaFilter */
G_DEFINE_ABSTRACT_TYPE(CamoramaFilter, camorama_filter, G_TYPE_OBJECT);

static void
camorama_filter_init(CamoramaFilter* self) {}

static void
camorama_filter_class_init(CamoramaFilterClass* self_class) {}

#include "v4l.h"

static inline void move_420_block (int yTL, int yTR, int yBL, int yBR, int u,
                                   int v, int rowPixels, unsigned char *rgb,
                                   int bits);

/*these two functions are borrowed from the ov511 driver, with the author, 
 *Mark McClelland's kind encoragement*/

/* LIMIT: convert a 16.16 fixed-point value to a byte, with clipping. */
#define LIMIT(x) ((x)>0xffffff?0xff: ((x)<=0xffff?0:((x)>>16)))

void
yuv420p_to_rgb (unsigned char *image, unsigned char *temp, int x, int y, int z) {
    const int numpix = x * y;
    const int bytes = z;        /* (z*8) >> 3; */
    int i, j, y00, y01, y10, y11, u, v;
    unsigned char *pY = image;
    unsigned char *pU = pY + numpix;
    unsigned char *pV = pU + numpix / 4;
    unsigned char *image2 = temp;
    if(FALSE) // FIXME: make TRUE to add debugging in here
	g_print("%s\n", "yuv420p->rgb");
    for (j = 0; j <= y - 2; j += 2) {
        for (i = 0; i <= x - 2; i += 2) {
            y00 = *pY;
            y01 = *(pY + 1);
            y10 = *(pY + x);
            y11 = *(pY + x + 1);
            u = (*pU++) - 128;
            v = (*pV++) - 128;

                move_420_block (y00, y01, y10, y11, u, v, x, image2, z * 8);

                pY += 2;
                image2 += 2 * bytes;
            }
            pY += x;
            image2 += x * bytes;
        }
}

static inline void
move_420_block (int yTL, int yTR, int yBL, int yBR, int u, int v,
                int rowPixels, unsigned char *rgb, int bits)
{
    const int rvScale = 91881;
    const int guScale = -22553;
    const int gvScale = -46801;
    const int buScale = 116129;
    const int yScale = 65536;
    int r, g, b;

    g = guScale * u + gvScale * v;
    if (1) {
        r = buScale * u;
        b = rvScale * v;
    } else {
        r = rvScale * v;
        b = buScale * u;
    }

    yTL *= yScale;
    yTR *= yScale;
    yBL *= yScale;
    yBR *= yScale;

    if (bits == 24) {
        /* Write out top two pixels */
        rgb[0] = LIMIT (b + yTL);
        rgb[1] = LIMIT (g + yTL);
        rgb[2] = LIMIT (r + yTL);

        rgb[3] = LIMIT (b + yTR);
        rgb[4] = LIMIT (g + yTR);
        rgb[5] = LIMIT (r + yTR);

        /* Skip down to next line to write out bottom two pixels */
        rgb += 3 * rowPixels;
        rgb[0] = LIMIT (b + yBL);
        rgb[1] = LIMIT (g + yBL);
        rgb[2] = LIMIT (r + yBL);

        rgb[3] = LIMIT (b + yBR);
        rgb[4] = LIMIT (g + yBR);
        rgb[5] = LIMIT (r + yBR);
    } else if (bits == 16) {
        /* Write out top two pixels */
        rgb[0] = ((LIMIT (b + yTL) >> 3) & 0x1F)
            | ((LIMIT (g + yTL) << 3) & 0xE0);
        rgb[1] = ((LIMIT (g + yTL) >> 5) & 0x07)
            | (LIMIT (r + yTL) & 0xF8);

        rgb[2] = ((LIMIT (b + yTR) >> 3) & 0x1F)
            | ((LIMIT (g + yTR) << 3) & 0xE0);
        rgb[3] = ((LIMIT (g + yTR) >> 5) & 0x07)
            | (LIMIT (r + yTR) & 0xF8);

        /* Skip down to next line to write out bottom two pixels */
        rgb += 2 * rowPixels;

        rgb[0] = ((LIMIT (b + yBL) >> 3) & 0x1F)
            | ((LIMIT (g + yBL) << 3) & 0xE0);
        rgb[1] = ((LIMIT (g + yBL) >> 5) & 0x07)
            | (LIMIT (r + yBL) & 0xF8);

        rgb[2] = ((LIMIT (b + yBR) >> 3) & 0x1F)
            | ((LIMIT (g + yBR) << 3) & 0xE0);
        rgb[3] = ((LIMIT (g + yBR) >> 5) & 0x07)
            | (LIMIT (r + yBR) & 0xF8);
    }
}

/* GType stuff for CamoramaFilterColor */
typedef struct _CamoramaFilter      CamoramaFilterColor;
typedef struct _CamoramaFilterClass CamoramaFilterColorClass;

G_DEFINE_TYPE(CamoramaFilterColor, camorama_filter_color, CAMORAMA_TYPE_FILTER);

static void
camorama_filter_color_init(CamoramaFilterColor* self) {}

void
camorama_filter_color_filter(void *__filter, guchar *image, int x, int y, int depth) {
	CamoramaFilterColor *filter = __filter;
	int i;
	char tmp;
	i = x * y;
	while (i--) {
		tmp = image[0];
		image[0] = image[2];
		image[2] = tmp;
		image += 3;
	}
}

static void
camorama_filter_color_class_init(CamoramaFilterColorClass* self_class) {
	self_class->filter = camorama_filter_color_filter;
	self_class->name   = _("Color Correction");
}

/* GType stuff for CamoramaFilterInvert */
typedef struct _CamoramaFilter      CamoramaFilterInvert;
typedef struct _CamoramaFilterClass CamoramaFilterInvertClass;

G_DEFINE_TYPE(CamoramaFilterInvert, camorama_filter_invert, CAMORAMA_TYPE_FILTER);

static void
camorama_filter_invert_init(CamoramaFilterInvert* self) {}

static void
camorama_filter_invert_filter(CamoramaFilter* filter, guchar *image, int x, int y, int depth) {
#warning "FIXME: add a checking cast here"
	CamoramaFilterInvert* self = (CamoramaFilterInvert*)filter;
	int i;
	for (i = 0; i < x * y * depth; i++) {
		image[i] = 255 - image[i];
	}
}

static void
camorama_filter_invert_class_init(CamoramaFilterClass* self_class) {
	self_class->filter = camorama_filter_invert_filter;
	self_class->name   = _("Invert");
}

/* GType stuff for CamoramaFilterThreshold */
typedef struct _CamoramaFilterThreshold {
	CamoramaFilter base_instance;
	gint threshold;
} CamoramaFilterThreshold;
typedef struct _CamoramaFilterClass CamoramaFilterThresholdClass;

G_DEFINE_TYPE(CamoramaFilterThreshold, camorama_filter_threshold, CAMORAMA_TYPE_FILTER);

static void
camorama_filter_threshold_init(CamoramaFilterThreshold* self) {
	self->threshold = 127;
}

static void
camorama_filter_threshold_filter(CamoramaFilter* filter, guchar *image, int x, int y, int depth) {
#warning "FIXME: cast"
    CamoramaFilterThreshold* self = (CamoramaFilterThreshold*)filter;
    int i;
    for (i = 0; i < x * y; i++) {
        if ((image[0] + image[1] + image[2]) > (self->threshold * 3)) {
            image[0] = 255;
            image[1] = 255;
            image[2] = 255;
        } else {
            image[0] = 0;
            image[1] = 0;
            image[2] = 0;
        }
        image += 3;
    }
}

static void
camorama_filter_threshold_class_init(CamoramaFilterThresholdClass* self_class) {
	self_class->filter = camorama_filter_threshold_filter;
	self_class->name   = _("Threshold (Overall)");
}

/* GType stuff for CamoramaFilterThresholdChannel */
typedef struct _CamoramaFilterThreshold CamoramaFilterThresholdChannel;
typedef struct _CamoramaFilterClass     CamoramaFilterThresholdChannelClass;

G_DEFINE_TYPE(CamoramaFilterThresholdChannel, camorama_filter_threshold_channel, CAMORAMA_TYPE_FILTER);

static void
camorama_filter_threshold_channel_init(CamoramaFilterThresholdChannel* self) {
	self->threshold = 127;
}

static void
camorama_filter_threshold_channel_filter(CamoramaFilter* filter, unsigned char *image, int x, int y, int depth) {
#warning "FIXME: cast"
    CamoramaFilterThresholdChannel* self = (CamoramaFilterThresholdChannel*)filter;
    int i;
    for (i = 0; i < x * y; i++) {
        if (image[0] > self->threshold) {
            image[0] = 255;
        } else {
            image[0] = 0;
        }
        if (image[1] > self->threshold) {
            image[1] = 255;
        } else {
            image[1] = 0;
        }
        if (image[2] > self->threshold) {
            image[2] = 255;
        } else {
            image[2] = 0;
        }
        image += 3;
    }
}

static void
camorama_filter_threshold_channel_class_init(CamoramaFilterThresholdChannelClass* self_class) {
	self_class->filter = camorama_filter_threshold_channel_filter;
	self_class->name   = _("Threshold (Per Channel)");
}

/* GType stuff for CamoramaFilterWacky */
typedef struct _CamoramaFilter      CamoramaFilterWacky;
typedef struct _CamoramaFilterClass CamoramaFilterWackyClass;

G_DEFINE_TYPE(CamoramaFilterWacky, camorama_filter_wacky, CAMORAMA_TYPE_FILTER);

static void
camorama_filter_wacky_init(CamoramaFilterWacky* self) {}

static void
camorama_filter_wacky_filter(CamoramaFilter* filter, unsigned char *image, int x, int y, int depth) {
    int i;
    int neighbours;
    int total;
    unsigned char *image2, *image3;
    image2 = (unsigned char *) malloc (sizeof (unsigned char) * x * y * depth);
    memcpy (image2, image, x * y * depth);
    image3 = image2;

    for (i = 0; i < x * y; i++) {
        total = 0;
        neighbours = 0;

        if (i < x * depth) {
            /*we are in the top row */
        } else {
            image2 -= (x + 1) * depth;
            total = total + ((1 / 6) * image2[0]);
            image2 += depth;
            total = total + ((4 / 6) * image2[0]);
            image2 += depth;
            total = total + ((1 / 6) * image2[0]);
            neighbours = neighbours + depth;
            image2 += (x - 1) * depth;
        }
        if (i > x * (y - 1) * depth) {
            /*we are in the bottom row */
        } else {
            image2 += (x + 1) * depth;
            total = total + ((1 / 6) * image2[0]);
            image2 -= depth;
            total = total + ((4 / 6) * image2[0]);
            image2 -= depth;
            total = total + ((1 / 6) * image2[0]);
            image2 -= (x - 1) * depth;
            neighbours = neighbours + depth;
        }

        image2 += depth;
        total = total + ((4 / 6) * image2[0]);
        image2 -= depth;
        neighbours++;

        image2 -= depth;
        total = total + ((4 / 6) * image2[0]);
        image2 += depth;
        neighbours++;

        image[0] = image[0] * (-20 / 6);
        image[0] = image[0] + total;
        image[1] = image[0];
        image[2] = image[0];
        image += depth;
    }
    free (image2);
}

static void
camorama_filter_wacky_class_init(CamoramaFilterWackyClass* self_class) {
	self_class->filter = camorama_filter_wacky_filter;
	self_class->name   = _("Wacky");
}

/* GType stuff for CamoramaFilterSmotth */
typedef struct _CamoramaFilter      CamoramaFilterSmooth;
typedef struct _CamoramaFilterClass CamoramaFilterSmoothClass;

G_DEFINE_TYPE(CamoramaFilterSmooth, camorama_filter_smooth, CAMORAMA_TYPE_FILTER);

static void
camorama_filter_smooth_init(CamoramaFilterSmooth* self) {}

static void
camorama_filter_smooth_filter(CamoramaFilter* filter, guchar *image, int x, int y, int depth) {
    int i;
    int neighbours;
    int total0, total1, total2;
    unsigned char *image2, *image3;
    int tr = 0, br = 0;

    image2 = (unsigned char *) malloc (sizeof (unsigned char) * x * y * depth);
    memcpy (image2, image, x * y * depth);
    image3 = image2;

    for (i = 0; i < x * y; i++) {
        total0 = 0;
        total1 = 0;
        total2 = 0;
        neighbours = 0;

        if (i < x) {
            /*we are in the top row */
            tr++;
        } else {
            image2 -= (x + 1) * depth;
            total0 = total0 + image2[0];
            total1 = total1 + image2[1];
            total2 = total2 + image2[2];

            total0 = total0 + image2[3];
            total1 = total1 + image2[4];
            total2 = total2 + image2[5];

            total0 = total0 + image2[6];
            total1 = total1 + image2[7];
            total2 = total2 + image2[8];
            neighbours = neighbours + depth;
            if (tr > 1) {
                tr = 0;
            }
            image2 += (x + 1) * depth;
        }
        if (i > x * (y - 1)) {
            br++;
            /*we are in the bottom row */
        } else {
            image2 += (x - 1) * depth;
            total0 = total0 + image2[0];
            total1 = total1 + image2[1];
            total2 = total2 + image2[2];

            total0 = total0 + image2[3];
            total1 = total1 + image2[4];
            total2 = total2 + image2[5];

            total0 = total0 + image2[6];
            total1 = total1 + image2[7];
            total2 = total2 + image2[8];

            image2 -= (x - 1) * depth;

            neighbours = neighbours + depth;
        }

        image2 += 3;
        total0 = total0 + image2[0];
        total1 = total1 + image2[1];
        total2 = total2 + image2[2];
        image2 -= 3;
        neighbours++;

        image2 -= 3;
        total0 = total0 + image2[0];
        total1 = total1 + image2[1];
        total2 = total2 + image2[2];
        image2 += 3;
        neighbours++;

        image[0] = (int) (total0 / neighbours);
        image[1] = (int) (total1 / neighbours);
        image[2] = (int) (total2 / neighbours);

        image += 3;
        image2 += 3;
    }
    free (image3);
}

static void
camorama_filter_smooth_class_init(CamoramaFilterSmoothClass* self_class) {
	self_class->filter = camorama_filter_smooth_filter;
	self_class->name   = _("Smooth");
}

/* GType for CamoramaFilterMono */
typedef struct _CamoramaFilter      CamoramaFilterMono;
typedef struct _CamoramaFilterClass CamoramaFilterMonoClass;

G_DEFINE_TYPE(CamoramaFilterMono, camorama_filter_mono, CAMORAMA_TYPE_FILTER);

static void
camorama_filter_mono_init(CamoramaFilterMono* self) {}

static void
camorama_filter_mono_filter(CamoramaFilter* filter, unsigned char *image, int x, int y, int depth) {
    int i;
    int total, avg;

    for (i = 0; i < x * y; i++) {
        total = image[0] + image[1] + image[2];
        avg = (int) (total / 3);

        image[0] = avg;
        image[1] = avg;
        image[2] = avg;
        image += 3;
    }
}

static void
camorama_filter_mono_class_init(CamoramaFilterMonoClass* self_class) {
	self_class->filter = camorama_filter_mono_filter;
	self_class->name   = _("Monochrome");
}

/* GType for CamoramaFilterMonoWeight */
typedef struct _CamoramaFilter      CamoramaFilterMonoWeight;
typedef struct _CamoramaFilterClass CamoramaFilterMonoWeightClass;

G_DEFINE_TYPE(CamoramaFilterMonoWeight, camorama_filter_mono_weight, CAMORAMA_TYPE_FILTER);

static void
camorama_filter_mono_weight_init(CamoramaFilterMonoWeight* self) {}

static void
camorama_filter_mono_weight_filter(CamoramaFilter* filter, unsigned char *image, int x, int y, int depth) {
    int i;
    int avg;
    for (i = 0; i < x * y; i++) {
        avg = (int) ((image[0] * 0.2125) + (image[1] * 0.7154) +
                     (image[2] * 0.0721));

        image[0] = avg;
        image[1] = avg;
        image[2] = avg;
        image += 3;             /* bump to next triplet */
    }
}

static void
camorama_filter_mono_weight_class_init(CamoramaFilterMonoWeightClass* self_class) {
	self_class->filter = camorama_filter_mono_weight_filter;
	self_class->name   = _("Monochrome (Weight)");
}

/* GType stuff for CamoramaFilterSobel */
typedef struct _CamoramaFilter      CamoramaFilterSobel;
typedef struct _CamoramaFilterClass CamoramaFilterSobelClass;

G_DEFINE_TYPE(CamoramaFilterSobel, camorama_filter_sobel, CAMORAMA_TYPE_FILTER);

static void
camorama_filter_sobel_init(CamoramaFilterSobel* self) {}

/* fix this at some point, very slow */
static void
camorama_filter_sobel_filter(CamoramaFilter* filter, unsigned char *image, int x, int y, int depth) {
    int i, j, grad[3];
    int deltaX[3], deltaY[3];
    int width = x * 3;
    guchar *image2;

    image2 = (guchar *) malloc (sizeof (guchar) * (x * y * 3));

    for (i = width; i < (y - 1) * width; i++) {
        for (j = 0; j <= 2; j++) {
            deltaX[j] =
                2 * image[i + 1] + image[i - width + 1] +
                image[i + width + 1] - 2 * image[i - 1] -
                image[i - width - 1] - image[i + width - 1];

            deltaY[j] =
                image[i - width - 1] + 2 * image[i -
                                                 width] +
                image[i - width + 1] - image[i + width -
                                             1] -
                2 * image[i + width] - image[i + width + 1];
            grad[j] = (abs (deltaX[j]) + abs (deltaY[j]));
            grad[j] = grad[j] / 5.66;   /* <<<<<------------------------ new line */
            if (grad[j] > 255) {
                grad[j] = 255;
            }
            image2[i + j] = (unsigned char) grad[j];
        }
    }

    memcpy (image, image2, (x * y * 3));
    free (image2);
}

static void
camorama_filter_sobel_class_init(CamoramaFilterSobelClass* self_class) {
	self_class->filter = camorama_filter_sobel_filter;
	// TRANSLATORS: http://en.wikipedia.org/wiki/Sobel
	self_class->name   = _("Sobel");
}

/* general filter initialization */
void
camorama_filters_init(void) {
	camorama_filter_color_get_type();
	camorama_filter_invert_get_type();
	camorama_filter_threshold_get_type();
	camorama_filter_threshold_channel_get_type();
	camorama_filter_mirror_get_type();
	camorama_filter_reichardt_get_type();
	camorama_filter_wacky_get_type();
	camorama_filter_smooth_get_type();
	camorama_filter_laplace_get_type();
	camorama_filter_mono_get_type();
	camorama_filter_mono_weight_get_type();
	camorama_filter_sobel_get_type();
}

Privacy Policy