Class: Quirc::Decoder

Inherits:
Object
  • Object
show all
Defined in:
ext/quirc/quirc.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'ext/quirc/quirc.c', line 18

static VALUE decoder_m_initialize(VALUE self, VALUE width, VALUE height) {
    struct quirc *qr;

    Data_Get_Struct(self, struct quirc, qr);
    if (quirc_resize(qr, NUM2INT(width), NUM2INT(height)) < 0) {
        rb_raise(rb_const_get(rb_cObject, rb_intern("NoMemoryError")), "Could not allocate memory");
    }
    rb_iv_set(self, "@width", width);
    rb_iv_set(self, "@height", height);

    return Qnil;
}

Instance Attribute Details

#heightObject (readonly)

#widthObject (readonly)

Instance Method Details

#decode(image) ⇒ Object



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
# File 'ext/quirc/quirc.c', line 31

static VALUE decoder_m_decode(VALUE self, VALUE image) {
    struct quirc *qr;
    int width, height, size;
    uint8_t *buffer;
    int num_codes;
    VALUE results;

    Check_Type(image, T_STRING);
    Data_Get_Struct(self, struct quirc, qr);

    buffer = quirc_begin(qr, &width, &height);
    size = width * height;
    if (RSTRING_LEN(image) != size) {
        rb_raise(rb_const_get(rb_cObject, rb_intern("ArgumentError")), "Decoder is allocated for %dx%d images", width, height);
    }
    memcpy(buffer, StringValuePtr(image), size);
    quirc_end(qr);

    num_codes = quirc_count(qr);
    results = rb_ary_new2(num_codes);
    for (int i = 0; i < num_codes; i++) {
        struct quirc_code code;
        struct quirc_data data;
        quirc_decode_error_t err;
        VALUE item;

        quirc_extract(qr, i, &code);
        err = quirc_decode(&code, &data);
        if (err) continue;

        item = rb_class_new_instance(0, NULL, cResult);
        rb_iv_set(item, "@version", INT2FIX(data.version));
        rb_iv_set(item, "@ecc_level", INT2FIX(data.ecc_level));
        rb_iv_set(item, "@mask", INT2FIX(data.mask));
        rb_iv_set(item, "@data_type", INT2FIX(data.data_type));
        rb_iv_set(item, "@payload", rb_str_freeze(rb_str_new((const char *) data.payload, data.payload_len)));
        rb_iv_set(item, "@eci", INT2NUM(data.eci));
        rb_ary_push(results, item);
    }

    return results;
}