简体   繁体   中英

Image rotation in Qt

I am programming in C++ on Qt Creator, with a CImagePPM class allowing to manipulate images in PPM format (opening, rotations, etc).

My 90 degree rotation function crashes most of the time (often the first rotation works, but the second one crashes), as does my symmetry function. The problem is probably minor but I don't manage to debug it.

This is my CImagePPM class:

struct rgb
{
    int r,g,b;
};

class CImagePPM
{
public:
    CImagePPM();
    CImagePPM(string nomfich);
    ~CImagePPM();
    void dessiner(QPainter * p);
    void rot90Droite();
    void rot90Gauche();
    void symAxeVertical();
    void symAxeHorizontal();
    void reduction();
    void agrandissement();
    void enregistrer();

private:
    int largeur;
    int hauteur;
    int intensiteMax;
    rgb** pixels;
};

Here is my rotation function:

void CImagePPM::rot90Gauche() {
    rgb** pixels2;
    int tmp=0;

    tmp=this->hauteur;
    this->hauteur=this->largeur;
    this->largeur=tmp;

    pixels2=new rgb*[this->largeur];

    for (int i=0;i<this->hauteur;i++) {
        pixels2[i]=new rgb[this->hauteur];
    }

    for (int i=0;i<this->hauteur;i++) {
        for (int j=0;j<this->largeur;j++) {
            pixels2[i][j]=this->pixels[j][this->hauteur-i-1];
        }
    }
    delete[] this->pixels;
    this->pixels=pixels2;
}

And here is my symmetry function:

void CImagePPM::symAxeHorizontal() {
    rgb** pixels2;

    pixels2=new rgb*[this->hauteur];
    for (int i=0;i<this->hauteur;i++) {
        pixels2[i]=new rgb[this->largeur];
        for (int j=0;j<this->largeur;i++) {
            pixels2[i][j]=this->pixels[this->hauteur-1-i][j];
        }
    }
    pixels=pixels2;
    this->pixels=pixels2;
}
  1. In CImagePPM::rot90Gauche() , for-loop should iterate from 0 to largeur , otherwise you will get square image.

     pixels2=new rgb*[this->l]; //Dimensions - [lxh] for (int i=0;i<this->l;i++) { //Iterate by l, not h. pixels2[i]=new rgb[this->h]; }
  2. In second for-loop, you should iterate i from 0 to l and j from 0 to h:

     for (int i=0;i<this->l;i++) { //i should be from 0 to l, j should be from 0 to h for (int j=0;j<this->h;j++) { pixels2[i][j]=this->pixels[j][this->hi-1]; } }
  3. You can't delete 2D-array by using delete[] . You have to delete inner arrays before deleting outer:

     //Before deleting this->pixels, you should delete all this->pixels[i] for (int i = 0; i < this->h; i++) { delete[] this->pixels[i]; } delete[] this->pixels;
  4. Your functions are part of class, so you don't have to use this there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM