suyashkumar / dicom

⚡High Performance DICOM Medical Image Parser in Go.
MIT License
933 stars 136 forks source link

Can not load dicom #240

Closed elviskuo07 closed 1 year ago

elviskuo07 commented 2 years ago

I can't load this dicom, buy pydicom can with "force=True" parameter. Does there have any hotfix for this kind dicom?

err message read.go:534: error reading value unexpected EOF test-dicom.dcm.zip

itp-atakan commented 2 years ago

You are getting this error because the dicom file which you are trying to parse has no preamble (128 bytes zeros) and magic bytes ('D', 'I', 'C', 'M'). If you'd like to have detailed information about the dicom format and file structure please have a look at (https://dicom.nema.org/dicom/2013/output/chtml/part10/chapter_7.html).

Regarding the problem, you can try to insert the preamble and magic at the beginning of your file and then parse it. You can apply the following snippet, but it's important to note that this is not the best solution. You may need to consider better solution in different cases.

package main

import (
    "bytes"
    "fmt"
    "log"
    "os"

    "github.com/suyashkumar/dicom"
)

func main() {
    dcmMagic := make([]byte, 128)
    dcmMagic = append(dcmMagic, []byte{'D', 'I', 'C', 'M'}...)
    buf, _ := os.ReadFile("test-dicom.dcm")
    rd := bytes.NewReader(append(dcmMagic, buf...))
    ds, err := dicom.Parse(rd, rd.Size(), nil)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(ds)
}
suyashkumar commented 1 year ago

Thanks for helping out the community @itp-atakan! Sounds like maybe this isn't an issue? Note that we have a compatability mode that tries to read the dataset without the magic word: https://github.com/suyashkumar/dicom/blob/961275650f24563c1d4738a20829ee4c41983d18/read.go#L149