Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Right now I'm creating procedural textures via noise. So far, I've set up a 512 x 512 JPG image file for the program to write to, and it does, but there's a problem. The problem I'm having is that the noise doesn't fill up the file properly. The noise only seems to fill the bottom of the image like so https://pasteboard.co/r3VwxscTGLNL.jpg[^]

For reference, this is what I expected / want my result to look like (along the lines)
https://i.stack.imgur.com/X95qE.jpg[^]

Here's the code I used (Wrote to image using STB, and created noise using FNL (FastNoise Lite))
C
void write_noise_2d(int w, int h, int channels_num)
{
    fnl_state noise = fnlCreateState();
    noise.noise_type = FNL_NOISE_OPENSIMPLEX2;

    unsigned char noise_data[w * h * channels_num];
    int index = 0;

    for(int x=0; x<w; x++)
    {
        for(int y=0; y<h; y++)
        {
            noise_data[index++] = fnlGetNoise2D(&noise, x, y);
        }
    }

    stbi_write_jpg("textures/noisemap.jpg", w, h, channels_num, noise_data, w * channels_num);
}

write_noise_2d(512, 512, 2);



What I have tried:

Researching through the official documents of both STB and FNL, as well as checking other related posts on multiple forums. Also changed around the color channels and image dimensions to see if that would work.
Posted
Updated 9-May-23 7:49am

Your code generates and array channels_num times the size you fill. Since you don't check the value passed, you app can overwrite memory (if given channels_num = 0 or do what you see: fill only part of the array (if channels_num > 1) And if channels_num gets large, then you app will blow out the stack very, very quickly - they generally are set at 1Mb, so it doesn't take much to smash it completely. Change your code to use malloc and free instead of local allocation, and fill the whole thing to see the image.
 
Share this answer
 
Comments
Chillzy 8-May-23 15:18pm    
I did what you told me to, and now the image is just pure black, no variations. On the plus side though, the images are only around 8 kb now instead of 50 so that's a plus.
OriginalGriff 8-May-23 15:38pm    
Without access to your modified code, we can't help you debug it - you should use the debugger for that.
But the old size sounds about right - jpg is a (lossy) compressed format, so a 8bit by 512 x 512 "raw image" coming down to 50K isn't that odd.
But ... bear in mind that jpg is a lossy compression - it effectively averages a bunch of pixels into a single pixel so random data isn't going to do well! I'd suggest that at least for dev, you should be considering BMP format instead of any compression (non-lossy compressions can increase file size as they have to add information to data that shouldn't have much compressibility!)
OriginalGriff 8-May-23 15:45pm    
I'd consider starting here:
chan blog: How to use stb_image_write?[^]
And checking that your output works at all to "prove the system" before you start filling with random data.
Chillzy 9-May-23 11:07am    
Couldn't finish it yesterday because of errands but the source code from the blog just worked so I'm going to try to use it for the noise data, appreciate the help
OriginalGriff 9-May-23 11:34am    
You're welcome!
On a different forum, I got clarity on how I should render the noise, this is the code I was provided that actually worked.

C
void write_noise_2d(int w, int h, int channels_num)
{
    // initialize noise
    fnl_state noise = fnlCreateState();
    noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
    noise.octaves = 8;
    // create data array for noise
    float* noise_data = malloc(w * h * channels_num * sizeof(float));
    int index = 0;
    // create noise throughout the entire image
    for(int x=0; x<w; x++)
    {
        for(int y=0; y<h; y++)
        {
            noise_data[index++] = (1 + fnlGetNoise2D(&noise, x, y)) * 127.999;
        }
    }

    stbi_write_jpg("textures/noisemap.jpg", w, h, channels_num, noise_data, w * channels_num);
    free(noise_data);
}

write_noise_2d(512, 512, 1);


Quick little update for future readers, it turns out that I didn't even need to do any of that other stuff in the noise calculuations, I just needed to use a bmp, the whole
noise_data[index++] = (1 + fnlGetNoise2D(&noise, x, y)) * 127.999;


Was unnecesssary
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900