C-调整程序适用于某些而不是其他程序



,因此问题应该进行3个参数(因素,填充和外档(。该因子是1-100的正整数。然后,该程序应该调整含图像的大小。如果因素为1:产生相同的图像。如果因素为2:产生图像的大两倍。等等。输出图像应写入外档。

目前,我的程序成功地用于某些图像,仅在某些比例因素上进行。

当我通过课程的IDE检查程序进行此问题时,我收到的结果是:

:) resize.c和bmp.h存在。
:) resize.c compiles。
:) n是1
时不调整small.bmp的大小:(当n为2
时,正确调整了Small.BMP像素数据的字节34不匹配。预期0xff,而不是0x00

:(当n为3是3
时,正确调整了Small.BMP像素数据的字节48不匹配。预期0xff,而不是0x00

:(当n为4
时,正确调整了Small.BMP像素数据的字节62不匹配。预期0xff,而不是0x00

:(当n为5
时,正确调整了Small.BMP像素数据的字节80不匹配。预期0xff,而不是0x00

:)当n为2
时,正确调整了大尺寸。:)当n为2

时,正确调整smiley.bmp
// Copies a BMP file and resizes it
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: ./resize factor infile outfilen");
        return 1;
    }
    // Check argument 1 to see if integer within acceptable range
    int factor = atoi(argv[1]);
    if (factor <= 0 || factor > 100)
    {
        fprintf(stderr, "Must be a positive integer greater than 0 and equal or less than 100n");
        return 1;
    }
    // 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;
    BITMAPFILEHEADER bf_New;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    bf_New = bf;
    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    BITMAPINFOHEADER bi_New;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    bi_New = 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;
    }
    // set new height and width of BMP
    bi_New.biHeight = bi.biHeight * factor;
    bi_New.biWidth = bi.biWidth * factor;
    // calculate padding for old file and new file
    int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int padding_New = (4 - (bi_New.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    // set the file size for the new file
    bi_New.biSizeImage = (bi_New.biWidth * sizeof(RGBTRIPLE) + padding_New) * abs(bi_New.biHeight);
    bf_New.bfSize = bi_New.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    // write outfile's BITMAPFILEHEADER
    fwrite(&bf_New, sizeof(BITMAPFILEHEADER), 1, outptr);
    // write outfile's BITMAPINFOHEADER
    fwrite(&bi_New, sizeof(BITMAPINFOHEADER), 1, outptr);
    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            // itterate factor times
            for (int k = 0; k < factor; k++)
            {
                // 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);
                    // iterate over horizontal pixels
                    for (int l = 0; l < factor; l++)
                    {
                        // write RGB triple to outfile iterate the same pixel by factor times
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    }
                }
                // skip over padding, if any
                fseek(inptr, padding, SEEK_CUR);
                // add new padding
                for (int m = 0; m < padding_New; m++)
                {
                    fputc(0x00, outptr);
                }
                // seek back to the beginning of row in input file, but not after iteration of printing
                if (k + 1 < factor )
                {
                    fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
                }
            }
        }
    // close infile
    fclose(inptr);
    // close outfile
    fclose(outptr);
    // success
    return 0;
}

我找到了我希望的地方。不包括填充..我认为您可以这样分开。准备启动新线...

相关内容

最新更新