Example #1
0
void crop(ImageType& image)
{
    if(imageLoaded(image))
        {
                // initialize to -1 for input validation
                int ULr = -1, ULc = -1, LRr = -1, LRc = -1;
                int N, M, Q;
                int errorCode = 0;

                // get values
                image.getImageInfo(N,M,Q);

                // get inputs
                cout << "Enter the upper left corner's row: ";
                cin >> ULr;             

                cout << endl << "Enter the upper left corner's column: ";
                cin >> ULc;

                cout << endl << "Enter the lower right corner's row: ";
                cin >> LRr;

                cout << endl << "Enter the lower right corner's column: ";
                cin >> LRc;

                // check for errors
                if(ULr < 0 || ULc < 0 || LRr < 0 || LRc < 0)
                        errorCode = 1;
                        
                else if(ULr > N || LRr > N || ULc > M || LRc > M)
                        errorCode = 2;

                else if(ULr >= LRr || ULc >= LRc)
                        errorCode = 3;

                switch(errorCode)
                {
                        case 1:
                                cout << "ERROR: All inputs must be non-negative.";
                                break;
                        case 2:
                                cout << "ERROR: All crop boundaries must be within image boundaries.";
                                break;
                        case 3:
                                cout << "ERROR: All crop boundaries must be in the correct order.";
                                break;
                }
                
                // crop image if no error was found
                if(errorCode == 0)
                {
                        image.getSubImage(ULr, ULc, LRr, LRc, image);
                        cout << endl << endl << "Image has been cropped successfully.";
                }
        
                pressEnterToContinue();
        }
}