Search code examples
c++imageimage-processingppmimage-formats

Save RGB image as PPM format


I am trying to create RGB image in C++. I am not using any image libraries like OpenCv.

Firstly, I tried to create grayscale image. I want to draw rectangle to image. I am giving parameters at function like starting points, width, height etc. This code lines are working good for this grayscale challenge but I am trying to increase color channels to 3 like RGB. Then, I am setting Red, Green and Blue values but it is not working. This is my problem.

How can I work correctly?

  • x => starting point where x coordinate
  • y => starting point where y coordinate
  • width => rectangle width
  • height => rectangle height
  • value => RGB or Grayscale color value

My codes

Image::Image(int width, int height, int n_channels, int step)
{
    cout << "Image constructor is running!" << endl;
    m_width = width;
    m_height = height;
    m_n_channels = n_channels;

    m_step = m_width*m_n_channels;
    if (m_step < step)
            m_step = step;
    m_data = new uchar[m_step*height];
}


Image* Image::new_gray(int width, int height)
{
    cout << "New gray image is creating!" << endl;
    return new Image(width, height, 1);
}

Image* Image::new_rgb(int width, int height)
{
    cout << "New RGB image is creating!" << endl;
    return new Image(width, height, 3);
} 



void Image::set_rect(int x, int y, int width, int height, uchar value)
{
    if (x < 0) {
        width += x;
        x = 0;
    }

    if (y < 0) {
        height += y;
        y = 0;
    }

    for (int j = y; j < y+height; ++j) {
        if (j >= m_height)
            break;
        uchar* row_data = data(j);
        for (int i = x; i < x+width; ++i) {
            if (i >= m_width)
                break;
            for (int c = 0; c < m_n_channels; ++c)
                if (c == 0) {
                    row_data[i*m_n_channels + c] = value;
                } else if (c == 1) {
                    row_data[i*m_n_channels + c] = value;
                } else if (c == 2) {
                    row_data[i*m_n_channels + c] = value;
                }
        }
    }
}


bool Image::write_pnm(const std::string& filename) const
{
    if (m_n_channels != 1) {
            const string magic_head = "P6";
            ofstream fout;
            string extended_name = filename + ".ppm";
            fout.open(extended_name.c_str(), ios::out | ios::binary);
            fout << magic_head << "\n";
            fout << m_width << " " << m_height << " 255\n";
            for (int y = 0; y < m_height; ++y) {
                    const uchar *row_data = data(y);
                    cout << reinterpret_cast<const char*>(row_data);
                    fout.write(reinterpret_cast<const char*>(row_data), m_width*sizeof(uchar));
            }
            fout.close();
            return true;
    }

    const string magic_head = "P5";
    ofstream fout;
    string extended_name = filename + ".pgm";
    fout.open(extended_name.c_str(), ios::out | ios::binary);
    fout << magic_head << "\n";
    fout << m_width << " " << m_height << " 255\n";
    for (int y = 0; y < m_height; ++y) {
            const uchar *row_data = data(y);
            fout.write(reinterpret_cast<const char*>(row_data), m_width*sizeof(uchar));
    }
    fout.close();

    return true;
}

My main function

#include <cstdlib>
#include <iostream>

#include "image.h"

using std::cout;
using std::endl;
using ceng391::Image;

int main(int argc, char** argv)
{
    Image* gray = Image::new_gray(128, 128);
    cout << "(" << gray->w() << "x" << gray->h() << ") channels: "
         << gray->n_ch() << " step: " << gray->step() << endl;
    gray->set_zero();
    gray->set_rect(32, 32, 64, 64, 255);
    gray->write_pnm("/tmp/test_image");


    Image* rgb_image = Image::new_rgb(128,128);
    cout << "(" << rgb_image->w() << "x" << rgb_image->h() << ") channels: "
         << rgb_image->n_ch() << " step: " << rgb_image->step() << endl;
    rgb_image->set_zero_rgb();
    rgb_image->set_rect(32, 32, 64, 64, 150);
    rgb_image->write_pnm("/tmp/test_image_rgb");


    delete gray;
    delete rgb_image;


    return EXIT_SUCCESS;
}

Solution

  • This code is working for grayscale images because grayscale images have same number of pixels with width.

    fout.write(reinterpret_cast<const char*>(row_data), m_width*sizeof(uchar));
    

    But when I wanted to save RGB images, increased number of pixels 3 times. One pixel respresents via 3 channels so need to multiply stream size with 3 (R,G,B channels) for one pixel.

    fout.write(reinterpret_cast<const char*>(row_data), m_width*sizeof(uchar)*3);