热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

调整位图文件的大小部分有效可以放大但不能缩小

我编写了一个程序,将位图的大小调整为0-100之间的值(小于1的值将缩小图像)

我编写了一个程序,将位图的大小调整为0-100之间的值(小于1的值将缩小图像)。该程序可以放大文件,但无法缩小文件。

我认为我可能不正确地使用fseek来跳过像素和行。

感谢您随时致力于解决此问题。

请注意,代码很丑陋,因为我只是在学习编码。欢迎提出任何建议,但可能会引起其他混乱,因此请解释您对5岁儿童的回答。

您可以使用类似于“ ./resize .5 large.bmp test.bmp”的命令运行程序

两个程序文件都附有resize.c和bmp.h

我还附上了之前和之后的图片,以更好地了解问题所在。

之后:

调整位图文件的大小部分有效-可以放大但不能缩小

之前:

调整位图文件的大小部分有效-可以放大但不能缩小

// Copies a BMP file
#include
#include
#include
#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),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,outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&newbi,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 {
printf("H%i ",i);
for (int j = 0; j {
printf("W%i ",j);
if (num0 > .5 && num0 <1)
fread(triple1,sizeof(RGBTRIPLE),factor,inptr);
else
fread(&triple,inptr);
//store new triple as new scanline
for (int m = 0; 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 {
if (num0 > .5 && num0 <1)
{
fseek(inptr,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 {
printf("F%i ",k);
fwrite(sline,(newbi.biWidth * 3),outptr);
// add padding if any
for (int h = 0; h {
fputc(0x00,outptr);
}
}
//skip scanline(s) if shrinking
for (int o = 0; 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 {
zoom1 = b/a;
}
else if (b <= 0)
{
zoom1 = 1;
}
else
{
zoom1 = 1;
}
return zoom1;
}
// BMP-related data types based on microsoft's own
#include
// 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);

该程序现在可以使用此修复程序来放大和缩小图像。

,

#include
#include
#include
#include
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),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,outptr);
fwrite(&bi2,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 {
for (int j = 0; j {
fread(p + say,3,inptr);
say++;
}
fseek(inptr,pad,SEEK_CUR);
}
//write p---->p2
int say2 = 0;
for (int i = 0; i {
for (int j = 0; 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 {
for (int j = 0; j {
fwrite(p2+say,sizeof(rgb),outptr);
say++;
}
for (int h = 0; h fputc(0x00,outptr);
}
fclose(inptr);
fclose(outptr);
return 0;
}

推荐阅读
author-avatar
diuhunpo_813
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有