File: vtkSLCReader.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (456 lines) | stat: -rw-r--r-- 11,143 bytes parent folder | download | duplicates (3)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkSLCReader.h"

#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include <vtksys/SystemTools.hxx>

#include <cctype>

VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkSLCReader);

// Constructor for a vtkSLCReader.
vtkSLCReader::vtkSLCReader()
{
  this->FileName = nullptr;
  this->Error = 0;
}

vtkSLCReader::~vtkSLCReader() = default;

// Decodes an array of eight bit run-length encoded data.
unsigned char* vtkSLCReader::Decode8BitData(unsigned char* in_ptr, int size)
{
  unsigned char* curr_ptr;
  unsigned char* decode_ptr;
  unsigned char* return_ptr;
  unsigned char current_value;
  unsigned char remaining;

  curr_ptr = in_ptr;

  decode_ptr = return_ptr = new unsigned char[size];

  while (true)
  {
    current_value = *(curr_ptr++);

    if (!(remaining = (current_value & 0x7f)))
    {
      break;
    }

    if (current_value & 0x80)
    {
      while (remaining--)
      {
        *(decode_ptr++) = *(curr_ptr++);
      }
    }
    else
    {
      current_value = *(curr_ptr++);
      while (remaining--)
      {
        *(decode_ptr++) = current_value;
      }
    }
  }

  return return_ptr;
}

// This will be needed when we make this an imaging filter.
int vtkSLCReader::RequestInformation(
  vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
  FILE* fp;
  int temp;
  double f[3];
  int size[3];
  int magic_num;
  this->Error = 1;

  if (!this->FileName)
  {
    vtkErrorMacro(<< "A FileName must be specified.");
    return 0;
  }

  // Initialize
  if ((fp = vtksys::SystemTools::Fopen(this->FileName, "rb")) == nullptr)
  {
    vtkErrorMacro(<< "File " << this->FileName << " not found");
    return 0;
  }
  this->FileDimensionality = 3;
  if (fscanf(fp, "%d", &magic_num) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to read magic number");
    fclose(fp);
    return 1;
  }
  if (magic_num != 11111)
  {
    vtkErrorMacro(<< "SLC magic number is not correct");
    fclose(fp);
    return 1;
  }

  f[0] = f[1] = f[2] = 0.0;
  this->SetDataOrigin(f);

  if (fscanf(fp, "%d", size) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to read size[0]");
    fclose(fp);
    return 1;
  }
  if (fscanf(fp, "%d", size + 1) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to read size[1]");
    fclose(fp);
    return 1;
  }
  if (fscanf(fp, "%d", size + 2) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to read size[2]");
    fclose(fp);
    return 1;
  }
  this->SetDataExtent(0, size[0] - 1, 0, size[1] - 1, 0, size[2] - 1);

  // Skip Over bits_per_voxel Field */
  if (fscanf(fp, "%d", &temp) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName
                  << "Failed to skip over bits per pixel");
    fclose(fp);
    return 1;
  }

  if (fscanf(fp, "%lf", f) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to spacing[0]");
    fclose(fp);
    return 1;
  }
  if (fscanf(fp, "%lf", f + 1) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to spacing[1]");
    fclose(fp);
    return 1;
  }
  if (fscanf(fp, "%lf", f + 2) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to spacing[2]");
    fclose(fp);
    return 1;
  }
  this->SetDataSpacing(f);

  // Skip Over unit_type, data_origin, and data_modification
  if (fscanf(fp, "%d", &temp) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to skip over unit type");
    fclose(fp);
    return 1;
  }
  if (fscanf(fp, "%d", &temp) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to skip over data origin");
    fclose(fp);
    return 1;
  }
  if (fscanf(fp, "%d", &temp) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName
                  << "Failed to skip over data modification");
    fclose(fp);
    return 1;
  }

  this->SetDataScalarType(VTK_UNSIGNED_CHAR);
  this->SetNumberOfScalarComponents(1);

  fclose(fp);
  return this->Superclass::RequestInformation(request, inputVector, outputVector);
}

