Vector Image Enlarge

This is a discussion on Vector Image Enlarge within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get better at C++ so I have been doing some of the challenges at topcoder. This was ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    38

    Vector Image Enlarge

    I'm trying to get better at C++ so I have been doing some of the challenges at topcoder.

    This was the problem

    Given an image as a vector <string>, scale it up by factor. That is, if the original image is M x N, the scaled image should be M*factor x N*factor. Each character in the input vector <string> should be represented by factor x factor of the same character in the output (see examples).

    example

    {"abc",
    "def",
    "ghi"}

    3
    Returns:
    { "aaabbbccc",
    "aaabbbccc",
    "aaabbbccc",
    "dddeeefff",
    "dddeeefff",
    "dddeeefff",
    "ggghhhiii",
    "ggghhhiii",
    "ggghhhiii" }

    Here is my solution but 4 loops seems too much. Can anybody show me what I could do better? Thanks

    Code:
    #include <vector>
    #include <iostream>
    using namespace std;
    
    class ImageEnlarger{
    public:
    vector <string> enlarge(vector <string> image, int factor){
      string tempImage="", tempReturn="";  
      vector <string> returnImage;  
     
      for(int i=0;i<image.size();i++){  
        tempImage=image[i];   
        for(int j=0;j<factor;j++){ 
          for(int k=0;k<tempImage.length();k++){    
            for(int z=0;z<factor;z++){      
              tempReturn+=tempImage[k];
            }//end for
          }//end for       
       
         returnImage.push_back(tempReturn);  
         tempReturn="";
         }//end for
      }//end for
        
      return returnImage;
    }//end enlarge
    };//end class

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    34
    Here's my take on it, although I didn't bother to make it a function or put it in a class or anything. I'm sure the scaling algorithm could be better still...and I think I miss-used the size_t type, but I'm still in the process of learning myself

    EDIT: I decided to put it in function form like you did, so I'm replacing the old code with the new code. I'm just not sure it's a good idea to be passing and returning a vector this way...I believe I read that it's more efficient to pass/return an iterator instead.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    vector<string> Enlarge(const vector<string> &inImage, int scaleFactor)
    {
    	vector<string> outImage;
    
    	size_t inWidth = inImage[0].length();  // first string determines width of image
    	size_t inHeight = inImage.size();
    	size_t outWidth = inWidth * scaleFactor;
    	size_t outHeight = inHeight * scaleFactor;
    
    	double step = 1.0 / scaleFactor;
    
    	for (size_t y = 0; y != outHeight; ++y)
    	{
    		string temp;
    		size_t yIndex = static_cast<size_t>(step * y);
    		for (size_t x = 0; x != outWidth; ++x)
    		{
    			size_t xIndex = static_cast<size_t>(step * x);
    			temp += inImage[yIndex][xIndex];
    		}
    		outImage.push_back(temp);
    	}
    	
    	return outImage;
    }
    
    
    int main()
    {
    	char *data[] = {"abc", "def", "ghi"};
    
    	size_t dataSize = sizeof(data) / sizeof(char *);
    
    	vector<string> image(data, data + dataSize);
    	vector<string> scaledImage;
    
    	scaledImage = Enlarge(image, 3);
    
    	// Output both image and scaledImage vectors
    	copy(image.begin(), image.end(), ostream_iterator<string>(cout, "\n"));
    	cout << endl;
    	copy(scaledImage.begin(), scaledImage.end(), ostream_iterator<string>(cout, "\n"));
    
    	return 0;
    }
    Last edited by veecee; 05-20-2006 at 08:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bugs (and strange things...)
    By kermi3 in forum General Discussions
    Replies: 380
    Last Post: 4 Weeks Ago, 07:29 AM
  2. Please enlarge the post reply text box
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 01-03-2005, 01:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21