vtkPNMReader.cc 7.36 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
1
2
/*=========================================================================

Ken Martin's avatar
Ken Martin committed
3
  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
  Module:    vtkPNMReader.cc
Will Schroeder's avatar
Will Schroeder committed
5
6
7
8
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

Ken Martin's avatar
Ken Martin committed
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

Copyright (c) 1993-1995 Ken Martin, Will Schroeder, Bill Lorensen.

This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.

The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.

IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Will Schroeder's avatar
Will Schroeder committed
38
39
40


=========================================================================*/
Ken Martin's avatar
Ken Martin committed
41
#include "vtkPNMReader.hh"
Will Schroeder's avatar
Will Schroeder committed
42

Ken Martin's avatar
Ken Martin committed
43
char vtkPNMReaderGetChar(FILE *fp)
Ken Martin's avatar
Ken Martin committed
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
{
  char c;
  int result;

  if ((result = getc(fp)) == EOF )
    {
    return '\0';
    }
  
  c = (char)result;
  if (c == '#')
    {
    do
      {
      if ((result = getc(fp)) == EOF )
	{
	return '\0';
	}
      c = (char)result;
      }
    while (c != '\n');
    }
  
  return c;
}

Ken Martin's avatar
Ken Martin committed
70
int vtkPNMReaderGetInt(FILE *fp)
Ken Martin's avatar
Ken Martin committed
71
72
73
74
75
76
{
  char c;
  int result = 0;
  
  do
    {
Ken Martin's avatar
Ken Martin committed
77
    c = vtkPNMReaderGetChar(fp);
Ken Martin's avatar
Ken Martin committed
78
79
80
81
82
    }
  while ((c < '1')||(c > '9'));
  do
    {
    result = result * 10 + (c - '0');
Ken Martin's avatar
Ken Martin committed
83
    c = vtkPNMReaderGetChar(fp);
Ken Martin's avatar
Ken Martin committed
84
85
86
87
88
89
90
    }
  while ((c >= '0')&&(c <= '9'));

  return result;
}
  

Ken Martin's avatar
Ken Martin committed
91
vtkPNMReader::vtkPNMReader()
Will Schroeder's avatar
Will Schroeder committed
92
93
94
95
96
97
98
99
{
  this->Filename = NULL;
  this->ImageRange[0] = this->ImageRange[1] = -1;

  this->DataOrigin[0] = this->DataOrigin[1] = this->DataOrigin[2] = 0.0;
  this->DataAspectRatio[0] = this->DataAspectRatio[1] = this->DataAspectRatio[2] = 1.0;
}

Ken Martin's avatar
Ken Martin committed
100
void vtkPNMReader::Execute()
Will Schroeder's avatar
Will Schroeder committed
101
{
Ken Martin's avatar
Ken Martin committed
102
  vtkColorScalars *newScalars;
Will Schroeder's avatar
Will Schroeder committed
103
  int dim[3];
Ken Martin's avatar
Ken Martin committed
104
105
  vtkStructuredPoints *output = this->GetOutput();
  
Will Schroeder's avatar
Will Schroeder committed
106
107
108

  if ( this->Filename == NULL )
    {
Ken Martin's avatar
Ken Martin committed
109
    vtkErrorMacro(<<"Please specify a filename!");
Will Schroeder's avatar
Will Schroeder committed
110
111
112
113
114
115
116
117
118
119
120
121
    return;
    }

  if ( this->ImageRange[0] < 0 )
    {
    newScalars = this->ReadImage(dim);
    }
  else
    {
    newScalars = this->ReadVolume(dim);
    }

Will Schroeder's avatar
Will Schroeder committed
122
123
  if ( ! newScalars ) return;

Ken Martin's avatar
Ken Martin committed
124
125
126
127
  output->SetDimensions(dim);
  output->SetAspectRatio(this->DataAspectRatio);
  output->SetOrigin(this->DataOrigin);
  output->GetPointData()->SetScalars(newScalars);
Will Schroeder's avatar
Will Schroeder committed
128
  newScalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
129
130
}

