簡體   English   中英

具有魔術代碼P3的ppm格式

[英]ppm format that have magic code P3

我如何讀取具有魔術代碼P3的ppm圖像。 用C語言? 此代碼讀取具有魔術代碼P3的ppm圖像,但加載方法存在問題,該代碼在讀取像素值時陷入無限循環?

    /* This is for reading and Writing images file in PPM
    Also example of making Negative images

    - Use this file as an example of reading (loading) and writing (storing) images files.

    - This program loads an PPM image and store a new image that is the negative of the      original image.


    */
    typedef struct pdata {
    int red;
    int green;
    int blue;

    } pdata;
    typedef struct ppm {
    int w;
    int h;
    int max;
    pdata *pData;
    } ppm;


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//#define kernelW 3
//#define kernelH 3
//#define imageW 403
//#define imageH 332
char filename[256];

/*
   This LoadPGM function is an updated version that will deal with
   comments in file headers and do at least some basic checking if the
   file can be opened or is the correct format. It does assume the file
   is actually a valid length once the header has been read.

   You are of course free to use your own file reading functions.
*/

void LoadPGM( char* filename, ppm* pPgm ) {
  FILE* ifp;
  int word;
  int nRead = 0;
  char readChars[256];

  //open the file, check if successful
  ifp = fopen( filename, "r" );
  if (!ifp) {
    printf("Error: Unable to open file %s.\n\n", filename);
    exit(1);
  }

  //read headers from file
  printf ("Reading PPM file: %s...\n", filename);
  fscanf (ifp, "%s", readChars);

  if (strcmp(readChars, "P3") == 0) {
    //valid file type
    //get a word from the file
    fscanf (ifp, "%s", readChars);
    while (readChars[0] == '#') {
      //if a comment, get the rest of the line and a new word
      fgets (readChars, 255, ifp);
      fscanf (ifp, "%s", readChars);
    }

    //ok, comments are gone
    //get width, height, color depth
    sscanf (readChars, "%d", &pPgm->w);
    fscanf (ifp, "%d", &pPgm->h);
    fscanf (ifp, "%d", &pPgm->max);

    // allocate some memory, note that on the HandyBoard you want to
    // use constant memory and NOT use calloc/malloc
      pPgm = (ppm *)malloc(sizeof(ppm));
     pPgm->pData = (pdata*)malloc(pPgm->w *pPgm->h * sizeof(pPgm->pData));
 fscanf(ifp, "%d" ,&word);
      pPgm->pData[0].red = word;
      printf (" %d \n", word);
    for (nRead = 0; nRead < pPgm->w * pPgm->h; nRead++) {
      fscanf(ifp, "%d" ,&word);
      // printf (" %d \n",word);
      pPgm->pData[nRead].red = word;
      fscanf(ifp, "%d" ,&word);
      pPgm->pData[nRead].green = word;
      fscanf(ifp, "%d" ,&word);
      pPgm->pData[nRead].blue = word;

    }

    printf ("Loaded PPM. Size: %dx%d, Greyscale: %d \n",
        pPgm->w, pPgm->h, pPgm->max + 1);
  }
  else {
    printf ("Error: Read file type %s. Format unsupported.\n\n", readChars);
    exit(1);
  }
  fclose(ifp);
}

/* Simply saves the PGM file to a file whose name is in filename */

void WritePGM( char* filename, ppm* pPgm ) {
  FILE* ofp;
  int w, h, max;
  int nWrite = 0;
  int i,j;


  strcat(filename, ".out.ppm");
  ofp = fopen(filename, "w");
  if (!ofp) {
    printf("Error: Unable to open file %s.\n\n", filename);
    exit(1);
  }

  printf ("Writing ouput PPM: %s\n\n", filename);


  //write the header
  fprintf( ofp, "P3\n" );
  fprintf( ofp, "%d %d\n", pPgm->w, pPgm->h );
 //  printf("Done. Have a nice day...\n\n");
  fprintf( ofp, "%d\n", pPgm->max );


  //write the image data
  for (i = 0; i < pPgm->h;i++) {
    for (j = 0; j < pPgm->w; j++)
      fprintf(ofp,"%d ",*(pPgm->pData + i * pPgm->w + j));
    fprintf(ofp,"\n");
  }
  fclose(ofp);
}


int main(int argc, char * argv[]) {

  ppm* image, *r1,*r2;
 // char filename[256];
  int val;
  int errchk;
  char f1[256];

  //check if a filename was given, if not, ask for one
  if (argc > 1) {
    strcpy(filename, argv[1]);
  }
  else {
    printf ("Enter filename: ");
    scanf ("%s", filename);
  }

  //allocate memory for the pgm struct
  image = (ppm *) malloc (sizeof(ppm));




  //read the file
  LoadPGM(filename, image);
 int h,w;

w=image->w;
h=image->h;

int* pBuff1;
int* pBuff2;

   int size;
   int sum=0;




 WritePGM(filename, image);




//  end of the program
}

這條線

malloc(pPgm->w *pPgm->h * sizeof(pPgm->pData))

分配了錯誤的大小。 當您需要sizeof(pdata) ,它會分配sizeof(pointer) ,這是您自己的RGB結構。

通常,一個指針只需要RGB作為3個無符號字符,並且您曾經使用過該錯誤將是不可見的,因為指針通常為4個字節長(如今甚至可以更長)。 但是由於您將每個值都設置為一個int,所以該結構總共使用12個字節。 在讀取第三個三分之一(給定或取走)之后的某個地方,您的代碼開始覆蓋內存中的其他內容,並且被敬酒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM