Hello all C++ Experts,
It’s me again. I will get straight to the point.
I’ve successfully got the bitmap image rgb colors pixel which is (blue – 178, green – 130 and red 131).
What I do want to do next is to loop through the pixels and make it such that the picture will be totally blue. (Eg. Blue – 255, green – 0, red – 0)
I did tried a couple of for loops but it does not work, therefore, requiring assistance!
/*for (int i = 0; i < test; i++)
{
image[i].rgbtBlue;
image[i].rgbtGreen;
image[i].rgbtRed;
}*/
But obviously it does not work, that was why i needed the assistance. To add it on, hfile was being initialized for other purposes so i did not think it was relevant. Appreciated all the comments, thank you.
Thank you! As below is the code.
P.S: Using Microsoft Visual Studio 2010 – Win32 application, not Console.
#include "stdafx.h"
#include "winmain.h"
#include "Resource.h"
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <CommDlg.h>
#include <stdint.h>
#include <string>
#include <WinGDI.h>
HANDLE hfile2;
DWORD written;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
RGBTRIPLE *image;
hfile2 = CreateFile(ofn.lpstrFile`, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_OVERLAPPED, NULL);
//Read the header
ReadFile(hfile, &bfh, sizeof(bfh), &written, NULL);
ReadFile(hfile, &bih, sizeof(bih), &written, NULL);
// Read image
int imagesize = bih.biWidth * bih.biHeight; // Helps you allocate memory for the image
image = new RGBTRIPLE[imagesize]; // Create a new image (I'm creating an array during runtime
ReadFile(hfile, image, imagesize * sizeof(RGBTRIPLE), &written, NULL); // Reads it off the disk
get_pixel(bih.biWidth, bih.biHeight);
int test = bih.biHeight * bih.biWidth * bih.biBitCount;
RGBTRIPLE get_pixel(int x,int y)
{
// Image define from earlier
return image[(bih.biHeight-1-y)*bih.biWidth+x];
}
Your comment is pretty much right on. Each element
image[i]is aRGBTRIPLEvariable, which you can read and write. Therefore,setpixelis straightforward:You see that the same code is used to select the right pixel of the image. You’ll probably want to define a
const RGBTRIPLE blue;constant.