Bitmap Noise Generator – C++ Code

I came across some old C++ files I had saved on a backup and found this one particularly interesting. I always loved to use noise in my textures for 3D models and grew tired of opening Photoshop when I needed a quick noise map to tie into a material (specifically when making materials in Unreal Engine). This is where the follow code came into play. I remember having a version that would ask if you wanted monochrome or color noise, what width and height you wanted and the ‘strength’ of the noise pattern (basically it would clamp the values from 1 to 256 closer or further apart from each other to handle how strong the noise was), but I cannot find that file for some reason… Maybe some day I’ll actually make this an ‘app’ – apps weren’t really a ‘thing’ when I originally wrote this circa 2005… we called them ‘programs’ 😉 – anyway, here’s a good intro on writing to files with C++ and also how to create bitmaps. I’m sure some of this could be changed/optimized, as C++ has really changed since 2005, but for now this is what you get!

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

#define FHEADERSIZE 14

typedef struct{
    unsigned short type;
    unsigned int fSize;
    unsigned short reserved1;
    unsigned short reserved2;
    unsigned int offset;

typedef struct{
    unsigned int hSize;
    int iWidth;
    int iHeight;
    unsigned short iPlanes;
    unsigned short iBPP; //Bits per pixel
    unsigned int iCompression;
    unsigned int iSize;
    int i_xPPM; //Pixel per Meter
    int i_yPPM;
    unsigned int iClrs;
    unsigned int iClrsImp;

typedef struct{
    unsigned char b;
    unsigned char g;
    unsigned char r;
}RGBColor; //24 Bit Color Struct

int main(void)
    ofstream bmpfile;"example.bmp", ios::out | ios::binary | ios::trunc);
        cout << "ERROR: FILE COULD NOT BE OPENED" << endl;
        return 1;
    int width = 1024;
    int height = 1024;
    BMPFileHeader fHeader;   //File Header 
    fHeader.type = 19778;    //BM
    fHeader.fSize = FHEADERSIZE + sizeof(BMPInfoHeader) + sizeof(RGBColor)*width*height;   //File Size info
    fHeader.fSize = sizeof(fHeader) + sizeof(RGBColor);
    fHeader.reserved1 = 0;
    fHeader.reserved2 = 0;
    fHeader.offset = FHEADERSIZE + sizeof(BMPInfoHeader);    //Where we actually start writing the Bitmap data/image
    bmpfile.write((char*)(&fHeader.type), sizeof(fHeader.type));     //Let's start writing the file
    bmpfile.write((char*)(&fHeader.fSize), sizeof(fHeader.fSize));   //Header information
    bmpfile.write((char*)(&fHeader.reserved1), sizeof(fHeader.reserved1));
    bmpfile.write((char*)(&fHeader.reserved2), sizeof(fHeader.reserved2));
    bmpfile.write((char*)(&fHeader.offset), sizeof(fHeader.offset));
    BMPInfoHeader iHeader;
    iHeader.hSize = sizeof(BMPInfoHeader);
    iHeader.iWidth = width;
    iHeader.iHeight = height;
    iHeader.iPlanes = 1;
    iHeader.iBPP = 24;
    iHeader.iCompression = 0; //Uncompressed
    iHeader.iSize = width * height * 3;
    iHeader.i_xPPM = 0;
    iHeader.i_yPPM = 0;
    iHeader.iClrs = 0;
    iHeader.iClrsImp = 0;
    bmpfile.write((char*)&iHeader, sizeof(BMPInfoHeader));   //More Header information being placed in the file
    RGBColor* image;
    image = new RGBColor[width * height];

    //Write each R G B value (this will produce color noise - for B&W, make each RGB value the same for every iteration
    for(int i = 0; i < width * height; i++) 
        image[i].r = rand() % 256 + 1;
        image[i].g = rand() % 256 + 1;
        image[i].b = rand() % 256 + 1;

    //Write our arrays to the file

    for(int i = 0; i < width * height; i++)
        bmpfile.write((char*)&image[i], sizeof(RGBColor));

    // remove our temp image array
    delete [] image;

   // close the file
    return 0;

I also put this up on Github, if that’s your thing:

Leave a Reply

%d bloggers like this: