extern DmtxPassFail dmtxDecodeGetPixelValue(DmtxDecode *dec, int x, int y, int channel, int *value) { int xUnscaled, yUnscaled; DmtxPassFail err; xUnscaled = x * dec->scale; yUnscaled = y * dec->scale; /* Remove spherical lens distortion */ /* int width, height; double radiusPow2, radiusPow4; double factor; DmtxVector2 pointShifted; DmtxVector2 correctedPoint; width = dmtxImageGetProp(img, DmtxPropWidth); height = dmtxImageGetProp(img, DmtxPropHeight); pointShifted.X = point.X - width/2.0; pointShifted.Y = point.Y - height/2.0; radiusPow2 = pointShifted.X * pointShifted.X + pointShifted.Y * pointShifted.Y; radiusPow4 = radiusPow2 * radiusPow2; factor = 1 + (k1 * radiusPow2) + (k2 * radiusPow4); correctedPoint.X = pointShifted.X * factor + width/2.0; correctedPoint.Y = pointShifted.Y * factor + height/2.0; return correctedPoint; */ err = dmtxImageGetPixelValue(dec->image, xUnscaled, yUnscaled, channel, value); return err; }
static PyObject * dmtx_encode(PyObject *self, PyObject *arglist, PyObject *kwargs) { const unsigned char *data; int count = 0; int data_size = 0; int module_size = DmtxUndefined; int margin_size = DmtxUndefined; int scheme = DmtxUndefined; int shape = DmtxUndefined; PyObject *plotter = NULL; PyObject *start_cb = NULL; PyObject *finish_cb = NULL; PyObject *context = Py_None; PyObject *args; DmtxEncode *enc; int row, col; int rgb[3]; static char *kwlist[] = { "data", "module_size", "margin_size", "scheme", "shape", "plotter", "start", "finish", "context", NULL }; /* Parse out the options which are applicable */ PyObject *filtered_kwargs; filtered_kwargs = PyDict_New(); count = 1; /* Skip the first keyword as it is sent in arglist */ while(kwlist[count]){ if(PyDict_GetItemString(kwargs, kwlist[count])) { PyDict_SetItemString(filtered_kwargs, kwlist[count], PyDict_GetItemString(kwargs, kwlist[count])); } count++; } if(!PyArg_ParseTupleAndKeywords(arglist, filtered_kwargs, "s#iiii|OOOO", kwlist, &data, &data_size, &module_size, &margin_size, &scheme, &shape, &plotter, &start_cb, &finish_cb, &context)) return NULL; Py_INCREF(context); /* Plotter is required, and must be callable */ if(plotter == NULL || !PyCallable_Check(plotter)) { PyErr_SetString(PyExc_TypeError, "plotter must be callable"); return NULL; } enc = dmtxEncodeCreate(); if(enc == NULL) return NULL; dmtxEncodeSetProp(enc, DmtxPropPixelPacking, DmtxPack24bppRGB); dmtxEncodeSetProp(enc, DmtxPropImageFlip, DmtxFlipNone); if(scheme != DmtxUndefined) dmtxEncodeSetProp(enc, DmtxPropScheme, scheme); if(shape != DmtxUndefined) dmtxEncodeSetProp(enc, DmtxPropSizeRequest, shape); if(margin_size != DmtxUndefined) dmtxEncodeSetProp(enc, DmtxPropMarginSize, margin_size); if(module_size != DmtxUndefined) dmtxEncodeSetProp(enc, DmtxPropModuleSize, module_size); dmtxEncodeDataMatrix(enc, data_size, (unsigned char *)data); if((start_cb != NULL) && PyCallable_Check(start_cb)) { args = Py_BuildValue("(iiO)", enc->image->width, enc->image->height, context); (void)PyEval_CallObject(start_cb, args); Py_DECREF(args); } for(row = 0; row < enc->image->height; row++) { for(col = 0; col < enc->image->width; col++) { dmtxImageGetPixelValue(enc->image, col, row, 0, &rgb[0]); dmtxImageGetPixelValue(enc->image, col, row, 1, &rgb[1]); dmtxImageGetPixelValue(enc->image, col, row, 2, &rgb[2]); args = Py_BuildValue("(ii(iii)O)", col, row, rgb[0], rgb[1], rgb[2], context); (void)PyEval_CallObject(plotter, args); Py_DECREF(args); } } if((finish_cb != NULL) && PyCallable_Check(finish_cb)) { args = Py_BuildValue("(O)", context); (void)PyEval_CallObject(finish_cb, args); Py_DECREF(args); } dmtxEncodeDestroy(&enc); Py_DECREF(context); return Py_None; }
void renderCodeDatamatrix(OROPage *page, const QRectF &qrect, const QString &qstr, ORBarcodeData * bc) { //5 pixel par carré //qreal pix = 5; //lecture du type de datamatrix QRegExp regex("[a-zA-Z]{10}_([0-9]{1,2})_([LCR]{1})"); regex.indexIn(bc->format); int type = regex.cap(1).toInt(); QString align = regex.cap(2); size_t width, height, bytesPerPixel; //pointer declaration unsigned char *pxl = NULL; DmtxEncode *enc = NULL; DmtxImage *img = NULL; ORORect *rect = NULL; int valeur = 0; /* 1) ENCODE a new Data Matrix barcode image (in memory only) */ enc = dmtxEncodeCreate(); //see DmtxSymbolSize in dmtx.h for more details enc->sizeIdxRequest = type; enc->marginSize = 0; //number of pixel for one square enc->moduleSize = 1; try { //assert(enc != NULL); dmtxEncodeDataMatrix(enc, qstr.size(), (unsigned char*)qstr.toStdString().c_str()); /* 2) COPY the new image data before releasing encoding memory */ width = dmtxImageGetProp(enc->image, DmtxPropWidth); height = dmtxImageGetProp(enc->image, DmtxPropHeight); bytesPerPixel = dmtxImageGetProp(enc->image, DmtxPropBytesPerPixel); if(width > 1000000000) { throw std::runtime_error("Code is to big for the Datamatrix"); } pxl = (unsigned char *)malloc(width * height * bytesPerPixel); //assert(pxl != NULL); memcpy(pxl, enc->image->pxl, width * height * bytesPerPixel); dmtxEncodeDestroy(&enc); /* 3) DECODE the Data Matrix barcode from the copied image */ img = dmtxImageCreate(pxl, width, height, DmtxPack24bppRGB); QPen pen(Qt::NoPen); QBrush brush(QColor("black")); qreal Xo = 0; qreal Yo = 0; //length of square qreal pas = 0; datamatrixGeometry(align,qrect,img,&Xo,&Yo,&pas); //draw the datamatrix for(int y = 0; y < img->height; y++) { for(int x = 0; x < img->width; x++) { dmtxImageGetPixelValue(img,x,y,0,&valeur); rect = new ORORect(bc); rect->setPen(pen); if(valeur == 0) { brush.setColor(Qt::black); rect->setBrush(brush); rect->setRect(QRectF( Xo + x*pas, Yo - y*pas, pas, pas)); rect->setRotationAxis(qrect.topLeft()); page->addPrimitive(rect); } } delete rect; } //memory cleanning free(pxl); dmtxEncodeDestroy(&enc); dmtxImageDestroy(&img); } catch(...) { //there is a problem with the datamatrix //RR is printed printRR(page,bc,qrect); //memory cleaning if(rect != NULL) { delete rect; } if(enc != NULL) { dmtxEncodeDestroy(&enc); } if(img != NULL) { dmtxImageDestroy(&img); } if(pxl!=NULL) { free(pxl); } } }