Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

return numpy array values from ctypes call

$
0
0

I am trying to use cytpes to interface to the Little CMS color system. I am on windows and python 3.12 with numpy 1.26.4. I have compiled lcms myself using msvc 2022. Calls to lcms2.dll to open profiles and create transforms appear to work. My problem occurs with the lcms2 call "cmsDoTransform". It returns rubbish values.This is my first so question, so I apologise in advance if I have not gotten the formatting and content correct

Here is my python ctypes function for to wrap this call:

    def cmsDoTransform(self, transform, input_buffer, output_buffer, size):        # # CMSAPI  void             CMSEXPORT cmsDoTransform(cmsHTRANSFORM Transform,        #                                                  const void * InputBuffer,        #                                                  void * OutputBuffer,        #                                                  cmsUInt32Number Size);        try:            self.lcmsLib.cmsDoTransform.restype = None            self.lcmsLib.cmsDoTransform.argtypes = [                cmsHTRANSFORM,                ctypeslib.ndpointer(dtype=np.uint8,                                    ndim=1,                                    flags='C_CONTIGUOUS'),                ctypeslib.ndpointer(dtype=np.float64,                                    ndim=1,                                    flags='C_CONTIGUOUS'),                c_uint32,            ]        except Exception as e:            print(bcolors.FAIL +"exception in cmsDoTransform restype/argtyps" + bcolors.ENDC)            print(bcolors.FAIL + e + bcolors.ENDC)        result = None        try:            result = self.lcmsLib.cmsDoTransform(transform, input_buffer, output_buffer, size)        except Exception as e:            print(bcolors.FAIL +"exception in cmsDoTransform")            print(e)            print(bcolors.ENDC)        return result

The littleCMS c function signature is:

void cmsDoTransform(cmsHTRANSFORM hTransform, const void * InputBuffer, void * OutputBuffer, cmsUInt32Number Size);

Using a simple test to convert a single sRGB value (10, 200, 100) to Lab Using the "cmsCreateLab4Prifle" call thus:

input_format = TYPE_RGB_8output_format = TYPE_Lab_FLTinput_buffer = np.array([10, 200, 100], dtype=np.uint8)output_buffer = np.empty(3, dtype=np.float64)transform = lcms_interface.cmsCreateTransformTHR(context, input_profile, input_format, output_profile, output_format, 0, 0)

results in the values [-4.33999486e+013 5.47595081e-315 8.40779170e-044]. Clearly rubbish.I have tried with other profiles and also gotten rubbish.Has anyone any experience with lcms or this type of ctypes processing?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>