gpt4 book ai didi

c - 如何在 C 中调整位图 (BMP) 的大小 - 我的代码不起作用

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

我一直在尝试修复此代码。根据我添加的 printf 测试,一切似乎都工作正常,但新图像没有水平调整大小。

这是我正在学习的在线类(class),因此编码可能看起来很简单,但我们可能还没有涵盖更高级的解决方案。

我已经粘贴了执行该程序所需的两个程序,并且还包括了图片。

使用此参数或类似的参数执行程序./copy 2 Staff.bmp large.bmp

image to resize image after resize

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

关于c - 如何在 C 中调整位图 (BMP) 的大小 - 我的代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59348534/

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