[英]How to resize a bitmap (BMP) in C - my code is not working
我一直在尝试修复此代码。 根据我添加的 printf 测试,一切似乎都运行良好,但新图像没有水平调整大小。
这是我正在参加的在线课程,因此编码可能看起来很初级,但我们可能还没有涵盖更高级的解决方案。
我已经粘贴了执行程序所需的两个程序,还包括了图片。
使用此参数或类似的./copy 2 staff.bmp large.bmp
执行程序。
// Copies a BMP file
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: copy infile outfile\n");
return 1;
}
// remember filenames
int num = atoi(argv[1]);
char *infile = argv[2];
char *outfile = argv[3];
if (num < 1 || num > 100)
{
fprintf(stderr, "Resize only 1-100. Try again.\n");
return 4;
}
// 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 padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
printf("padding%i ", padding);
BITMAPINFOHEADER newbi = bi;
BITMAPFILEHEADER newbf = bf;
newbi.biWidth = bi.biWidth * num;
printf("newbiwidth%i ", newbi.biWidth);
newbi.biHeight = bi.biHeight * num;
printf("newbiheight%i ", newbi.biHeight);
// determine padding for scanlines
int newpadding = (4 - (newbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
printf("newpadding%i ", newpadding);
newbi.biSizeImage = (newbi.biWidth * sizeof(RGBTRIPLE) + newpadding) * abs(newbi.biHeight);
printf("newbisizeimage%i ", newbi.biSizeImage);
newbf.bfSize = newbi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
printf("newbfsize%i ", newbf.bfSize);
// write outfile's BITMAPFILEHEADER
fwrite(&newbf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&newbi, sizeof(BITMAPINFOHEADER), 1, outptr);
// temporary storage
RGBTRIPLE triple;
RGBTRIPLE *newtriple = malloc(num * sizeof(RGBTRIPLE) + 1);
RGBTRIPLE *sline = malloc(newbi.biWidth * sizeof(RGBTRIPLE));
// read RGB triple from infile
for (int i = 0, biheight = abs(bi.biHeight); i < biheight; i++)
{
printf("H%i ", i);
for (int j = 0; j < bi.biWidth; j++)
{
printf("W%i ", j);
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//store triple times resize number
for (int m = 0; m < num; m++)
{
newtriple[m] = triple;
printf("T%i ", m);
}
//store new triple as new scanline
sline[j] = *newtriple;
printf("S%i ", j);
}
//write new scanline to file
for (int k = 0; k < num; k++)
{
printf("F%i ", k);
fwrite(sline, newbi.biWidth * 3, 1, outptr);
// add padding if any
for (int h = 0; h < newpadding; h++)
{
fputc(0x00, outptr);
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
}
}
free(newtriple);
free(sline);
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
// 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;
for (int j = 0; j < bi.biWidth; j++) { ... sline[j] = *newtriple; }
这只是为每一行设置从 0 到原始宽度的像素。 例如,如果您将宽度加倍,则图像的右后半部分不会被初始化,这就是输出图像中出现的内容。 您应该如下所示设置所有像素。 你实际上并不需要newtriple
还要确保在阅读每一行后立即跳过填充。
for(int j = 0; j < bi.biWidth; j++)
{
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
for(int m = 0; m < num; m++)
{
sline[j * num + m] = triple;
}
//sline[j] = *newtriple; <- remove
}
fseek(inptr, padding, SEEK_CUR); //<- move fseek here
sline[j * num + m]
正在做什么:
假设我们有这一行:
sline_in[4]: 0 1 2 3 4
我们想要放大 2 倍。它应该变成:
sline_out[8]: 0 0 1 1 2 2 3 3 4 4
我们使用循环:
for(int j = 0; j < width; j++)
{
sline_out[j * 2 + 0] = sline_in[j];
sline_out[j * 2 + 1] = sline_in[j];
}
对于任意缩放因子,我们使用以下内容:
for(int j = 0; j < width; j++)
{
for(int m = 0; m < zoom; m++)
sline_out[j * zoom + m] = sline_in[j];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.