Grafik spiegeln



  • Hallo Leute,

    vielleicht kann mir jemand bei meinem Problem helfen.
    Folgende Code habe ich, der aber nicht ganz macht was er soll, ich weiß nur nicht warum

    #include <stdio.h>
    #include <stdlib.h>
    #include "stack.h"
    
    const char FILLED = 'F';
    const char WHITE = '.';    // Wert eines weißen Punktes
    const char BLACK = '*';    // Wert eines schwarzen Punktes
    
    // Struktur zur Speicherung einer Bitmap
    struct TBitmap {
      int xdim;    // Breite der Bitmap
      int ydim;    // Hoehe der Bitmap
      char* pixel; // Zeiger auf ein Feld mit Werten der Pixel
    };
    
    // Erzeugt eine weisse Bitmap mit Breite xdim und Hoehe ydim,
    // indem der erforderliche Speicher angefordert wird.
    TBitmap* newBmp(int xdim, int ydim)
    {
      TBitmap* newBmp;
      char* currPixel;
      int i;
    
      newBmp = (TBitmap*)malloc(sizeof(TBitmap));
      newBmp->xdim = xdim;
      newBmp->ydim = ydim;
      // Ein Zeichen mehr als erforderlich anfordern, damit readBmp abschließende
      // 0 speichern kann.
      newBmp->pixel = (char*)malloc(sizeof(char) * xdim * ydim + 2);
    
      currPixel = newBmp->pixel;
      for(i = 0; i < xdim*ydim; i++)
        *currPixel++ = WHITE;
      return newBmp;
    }
    
    TBitmap* readBmp(char* filename)
    {
      FILE* file;
      TBitmap* bmp;
      char* pixelPos;
      int xdim;
      int ydim;
      char dims[32];
    
      if(file = fopen(filename,"r")) {
        fgets(dims,32,file);
        sscanf(dims,"%d%d",&xdim,&ydim);
        bmp = newBmp(xdim, ydim);
        pixelPos = bmp->pixel;
        while(ydim) {
          fgets(pixelPos,2,file);
          if(*pixelPos != '\n') {
            printf("Fehler in der Datei.\n");
            break;
          }
          fgets(pixelPos,xdim + 1,file);
          pixelPos = pixelPos + xdim;
          ydim--;
        }
        fclose(file);
        return bmp;
      }else
        printf("Datei '%s' nicht gefunden.",filename);
      return 0;
    }
    
    // Gibt den Speicher, der durch newBMP angefordert wurde,
    // wieder frei.
    void destroyBmp(TBitmap* bmp)
    {
      free(bmp->pixel);
      free(bmp);
      return;
    }
    
    // Druckt die Bitmap auf dem Bildschirm.
    void printBmp(TBitmap* bmp) 
    {
      char* currChar = bmp->pixel;
      int x;
      int y;
      char formatString[16];
    
      sprintf(formatString,"%s%ds\n","%.",bmp->xdim);
    
      for(y = 0; y < bmp->ydim; y++) {
        printf(formatString,currChar);
        currChar = currChar + bmp->xdim;
      }
    }
    
    void rotate(TBitmap* bmp)
    {            
    }
    
    void flipH(TBitmap* bmp)
    {
         TBitmap* flipbmp = bmp;
         char* fH = bmp->pixel;
         int i;
         int j;
         int xmax = bmp->xdim;
         int ymax = bmp->ydim;
         char* neu = flipbmp->pixel;
    
         for (i=0;i<ymax;i++) {
             for (j=0;j<xmax;j++) {
                 fH[j*xmax+i]=neu[j*xmax+(-i+(xmax-1))];
    
                 }
                 }
    
    }
    
    void flipV(TBitmap* bmp)
    {
    }
    
    int main(int argc, char* argv[])
    {
      char filename[256];
      int x,y;
      char cmd;
      TBitmap* bmp;
    
      printf("Dateiname? ");
      gets(filename);
      if(bmp = readBmp(filename)) {
        printf("cmd (r)otate  flip(h)  flip(v) (e)nd? ");
        do {
          scanf("%c",&cmd);
          switch(cmd) {
            case 'r': rotate(bmp);
                      printf("\n");
                      printBmp(bmp);
                      printf("cmd (r)otate  flip(h)  flip(v) (e)nd? ");
                      break;
            case 'h': flipH(bmp);
                      printf("\n");
                      printBmp(bmp);
                      printf("cmd (r)otate  flip(h)  flip(v) (e)nd? ");
                      break;
            case 'v': flipV(bmp);
                      printf("\n");
                      printBmp(bmp);
                      printf("cmd (r)otate  flip(h)  flip(v) (e)nd? ");
                      break;
          }
        }while(cmd != 'e');
      }
    }
    

    Es geht um die Funktion flipH
    Wenn man z.B. folgende Datei einliest

    5 4
    
    *....
    **...
    ***..
    ****.
    

    soll folgendes angezeigt werden

    ....*
    ...**
    ..***
    .****
    

    bei mir wird aber nur angezeigt:

    .....
    .....
    ..*..
    .***.
    

    Kann mir einer nen Tipp geben, wo da der Fehler in der flipH liegt, weil dann wären die Funktion flipV und rotate wohl auch kein Problem.

    Vielen Dank schonmal!



  • probier mal so:

    void flipH(TBitmap* bmp)
    {
      int size = bmp->xdim * bmp->ydim; 
      char *copy = malloc (size);
      int s;
    
      if (copy)
      {
        memcpy (copy, bmp->pixel, size);
        for (s=0; s<bmp->ydim; s++)
        {
          memcpy (bmp->pixel+s*bmp->xdim, 
                  copy+(bmp->ydim-s-1)*bmp->xdim, 
                  bmp->ydim);
        }
        free (copy);
      }
    }
    

    ^^das ist die primitivmethode, also erst alles kopieren und dann umgekehrt wieder aufbauen.
    🙂



  • void flipH(TBitmap* bmp)
    {
         TBitmap* flipbmp = bmp;
         char* fH = bmp->pixel;
         ...
         char* neu = flipbmp->pixel; // entsricht bmp->pixel
    
         for (i=0;i<ymax;i++) {
             for (j=0;j<xmax;j++) {
                 fH[j*xmax+i]=neu[j*xmax+(-i+(xmax-1))];
    
                 }
                 }
    
    }
    

    Deine beiden Zeiger neu und fH zeigen auf denselben Speicher, d.h. du überschreibst dich selber. Du schreibst zuerst deinen Punkt nach hinten auf den Stern und wenn du diesen dann nach vorne spiegeln willst, ist er natürlich schon nicht mehr da.
    Deshalb bekommst du den ersten Stern auch erst, wenn die Anzahl der Sterne größer ist als die Zeichen in einer Zeile.
    Du musst für das gespiegelte Bild einen neuen Speicherbereich anlegen, den du füllst.

    Und du meinst wahrscheinlich

    neu[j*xmax+i]=fH[j*xmax+(-i+(xmax-1))];
    


  • Dieser Thread wurde von Moderator/in rüdiger aus dem Forum ANSI C in das Forum Spiele-/Grafikprogrammierung verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • Also vielleicht einfach mit newBMP einen neuen Speicherbereich anfordern. Dort die neuen Punkte hineinschreiben und zum schluss die Grafik in den alten Speicherbereich (bmp) kopieren?

    Schonmal danke für die Hinweise!



  • hi leuts kann zwar nicht auf deine frage antworten aber da ihr grad beim thema seit kann mir mal jemand erklären oder ein tutorial geben wie man eine grafik einfügt


Anmelden zum Antworten