marlam / gencolormap

color map generator for scientific visualization
https://marlam.de/gencolormap
Other
10 stars 3 forks source link

test image #2

Open adammaj1 opened 8 months ago

adammaj1 commented 8 months ago

Hi

I try to add test image by Peter Kovesi


/* Apply color map to test image. See http://peterkovesi.com/projects/colourmaps/colourmaptestimage.html */

std::string ToPPM_Test(int n, const unsigned char* srgb_colormap)
{

    static const float twopi = 2.0 * M_PI;
    int jMax = 128; // height
    int iMax = 512; // width
    int colormap_size = jMax * iMax;

    std::string s = "P3\n"; // magic number for plain (ASCII txt) PPM
    s += std::to_string(n) + "  " + std::to_string(jMax) + "\n"; // save n=width and jMax=height to s string
    s += "255\n"; // max val
    for (int j = 0; j < jMax; j++){ // y , jMax = height
        float v = 1.0f - (j / (jMax - 1.0f));
        for (int i = 0; i < iMax; i++) // x , so here n = width. outside n = number of colors in the map

        { 
        float u = i / (iMax - 1.0f);
        // Test image formula
        float ramp = u;
        float modulation = 0.05f * std::sin(iMax / 8 * twopi * u);
        float value = ramp + v * v * modulation;
        // Applying colormap
        int k = std::round(value * (colormap_size / 3 - 1));
        if (k < 0)
            {k = 0;}
            else if (k >= colormap_size / 3) k = colormap_size / 3 - 1;

        // add RGB string to s string
        s +=  std::to_string(srgb_colormap[3 * k + 0]) + ' '
        +     std::to_string(srgb_colormap[3 * k + 1]) + ' '
        +     std::to_string(srgb_colormap[3 * k + 2]) + '\n';
        }}
    return s;
}

It gives Segmentation fault

What should I change ?

marlam commented 7 months ago

I don't have time to debug this, but it seems you have 0 <= k < jMax*iMax and then use k as an index to srgb_colormap, where it should be 0 <= k < n.