Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test image #2

Open
adammaj1 opened this issue Nov 29, 2023 · 1 comment
Open

test image #2

adammaj1 opened this issue Nov 29, 2023 · 1 comment

Comments

@adammaj1
Copy link

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
Copy link
Owner

marlam commented Dec 5, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants