位图文件的填充
Padding for bitmap file
我希望能够在 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 中调整位图图片的大小,我认为存在一些填充问题导致新图片出现错误,但我不知道是什么问题。
/**
* 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);
}