Cs50 Pset4。调整更多。无法弄清楚如何缩小尺寸

时间:2018-08-29 04:16:43

标签: c cs50

我想出了如何放大图像,但是我一生都无法弄清楚如何缩小图像。当前代码将使位图的大小正确,但用随机颜色填充它。当前代码对我来说很有意义,我认为问题可能出在标题或填充上。请帮忙! 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;
}
`

1 个答案:

答案 0 :(得分:0)

我在您的代码中发现的问题是,遍历源代码的宽度/高度并尝试随手读取/写入将最终写入比应有的数据更多的数据,并可能使文件指针不同步。

例如,使用512x512的源图像和3的倍数,您最终每行写入额外的像素,这都是因为512 * 0.33与512/3不同,并且512%3!= 0;

我通常的做法是将数据加载到内存中,并在目标宽度/高度上进行迭代,从源中分配适当的像素。

我没有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;
}