Hey all,
I'm fairly new to programming with C++ so please forgive me if this is a little basic. I've got some code that works on windows but doesn't seem to work with OSX. Basically it loads an image (jontest.bmp, 24 bit) into the memory and then works through the image (all blue except a small white cirlcle) looking for any pixels with an RGB Green value of more than 0. This for now distinguishes between the Blue (Green = 0) and white (Green =255). This is a command line app so in xcode I chose the C++ Tool preset before copying in the code.
Although it works fine on a PC, when on a mac it builds but all I get is an output of 0.
So if anyone could answer my questions below and perhaps shed any light on why it's not working it'd be much appreciated.
Xcode doesn't seem to like the "PAUSE" command in the main.cpp file.
1) Is this because I'm missing a library on the mac?
I'm guessing that I've entered the path of the file incorrectly in the test.cpp.
2) How should it be formatted for OS X?
Also, I'm concerned that OS X won't handle bitmaps the same way as Windows does, so I may have to find an alternate method.
3)Can any one shed any light on this?
It's split over three files:
test.h
void test();
main.cpp
#include <cstdlib>
#include <iostream>
#include "test.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Starting..." << endl;
test();
system("PAUSE");
return EXIT_SUCCESS;
}
test.cpp
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
#define HEADER_LENGTH 54
#define BLUE 0
#define GREEN 1
#define RED 2
ifstream image;
void test()
{
// open the image file for reading
image.open ("~/cplusplus/images/jontest.bmp", ios::in|ios::binary);
// copy the header from the input image
long i;
char headerpixel;
for (i = 0; i < HEADER_LENGTH; i++)
{
// read each byte in the header
image.read (&headerpixel, 1);
}
// Load input image into memory
int x,y;
unsigned char framebuffer[648][365][3];
char pixel[3];
for (i = 0; i < 648*365; i++)
{
image.read (pixel, 3);
x = i%648;
y = i/648;
framebuffer[x][y][BLUE] = pixel[BLUE];
framebuffer[x][y][GREEN]= pixel[GREEN];
framebuffer[x][y][RED] = pixel[RED];
}
// search for pattern
int pixelcounter = 0;
for (i = 0; i < 648*365; i++)
{
x = i%648;
y = i/648;
//If "that pixel's green value is greater than 0
if(framebuffer[x][y][GREEN] > 0)
{
//increment pixelcounter by 1
pixelcounter++;
}
}
// Output the value of pixelcounter to the comand line
cout << pixelcounter << endl;
image.close();
}
Thanks in advance,
Jon
I'm fairly new to programming with C++ so please forgive me if this is a little basic. I've got some code that works on windows but doesn't seem to work with OSX. Basically it loads an image (jontest.bmp, 24 bit) into the memory and then works through the image (all blue except a small white cirlcle) looking for any pixels with an RGB Green value of more than 0. This for now distinguishes between the Blue (Green = 0) and white (Green =255). This is a command line app so in xcode I chose the C++ Tool preset before copying in the code.
Although it works fine on a PC, when on a mac it builds but all I get is an output of 0.
So if anyone could answer my questions below and perhaps shed any light on why it's not working it'd be much appreciated.
Xcode doesn't seem to like the "PAUSE" command in the main.cpp file.
1) Is this because I'm missing a library on the mac?
I'm guessing that I've entered the path of the file incorrectly in the test.cpp.
2) How should it be formatted for OS X?
Also, I'm concerned that OS X won't handle bitmaps the same way as Windows does, so I may have to find an alternate method.
3)Can any one shed any light on this?
It's split over three files:
test.h
void test();
main.cpp
#include <cstdlib>
#include <iostream>
#include "test.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Starting..." << endl;
test();
system("PAUSE");
return EXIT_SUCCESS;
}
test.cpp
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
#define HEADER_LENGTH 54
#define BLUE 0
#define GREEN 1
#define RED 2
ifstream image;
void test()
{
// open the image file for reading
image.open ("~/cplusplus/images/jontest.bmp", ios::in|ios::binary);
// copy the header from the input image
long i;
char headerpixel;
for (i = 0; i < HEADER_LENGTH; i++)
{
// read each byte in the header
image.read (&headerpixel, 1);
}
// Load input image into memory
int x,y;
unsigned char framebuffer[648][365][3];
char pixel[3];
for (i = 0; i < 648*365; i++)
{
image.read (pixel, 3);
x = i%648;
y = i/648;
framebuffer[x][y][BLUE] = pixel[BLUE];
framebuffer[x][y][GREEN]= pixel[GREEN];
framebuffer[x][y][RED] = pixel[RED];
}
// search for pattern
int pixelcounter = 0;
for (i = 0; i < 648*365; i++)
{
x = i%648;
y = i/648;
//If "that pixel's green value is greater than 0
if(framebuffer[x][y][GREEN] > 0)
{
//increment pixelcounter by 1
pixelcounter++;
}
}
// Output the value of pixelcounter to the comand line
cout << pixelcounter << endl;
image.close();
}
Thanks in advance,
Jon