@@ -34,28 +34,22 @@ typedef struct
3434
3535static NvJpegPythonHandle * __gllobal_NvJpegPython = NULL ;
3636
37- NvJpegPythonHandle * NvJpegPython_createHandle (){
37+ NvJpegPythonHandle * NvJpegPython_startUpEnv (){
3838 if (__gllobal_NvJpegPython == NULL ){
3939 __gllobal_NvJpegPython = (NvJpegPythonHandle * )malloc (sizeof (NvJpegPythonHandle ));
4040 nvjpegCreateSimple (& (__gllobal_NvJpegPython -> nv_handle ));
4141 nvjpegJpegStateCreate (__gllobal_NvJpegPython -> nv_handle , & (__gllobal_NvJpegPython -> nv_statue ));
4242 nvjpegEncoderStateCreate (__gllobal_NvJpegPython -> nv_handle , & (__gllobal_NvJpegPython -> nv_enc_state ), NULL );
4343 }
4444 return __gllobal_NvJpegPython ;
45- // NvJpegPythonHandle* handle = NULL;
46- // handle = (NvJpegPythonHandle*)malloc(sizeof(NvJpegPythonHandle));
47- // nvjpegCreateSimple(&(handle->nv_handle));
48- // nvjpegJpegStateCreate(handle->nv_handle, &(handle->nv_statue));
49- // nvjpegEncoderStateCreate(handle->nv_handle, &(handle->nv_enc_state), NULL);
50- // return handle;
5145}
5246
53- void NvJpegPython_destoryHandle ( NvJpegPythonHandle * * handle ){
54- // nvjpegJpegStateDestroy((*handle )->nv_statue);
55- // nvjpegEncoderStateDestroy((*handle )->nv_enc_state);
56- // nvjpegDestroy((*handle )->nv_handle);
57- // free(*handle );
58- * handle = NULL ;
47+ void NvJpegPython_cleanUpEnv ( ){
48+ nvjpegJpegStateDestroy ((__gllobal_NvJpegPython )-> nv_statue );
49+ nvjpegEncoderStateDestroy ((__gllobal_NvJpegPython )-> nv_enc_state );
50+ nvjpegDestroy ((__gllobal_NvJpegPython )-> nv_handle );
51+ free (__gllobal_NvJpegPython );
52+ __gllobal_NvJpegPython = NULL ;
5953}
6054
6155NvJpegPythonImage * NvJpegPython_createImage (int width , int height , int nComponent , nvjpegChromaSubsampling_t subsampling ){
@@ -114,8 +108,6 @@ void NvJpegPython_destoryImage(NvJpegPythonImage** img){
114108NvJpegPythonImage * NvJpegPython_decode (NvJpegPythonHandle * handle , const unsigned char * jpegData , size_t length ){
115109 nvjpegHandle_t nv_handle = handle -> nv_handle ;
116110 nvjpegJpegState_t nv_statue = handle -> nv_statue ;
117- // nvjpegCreateSimple(&nv_handle);
118- // nvjpegJpegStateCreate(nv_handle, &nv_statue);
119111
120112 int widths [NVJPEG_MAX_COMPONENT ];
121113 int heights [NVJPEG_MAX_COMPONENT ];
@@ -132,9 +124,6 @@ NvJpegPythonImage* NvJpegPython_decode(NvJpegPythonHandle* handle, const unsigne
132124 return NULL ;
133125 }
134126
135- // nvjpegJpegStateDestroy(nv_statue);
136- // nvjpegDestroy(nv_handle);
137-
138127 return imgdesc ;
139128}
140129
@@ -160,12 +149,10 @@ void NvJpegPython_destoryJpegData(NvJpegJpegData** jpegData){
160149
161150NvJpegJpegData * NvJpegPython_encode (NvJpegPythonHandle * handle , NvJpegPythonImage * img , int quality ){
162151 nvjpegHandle_t nv_handle = handle -> nv_handle ;
163- // nvjpegCreateSimple(&nv_handle);
164152
165153 nvjpegEncoderState_t nv_enc_state = handle -> nv_enc_state ;
166154 nvjpegEncoderParams_t nv_enc_params ;
167155
168- // nvjpegEncoderStateCreate(nv_handle, &nv_enc_state, NULL);
169156 nvjpegEncoderParamsCreate (nv_handle , & nv_enc_params , NULL );
170157
171158 nvjpegEncoderParamsSetQuality (nv_enc_params , quality , NULL );
@@ -181,8 +168,6 @@ NvJpegJpegData* NvJpegPython_encode(NvJpegPythonHandle* handle, NvJpegPythonImag
181168 nvjpegEncodeRetrieveBitstream (nv_handle , nv_enc_state , jpegData -> data , & (jpegData -> size ), NULL );
182169
183170 nvjpegEncoderParamsDestroy (nv_enc_params );
184- // nvjpegEncoderStateDestroy(nv_enc_state);
185- // nvjpegDestroy(nv_handle);
186171 return jpegData ;
187172}
188173
@@ -196,7 +181,7 @@ int NvJpegPython_test(char* inputJpegFilePath, char* outputRawPath, char* output
196181 return -1 ;
197182 }
198183
199- NvJpegPythonHandle * handle = NvJpegPython_createHandle ();
184+ NvJpegPythonHandle * handle = NvJpegPython_startUpEnv ();
200185 FILE * fp = fopen (inputJpegFilePath , "rb" );
201186 unsigned char * jpegData = (unsigned char * )malloc (fileInfo .st_size );
202187 size_t size = fread (jpegData , 1 , fileInfo .st_size , fp );
@@ -239,7 +224,7 @@ int NvJpegPython_test(char* inputJpegFilePath, char* outputRawPath, char* output
239224 NvJpegPython_destoryImage (& img );
240225 NvJpegPython_destoryJpegData (& jd );
241226
242- NvJpegPython_destoryHandle ( & handle );
227+ NvJpegPython_cleanUpEnv ( );
243228 return 0 ;
244229}
245230
@@ -270,13 +255,11 @@ static PyMemberDef NvJpeg_DataMembers[] =
270255
271256static void NvJpeg_init (NvJpeg * Self , PyObject * pArgs )
272257{
273- Self -> m_handle = (long long )(NvJpegPython_createHandle ());
258+ Self -> m_handle = (long long )(NvJpegPython_startUpEnv ());
274259}
275260
276261static void NvJpeg_Destruct (NvJpeg * Self )
277262{
278- NvJpegPythonHandle * m_handle = (NvJpegPythonHandle * )Self -> m_handle ;
279- NvJpegPython_destoryHandle (& m_handle );
280263 Py_TYPE (Self )-> tp_free ((PyObject * )Self );
281264}
282265
@@ -347,7 +330,6 @@ static PyObject* NvJpeg_encode(NvJpeg* Self, PyObject* Argvs)
347330
348331 NvJpegJpegData * data = NvJpegPython_encode (m_handle , img , quality );
349332
350- // PyObject* rtn = PyByteArray_FromStringAndSize((const char*)data->data, data->size);
351333 PyObject * rtn = PyBytes_FromStringAndSize ((const char * )data -> data , data -> size );
352334
353335 NvJpegPython_destoryJpegData (& data );
@@ -472,14 +454,18 @@ static PyTypeObject NvJpeg_ClassInfo =
472454 NULL ,
473455};
474456
457+ void NvJpeg_module_destroy (void * _ ){
458+ NvJpegPython_cleanUpEnv ();
459+ }
475460
476461static PyModuleDef ModuleInfo =
477462{
478463 PyModuleDef_HEAD_INIT ,
479464 "NvJpeg Module" ,
480465 "NvJpeg by Nvjpeg" ,
481466 -1 ,
482- NULL , NULL , NULL , NULL , NULL
467+ NULL , NULL , NULL , NULL ,
468+ NvJpeg_module_destroy
483469};
484470
485471PyMODINIT_FUNC
@@ -501,5 +487,4 @@ PyInit_nvjpeg(void) {
501487 return pReturn ;
502488}
503489
504-
505490#endif
0 commit comments