C 创建 PGM 图像的参数编号
C Argument number to create PGM images
我必须用 C 为大学做一个任务。程序在 PGM 中创建图像 format.The 程序编译成功,但是,我不能 运行 程序正确。在主函数的开头,if 运算符需要 2 个 argc,但每次我尝试 运行 程序时,我只有一个 argc,我得到结果
“错误:参数丢失或错误,使用 a3 [图像文件]”
密码是:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
const double PI = 3.1415926535897932;
int** loadImage (const char *filename, int* width, int* height);
bool saveImage (const char *filename, int **pixels,
const unsigned int width, const unsigned int height);
/* *** task (a) *** */
int normalize(int value) {
return 0; // Dummy return
}
/* *** task (b) *** */
int** createImage(unsigned int width, unsigned int height, int value){
return NULL; // Dummy return
}
/* *** task (c) *** */
void freeImage(int** pixels, unsigned int width, unsigned int height){
}
/* *** task (d) *** */
int** inverter (int **pixels, int width, int height)
{
return NULL; // Dummy return
}
/* *** task (e) *** */
int** laplace (int **pixels, int width, int height)
{
return NULL; // Dummy return
}
/* *** task (f) *** */
int** rotate (int** pixels, int width, int height, double angle, int background)
{
return NULL; // Dummy return
}
int main (int argc, char **argv)
{
if (argc == 2)
{
int width=0, height=0;
int **image = loadImage (argv[1], &width, &height);
if (image != NULL)
{
int **image1 = inverter (image, width, height);
int **image2 = laplace (image, width, height);
int **image3 = rotate (image, width, height, 150, 128);
saveImage ("image0.pgm", image, width, height);
saveImage ("image1.pgm", image1, width, height);
saveImage ("image2.pgm", image2, width, height);
saveImage ("image3.pgm", image3, width, height);
freeImage (image, width, height);
freeImage (image1, width, height);
freeImage (image2, width, height);
freeImage (image3, width, height);
}
}
else {
printf ("Error: missing or bad parameters, usage a3 [imagefile]\n");
}
return 0;
}
/* *** Bild aus Datei laden *** */
int** loadImage (const char *filename, int* width, int* height)
{
const char *delimiter = " \n\r\t";
FILE *file = fopen (filename, "rt");
int** pixels;
bool isValid = true;
if (file != NULL)
{
char line [80];
int n = 0, params [3];
if (fgets (line, 80, file) == NULL) isValid = false;
if (strlen (line) != 3 || strncmp (line, "P2", 2)) isValid = false;
while (isValid && fgets (line, 80, file) != NULL)
{
if (strlen (line) <= 71)
{
if (line[0] == '#') continue;
char *p = strtok (line, delimiter);
while (isValid && p != NULL)
{
if (n < 3)
{
int a = atoi (p);
if (a <= 0) {isValid = false;}
else {params[n++] = a;}
}
else
{
if (n == 3)
{
if (params[2] != 255) {isValid = false;}
else {
pixels = createImage (params[0], params[1], 0);
*width = params[0];
*height = params[1];
}
}
if (n-3 < *width * *height)
{
int x = (n-3) % *width;
int y = (n-3) / *width;
int value = atoi (p);
pixels[y][x] = normalize(value);
n++;
}
else {isValid = false;}
}
p = strtok (NULL, delimiter);
}
}
else { isValid = false;}
}
fclose (file);
if (n-3 != *width * *height) {isValid = false;}
if (!isValid) freeImage (pixels, *width, *height);
}
return isValid ? pixels : NULL;
}
/* *** Bild in Datei speichern *** */
bool saveImage (const char *filename, int **pixels, unsigned int width, unsigned int height)
{
FILE *file = fopen (filename, "wt");
if (file != NULL && pixels != NULL)
{
fprintf (file, "P2\n");
fprintf (file, "%d %d\n", width, height);
fprintf (file, "%d\n", 255);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
fprintf (file, "%d\n", pixels[y][x]);
}
}
fclose (file);
return true;
}
return false;
}
教授还说我们应该在VSCode的任务中添加以下参数:
"args": [
"-g",
"-std=c99",
"-Wall",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lm"
],
您的程序 a3.exe
需要一个参数,即文件名。要从命令行 运行 它,请输入
.\a3.exe YourFilename
将 YourFilename
替换为您要使用的实际文件名。
我必须用 C 为大学做一个任务。程序在 PGM 中创建图像 format.The 程序编译成功,但是,我不能 运行 程序正确。在主函数的开头,if 运算符需要 2 个 argc,但每次我尝试 运行 程序时,我只有一个 argc,我得到结果
“错误:参数丢失或错误,使用 a3 [图像文件]”
密码是:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
const double PI = 3.1415926535897932;
int** loadImage (const char *filename, int* width, int* height);
bool saveImage (const char *filename, int **pixels,
const unsigned int width, const unsigned int height);
/* *** task (a) *** */
int normalize(int value) {
return 0; // Dummy return
}
/* *** task (b) *** */
int** createImage(unsigned int width, unsigned int height, int value){
return NULL; // Dummy return
}
/* *** task (c) *** */
void freeImage(int** pixels, unsigned int width, unsigned int height){
}
/* *** task (d) *** */
int** inverter (int **pixels, int width, int height)
{
return NULL; // Dummy return
}
/* *** task (e) *** */
int** laplace (int **pixels, int width, int height)
{
return NULL; // Dummy return
}
/* *** task (f) *** */
int** rotate (int** pixels, int width, int height, double angle, int background)
{
return NULL; // Dummy return
}
int main (int argc, char **argv)
{
if (argc == 2)
{
int width=0, height=0;
int **image = loadImage (argv[1], &width, &height);
if (image != NULL)
{
int **image1 = inverter (image, width, height);
int **image2 = laplace (image, width, height);
int **image3 = rotate (image, width, height, 150, 128);
saveImage ("image0.pgm", image, width, height);
saveImage ("image1.pgm", image1, width, height);
saveImage ("image2.pgm", image2, width, height);
saveImage ("image3.pgm", image3, width, height);
freeImage (image, width, height);
freeImage (image1, width, height);
freeImage (image2, width, height);
freeImage (image3, width, height);
}
}
else {
printf ("Error: missing or bad parameters, usage a3 [imagefile]\n");
}
return 0;
}
/* *** Bild aus Datei laden *** */
int** loadImage (const char *filename, int* width, int* height)
{
const char *delimiter = " \n\r\t";
FILE *file = fopen (filename, "rt");
int** pixels;
bool isValid = true;
if (file != NULL)
{
char line [80];
int n = 0, params [3];
if (fgets (line, 80, file) == NULL) isValid = false;
if (strlen (line) != 3 || strncmp (line, "P2", 2)) isValid = false;
while (isValid && fgets (line, 80, file) != NULL)
{
if (strlen (line) <= 71)
{
if (line[0] == '#') continue;
char *p = strtok (line, delimiter);
while (isValid && p != NULL)
{
if (n < 3)
{
int a = atoi (p);
if (a <= 0) {isValid = false;}
else {params[n++] = a;}
}
else
{
if (n == 3)
{
if (params[2] != 255) {isValid = false;}
else {
pixels = createImage (params[0], params[1], 0);
*width = params[0];
*height = params[1];
}
}
if (n-3 < *width * *height)
{
int x = (n-3) % *width;
int y = (n-3) / *width;
int value = atoi (p);
pixels[y][x] = normalize(value);
n++;
}
else {isValid = false;}
}
p = strtok (NULL, delimiter);
}
}
else { isValid = false;}
}
fclose (file);
if (n-3 != *width * *height) {isValid = false;}
if (!isValid) freeImage (pixels, *width, *height);
}
return isValid ? pixels : NULL;
}
/* *** Bild in Datei speichern *** */
bool saveImage (const char *filename, int **pixels, unsigned int width, unsigned int height)
{
FILE *file = fopen (filename, "wt");
if (file != NULL && pixels != NULL)
{
fprintf (file, "P2\n");
fprintf (file, "%d %d\n", width, height);
fprintf (file, "%d\n", 255);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
fprintf (file, "%d\n", pixels[y][x]);
}
}
fclose (file);
return true;
}
return false;
}
教授还说我们应该在VSCode的任务中添加以下参数:
"args": [
"-g",
"-std=c99",
"-Wall",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lm"
],
您的程序 a3.exe
需要一个参数,即文件名。要从命令行 运行 它,请输入
.\a3.exe YourFilename
将 YourFilename
替换为您要使用的实际文件名。