comparison UnitTestsSources/FromDcmtkTests.cpp @ 3727:090022f1b5e1

auto-generation of primitives to handle transfer syntaxes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 06 Mar 2020 17:10:03 +0100
parents 2a170a8f1faf
children 33c19a6643e1
comparison
equal deleted inserted replaced
3726:7b7ca203f1a3 3727:090022f1b5e1
1910 ASSERT_EQ(std::string(reinterpret_cast<const char*>(line2), sizeof(line2)), lines[1]); 1910 ASSERT_EQ(std::string(reinterpret_cast<const char*>(line2), sizeof(line2)), lines[1]);
1911 ASSERT_EQ(std::string(reinterpret_cast<const char*>(line3), sizeof(line3)), lines[2]); 1911 ASSERT_EQ(std::string(reinterpret_cast<const char*>(line3), sizeof(line3)), lines[2]);
1912 ASSERT_TRUE(lines[3].empty()); 1912 ASSERT_TRUE(lines[3].empty());
1913 } 1913 }
1914 1914
1915
1916
1917
1918 #if ORTHANC_ENABLE_DCMTK_TRANSCODING == 1
1919
1920 #include <dcmtk/dcmdata/dcostrmb.h>
1921
1922 static bool Transcode(std::string& buffer,
1923 DcmDataset& dataSet,
1924 E_TransferSyntax xfer)
1925 {
1926 // Determine the transfer syntax which shall be used to write the
1927 // information to the file. We always switch to the Little Endian
1928 // syntax, with explicit length.
1929
1930 // http://support.dcmtk.org/docs/dcxfer_8h-source.html
1931
1932
1933 /**
1934 * Note that up to Orthanc 0.7.1 (inclusive), the
1935 * "EXS_LittleEndianExplicit" was always used to save the DICOM
1936 * dataset into memory. We now keep the original transfer syntax
1937 * (if available).
1938 **/
1939 //E_TransferSyntax xfer = dataSet.getOriginalXfer();
1940 if (xfer == EXS_Unknown)
1941 {
1942 // No information about the original transfer syntax: This is
1943 // most probably a DICOM dataset that was read from memory.
1944 xfer = EXS_LittleEndianExplicit;
1945 }
1946
1947 E_EncodingType encodingType = /*opt_sequenceType*/ EET_ExplicitLength;
1948
1949 // Create the meta-header information
1950 DcmFileFormat ff(&dataSet);
1951 ff.validateMetaInfo(xfer);
1952 ff.removeInvalidGroups();
1953
1954 // Create a memory buffer with the proper size
1955 {
1956 const uint32_t estimatedSize = ff.calcElementLength(xfer, encodingType); // (*)
1957 buffer.resize(estimatedSize);
1958 }
1959
1960 DcmOutputBufferStream ob(&buffer[0], buffer.size());
1961
1962 // Fill the memory buffer with the meta-header and the dataset
1963 ff.transferInit();
1964 OFCondition c = ff.write(ob, xfer, encodingType, NULL,
1965 /*opt_groupLength*/ EGL_recalcGL,
1966 /*opt_paddingType*/ EPD_withoutPadding);
1967 ff.transferEnd();
1968
1969 if (c.good())
1970 {
1971 // The DICOM file is successfully written, truncate the target
1972 // buffer if its size was overestimated by (*)
1973 ob.flush();
1974
1975 size_t effectiveSize = static_cast<size_t>(ob.tell());
1976 if (effectiveSize < buffer.size())
1977 {
1978 buffer.resize(effectiveSize);
1979 }
1980
1981 return true;
1982 }
1983 else
1984 {
1985 // Error
1986 buffer.clear();
1987 return false;
1988 }
1989 }
1990
1991 #include "dcmtk/dcmjpeg/djrploss.h" /* for DJ_RPLossy */
1992 #include "dcmtk/dcmjpeg/djrplol.h" /* for DJ_RPLossless */
1993
1994 TEST(Toto, Transcode)
1995 {
1996 OFLog::configure(OFLogger::DEBUG_LOG_LEVEL);
1997 std::string s;
1998 //SystemToolbox::ReadFile(s, "/home/jodogne/Subversion/orthanc-tests/Database/TransferSyntaxes/1.2.840.10008.1.2.4.50.dcm");
1999 //SystemToolbox::ReadFile(s, "/home/jodogne/DICOM/Alain.dcm");
2000 SystemToolbox::ReadFile(s, "/home/jodogne/Subversion/orthanc-tests/Database/Brainix/Epi/IM-0001-0002.dcm");
2001
2002 std::auto_ptr<DcmFileFormat> dicom(FromDcmtkBridge::LoadFromMemoryBuffer(s.c_str(), s.size()));
2003
2004 // less /home/jodogne/Downloads/dcmtk-3.6.4/dcmdata/include/dcmtk/dcmdata/dcxfer.h
2005 printf(">> %d\n", dicom->getDataset()->getOriginalXfer()); // => 4 == EXS_JPEGProcess1
2006
2007 const DcmRepresentationParameter *p;
2008
2009 #if 0
2010 E_TransferSyntax target = EXS_LittleEndianExplicit;
2011 p = NULL;
2012 #elif 1
2013 E_TransferSyntax target = EXS_JPEGProcess14SV1;
2014 DJ_RPLossless rp_lossless(6, 0);
2015 p = &rp_lossless;
2016 #else
2017 E_TransferSyntax target = EXS_JPEGProcess1;
2018 DJ_RPLossy rp_lossy(90); // quality
2019 p = &rp_lossy;
2020 #endif
2021
2022 //E_TransferSyntax target = EXS_LittleEndianImplicit;
2023
2024 ASSERT_TRUE(dicom->getDataset()->chooseRepresentation(target, p).good());
2025 ASSERT_TRUE(dicom->getDataset()->canWriteXfer(target));
2026
2027 std::string t;
2028 ASSERT_TRUE(Transcode(t, *dicom->getDataset(), target));
2029
2030 SystemToolbox::WriteFile(s, "source.dcm");
2031 SystemToolbox::WriteFile(t, "target.dcm");
2032 }
2033
2034 #endif