[vtkusers] Outputting a 2D ImageData into a JPEG/PNG...

Jefferson Thomas foramuyou at gmail.com
Thu May 22 04:16:25 EDT 2008


Hi,
I want to create a JPEG or PNG image file out of a 2D ImageData structure.
I create my ImageData like this:

  data=vtkImageData::New();
  int size1=100, size2=100, size3=1;

  data->SetDimensions(size1,size2,1);
  data->SetSpacing(1.0, 1.0, 1.0);
  data->SetOrigin(0.0, 0.0, 0.0);
  data->SetScalarTypeToUnsignedChar();

  vtkUnsignedCharArray* array;
  array = (vtkUnsignedCharArray*)vtkUnsignedCharArray::New();

  array->SetNumberOfValues(size1*size2*size3);
  array->SetNumberOfComponents(3);

  unsigned char curval;

  for(int x=0;x<size1;x++) {
    for(int y=0;y<size2;y++) {
      if (x<50) curval=200; else curval=0;  //Thats an example for how
I fill the ImageData with values
      array->SetTuple3(x+y*size1,curval, curval, curval);
    }
  }

  data->GetPointData()->AddArray(array); //Adding the data array to the image

 And I write it to JPEG like this:

 vtkJPEGWriter* jpgw=vtkJPEGWriter::New();
 jpgw->SetInput(data);
 jpgw->SetFileName("test.jpg");
 jpgw->Write();

After that I get strange image with gray stripes on the bottom.
What am I doing wrong ? Is there any other way to convert a 2D
ImageData into a JPEG file ?

Thank you,
Thomas



More information about the vtkusers mailing list