gpt4 book ai didi

c - 位图文件的填充

转载 作者:行者123 更新时间:2023-11-30 16:43:50 25 4
gpt4 key购买 nike

我希望能够在 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;
}

抱歉,间距太糟糕了。

最佳答案

您对内文件和外文件使用相同的填充,并且混合了像素。

// 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);
}

关于c - 位图文件的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44931591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com