Thread: can I do this?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    12

    can I do this?

    I'm working on a program that modifies images. It's a very simple program that uses 2d arrays and all that fun stuff. I'm trying to transpose (rotate it 90 degrees one way), however, when I make that selection (in .exe) the program just hangs... here is my transpose function:

    void transpse(Pixel image_mod[][800], int w, int h)
    {
    Pixel transposed[MAXW][MAXH];

    for(int i = 1; i < w; i++)
    {
    for(int j = 1; j < h; j++)
    {
    transposed[j][i] = image_mod[i][j];
    }
    }

    for(int i = 1; i < w; i++)
    {
    for(int j = 1; j < h; j++)
    {
    image_mod[i][j] = transposed[i][j];
    }
    }
    }
    -----------------------------------------------------------------------------
    it all seems correct...but it doesn't do anything... Do you guys see anything?? Let me know if you want to see anything else.

    SAW

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    12
    hehehe...silly me... Okay, my problem wasn't with my void function it was with my while loop in my selection menu... let me show you:
    -----------------------------------------------------------------------------
    cout << "You can now modify the image you chose with the following choices: " << endl;
    cout << "You can keep making changes to the picture until you get tired." << endl;
    cout << "\t S - Smooth" << endl;
    cout << "\t T - Transpose (Turn the image to the side)" << endl;
    cout << "\t D - Dither (create a checkerboard effect)" << endl;;
    cout << "\n brightest to darkest. The image will be no more.)" << endl;
    cout << "\t G - Gray Scale (makes the image gray)" << endl;
    cout << "\t E - Exit modifications." << endl;
    cin >> menu;
    while(menu != 'E')
    {
    switch(menu)
    {
    case 'D':
    {
    dither(image, W, H);
    break;
    }
    case 'G':
    {
    make_grey(image, W, H);
    break;
    }
    case 'S':
    {
    smooth(image, W, H);
    break;
    }

    case 'T':
    {
    transpose(image, W, H);
    break;
    }
    }
    }

    -----------------------------------------------------------------------------
    )okay...so now...my question is. How do I make it to where I can keep chosing and it keeps going through until some one makes a choice to exit??

    SAW

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    put the input statement in the while, before the switch... if you choose to put it at the end of the while, preset the input variable as a precaution since an uninitialized variable could very well have the exit command as a value...
    hasafraggin shizigishin oppashigger...

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    12
    there's an input statement before the while...

    SAW

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You could add another case to your menu.....

    case q: exit(0);

    this will cause your program to exit. you need stdlib.h or cstdlib included.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    12
    well..my problem now is that I can make numerous choices...but it'll only do the first choice. It won't do any other ones after making the first choice...

    SAW

  7. #7
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    there's a check of the input, but no refresh of the input... post your latest source so we get a better idea of how to mold your code correctly...
    hasafraggin shizigishin oppashigger...

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    12
    Okie...this is my entire code... except for the .h files. sorry if it's very long.

    ---------------------------------------------------------------------------------

    #include <iostream>
    #include <string>
    #include "Pixel.h"

    using namespace std;

    const int MAXH = 800;
    const int MAXW = 800;

    void read_header(ifstream& fin, ofstream& fout, int& width, int& height, int& max);
    void make_grey(Pixel image_mod[][800], int w, int h);
    void dither(Pixel image_mod[][800], int w, int h);
    void smooth(Pixel image_mod[][800], int w, int h);
    void transpose(Pixel image_mod[][800], int w, int h);
    void make_purple(Pixel image_mod[][800], int w, int h);
    void make_blue(Pixel image_mod[][800], int w, int h);
    void make_green(Pixel image_mod[][800], int w, int h);
    void make_bright(Pixel image[][800], int w, int h, int color);

    int main()
    {
    Pixel image[MAXW][MAXH];
    ifstream fin;
    ofstream fout;
    int W, H, colormax;
    string image_file;
    char menu, anykey;

    cout << "Please enter the name of the image you'd like to use: " << endl;
    cin >> image_file;

    if(fin.fail())
    {
    cout << "File was unable to open. Check syntax and try again." << endl;
    return 0;
    }

    cout << "Gathering Data from the image, please wait." << endl << endl;

    fin.open(image_file.c_str());
    image_file= "mod_" + image_file;
    fout.open(image_file.c_str());

    read_header(fin, fout, W, H, colormax);

    for(int column = 0; column < W; column++)
    {
    for(int row = 0; row < H; row++)
    {
    fin >> image[column][row];
    }
    }

    cout << "You can now modify the image you chose with the following choices: " << endl;
    cout << "You can keep making changes to the picture until you get tired." << endl;
    cout << "\t S - Smooth" << endl;
    cout << "\t T - Transpose. Will turn the image to the side)" << endl;
    cout << "\t D - Dither, creates a checkerboard effect." << endl;
    cout << "\t b- Brighten, will brighten or darken the image." << endl;
    cout << "\t g - Make the image gray." << endl;
    cout << "\t P - Make the image purple." << endl;
    cout << "\t B - Make the image blue." << endl;
    cout << "\t G - Make the image green." << endl;
    cout << "\t O - Make other." << endl;
    cout << "\t E - Exit modifications." << endl;
    cout << "\nYour Selection: " << endl;

    cin >> menu;

    while(menu != 'E')
    {
    switch(menu)
    {
    case 'D':
    {
    dither(image, W, H);
    break;
    }
    case 'g':
    {
    make_grey(image, W, H);
    break;
    }
    case 'S':
    {
    smooth(image, W, H);
    break;
    }
    case 'T':
    {
    transpose(image, W, H);
    break;
    }
    case 'P':
    {
    make_purple(image, W, H);
    break;
    }
    case 'B':
    {
    make_blue(image, W, H);
    break;
    }
    case 'G':
    {
    make_green(image, W, H);
    break;
    }
    case 'b':
    {
    make_bright(image, W, H, colormax);
    break;
    }
    }

    for (int columnout = 0; columnout < W; columnout++)
    {
    for(int rowout = 0; rowout < H; rowout++)
    {
    fout << image[columnout][rowout];
    }
    }
    cout << "Make another selection or E to exit." << endl;
    cin >> menu;
    }
    cout << "Hit any key to exit." << endl;
    cin >> anykey;
    fin.close();
    fout.close();
    return 0;
    }
    //************************************************** ***********************
    void read_header(ifstream& fin, ofstream& fout, int& width, int& height, int& max)
    {
    char header[100];
    char ch;
    /* get magic number */
    fin.getline(header, 100);
    /* write magic number */
    fout << header << endl;
    cout << header << endl;

    /* get all comment lines and write to new file */
    fin >> ch;
    while(ch == '#')
    {
    fin.getline(header, 100);
    fout <<ch << header << endl;
    cout << ch <<header << endl;
    fin >> ch;
    }
    fin.putback(ch);
    /* input width and height of image */
    fin >> width >> height;
    cout << width <<" " << height << endl;
    fout << width << " " << height << endl;
    /* input maximum color value */
    fin >> max;
    cout << max << endl;
    fout << max << endl;
    return;
    }

    void make_grey(Pixel image_mod[][800], int w, int h)
    {
    int grey;
    for(int i = 0; i < h; i++)
    {
    for(int j = 0; j < w; j++)
    {
    grey = (image_mod[i][j].get_red()+image_mod[i][j].get_blue()+
    image_mod[i][j].get_green())/3;
    image_mod[i][j].set_pixel_value(grey, grey, grey);
    }
    }
    }
    void dither(Pixel image_mod[][800], int w, int h)
    {
    bool z;
    int x, y;
    for (x=0; x < w; x++)
    {
    z=x%2;
    for(y=0; y < h; y++)
    {
    if(z!=0)
    {
    image_mod[x][y].set_pixel_value(0,0,0);
    z =0;
    }
    else
    z=1;
    }
    }
    }

    void smooth(Pixel image_mod[][800], int w, int h)
    {
    for(int i = 1; i < h-1; i++)
    {
    for(int j = 1; j < w-1; j++)
    {
    image_mod[i][j] = (image_mod[i][j]+image_mod[i-1][j]+image_mod[i+1][j]+
    image_mod[i][j-1]+image_mod[i][j+1]+image_mod[i][j]+
    image_mod[i+1][j+1]+image_mod[i-1][j-1]+image_mod[i+1][j-1])
    /9;
    }
    }
    }

    void transpose(Pixel image_mod[][800], int w, int h)
    {
    //int maxfill = 800;
    Pixel transposed[MAXW][MAXH];

    for(int i = 1; i < w; i++)
    {
    for(int j = 1; j < h; j++)
    {
    transposed[j][i] = image_mod[i][j];
    }
    }

    for(int i = 1; i < w; i++)
    {
    for(int j = 1; j < h; j++)
    {
    image_mod[i][j] = transposed[i][j];
    }
    }
    }

    void make_purple(Pixel image_mod[][800], int w, int h)
    {
    int purple;
    for(int i = 0; i < h; i++)
    {
    for(int j = 0; j < w; j++)
    {
    purple = (image_mod[i][j].get_red()+image_mod[i][j].get_blue()+ image_mod[i][j].get_green())/3;
    image_mod[i][j].set_pixel_value(purple, 0, purple);
    }
    }
    }

    void make_blue(Pixel image_mod[][800], int w, int h)
    {
    int blue;
    for(int i = 0; i < h; i++)
    {
    for(int j = 0; j < w; j++)
    {
    blue = (image_mod[i][j].get_red()+image_mod[i][j].get_blue()+ image_mod[i][j].get_green())/3;
    image_mod[i][j].set_pixel_value(0, 0, blue);
    }
    }
    }

    void make_green(Pixel image_mod[][800], int w, int h)
    {
    int green;
    for(int i = 0; i < h; i++)
    {
    for(int j = 0; j < w; j++)
    {
    green = (image_mod[i][j].get_red()+image_mod[i][j].get_blue()+ image_mod[i][j].get_green())/3;
    image_mod[i][j].set_pixel_value(0, green, 0);
    }
    }
    }

    void make_bright(Pixel image[][800], int w, int h, int color)
    {
    double bright;
    char anykey;

    cout << "In order to make this function work properly, please enter "
    << "\na brightness multiplier. The mulitplier has to be between 0 and 2."
    << "\nAnything less than one will darken the image. Anything higher than "
    << "\n1 will brighten it. The numbers can be in decimal form as well." << endl;
    cin >> bright;

    if (bright< 0 || bright >2)
    {
    cout << "Modifier out of range. Quitting Function." << endl;
    return;
    }

    for (int i = 0; i < h; i++)
    {
    for (int j = 0; j < w; j++)
    {
    image[i][j] = image[i][j]*bright;
    if(image[i][j].get_red() > color)
    image[i][j].set_red(color);
    if(image[i][j].get_green() > color)
    image[i][j].set_green(color);
    if(image[i][j].get_blue() > color)
    image[i][j].set_blue(color);
    }
    }
    cout << "Hit any key to exit." << endl;
    cin >> anykey;
    }
    -----------------------------------------------------------------------------------

    There it is...

    SAW

  9. #9
    Unregistered
    Guest
    You should put "cin >> menu;" inside the 'while' loop, otherwise it'll only get executed once!

Popular pages Recent additions subscribe to a feed