// Reads an SLC file and creates a vtkStructuredPoints dataset.
void vtkSLCReader::ExecuteDataWithInformation(
  vtkDataObject* output_do, vtkInformation* vtkNotUsed(outInfo))
{
  vtkImageData* output = vtkImageData::SafeDownCast(output_do);

  FILE* fp;

  int temp;
  int data_compression;
  int plane_size;
  double f[3];
  int size[3];
  int magic_num;
  int z_counter;
  int icon_width, icon_height;
  int compressed_size;

  unsigned char* icon_ptr;
  unsigned char* compressed_ptr;
  unsigned char* scan_ptr = nullptr;

  this->Error = 1;

  if (!this->FileName)
  {
    vtkErrorMacro(<< "A FileName must be specified.");
    return;
  }

  // Initialize
  if ((fp = vtksys::SystemTools::Fopen(this->FileName, "rb")) == nullptr)
  {
    vtkErrorMacro(<< "File " << this->FileName << " not found");
    return;
  }

  if (fscanf(fp, "%d", &magic_num) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to read magic number");
    fclose(fp);
    return;
  }
  if (magic_num != 11111)
  {
    vtkErrorMacro(<< "SLC magic number is not correct");
    fclose(fp);
    return;
  }

  f[0] = f[1] = f[2] = 0.0;
  output->SetOrigin(f);

  if (fscanf(fp, "%d", size) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed read size[0]");
    fclose(fp);
    return;
  }
  if (fscanf(fp, "%d", size + 1) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed read size[1]");
    fclose(fp);
    return;
  }
  if (fscanf(fp, "%d", size + 2) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed read size[2]");
    fclose(fp);
    return;
  }
  output->SetDimensions(size);

  output->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
  output->GetPointData()->GetScalars()->SetName("SLCImage");

  // Skip Over bits_per_voxel Field */
  if (fscanf(fp, "%d", &temp) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName
                  << "Failed to skip over bits per voxel");
    fclose(fp);
    return;
  }
  if (fscanf(fp, "%lf", f) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed read spacing[0]");
    fclose(fp);
    return;
  }
  if (fscanf(fp, "%lf", f + 1) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed read spacing[1]");
    fclose(fp);
    return;
  }
  if (fscanf(fp, "%lf", f + 2) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed read spacing[2]");
    fclose(fp);
    return;
  }
  output->SetSpacing(f);

  // Skip Over unit_type, data_origin, and data_modification
  if (fscanf(fp, "%d", &temp) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to skip over unit type");
    fclose(fp);
    return;
  }
  if (fscanf(fp, "%d", &temp) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to skip over data origin");
    fclose(fp);
    return;
  }
  if (fscanf(fp, "%d", &temp) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName
                  << "Failed to skip over data modification");
    fclose(fp);
    return;
  }

  if (fscanf(fp, "%d\n", &data_compression) != 1)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to read data compression");
    fclose(fp);
    return;
  }

  plane_size = size[0] * size[1];
#ifndef NDEBUG
  int volume_size = plane_size * size[2];
#endif

  // Skip Over Icon
  if (fscanf(fp, "%d %d X", &icon_width, &icon_height) != 2)
  {
    vtkErrorMacro(<< "Error reading file: " << this->FileName << "Failed to skip over icon");
    fclose(fp);
    return;
  }
  icon_ptr = new unsigned char[(icon_width * icon_height)];

  if (fread(icon_ptr, (icon_width * icon_height), 1, fp) != 1)
  {
    vtkErrorMacro(
      "SLCReader error reading file: " << this->FileName << " Premature EOF while reading icon.");
    delete[] icon_ptr;
    fclose(fp);
    return;
  }
  if (fread(icon_ptr, (icon_width * icon_height), 1, fp) != 1)
  {
    vtkErrorMacro(
      "SLCReader error reading file: " << this->FileName << " Premature EOF while reading icon.");
    delete[] icon_ptr;
    fclose(fp);
    return;
  }
  if (fread(icon_ptr, (icon_width * icon_height), 1, fp) != 1)
  {
  }

  delete[] icon_ptr;

  // Read In Data Plane By Plane
  for (z_counter = 0; z_counter < size[2]; z_counter++)
  {
    if (!(z_counter % 10) && !z_counter)
    {
      this->UpdateProgress((float)z_counter / size[2]);
    }

    // Read a single plane into temp memory
    switch (data_compression)
    {
      case 0:

        if (!scan_ptr)
        {
          scan_ptr = new unsigned char[plane_size];
        }

        if (fread(scan_ptr, 1, plane_size, fp) != (unsigned int)plane_size)
        {
          vtkErrorMacro(<< "Unable to read slice " << z_counter << " from SLC File");
          fclose(fp);
          return;
        }

        break;

      case 1:

        delete[] scan_ptr;

        if (fscanf(fp, "%d X", &compressed_size) != 1)
        {
          vtkErrorMacro(<< "Error reading file: " << this->FileName
                        << "Failed to read compressed size");
          fclose(fp);
          return;
        }

        compressed_ptr = new unsigned char[compressed_size];

        if (fread(compressed_ptr, 1, compressed_size, fp) != (unsigned int)compressed_size)
        {
          vtkErrorMacro(<< "Unable to read compressed slice " << z_counter << " from SLC File");
          delete[] compressed_ptr;
          fclose(fp);
          return;
        }

        scan_ptr = this->Decode8BitData(compressed_ptr, plane_size);
        delete[] compressed_ptr;

        break;
      default:
        vtkErrorMacro(<< "Unknown SLC compression type: " << data_compression);
        fclose(fp);
        return;
    }
    void* outputSlice = output->GetScalarPointer(0, 0, z_counter);
    if (outputSlice && scan_ptr)
    {
      memcpy(outputSlice, scan_ptr, plane_size);
    }
  }

  delete[] scan_ptr;

  vtkDebugMacro(<< "Read " << volume_size << " points");

  fclose(fp);
  this->Error = 0;
}

int vtkSLCReader::CanReadFile(const char* fname)
{
  FILE* fp;
  int magic_num = 0;
  if ((fp = vtksys::SystemTools::Fopen(fname, "rb")) == nullptr)
  {
    return 0;
  }

  if (fscanf(fp, "%d", &magic_num) != 1)
  {
    fclose(fp);
    return 0;
  }
  if (magic_num != 11111)
  {
    fclose(fp);
    return 0;
  }
  fclose(fp);
  return 3;
}

void vtkSLCReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);

  os << indent << "Error: " << this->Error << "\n";
  os << indent << "File Name: " << (this->FileName ? this->FileName : "(none)") << "\n";
}
VTK_ABI_NAMESPACE_END