c++multidimensional-arrayppm

How do I initialize values of a 2d array of structs?


Im writing a ppm file with a 2d array of structs which act as the rgb values. The file writes, but when I try to pass the struct which contains the color value I want through my drawRect and drawCircle functions, the image does not draw with the dimensions I intended. The pixel values aren't written correctly into the array as well. I have separate functions that draw rectangles and circles, and createImg draws the entire image.

struct Color {
unsigned char red;
unsigned char green;
unsigned char blue;
};
int main() {

static Color image[LENGTH][WIDTH] = {};
createImg(image);
writeImg(image, 300, "torinCostales.ppm");

}

void createImg(Color image[][WIDTH]) {

//variables for red green and blue
Color red = { 255, 0, 0 };
Color green = { 0, 255, 0 };
Color blue = { 0, 0, 255 };

//dimensions and color value for the 4 different shapes
drawRect(image, 20, 15, 75, 150, blue);
drawRect(image, 100, 20, 100, 50, red);
drawCircle(image, 100, 200, 50, green);
drawCircle(image, 150, 150, 50, red);

//-------------------------------------------------------------------
//draws circle with given dimensions and sets array values to colorLevel
void drawCircle(Color image[][WIDTH],
int centerX, int centerY, int radius, Color colorLevel) {

for (int x = centerX - radius; x <= centerX + radius; x++)

   for (int y = centerY - radius; y <= centerY + radius; y++)
    
        if ((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY) <= radius * radius)
        
            image[x][y] = colorLevel;
 }
        
//--------------------------------------------------------------------
//draws the rectangle with given dimensions and colorLevel
void drawRect(Color image[][WIDTH],
int rectTop, int rectLeft, int rectHeight, int rectWidth, Color colorLevel) {

for (int col = rectLeft; col < rectWidth + rectLeft; col++)

    for (int rows = rectTop; rows < rectHeight + rectTop; rows++)
    
        image[col][rows] = colorLevel;
}

//writes the file
void writeImg(const Color image[][WIDTH], int height, const string fileName) {

std::ofstream imgFile;
imgFile.open("torinCostales.ppm");

//header
imgFile << "P3";
imgFile << "\n" << WIDTH << ' ' << LENGTH;
imgFile << "\n" << "255" << "\n";

//writes pixel values
for (int row = 0; row < LENGTH; row++) {
    for (int col = 0; col < WIDTH; col++)
        imgFile << static_cast<int>(image[row][col].red << image[row][col].green << image[row][col].blue) << ' ';
    imgFile << '\n';
}

imgFile.close();

}
    

Solution

  • Looks like this line is wrong:

    imgFile << static_cast<int>(image[row][col].red << image[row][col].green << image[row][col].blue) << ' ';
    

    Instead of casting each color to int, yous SHIFT the value of red by the number of bits specified by green, then by blue.

    You want this instead:

    imgFile << static_cast<int>(image[row][col].red)   << ' '
            << static_cast<int>(image[row][col].green) << ' '
            << static_cast<int>(image[row][col].blue)  << ' ';