c - 在C中将图像转换为黑白的问题

标签 c linux bash ubuntu

我的程序有问题。我一直在尝试用谷歌搜索这些问题,但似乎找不到我可以使用的任何东西。我是 C 语言的新手,所以尽我所能学习。

当我尝试使用 ./imgconvert.c 运行它时,出现以下错误:

 ./imgconvert.c: line 6: struct: command not found
 ./imgconvert.c: line 7: uint8_t: command not found
 ./imgconvert.c: line 8: syntax error near unexpected token `}'
 ./imgconvert.c: line 8: `};'

我试图将程序编译成类似 myProgram.o 的东西: gcc -c imgconvert.c -o myProgram.o 然后是 ./myProgram。但是我得到一个权限错误,如果我用 chmod 修复它然后我得到这个错误:

bash: ./myProgram.o: cannot execute binary file

我不知道该怎么办?

代码:

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct pixel {
    uint8_t r, g, b, a;
};

static uint8_t *load_image(char *filename, int *sizex, int *sizey)
{
    uint8_t *image;
    char buf[512];
    char *bufptr;
    int ret;

    FILE *fp = fopen(filename, "r");
    bufptr = fgets(buf, 512, fp);
    ret = fscanf(fp, "%d %d\n", sizex, sizey);
    bufptr = fgets(buf, 512, fp);

    image = malloc(*sizex * *sizey * 4);

    int i;
    uint8_t *ptr = image;
    for (i=0; i<*sizex * *sizey; ++i) {
        ret = fread(ptr, 1, 3, fp);
        ptr += 4;
    }

    fclose(fp);
    return image;
}

static int save_image(const char *filename, uint8_t *image, int sizex, int sizey)
{
    FILE *fp = fopen(filename, "w");
    fprintf(fp, "P6\n%d %d\n255\n", sizex, sizey);

    int i;
    uint8_t *ptr = image;
    for (i=0; i<sizex * sizey; ++i) {
        fwrite(ptr, 1, 3, fp);
        ptr += 4;
    }
    fclose(fp);

    return 1;
}

void convert_grayscale(uint8_t *input, uint8_t *output, int sizex, int sizey)
{
    // Y = 0.299 * R + 0.587 * G + 0.114 * B

    int i;

    for (i = 0; i < sizex * sizey; ++i)
        {
            struct pixel *pin = (struct pixel*) &input[i*4];
            struct pixel *pout = (struct pixel*) &output[i*4];

            float luma = 0.299 * pin->r + 0.587 * pin->g + 0.114 * pin->b;

            if (luma > 255)
                luma = 255;

            uint8_t intluma = (int) luma;

            pout->r = intluma;
            pout->g = intluma;
            pout->b = intluma;
            pout->a = 255;
        }

}

int main()
{
    uint8_t *inputimg, *outputimg;
    int sizex, sizey;

    inputimg = load_image("image.ppm", &sizex, &sizey);

    outputimg = malloc(sizex * sizey * 4);

    convert_grayscale(inputimg, outputimg, sizex, sizey);

    save_image("output.ppm", outputimg, sizex, sizey);
}

最佳答案

您的直接问题是 C 程序必须编译并链接。您的 GCC 调用使用 -c 选项,告诉它只执行“编译”部分。试试看

gcc -g -Wall imgconvert.c -o imgconvert

然后

./imgconvert

我添加了一些新选项,-g 表示生成调试信息,-Wall 表示启用所有真正应该默认打开但实际上没有打开的警告吨。我没有详细查看您的代码,但是很有可能您会从第一个命令中收到一些“警告:”消息,您应该修复这些消息。

使用 -c 选项,您得到的是一个“目标”文件(这就是“.o”所代表的意思),它仅用作后续链接操作的输入。当您开始编写比一个文件合理容纳的更大的程序时,您会希望如此。

顺便说一句,当您尝试直接执行 C 源代码时遇到的错误是因为,由于为向后兼容而保留的古老默认值,shell 会尝试执行任何无法识别为已编译可执行文件的内容( \177ELF 在文件开头)或正确标记的解释脚本(#!/path/to/interpreter 在文件开头)就好像它是一个外壳脚本。

关于c - 在C中将图像转换为黑白的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20105904/

相关文章:

c - ANSI C 中的复合语句( block )是否被括号表达式包围?

c - 使用 free() 时内存使用量不会减少

linux - 一个进程占用多少核?

regex - bash 逃脱 ~ 代字号

mysql - 在同一 mysql session 中具有多个查询和重定向的 Bash 脚本

c++ - 使用 omp_set_num_threads() 将线程数设置为 2,但 omp_get_num_threads() 返回 1

具有一种功能的计算器

php - 如何创建一个空的 op_array?

linux - SSH - 帮助理解代理命令

swift - 在 bash 脚本中运行 swift 脚本