| 1 |
|
| #ifdef __cplusplus |
| 2 |
|
| extern "C" { |
| 3 |
|
| #endif |
| 4 |
|
| #include "EXTERN.h" |
| 5 |
|
| #include "perl.h" |
| 6 |
|
| #include "XSUB.h" |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| #ifdef __cplusplus |
| 9 |
|
| } |
| 10 |
|
| #endif |
| 11 |
|
| |
| 12 |
|
| #include "imext.h" |
| 13 |
|
| #include "imperl.h" |
| 14 |
|
| #include "qrencode.h" |
| 15 |
|
| |
| 16 |
|
| #ifdef UNDER_LIBQRENCODE_1_0_2 |
| 17 |
|
| QRcode *encode(const char *text, |
| 18 |
|
| int version, |
| 19 |
|
| QRecLevel level, |
| 20 |
|
| QRencodeMode mode, |
| 21 |
|
| int casesensitive) |
| 22 |
|
| { |
| 23 |
|
| QRcode *code; |
| 24 |
|
| |
| 25 |
|
| if(casesensitive) { |
| 26 |
|
| code = QRcode_encodeStringCase(text, version, level); |
| 27 |
|
| } else { |
| 28 |
|
| code = QRcode_encodeString(text, version, level, mode); |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
|
| return code; |
| 32 |
|
| } |
| 33 |
|
| #else |
| 34 |
0 |
| QRcode *encode(const char *text, |
| 35 |
|
| int version, |
| 36 |
|
| QRecLevel level, |
| 37 |
|
| QRencodeMode mode, |
| 38 |
|
| int casesensitive) |
| 39 |
|
| { |
| 40 |
0 |
| QRcode *code = QRcode_encodeString(text, version, level, mode, casesensitive); |
| 41 |
0 |
| return code; |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
6 |
| QRcode *encode_8bit(const char *text, |
| 45 |
|
| int version, |
| 46 |
|
| QRecLevel level) |
| 47 |
|
| { |
| 48 |
6 |
| QRcode *code = QRcode_encodeString8bit(text, version, level); |
| 49 |
6 |
| return code; |
| 50 |
|
| } |
| 51 |
|
| #endif |
| 52 |
|
| |
| 53 |
6 |
| void generate(i_img *im, |
| 54 |
|
| QRcode *qrcode, |
| 55 |
|
| int size, |
| 56 |
|
| int margin, |
| 57 |
|
| i_color *lightcolor, |
| 58 |
|
| i_color *darkcolor) |
| 59 |
|
| { |
| 60 |
|
| unsigned char *p, *q; |
| 61 |
|
| int x, y; |
| 62 |
|
| int base_y; |
| 63 |
|
| |
| 64 |
|
| /* top margin */ |
| 65 |
6 |
| base_y = 0; |
| 66 |
22 |
▶ | for(y=base_y; y<base_y+margin; y++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(y=base_y; y<base_y+margin; y++) { |
16 |
6 |
22 |
|
| 67 |
656 |
▶ | for(x=0; x<qrcode->width + margin * 2; x++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(x=0; x<qrcode->width + margin * 2; x++) { |
640 |
16 |
656 |
|
| 68 |
640 |
| i_box_filled(im, |
| 69 |
|
| x*size, y*size, x*size + size, y*size + size, |
| 70 |
|
| lightcolor); |
| 71 |
|
| } |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
|
| /* data */ |
| 75 |
6 |
| p = qrcode->data; |
| 76 |
6 |
| q = p; |
| 77 |
6 |
| base_y = margin; |
| 78 |
212 |
▶ | for(y=base_y; y<base_y+qrcode->width; y++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(y=base_y; y<base_y+qrcode->width; y++) { |
206 |
6 |
212 |
|
| 79 |
750 |
▶ | for (x=0; x<margin; x++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x=0; x<margin; x++) { |
544 |
206 |
750 |
|
| 80 |
544 |
| i_box_filled(im, |
| 81 |
|
| x*size, y*size, x*size + size, y*size + size, |
| 82 |
|
| lightcolor); |
| 83 |
|
| } |
| 84 |
8356 |
▶ | for(x=margin; x<margin + qrcode->width; x++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(x=margin; x<margin + qrcode->width; x++) { |
8150 |
206 |
8356 |
|
| 85 |
8150 |
▶ | i_box_filled(im, |
Branch: 1 branch
| Branch | True | False | Total |
| i_box_filled(im, |
4206 |
3944 |
8150 |
|
| 86 |
|
| x*size, y*size, x*size + size, y*size + size, |
| 87 |
|
| (*q & 1) ? darkcolor : lightcolor); |
| 88 |
8150 |
| q++; |
| 89 |
|
| } |
| 90 |
750 |
▶ | for (x=margin + qrcode->width; x< qrcode->width + margin * 2; x++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x=margin + qrcode->width; x< qrcode->width + margin * 2; x++) { |
544 |
206 |
750 |
|
| 91 |
544 |
| i_box_filled(im, |
| 92 |
|
| x*size, y*size, x*size + size, y*size + size, |
| 93 |
|
| lightcolor); |
| 94 |
|
| } |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
|
| /* bottom margin */ |
| 98 |
6 |
| base_y = qrcode->width + margin; |
| 99 |
22 |
▶ | for(y=base_y; y<base_y + margin; y++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(y=base_y; y<base_y + margin; y++) { |
16 |
6 |
22 |
|
| 100 |
656 |
▶ | for(x=0; x<qrcode->width + margin * 2; x++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(x=0; x<qrcode->width + margin * 2; x++) { |
640 |
16 |
656 |
|
| 101 |
640 |
| i_box_filled(im, x*size, y*size, x*size + size, y*size + size - 1, lightcolor); |
| 102 |
|
| } |
| 103 |
|
| } |
| 104 |
6 |
| } |
| 105 |
|
| |
| 106 |
|
| DEFINE_IMAGER_CALLBACKS; |
| 107 |
|
| |
| 108 |
8 |
| i_img *_plot(char* text, HV *hv) |
| 109 |
|
| { |
| 110 |
|
| i_img* im; |
| 111 |
|
| QRcode *qrcode; |
| 112 |
|
| SV** svp; |
| 113 |
|
| STRLEN len; |
| 114 |
|
| char* ptr; |
| 115 |
8 |
| int size = 3; |
| 116 |
8 |
| int margin = 4; |
| 117 |
8 |
| int version = 0; |
| 118 |
8 |
| int casesensitive = 0; |
| 119 |
8 |
| QRencodeMode mode = QR_MODE_8; |
| 120 |
8 |
| QRecLevel level = QR_ECLEVEL_L; |
| 121 |
|
| i_color lightcolor, darkcolor; |
| 122 |
|
| |
| 123 |
8 |
▶ | if ((svp = hv_fetch(hv, "size", 4, 0)) && *svp && SvOK(*svp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hv, "size", 4, 0)) && *svp && SvOK(*svp)) { |
6 |
2 |
8 |
| if ((svp = hv_fetch(hv, "size", 4, 0)) && *svp && SvOK(*svp)) { |
6 |
0 |
6 |
| if ((svp = hv_fetch(hv, "size", 4, 0)) && *svp && SvOK(*svp)) { |
6 |
0 |
6 |
|
| 124 |
12 |
| ptr = SvPV(*svp, len); |
| 125 |
|
| if (ptr >= 0) |
| 126 |
6 |
| size = atoi(ptr); |
| 127 |
|
| } |
| 128 |
8 |
▶ | if ((svp = hv_fetch(hv, "margin", 6, 0)) && *svp && SvOK(*svp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hv, "margin", 6, 0)) && *svp && SvOK(*svp)) { |
6 |
2 |
8 |
| if ((svp = hv_fetch(hv, "margin", 6, 0)) && *svp && SvOK(*svp)) { |
6 |
0 |
6 |
| if ((svp = hv_fetch(hv, "margin", 6, 0)) && *svp && SvOK(*svp)) { |
6 |
0 |
6 |
|
| 129 |
12 |
| ptr = SvPV(*svp, len); |
| 130 |
|
| if (ptr >= 0) |
| 131 |
6 |
| margin = atoi(ptr); |
| 132 |
|
| } |
| 133 |
8 |
▶ | if ((svp = hv_fetch(hv, "level", 5, 0)) && *svp && SvOK(*svp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hv, "level", 5, 0)) && *svp && SvOK(*svp)) { |
6 |
2 |
8 |
| if ((svp = hv_fetch(hv, "level", 5, 0)) && *svp && SvOK(*svp)) { |
6 |
0 |
6 |
| if ((svp = hv_fetch(hv, "level", 5, 0)) && *svp && SvOK(*svp)) { |
6 |
0 |
6 |
|
| 134 |
6 |
| ptr = SvPV(*svp, len); |
| 135 |
6 |
| switch (*ptr) { |
| 136 |
0 |
| case 'l': |
| 137 |
|
| case 'L': |
| 138 |
0 |
| level = QR_ECLEVEL_L; |
| 139 |
0 |
| break; |
| 140 |
6 |
| case 'm': |
| 141 |
|
| case 'M': |
| 142 |
6 |
| level = QR_ECLEVEL_M; |
| 143 |
6 |
| break; |
| 144 |
0 |
| case 'q': |
| 145 |
|
| case 'Q': |
| 146 |
0 |
| level = QR_ECLEVEL_Q; |
| 147 |
0 |
| break; |
| 148 |
0 |
| case 'h': |
| 149 |
|
| case 'H': |
| 150 |
0 |
| level = QR_ECLEVEL_H; |
| 151 |
0 |
| break; |
| 152 |
0 |
| default: |
| 153 |
0 |
| level = QR_ECLEVEL_L; |
| 154 |
|
| } |
| 155 |
|
| } |
| 156 |
8 |
▶ | if ((svp = hv_fetch(hv, "version", 7, 0)) && *svp && SvOK(*svp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hv, "version", 7, 0)) && *svp && SvOK(*svp)) { |
6 |
2 |
8 |
| if ((svp = hv_fetch(hv, "version", 7, 0)) && *svp && SvOK(*svp)) { |
6 |
0 |
6 |
| if ((svp = hv_fetch(hv, "version", 7, 0)) && *svp && SvOK(*svp)) { |
6 |
0 |
6 |
|
| 157 |
12 |
| ptr = SvPV(*svp, len); |
| 158 |
|
| if (ptr >= 0) |
| 159 |
6 |
| version = atoi(ptr); |
| 160 |
|
| } |
| 161 |
8 |
▶ | if ((svp = hv_fetch(hv, "mode", 4, 0)) && *svp && SvOK(*svp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hv, "mode", 4, 0)) && *svp && SvOK(*svp)) { |
4 |
4 |
8 |
| if ((svp = hv_fetch(hv, "mode", 4, 0)) && *svp && SvOK(*svp)) { |
4 |
0 |
4 |
| if ((svp = hv_fetch(hv, "mode", 4, 0)) && *svp && SvOK(*svp)) { |
4 |
0 |
4 |
|
| 162 |
4 |
| ptr = SvPV(*svp, len); |
| 163 |
4 |
▶ | if (strcmp(ptr, "numerical") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(ptr, "numerical") == 0) { |
0 |
4 |
4 |
|
| 164 |
0 |
| mode = QR_MODE_NUM; |
| 165 |
|
| } |
| 166 |
4 |
▶ | else if (strcmp(ptr, "alpha-numerical") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(ptr, "alpha-numerical") == 0) { |
0 |
4 |
4 |
|
| 167 |
0 |
| mode = QR_MODE_AN; |
| 168 |
|
| } |
| 169 |
4 |
▶ | else if (strcmp(ptr, "8-bit") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(ptr, "8-bit") == 0) { |
2 |
2 |
4 |
|
| 170 |
2 |
| mode = QR_MODE_8; |
| 171 |
|
| } |
| 172 |
2 |
▶ | else if (strcmp(ptr, "kanji") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(ptr, "kanji") == 0) { |
0 |
2 |
2 |
|
| 173 |
0 |
| mode = QR_MODE_KANJI; |
| 174 |
|
| } |
| 175 |
|
| else { |
| 176 |
2 |
| croak("Invalid mode: XS error"); |
| 177 |
|
| } |
| 178 |
|
| } |
| 179 |
6 |
▶ | if ((svp = hv_fetch(hv, "casesensitive", 13, 0)) && *svp) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hv, "casesensitive", 13, 0)) && *svp) { |
4 |
2 |
6 |
| if ((svp = hv_fetch(hv, "casesensitive", 13, 0)) && *svp) { |
4 |
0 |
4 |
|
| 180 |
4 |
| casesensitive = SvTRUE(*svp); |
| 181 |
|
| } |
| 182 |
6 |
▶ | if ((svp = hv_fetch(hv, "lightcolor", 10, 0)) && *svp |
Branch: 2 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hv, "lightcolor", 10, 0)) && *svp |
4 |
2 |
6 |
| if ((svp = hv_fetch(hv, "lightcolor", 10, 0)) && *svp |
4 |
0 |
4 |
|
| 183 |
4 |
▶ | && SvOK(*svp) && sv_derived_from(*svp, "Imager::Color")) { |
Branch: 2 branches
| Branch | True | False | Total |
| && SvOK(*svp) && sv_derived_from(*svp, "Imager::Color")) { |
4 |
0 |
4 |
| && SvOK(*svp) && sv_derived_from(*svp, "Imager::Color")) { |
4 |
0 |
4 |
|
| 184 |
4 |
| lightcolor = *INT2PTR(i_color *, SvIV((SV *)SvRV(*svp))); |
| 185 |
|
| } |
| 186 |
|
| else { /* white */ |
| 187 |
2 |
| lightcolor.rgba.r = 255; |
| 188 |
2 |
| lightcolor.rgba.g = 255; |
| 189 |
2 |
| lightcolor.rgba.b = 255; |
| 190 |
2 |
| lightcolor.rgba.a = 255; |
| 191 |
|
| } |
| 192 |
6 |
▶ | if ((svp = hv_fetch(hv, "darkcolor", 9, 0)) && *svp && SvOK(*svp) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hv, "darkcolor", 9, 0)) && *svp && SvOK(*svp) |
4 |
2 |
6 |
| if ((svp = hv_fetch(hv, "darkcolor", 9, 0)) && *svp && SvOK(*svp) |
4 |
0 |
4 |
| if ((svp = hv_fetch(hv, "darkcolor", 9, 0)) && *svp && SvOK(*svp) |
4 |
0 |
4 |
|
| 193 |
4 |
▶ | && SvOK(*svp) && sv_derived_from(*svp, "Imager::Color")) { |
Branch: 2 branches
| Branch | True | False | Total |
| && SvOK(*svp) && sv_derived_from(*svp, "Imager::Color")) { |
4 |
0 |
4 |
| && SvOK(*svp) && sv_derived_from(*svp, "Imager::Color")) { |
4 |
0 |
4 |
|
| 194 |
4 |
| darkcolor = *INT2PTR(i_color *, SvIV((SV *)SvRV(*svp))); |
| 195 |
|
| } |
| 196 |
|
| else { /* black */ |
| 197 |
2 |
| darkcolor.rgba.r = 0; |
| 198 |
2 |
| darkcolor.rgba.g = 0; |
| 199 |
2 |
| darkcolor.rgba.b = 0; |
| 200 |
2 |
| darkcolor.rgba.a = 255; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
|
| #ifdef UNDER_LIBQRENCODE_1_0_2 |
| 204 |
|
| qrcode = encode(text, version, level, mode, casesensitive); |
| 205 |
|
| #else |
| 206 |
6 |
▶ | if (mode == QR_MODE_8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mode == QR_MODE_8) |
6 |
0 |
6 |
|
| 207 |
6 |
| qrcode = encode_8bit(text, version, level); |
| 208 |
|
| else |
| 209 |
0 |
| qrcode = encode(text, version, level, mode, casesensitive); |
| 210 |
|
| #endif |
| 211 |
6 |
▶ | if (qrcode == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (qrcode == NULL) |
0 |
6 |
6 |
|
| 212 |
0 |
| croak("Failed to encode the input data: XS error"); |
| 213 |
|
| |
| 214 |
6 |
| im = i_img_16_new( |
| 215 |
|
| (qrcode->width + margin * 2) * size, |
| 216 |
|
| (qrcode->width + margin * 2) * size, |
| 217 |
|
| 4 |
| 218 |
|
| ); |
| 219 |
6 |
| generate(im, qrcode, size, margin, &lightcolor, &darkcolor); |
| 220 |
6 |
| QRcode_free(qrcode); |
| 221 |
|
| |
| 222 |
6 |
| return im; |
| 223 |
|
| } |
| 224 |
|
| |
| 225 |
|
| MODULE = Imager::QRCode PACKAGE = Imager::QRCode |
| 226 |
|
| |
| 227 |
|
| PROTOTYPES: ENABLE |
| 228 |
|
| |
| 229 |
|
| Imager::ImgRaw |
| 230 |
|
| _plot(text, hv) |
| 231 |
|
| char *text |
| 232 |
|
| HV *hv |
| 233 |
|
| |
| 234 |
|
| BOOT: |
| 235 |
3 |
▶ | PERL_INITIALIZE_IMAGER_CALLBACKS; |
Branch: 3 branches
| Branch | True | False | Total |
| PERL_INITIALIZE_IMAGER_CALLBACKS; |
0 |
3 |
3 |
| PERL_INITIALIZE_IMAGER_CALLBACKS; |
0 |
3 |
3 |
| PERL_INITIALIZE_IMAGER_CALLBACKS; |
0 |
3 |
3 |
|