@@ -49,17 +49,22 @@ static PyObject* NvJpeg_decode(NvJpeg* Self, PyObject* Argvs)
4949{
5050 JpegCoder* m_handle = (JpegCoder*)Self->m_handle ;
5151
52+ Py_buffer pyBuf;
5253 unsigned char * jpegData;
5354 int len;
54- if (!PyArg_ParseTuple (Argvs, " y# " , &jpegData, &len )){
55+ if (!PyArg_ParseTuple (Argvs, " y* " , &pyBuf )){
5556 PyErr_SetString (PyExc_ValueError, " Parse the argument FAILED! You should jpegData byte string!" );
5657 return NULL ;
5758 }
59+ jpegData = (unsigned char *)pyBuf.buf ;
60+ len = pyBuf.len ;
5861 JpegCoderImage* img;
5962 try {
6063 m_handle->ensureThread (PyThread_get_thread_ident ());
6164 img = m_handle->decode ((const unsigned char *)jpegData, len);
65+ PyBuffer_Release (&pyBuf);
6266 }catch (JpegCoderError e){
67+ PyBuffer_Release (&pyBuf);
6368 PyErr_Format (PyExc_ValueError, " %s, Code: %d" , e.what (), e.code ());
6469 return NULL ;
6570 }
@@ -101,11 +106,14 @@ static PyObject* NvJpeg_encode(NvJpeg* Self, PyObject* Argvs)
101106
102107 PyObject* bytes = PyObject_CallMethod ((PyObject*)vecin, " tobytes" , NULL );
103108
104- int length;
109+ Py_buffer pyBuf;
110+
105111 unsigned char * buffer;
106- PyArg_Parse (bytes, " y#" , &buffer, &length);
112+ PyArg_Parse (bytes, " y*" , &pyBuf);
113+ buffer = (unsigned char *)pyBuf.buf ;
107114 auto img = new JpegCoderImage (PyArray_DIM (vecin, 1 ), PyArray_DIM (vecin, 0 ), 3 , JPEGCODER_CSS_444);
108115 img->fill (buffer);
116+ PyBuffer_Release (&pyBuf);
109117 Py_DECREF (bytes);
110118
111119 m_handle->ensureThread (PyThread_get_thread_ident ());
0 commit comments