python-gdcm icon indicating copy to clipboard operation
python-gdcm copied to clipboard

Variable type errors trying to use GetLUTDescriptor()

Open bal-agates opened this issue 1 year ago • 2 comments

I am getting this error

$ python gdcm_lut_problem.py 
Traceback (most recent call last):
  File "/Users/brett/Brett2025/Source/Python/dicom/gdcm_lut_problem.py", line 15, in <module>
    lutDescRed = lut.GetLUTDescriptor(lut.RED, length, subscript, bitsize)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/brett/Library/Python/3.12/lib/python/site-packages/_gdcm/gdcmswig.py", line 3537, in GetLUTDescriptor
    return _gdcmswig.LookupTable_GetLUTDescriptor(self, type, length, subscript, bitsize)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: in method 'LookupTable_GetLUTDescriptor', argument 3 of type 'unsigned short &'

when trying to run this code

import sys
import gdcm

filename = "pydicom_examples/examples_palette.dcm"
reader = gdcm.ImageReader()
reader.SetFileName(filename)
if not reader.Read():
    print("Error reading file:", filename)
    sys.exit(1)
image = reader.GetImage()
lut = image.GetLUT()
length = bytearray(b'\x00\x00')
subscript = bytearray(b'\x00\x00')
bitsize = bytearray(b'\x00\x00')
lutDescRed = lut.GetLUTDescriptor(lut.RED, length, subscript, bitsize)

Help on method GetLUTDescriptor in module _gdcm.gdcmswig:

GetLUTDescriptor(type, length, subscript, bitsize) method of _gdcm.gdcmswig.LookupTable instance
    void
    gdcm::LookupTable::GetLUTDescriptor(LookupTableType type, unsigned
    short &length, unsigned short &subscript, unsigned short &bitsize)
    const

Can you suggest Python code to satisfy "unsigned short &" arguments? Is there a way to use this function in Python? I have looked at Python bytearray, struct, array, bytes. The bytearray is mutable to hopefully satisfy the &. Not sure how to satisfy the type checking for "unsigned short".

Note that my input example is the only DICOM image with PALETTE encoding I have found. This one is strange because the LUT takes 8-bits (raw) and produces 16-bits for each color. There doesn't seem to be a built-in gdcm way to decode with 8-to-16 bits so I was going try do that in numpy, thus the need to access the LUT table data directly.

I have seen in the opencv4 library they have been able to convert C function signatures like this to

[length, subscript, bitsize] = lut.GetLUTDescriptor(type)

bal-agates avatar Jan 12 '25 22:01 bal-agates

I wasn't able to run this code too. I tried to use ctypes and numpy but nothing worked. I think it's missing something in the wrapping code to python. I thinks it's better to report this problem in GDCM bug report https://sourceforge.net/p/gdcm/bugs/

tfmoraes avatar Jan 13 '25 14:01 tfmoraes

Thanks for looking at this. I will submit a bug report as you suggested.

bal-agates avatar Jan 14 '25 02:01 bal-agates