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
|
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#ifdef __cplusplus
}
#endif
#include <imext.h>
#include <imperl.h>
#include <qrencode.h>
#ifdef UNDER_LIBQRENCODE_1_0_2
QRcode *encode(const char *text,
int version,
QRecLevel level,
QRencodeMode mode,
int casesensitive)
{
QRcode *code;
if(casesensitive) {
code = QRcode_encodeStringCase(text, version, level);
} else {
code = QRcode_encodeString(text, version, level, mode);
}
return code;
}
#else
QRcode *encode(const char *text,
int version,
QRecLevel level,
QRencodeMode mode,
int casesensitive)
{
QRcode *code = QRcode_encodeString(text, version, level, mode, casesensitive);
return code;
}
QRcode *encode_8bit(const char *text,
int version,
QRecLevel level)
{
QRcode *code = QRcode_encodeString8bit(text, version, level);
return code;
}
#endif
void generate(i_img *im,
QRcode *qrcode,
int size,
int margin,
i_color *lightcolor,
i_color *darkcolor)
{
unsigned char *p, *q;
int x, y;
int base_y;
/* top margin */
base_y = 0;
for(y=base_y; y<base_y+margin; y++) {
for(x=0; x<qrcode->width + margin * 2; x++) {
i_box_filled(im,
x*size, y*size, x*size + size, y*size + size,
lightcolor);
}
}
/* data */
p = qrcode->data;
q = p;
base_y = margin;
for(y=base_y; y<base_y+qrcode->width; y++) {
for (x=0; x<margin; x++) {
i_box_filled(im,
x*size, y*size, x*size + size, y*size + size,
lightcolor);
}
for(x=margin; x<margin + qrcode->width; x++) {
i_box_filled(im,
x*size, y*size, x*size + size, y*size + size,
(*q & 1) ? darkcolor : lightcolor);
q++;
}
for (x=margin + qrcode->width; x< qrcode->width + margin * 2; x++) {
i_box_filled(im,
x*size, y*size, x*size + size, y*size + size,
lightcolor);
}
}
/* bottom margin */
base_y = qrcode->width + margin;
for(y=base_y; y<base_y + margin; y++) {
for(x=0; x<qrcode->width + margin * 2; x++) {
i_box_filled(im, x*size, y*size, x*size + size, y*size + size - 1, lightcolor);
}
}
}
DEFINE_IMAGER_CALLBACKS;
i_img *_plot(char* text, HV *hv)
{
i_img* im;
QRcode *qrcode;
SV** svp;
STRLEN len;
char* ptr;
int size = 3;
int margin = 4;
int version = 0;
int casesensitive = 0;
QRencodeMode mode = QR_MODE_8;
QRecLevel level = QR_ECLEVEL_L;
i_color lightcolor, darkcolor;
if ((svp = hv_fetch(hv, "size", 4, 0)) && *svp && SvOK(*svp)) {
ptr = SvPV(*svp, len);
if (ptr >= 0)
size = atoi(ptr);
}
if ((svp = hv_fetch(hv, "margin", 6, 0)) && *svp && SvOK(*svp)) {
ptr = SvPV(*svp, len);
if (ptr >= 0)
margin = atoi(ptr);
}
if ((svp = hv_fetch(hv, "level", 5, 0)) && *svp && SvOK(*svp)) {
ptr = SvPV(*svp, len);
switch (*ptr) {
case 'l':
case 'L':
level = QR_ECLEVEL_L;
break;
case 'm':
case 'M':
level = QR_ECLEVEL_M;
break;
case 'q':
case 'Q':
level = QR_ECLEVEL_Q;
break;
case 'h':
case 'H':
level = QR_ECLEVEL_H;
break;
default:
level = QR_ECLEVEL_L;
}
}
if ((svp = hv_fetch(hv, "version", 7, 0)) && *svp && SvOK(*svp)) {
ptr = SvPV(*svp, len);
if (ptr >= 0)
version = atoi(ptr);
}
if ((svp = hv_fetch(hv, "mode", 4, 0)) && *svp && SvOK(*svp)) {
ptr = SvPV(*svp, len);
if (strcmp(ptr, "numerical") == 0) {
mode = QR_MODE_NUM;
}
else if (strcmp(ptr, "alpha-numerical") == 0) {
mode = QR_MODE_AN;
}
else if (strcmp(ptr, "8-bit") == 0) {
mode = QR_MODE_8;
}
else if (strcmp(ptr, "kanji") == 0) {
mode = QR_MODE_KANJI;
}
else {
croak("Invalid mode: XS error");
}
}
if ((svp = hv_fetch(hv, "casesensitive", 13, 0)) && *svp) {
casesensitive = SvTRUE(*svp);
}
if ((svp = hv_fetch(hv, "lightcolor", 10, 0)) && *svp
&& SvOK(*svp) && sv_derived_from(*svp, "Imager::Color")) {
lightcolor = *INT2PTR(i_color *, SvIV((SV *)SvRV(*svp)));
}
else { /* white */
lightcolor.rgba.r = 255;
lightcolor.rgba.g = 255;
lightcolor.rgba.b = 255;
lightcolor.rgba.a = 255;
}
if ((svp = hv_fetch(hv, "darkcolor", 9, 0)) && *svp && SvOK(*svp)
&& SvOK(*svp) && sv_derived_from(*svp, "Imager::Color")) {
darkcolor = *INT2PTR(i_color *, SvIV((SV *)SvRV(*svp)));
}
else { /* black */
darkcolor.rgba.r = 0;
darkcolor.rgba.g = 0;
darkcolor.rgba.b = 0;
darkcolor.rgba.a = 255;
}
#ifdef UNDER_LIBQRENCODE_1_0_2
qrcode = encode(text, version, level, mode, casesensitive);
#else
if (mode == QR_MODE_8)
qrcode = encode_8bit(text, version, level);
else
qrcode = encode(text, version, level, mode, casesensitive);
#endif
if (qrcode == NULL)
croak("Failed to encode the input data: XS error");
im = i_img_16_new(
(qrcode->width + margin * 2) * size,
(qrcode->width + margin * 2) * size,
4
);
generate(im, qrcode, size, margin, &lightcolor, &darkcolor);
QRcode_free(qrcode);
return im;
}
MODULE = Imager::QRCode PACKAGE = Imager::QRCode
PROTOTYPES: ENABLE
Imager::ImgRaw
_plot(text, hv)
char *text
HV *hv
BOOT:
PERL_INITIALIZE_IMAGER_CALLBACKS;
|