Cs50 Pset4。调整更多。无法弄清楚如何让它变小

Cs50 Pset4. Resize More. Cant figure out how to make it smaller

我想出了如何使图像变大,但我终究无法弄清楚如何使图像变小。当前代码将使位图大小合适,但会用随机颜色填充。当前代码对我来说很有意义,我认为问题可能出在 headers 或填充上。请帮忙! Original vs output

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

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: ./resize f infile outfile\n");
        return 1;
    }

    /* GET FACTOR */
    float f = atof( argv[1]);
    if (f < 0.0 || f > 100.0)
    {
        fprintf(stderr, "The factor is out of bounds\n");
        return 5;
    }

    //check if we are making it bigger
    bool big = true;
    int factor = 0;
    if ( f <= 1.0)
    {
        big = false;
        factor = floor(1/f);
    }
    else
    {
        big = true;
        factor = floor(f);
    }
    /**/

    // remember filenames
    char *infile = argv[2];
    char *outfile = argv[3];

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

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

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

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

    // 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);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    /*Original pad*/
    int oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    /**/

    /*CHANGE THE INFOHEADER W&H SIZE*/
    bin.biWidth= floor(bi.biWidth * f);
    bin.biHeight= floor(bi.biHeight * f);

     /*NEW PADDING*/
     int padding = (4 - (bin.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    /*GET NEW SIZE*/
    bin.biSizeImage = ((sizeof(RGBTRIPLE) * bin.biWidth) + padding) * abs(bin.biHeight);
    bfn.bfSize = bin.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    /**/




    // write outfile's BITMAPFILEHEADER
    fwrite(&bfn, sizeof(BITMAPFILEHEADER), 1, outptr);

    // write outfile's BITMAPINFOHEADER
    fwrite(&bin, sizeof(BITMAPINFOHEADER), 1, outptr);



    if(big)
    {
        // iterate over infile's scanlines
       for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {

            for(int w = 0; w < factor; w++)
            {
                // temporary storage
                RGBTRIPLE triple;


                // iterate over pixels in scanline
                for (int j = 0; j < bi.biWidth; j++)
                {
                    // read RGB triple from infile
                    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                    for(int x = 0 ; x < factor ; x++)
                    {
                        // write RGB triple to outfile
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    }

                }
                for (int k = 0; k < padding; k++)
                {
                    fputc(0x00, outptr);
                }
                fseek(inptr, -1*sizeof(RGBTRIPLE)*bi.biWidth, SEEK_CUR);
            }
            // skip over padding, if any
            fseek(inptr, sizeof(RGBTRIPLE)*bi.biWidth+oldpadding, SEEK_CUR);
        }
    }
    else
    {
        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            // iterate over pixels in scanline
            for (int j = 0; j < bi.biWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;

                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                if(i%factor==0&&j%factor==0)
                {

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

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

            // then add it back (to demonstrate how)
            for (int k = 0; k < padding; k++)
            {
                fputc(0x00, outptr);
            }
        }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
`

我在您的代码中发现的问题是迭代源 width/height 并尝试 read/write 最终会写入比应有的更多数据,并可能获取文件指针不同步。

例如,对于 512x512 的源图像和 3 的因子,您最终会在每行中写入额外的像素,这既是因为 512 * 0.33 与 512 / 3 不同,也是因为 512 % 3 != 0;

我通常这样做的方式是将数据加载到内存中并遍历目标width/height从源分配适当的像素。

我没有 bmp.h,所以我加入了 Windows.h,并且我切换到以二进制模式打开文件,因为您不希望任何行结尾被翻译。我也在 C++ 模式下编译,因此如果您以 C 模式编译,则可能不需要一些转换。

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float f = 0.33f;
    int factor = 1 / f;
    FILE *inptr = fopen("infile.bmp", "rb");
    FILE *outptr = fopen("outfile.bmp", "wb");

    BITMAPFILEHEADER bf, bfn;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    bfn = bf;
    BITMAPINFOHEADER bi, bin;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    bin = bi;

    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    bin.biWidth *= f;
    bin.biHeight *= f;
    int srcStride = (bi.biWidth * sizeof(RGBTRIPLE) + 3) & ~3;
    int dstStride = (bin.biWidth * sizeof(RGBTRIPLE) + 3) & ~3;

    bin.biSizeImage = dstStride * abs(bin.biHeight);
    bfn.bfSize = bin.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    fwrite(&bfn, sizeof(BITMAPFILEHEADER), 1, outptr);
    fwrite(&bin, sizeof(BITMAPINFOHEADER), 1, outptr);

    char* srcData = (char*)calloc(1, bi.biSizeImage);
    fread(srcData, bi.biSizeImage, 1, inptr);
    char* dstData = (char*)calloc(1, bin.biSizeImage);

    for (int i = 0, binHeight = abs(bin.biHeight); i < binHeight; i++)
    {
        RGBTRIPLE* src = (RGBTRIPLE*)&srcData[srcStride * i * factor];
        RGBTRIPLE* dst = (RGBTRIPLE*)&dstData[dstStride * i];
        for (int j = 0; j < bin.biWidth; j++)
        {
            dst[j] = src[j * factor];
        }
    }
    fwrite(dstData, bin.biSizeImage, 1, outptr);
    fclose(inptr);
    fclose(outptr);
    free(srcData);
    free(dstData);
    return 0;
}