bmp.cpp
来自「这是一个用c++编写的实现指纹识别的程序」· C++ 代码 · 共 253 行
CPP
253 行
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "thin_image.h"
int nCol,nVS;
void readBmp(char* bmpFile)
{
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);
int count = 0;
for(r=0; r<=originalImage.rows - 1; r++)
{
for(c=0; c<=originalImage.cols - 1; c++)
{
count++;
fread(pChar, sizeof(char), 1, bmpInput);
image->p[r][c] = int(*pChar)/255;
// inverting the image
if(image->p[r][c]==1)
{
image->p[r][c]=0;
}
else
{
image->p[r][c]=1;
}
fprintf(rasterOutput, "(%d, %d) = %d\n", r, c, int(*pChar)/255);
}
}
fclose(bmpInput);
fclose(rasterOutput);
}
void writeBmp(char* bmpFile)
{
FILE *bmpOutput, *rasterOutput, *oldFile;
sImage originalImage;
unsigned char someChar;
unsigned char* pChar;
int nColors;
long fileSize;
int vectorSize, r, c;
oldFile = fopen("output.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;
//image->i = new Pixel[fileSize];
//image->p = new Pixel*[fileSize];
// write the header
// 54 + 4*nColors
// char ch[2];
// for( int i=0; i< 54 + 4* nColors; i++ ){/
// fread(ch, sizeof(char), 1, oldFile);
// fwrite(ch, sizeof(char), 1, bmpOutput);
// }
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);
//fprintf(rasterOutput, "(%d, %d) = %d\n", r, c, int(*pChar)/255);
}
}
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 */
/*----------SET IMAGE INFO SUBPROGRAM--------------*/
void setImageInfo(FILE* outputFile, long offset, int numberOfChars,long value)
{
unsigned char *ptrC;
//long value = 0L;
unsigned char dummy;
int i;
dummy = '0';
ptrC = &dummy;
fseek(outputFile, offset, SEEK_SET);
for(i=1; i<=numberOfChars; i++)
{
printf("%d\n",value);
fwrite( &(((char *)(&value))[i]), sizeof(char),1, outputFile );
/* calculate value based on adding bytes */
//value = (long)(value + (*ptrC)*(pow(256, (i-1))));
//*ptrC = (unsigned char) value/pow(256,(i-1));
//value = fmod(value,pow(256,(i-1)) );
//fwrite(ptrC, sizeof(char), 1, outputFile);
}
//return(value);
} /* end of setImageInfo */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?