File: gdcmImageChangePhotometricInterpretation.cxx

package info (click to toggle)
gdcm 3.0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 26,880 kB
  • sloc: cpp: 203,477; ansic: 78,582; xml: 48,129; python: 3,459; cs: 2,308; java: 1,629; lex: 1,290; sh: 334; php: 128; makefile: 117
file content (286 lines) | stat: -rw-r--r-- 10,162 bytes parent folder | download
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*=========================================================================

  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 "gdcmImageChangePhotometricInterpretation.h"
#include "gdcmSequenceOfFragments.h"
#include "gdcmSequenceOfItems.h"
#include "gdcmFragment.h"
#include "gdcmRAWCodec.h"

namespace gdcm
{

/*
 * http://groups.google.com/group/comp.protocols.dicom/browse_thread/thread/10f91b14e3013a11
 * http://groups.google.com/group/comp.protocols.dicom/browse_thread/thread/1190189387c1702c
 * http://groups.google.com/group/comp.protocols.dicom/browse_thread/thread/a9e118fbbf6dcc9f
 * http://forum.dcmtk.org/viewtopic.php?p=5441&sid=61ad1304edb31203c4136890ab651405

YBR_FULL as Photometric Interpretation is really the right thing to do. The
problem is that the JPEG bitstream as such does not contain any indication of
the color model - it just specifies that there are three samples per pixel. In
theory it is well possible to apply baseline JPEG compression to RGB pixel
data, although this is an unusual approach since YCbCr provides for better
compression ratio at given image quality. A JFIF header would contain that
information, but the JFIF header is neither required nor recommended in the
DICOM JPEG bitstream. In the absence of that information, and with a JPEG
compressed DICOM file where Photometric Interpretation is "RGB", the parser
needs to decide whether the encoder did something unusual but legal and
decompress the JPEG bitstream as RGB, or whether the encoder just failed to
correctly encode the color model of the JPEG bitstream (which in my experience
is in most cases the correct assumption) and ignore Photometric Interpretation
(and thus incorrectly decode unusual but legal images).
*/
bool ImageChangePhotometricInterpretation::ChangeMonochrome()
{
  // Ok let's give up on this one for now.
  // We would need to take care of Pixel Padding Value to actually be able to
  // invert the image without this information we potentially will be making
  // mistake. just like Largest Image Pixel Value and other would be wrong
  const Bitmap &image = *Input;
  PhotometricInterpretation pi = image.GetPhotometricInterpretation();
  if( pi != PhotometricInterpretation::MONOCHROME1 && pi != PhotometricInterpretation::MONOCHROME2 ) return false;
  if( pi == PI )
    {
    return true;
    }

  unsigned long len = image.GetBufferLength();
  char *p = new char[len];
  image.GetBuffer( p );
  std::stringstream is;
  is.write( p, len );
  delete[] p;

  //ImageCodec ic;
  RAWCodec ic;
  ic.SetPixelFormat(image.GetPixelFormat());
  std::ostringstream os;
  ic.DoInvertMonochrome( is, os );

  DataElement &de = Output->GetDataElement();
  std::string str = os.str();
  VL::Type strSize = (VL::Type)str.size();
  de.SetByteValue( str.c_str(), strSize);
  //Output->GetLUT().Clear();
  Output->SetPhotometricInterpretation( PI );
  //Output->GetPixelFormat().SetSamplesPerPixel( 3 );
  //Output->SetPlanarConfiguration( 0 ); // FIXME OT-PAL-8-face.dcm has a PlanarConfiguration while being PALETTE COLOR...
  //const TransferSyntax &ts = image.GetTransferSyntax();
  ////assert( ts == TransferSyntax::RLELossless );
  //if( ts.IsExplicit() )
  //  {
  //  Output->SetTransferSyntax( TransferSyntax::ExplicitVRLittleEndian );
  //  }
  //else
  //  {
  //  assert( ts.IsImplicit() );
  //  Output->SetTransferSyntax( TransferSyntax::ImplicitVRLittleEndian );
  //  }


  bool success = true;
  return success;
}

bool ImageChangePhotometricInterpretation::ChangeYBR2RGB()
{
  // Ok let's give up on this one for now.
  // We would need to take care of Pixel Padding Value to actually be able to
  // invert the image without this information we potentially will be making
  // mistake. just like Largest Image Pixel Value and other would be wrong
  const Bitmap &image = *Input;
  PhotometricInterpretation pi = image.GetPhotometricInterpretation();
  //assert( pi == PhotometricInterpretation::MONOCHROME1 || pi == PhotometricInterpretation::MONOCHROME2 );
  if( pi == PI )
  {
    return true;
  }

  unsigned long len = image.GetBufferLength();
  char *p8 = new char[len];
  image.GetBuffer( p8 );

  const PixelFormat &pf = image.GetPixelFormat();
  if( image.GetPlanarConfiguration() != 0 ) return false;
  if( pf.GetSamplesPerPixel() != 3 || pf.GetPixelRepresentation() != 0 ) return false;
  if( pf.GetBitsAllocated() == 16 )
  {
    unsigned short *p = (unsigned short*)p8;
    unsigned short rgb[3];
    unsigned short ybr[3];
    for( unsigned long i = 0; i < len / (3 * 2); ++i ) {
      ybr[0] = p[ 3 * i + 0];
      ybr[1] = p[ 3 * i + 1];
      ybr[2] = p[ 3 * i + 2];
      YBR2RGB(rgb, ybr);
      p[ 3 * i + 0] = rgb[0];
      p[ 3 * i + 1] = rgb[1];
      p[ 3 * i + 2] = rgb[2];
    }
  }
  else if( pf.GetBitsAllocated() == 8 )
  {
    unsigned char *p = (unsigned char*)p8;
    unsigned char rgb[3];
    unsigned char ybr[3];
    for( unsigned long i = 0; i < len / 3; ++i ) {
      ybr[0] = p[ 3 * i + 0];
      ybr[1] = p[ 3 * i + 1];
      ybr[2] = p[ 3 * i + 2];
      YBR2RGB(rgb, ybr);
      p[ 3 * i + 0] = rgb[0];
      p[ 3 * i + 1] = rgb[1];
      p[ 3 * i + 2] = rgb[2];
    }
  }

  DataElement &de = Output->GetDataElement();
  de.SetByteValue( p8, len);
  //Output->GetLUT().Clear();
  Output->SetPhotometricInterpretation( PI );
  //Output->GetPixelFormat().SetSamplesPerPixel( 3 );
  //Output->SetPlanarConfiguration( 0 ); // FIXME OT-PAL-8-face.dcm has a PlanarConfiguration while being PALETTE COLOR...
  //const TransferSyntax &ts = image.GetTransferSyntax();
  ////assert( ts == TransferSyntax::RLELossless );
  //if( ts.IsExplicit() )
  //  {
  //  Output->SetTransferSyntax( TransferSyntax::ExplicitVRLittleEndian );
  //  }
  //else
  //  {
  //  assert( ts.IsImplicit() );
  //  Output->SetTransferSyntax( TransferSyntax::ImplicitVRLittleEndian );
  //  }


  bool success = true;
  delete[] p8;
  return success;
}

bool ImageChangePhotometricInterpretation::ChangeRGB2YBR()
{
  // Ok let's give up on this one for now.
  // We would need to take care of Pixel Padding Value to actually be able to
  // invert the image without this information we potentially will be making
  // mistake. just like Largest Image Pixel Value and other would be wrong
  const Bitmap &image = *Input;
  PhotometricInterpretation pi = image.GetPhotometricInterpretation();
  //assert( pi == PhotometricInterpretation::MONOCHROME1 || pi == PhotometricInterpretation::MONOCHROME2 );
  if( pi == PI )
  {
    return true;
  }

  unsigned long len = image.GetBufferLength();
  char *p8 = new char[len];
  image.GetBuffer( p8 );

  const PixelFormat &pf = image.GetPixelFormat();
  if( image.GetPlanarConfiguration() != 0 ) return false;
  if( pf.GetSamplesPerPixel() != 3 || pf.GetPixelRepresentation() != 0 ) return false;
  if( pf.GetBitsAllocated() == 16 )
  {
    unsigned short *p = (unsigned short*)p8;
    unsigned short rgb[3];
    unsigned short ybr[3];
    for( unsigned long i = 0; i < len / (3 * 2); ++i ) {
      rgb[0] = p[ 3 * i + 0];
      rgb[1] = p[ 3 * i + 1];
      rgb[2] = p[ 3 * i + 2];
      RGB2YBR(ybr, rgb, pf.GetBitsStored());
      p[ 3 * i + 0] = ybr[0];
      p[ 3 * i + 1] = ybr[1];
      p[ 3 * i + 2] = ybr[2];
    }
  }
  else if( pf.GetBitsAllocated() == 8 )
  {
    unsigned char *p = (unsigned char*)p8;
    unsigned char rgb[3];
    unsigned char ybr[3];
    for( unsigned long i = 0; i < len / 3; ++i ) {
      rgb[0] = p[ 3 * i + 0];
      rgb[1] = p[ 3 * i + 1];
      rgb[2] = p[ 3 * i + 2];
      RGB2YBR(ybr, rgb, pf.GetBitsStored());
      p[ 3 * i + 0] = ybr[0];
      p[ 3 * i + 1] = ybr[1];
      p[ 3 * i + 2] = ybr[2];
    }
  }

  DataElement &de = Output->GetDataElement();
  de.SetByteValue( p8, len);
  //Output->GetLUT().Clear();
  Output->SetPhotometricInterpretation( PI );
  //Output->GetPixelFormat().SetSamplesPerPixel( 3 );
  //Output->SetPlanarConfiguration( 0 ); // FIXME OT-PAL-8-face.dcm has a PlanarConfiguration while being PALETTE COLOR...
  //const TransferSyntax &ts = image.GetTransferSyntax();
  ////assert( ts == TransferSyntax::RLELossless );
  //if( ts.IsExplicit() )
  //  {
  //  Output->SetTransferSyntax( TransferSyntax::ExplicitVRLittleEndian );
  //  }
  //else
  //  {
  //  assert( ts.IsImplicit() );
  //  Output->SetTransferSyntax( TransferSyntax::ImplicitVRLittleEndian );
  //  }


  bool success = true;
  delete[] p8;
  return success;
}

bool ImageChangePhotometricInterpretation::Change()
{
  // PS 3.3 - 2008 C.7.6.3.1.2 Photometric Interpretation
  Output = Input;
  if( PI == PhotometricInterpretation::YBR_FULL )
    {
    if( Input->GetPhotometricInterpretation() != PhotometricInterpretation::RGB ) return false;
    /*
    In the case where Bits Allocated (0028,0100) has a value of 8 then the following equations convert
    between RGB and YCBCR Photometric Interpretation.
    Y = + .2990R + .5870G + .1140B
    CB = - .1687R - .3313G + .5000B + 128
    CR = + .5000R - .4187G - .0813B + 128
    Note: The above is based on CCIR Recommendation 601-2 dated 1990.
    */
    return ChangeRGB2YBR();
    }
  else if( PI == PhotometricInterpretation::RGB )
    {
    if( Input->GetPhotometricInterpretation() != PhotometricInterpretation::YBR_FULL ) return false;
    /* octave:
     * B = [.2990,.5870,.1140;- .16874, - .33126,  .5000; .5000, - .41869, - .08131]
     * inv(B)
     * 1.0000e+00   -3.6820e-05    1.4020e+00
     * 1.0000e+00   -3.4411e-01   -7.1410e-01
     * 1.0000e+00    1.7720e+00   -1.3458e-04
     */
    return ChangeYBR2RGB();
    }
  else if( PI == PhotometricInterpretation::MONOCHROME1 || PI == PhotometricInterpretation::MONOCHROME2 )
    {
    return ChangeMonochrome();
    }
  //else
  return false;
}


} // end namespace gdcm