ćw 1.txt

(5 KB) Pobierz
// NIE KOPIOWAĆ!!! TYLKO DO CELÓW POGLĄDOWYCH!!! ;)
#include <cstdlib>
#include <iostream>
//#include "image.cpp"

using namespace std;

struct image                        
{
       int w,h;
       unsigned char** Image;         //dwie ** bo 2 wymiary   - tablica tablic           
};

image CreateImage()        //tworzenie struktury
{
      int height,width;
      
      cout << "podaj szerokosc :";
      cin>>  height;       
      cout << "podaj wysokosc  :";   
      cin>> width;
      image Myimage;
      
      Myimage.h=height;
      Myimage.w=width;    
      
      if (height&&width) {
          Myimage.Image = new unsigned char* [width];
          for(int i=0; i<width; i++)
          {
              Myimage.Image[i] = new unsigned char [height];                      
          }
      }else{
          Myimage.Image=NULL;                          //pusty obraz
      }     
          return Myimage;          
}

void DeleteImage (image Myimage)                       //usuwanie struktury
{      
      for(int i=0; i<Myimage.w; i++)		//ważna kolejność: najpierw tablice, później tablica wskaźników
      {
          delete[] Myimage.Image[i];                      
      } 
      delete[] Myimage.Image;
}

void fillInImage ( image* Myimage)
{
     int value;
     do{
         cout<<"podaj wartosc w zakresie 0...255  :"<<endl;         
         cin>>value;
     }while( (value>255) || (value<0) );     
      for(int i=0; i< (Myimage->w); i++)
      {
          for(int j=0; j< (Myimage->h); j++)    
          {    
              Myimage->Image[j][i]=value;       
          }
          cout<<endl;                      
      }
}     
     
void DisplayImage ( image* Myimage)                               //wyświetlanie
{
     for(int i=0; i<Myimage->w; i++)                             
      {
          for(int j=0; j<Myimage->h; j++)    
          {    
               printf("%3i|",Myimage->Image[j][i]);       
          }
          cout<<endl;                      
      }         
}

void ReadPixel ( image* Myimage )
{
     int w,h;
                        do{
                          cout << "podaj szerokosc :";
                          cin>>  h;       
                          cout << "podaj wysokosc  :";   
                          cin>> w;
                        }while( w>=(Myimage->w) || h>=(Myimage->h)  );    
     printf( "wartosc piksela wynosi  : %i\n\n",Myimage->Image[h][w]);    
}

void WritePixel ( image* Myimage )
{
     int w,h,value;
                        do{
                          cout << "podaj szerokosc :";
                          cin>>  h;       
                          cout << "podaj wysokosc  :";   
                          cin>> w;
                          cout << "podaj wartosc   :";   
                          cin>> value;
                        }while( w>=(Myimage->w) || h>=(Myimage->h) || (value>255) );    
                        Myimage->Image[h][w]=value;
                        cout << "wykonano"<<endl;
   
}

void SizeImage ( image* Myimage )
{    
     cout << "wysokosc obrazu: "<<Myimage->h<<", szerokosc: "<<Myimage->w<<endl<<endl;   
}
   
////////////////////////////////////////////////////////////////////////////////


int main(int argc, char *argv[])
{
      int a;
      
      image mojobraz = CreateImage();   
    
      DisplayImage (&mojobraz);              //&adres - f. odwrotna do wyłuskania

    do{
        cout<<"Co chcesz zrobic?"<<endl;
        cout<<" 1 - nadac te sama jasnosc wszystkim pikselom"<<endl; 
        cout<<" 2 - odczytac wybrany piksel"<<endl;
        cout<<" 3 - zmienic wartosc wybranego piksela"<<endl;
        cout<<" 4 - odczytac wysokosc i szerokosc obrazu"<<endl;
        cout<<" 5 - wyswietlic wartosci calego obrazu"<<endl; 
        cout<<" 6 - czyszczenie konsoli"<<endl;   
        cout<<" 0 - wyjscie z programu                 wybor:  "<<endl;

        cin>>a;
        
        switch(a){
                case 0:{
                     DeleteImage (mojobraz);
                     return EXIT_SUCCESS;}
                break; 
                case 1:{
                       fillInImage (&mojobraz);            
                       break;
                    }     
                case 2:{
                       ReadPixel (&mojobraz); 
                       break;
                     }     
                case 3:{
                       WritePixel (&mojobraz); 
                       break;
                      }     
                case 4:{
                       SizeImage (&mojobraz); 
                       break;
                     }
                case 5:{
                       DisplayImage (&mojobraz);  
                       break;
                     }
                case 6:{
                       system("CLS");  
                       break;
                     }     
                default:
                    cout<<"bledny wybor"<<endl;         
        }
    }while(1);
    
    DeleteImage (mojobraz);
    system("PAUSE");
    return EXIT_SUCCESS;
}
Zgłoś jeśli naruszono regulamin