Skip to content

Commit b3f6be1

Browse files
committed
repair a bug( for DecodeBuffer and StartVideoMode )
1 parent b674345 commit b3f6be1

File tree

1 file changed

+110
-118
lines changed

1 file changed

+110
-118
lines changed

src/dbr.c

Lines changed: 110 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,7 @@ decodeBuffer(PyObject *obj, PyObject *args)
13171317
nd = (int)(view->ndim);
13181318
int len = (int)(view->len);
13191319
int stride = (int)(view->strides[0]);
1320+
int channels = (int)(view->strides[1]);
13201321
int width = (int)(view->strides[0] / view->strides[1]);
13211322
int height = len / stride;
13221323
#else
@@ -1342,30 +1343,40 @@ decodeBuffer(PyObject *obj, PyObject *args)
13421343
char *buffer = (char *)pai->data; // The address of image data
13431344
int width = (int)pai->shape[1]; // image width
13441345
int height = (int)pai->shape[0]; // image height
1346+
int channels = (int)pai->shape[2];
13451347
int stride = (int)pai->strides[0]; // image stride
13461348
#endif
13471349

13481350
// Initialize Dynamsoft Barcode Reader
13491351
TextResultArray *pResults = NULL;
13501352

13511353
// Detect barcodes
1352-
ImagePixelFormat format = IPF_RGB_888;
1353-
1354-
if (width == stride)
1355-
{
1356-
format = IPF_GRAYSCALED;
1357-
}
1358-
else if (width * 3 == stride)
1359-
{
1360-
format = IPF_RGB_888;
1361-
}
1362-
else if (width * 4 == stride)
1363-
{
1364-
format = IPF_ARGB_8888;
1354+
ImagePixelFormat imagePixelFormat = IPF_RGB_888;
1355+
1356+
switch (channels)
1357+
{
1358+
case 1:
1359+
imagePixelFormat = IPF_GRAYSCALED;
1360+
break;
1361+
case 2:
1362+
imagePixelFormat = IPF_RGB_555;
1363+
break;
1364+
case 3:
1365+
imagePixelFormat = IPF_RGB_888;
1366+
break;
1367+
case 4:
1368+
imagePixelFormat = IPF_ARGB_8888;
1369+
break;
1370+
case 5:
1371+
imagePixelFormat = IPF_RGB_161616;
1372+
break;
1373+
case 6:
1374+
imagePixelFormat = IPF_ARGB_16161616;
1375+
break;
13651376
}
13661377

13671378
PyObject *list = NULL;
1368-
int ret = DBR_DecodeBuffer(self->hBarcode, buffer, width, height, stride, format, templateName ? templateName : "");
1379+
int ret = DBR_DecodeBuffer(self->hBarcode, buffer, width, height, stride, imagePixelFormat, templateName ? templateName : "");
13691380
if (ret)
13701381
{
13711382
printf("Detection error: %s\n", DBR_GetErrorString(ret));
@@ -1720,8 +1731,8 @@ startVideoMode(PyObject *obj, PyObject *args)
17201731
DynamsoftBarcodeReader *self = (DynamsoftBarcodeReader *)obj;
17211732

17221733
PyObject *callback = NULL;
1723-
int maxListLength, maxResultListLength, width, height, imageformat, iFormat, stride;
1724-
if (!PyArg_ParseTuple(args, "iiiiiiO", &maxListLength, &maxResultListLength, &width, &height, &imageformat, &iFormat, &callback))
1734+
int maxListLength, maxResultListLength, width, height, channels, stride, iFormat;
1735+
if (!PyArg_ParseTuple(args, "iiiiiiiO", &maxListLength, &maxResultListLength, &width, &height, &stride, &channels, &iFormat, &callback))
17251736
{
17261737
return NULL;
17271738
}
@@ -1740,22 +1751,33 @@ startVideoMode(PyObject *obj, PyObject *args)
17401751
self->py_callback = callback;
17411752
}
17421753

1743-
ImagePixelFormat format = IPF_RGB_888;
1754+
ImagePixelFormat imagePixelFormat = IPF_RGB_888;
17441755

1745-
if (imageformat == 0)
1746-
{
1747-
stride = width;
1748-
format = IPF_GRAYSCALED;
1749-
}
1750-
else
1756+
switch (channels)
17511757
{
1752-
stride = width * 3;
1753-
format = IPF_RGB_888;
1758+
case 1:
1759+
imagePixelFormat = IPF_GRAYSCALED;
1760+
break;
1761+
case 2:
1762+
imagePixelFormat = IPF_RGB_555;
1763+
break;
1764+
case 3:
1765+
imagePixelFormat = IPF_RGB_888;
1766+
break;
1767+
case 4:
1768+
imagePixelFormat = IPF_ARGB_8888;
1769+
break;
1770+
case 5:
1771+
imagePixelFormat = IPF_RGB_161616;
1772+
break;
1773+
case 6:
1774+
imagePixelFormat = IPF_ARGB_16161616;
1775+
break;
17541776
}
17551777

17561778
DBR_SetTextResultCallback(self->hBarcode, onResultCallback, self);
17571779

1758-
int ret = DBR_StartFrameDecoding(self->hBarcode, maxListLength, maxResultListLength, width, height, stride, format, "");
1780+
int ret = DBR_StartFrameDecoding(self->hBarcode, maxListLength, maxResultListLength, width, height, stride, imagePixelFormat, "");
17591781
return Py_BuildValue("i", ret);
17601782
}
17611783

@@ -1793,7 +1815,6 @@ appendVideoFrame(PyObject *obj, PyObject *args)
17931815
#if defined(IS_PY3K)
17941816
//Refer to numpy/core/src/multiarray/ctors.c
17951817
Py_buffer *view;
1796-
int nd;
17971818
PyObject *memoryview = PyMemoryView_FromObject(o);
17981819
if (memoryview == NULL)
17991820
{
@@ -1803,11 +1824,7 @@ appendVideoFrame(PyObject *obj, PyObject *args)
18031824

18041825
view = PyMemoryView_GET_BUFFER(memoryview);
18051826
unsigned char *buffer = (unsigned char *)(view->buf);
1806-
nd = (int)(view->ndim);
1807-
int len = (int)(view->len);
1808-
int stride = (int)(view->strides[0]);
1809-
int width = (int)(view->strides[0] / view->strides[1]);
1810-
int height = len / stride;
1827+
18111828
#else
18121829

18131830
PyObject *ao = PyObject_GetAttrString(o, "__array_struct__");
@@ -1829,29 +1846,8 @@ appendVideoFrame(PyObject *obj, PyObject *args)
18291846

18301847
// Get image information
18311848
unsigned char *buffer = (unsigned char *)pai->data; // The address of image data
1832-
int width = (int)pai->shape[1]; // image width
1833-
int height = (int)pai->shape[0]; // image height
1834-
int stride = (int)pai->strides[0]; // image stride
1835-
#endif
18361849

1837-
// Initialize Dynamsoft Barcode Reader
1838-
TextResultArray *pResults = NULL;
1839-
1840-
// Detect barcodes
1841-
ImagePixelFormat format = IPF_RGB_888;
1842-
1843-
if (width == stride)
1844-
{
1845-
format = IPF_GRAYSCALED;
1846-
}
1847-
else if (width == stride * 3)
1848-
{
1849-
format = IPF_RGB_888;
1850-
}
1851-
else if (width == stride * 4)
1852-
{
1853-
format = IPF_ARGB_8888;
1854-
}
1850+
#endif
18551851

18561852
int frameId = DBR_AppendFrame(self->hBarcode, buffer);
18571853
return 0;
@@ -2142,8 +2138,9 @@ static PyObject * DecodeBuffer(PyObject *obj, PyObject *args)
21422138

21432139
PyObject *o;
21442140
char *templateName = NULL;
2141+
int width, height, channels, stride;
21452142
// char *encoding = NULL;
2146-
if (!PyArg_ParseTuple(args, "O|s", &o, &templateName))
2143+
if (!PyArg_ParseTuple(args, "Oiiii|s", &o, &height, &width, &stride, &channels, &templateName))
21472144
return NULL;
21482145

21492146

@@ -2160,11 +2157,11 @@ static PyObject * DecodeBuffer(PyObject *obj, PyObject *args)
21602157

21612158
view = PyMemoryView_GET_BUFFER(memoryview);
21622159
char *buffer = (char *)(view->buf);
2163-
nd = (int)(view->ndim);
2164-
int len = (int)(view->len);
2165-
int stride = (int)(view->strides[0]);
2166-
int width = (int)(view->strides[0] / view->strides[1]);
2167-
int height = len / stride;
2160+
// nd = (int)(view->ndim);
2161+
// int len = (int)(view->len);
2162+
// int stride = (int)(view->strides[0]);
2163+
// int width = (int)(view->strides[0] / view->strides[1]);
2164+
// int height = len / stride;
21682165
#else
21692166

21702167
PyObject *ao = PyObject_GetAttrString(o, "__array_struct__");
@@ -2186,35 +2183,45 @@ static PyObject * DecodeBuffer(PyObject *obj, PyObject *args)
21862183

21872184
// Get image information
21882185
char *buffer = (char *)pai->data; // The address of image data
2189-
int width = (int)pai->shape[1]; // image width
2190-
int height = (int)pai->shape[0]; // image height
2191-
int stride = (int)pai->strides[0]; // image stride
2186+
// int width = (int)pai->shape[1]; // image width
2187+
// int height = (int)pai->shape[0]; // image height
2188+
// int channels = (int)pai->shape[2];
2189+
// int stride = (int)pai->strides[0]; // image stride
21922190
#endif
21932191

21942192
// Initialize Dynamsoft Barcode Reader
21952193
TextResultArray *pResults = NULL;
21962194

21972195
// Detect barcodes
2198-
ImagePixelFormat format = IPF_RGB_888;
2199-
2200-
if (width == stride)
2201-
{
2202-
format = IPF_GRAYSCALED;
2203-
}
2204-
else if (width * 3 == stride)
2205-
{
2206-
format = IPF_RGB_888;
2207-
}
2208-
else if (width * 4 == stride)
2209-
{
2210-
format = IPF_ARGB_8888;
2196+
ImagePixelFormat imagePixelFormat = IPF_RGB_888;
2197+
2198+
switch (channels)
2199+
{
2200+
case 1:
2201+
imagePixelFormat = IPF_GRAYSCALED;
2202+
break;
2203+
case 2:
2204+
imagePixelFormat = IPF_RGB_555;
2205+
break;
2206+
case 3:
2207+
imagePixelFormat = IPF_RGB_888;
2208+
break;
2209+
case 4:
2210+
imagePixelFormat = IPF_ARGB_8888;
2211+
break;
2212+
case 5:
2213+
imagePixelFormat = IPF_RGB_161616;
2214+
break;
2215+
case 6:
2216+
imagePixelFormat = IPF_ARGB_16161616;
2217+
break;
22112218
}
22122219

22132220
if(templateName == NULL)
22142221
{
22152222
templateName = "";
22162223
}
2217-
int ret = DBR_DecodeBuffer(self->hBarcode, buffer, width, height, stride, format, templateName);
2224+
int ret = DBR_DecodeBuffer(self->hBarcode, buffer, width, height, stride, imagePixelFormat, templateName);
22182225
if (ret)
22192226
{
22202227
printf("Detection error: %s\n", DBR_GetErrorString(ret));
@@ -2328,8 +2335,8 @@ static PyObject * StartVideoMode(PyObject *obj, PyObject *args)
23282335
DynamsoftBarcodeReader *self = (DynamsoftBarcodeReader *)obj;
23292336

23302337
PyObject *callback = NULL;
2331-
int maxListLength, maxResultListLength, width, height, imageformat, stride;
2332-
if (!PyArg_ParseTuple(args, "iiiiiO", &maxListLength, &maxResultListLength, &width, &height, &imageformat, &callback))
2338+
int maxListLength, maxResultListLength, width, height, channels, stride;
2339+
if (!PyArg_ParseTuple(args, "iiiiiiO", &maxListLength, &maxResultListLength, &width, &height, &stride, &channels, &callback))
23332340
{
23342341
return NULL;
23352342
}
@@ -2346,22 +2353,33 @@ static PyObject * StartVideoMode(PyObject *obj, PyObject *args)
23462353
self->py_callback = callback;
23472354
}
23482355

2349-
ImagePixelFormat format = IPF_RGB_888;
2356+
ImagePixelFormat imagePixelFormat = IPF_RGB_888;
23502357

2351-
if (imageformat == 0)
2358+
switch (channels)
23522359
{
2353-
stride = width;
2354-
format = IPF_GRAYSCALED;
2355-
}
2356-
else
2357-
{
2358-
stride = width * 3;
2359-
format = IPF_RGB_888;
2360+
case 1:
2361+
imagePixelFormat = IPF_GRAYSCALED;
2362+
break;
2363+
case 2:
2364+
imagePixelFormat = IPF_RGB_555;
2365+
break;
2366+
case 3:
2367+
imagePixelFormat = IPF_RGB_888;
2368+
break;
2369+
case 4:
2370+
imagePixelFormat = IPF_ARGB_8888;
2371+
break;
2372+
case 5:
2373+
imagePixelFormat = IPF_RGB_161616;
2374+
break;
2375+
case 6:
2376+
imagePixelFormat = IPF_ARGB_16161616;
2377+
break;
23602378
}
23612379

23622380
DBR_SetTextResultCallback(self->hBarcode, OnResultCallback, self);
23632381

2364-
int ret = DBR_StartFrameDecoding(self->hBarcode, maxListLength, maxResultListLength, width, height, stride, format, "");
2382+
int ret = DBR_StartFrameDecoding(self->hBarcode, maxListLength, maxResultListLength, width, height, stride, imagePixelFormat, "");
23652383
return Py_BuildValue("i", ret);
23662384
}
23672385

@@ -2389,7 +2407,6 @@ static PyObject * AppendVideoFrame(PyObject *obj, PyObject *args)
23892407
#if defined(IS_PY3K)
23902408
//Refer to numpy/core/src/multiarray/ctors.c
23912409
Py_buffer *view;
2392-
int nd;
23932410
PyObject *memoryview = PyMemoryView_FromObject(o);
23942411
if (memoryview == NULL)
23952412
{
@@ -2399,11 +2416,7 @@ static PyObject * AppendVideoFrame(PyObject *obj, PyObject *args)
23992416

24002417
view = PyMemoryView_GET_BUFFER(memoryview);
24012418
unsigned char *buffer = (unsigned char *)view->buf;
2402-
nd = (int)(view->ndim);
2403-
int len = (int)(view->len);
2404-
int stride = (int)(view->strides[0]);
2405-
int width = (int)(view->strides[0] / view->strides[1]);
2406-
int height = len / stride;
2419+
24072420
#else
24082421

24092422
PyObject *ao = PyObject_GetAttrString(o, "__array_struct__");
@@ -2425,29 +2438,8 @@ static PyObject * AppendVideoFrame(PyObject *obj, PyObject *args)
24252438

24262439
// Get image information
24272440
unsigned char *buffer = (unsigned char *)pai->data; // The address of image data
2428-
int width = (int)pai->shape[1]; // image width
2429-
int height = (int)pai->shape[0]; // image height
2430-
int stride = (int)pai->strides[0]; // image stride
2431-
#endif
2432-
2433-
// Initialize Dynamsoft Barcode Reader
2434-
TextResultArray *pResults = NULL;
2435-
2436-
// Detect barcodes
2437-
ImagePixelFormat format = IPF_RGB_888;
24382441

2439-
if (width == stride)
2440-
{
2441-
format = IPF_GRAYSCALED;
2442-
}
2443-
else if (width == stride * 3)
2444-
{
2445-
format = IPF_RGB_888;
2446-
}
2447-
else if (width == stride * 4)
2448-
{
2449-
format = IPF_ARGB_8888;
2450-
}
2442+
#endif
24512443

24522444
int frameId = DBR_AppendFrame(self->hBarcode, buffer);
24532445
return 0;
@@ -3293,4 +3285,4 @@ void initdbr(void)
32933285
#if defined(IS_PY3K)
32943286
return module;
32953287
#endif
3296-
}
3288+
}

0 commit comments

Comments
 (0)