BerkleyJ
BerkleyJ

Reputation: 13

Creating a 2D-Array to output a pmm file C++

The instructions that were given to me were very vague and I'm completely lost on what I'm supposed to do. Need some help. I'll try to explain and hopefully someone can help me.

I need to generate a ppm file of specified a width and height when you set color of the pixel (i,j) with the value col as in:

float col = float (((i & 0x08) == 0) ^ ((j & 0x08) == 0));

you may want to use the following structure for RGB color:

struct RGB {
    float r;
    float g;
    float b;
};

Each element in the array is in the range of [0.0, 1.0] and must be converted to [0, 255] before writing to the file.

This is about as much information as I have. I really just lost on what he actually wants me to do. This may be enough information for someone more experienced but not for me. Can someone elaborate on this. Also, what the hell does that first formula/code snippet do?

I still dont what the i and j are or what the formula is for or how to generate the values to write to the 2d array of RGB!?!?

Upvotes: 0

Views: 1668

Answers (2)

sdkljhdf hda
sdkljhdf hda

Reputation: 1407

PPM is really simple bitmap format, you have basically described it in your question. To see more details google it or chec out http://netpbm.sourceforge.net/doc/ppm.html .

To convert from range [0.0 .. 1.0] to [0 .. 255] you simply need to perform multiplication by 255.0 for each pixel component.

To summarize: write ppm header to file, iterate over all pixels, multiply its components by 255, convert to intever, write to file as text. All values in file are separated by space.

The first code snippet tells you how to generate the picture. It basically means that the color is white if fourth bit from the right is different in i and j coordinate and black if they are the same. This should generate a checkerboard pattern with 16 by 16 squares.

Upvotes: 1

borisbn
borisbn

Reputation: 5054

In the first formula you will get not values in range of [0 1], but only 0s and 1s, because operator == returns either true or false (0 or 1 in bit's representation). The operation XOR ( ^ ) over 0 and/or 1 will return also either 0 or 1.

Here is a truth table for operation XOR

-----------------
| a | b | a ^ b |
-----------------
| 0 | 0 |   0   |
-----------------
| 0 | 1 |   1   |
-----------------
| 1 | 0 |   1   |
-----------------
| 1 | 1 |   0   |
-----------------

Conversion from range [0 1] to [0 255] is just a multiplication the value by 255.

void convertTo_255( struct RGB * array, size_t count {
    for ( size_t i = 0; i < count; i++ ) {
        array[ i ].r *= 255;
        array[ i ].g *= 255;
        array[ i ].b *= 255;
    }

Upvotes: 0

Related Questions