Ken Martin's avatar
Ken Martin committed
131
vtkColorScalars *vtkPNMReader::ReadImage(int dim[3])
Will Schroeder's avatar
Will Schroeder committed
132
133
{
  char magic[80];
Ken Martin's avatar
Ken Martin committed
134
135
136
137
  vtkPixmap *pixmap;
  vtkGraymap *graymap;
  vtkBitmap *bitmap;
  vtkColorScalars *s=NULL;
Will Schroeder's avatar
Will Schroeder committed
138
139
  FILE *fp;
  int numPts;
Ken Martin's avatar
Ken Martin committed
140
141
  char c;
  
Will Schroeder's avatar
Will Schroeder committed
142
143
144
145
  dim[2] = 1;

  if ( !(fp = fopen(this->Filename,"r")) )
    {
Ken Martin's avatar
Ken Martin committed
146
    vtkErrorMacro(<<"Can't find file: " << this->Filename);
Will Schroeder's avatar
Will Schroeder committed
147
148
149
    return NULL;
    }

Ken Martin's avatar
Ken Martin committed
150
151
152
  // get the magic number
  do
    {
Ken Martin's avatar
Ken Martin committed
153
    c = vtkPNMReaderGetChar(fp);
Ken Martin's avatar
Ken Martin committed
154
155
156
    }
  while (c != 'P');
  magic[0] = c;
Ken Martin's avatar
Ken Martin committed
157
  magic[1] = vtkPNMReaderGetChar(fp);
Ken Martin's avatar
Ken Martin committed
158
159
160
  magic[2] = '\0';

  // now get the dimensions
Ken Martin's avatar
Ken Martin committed
161
162
  dim[0] = vtkPNMReaderGetInt(fp);
  dim[1] = vtkPNMReaderGetInt(fp);
Will Schroeder's avatar
Will Schroeder committed
163
164
165
166

  // check input
  if ( (numPts = dim[0]*dim[1]) < 1 )
    {
Ken Martin's avatar
Ken Martin committed
167
    vtkErrorMacro(<<"Bad input data!");
Will Schroeder's avatar
Will Schroeder committed
168
169
170
171
172
173
    return NULL;
    }

  // compare magic number to see proper file type
  if ( ! strcmp(magic,"P4") ) //pbm file
    {
Ken Martin's avatar
Ken Martin committed
174
    bitmap = new vtkBitmap(numPts);
Will Schroeder's avatar
Will Schroeder committed
175
176
177
178
    }

  else if ( ! strcmp(magic,"P5") ) //pgm file
    {
Ken Martin's avatar
Ken Martin committed
179
    graymap = new vtkGraymap(numPts);
Ken Martin's avatar
Ken Martin committed
180
    if ( this->ReadBinaryPGM(fp,graymap,dim[0],dim[1]) )
Will Schroeder's avatar
Will Schroeder committed
181
      {
Ken Martin's avatar
Ken Martin committed
182
      s = (vtkColorScalars *) graymap;
Will Schroeder's avatar
Will Schroeder committed
183
184
185
      }
    else
      {
Will Schroeder's avatar
Will Schroeder committed
186
      graymap->Delete();
Will Schroeder's avatar
Will Schroeder committed
187
188
189
190
191
      }
    }

  else if ( ! strcmp(magic,"P6") ) //ppm file
    {
Ken Martin's avatar
Ken Martin committed
192
    pixmap = new vtkPixmap(numPts);
Ken Martin's avatar
Ken Martin committed
193
    if ( this->ReadBinaryPPM(fp,pixmap,dim[0],dim[1]) )
Will Schroeder's avatar
Will Schroeder committed
194
      {
Ken Martin's avatar
Ken Martin committed
195
      s = (vtkColorScalars *) pixmap;
Will Schroeder's avatar
Will Schroeder committed
196
197
198
      }
    else
      {
Will Schroeder's avatar
Will Schroeder committed
199
      pixmap->Delete();
Will Schroeder's avatar
Will Schroeder committed
200
201
202
203
      }
    }
  else
    {
Ken Martin's avatar
Ken Martin committed
204
    vtkErrorMacro(<<"Unknown file type!");
Will Schroeder's avatar
Will Schroeder committed
205
206
207
208
209
210
    return NULL;
    }

  return s;
}

Ken Martin's avatar
Ken Martin committed
211
vtkColorScalars *vtkPNMReader::ReadVolume(int dim[3])
Will Schroeder's avatar
Will Schroeder committed
212
{
Ken Martin's avatar
Ken Martin committed
213
  vtkColorScalars *s=NULL;
Will Schroeder's avatar
Will Schroeder committed
214
215
216
217
218

  return s;
}


Ken Martin's avatar
Ken Martin committed
219
int vtkPNMReader::ReadBinaryPBM(FILE *fp, vtkBitmap* bitmap,
220
                               int xsize, int ysize)
