File: gdcmPNMCodec.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 (392 lines) | stat: -rw-r--r-- 9,336 bytes parent folder | download | duplicates (6)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*=========================================================================

  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 "gdcmPNMCodec.h"
#include "gdcmTransferSyntax.h"
#include "gdcmSystem.h"
#include "gdcmDataElement.h"
#include "gdcmSequenceOfFragments.h"
#include "gdcmSwapper.h"

namespace gdcm
{

PNMCodec::PNMCodec():BufferLength(0)
{
}

PNMCodec::~PNMCodec()
= default;

bool PNMCodec::CanDecode(TransferSyntax const &) const
{
  return false;
}

bool PNMCodec::CanCode(TransferSyntax const &) const
{
  return false;
}


static uint8_t reverseBitsByte(uint8_t x)
{
  uint8_t b = 0;
  for (int i = 0; i < 8; ++i) {
    b <<= 1;
    b |= (x & 1);
    x >>= 1;
  }
  return b;
}

bool PNMCodec::Write(const char *filename, const DataElement &out) const
{
  std::ofstream os(filename, std::ios::binary);
  const unsigned int *dims = this->GetDimensions();
  const PhotometricInterpretation &pi = this->GetPhotometricInterpretation();
  const PixelFormat& pf = GetPixelFormat();
  if( pi == PhotometricInterpretation::MONOCHROME2
    || pi == PhotometricInterpretation::MONOCHROME1 ) // warning viz will be surprising
    {
    // FIXME possible mismatch pi vs pf (eg. pbm with mono2)
    if( pf == PixelFormat::SINGLEBIT)
      os << "P4\n";
    else
      os << "P5\n";
    }
  else if( pi == PhotometricInterpretation::RGB
    || pi == PhotometricInterpretation::PALETTE_COLOR )
    {
    os << "P6\n";
    }
  else
    {
    gdcmErrorMacro( "PhotometricInterpretation unhandled: " << pi );
    return false;
    }
  os << dims[0] << " " << dims[1] << "\n";

  const unsigned int pc = this->GetPlanarConfiguration();
  if( pc )
    {
    gdcmErrorMacro( "PlanarConfiguration unhandled: " << pc );
    return false;
    }

  switch(pf)
    {
  case PixelFormat::SINGLEBIT:
    break;
  case PixelFormat::UINT8:
  //case PixelFormat::INT8:
    os << "255\n";
    break;
  case PixelFormat::UINT16:
  //case PixelFormat::INT16:
    os << "65535\n";
    break;
  default:
    gdcmErrorMacro( "Unhandled PF: " << pf );
    return false;
    }

  const ByteValue *bv = out.GetByteValue();
  // FIXME: PNM Codec cannot handle encapsulated syntax... sigh
  if(!bv)
    {
    gdcmErrorMacro( "PNM Codec does not handle compress syntax. You need to decompress first." );
    return false;
    }
  assert(bv);

  if( pi == PhotometricInterpretation::PALETTE_COLOR )
    {
    std::stringstream is;
    is.write( bv->GetPointer(), bv->GetLength() );

    const LookupTable &lut = this->GetLUT();
    lut.Decode(is, os);
    }
  else
    {
    if( pf.GetBitsAllocated() == 16 )
      {
      bv->Write<SwapperDoOp, uint16_t>( os );
      }
    else if( pf.GetBitsAllocated() == 1 )
      {
      const uint8_t *x = (const uint8_t*) bv->GetPointer();
      for( size_t i = 0; i < bv->GetLength(); i++ )
        {
        uint8_t b = reverseBitsByte(x[i]);
        os.write((char*)&b, 1);
        }
      }
    else
      {
      //bv->Write<SwapperDoOp, uint8_t>( os );
      bv->WriteBuffer( os );
      }
    }

  os.close();


  return true;
}

bool PNMCodec::Read(const char *filename, DataElement &out) const
{
  size_t len = System::FileSize(filename);
  std::ifstream is(filename, std::ios::binary);
  std::string type, str;
  std::getline(is,type);
  PhotometricInterpretation pi;
  if( type == "P5" )
    pi = PhotometricInterpretation::MONOCHROME2;
  else if( type == "P6" )
    pi = PhotometricInterpretation::RGB;
  else
    {
    std::cerr << "Unhandled PGM type: " << type << std::endl;
    return false;
    }

  // skip comments:
  while( is.peek() == '#' )
    {
    std::getline(is, str);
    //std::cout << str << std::endl;
    }
  unsigned int dims[3] = {};
  is >> dims[0]; is >> dims[1];
  unsigned int maxval;
  is >> maxval;
  // some kind of empty line...
  if( is.peek() == '\n' )
    {
    is.get();
    }
  std::streampos pos = is.tellg();
  //assert(pos < INT_MAX);
  size_t m = (len - (size_t)pos ) / ( dims[0]*dims[1] );
  if( m * dims[0] * dims[1] != len - pos )
    {
    std::cerr << "Problem computing length" << std::endl;
    return false;
    }
  PixelFormat pf;
  switch(maxval)
    {
  case 255:
    pf = PixelFormat::UINT8;
    break;
  case 1023:
    pf = PixelFormat::UINT16;
    pf.SetBitsStored( 10 );
    break;
  case 4095:
    pf = PixelFormat::UINT16;
    pf.SetBitsStored( 12 );
    break;
  case 32767:
    pf = PixelFormat::UINT16;
    pf.SetBitsStored( 15 );
    break;
  case 65535:
    pf = PixelFormat::UINT16;
    break;
  default:
    std::cerr << "Unhandled max val: " << maxval << std::endl;
    return false;
    }
  if( pi == PhotometricInterpretation::RGB )
    {
    pf.SetSamplesPerPixel( 3 );
    }
  //if ( maxval * 8 != bpp ) return 1;

  size_t pdlen = GetBufferLength();
  assert( pdlen );
  char * buf = new char[pdlen];
  // is should be at right offset, just read!
  is.read(buf, len);
  if( !is.eof() )
  {
    delete[] buf;
    return false;
  }

  out.SetTag( Tag(0x7fe0,0x0010) );
  VL::Type pdLenSize = (VL::Type)pdlen;
  out.SetByteValue( buf, pdLenSize );
  delete[] buf;

  is.close();

  return true;
}

static inline int log2( int n )
{
  int bits = 0;
  while (n > 0)
    {
    bits++;
    n >>= 1;
    }
  return bits;
}

bool PNMCodec::GetHeaderInfo(std::istream &is, TransferSyntax &ts)
{
  is.seekg( 0, std::ios::end );
  std::streampos len = is.tellg();
  //assert(len < INT_MAX);
  is.seekg( 0, std::ios::beg );

  std::string type, str;
  std::getline(is,type);
  PhotometricInterpretation pi;
  if( type == "P4" ) // P4 => mono W/B !
    pi = PhotometricInterpretation::MONOCHROME1;
  else if( type == "P5" )
    pi = PhotometricInterpretation::MONOCHROME2;
  else if( type == "P6" ) // P3 => ASCII
    pi = PhotometricInterpretation::RGB;
  else
    {
    std::cerr << "Unhandled PGM type: " << type << std::endl;
    return false;
    }

  // skip comments:
  while( is.peek() == '#' )
    {
    std::getline(is, str);
    //std::cout << str << std::endl;
    }
  unsigned int dims[3] = {};
  is >> dims[0]; is >> dims[1];
  unsigned int maxval;
  if( type == "P4" )
    maxval = 1;
  else
    is >> maxval;
  // http://netpbm.sourceforge.net/doc/pgm.html
  // some kind of empty line...
  if( is.peek() == '\n' )
    {
    is.get();
    }
  std::streamoff pos = is.tellg();
  //assert(len < INT_MAX);
  //assert(pos < INT_MAX);
  size_t m = ((size_t)len - (size_t)pos ) / ( dims[0]*dims[1] );
  bool cond;
  if( type == "P4" ) {
    const size_t bytesPerRow = dims[0] / 8 + (dims[0] % 8 != 0 ? 1 : 0);
    cond = bytesPerRow * dims[1] != ((size_t)len - (size_t)pos);
  }
  else
    cond = m * dims[0] * dims[1] != (size_t)len - (size_t)pos;
  if( cond )
    {
    std::cerr << "Problem computing length" << std::endl;
    std::cerr << "Pos: " << len - pos << std::endl;
    std::cerr << "expected: " << m * dims[0] * dims[1] << std::endl;
    return false;
    }
  PixelFormat pf = GetPixelFormat();
#if 0
  switch(maxval)
    {
  case 255:
    pf = PixelFormat::UINT8;
    break;
  case 1023:
    pf = PixelFormat::UINT16;
    pf.SetBitsStored( 10 );
    break;
  case 4095:
    pf = PixelFormat::UINT16;
    pf.SetBitsStored( 12 );
    break;
  case 32767:
    pf = PixelFormat::UINT16;
    pf.SetBitsStored( 15 );
    break;
  case 65535:
    pf = PixelFormat::UINT16;
    break;
  default:
    std::cerr << "Unhandled max val: " << maxval << std::endl;
    return false;
    }
#else
  const int nbits = log2( maxval );
  // handle case where nbits = 0 also:
  if( nbits > 0 && nbits <= 1 )
    {
    pf.SetBitsAllocated( 1 );
    }
  else if( nbits > 1 && nbits <= 8 )
    {
    pf.SetBitsAllocated( 8 );
    pf.SetBitsStored( (unsigned short)nbits );
    }
  else if( nbits > 8 && nbits <= 16 )
    {
    pf.SetBitsAllocated( 16 );
    pf.SetBitsStored( (unsigned short)nbits );
    }
  else if( nbits > 16 && nbits <= 32 )
    {
    pf.SetBitsAllocated( 32 );
    pf.SetBitsStored( (unsigned short)nbits );
    }
  else
    {
    std::cerr << "Unhandled max val: " << maxval << std::endl;
    return false;
    }
#endif
  if( pi == PhotometricInterpretation::RGB )
    {
    pf.SetSamplesPerPixel( 3 );
    }
  //if ( maxval * 8 != bpp ) return 1;

  //image.SetTransferSyntax( TransferSyntax::ExplicitVRBigEndian ); // PGM are big endian
  //image.SetTransferSyntax( TransferSyntax::ExplicitVRLittleEndian ); // PGM are big endian
  //image.SetTransferSyntax( TransferSyntax::ImplicitVRBigEndianPrivateGE ); // PGM are big endian
  if( pf.GetBitsAllocated() > 8 )
    ts = TransferSyntax::ImplicitVRBigEndianPrivateGE;
  else
    //ts = TransferSyntax::ImplicitVRLittleEndian; // nicer to handle than private GE
    ts = TransferSyntax::ExplicitVRLittleEndian; // nicer to handle than private GE

  SetPhotometricInterpretation( pi );
  SetPixelFormat( pf );
  SetDimensions( dims );

  return true;
}

ImageCodec * PNMCodec::Clone() const
{
  return nullptr;
}

} // end namespace gdcm