CS50调整大小-图像调整大小问题

时间:2019-10-02 15:26:55

标签: c cs50

我的代码对于f = 0.5似乎可以正常工作,但是与2个员工的解决方案不匹配。 if{}部分用于放大效果良好的图像。 else{}用于缩小尺寸。

对于f = 0.5,将跳过所有交替的行和列。它与员工的12x12像素bmp匹配,但与6x618x18不匹配。员工缩小bmp的算法是什么?

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

    // remember filenames
    float f = 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;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

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

    int oldbiWidth = bi.biWidth;
    int oldbiHeight = bi.biHeight;

    int intf = (int)f;
    float decimal = f - intf;
    int extrapixels = (int)(decimal * oldbiWidth);

    bool large = true;
    if (f < 1)
    {
        large = false;
    }

    bi.biWidth = (bi.biWidth * intf) + extrapixels;
    bi.biHeight = (bi.biHeight * intf) - extrapixels;


    int oldpadding = (4 - (oldbiWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // determine padding for scanlines
    int newpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + newpadding) * 
    abs(bi.biHeight);
    bf.bfSize = bi.biSizeImage + sizeof(BITMAPINFOHEADER) + 
    sizeof(BITMAPFILEHEADER);


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

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

    oldbiHeight = abs(oldbiHeight);

    float a = (float)1.0 / (float)f;
    float b = a - 1;
    float c = 0;
    float d = 0;

    float foldbiWidth = (float)oldbiWidth;

    int extraPixelCount;

    // >1
    if (large)
    {
        //code to enlarge the size of an image
    }

    // <1
    else
    {
        for (int i = 0; i < oldbiHeight; i++)
        {

            if (d < 1)
            {

                for (int j = 0; j < oldbiWidth; j++)
                {
                    // temporary storage
                    RGBTRIPLE triple;

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

                        // write RGB triple to outfile
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                        c = c + b;
                    }
                    else
                    {
                        fseek(inptr, 3, SEEK_CUR);
                        c = c - 1;
                    }
                }

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

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

                d = d + b;
            }
            else
            {
                fseek(inptr, oldbiWidth * sizeof(RGBTRIPLE), SEEK_CUR);
                d = d - 1;
            }
        }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

0 个答案:

没有答案