1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmImageWriter.h"
#include "gdcmImageReader.h"
#include "gdcmImage.h"
#include "gdcmTesting.h"
#include "gdcmSystem.h"
#include "gdcmTrace.h"
#include "gdcmAttribute.h"
/*
Let's test the relation in between lossless transfer syntax and lossless compressed stream.
D_CLUNIE_CT1_J2KR.dcm
D_CLUNIE_CT1_J2KI.dcm
D_CLUNIE_MR1_JPLL.dcm
D_CLUNIE_MR1_JPLY.dcm
D_CLUNIE_CT1_JLSL.dcm
D_CLUNIE_CT1_JLSN.dcm
*/
struct LossLessTest
{
const char *lossyfile;
const char *losslessfile;
gdcm::TransferSyntax::TSType lossyts;
gdcm::TransferSyntax::TSType losslessts;
};
static const LossLessTest LossLessTestArray[] = {
{ "/D_CLUNIE_CT1_J2KI.dcm", "/D_CLUNIE_CT1_J2KR.dcm", gdcm::TransferSyntax::JPEG2000, gdcm::TransferSyntax::JPEG2000Lossless },
{ "/D_CLUNIE_MR1_JPLY.dcm", "/D_CLUNIE_MR1_JPLL.dcm", gdcm::TransferSyntax::JPEGExtendedProcess2_4, gdcm::TransferSyntax::JPEGLosslessProcess14_1 },
{ "/D_CLUNIE_CT1_JLSN.dcm", "/D_CLUNIE_CT1_JLSL.dcm", gdcm::TransferSyntax::JPEGLSNearLossless, gdcm::TransferSyntax::JPEGLSLossless }
};
int TestImageWriter2(int , char *[])
{
const char *directory = gdcm::Testing::GetDataRoot();
gdcm::Trace::WarningOff();
static const unsigned int ntests = sizeof( LossLessTestArray ) / sizeof( *LossLessTestArray );
for( unsigned int test = 0; test < ntests; ++test )
{
const LossLessTest <est = LossLessTestArray[test];
const char *j2k_filename1 = ltest.lossyfile;
const char *j2k_filename2 = ltest.losslessfile;
//const char j2k_filename1[] = "/D_CLUNIE_CT1_J2KI.dcm";
//const char j2k_filename2[] = "/D_CLUNIE_CT1_J2KR.dcm";
std::string filename_lossy = directory;
filename_lossy += j2k_filename1;
std::string filename_lossless = directory;
filename_lossless += j2k_filename2;
// Create directory first:
const char subdir[] = "TestImageWriter2";
std::string tmpdir = gdcm::Testing::GetTempDirectory( subdir );
if( !gdcm::System::FileIsDirectory( tmpdir.c_str() ) )
{
gdcm::System::MakeDirectory( tmpdir.c_str() );
//return 1;
}
std::string outfilename_lossy = gdcm::Testing::GetTempFilename( filename_lossy.c_str(), subdir );
std::string outfilename_lossless = gdcm::Testing::GetTempFilename( filename_lossless.c_str(), subdir );
{
gdcm::ImageReader reader;
reader.SetFileName( filename_lossy.c_str() );
if( !reader.Read() )
{
return 1;
}
gdcm::Image &ir = reader.GetImage();
if( !ir.IsLossy() )
{
std::cerr << "Image is not lossy" << std::endl;
std::cerr << filename_lossy << std::endl;
return 1;
}
//if( ir.GetTransferSyntax() != gdcm::TransferSyntax::JPEG2000 )
if( ir.GetTransferSyntax() != ltest.lossyts )
{
std::cerr << filename_lossy << " " << filename_lossless << std::endl;
std::cerr << ir.GetTransferSyntax() << " vs " << gdcm::TransferSyntax::GetTSString( ltest.lossyts ) << std::endl;
return 1;
}
//ir.SetTransferSyntax( gdcm::TransferSyntax::JPEG2000Lossless );
ir.SetTransferSyntax( ltest.losslessts );
gdcm::ImageWriter writer;
writer.SetImage( ir );
writer.SetFileName( outfilename_lossy.c_str() );
// We should never authorized writing of image that was lossy compress and declare as lossless
if( writer.Write() )
{
gdcm::Attribute<0x0028,0x2110> at;
at.Set( writer.GetFile().GetDataSet() );
if( at.GetValue() != "01" )
{
std::cerr << "We should never authorize writing of image that was lossy "
"compress and declare as lossless" << std::endl;
std::cerr << filename_lossy << " " << filename_lossless << std::endl;
return 1;
}
}
}
// But the contrary is ok:
{
gdcm::ImageReader reader;
reader.SetFileName( filename_lossless.c_str() );
if( !reader.Read() )
{
return 1;
}
gdcm::Image &ir = reader.GetImage();
//if( ir.GetTransferSyntax() != gdcm::TransferSyntax::JPEG2000Lossless )
if( ir.GetTransferSyntax() != ltest.losslessts )
{
return 1;
}
//ir.SetTransferSyntax( gdcm::TransferSyntax::JPEG2000 );
ir.SetTransferSyntax( ltest.lossyts );
gdcm::ImageWriter writer;
writer.SetImage( ir );
writer.SetFileName( outfilename_lossless.c_str() );
// It is ok to save a lossless file and declare transfer syntax to JPEG2000
if( !writer.Write() )
{
return 1;
}
}
}
#if 0
const gdcm::Image &ir = reader.GetImage();
gdcm::Image image;
image.SetNumberOfDimensions( ir.GetNumberOfDimensions() );
const unsigned int *dims = ir.GetDimensions();
image.SetDimension(0, dims[0] );
image.SetDimension(1, dims[1] );
const gdcm::PixelFormat &pixeltype = ir.GetPixelFormat();
image.SetPixelFormat( pixeltype );
const gdcm::PhotometricInterpretation &pi = ir.GetPhotometricInterpretation();
image.SetPhotometricInterpretation( pi );
unsigned long len = image.GetBufferLength();
gdcm_assert( len = ir.GetBufferLength() );
std::vector<char> buffer;
buffer.resize(len); // black image
gdcm::ByteValue *bv = new gdcm::ByteValue(buffer);
gdcm::DataElement pixeldata( gdcm::Tag(0x7fe0,0x0010) );
pixeldata.SetValue( *bv );
image.SetDataElement( pixeldata );
const char filename[] = "toto.dcm";
gdcm::ImageWriter writer;
writer.SetImage( image );
writer.SetFileName( filename );
gdcm::File& file = writer.GetFile();
gdcm::DataSet& ds = file.GetDataSet();
gdcm::DataElement de( gdcm::Tag(0x0010,0x0010) );
const char s[] = "GDCM^Rocks";
de.SetByteValue( s, strlen( s ) );
ds.Insert( de );
if( !writer.Write() )
{
return 1;
}
return 0;
#endif
return 0;
}