learning to use dcmtk code

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
George
Posts: 57
Joined: Sun, 2023-11-12, 16:50

learning to use dcmtk code

#1 Post by George »

Hello,

I request a hand, I was going over a dicom code of dcmdump.cc and I need a hand to know how to call filedump() below in order to print metadata and the raw file

Code: Select all

 int dumpFile(STD_NAMESPACE ostream &out,
                    const OFFilename &ifname,
                    const E_FileReadMode readMode,
                    const E_TransferSyntax xfer,
                    const size_t printFlags,
                    const OFBool loadIntoMemory,
                    const OFBool stopOnErrors,
                    const OFBool convertToUTF8,
                    const DcmTagKey &stopParsingAtElement,
                    const char *pixelDirectory)
{
    int result = 0;

    if (ifname.isEmpty())
    {
        OFLOG_ERROR(dcmdumpLogger, OFFIS_CONSOLE_APPLICATION << ": invalid filename: <empty string>");
        return 1;
    }

    DcmFileFormat dfile;
    DcmObject *dset = &dfile;
    if (readMode == ERM_dataset) dset = dfile.getDataset();
    OFCondition cond;

    if (stopParsingAtElement == DCM_UndefinedTagKey)
    {
        cond = dfile.loadFile(ifname, xfer, EGL_noChange, OFstatic_cast(Uint32, maxReadLength), readMode);
    }
    else
    {
        // instead of using loadFile(), we call loadFileUntilTag().
        cond = dfile.loadFileUntilTag(ifname, xfer, EGL_noChange, OFstatic_cast(Uint32, maxReadLength), readMode, stopParsingAtElement);
    }
    if (cond.bad())
    {
        OFLOG_ERROR(dcmdumpLogger, OFFIS_CONSOLE_APPLICATION << ": " << cond.text() << ": reading file: "<< ifname);
        result = 1;
        if (stopOnErrors) return result;
    }

    if (loadIntoMemory) dfile.loadAllDataIntoMemory();

#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
    if (convertToUTF8)
    {
        OFLOG_INFO(dcmdumpLogger, "converting all element values that are affected by Specific Character Set (0008,0005) to UTF-8");
        cond = dfile.convertToUTF8();
        if (cond.bad())
        {
            OFLOG_ERROR(dcmdumpLogger, OFFIS_CONSOLE_APPLICATION << ": " << cond.text() << ": converting file to UTF-8: " << ifname);
            result = 1;
            if (stopOnErrors) return result;
        }
    }
#else
    // avoid compiler warning on unused variable
    (void)convertToUTF8;
#endif

    size_t pixelCounter = 0;
    const char *pixelFileName = NULL;
    OFFilename pixelFilenameStr;
    if (pixelDirectory != NULL)
    {
        /* create filename for pixel data */
        OFFilename fileName;
        OFStandard::getFilenameFromPath(fileName, ifname);
        OFStandard::combineDirAndFilename(pixelFilenameStr, pixelDirectory, fileName);
        pixelFileName = pixelFilenameStr.getCharPointer();
    }

    /* dump complete file content */
    if (printTagCount == 0)
    {
        dset->print(out, printFlags, 0 /*level*/, pixelFileName, &pixelCounter);
    } else {
        OFBool firstTag = OFTrue;
        /* only print specified tags */
        for (int i = 0; i < printTagCount; i++)
        {
            unsigned int group = 0xffff;
            unsigned int elem = 0xffff;
            DcmTagKey searchKey;
            const char *tagName = printTagNames[i];
            if (printTagKeys[i])
                searchKey = *printTagKeys[i];
            else if (sscanf(tagName, "%x,%x", &group, &elem) == 2)
                searchKey.set(OFstatic_cast(Uint16, group), OFstatic_cast(Uint16, elem));
            else {
                OFLOG_FATAL(dcmdumpLogger, "Internal ERROR in File " << __FILE__ << ", Line "
                    << __LINE__ << OFendl << " -- Named tag inconsistency");
                abort();
            }

            DcmStack stack;
            if (dset->search(searchKey, stack, ESM_fromHere, OFTrue) == EC_Normal)
            {
                if (firstTag)
                {
                    if (!printFilename)
                    {
                        /* a newline separates two consecutive "dumps" */
                        if (++fileCounter > 1)
                            COUT << OFendl;
                    }
                    /* print header with filename */
                    if (printFileSearch)
                        COUT << "# " << OFFIS_CONSOLE_APPLICATION << " (" << fileCounter << "): " << ifname << OFendl;
                    firstTag = OFFalse;
                }
                printResult(out, stack, printFlags, pixelFileName, &pixelCounter);
                if (printAllInstances)
                {
                    while (dset->search(searchKey, stack, ESM_afterStackTop, OFTrue) == EC_Normal)
                      printResult(out, stack, printFlags, pixelFileName, &pixelCounter);
                }
            }
        }
    }
    return result;
}
ialso, how to call the code below to convert from raw to dcm:

