Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8897241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:17:20+00:00 2026-06-15T00:17:20+00:00

I took a Bitmap image and pulled the R, G, B ints from it

  • 0

I took a Bitmap image and pulled the R, G, B ints from it for a specified row of pixels. Converted the int to a string so that I could print my 6 specific colors. I couldn’t figure out how to do it with int.

The Problem
I am able to print row 0-184 (corresponding to pixels in that row) out as a sequential data 1234… or color red, red, red, black, black, gray….

However I need to count like/same colors, display the sum of like colors, and reset a counter until that color comes up again then recount. I thought an if or if else would do it but not quite. It may be my code structure that is causing an issue?

So what I desire is:

5   red,
10  black,
2   red,
1   gray,

etc……

Here is my code, I am a beginner so criticizes on my lack of knowledge so that I may learn properly.

#include <iostream>
#include <sstream>
#include <string>
#include "EasyBMP.h"
#include "EasyBMP_BMP.h"
#include "EasyBMP_DataStructures.h"
#include "EasyBMP_VariousBMPutilities.h"

//Conversion and comparison function
void calculate(int i, int x, int p);

int main(int argc, const char * argv[])
{

BMP Image;
Image.ReadFromFile( "BMP GOES HERE 24bit" );

std::cout << "Image Height and Width: " << Image.TellHeight() << " x " << Image.TellWidth() << std::endl;

std::cout << "Enter your row: ";

int pixX = 0;
std::cin >> pixX;

//Set getpixel to top of row
int pixY  = 0;

for( pixY = 0; pixY < Image.TellHeight() ; pixY++ )
{
    std::cout << "Pixel: " << pixY + 1;

    RGBApixel Temp = Image.GetPixel(pixX,pixY);

    //Array to store pixel color ints
    int pixy[3];
    pixy[0] = Temp.Red;
    pixy[1] = Temp.Green;
    pixy[2] = Temp.Blue;

    calculate(pixy[0], pixy[1], pixy[2]);
}

return 0;
}


void calculate(int rnum, int gnum, int bnum)
{

//String which will contain the result
std::string result;

//Stream used for the conversion
std::ostringstream convert;

//Insert the textual representation of 'Number' in the characters in the stream
convert << rnum;                

convert << gnum;

convert << bnum;

// set 'Result' to the contents of the stream
result = convert.str();    

// compare result to my given value
if (result == "25500")
{
    std::cout << " RED  " << std::endl;
}
if (result == "255255255")
{
    std::cout << " WHITE " << std::endl;
}
if (result == "000")
{
    std::cout << " BLACK" << std::endl;
}
if (result == "148148148")
{
    std::cout << " GRAY " << std::endl;
}
if (result == "267326")
{
    std::cout << " GREEN " << std::endl;
}
if (result == "2551260")
{
    std::cout << " ORANGE " << std::endl;
}
}

Following is the working code. Note that if you use it that my image only has 6 specific colors. To change the print out one would have to modify the switch statement cases as desired.

#include <iostream>
#include <vector>
#include "EasyBMP.h"    
#include "EasyBMP_BMP.h"
#include "EasyBMP_DataStructures.h"
#include "EasyBMP_VariousBMPutilities.h"

long toRGB(long red, long grn, long blu);


