Create grid of images with increasing grey scale values

Viewed 122

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]: https://i.stack.imgur.com/BmGOZ.png [2]: https://i.stack.imgur.com/FMpi1.png

using namespace std;

#include <fstream>
#include <iostream>


int main() 
{
    char img[256][256];
    ofstream binaryFile("file.raw", ios::out | ios::binary);
    if (!binaryFile) {
        cout << "cannot create file";
        return 1;
    }
    //create raw file 
    char color = 0;

    //nested for loops to iterate the pixel with varying grey scale values
    for (int y = 0; y < 15; y++) {
        for (int i = 0; i < 256; i++) {
            for (int j = 0; j < 256; j++) {
                img[i][j] = color;
            }
        }
        color = color + 15;
        for (int i = 0; i < 256; i++) {
            img[0][i] = 0;
            img[i][0] = 0;
            img[255][i] = 0;
            img[i][255] = 0;
        }
        



        for (int i = 0; i < 256; i++) {
            for (int j = 0; j < 256; j++) {
                binaryFile.write((char*)&img[i][j], sizeof(img[i][j]));
            }
        }
    }
    // end raw file editing
    binaryFile.close();
    if (!binaryFile.good()) {
        cout << "Error occurred at writing time!" << endl;
        return 1;
    }
}
2 Answers

So each row of pixels (from left to right) spans 4 different colored squares (4 columns using x), and each square is 256 pixels wide:

for (int x = 0; x < 4; x++) {
    for (int j = 0; j < 256; j++) {
        // write one pixel here
    }
}

Each column of pixels (from top to bottom) also spans 4 different colored squares (4 rows using y), and each square is 256 pixels high:

for (int y = 0; y < 4; y++) {
    for (int i = 0; i < 256; i++) {
        // inner loop here
    }
}

Then all you have to do is to determine the color of each square. The color should advance 1 "increment" of 17 (max color / number of increments = 255 / 15) for each row. And each row should advance the color 4 "increments" of 17.

Now i hear you say, 17? That can't be right. Just hold on a bit longer.

For each column x, increment by 1. And for each row y, increment by 4. That comes down to: x + ( y * 4 ). Apply the increment 17 like we said before, and you get: ( x + ( y * 4 ) ) * 17. Since the * takes precedence over +, the internal brackets ( ) are not needed, leaving just: (x+y*4)*17.

That will make the colors for each square look like this:

Col 0 Col 1 Col 2 Col 3
Row 0 0 17 34 51
Row 1 68 85 102 119
Row 2 136 153 170 187
Row 3 204 221 238 255

See? Nicely spaced colors, starting on 0 and ending on 255.

Putting it all together:

for (int y = 0; y < 4; y++) {
    for (int i = 0; i < 256; i++) {
        for (int x = 0; x < 4; x++) {
            char color = (x+y*4)*17;
            for (int j = 0; j < 256; j++) {
                binaryFile.put(color);
            }
        }
    }
}

enter image description here

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));
    }
}
Related