Home > OS >  Create grid of images with increasing grey scale values
Create grid of images with increasing grey scale values

Time:01-21

I'm trying to write some C to create a 1048x1048x8 bit matrix of 256x256 squares. The first should have a grey scale value of 0 while the last should be 255. This is what I've tried so far. Any feedback is appreciated. First image is my result. Second is the desired. [1]: enter image description here

CodePudding user response:

Pixel data is written in order of rows. So writing blocks of 256x256 will not do the job. (You can consider this by thinking img[256][256] same as img[256*256]). To make this right, you must write a first row of first 4 blocks, then a second row of first 4 blocks, etc... (Block here means 256x256 section).

I think this code should do:

for (int row = 0; row < 1024;   row) {
    for (int col = 0; col < 1024;   col) {

        // the part ((row / 256) * 4   (col / 256)) will go from 0 to 15
        unsigned char color = ((row / 256) * 4   (col / 256)) * 16;
        
        if (row % 256 == 0 || col % 256 == 0) {
            // This will not draw last border
            color = 0;
        }
        // If you need last border uncomment below, but will reduce last block size by one
        /*
        if (row == 1024 - 1 || col == 1024 - 1) {
            color = 0;
        }
        */
        binaryFile.write((char*) &color, sizeof(color));
    }
}
  •  Tags:  
  • Related