C语言实现bmp图片裁剪
bmp图片如何储存的,在这篇文章中有很好的介绍:https://blog.csdn.net/weixin_46987028/article/details/109171867
附上两张用于测试的图(从别的博主那盗来的,来源见水印)
8位深 256*256图片:
24位深 512*512图片:
以下代码支持将24位深或8位深的图片裁剪为矩形
#include<stdio.h>
#include<windows.h>
//需要的参数有原图片路径,输出图片路径,裁剪起点坐标,裁剪后的宽度和高度
//裁剪起点为矩形的左下角像素坐标(begin_x,begin_y),根据给定的宽度和高度向右上方拓展
void cutting(char *source, char *des, int begin_x, int begin_y, int width, int height);
//main函数仅用于展示功能的必要输入,毫无意义
int main(void){
int begin_x, begin_y, height, width;
char *source, *destination;
source = (char *)malloc(sizeof(char) * 100);
destination = (char *)malloc(sizeof(char) * 100);
printf("input the datapath of input file:");
scanf("%s", source);
printf("output the file as:");
scanf("%s", destination);
printf("intput the start point (x,y) and the width and height after cutting\n");
printf("x:");
scanf("%d", &begin_x);
printf("y:");
scanf("%d", &begin_y);
printf("width:");
scanf("%d", &width);
printf("height:");
scanf("%d", &height);
cutting(source, destination, begin_x, begin_y, width, height);
return 0;
}
void cutting(char *source, char *des, int begin_x, int begin_y, int width, int height){
int bmpHeight;
int bmpWidth;
int biBitCount;
unsigned char* pBmpBuf;
unsigned char* pBmpBuf2;
RGBQUAD* pColorTable;
FILE* fp = fopen(source, "rb");//打开源文件
if (fp == 0){
printf("Wrong path!\n");
return;
}
BITMAPFILEHEADER fileHead;
fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);//读取BITMAPFILEHEADER
BITMAPINFOHEADER head;
fread(&head, sizeof(BITMAPINFOHEADER), 1, fp);//读取BITMAPINFOHEADER
bmpHeight = head.biHeight;//获取原图的长和宽
bmpWidth = head.biWidth;
if(begin_x+width>bmpWidth){//如果裁剪后的右上角在原图右上角之外,将其移回原图右上角
width = bmpWidth - begin_x;
}
if(begin_y+height>bmpHeight){
height = bmpHeight - begin_y;
}
biBitCount = head.biBitCount; // 获取位深度
if (biBitCount < 24) {//如果位深度小于24,还要读取ColorTable
pColorTable = new RGBQUAD[2 << biBitCount];
fread(pColorTable, sizeof(RGBQUAD), 2 << biBitCount, fp);
}
int LineByte = (bmpWidth * biBitCount / 8 + 3) / 4 * 4;//计算出每行有多少个byte,/8是因为8个bits等于一个byte,+3)/4*4是为了对齐(每行的byte必须是4的整数倍)
pBmpBuf = new unsigned char[LineByte * bmpHeight];
fread(pBmpBuf, LineByte * bmpHeight, 1, fp);
FILE* fop = fopen(des, "wb");
head.biHeight = height;//重写高度
head.biWidth = width;//重写宽度
int LineByte2 = (head.biWidth * biBitCount / 8 + 3) / 4 * 4;对齐
pBmpBuf2 = new unsigned char[LineByte * head.biHeight];
head.biSizeImage = LineByte2 * head.biHeight;//重写图片数据大小
fileHead.bfSize = head.biSizeImage + fileHead.bfOffBits;//重写总大小(包括了BITMAPFILEHEADER、BITMAPINFOHEADER、ColorTable)
fwrite(&fileHead, sizeof(BITMAPFILEHEADER), 1, fop);//写到文件中
fwrite(&head, sizeof(BITMAPINFOHEADER), 1, fop);
if(biBitCount<24){//如果有ColorTable,也写到文件中
fwrite(pColorTable, sizeof(RGBQUAD), 256, fop);
}
if(biBitCount==24){//如果是24位深的图片,每个像素有3个byte(分别代表RGB)
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
pBmpBuf2[i * width * 3 + j * 3] = pBmpBuf[(begin_y + i) * bmpWidth * 3 + begin_x * 3 + j * 3];
pBmpBuf2[i * width * 3 + j * 3 + 1] = pBmpBuf[(begin_y + i) * bmpWidth * 3 + begin_x * 3 + j * 3 + 1];
pBmpBuf2[i * width * 3 + j * 3 + 2] = pBmpBuf[(begin_y + i) * bmpWidth * 3 + begin_x * 3 + j * 3 + 2];
}
}
}
else if(biBitCount==8){//如果是8位深图片,每个像素只占一个byte
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
pBmpBuf2[i * width + j] = pBmpBuf[(begin_y + i) * bmpWidth + begin_x + j];
}
}
}
else{
printf("Sorry we don't support photo with biBitCount less than 8!\n");
return;
}
fwrite(pBmpBuf2, LineByte2 * head.biHeight, 1, fop);//写回到文件中
fclose(fop);
return ;
}