模块化出了问题
所以我有一个有效的大型函数。然后我决定修改它,现在我只是打印出一个黑色方块。我附上我的代码,看看是否有人能理解发生了什么。这三个函数曾经是一个大函数: 函数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);
}
前两个函数在main 中调用。
So I had one large function that worked. Then I decided to modify it and now I am just printing out a black square. I am attaching my code to see if any one would understand what is happening. These three function where once large function:
Function 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);
}
The first two function are call in main.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我有用。
This works for me.