填充位图文件

时间:2017-07-05 16:30:29

标签: c bitmap resize cs50

我希望能够在C中调整位图图片的大小,我相信有一些填充问题会导致新图片出错,但我不知道问题是什么。

/**
* Copies a BMP piece by piece, just because.
*/ 

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

#include "bmp.h"

int main(int argc, char *argv[])
{

double n = atof(argv[1]);

// ensure proper usage
if (argc != 4)
{
    fprintf(stderr, "Usage: ./resize n infile outfile\n");
    return 1;
}

if (n >= 100 || n < 0)
{
    fprintf(stderr, "n must be positibe and less than 100\n");
    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;
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;
}



// Change height and width
bi.biHeight *= n;
bi.biWidth *= n;

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

// Change size and image size
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);   
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    //resize vertically
    for (int j = 0; j < n; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

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

        // iterate over pixels in scanline
        for (int k = 0; k < bi.biWidth; k++)
        {
            // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), n, outptr);
        }
        // Add padding, if necessary
        for (int l = 0; l < padding; l++)
        {
            fwrite(0x00, 1, padding, outptr);
        }
        if (j == n - 1)
        {
            // skip over padding, if any
            fseek(inptr, padding, SEEK_CUR);
        }
        else
        {
            fseek(inptr, bi.biWidth, SEEK_CUR);
        }
    }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

抱歉可怕的间距。

1 个答案:

答案 0 :(得分:0)

您对infile使用与outfile相同的填充,并将像素混合在一起。

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

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi_in;
fread(&bi_in, sizeof(BITMAPINFOHEADER), 1, inptr);

// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi_in.biSize != 40 || 
    bi_in.biBitCount != 24 || bi_in.biCompression != 0)
{
    fclose(outptr);
    fclose(inptr);
    fprintf(stderr, "Unsupported file format.\n");
    return 4; 
}

// build outfile's BITMAPINFOHEADER and Change height and width
BITMAPINFOHEADER bi_out;
bi_out=bi_in;

bi_out.biHeight *= n;
bi_out.biWidth *= n;

// determine paddings
int padding_in  = (4 - (bi_in.biWidth  * sizeof(RGBTRIPLE)) % 4) % 4;
int padding_out = (4 - (bi_out.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// Change size and image size
bi_out.biSizeImage = ((sizeof(RGBTRIPLE) * bi_out.biWidth) + padding_out) * abs(bi_out.biHeight);   
bf.bfSize = bi_out.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

// iterate over infile's scanlines
for (int i = abs(bi_in.biHeight); i--;)
{
    long StartOfScanline = ftell(inptr); //we have to return here
    //resize vertically, issue the same line n times
    for (int j = 0; j < n; j++)
    {
        fseek(inptr,StartOfScanline, SEEK_SET); //go to start of line 
        for (int k=bi_in.biWidth; k--;) //for each pixel in line...
        {   
            RGBTRIPLE triple;
            // read one RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
            for (int l=n;l--;) //write out pix n times 
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
        // Add padding, if necessary
        //This was wrong: fwrite needs a pointer to data, and not 0x00!!
        //for (int l = 0; l < padding_out; l++)
        //    fwrite(0x00, 1, 1, outptr);
        //FIXED:
        static const char Padding[4]={0,0,0,0};
        fwrite(Padding, padding_out, 1, outptr);

    }
    //add padding to infile
    fseek(inptr, padding_in, SEEK_CUR);
}