调整 bmp 图像大小
Posted
技术标签:
【中文标题】调整 bmp 图像大小【英文标题】:Resizing bmp image 【发布时间】:2020-10-29 14:30:09 【问题描述】:我正在尝试调整 bmp 图像的大小,它几乎可以工作,但由于某种原因,我得到了用零填充的额外字节。它有点工作,但我想知道这些零是从哪里来的。
我不确定它是否与那些零以某种方式连接,但有时图像深度 (biBitCount) 从原始文件中的 24 变为 32。我无法弄清楚为什么会这样。不幸的是,我没有保存那个截图。
我敢肯定,我只是犯了一些我看不到的愚蠢错误。请帮帮我。 original bmp double sized bmp with extra zeros
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "bmp.h"
float atoi2_0(char* S);
int main(int argc, char *argv[])
// ensure proper usage
if (argc != 4)
fprintf(stderr, "Usage: ./resize f infile outfile\n");
return 1;
// remember filenames
float f = atoi2_0(argv[1]);
if(f == -1 || f <= 0.0 || f > 100)
printf("Usage: ./resize f infile outfile\n");
return 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 oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biWidth *= f;
bi.biHeight *= f;
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + 54;
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
float df = f - (int)f;
int revdf = 1 / df;
int bbw = bi.biWidth / f;
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
RGBTRIPLE * oldtriple = (RGBTRIPLE*)calloc(bbw, sizeof(RGBTRIPLE));
if (oldtriple == NULL)
fclose(inptr);
fclose(outptr);
return 5;
fread(oldtriple, sizeof(RGBTRIPLE), bbw, inptr);
RGBTRIPLE * triple = (RGBTRIPLE*)calloc(bi.biWidth, sizeof(RGBTRIPLE));
if (triple == NULL)
free(oldtriple);
fclose(inptr);
fclose(outptr);
return 5;
for (int j = 0; j < bbw; j++)
int jxf = j * f;
for (int n = 0; n < f; n++)
*(triple + jxf + n) = *(oldtriple + j);
for (int j = 0; j < f; j++)
if (i % revdf && f - j == df) continue;
fwrite(triple, sizeof(RGBTRIPLE), bi.biWidth, outptr);
for (int n = 0; n < padding; n++)
fputc(0x00, outptr);
fseek(inptr, oldpadding, SEEK_CUR);
free(triple);
free(oldtriple);
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
float atoi2_0(char* S)//kinda atoi for floats
int strl = strlen(S);
for (int i = 0; i < strl; i++)//input check
if ((S[i] < '0' || S[i] > '9') && (S[i] != '.'))
printf("Invalid input");
return -1;
float a = atoi(S);
char D[10] = 0 ;
for (int i = 0; i < strl; i++)
if (S[i] == '.')
++i;
for (int j = 0; i < strl; i++, j++)
D[j] = S[i];
a += (float)atoi(D) / pow(10, strlen(D));
return a;
BMP.h :
``` // BMP-related data types based on Microsoft's own
#include <stdint.h>
// aliases for C/C++ primitive data types
// https://msdn.microsoft.com/en-us/library/cc230309.aspx
typedef uint8_t BYTE;
typedef uint32_t DWORD;
typedef int32_t LONG;
typedef uint16_t WORD;
// information about the type, size, and layout of a file
// https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx
typedef struct
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
__attribute__((__packed__))
BITMAPFILEHEADER;
// information about the dimensions and color format
// https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx
typedef struct
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
__attribute__((__packed__))
BITMAPINFOHEADER;
// relative intensities of red, green, and blue
// https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx
typedef struct
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
__attribute__((__packed__))
RGBTRIPLE;
【问题讨论】:
您知道stdlib.h 包含atoi
和atof
吗?
不,我不知道,但这不是什么大问题。
【参考方案1】:
是的,我自己修好了。问题在于周期长度。
【讨论】:
以上是关于调整 bmp 图像大小的主要内容,如果未能解决你的问题,请参考以下文章