Code: Select all


    int status = 0;
    E_TransferSyntax xfer;
    // read dump file into metaheader and dataset
    if (readDumpFile(metaheader, dataset, dumpfile, opt_ifname, xfer, opt_stopOnErrors,
        OFstatic_cast(unsigned long, opt_linelength)))
    {
        /* generate new UIDs (if required) */
        if (opt_generateUIDs)
        {
            char uid[100];
            if (opt_overwriteUIDs || !dataset->tagExistsWithValue(DCM_StudyInstanceUID))
            {
                OFLOG_INFO(dump2dcmLogger, "generating new Study Instance UID");
                dataset->putAndInsertString(DCM_StudyInstanceUID, dcmGenerateUniqueIdentifier(uid, SITE_STUDY_UID_ROOT));
            }
            if (opt_overwriteUIDs || !dataset->tagExistsWithValue(DCM_SeriesInstanceUID))
            {
                OFLOG_INFO(dump2dcmLogger, "generating new Series Instance UID");
                dataset->putAndInsertString(DCM_SeriesInstanceUID, dcmGenerateUniqueIdentifier(uid, SITE_SERIES_UID_ROOT));
            }
            if (opt_overwriteUIDs || !dataset->tagExistsWithValue(DCM_SOPInstanceUID))
            {
                OFLOG_INFO(dump2dcmLogger, "generating new SOP Instance UID");
                dataset->putAndInsertString(DCM_SOPInstanceUID, dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT));
                /* make sure that the file meta information is updated correspondingly */
                if (opt_writeMode == EWM_fileformat)
                    opt_writeMode = EWM_updateMeta;
            }
        }

        // write into file format or dataset
        OFLOG_INFO(dump2dcmLogger, "writing DICOM file");

        /* determine transfer syntax to write the file */
        if (opt_xfer == EXS_Unknown)
        {
            opt_xfer = xfer;
            /* check whether output xfer is still unknown */
            if (opt_xfer == EXS_Unknown)
            {
                OFLOG_WARN(dump2dcmLogger, "output transfer syntax unknown, assuming --write-xfer-little");
                opt_xfer = EXS_LittleEndianExplicit;
            }
        }
        /* check whether it is possible to write the file */
        if (fileformat.canWriteXfer(opt_xfer))
        {
            /* check whether pixel data is compressed */
            if ((opt_writeMode == EWM_dataset) && DcmXfer(xfer).usesEncapsulatedFormat())
            {
                OFLOG_WARN(dump2dcmLogger, "encapsulated pixel data require file format, ignoring --write-dataset");
                opt_writeMode = EWM_fileformat;
            }
            OFCondition l_error = fileformat.saveFile(opt_ofname, opt_xfer, opt_enctype, opt_glenc, opt_padenc,
                OFstatic_cast(Uint32, opt_filepad), OFstatic_cast(Uint32, opt_itempad), opt_writeMode);

            if (l_error == EC_Normal)
            {
                OFLOG_INFO(dump2dcmLogger, "dump successfully converted");
            } else {
                OFLOG_ERROR(dump2dcmLogger, l_error.text() << ": writing file: "  << opt_ofname);
                status = 1;
            }
        } else {
            OFLOG_ERROR(dump2dcmLogger, "no conversion to transfer syntax " << DcmXfer(opt_xfer).getXferName() << " possible!");
            status = 2;
        }
    }
    fclose(dumpfile);