int main(int argc, const char * argv[])
{

BMP Image;
Image.ReadFromFile( "Your BMP HERE" );

std::cout << "Image Height and Width: " << Image.TellHeight() << " x " << Image.TellWidth() << std::endl;

std::cout << "Enter your row: ";

int pixX = 0;
std::cin >> pixX;
if (pixX != 0)                              //Subtract one from input if not 0, image starts at 0,0
{
    pixX -= 1;
}

long pop  = 0;
long pop1 = 0;

RGBApixel current = Image.GetPixel(pixX,0);

long pixy1[3];                                        //Array to store pixel color ints
pixy1[0] = current.Red;
pixy1[1] = current.Green;
pixy1[2] = current.Blue;

pop1 = toRGB(pixy1[0], pixy1[1], pixy1[2]);


int count = 0;
for( int pixY = 0; pixY < Image.TellHeight() ; pixY++ )
{
    RGBApixel Temp = Image.GetPixel(pixX,pixY);

    long pixy[3];                                        //Array to store pixel color ints
    pixy[0] = Temp.Red;
    pixy[1] = Temp.Green;
    pixy[2] = Temp.Blue;

    pop = toRGB(pixy[0], pixy[1], pixy[2]);

    if (pop == pop1)
    {
        count++;
    }
    else
    {
        switch (pop1) {
            case 0:
                std::cout << "(" << count << ")\t" << "BLACK\n" << std::endl;
                break;
            case 16711680:
                std::cout << "(" << count << ")\t" << "RED\n" << std::endl;
                break;
            case 9737364:
                std::cout << "(" << count << ")\t" << "GRAY\n" << std::endl;
                break;
            case 16777215:
                std::cout << "(" << count << ")\t" << "WHITE\n" << std::endl;
                break;
            case 1722650:
                std::cout << "(" << count << ")\t" << "GREEN\n" << std::endl;
                break;
            case 16743936:
                std::cout << "(" << count << ")\t" << "ORANGE\n" << std::endl;
                break;
            default:
                std::cout << " !!!NO Specified COLOR For!!! " << pop1 << std::endl;
                break;
        }

        pop1 = pop;                                     //Reset the count and current     color
        count = 1;
    }
}
    if (count > 0)                                      //Returns last color and count
    {
        switch (pop1) {
            case 0:
                std::cout << "(" << count << ")\t" << "BLACK\n" << std::endl;
                break;
            case 16711680:
                std::cout << "(" << count << ")\t" << "RED\n" << std::endl;
                break;
            case 9737364:
                std::cout << "(" << count << ")\t" << "GRAY\n" << std::endl;
                break;
            case 16777215:
                std::cout << "(" << count << ")\t" << "WHITE\n" << std::endl;
                break;
            case 1722650:
                std::cout << "(" << count << ")\t" << "GREEN\n" << std::endl;
                break;
            case 16743936:
                std::cout << "(" << count << ")\t" << "ORANGE\n" << std::endl;
                break;
            default:
                std::cout << " !!!NO Specified COLOR For!!! " << pop1 << std::endl;
                break;
    }
}

return 0;
}

long toRGB(long a, long b, long c)                          //Function to convert R, G, B      values to unique value
{
long color = 0;
color |= (a & 255) << 16;
color |= (b & 255) << 8;
color |= (c & 255);

return color;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:17:22+00:00Added an answer on June 15, 2026 at 12:17 am

    I didn’t really understand your question before, so I wrote a new answer more appropriate to what you’re asking.

    I think you can get what you want with something like this (modifying your code):

    RGBApixel current = Image.GetPixel(pixX,0);
    int count = 0;
    for( pixY = 0; pixY < Image.TellHeight() ; pixY++ )
    {
        RGBApixel Temp = Image.GetPixel(pixX,pixY);
        if (Temp == current)
        {
             count++;
        }
        else
        {
             // same-color sequence ended
             // Add code here to print out current color and count
             --- your output code ----
    
             // now reset the count and current color
             current = Temp;
             count = 1;
        }
    }
    
    // Now just print out the last sequence
    if (count > 0)
    {
        --- your output code here again ---
    }      
    

    One thing I’m not sure is how, if at all, the == operator is defined for RGBApixel. If it isn’t defined or doesn’t seem to equate pixels based on their color, just write a function like pixelsAreEqual(RBGApixel p1, RGBApixel p2) that takes two pixels and returns true if they have the same RGB values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I took some code from the internet that searches data entered in a table
For my app I have created a QR Code, then took that bitmap and
it took me some time to find out that both Eclipse and Aptana get
I took the rendered page from the SWT Browser and exported it to an
I took the example code from the Kendo UI demos at http://demos.kendoui.com/web/grid/remote-data.html , binding
I took Computer Architecture course and I understood that processor has 32 registers each
I took apple's sample code tweeting and modified it so that it doesn't use
there are many related questions/answers about that in SO. I took a look to
I am trying to fetch a Device Independent Bitmap from the clipboard. I am
I took a hiatus from C and am just getting back into it again.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.