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

时间:2017-04-06 02:08:35

标签: c bitmap bmp bitmapimage

我遇到的问题是我的位图生成器的输出非常偏斜。如您所见,电流发生器使用一个24位乘24位数组作为输入,将其扩展到600位乘600位,并在缩放图像周围生成位图。我认为问题出在缩放或pheader文件中,但是我找不到前者的问题,而且我对BMP格式不太熟悉,不知道如何修复后者。

我的代码中的问题出在哪里,我该如何解决?您可以在代码下方看到生成的位图的屏幕截图(我无法将.bmp文件本身成功上传到图像主机)。生成的图像为3x3棋盘图案。作为旁注,我也无法在我的Ubuntu VM之外的系统上打开生成的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);
    }
  }
}

Screenshot of BMP

0 个答案:

没有答案