为什么会出现错误“无效或不受支持的图像格式”?

时间:2019-12-24 09:07:24

标签: 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;
    }

2 个答案:

答案 0 :(得分:1)

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

您必须读取一条完整的扫描线,将其像素放大n倍至新的扫描线,然后再将其写入n遍。

我请您相应地更改代码。

答案 1 :(得分:0)

您的问题:

为什么会出现错误“无效或不受支持的图像格式”

出现该图像是因为当您尝试显示图像时,操作系统会识别出图像文件的格式不正确。

bmp image format