All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.charleslzq.dicom.DicomDataReader.kt Maven / Gradle / Ivy

There is a newer version: 1.1.0-RC2
Show newest version
package com.github.charleslzq.dicom

import org.dcm4che3.data.Tag
import org.dcm4che3.io.DicomInputStream
import java.io.File

class DicomDataReader(imageFileBase: String) {
    private val dicomTagInfoReader = DicomTagInfoReader()
    private val dicomImageReader = DicomImageReader(imageFileBase)

    fun parse(dcmFile: File): DicomData {
        val imageUri = dicomImageReader.convert(dcmFile)
        val dicomInputStream = DicomInputStream(dcmFile)
        val tagMap = dicomTagInfoReader.parse(dicomInputStream).map { it.tag to it }.toMap()
        val patient = getPatient(tagMap)
        val study = getStudy(tagMap)
        val series = getSeries(tagMap)
        return DicomData(patient, study, series, imageUri)
    }

    private fun getPatient(tagMap: Map): DicomPatient {
        val dicomPatient = DicomPatient()
        getFromTagMap(tagMap, Tag.PatientID, dicomPatient::id::set)
        getFromTagMap(tagMap, Tag.PatientName, dicomPatient::name::set)
        getFromTagMap(tagMap, Tag.PatientAddress, dicomPatient::address::set)
        getFromTagMap(tagMap, Tag.PatientBirthDate, dicomPatient::birthday::set)
        getFromTagMap(tagMap, Tag.PatientPosition, dicomPatient::position::set)
        getFromTagMap(tagMap, Tag.PatientSex, dicomPatient::sex::set)
        getFromTagMap(tagMap, Tag.IssuerOfPatientID, dicomPatient::idIssuer::set)
        getFromTagMap(tagMap, Tag.PatientAge, { value -> dicomPatient.age = value.substringBefore("Y").toInt() })
        return dicomPatient
    }

    private fun getStudy(tagMap: Map): DicomStudy {
        val dicomStudy = DicomStudy()
        getFromTagMap(tagMap, Tag.StudyID, dicomStudy::id::set)
        getFromTagMap(tagMap, Tag.StudyInstanceUID, dicomStudy::instanceUID::set)
        getFromTagMap(tagMap, Tag.StudyDate, dicomStudy::date::set)
        getFromTagMap(tagMap, Tag.StudyTime, dicomStudy::time::set)
        getFromTagMap(tagMap, Tag.StudyDescription, dicomStudy::description::set)
        return dicomStudy
    }

    private fun getSeries(tagMap: Map): DicomSeries {
        val dicomSeries = DicomSeries();
        getFromTagMap(tagMap, Tag.SeriesNumber, { value -> dicomSeries.number = value.toInt() })
        getFromTagMap(tagMap, Tag.SeriesInstanceUID, dicomSeries::instanceUID::set)
        getFromTagMap(tagMap, Tag.SeriesDate, dicomSeries::date::set)
        getFromTagMap(tagMap, Tag.SeriesTime, dicomSeries::time::set)
        getFromTagMap(tagMap, Tag.SeriesDescription, dicomSeries::description::set)
        return dicomSeries
    }


    private fun getFromTagMap(tagMap: Map, tagNo: Int, consumer: (String) -> Unit) {
        val tag = tagMap.get(tagNo)
        if (tag != null) {
            consumer(tag.stringValue)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy