2011-07-06 6 views
0

私は働いた1つの大きな機能を持っていました。それから、私はそれを修正することに決めました。今は黒い正方形を印刷しています。誰かが何が起こっているのかを理解するかどうか確認するために私のコードを添付しています。かつて大規模な機能これら三つの機能は: 機能1Modulization Awryが消えた

int 
readpgm (pgm_type * header, char input[80], char output[80]) 
{ 
    FILE *instream; 

    int size, read; 
    instream = fopen (input, "rb"); 

    fileChecker (instream); 

    fscanf (instream, "%2s%d%d%d", header->filetype, &header->width, 
     &header->height, &header->maxgray); 

    if (!header->filetype[0] == 'P' || !header->filetype[1] == '5') { 
     fatal ("Incorrect Type"); 
    } 

    size = header->width * header->height; 

    header->p = malloc (size * sizeof (char)); 

    read = fread (header->p, 1, size, instream); 
    if (read != size) { 
     fatal ("Incorrect Size"); 
    } 

    return size; 
} 

void 
crop (pgm_type * header, char output[80]) 
{ 
    printf ("Height: %i, Width: %i, Total pixels: %i \n", header->height, 
     header->width, header->height * header->width); 

    int temp, y1, y2, x1, x2, wide, high; 

    printf ("Please Enter x1 y1 x2 y2 \n"); 


    scanf ("%i %i %i %i", &x1, &y1, &x2, &y2); 
    if (y1 > y2) { 
     temp = y1; 
     y1 = y2; 
     y2 = temp; 
    } 
    if (x1 > x2) { 
     temp = x1; 
     x1 = x2; 
     x2 = temp; 
    } 
    wide = x2 - x1 + 1; 
    high = y2 - y1 + 1; 

    printFile (wide, high, x1, x2, y1, y1, header, output); 
} 

void 
printFile (int wide, int high, int x1, int x2, int y1, int y2, 
     pgm_type * header, char output[80]) 
{ 
    FILE *outstream; 

    outstream = fopen (output, "wb"); 

    fileChecker (outstream); 

    fprintf (outstream, "%2s\n%i %i\n%i\n", header->filetype, wide, high, 
     header->maxgray); 

    pixel image[header->height][header->width]; 
    pixel *pix = malloc ((wide * high) * sizeof (char)); 

    int a = 0; 

    for (int b = 0; b < header->height; ++b) { 
     for (int c = 0; c < header->width; ++c) { 
      image[b][c] = header->p[a]; 
      ++a; 
     } 
    } 

    int k = 0; 
    for (int i = y1; i <= y2; ++i) { 
     for (int j = x1; j <= x2; ++j) { 
      pix[k] = image[i][j]; 
      ++k; 
     } 
    } 

    fwrite (pix, 1, (wide * high) * sizeof (pixel), outstream); 
    free (pix); 
    fclose (outstream); 
} 

最初の二つの機能がメインで呼び出します。

+0

この行は私に間違って見える:ピクセル画像【header->高さ] [header->幅]高さは4であり、幅が5だった場合、それは[画像あろう2Dアレイであろう – whoplisp

+0

@whoplisp 4] [5]。 –

+1

私は脱線したくないですが、あなたのコードは一般に公開されているプログラムの一部ではないことを願っています。長さチェックを持たない固定サイズのchar配列はバッファオーバーフローを起こしやすい。 –

答えて

1

これは私のために働く。

//gcc c2.c -std=c99 -g -Wall -Wextra 
#include <stdio.h> 
#include <malloc.h> 
#include <assert.h> 

typedef struct pgm_type pgm_type; 
struct pgm_type{ 
    char filetype[2]; 
    int width,height,maxgray; 
    unsigned char*p; 
}; 


int 
readpgm (pgm_type * header, char*fn) 
{ 
    FILE *instream; 

    int size, read; 
    instream = fopen (fn,"rb"); 

    assert(instream); 

    fscanf (instream, "%2s%d%d%d", header->filetype, &header->width, 
     &header->height, &header->maxgray); 
    printf("%2s %d %d %d\n",header->filetype,header->width, 
     header->height,header->maxgray); 

    if (!header->filetype[0] == 'P' || !header->filetype[1] == '5') { 
     printf ("Incorrect Type"); 
    } 

    size = header->width * header->height; 

    header->p = malloc (size * sizeof (char)); 

    read = fread (header->p, 1, size, instream); 
    if (read != size) { 
     printf ("Incorrect Size"); 
    } 

    return size; 
} 

void 
printFile (int x1, int x2, int y1, int y2,pgm_type * header, char*fn) 
{ 
    FILE *outstream; 
    int wide=x2-x1+1,high=y2-y1+1; 
    printf("cropping to %dx%d\n",wide,high); 
    outstream = fopen (fn, "wb"); 

    assert (outstream); 

    fprintf (outstream, "%2s\n%i %i\n%i\n", header->filetype, wide, high, 
     header->maxgray); 

    unsigned char image[header->height][header->width]; 
    unsigned char *pix = malloc ((wide * high) * sizeof (char)); 

    int a = 0; 

    for (int b = 0; b < header->height; ++b) { 
     for (int c = 0; c < header->width; ++c) { 
      image[b][c] = header->p[a]; 
      ++a; 
     } 
    } 

    int k = 0; 
    for (int j = y1; j <= y2; ++j) { 
     for (int i = x1; i <= x2; ++i) { 
      pix[k] = image[j][i]; 
      ++k; 
     } 
    } 

    fwrite (pix, wide,high, outstream); 
    free (pix); 
    fclose (outstream); 
} 


int 
main() 
{ 
    pgm_type h; 
    readpgm(&h,"lena.pgm"); 
    printFile(64,129,32,230,&h,"o2.pgm"); 
    return 0; 
} 
+0

私の投稿を読んでいれば、私は機能を分断する前に仕事をしていました。 –

+0

よろしいですか。今は自分のマシンで動作します。 – whoplisp

+0

ありがとう、ちょっとxとyをソートする関数を追加するだけでいいです –

関連する問題