Windows C++ in Xcode

Discussion in 'Mac Programming' started by jonkirk, Feb 18, 2007.

  1. macrumors newbie

    Joined:
    Feb 18, 2007
    Location:
    London, UK
    #1
    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
     
  2. macrumors member

    Joined:
    Feb 18, 2007
    Location:
    Eindhoven
    #2
    1) system() passes a command to the shell. "PAUSE" is not a command in OSX/Unix. Has nothing to do with missing libraries.

    2) The file is entered as a string: "~/cplusplus/images/jontest.bmp". The iostream library cannot expand it, since the expansion is Unix specific. Use the full path ("/Users/name/cplusplus/images/jontest.bmp"), a relative path or get the filename from the command prompt, using *argv[]. That way the shell does the expansion.

    3) I don't see why OSX would handle bitmaps any different from Windows.

    Wittegijt
     
  3. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    Just a quick note… if you start reading the bmp header to get the dimensions etc of the image rather than hard coding them, then beware of big/little endian issues if you're writing for a PowerPC based Mac.

    b e n
     
  4. thread starter macrumors newbie

    Joined:
    Feb 18, 2007
    Location:
    London, UK
    #4
    Thanks for the tips guys, I'll have a play over the next few days and let you know how I get on.
     

Share This Page