also how to call the dcmmodify to insert the raw file:

Code: Select all


OFCondition MdfDatasetManager::modifyOrInsertFromFile(OFString tag_path,
                                                      const OFString& filename,
                                                      const OFBool only_modify,
                                                      const OFBool update_metaheader,
                                                      const OFBool ignore_missing_tags,
                                                      const OFBool no_reservation_checks)
{
    // if no file loaded: return an error
    if (dfile == NULL)
        return makeOFCondition(OFM_dcmdata, 22, OF_error, "No file loaded yet!");

    // first, perform some basic checks on the specified file(name)
    if (filename.empty())
        return makeOFCondition(OFM_dcmdata, 22, OF_error, "No filename specified to read value from!");
    if (!OFStandard::fileExists(filename))
        return makeOFCondition(OFM_dcmdata, 22, OF_error, "File to read value from does not exist!");
    if (!OFStandard::isReadable(filename))
        return makeOFCondition(OFM_dcmdata, 22, OF_error, "File to read value from is not readable!");

    // Find or create path and perform basic validity checks
    DcmPathProcessor proc;
    OFCondition result = findOrCreateValidPath(proc, tag_path, filename, only_modify, ignore_missing_tags, no_reservation_checks);
    if (result.bad())
        return result;

    // start modifying element value as desired
    OFList<DcmPath*> resultPaths;
    proc.getResults(resultPaths);
    OFListIterator(DcmPath*) resultPath = resultPaths.begin();
    DcmPathNode* lastElement;
    while (resultPath != resultPaths.end())
    {
        lastElement = (*resultPath)->back();
        if (lastElement == NULL)
            return EC_IllegalCall;
        // if tag is already present, start modify operation
        DcmElement* elem = OFstatic_cast(DcmElement*, lastElement->m_obj);
        if (elem == NULL)
            return EC_IllegalCall;
        // Check if pixel data insertion can be performed (and report error if result.bad())
        result = checkPixelDataInsertion(elem);
        if (result.good())
        {
            // check whether VR is "unknown"
            DcmEVR vr = elem->getTag().getEVR();
            if (ignore_un_modifies && ((vr == EVR_UN) || (vr == EVR_UNKNOWN) || (vr == EVR_UNKNOWN2B)))
            {
                OFLOG_WARN(mdfdsmanLogger, "will not write value to attribute having VR=UN: " << elem->getTag());
                return EC_Normal;
            }
            // create stream object for binary file
            DcmInputFileStream fileStream(filename.c_str());
            result = fileStream.status();
            if (result.good())
            {
                const size_t fileLen = OFStandard::getFileSize(filename);
                if (fileLen & 1)
                    return makeOFCondition(
                        OFM_dcmdata, 22, OF_error, "Cannot insert/modify value with odd length from file!");
                // read element value from binary file (requires even length)
                result = elem->createValueFromTempFile(
                    fileStream.newFactory(), OFstatic_cast(Uint32, fileLen), EBO_LittleEndian);
            }
            if (result.bad())
                return result;
            if (update_metaheader)
                deleteRelatedMetaheaderTag(elem->getTag());
        }
        resultPath++;
    }
    return EC_Normal;
}

it would be nice to get access to code that extract single frame dicom to jpeg2000 frame, i noticed that there is opj compress code, my findings were deprecated.
Can we negotiate them?

Best Regards
George

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 1 guest