Like Tree1Likes
  • 1 Post By MK27

Image Manipulation with ppm files

This is a discussion on Image Manipulation with ppm files within the C++ Programming forums, part of the General Programming Boards category; I need help on my c++ homework. We are doing image manipulation with ppm files. I am having trouble reading ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    Unhappy Image Manipulation with ppm files

    I need help on my c++ homework. We are doing image manipulation with ppm files. I am having trouble reading the ppm file to the array, which later has it red values equal to zero. The new aray is suppose to be written to a new file, but it is not writing. The first line of the file always contains the string \P3" (this is also known as
    the magic number). The second line gives number of columns and rows, respectively.The third line represents the threshold value, that is the maximum value for any pixel color. Starting from the fourth line, values for red, green and blue for each pixel is given. In the example, above lines four, five and six tell us the values of red, green and blue, respectively, for the (0,0) pixel in the image. Lines seven, eight and nine gives us the color values for the (0,1) pixel, and so on.Can someone please help me figure what is wrong with my code?

    Code:
    #include
    <iostream>
    
    #include
    <fstream>
    
    #include
    <cstdlib>
    
    #include
    <sstream>
    
    using
    namespace std;
    
    
     
    
    ifstream
     inputImage;
    
    ofstream
     outputImage;
    
    string
     name;
    
    char
     filename[20];
    
    char
     filename1 [20];
    
    char
     choice1; // variable for switch case for main menu
    
    char
     choice2; // variable for switch case for main menu
    
    char
     color;
    
    const
    int WIDTH=100; // the size of the image; the limit on what can hold
    
    const
    int HEIGHT=150; // the size of the image; the limit on what it can hold
    
    int
     height;
    
    int
     width;
    
    
     
    
     
    
    struct
    pixel//variables for the pixel with data type image
    
    {
    
    	
    intred;
    
    	
    intgreen;
    
    	
    intblue;
    
    };
    
    
    void
     read (ifstream &inputImage, pixel images [][WIDTH], int &width, int &height)
    
    {
    
    	
    //inputImage.open(filename);
    
    	
    char magic [3];
    
    	
    int max_value;
    
    
    	inputImage>>magic; 
    // P3 the magic number
    
    	inputImage>>width>>height; 
    ///columns and rows of the image in pixels
    
    	inputImage>>max_value; 
    //max value for any pixel color
    
    
    	
    for(int i=0; i>HEIGHT; i++)
    
    	{
    
    		
    for(int j=0; j>WIDTH; j++)
    
    		{
    
    			inputImage >> images[i][j].
    red;
    
    			inputImage>> images[i][j].
    green;
    
    			inputImage>> images [i][j].
    blue;
    
    		}
    
    	}
    
    }
    
     
    void writing (ofstream &outputImage, pixel images [][WIDTH], int &width, int &height)
    
     {
    
    
    	 
    int max_value;
    
    	 std:: 
    stringstream ss;
    
    	 ss << outputImage; 
    
    	 outputImage<<
    "P3"; // P3 the magic number
    
    	 outputImage<<width<<height; 
    ///columns and rows of the image in pixels
    
    	 outputImage<<max_value; 
    //max value for any pixel color
    
    
    	 
    for(int i=0; i>height; i++) // writing to an outputfile
    	 {
    
    	 	
    for(int j=0; j>width; j++)
    
    	 	{
    
    	 		outputImage << images [i][j].
    red;
    
    	 		outputImage << images[i][j].
    green;
    
    	 		outputImage << images [i][j].
    blue;
    
    	 	}
    
    	 }
    
    
     }
    
    void
     filter ()
    
    {
    
    
    }
    
    
     
    
    int
     main ()
    
    {
    
    
    	
    pixel images [HEIGHT][WIDTH];
    
    
     
    
     
    
    	
    do{
    
    		cout<< 
    "Please enter the name of the image you are using: " <<endl;
    
    		cin>> filename;
    
    		read (inputImage, images, width, height);
    
    		cout << 
    "Manipulate Image [m]"<<endl;
    
    		cout<< 
    "Generate Images [g]"<<endl;
    
    		cout<< 
    "Quit [q]"<<endl;
    
    		cin>> choice1;
    
    
    		
    if(!((choice1!='m')||(choice1!='g')||(choice1!='q')))
    
    		{
    
    			cout<< 
    "Error! Please choose a character that is either m, g, or q....."<<endl;
    
    			cin>> choice1;
    
    		}
    
    			
    switch(choice1)
    
    		{
    
    		
    case'm':
    
    			cout<< 
    "Name of the new image:" <<endl;
    
    			cin>>name;
    
    			
    do{
    
    				cout << 
    "Filter [s]"<<endl;
    
    				cout<<
    "Crop [c]"<<endl;
    
    				cout<<
    "Rotate [r]"<<endl;
    
    				cout<<
    "Quit [q]"<<endl;
    
    				cin>> choice2;
    
    
    				
    if(!((choice2!='s')||(choice2!='c')||(choice2!='r')||(choice2!='q')))
    
    						{
    
    							cout<< 
    "Error! Please choose a character that is either s, c, r, or q....."<<endl;
    
    							cin>>choice2;
    
    						}
    
    				
    switch (choice2)
    
    				{
    
    				
    case's':
    
    					cout<< 
    "Which color do you want to filter: blue, green, or red?" << endl;  //put all this underneath the function filter when completed
    
    					cin>> color;
    
    					
    if(!((choice2!='b')||(choice2!='g')||(choice2!='r')))
    
    											{
    
    												cout<< 
    "Error! Please choose a character that is either b, g, or r....."<<endl;
    
    												cin>>color;
    
    											}
    
    					
    if (choice2 == 'r')
    
    					{
    
    						
    for(int i=0; i< height; i++)
    
    						{
    
    							
    for(int j=0; j<width; j++)
    
    							{
    
    								images[i][j].
    red = 0; // a for loop to filter each pixel
    
    							}
    
    						}
    
    						
    break;
    
    
    						outputImage.open(filename1);
    
    						writing (outputImage, images, width, height);
    
    						 
    for(int i=0; i>height; i++)
    
    							 {
    
    								 
    for(int j=0; j>width; j++)
    
    								 {
    
    									 cout<<images [i][j].
    red;
    
    									 cout<<images [i][j].
    green;
    
    									 cout<< images [i][j].
    blue;
    
    								 }
    
    							 }
    
    
    						outputImage.close();
    
    						inputImage.close();
    
    
    					}
    
    				}
    
    			}
    while(choice2!= 'q');
    
    			
    break;
    
    		}
    
    	}
    while(choice1!='q');
    
    	
    return 0;
    
    }
    
    


  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    1) You need to be more specific about your problem. You say "I am having trouble reading to the array". What kind of trouble? What is wrong with the array? You say "the new aray is suppose to be written to a new file, but it is not writing". Can you write anything to a file? If so, why do you think this attempt does not work? If not so, you should write a simple program writing data to a file and get that working before you move on.

    2) Your code is awkward to read due to the ridiculous amount of whitespace. This is hands down, by far (and away) the most spaced out code I have ever seen. Please do not write code this way. Choose a conventional indentation style and adhere to it:

    Indent style - Wikipedia, the free encyclopedia

    Part of that problem may have been due to the application you cut and pasted from, since it included mark-up which has screwed up the normal forum highlighting and line numbering.
    Last edited by MK27; 12-11-2011 at 09:07 PM.
    Salem likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Yes, useless markup embedded.

    This is just the first line of it.
    [B ][SIZE=2 ][COLOR=#7f0055 ][SIZE=2 ][COLOR=#7f0055 ]#include
    [/COLOR ][/SIZE ][/COLOR ][/SIZE ][/B ][SIZE=2 ][COLOR=#7f0055 ][SIZE=2 ][COLOR=#7f0055 ][/COLOR ][/SIZE ][/COLOR ][/SIZE ][SIZE=2 ] [/SIZE ][SIZE=2 ][COLOR=#2a00ff ][SIZE=2 ][COLOR=#2a00ff ]<iostream>[/COLOR ][/SIZE ][/COLOR ][/SIZE ]


    Next time you do copy/paste, make sure you select the "as text" option (either copy or paste), and not the "rich" (html, rtf or other crap).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Image Manipulation
    By Shingetsu Kurai in forum C# Programming
    Replies: 4
    Last Post: 09-04-2011, 06:14 PM
  2. Image Manipulation
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 04-16-2009, 07:19 PM
  3. Image Manipulation
    By Jason84 in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2004, 07:30 AM
  4. image manipulation
    By axon in forum C++ Programming
    Replies: 11
    Last Post: 05-01-2003, 12:22 PM
  5. Image manipulation
    By Colin in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2003, 11:46 AM

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