在C中生成的位图文件是倾斜的

在C中生成的位图文件是倾斜的,c,bitmap,bmp,bitmapimage,C,Bitmap,Bmp,Bitmapimage,我有一个问题,我的位图生成器的输出是非常倾斜。如您所见,当前生成器使用恒定的24位乘24位数组作为输入,将其放大到600位乘600位,并围绕缩放后的图像生成位图。我认为问题在于缩放或pheader文件,但我找不到前者的问题,而且我对BMP格式不够熟悉,不知道如何修复后者 在我的代码中,问题在哪里?如何解决它?您可以在代码下面看到生成位图的屏幕截图(我无法成功地将.bmp文件本身上载到图像主机)。生成的图像应为3x3棋盘图案。顺便说一句,除了我的Ubuntu虚拟机之外,我在打开生成的BMP时也遇到

我有一个问题,我的位图生成器的输出是非常倾斜。如您所见,当前生成器使用恒定的24位乘24位数组作为输入,将其放大到600位乘600位,并围绕缩放后的图像生成位图。我认为问题在于缩放或pheader文件,但我找不到前者的问题,而且我对BMP格式不够熟悉,不知道如何修复后者

在我的代码中,问题在哪里?如何解决它?您可以在代码下面看到生成位图的屏幕截图(我无法成功地将.bmp文件本身上载到图像主机)。生成的图像应为3x3棋盘图案。顺便说一句,除了我的Ubuntu虚拟机之外,我在打开生成的BMP时也遇到了问题

提前感谢您的帮助

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

#pragma pack (1)

#define _WIDTH 600
#define _HEIGHT 600
#define _BITCOUNT 1
#define _PPM 2835
#define _SLICE_WIDTH 24
#define _SLICE_HEIGHT 24
#define _SEQUENCE_LENGTH 20

void slicecpy(uint8_t* dst, uint8_t* src, int offset);
void scale(uint8_t* dst, uint8_t* src);

typedef struct {
  uint16_t signature;
  uint32_t fileSize;
  uint32_t reserved;
  uint32_t DataOffset;
} FileHeader;

typedef struct {
  uint32_t size;
  uint32_t width,height;
  uint16_t planes;
  uint16_t bitCount;
  uint32_t compression;
  uint32_t imageSize;
  uint32_t xppm,yppm;
  uint32_t colorsUsed;
  uint32_t colorsImportant;
} InfoHeader;

int main(void){
  FILE *fp;

  uint8_t* slices = calloc((_SLICE_WIDTH*_SLICE_HEIGHT)/8,sizeof(uint8_t));

  uint8_t *pixel_buf = calloc((_HEIGHT*_WIDTH*_BITCOUNT)/8,sizeof(uint8_t));
  uint8_t bram[]={0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0xff,0x00,0xff,
          0xff,0x00,0xff,
          0xff,0x00,0xff,
          0xff,0x00,0xff,
          0xff,0x00,0xff,
          0xff,0x00,0xff,
          0xff,0x00,0xff,
          0xff,0x00,0xff,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00,
          0x00,0xff,0x00};

  FileHeader *fh = (FileHeader*)calloc(1,sizeof(FileHeader));
  InfoHeader *ih = (InfoHeader*)calloc(1,sizeof(InfoHeader));

  char i=0;
  char filename[12];
  uint32_t *bl = malloc(sizeof(uint32_t));
  uint32_t *wh = malloc(sizeof(uint32_t));

  *bl=0x00000000;
  *wh=0xffffffff;

  // Set File Header Values
  fh->signature = 19778;
  fh->fileSize = sizeof(FileHeader)+sizeof(InfoHeader)+2*sizeof(uint32_t) + (_WIDTH*_HEIGHT)/8;
  fh->reserved = 0;
  fh->DataOffset = sizeof(FileHeader)+sizeof(InfoHeader)+2*sizeof(uint32_t);

  // Set Bitmap Header Values
  ih->size  = 40;
  ih->width     = _WIDTH;
  ih->height    = _HEIGHT;
  ih->planes    = 1;
  ih->bitCount  = _BITCOUNT;
  ih->compression = 0;
  ih->imageSize = (_WIDTH*_HEIGHT*_BITCOUNT)/8;
  ih->xppm = _PPM;
  ih->yppm = _PPM;
  ih->colorsUsed = (_BITCOUNT==1) ? 2 : 0;
  ih->colorsImportant = 0;

  slicecpy(slices,bram,0);
  scale(pixel_buf, slices);
  sprintf(filename,"image_%02d.bmp",i);
  fp=fopen(filename,"wb");

  fwrite(fh,14,1,fp);
  fwrite(ih,40,1,fp);
  //only need color table in monochrome one bit mode
  if(_BITCOUNT==1) fwrite(bl,sizeof(uint32_t),1,fp);
  if(_BITCOUNT==1) fwrite(wh,sizeof(uint32_t),1,fp);
  fwrite(pixel_buf,(_HEIGHT*_WIDTH*_BITCOUNT)/8,1,fp);

  fclose(fp);

  free(fh);
  free(ih);
}

void slicecpy(uint8_t* dst, uint8_t* src, int offset){
  int i;
  for(i=0;i<(_SLICE_HEIGHT*_SLICE_WIDTH)/8;i++){
    *(dst+i) = *(src+i+offset);
  }
}

void scale(uint8_t* dst, uint8_t* src){
  int i,j;
  for(j=0;j<_WIDTH;j++){
    for(i=0;i<_HEIGHT;i++){
      dst[((i>>3)+j*(_WIDTH/8))] |=
    (src[((i/(_HEIGHT/_SLICE_HEIGHT))>>3) + 
    (j/(_WIDTH/_SLICE_WIDTH))*(24/8)] >> (((i/(_HEIGHT/_SLICE_HEIGHT))%8)&1))<<(i%8);
    }
  }
}
#包括
#包括
#包括
#布拉格语包(1)
#定义_宽度600
#定义高度600
#定义位计数1
#定义_PPM2835
#定义切片宽度24
#定义切片高度24
#定义_序列_长度20
无效切片cpy(uint8_t*dst,uint8_t*src,int offset);
空隙率(uint8*dst、uint8*src);
类型定义结构{
uint16\t签名;
uint32_t文件大小;
uint32保留;
uint32_t数据偏移量;
}文件头;
类型定义结构{
uint32_t尺寸;
uint32_t宽度、高度;
uint16_t平面;
uint16_t比特计数;
uint32_t压缩;
uint32_t图像大小;
uint32_t xppm,yppm;
使用的uint32_t颜色;
uint32_t颜色很重要;
}信息头;
内部主(空){
文件*fp;
uint8_t*slices=calloc((_SLICE_WIDTH*_SLICE_HEIGHT)/8,sizeof(uint8_t));
uint8_t*pixel_buf=calloc((_HEIGHT*_WIDTH*_BITCOUNT)/8,sizeof(uint8_t));
uint8_t bram[]={0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0xff,0x00,0xff,
0xff,0x00,0xff,
0xff,0x00,0xff,
0xff,0x00,0xff,
0xff,0x00,0xff,
0xff,0x00,0xff,
0xff,0x00,0xff,
0xff,0x00,0xff,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00,
0x00,0xff,0x00};
FileHeader*fh=(FileHeader*)calloc(1,sizeof(FileHeader));
InfoHeader*ih=(InfoHeader*)calloc(1,sizeof(InfoHeader));
字符i=0;
字符文件名[12];
uint32_t*bl=malloc(sizeof(uint32_t));
uint32_t*wh=malloc(sizeof(uint32_t));
*bl=0x00000000;
*wh=0xFFFFFF;
//设置文件头值
fh->签名=19778;
fh->fileSize=sizeof(FileHeader)+sizeof(InfoHeader)+2*sizeof(uint32\t)+(宽度*\高度)/8;
fh->保留=0;
fh->DataOffset=sizeof(FileHeader)+sizeof(InfoHeader)+2*sizeof(uint32\t);
//设置位图标题值
ih->size=40;
ih->宽度=_宽度;
ih->高度=_高度;
ih->平面=1;
ih->bitCount=\u bitCount;
ih->压缩=0;
ih->imageSize=(\u宽度*\u高度*\u比特数)/8;
ih->xppm=_PPM;
ih->yppm=_PPM;
ih->colorsUsed=(\u比特数==1)?2:0;
ih->colorsImportant=0;
切片cpy(切片,bram,0);
比例(像素单位,切片);
sprintf(文件名为“image_u%02d.bmp”,i);
fp=fopen(文件名,“wb”);
fwrite(fh,14,1,fp);
fwrite(ih,40,1,fp);
//仅需要单色一位模式下的颜色表
如果(_BITCOUNT==1)fwrite(bl,sizeof(uint32_t),1,fp);
如果(_BITCOUNT==1)fwrite(wh,sizeof(uint32_t),1,fp);
fwrite(像素buf,(\u高度*\u宽度*\u比特数)/8,1,fp);
fclose(fp);
免费(fh);
免费(ih);
}
无效切片cpy(uint8_t*dst、uint8_t*src、int偏移量){
int i;
对于(i=0;i>3)+

(j/(u-WIDTH/_-SLICE\u-WIDTH))*(24/8)]>>((i/(u-HEIGHT/(u-SLICE\u-HEIGHT)]%8和1))每行像素的大小需要向上舍入到下一个32位的倍数…修改标头时,我是只是更改文件标头,还是新的填充也应该反映在BMP数据标头的x和y大小字段中?此外,是否所有位深度都需要填充?我只在一位中工作所有位d都需要填充epths,是的。在标题中,高度和宽度不包含填充,但图像大小包含填充。我将_-width和_-height设置为576(9*64)。输出在windows上可读并具有正确的模式。当我设置为608(19*32)时,仍然存在损坏。