#CS50#PSET3调整更多大小。减小bmp图像大小时的问题

我正在尝试减小bmp图像的大小(基于称为n的因数),并且在此上花费了数小时(实际上是几天)后,我无法使其工作(即使简单的因数n = 1/2) 。我遵循的(错误)逻辑是将光标移动+ = 1 / N(请参阅最后2个fseek),但是由于某些原因,输出是完全错误的。感谢您提供任何指导技巧来解决这种情况。

`

`// Copies a BMP file

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

#include "bmp.h"
#include <math.h>


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

    // remember filenames
    float n=atof(argv[1]);
    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;
       // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    BITMAPFILEHEADER nbf;
    BITMAPINFOHEADER nbi;

    fread(&bf,sizeof(BITMAPFILEHEADER),1,inptr);
    fread(&bi,sizeof(BITMAPINFOHEADER),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);
        fprintf(stderr,"Unsupported file format.\n");
        return 4;
    }

    nbf=bf;
    nbi=bi;

    nbi.biWidth *= n;
    nbi.biHeight *=n;
    int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int npadding = (4 - (nbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    nbi.biSizeImage=((sizeof(RGBTRIPLE)*nbi.biWidth)+npadding)*nbi.biHeight;
    nbf.bfSize=nbi.biSizeImage+sizeof(BITMAPINFOHEADER)+sizeof(BITMAPFILEHEADER);
    // write outfile's BITMAPFILEHEADER
    fwrite(&nbf,outptr);

    // write outfile's BITMAPINFOHEADER
    fwrite(&nbi,outptr);



    if (n >= 0 && n < 1)
    {

     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),inptr);


            // write RGB triple to outfile
            fwrite(&triple,outptr);

            //**This is the current logic=skipping the next pixel by 1/n**
            fseek(inptr,sizeof(RGBTRIPLE)*(1/n),SEEK_CUR);
        }



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

        // then add it back (to demonstrate how)
        for (int k = 0; k < npadding; k++)
        {
            fputc(0x00,outptr);
        }
        //*This is meant to skip the number of lines by (1/n)+padding*
        fseek(inptr,sizeof(RGBTRIPLE)*bi.biWidth*(1/n)+padding,SEEK_CUR);

    }

}

    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;

    }
haha004 回答:#CS50#PSET3调整更多大小。减小bmp图像大小时的问题

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3070546.html

大家都在问