⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmp.cpp

📁 这是一个用c++编写的实现指纹识别的程序
💻 CPP
字号:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bmp.h"

int	nCol,nVS;

void readBmp(char* bmpFile,Image *image)
{
  FILE			*bmpInput, *rasterOutput;
  sImage		originalImage;
  unsigned char		someChar;
  unsigned char*	pChar;
  int			nColors;
  long			fileSize;
  int			vectorSize, r, c;


  /* initialize pointer */
  someChar = '0';
  pChar = &someChar;

  printf("Reading filename %s\n", bmpFile);

  /*--------READ INPUT FILE------------*/
  bmpInput = fopen(bmpFile, "rb");
  fseek(bmpInput, 0L, SEEK_END);
  //rasterOutput = fopen("data.txt","w");

  /*--------GET BMP DATA---------------*/
  originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
  originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
  fileSize = getImageInfo(bmpInput, 2, 4);
  nCol = nColors = getImageInfo(bmpInput, 46, 4);
  nVS = vectorSize = fileSize - (14 + 40 + 4*nColors);
  

  /*-------PRINT TO SCREEN-------------*/
  printf("Width: %d\n", originalImage.cols);
  printf("Height: %d\n", originalImage.rows);
  printf("File size: %ld\n", fileSize);
  printf("# Colors: %d\n", nColors);
  printf("Vector size: %d\n", vectorSize);

  image->Hres = originalImage.cols;
  image->Vres = originalImage.rows;
  image->Size = fileSize;
  image->i = new Pixel[fileSize];
  image->p = new Pixel*[fileSize];

  

  for(int i=0;i<image->Hres;i++)
  image->p[i] = new Pixel[image->Vres];

  printf("%d \n",image->Hres);
  

  /*----------READ RASTER DATA---------*/
  fseek(bmpInput, (54 + 4*nColors), SEEK_SET);

  for(r=0; r<=originalImage.rows - 1; r++)
  {
    for(c=0; c<=originalImage.cols - 1; c++)
    {
		fread(pChar, sizeof(char), 1, bmpInput);
		image->p[r][c] = int(*pChar)/255;
		//fprintf(rasterOutput, "(%d, %d) = %d\n", r, c, int(*pChar)/255);
    }
  }

  fclose(bmpInput);
  //fclose(rasterOutput);
}


void writeBmp(char* bmpFile,Image *image)
{
  FILE			*bmpOutput, *rasterOutput, *oldFile;
  sImage		originalImage;
  unsigned char		someChar;
  unsigned char*	pChar;
  int			nColors;
  long			fileSize;
  int			vectorSize, r, c;


  oldFile = fopen("output_t.bmp","rb");
  /* initialize pointer */
  someChar = '0';
  pChar = &someChar;

  printf("Writing filename %s\n", bmpFile);

  /*--------write INPUT FILE------------*/
  bmpOutput = fopen(bmpFile, "wb");
  fseek(bmpOutput, 0L, SEEK_END);
  rasterOutput = fopen("data.txt","w");

  originalImage.cols = image->Hres;
  originalImage.rows = image->Vres;
  fileSize = image->Size;
  nColors = nCol;
  vectorSize = nVS;

  copyImageInfo(oldFile,bmpOutput);
  copyColorTable(oldFile,bmpOutput,nColors);


  /*-------PRINT TO SCREEN-------------*/
  printf("Width: %d\n", originalImage.cols);
  printf("Height: %d\n", originalImage.rows);
  printf("File size: %ld\n", fileSize);
  printf("# Colors: %d\n", nColors);


  /*----------Write RASTER DATA---------*/
  fseek(bmpOutput, (54 + 4*nColors), SEEK_SET);

  for(r=0; r<=originalImage.rows - 1; r++)
  {
    for(c=0; c<=originalImage.cols - 1; c++)
    {
		*pChar = (unsigned char)image->p[r][c]*255;
		fwrite(pChar, sizeof(char), 1, bmpOutput);
    }
  }

  fclose(bmpOutput);
  fclose(rasterOutput);
}


/*-------------COPIES HEADER AND INFO HEADER----------------*/
void copyImageInfo(FILE* inputFile, FILE* outputFile)
{
  unsigned char* ptrC;
  unsigned char dummy;
  int i;

  dummy = '0';
  ptrC = &dummy;

  fseek(inputFile, 0L, SEEK_SET);
  fseek(outputFile, 0L, SEEK_SET);

  for(i=0; i<=50; i++)
  {
    fread(ptrC, sizeof(char), 1, inputFile);
    fwrite(ptrC, sizeof(char), 1, outputFile);
  }

}



/*----------------COPIES COLOR TABLE-----------------------------*/
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
{
  unsigned char* ptrC;
  unsigned char dummy;
  int i;

  dummy = '0';
  ptrC = &dummy;

  fseek(inputFile, 54L, SEEK_SET);
  fseek(outputFile, 54L, SEEK_SET);

  for(i=0; i<=(4*nColors); i++)  /* there are (4*nColors) bytesin color table */
  {
    fread(ptrC, sizeof(char), 1, inputFile); 
    fwrite(ptrC, sizeof(char), 1, outputFile);
  }

}

/*----------GET IMAGE INFO SUBPROGRAM--------------*/
long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{
  unsigned char			*ptrC;
  long				value = 0L;
  unsigned char			dummy;
  int				i;

  dummy = '0';
  ptrC = &dummy;

  fseek(inputFile, offset, SEEK_SET);

  for(i=1; i<=numberOfChars; i++)
  {
    fread(ptrC, sizeof(char), 1, inputFile);
    /* calculate value based on adding bytes */
    value = (long)(value + (*ptrC)*(pow(256, (i-1))));
  }
  return(value);

} /* end of getImageInfo */



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -