bmp/zoom.cpp

100 lines
2.8 KiB
C++

#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char ** argv)
{
FILE *fp = fopen("C:\\Users\\leong\\Desktop\\bmp_exam\\input\\hello.bmp","rb");
if (fp == 0)
return 0;
BITMAPFILEHEADER fileHead;
fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);
BITMAPINFOHEADER infoHead;
fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp);
int width = infoHead.biWidth;
int height = infoHead.biHeight;
int biCount = infoHead.biBitCount;
RGBQUAD *pColorTable;
int pColorTableSize = 0;
pColorTable = new RGBQUAD[256];
fread(pColorTable, sizeof(RGBQUAD), 256, fp);
pColorTableSize = 1024;
unsigned char *pBmpBuf;
int lineByte = width*floor(biCount/8);
while(lineByte % 4 != 0){lineByte += 1;}
pBmpBuf = new unsigned char[lineByte*height];
fread(pBmpBuf, lineByte*height, 1, fp);
fclose(fp);
printf("倍:");
// float lx, ly;
// scanf("%f%f", &lx, &ly);
// int dstWidth = round(double(lx*width));
// int dstHeight = round(double(ly*height));
float k;
scanf("%f", &k);
int dstWidth = round(double(k*width));
int dstHeight = round(double(k*height));
int lineByte2 = dstWidth*floor(biCount/8);
while(lineByte2 % 4 != 0){lineByte2 += 1;}
unsigned char*pBmpBuf2;
pBmpBuf2 = new unsigned char[lineByte2*dstHeight];
for (int i = 0; i < dstHeight; ++i){
for (int j = 0; j < dstWidth; ++j){
unsigned char *p;
p = (unsigned char *)(pBmpBuf2 + lineByte2*i + j);
(*p) = 255;
}
}
int x = 0;
int y = 0;
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
unsigned char *p1, *p2;
// x = round(double(lx*j));
// y = round(double(ly*i));
x = round(double(k*j));
y = round(double(k*i));
p1 = (unsigned char *)(pBmpBuf + i*lineByte + j);
p2 = (unsigned char *)(pBmpBuf2 + y*lineByte2 + x);
(*p2) = (*p1);
}
}
FILE *fpo = fopen("C:\\Users\\leong\\Desktop\\bmp_exam\\output\\output.bmp", "wb");
if (fpo == 0)
return 0;
BITMAPFILEHEADER dstFileHead;
dstFileHead.bfOffBits = 14 + 40 + pColorTableSize;
dstFileHead.bfReserved1 = 0;
dstFileHead.bfReserved2 = 0;
dstFileHead.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+pColorTableSize + lineByte2*dstHeight;
dstFileHead.bfType = 0x4D42;
fwrite(&dstFileHead, sizeof(dstFileHead), 1, fpo);
BITMAPINFOHEADER dstInfoHead;
dstInfoHead.biBitCount = biCount;
dstInfoHead.biClrImportant = 0;
dstInfoHead.biClrUsed = 0;
dstInfoHead.biCompression = 0;
dstInfoHead.biHeight = dstHeight;
dstInfoHead.biPlanes = 1;
dstInfoHead.biSize = 40;
dstInfoHead.biSizeImage = lineByte2*dstHeight;
dstInfoHead.biWidth = dstWidth;
dstInfoHead.biXPelsPerMeter = 0;
dstInfoHead.biYPelsPerMeter = 0;
fwrite(&dstInfoHead, sizeof(BITMAPINFOHEADER), 1, fpo);
fwrite(pColorTable, sizeof(RGBQUAD), 256, fpo);
fwrite(pBmpBuf2, lineByte2*dstHeight, 1, fp);
fclose(fpo);
system("pause");
return 0;
}