# BMP Reader

**URL:** https://discourse.vtk.org/t/bmp-reader/2878
**Category:** Support
**Created:** [March 25, 2020, 1:02am UTC](https://discourse.vtk.org/t/bmp-reader/2878 "2020-03-25T01:02:47Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![kalpathi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kalpathi/32/1543_2.png) [@kalpathi](https://discourse.vtk.org/u/kalpathi)
#### Post date: [March 25, 2020, 1:02am UTC](https://discourse.vtk.org/t/bmp-reader/2878/1 "2020-03-25T01:02:48Z")

</div>

I havent used VTK in decades, so bear with me. It was a lot simpler C++ in the 90s! Or I am just too old!

Trying to read a series of BMP images into a volume and isosurface it. Somehow its seg faulting. So I want to look at the pixels in the individual BMP images. I can get the reader to work, but how do you go from the reader’s output to an ImageData object?

```
   -- krs
```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 25, 2020, 1:03am UTC](https://discourse.vtk.org/t/bmp-reader/2878/2 "2020-03-25T01:03:32Z")

</div>

share your code.

---

<div class="post-metadata">

### Author: ![kalpathi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kalpathi/32/1543_2.png) [@kalpathi](https://discourse.vtk.org/u/kalpathi)
#### Post date: [March 25, 2020, 1:12am UTC](https://discourse.vtk.org/t/bmp-reader/2878/3 "2020-03-25T01:12:25Z")

</div>

Made some progress on getting the pixel data, but the values are incorrect (used a pnm reader):

vtkSmartPointer pnm\_reader = vtkSmartPointer::New();  
pnm\_reader-\>SetFileName("…/…/ablation\_data/vol1/tmp.pnm");  
pnm\_reader-\>SetDataByteOrderToLittleEndian();  
pnm\_reader-\>SetDataScalarTypeToUnsignedChar();

```
pnm_reader->Update();
vtkSmartPointer<vtkImageData> image =
        static_cast<vtkSmartPointer<vtkImageData>>(pnm_reader->GetOutput());

int dims[3];
image->GetDimensions(dims);
cout << "Dims:" << dims[0] << "," << dims[1] << "," << dims[2] << endl;

for (int k = 0; k < 10; k++) {
    int *val = (int *) image->GetScalarPointer(k, 0, 0);
    cout << "Val:" << (int) val[0] << "," << val[1] << "," << val[2] << endl;
}

```

Anything obviously incorrect in the above fragment? I should be getting values in the range of 0-255, but all values are much larger…

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 26, 2020, 11:20am UTC](https://discourse.vtk.org/t/bmp-reader/2878/4 "2020-03-26T11:20:17Z")

</div>

Hello, Kalpathi

First, please, edit your post by encolsing your code between two ```, one above the first line and another after the last line. By doing this, you won’t have the templates (those tag-like thingies such as `vtkSmartPointer<vtkStructureGrid>`) trimmed. So we can better read your code.

But the first suspicious line in your code is a static cast of a smart pointer. Generally you don’t do or need that.

cheers,

Paulo

---

<div class="post-metadata">

### Author: ![kalpathi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kalpathi/32/1543_2.png) [@kalpathi](https://discourse.vtk.org/u/kalpathi)
#### Post date: [March 26, 2020, 3:02pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/5 "2020-03-26T15:02:39Z")

</div>

Paul, thanks for the advice. Hopefully this can be copy/pasted by someone to test and helps debug my issue:

Here is the very simple pnm reader (I went to pnm) and trying to check the image pixel output:

```auto
#include <vtkPNMReader.h>
#include <vtkSmartPointer.h>
#include <vtkImageData.h>

int main (int argc, char *argv[]) {

    vtkSmartPointer<vtkPNMReader> pnm_reader = 
                  vtkSmartPointer<vtkPNMReader>::New();
        pnm_reader->SetFileName("tmp.pnm");
        pnm_reader->SetDataByteOrderToLittleEndian();
        pnm_reader->SetDataScalarTypeToUnsignedChar();

        pnm_reader->Update();
    vtkSmartPointer<vtkImageData> image = pnm_reader->GetOutput();

    int dims[3];
    image->GetDimensions(dims);
    cout << "Dims:" << dims[0] << "," << dims[1] << "," << dims[2] << endl;

    for (int k = 0; k < 10; k++) {
        int *val = (int *) image->GetScalarPointer(k, 0, 0);
        cout << "Val:" << (int) val[0] << "," << val[1] << "," << val[2] << endl;
    }

    return EXIT_SUCCESS;
}

```

But the output pixel values are way out of range. They should be in 0-255 range.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 26, 2020, 8:51pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/6 "2020-03-26T20:51:20Z")

</div>

I suggest trying `image->GetScalarComponentAsFloat( k, 0, 0, <number of component>)`. There is also `GetScalarAsDouble(...)` if you want to get the values as doubles.

---

<div class="post-metadata">

### Author: ![kalpathi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kalpathi/32/1543_2.png) [@kalpathi](https://discourse.vtk.org/u/kalpathi)
#### Post date: [March 26, 2020, 10:03pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/7 "2020-03-26T22:03:45Z")

</div>

So that works and gets me reasonable values. I am stil curious about why the GetScalarPointer () didnt work or what I was doing incorrectly.

Thanks.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 26, 2020, 10:42pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/8 "2020-03-26T22:42:35Z")

</div>

`GetScalarPointer()` returns a `void*`. Thus, you have to know beforehand what is the underlying scalar type in the `vtkImageData` object. Perhaps casting it into `int*` is not an adequate cast. You could reverse back to using `GetScalarPonter()` and cast it to `float*` or `double*` and see which works instead of `int*`.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 26, 2020, 10:54pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/9 "2020-03-26T22:54:06Z")

</div>

You could use `GetScalarSize()` to get the size of the scalar type in bytes. If it returns 4, scalars are `float`; 8 means `double` and 16 means `long double`.

---

<div class="post-metadata">

### Author: ![kalpathi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kalpathi/32/1543_2.png) [@kalpathi](https://discourse.vtk.org/u/kalpathi)
#### Post date: [March 26, 2020, 10:53pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/10 "2020-03-26T22:53:30Z")

</div>

Yeah, I did try casting to float\* and try, but didnt help.

```
  Oh well, your suggestion is good enough for me to progress.. was

```

doing a sanity check on the reader… Thanks a bunch.

– krs

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 26, 2020, 10:55pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/11 "2020-03-26T22:55:28Z")

</div>

And double\*?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 26, 2020, 10:57pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/12 "2020-03-26T22:57:53Z")

</div>

Please, don’t forget to mark the answer of your choice as a solution so other people can skip straight to the sought answer.

---

<div class="post-metadata">

### Author: ![kalpathi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kalpathi/32/1543_2.png) [@kalpathi](https://discourse.vtk.org/u/kalpathi)
#### Post date: [March 31, 2020, 1:10am UTC](https://discourse.vtk.org/t/bmp-reader/2878/13 "2020-03-31T01:10:17Z")

</div>

Here is the program fragment for isolining from a BMP image… most of the issues were my learning curve after coming back to VTK after decades…

Tons of help from Paul Carvalho in answering my newbie questions! Thanks a ton.

```auto
int main(int, char*[])
{
    auto colors = vtkSmartPointer<vtkNamedColors>::New();
    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    auto renWin = vtkSmartPointer<vtkRenderWindow>::New();
        renWin->AddRenderer(renderer);
    auto iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
        iren->SetRenderWindow(renWin);

  vtkSmartPointer<vtkBMPReader> bmp_reader = vtkSmartPointer<vtkBMPReader>::New();
        bmp_reader->SetFileName("example.bmp");
        bmp_reader->SetDataByteOrderToLittleEndian();
        bmp_reader->SetDataScalarTypeToFloat();
        bmp_reader->Update();

    auto contour = vtkSmartPointer<vtkMarchingSquares>::New();
        contour->SetInputConnection(bmp_reader->GetOutputPort());
        contour->SetValue(0, 30.0);

    auto volMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        volMapper->SetInputConnection(contour->GetOutputPort());
        volMapper->ScalarVisibilityOff();
    auto volActor = vtkSmartPointer<vtkActor>::New();
        volActor->SetMapper(volMapper);
        volActor->GetProperty()->EdgeVisibilityOn();
        volActor->GetProperty()->SetColor(colors->GetColor3d("Salmon").GetData());
    renderer->AddActor(volActor);
    renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
    renWin->SetSize(512, 512);
    // interact with data
    renWin->Render();
    iren->Start();

    return EXIT_SUCCESS;
}

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 31, 2020, 11:07am UTC](https://discourse.vtk.org/t/bmp-reader/2878/14 "2020-03-31T11:07:38Z")

</div>

Well, according to yourself the solution was actually given by me (6th post). It is important to give credit to the people that spend their free time helping you as a way to stimulate more people to help.

---

<div class="post-metadata">

### Author: ![kalpathi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kalpathi/32/1543_2.png) [@kalpathi](https://discourse.vtk.org/u/kalpathi)
#### Post date: [March 31, 2020, 12:24pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/15 "2020-03-31T12:24:08Z")

</div>

My apologies, I just edited my solution post… Still getting  
used to the mailing lists of this century! Different from the  
news readers of the 90s…

```
  But yeah, I need to remember that in the future.. as I

```

definitely will have a lot more questions as I use VTK more and  
more.

Big thanks for answering my questions so fast!

– krs

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 31, 2020, 1:23pm UTC](https://discourse.vtk.org/t/bmp-reader/2878/16 "2020-03-31T13:23:25Z")

</div>

You’re quite welcome! Feel free to ask more questions. But keep in mind that answers in open source communities often don’t come so fast, or even don’t come at all. So, if your business depend on fast responses, please consider purchasing services from Kitware: [https://vtk.org/services/](https://vtk.org/services/).
