c - 为什么会出现 'invalid or unsupported image format'错误?

标签 c cs50

我一直在研究 CS50 的 pset3,并且已经被这个问题困扰了一段时间。我从 copy.c 复制了大部分代码,并引用演练来设计尽可能相似的代码,但我似乎无法显示放大的图像。代码编译得很好,当我运行 debug50 时似乎没有任何问题,所以我不太确定如何执行此操作。

//复制BMP文件

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>

    #include "bmp.h"

    int main(int argc, char *argv[])
    {

        // ensure proper usage
        if (argc != 4)
        {
            printf("Usage: ./resize n infile outfile");
            return 1;
        }

        // remember filenames
        int n = atoi(argv[1]);
        char *infile = argv[2];
        char *outfile = argv[3];

        if (n < 0 || n > 100)
        {
            printf("Usage: ./resize n infile outfile");
            return 1;
        }



        // open input file
        FILE *inptr = fopen(infile, "r");
        if (inptr == NULL)
        {
            printf("Could not open %s.\n", infile);
            return 2;
        }

        // open output file
        FILE *outptr = fopen(outfile, "w");
        if (outptr == NULL)
        {
            fclose(inptr);
            printf("Could not create %s.\n", outfile);
            return 3;
        }

        // read infile's BITMAPFILEHEADER
        BITMAPFILEHEADER bf, bf_new;
        fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

        // read infile's BITMAPINFOHEADER
        BITMAPINFOHEADER bi, bi_new;
        fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

        // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
        if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
            bi.biBitCount != 24 || bi.biCompression != 0)
        {
            fclose(outptr);
            fclose(inptr);
            printf("Unsupported file format.\n");
            return 4;
        }

        bi_new = bi;
        //increase the size of biWidth for outfile
        bi_new.biWidth = bi.biWidth * n;
        //increase the size of biHeight for outfile
        bi_new.biHeight = bi.biHeight * n;
        // determine padding for scanlines
        int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
        int newpadding = (4 - (bi_new.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

        bi_new.biSizeImage = (bi_new.biWidth * sizeof(RGBTRIPLE) + newpadding) * abs(bi_new.biHeight);
        // write outfile's BITMAPINFOHEADER
        fwrite(&bi_new, sizeof(BITMAPINFOHEADER), 1, outptr);

        bf_new = bf;
        bf_new.bfSize = bi_new.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
        // write outfile's BITMAPFILEHEADER
        fwrite(&bf_new, sizeof(BITMAPFILEHEADER), 1, outptr);

        // iterate over infile's scanlines
        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            //multiply each scanline by n
            for (int j = 1; j <= n; j++)
            {
                // iterate over pixels in scanline
                for (int k = 0; k < bi.biWidth; k++)
                {
                    //multiply each pixel in the width by n times
                    for (int l = 0; l < n; l++)
                    {
                        // temporary storage
                        RGBTRIPLE triple;

                        // read RGB triple from infile
                        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                        // write RGB triple to outfile
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    }


                }
                // skip over padding, if any
                fseek(inptr, padding, SEEK_CUR);

                //add padding to the outfile's scanline
                for (int m = 0; m < newpadding; m++)
                    {
                        fputc(0x00, outptr);
                    }
            }
        }

        // close infile
        fclose(inptr);

        // close outfile
        fclose(outptr);

        // success
        return 0;
    }

最佳答案

您放大图像的方式是错误的。现在,您可以在最内层循环中读取像素。那是行不通的。

您必须读取整个扫描线,将其像素放大 n 倍到新的扫描线中,然后将该扫描线写入 n 次。

我让您相应地更改您的代码。

关于c - 为什么会出现 'invalid or unsupported image format'错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59466357/

相关文章:

将 2 个 uint16_t 组合成 1 个 uint32_t

c - 有符号整数溢出 : 999999999 * 10 cannot be represented in type 'int' Error

c - "Vigenere": String lowercasing program is appending character for some inputs

c++ - 使用 C/C++ 读取/写入文件元数据

C 字符串指针与数组

c - 分配链表后出现段错误

c - 如何在 C 中检查动态分配的字符串数组中的每个字符?

python - 如何从整数列表中提取单个数字?

c - 如何定义此代码并将其作为函数调用?

c - 将 type int 乘以 struct 时出错