Will Schroeder's avatar
Will Schroeder committed
221
{
222
223
224
  int max, j, packedXSize=xsize/8;
  unsigned char *cptr;

Ken Martin's avatar
Ken Martin committed
225
  max = vtkPNMReaderGetInt(fp);
Ken Martin's avatar
Ken Martin committed
226

227
228
229
230
231
232
//
// Since pnm coordinate system is at upper left of image, need to convert
// to lower rh corner origin by reading a row at a time.
//
  for (j=0; j<ysize; j++)
    {
Ken Martin's avatar
Ken Martin committed
233
    cptr = bitmap->WritePtr((ysize-j-1)*packedXSize,packedXSize);
234
235
    if ( ! fread(cptr,1,packedXSize,fp) )
      {
Ken Martin's avatar
Ken Martin committed
236
      vtkErrorMacro(<<"Error reaading raw pbm data!");
237
238
239
240
      return 0;
      }
    }

Will Schroeder's avatar
Will Schroeder committed
241
242
243
  return 1;
}

Ken Martin's avatar
Ken Martin committed
244
int vtkPNMReader::ReadBinaryPGM(FILE *fp, vtkGraymap* graymap,
245
                               int xsize, int ysize)
Will Schroeder's avatar
Will Schroeder committed
246
{
247
  int max, j;
Will Schroeder's avatar
Will Schroeder committed
248
249
  unsigned char *cptr;

Ken Martin's avatar
Ken Martin committed
250
  max = vtkPNMReaderGetInt(fp);
251
252
253
254
255
//
// Since pnm coordinate system is at upper left of image, need to convert
// to lower rh corner origin by reading a row at a time.
//
  for (j=0; j<ysize; j++)
Will Schroeder's avatar
Will Schroeder committed
256
    {
Ken Martin's avatar
Ken Martin committed
257
    cptr = graymap->WritePtr((ysize-j-1)*xsize,xsize);
258
259
    if ( ! fread(cptr,1,xsize,fp) )
      {
Ken Martin's avatar
Ken Martin committed
260
      vtkErrorMacro(<<"Error reaading raw pgm data!");
261
262
      return 0;
      }
Will Schroeder's avatar
Will Schroeder committed
263
    }
264

Will Schroeder's avatar
Will Schroeder committed
265
266
267
  return 1;
}

Ken Martin's avatar
Ken Martin committed
268
int vtkPNMReader::ReadBinaryPPM(FILE *fp, vtkPixmap* pixmap,
269
                               int xsize, int ysize)
Will Schroeder's avatar
Will Schroeder committed
270
{
271
  int max, j;
Will Schroeder's avatar
Will Schroeder committed
272
273
  unsigned char *cptr;

Ken Martin's avatar
Ken Martin committed
274
  max = vtkPNMReaderGetInt(fp);
275
276
277
278
279
//
// Since pnm coordinate system is at upper left of image, need to convert
// to lower rh corner origin by reading a row at a time.
//
  for (j=0; j<ysize; j++)
Will Schroeder's avatar
Will Schroeder committed
280
    {
Ken Martin's avatar
Ken Martin committed
281
    cptr = pixmap->WritePtr((ysize-j-1)*xsize,xsize);
282
283
    if ( ! fread(cptr,3,xsize,fp) )
      {
Ken Martin's avatar
Ken Martin committed
284
      vtkErrorMacro(<<"Error reaading raw ppm data!");
285
286
      return 0;
      }
Will Schroeder's avatar
Will Schroeder committed
287
    }
288

Will Schroeder's avatar
Will Schroeder committed
289
290
291
  return 1;
}

Ken Martin's avatar
Ken Martin committed
292
void vtkPNMReader::PrintSelf(ostream& os, vtkIndent indent)
Will Schroeder's avatar
Will Schroeder committed
293
{
Ken Martin's avatar
Ken Martin committed
294
  vtkStructuredPointsSource::PrintSelf(os,indent);
Will Schroeder's avatar
Will Schroeder committed
295
296
297
298
299
300
301
302
303
304
305

  os << indent << "Filename: " << this->Filename << "\n";
  os << indent << "Image Range: (" << this->ImageRange[0] << ", " 
     << this->ImageRange[1] << ")\n";
  os << indent << "Data Origin: (" << this->DataOrigin[0] << ", "
                                   << this->DataOrigin[1] << ", "
                                   << this->DataOrigin[2] << ")\n";
  os << indent << "AspectRatio: (" << this->DataAspectRatio[0] << ", "
                                   << this->DataAspectRatio[1] << ", "
                                   << this->DataAspectRatio[2] << ")\n";
}