我编写了一个程序,将位图的大小调整为0-100之间的值(小于1的值将缩小图像)。该程序可以放大文件,但无法缩小文件。
我想我可能不正确地使用fseek来跳过像素和行。
感谢您随时致力于解决此问题。
[请注意,代码很丑,因为我只是在学习编码。欢迎提出任何建议,但可能会引起其他混乱,因此请解释您对5岁儿童的回答。
您可以使用类似于“ ./resize .5 large.bmp test.bmp”的命令运行该程序
两个程序文件都附有resize.c和bmp.h
我还附加了之前和之后的图像,以更好地了解问题。
// Copies a BMP file
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bmp.h"
int zoom(float number, int biwidth);
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: copy infile outfile\n");
return 1;
}
// remember filenames
char* a = NULL;
float num0 = strtof(argv[1], &a);
int num = ceil(num0);
char *infile = argv[2];
char *outfile = argv[3];
if (num0 < 0 || num0 > 100)
{
fprintf(stderr, "Resize only 0-100. Try again.\n");
return 5;
}
// open input file
FILE *inptr = fopen(infile, "rb");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
// open output file
FILE *outptr = fopen(outfile, "wb");
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;
}
// calculate old padding
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
printf("padding%i ", padding);
//create new header files to save to
BITMAPINFOHEADER newbi = bi;
BITMAPFILEHEADER newbf = bf;
//determine width and set minimum size
int biwidth = round(bi.biWidth * num0);
if (biwidth < 3)
{
newbi.biWidth = 3;
}
else
{
newbi.biWidth = biwidth;
}
printf("newbiwidth%i ", newbi.biWidth);
//determine height and set minimum size
int biheight = round(bi.biHeight) * num0;
if (biheight > -3)
{
newbi.biHeight = -3;
}
else
{
newbi.biHeight = biheight;
}
printf("newbiheight%i ", newbi.biHeight);
// determine new padding for scanlines
int newpadding = (4 - (newbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
printf("newpadding%i ", newpadding);
//determine new image size
newbi.biSizeImage = (newbi.biWidth * sizeof(RGBTRIPLE) + newpadding) * abs(newbi.biHeight);
printf("newbisizeimage%i ", newbi.biSizeImage);
//determine new file size
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
int factor = zoom(num0, bi.biWidth);
RGBTRIPLE triple;
RGBTRIPLE *triple1 = malloc(sizeof(RGBTRIPLE) * factor);
RGBTRIPLE *sline = malloc(newbi.biWidth * sizeof(RGBTRIPLE));
//determine whether to loop old or new width
int biheight1 = 0;
int biwidth1= 0;
if ( bi.biWidth > newbi.biWidth)
biwidth1 = newbi.biWidth;
else
biwidth1 = bi.biWidth;
//determine whether to loop old or new height
if ( abs(bi.biHeight) > abs(newbi.biHeight))
biheight1 = abs(newbi.biHeight);
else
biheight1 = abs(bi.biHeight);
// read RGB triple from infile based on shrink or enlarge
for (int i = 0; i < biheight1; i++)
{
printf("H%i ", i);
for (int j = 0; j < biwidth1; j++)
{
printf("W%i ", j);
if (num0 > .5 && num0 < 1)
fread(triple1, sizeof(RGBTRIPLE), factor, inptr);
else
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//store new triple as new scanline
for (int m = 0; m < num ; m++)
{
if (num0 <= .5)
{
sline[j] = triple;
}
else if (num0 > .5 && num0 < 1)
{
sline[j] = *triple1;
}
else
{
sline[j * num + m] = triple;
}
printf("T%i,J%i ", m, j);
}
//skip pixel(s) if image is shrinking
for (int n = 0; n < num ; n++)
{
if (num0 > .5 && num0 < 1)
{
fseek(inptr, sizeof(RGBTRIPLE), SEEK_CUR);
}
else if (num0 <= .5)
{
fseek(inptr, (sizeof(RGBTRIPLE) * factor), SEEK_CUR);
}
}
}
// skip over padding, if any
fseek(inptr,padding, SEEK_CUR);
//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 scanline(s) if shrinking
for (int o = 0; o < num ; o++)
{
if (num0 > .5 && num0 < 1)
{
fseek(inptr, (bi.biWidth + padding), SEEK_CUR);
}
else if (num0 <= .5)
{
fseek(inptr, ((bi.biWidth + padding) * factor), SEEK_CUR);
}
}
}
//free memory
free(sline);
free(triple1);
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
//determine shrink factor
int zoom(float number, int biwidth)
{
int zoom1;
int a;
int b;
a = (biwidth * number);
b = (biwidth - a);
if ( a > b && b != 0)
{
zoom1 = a/b;
}
else if (a < b && a != 0)
{
zoom1 = b/a;
}
else if (b <= 0)
{
zoom1 = 1;
}
else
{
zoom1 = 1;
}
return zoom1;
}
// 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;
以下解决方案由@CraigEstey发布:
寻求跳过扫描线的操作不完整。 bi.biWidth需要乘以sizeof(RGBTRIPLE)
使用fseek(inptr,(((bi.biWidth * sizeof(RGBTRIPLE)+ padding),SEEK_CUR);代替fseek(inptr,(bi.biWidth + padding),SEEK_CUR);
此程序现在可以使用此修复程序来放大和缩小图像。