File: caca-dither.c

package info (click to toggle)
libcaca 0.99.beta20-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,264 kB
  • sloc: ansic: 25,091; php: 2,763; python: 2,637; cs: 1,213; cpp: 1,127; java: 916; objc: 836; makefile: 543; perl: 505; sh: 472; asm: 297; ruby: 215; xml: 33
file content (209 lines) | stat: -rw-r--r-- 6,799 bytes parent folder | download | duplicates (6)
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
/*
 *  libcaca Ruby bindings
 *  Copyright (c) 2007-2012 Pascal Terjan <pterjan@linuxfr.org>
 *
 *  This library is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What the Fuck You Want
 *  to Public License, Version 2, as published by Sam Hocevar. See
 *  http://www.wtfpl.net/ for more details.
 */

#include <ruby.h>
#include <caca.h>
#include <errno.h>
#include "common.h"

VALUE cDither;

void dither_free(void *dither)
{
    caca_free_dither((caca_dither_t *)dither);
}

static VALUE dither_alloc(VALUE klass)
{
    VALUE obj;
    obj = Data_Wrap_Struct(klass, 0, dither_free, NULL);
    return obj;
}

static VALUE dither_initialize(VALUE self, VALUE bpp, VALUE w, VALUE h, VALUE pitch, VALUE rmask, VALUE gmask, VALUE bmask, VALUE amask)
{
    caca_dither_t *dither;

    dither = caca_create_dither(NUM2UINT(bpp), NUM2UINT(w), NUM2UINT(h), NUM2UINT(pitch), NUM2ULONG(rmask), NUM2ULONG(gmask), NUM2ULONG(bmask), NUM2ULONG(amask));
    if(dither == NULL)
    {
        rb_raise(rb_eRuntimeError, "%s", strerror(errno));
    }
    _SELF = dither;
    return self;
}

static VALUE set_dither_palette(VALUE self, VALUE palette)
{
    int i;
    unsigned int *red, *blue, *green, *alpha;
    VALUE v, r, g, b, a;
    int error = 0;

    if(RARRAY_LEN(palette) != 256)
    {
        rb_raise(rb_eArgError, "Palette must contain 256 elements");
    }

    red = ALLOC_N(unsigned int, 256);
    if(!red)
        rb_raise(rb_eNoMemError,"Out of memory");

    green = ALLOC_N(unsigned int, 256);
    if(!green)
    {
        free(red);
        rb_raise(rb_eNoMemError,"Out of memory");
    }

    blue = ALLOC_N(unsigned int, 256);
    if(!blue)
    {
        free(red);
        free(green);
        rb_raise(rb_eNoMemError,"Out of memory");
    }

    alpha = ALLOC_N(unsigned int, 256);
    if(!alpha)
    {
        free(red);
        free(green);
        free(blue);
        rb_raise(rb_eNoMemError,"Out of memory");
    }

    for(i=0; i<256; i++)
    {
        v = rb_ary_entry(palette, i);
        if((TYPE(v) == T_ARRAY) && (RARRAY_LEN(v) == 4))
        {
            r = rb_ary_entry(v,0);
            g = rb_ary_entry(v,1);
            b = rb_ary_entry(v,2);
            a = rb_ary_entry(v,3);
            if(rb_obj_is_kind_of(r, rb_cInteger) &&
               rb_obj_is_kind_of(g, rb_cInteger) &&
               rb_obj_is_kind_of(b, rb_cInteger) &&
               rb_obj_is_kind_of(a, rb_cInteger))
            {
                red[i]   = NUM2INT(r);
                green[i] = NUM2INT(g);
                blue[i]  = NUM2INT(b);
                alpha[i] = NUM2INT(a);
            } else
                error = 1;
        }
        else
            error = 1;
    }

    if(error)
    {
        free(red);
        free(green);
        free(blue);
        free(alpha);
        rb_raise(rb_eArgError, "Invalid palette");
    }

    if(caca_set_dither_palette(_SELF, red, green, blue, alpha)<0)
    {
        free(red);
        free(green);
        free(blue);
        free(alpha);
        rb_raise(rb_eRuntimeError, "%s", strerror(errno));
    }

    free(red);
    free(green);
    free(blue);
    free(alpha);

    return palette;
}

static VALUE set_dither_palette2(VALUE self, VALUE palette)
{
    set_dither_palette(self, palette);
    return self;
}

#define set_float(x)                                    \
static VALUE set_##x(VALUE self, VALUE x)               \
{                                                       \
    if(caca_set_dither_##x(_SELF, (float)NUM2DBL(x))<0) \
        rb_raise(rb_eRuntimeError, "%s", strerror(errno));    \
                                                        \
    return x;                                           \
}                                                       \
                                                        \
static VALUE set_##x##2(VALUE self, VALUE x)            \
{                                                       \
    set_##x(self, x);                                   \
    return self;                                        \
}

set_float(brightness)
set_float(gamma)
set_float(contrast)

#define get_set_str_from_list(x)                         \
get_double_list(dither_##x)                              \
static VALUE set_dither_##x(VALUE self, VALUE x)         \
{                                                        \
    if(caca_set_dither_##x(_SELF, StringValuePtr(x))<0)  \
    {                                                    \
        rb_raise(rb_eRuntimeError, "%s", strerror(errno));     \
    }                                                    \
    return x;                                            \
}                                                        \
                                                         \
static VALUE set_dither_##x##2(VALUE self, VALUE x)      \
{                                                        \
     set_dither_##x(self, x);                            \
     return self;                                        \
}

get_set_str_from_list(antialias)
get_set_str_from_list(color)
get_set_str_from_list(charset)
get_set_str_from_list(algorithm)

void Init_caca_dither(VALUE mCaca)
{
    cDither = rb_define_class_under(mCaca, "Dither", rb_cObject);
    rb_define_alloc_func(cDither, dither_alloc);

    rb_define_method(cDither, "initialize", dither_initialize, 8);
    rb_define_method(cDither, "palette=", set_dither_palette, 1);
    rb_define_method(cDither, "set_palette", set_dither_palette2, 1);
    rb_define_method(cDither, "brightness=", set_brightness, 1);
    rb_define_method(cDither, "set_brightness", set_brightness2, 1);
    rb_define_method(cDither, "gamma=", set_gamma, 1);
    rb_define_method(cDither, "set_gamma", set_gamma2, 1);
    rb_define_method(cDither, "contrast=", set_contrast, 1);
    rb_define_method(cDither, "set_contrast", set_contrast2, 1);
    rb_define_method(cDither, "antialias_list", dither_antialias_list, 0);
    rb_define_method(cDither, "antialias=", set_dither_antialias, 1);
    rb_define_method(cDither, "set_antialias", set_dither_antialias2, 1);
    rb_define_method(cDither, "color_list", dither_color_list, 0);
    rb_define_method(cDither, "color=", set_dither_color, 1);
    rb_define_method(cDither, "set_color", set_dither_color2, 1);
    rb_define_method(cDither, "charset_list", dither_charset_list, 0);
    rb_define_method(cDither, "charset=", set_dither_charset, 1);
    rb_define_method(cDither, "set_charset", set_dither_charset2, 1);
    rb_define_method(cDither, "algorithm_list", dither_algorithm_list, 0);
    rb_define_method(cDither, "algorithm=", set_dither_algorithm, 1);
    rb_define_method(cDither, "set_algorithm", set_dither_algorithm2, 1);
}