Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 模块化出错_C_Image Processing_Module_Modularity_Pgm - Fatal编程技术网

C 模块化出错

C 模块化出错,c,image-processing,module,modularity,pgm,C,Image Processing,Module,Modularity,Pgm,因此,我有一个大的功能,工作。然后我决定修改它,现在我只是打印出一个黑色的正方形。我附上我的代码,看看是否有人会明白发生了什么。这三个功能,其中一个功能很大: 职能1 int readpgm (pgm_type * header, char input[80], char output[80]) { FILE *instream; int size, read; instream = fopen (input, "rb"); fileChecker (inst

因此,我有一个大的功能,工作。然后我决定修改它,现在我只是打印出一个黑色的正方形。我附上我的代码,看看是否有人会明白发生了什么。这三个功能,其中一个功能很大: 职能1

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);
}
int
readpgm(pgm_类型*头,字符输入[80],字符输出[80])
{
文件*流内;
int大小,读取;
instream=fopen(输入,“rb”);
文件检查器(流内);
fscanf(流内,“%2s%d%d%d”,标题->文件类型,&标题->宽度,
&收割台->高度,&收割台->最大灰色);
如果(!header->filetype[0]='P'| |!header->filetype[1]=='5'){
致命(“错误类型”);
}
尺寸=收割台->宽度*收割台->高度;
标题->p=malloc(大小*大小(字符));
读取=fread(收割台->p,1,尺寸,流内);
如果(读取!=大小){
致命(“尺寸不正确”);
}
返回大小;
}
无效的
裁剪(pgm_类型*页眉,字符输出[80])
{
printf(“高度:%i,宽度:%i,总像素:%i\n”,标题->高度,
收割台->宽度,收割台->高度*收割台->宽度);
内部温度,y1,y2,x1,x2,宽,高;
printf(“请输入x1 y1 x2 y2\n”);
scanf(“%i%i%i%i”、&x1、&y1、&x2和&y2);
如果(y1>y2){
温度=y1;
y1=y2;
y2=温度;
}
如果(x1>x2){
温度=x1;
x1=x2;
x2=温度;
}
宽=x2-x1+1;
高=y2-y1+1;
打印文件(宽、高、x1、x2、y1、y1、标题、输出);
}
无效的
打印文件(整数宽、整数高、整数x1、整数x2、整数y1、整数y2、,
pgm_类型*头,字符输出[80])
{
文件*扩展;
超流=fopen(输出,“wb”);
文件检查器(分流);
fprintf(超出流,“%2s\n%i%i\n%i\n”,标题->文件类型,宽,高,
标题->最大灰色);
像素图像[标题->高度][标题->宽度];
像素*pix=malloc((宽*高)*sizeof(字符));
int a=0;
对于(int b=0;bheight;++b){
对于(int c=0;cwidth;++c){
图像[b][c]=标题->p[a];
++a;
}
}
int k=0;
对于(inti=y1;i这对我很有效

//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;
}
//gcc c2.c-std=c99-g-Wall-Wextra
#包括
#包括
#包括
类型定义结构pgm_类型pgm_类型;
结构pgm_类型{
char文件类型[2];
整数宽度、高度、最大灰度;
无符号字符*p;
};
int
readpgm(pgm_类型*头,字符*fn)
{
文件*流内;
int大小,读取;
河道内=fopen(fn,“rb”);
断言(流内);
fscanf(流内,“%2s%d%d%d”,标题->文件类型,&标题->宽度,
&收割台->高度,&收割台->最大灰色);
printf(“%2s%d%d%d\n”),标题->文件类型,标题->宽度,
收割台->高度,收割台->最大灰色);
如果(!header->filetype[0]='P'| |!header->filetype[1]=='5'){
printf(“错误类型”);
}
尺寸=收割台->宽度*收割台->高度;
标题->p=malloc(大小*大小(字符));
读取=fread(收割台->p,1,尺寸,流内);
如果(读取!=大小){
printf(“尺寸不正确”);
}
返回大小;
}
无效的
打印文件(intx1、intx2、inty1、inty2、pgm_类型*头、字符*fn)
{
文件*扩展;
int宽=x2-x1+1,高=y2-y1+1;
printf(“裁剪到%dx%d\n”,宽,高);
外流=fopen(fn,“wb”);
断言(扩展);
fprintf(超出流,“%2s\n%i%i\n%i\n”,标题->文件类型,宽,高,
标题->最大灰色);
无符号字符图像[标题->高度][标题->宽度];
无符号字符*pix=malloc((宽*高)*sizeof(字符));
int a=0;
对于(int b=0;bheight;++b){
对于(int c=0;cwidth;++c){
图像[b][c]=标题->p[a];
++a;
}
}
int k=0;

对于(int j=y1;j,我觉得这行看起来不对:像素图像[标题->高度][标题->宽度]@whoplisp如果高度为4,宽度为5,那么这将是一个2d数组,它将是图像[4][5]。我不想破坏,但我希望您的代码不是任何可供公众使用的程序的一部分。没有长度检查的固定大小字符数组容易出现缓冲区溢出。@Joe我知道这是一个2d数组,但是每当我使用这些数组时,我就遇到问题。我发现使用malloced 1d数组更可靠。@Whoplisp会导致我的apl吗要打印一个黑色正方形吗?如果你读了我的帖子,我在分解函数之前就已经让它工作了。谢谢,我只需要添加一个函数来对x和y进行排序,这样就可以了