Search code examples
c++imageppm

How to change RGB values in c++


I apologize for the a question that may be considered "newbie," but I just started learning how to use c++. I know how to read and write text files, but if I want to open an image file, will it be the same as opening a normal text file, or is it different? I believe it is different because when I drag the file to CodeBlocks to just examine it, it just uses numbers. For example, row 4 is 13 33 66, and row 19 is 15 28 80.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    ifstream file_reader("file_name_example.ppm", ios::binary | ios::out);

    if (! file_reader.is_open())
    {
        cout << "The file cannot open." << endl;
    }


return 0;

I only want to change the blue value of the image, so I understand I will need to do a for-loop to do every third value (R G B). My question is how exactly do I do this? After I open the file, how to I replace every third number with another value?

I really appreciate any help. Thank you!


Solution

  • I feel like this question's potential was ignored because of the irrelevant snippet of code and the misleading opening and reading text file part.

    You can replace change and replace RGB values in bitmaps with HBITMAP in windows.h(MFC)

    The Solution:

    HBITMAP hBmp;
    CCloneBitmap bmpClone;
    HICON hIcon;
    hBmp=LoadBitmap(AfxGetResourceHandle(),MAKEINTRESOURCE(ID_LIGHTCAR));
    if(hBmp!=NULL)
    {
      bmpClone.Clone(hBmp);
      DeleteObject(hBmp);
      bmpClone.ChangeColor(IRGB(0,0,0), IRGB(255,0,0));
      // change BLACK pixels to RED ones. 
      //Change the IRGB values to whatever you would like - blue - IRGB(0,0,255) 
    }