0

I'm doing a little physics experiment where I send numpy arrays to a manufacturer library that is written in C++.

Currently, the code looks as follows:

import ctypes
import numpy as np

is_rgba = ctypes.c_uint(1)

# Load DLL
ctypes.cdll.LoadLibrary("\path_to_dll")
slm_lib = ctypes.CDLL("Blink_C_wrapper")


# Phase is the input array with data type np.uint8 and shape (1920, 1200)
# Here I only print parts of it

phase = [[137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137
  137 137 137 137 137 137 137 137 137 137 137 137]
 [118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118
  118 118 118 118 118 118 118 118 118 118 118 118]
 [100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
  100 100 100 100 100 100 100 100 100 100 100 100]
 [ 82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82
   82  82  82  82  82  82  82  82  82  82  82  82]
 [ 63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63
   63  63  63  63  63  63  63  63  63  63  63  63]
 [ 45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45
   45  45  45  45  45  45  45  45  45  45  45  45]
 [ 27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27
   27  27  27  27  27  27  27  27  27  27  27  27]
 [  9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9
    9   9   9   9   9   9   9   9   9   9   9   9]
 [246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246
  246 246 246 246 246 246 246 246 246 246 246 246]
 [228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228
  228 228 228 228 228 228 228 228 228 228 228 228]
 [210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210
  210 210 210 210 210 210 210 210 210 210 210 210]
 [191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191
  191 191 191 191 191 191 191 191 191 191 191 191]
 [173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173
  173 173 173 173 173 173 173 173 173 173 173 173]
 [155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155
  155 155 155 155 155 155 155 155 155 155 155 155]
 [137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137
  137 137 137 137 137 137 137 137 137 137 137 137]
 [118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118
  118 118 118 118 118 118 118 118 118 118 118 118]
 [100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
  100 100 100 100 100 100 100 100 100 100 100 100]
 [ 82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82
   82  82  82  82  82  82  82  82  82  82  82  82]
 [ 63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63
   63  63  63  63  63  63  63  63  63  63  63  63]
 [ 45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45
   45  45  45  45  45  45  45  45  45  45  45  45]
 [ 27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27
   27  27  27  27  27  27  27  27  27  27  27  27]
 [  9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9
    9   9   9   9   9   9   9   9   9   9   9   9]
 [246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246
  246 246 246 246 246 246 246 246 246 246 246 246]
 [228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228
  228 228 228 228 228 228 228 228 228 228 228 228]
 [210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210
  210 210 210 210 210 210 210 210 210 210 210 210]
 [191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191
  191 191 191 191 191 191 191 191 191 191 191 191]
 [173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173
  173 173 173 173 173 173 173 173 173 173 173 173]
 [155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155
  155 155 155 155 155 155 155 155 155 155 155 155]
 [137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137
  137 137 137 137 137 137 137 137 137 137 137 137]
 [118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118
  118 118 118 118 118 118 118 118 118 118 118 118]
 [100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
  100 100 100 100 100 100 100 100 100 100 100 100]
 [ 82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82
   82  82  82  82  82  82  82  82  82  82  82  82]
 [ 63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63
   63  63  63  63  63  63  63  63  63  63  63  63]
 [ 45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45
   45  45  45  45  45  45  45  45  45  45  45  45]
 [ 27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27
   27  27  27  27  27  27  27  27  27  27  27  27]
 [  9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9
    9   9   9   9   9   9   9   9   9   9   9   9]
 [246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246
  246 246 246 246 246 246 246 246 246 246 246 246]
 [228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228
  228 228 228 228 228 228 228 228 228 228 228 228]
 [210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210
  210 210 210 210 210 210 210 210 210 210 210 210]
 [191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191
  191 191 191 191 191 191 191 191 191 191 191 191]
 [173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173
  173 173 173 173 173 173 173 173 173 173 173 173]
 [155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155
  155 155 155 155 155 155 155 155 155 155 155 155]
 [137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137
  137 137 137 137 137 137 137 137 137 137 137 137]
 [118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118
  118 118 118 118 118 118 118 118 118 118 118 118]
 [100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
  100 100 100 100 100 100 100 100 100 100 100 100]
 [ 82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82  82
   82  82  82  82  82  82  82  82  82  82  82  82]
 [ 63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63  63
   63  63  63  63  63  63  63  63  63  63  63  63]
 [ 45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45  45
   45  45  45  45  45  45  45  45  45  45  45  45]
 [ 27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27  27
   27  27  27  27  27  27  27  27  27  27  27  27]
 [  9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9
    9   9   9   9   9   9   9   9   9   9   9   9]]

# Write to SLM
matrix = phase.ravel()
c = matrix.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))


success = slm_lib.Write_image(c, is_rgba)

I do not have access to Write_image. The header is

BLINK_C_WRAPPER_API int Write_image(unsigned char* image_data, int is_8_bit);

From what I record in the experiment, it seems as if there is a flip along the horizontal axis, i.e. what actually gets written to my machine is np.flip(phase, axis=0). I know that Python is row-major and so is C++, so it shouldn't be the case.

My question is:

  • Is my use of ctpyes correct?
  • Is it possible that somewhere in this code snippet a flip happens?
khelwood
  • 55,782
  • 14
  • 81
  • 108
LionCereals
  • 171
  • 1
  • 10
  • [\[SO\]: How to write a function in C which takes as input two matrices as numpy array (@CristiFati's answer)](https://stackoverflow.com/a/73807241/4788546), [\[SO\]: How to convert a ctypes C pointer to a Numpy array? (@CristiFati's answer)](https://stackoverflow.com/a/67327122/4788546), [\[SO\]: CTypes: Numpy Array Always has Different Values (@CristiFati's answer)](https://stackoverflow.com/a/67128128/4788546). – CristiFati Mar 01 '23 at 12:02
  • @CristiFati Nowhere in these questions is the possibility of an axis flip? – LionCereals Mar 02 '23 at 08:47
  • I just gave some pointers. In this case you should check [\[SO\]: How to create a Minimal, Reproducible Example (reprex (mcve))](https://stackoverflow.com/help/minimal-reproducible-example), coming with some working code, an expected and actual output (with reasonable data - not arrays with thousands of elements). Also maybe the flip happens inside the function. – CristiFati Mar 02 '23 at 08:50

0 Answers0