gpt4 book ai didi

c - 我陷入了 pset 4 - CS50 垂直调整大小的困境

转载 作者:行者123 更新时间:2023-11-30 14:37:02 29 4
gpt4 key购买 nike

我陷入了调整大小问题的垂直调整大小部分。我从 Zamayla 的伪代码中知道,我每次都需要在输出文件上写入一个数组,但我不知道如何将值从一个传递到另一个。我是否需要使用 malloc 函数并通过指针传递?我使用这个的经验不是很丰富,因为我是编程新手。请告诉我我的代码是否朝着正确的方向发展。

// Resize 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: resize queficient infile outfile\n");
return 1;
}
int n = atoi(argv[1]);

if (n < 1 || n > 100)
{
printf ("Resize queficient should be between 1 and 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;
}
/* Update header info
*saving old information
*/
BITMAPINFOHEADER old_bi;
BITMAPFILEHEADER old_bf;
old_bi = bi;
old_bf = bf;
old_bi.biHeight = bi.biHeight;
old_bi.biWidth = bi.biWidth;
int old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// assign new header
bi.biHeight = bi.biHeight * n;
bi.biWidth = bi.biWidth * n;
int new_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + new_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);



// Resize horizontally
for (int i = 0, old_biHeight = abs(old_bi.biHeight); i < old_biHeight; i++)
{
// iterate over pixels in scanline
for (int j = 0; j < old_bi.biWidth; j++)
{

// temporary storage
RGBTRIPLE triple;

// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

triple.rgbtBlue = n * triple.rgbtBlue;
triple.rgbtRed = n * triple.rgbtRed;
triple.rgbtGreen = n * triple.rgbtGreen;

// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}

// skip over padding, if any
fseek(inptr, old_padding, SEEK_CUR);

// then add it back (to demonstrate how)
for (int k = 0; k < old_padding; k++)
{
fputc(0x00, outptr);
}
}

/*Resize vertically
for (int i = 0, old_biHeight = abs(old_bi.biHeight); i < old_biHeight; i++ )
{
for (int j = 0; j < old_bi.biWidth; j++)
{
RGBTRIPLE triple;
char *array_of_pixels_Blue = { "n * triple.rgbtBlue" };
char *array_of_pixels_Red = { "n * triple.rgbtRed" };
char *array_of_pixels_Green = { "n * triple.rgbtGreen" };
}

for (int k = 0; k < n; k++)
{
fwrite(&array_of_pixels_Blue, sizeof(RGBTRIPLE), 1, outptr);
fwrite(&array_of_pixels_Red, sizeof(RGBTRIPLE), 1, outptr);
fwrite(&array_of_pixels_Green, sizeof(RGBTRIPLE), 1, outptr);

for (int l = 0; l < new_padding; l++)
{
fputc(0x00, outptr);
}
}
}
fseek(inptr, new_padding, SEEK_CUR);
*/

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

最佳答案

为了简单起见,我们假设源图像是 3x2 像素。如果你用像素坐标在“纸”上画出来(我真的建议你用真正的笔和纸),它看起来像这样:

+-----+-----+-----+| 0,0 | 1,0 | 2,0 |+-----+-----+-----+| 0,1 | 1,1 | 2,1 |+-----+-----+-----+

If you multiply this with 2 (to get a double-sized image) then that becomes a 6x4 pixel image. And the easiest way to enlarge the pixels is simply to double them as well, in all directions. Then that enlarged image would look something like this (with the pixel coordinates of the orginial image):

+-----+-----+-----+-----+-----+-----+| 0,0 | 0,0 | 1,0 | 1,0 | 2,0 | 2,0 |+-----+-----+-----+-----+-----+-----+| 0,0 | 0,0 | 1,0 | 1,0 | 2,0 | 2,0 |+-----+-----+-----+-----+-----+-----+| 0,1 | 0,1 | 1,1 | 1,1 | 2,2 | 2,2 |+-----+-----+-----+-----+-----+-----+| 0,1 | 0,1 | 1,1 | 1,1 | 2,2 | 2,2 |+-----+-----+-----+-----+-----+-----+

Each pixel from the original image is now four pixels in the new image. The size grows by the square of the multiplier (so a multiplier of 3 means each pixel in the original images becomes 9 pixels in the new image).

There are a few different ways to handle it code-wise, but the simplest is to allocate two memory areas, one for the original image being WxH "pixels" large, and the other being (W*n)x(H*n) "pixels" large. Then you use a loop to fetch each single pixel from the original image, and nested inside you have another loop that writes that pixel to all its locations in the new image.

In pseudo code it could be something like this

// Loops over all the pixels in the original source image
for (unsigned orig_x = 0; orig_x < ORIG_WIDTH; ++orig_x)
{
for (unsigned orig_y = 0; orig_y < ORIG_HEIGHT; ++orig_y)
{
orig_pixel = GetPixelAt(orig_pixel_data, orig_x, orig_y);

// Write the original pixel (orig_pixel) N*N times in the new image data
for (unsigned new_x = 0; new_x < N; ++new_x)
{
for (unsigned new_y = 0; new_y < N; ++new_y)
{
SetPixelAt(new_pixel_data, new_x + orig_x * N, new_y + orig_y * N, orig_pixel);
}
}
}
}

现在您可能已经注意到,我没有提到“像素”的类型。那是因为这并不重要。它可以是浮点值、整数值或RGBTRIPLE结构。

正如我之前提到的,首先使用笔和纸来完成这一切

关于c - 我陷入了 pset 4 - CS50 垂直调整大小的困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57653142/

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