调整位图文件的大小部分有效 - 放大有效但缩小无效
Resizing Bitmap Files Partially Working - Enlarge Works but Reducing Doesn't
我编写了一个程序,可以将位图的大小调整为 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 发布了以下解决方案:
用于跳过扫描线的 fseek 不完整。 bi.biWidth 需要乘以 sizeof(RGBTRIPLE)
使用 fseek(inptr, ((bi.biWidth * sizeof(RGBTRIPLE) + padding), SEEK_CUR); 而不是 fseek(inptr, (bi.biWidth + padding), SEEK_CUR);
该程序现在可以通过此修复来放大和缩小图像。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
int main()
{
struct rgb { unsigned char r, g, b; };
float scale = 1.5;//0 to 100!
// open input and output file
FILE* inptr = fopen("in.bmp", "rb");
FILE* outptr = fopen("out.bmp","wb");
BITMAPFILEHEADER bf; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
BITMAPINFOHEADER bi; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// calculate old padding
int pad = (4 - (bi.biWidth * 3) % 4) % 4;
//create new header files
BITMAPINFOHEADER bi2 = bi;
BITMAPFILEHEADER bf2 = bf;
//image old
int width = bi.biWidth;
int height = bi.biHeight;
//image new
int width2 = ceil(bi.biWidth * scale);
int height2 = ceil(bi.biHeight *scale);
//new header
bi2.biWidth = width2;
bi2.biHeight = height2;
int pad2 = (4 - (width2 * 3) % 4) % 4;
bi2.biSizeImage = (width2 * 3 + pad2) * height2;
bf2.bfSize = bi2.biSizeImage + 54;
//header save
fwrite(&bf2, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bi2, sizeof(BITMAPINFOHEADER), 1, outptr);
rgb *p = (rgb*)malloc(width*height* sizeof(rgb)); //image old
rgb *p2 = (rgb*)malloc(width2*height2* sizeof(rgb)); //image2 new
//write p
int say = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
fread(p + say, 3, 1, inptr);
say++;
}
fseek(inptr, pad, SEEK_CUR);
}
//write p---->p2
int say2 = 0;
for (int i = 0; i < height2; i++)
{
for (int j = 0; j < width2; j++)
{
int x = j / scale ;
int y = i / scale ;
int say = y * width + x;
memcpy(p2+say2, p + say, sizeof(rgb));
say2++;
}
}
//write file
say = 0;
for (int i = 0; i < height2; i++)
{
for (int j = 0; j < width2; j++)
{
fwrite(p2+say, sizeof(rgb), 1, outptr);
say++;
}
for (int h = 0; h < pad2; h++)
fputc(0x00, outptr);
}
fclose(inptr);
fclose(outptr);
return 0;
}
我编写了一个程序,可以将位图的大小调整为 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 发布了以下解决方案:
用于跳过扫描线的 fseek 不完整。 bi.biWidth 需要乘以 sizeof(RGBTRIPLE)
使用 fseek(inptr, ((bi.biWidth * sizeof(RGBTRIPLE) + padding), SEEK_CUR); 而不是 fseek(inptr, (bi.biWidth + padding), SEEK_CUR);
该程序现在可以通过此修复来放大和缩小图像。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
int main()
{
struct rgb { unsigned char r, g, b; };
float scale = 1.5;//0 to 100!
// open input and output file
FILE* inptr = fopen("in.bmp", "rb");
FILE* outptr = fopen("out.bmp","wb");
BITMAPFILEHEADER bf; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
BITMAPINFOHEADER bi; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// calculate old padding
int pad = (4 - (bi.biWidth * 3) % 4) % 4;
//create new header files
BITMAPINFOHEADER bi2 = bi;
BITMAPFILEHEADER bf2 = bf;
//image old
int width = bi.biWidth;
int height = bi.biHeight;
//image new
int width2 = ceil(bi.biWidth * scale);
int height2 = ceil(bi.biHeight *scale);
//new header
bi2.biWidth = width2;
bi2.biHeight = height2;
int pad2 = (4 - (width2 * 3) % 4) % 4;
bi2.biSizeImage = (width2 * 3 + pad2) * height2;
bf2.bfSize = bi2.biSizeImage + 54;
//header save
fwrite(&bf2, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bi2, sizeof(BITMAPINFOHEADER), 1, outptr);
rgb *p = (rgb*)malloc(width*height* sizeof(rgb)); //image old
rgb *p2 = (rgb*)malloc(width2*height2* sizeof(rgb)); //image2 new
//write p
int say = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
fread(p + say, 3, 1, inptr);
say++;
}
fseek(inptr, pad, SEEK_CUR);
}
//write p---->p2
int say2 = 0;
for (int i = 0; i < height2; i++)
{
for (int j = 0; j < width2; j++)
{
int x = j / scale ;
int y = i / scale ;
int say = y * width + x;
memcpy(p2+say2, p + say, sizeof(rgb));
say2++;
}
}
//write file
say = 0;
for (int i = 0; i < height2; i++)
{
for (int j = 0; j < width2; j++)
{
fwrite(p2+say, sizeof(rgb), 1, outptr);
say++;
}
for (int h = 0; h < pad2; h++)
fputc(0x00, outptr);
}
fclose(inptr);
fclose(outptr);
return 0;
}