1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include <Windows.h> #include <stdio.h> #include <stdlib.h> unsigned char *pBmpBuf; int bmpWidth; int bmpHeight; RGBQUAD *pColorTable; int biBitCount; char readPath1[] = "D:/project/devcpp/img/input1.BMP"; char readPath2[]="D:/project/devcpp/img/input2.BMP"; char writePath[]="D:/project/devcpp/img/output.BMP";
int readBmp(char *bmpName){ FILE *fp=fopen(bmpName,"rb"); if(fp==0){ printf("打水印失败\n"); return 0; } fseek(fp, sizeof(BITMAPFILEHEADER),0); BITMAPINFOHEADER head; fread(&head, sizeof(BITMAPINFOHEADER), 1,fp); bmpWidth = head.biWidth; bmpHeight = head.biHeight; biBitCount = head.biBitCount; int lineByte=(bmpWidth * biBitCount/8+3)/4*4; if(biBitCount==8){ pColorTable=malloc(sizeof(RGBQUAD)*256); fread(pColorTable,sizeof(RGBQUAD),256,fp); } pBmpBuf=malloc(sizeof(unsigned char)*lineByte * bmpHeight); fread(pBmpBuf,1,lineByte * bmpHeight,fp); fclose(fp); return 1; }
int saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height, int biBitCount, RGBQUAD *pColorTable){ if(!imgBuf) return 0; int colorTablesize=0; if(biBitCount==8) colorTablesize=1024; int lineByte=(width * biBitCount/8+3)/4*4; FILE *fp=fopen(bmpName,"wb"); if(fp==0){ printf("!!!!\n"); return 0; } BITMAPFILEHEADER fileHead; fileHead.bfType = 0x4D42; fileHead.bfSize= sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+colorTablesize+lineByte*height; fileHead.bfReserved1 = 0; fileHead.bfReserved2 = 0; fileHead.bfOffBits=54+colorTablesize; fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp); BITMAPINFOHEADER head; head.biBitCount=biBitCount; head.biClrImportant=0; head.biClrUsed=0; head.biCompression=0; head.biHeight=height; head.biPlanes=1; head.biSize=40; head.biSizeImage=lineByte*height; head.biWidth=width; head.biXPelsPerMeter=0; head.biYPelsPerMeter=0; fwrite(&head, sizeof(BITMAPINFOHEADER),1, fp); if(biBitCount==8) fwrite(pColorTable, sizeof(RGBQUAD),256, fp); fwrite(imgBuf, height*lineByte, 1, fp); fclose(fp); return 1; } int main(){ int o=0; printf("BMP文件录入位置:\n%s\n%s\n\n",readPath1,readPath2); readBmp(readPath1); unsigned char *pBmpBuf1=pBmpBuf;; int bmpWidth1=bmpWidth; int bmpHeight1=bmpHeight; RGBQUAD *pColorTable1=pColorTable; int biBitCount1=biBitCount; readBmp(readPath2); unsigned char *pBmpBuf2=pBmpBuf;; int bmpWidth2=bmpWidth; int bmpHeight2=bmpHeight; RGBQUAD *pColorTable2=pColorTable; int biBitCount2=biBitCount; printf("第一张BMP:%d位 第二张BMP:%d位\n",biBitCount1,biBitCount2); int i,j; int lineByte1=(bmpWidth1*biBitCount1/8+3)/4*4; int lineByte2=(bmpWidth2*biBitCount2/8+3)/4*4; int k; if(biBitCount1==8 && biBitCount2==8){ for(i=0;i<bmpHeight2;i++){ for(j=0;j<bmpWidth2;j++){ if(*(pBmpBuf2+i*lineByte2+j)!=0){ *(pBmpBuf1+i*lineByte1+j)=*(pBmpBuf2+i*lineByte2+j); } } } } else if(biBitCount1==24 && biBitCount2==24){ for(i=0;i<bmpHeight2;i++){ for(j=0;j<bmpWidth2;j++){ for(k=0,o=0;k<3;k++){ if(*(pBmpBuf2+i*lineByte2+j*3+k)>5){ o++; } if(o==3){ *(pBmpBuf1+i*lineByte1+j*3+0)=*(pBmpBuf2+i*lineByte2+j*3+0); *(pBmpBuf1+i*lineByte1+j*3+1)=*(pBmpBuf2+i*lineByte2+j*3+1); *(pBmpBuf1+i*lineByte1+j*3+2)=*(pBmpBuf2+i*lineByte2+j*3+2); } } } } } if(saveBmp(writePath, pBmpBuf1, bmpWidth1, bmpHeight1, biBitCount1, pColorTable1)){ printf("打水印成功\n\n"); printf("BMP文件保存位置:\n%s\n",writePath); } else{ printf("打水印失败"); } free(pBmpBuf1); free(pBmpBuf2); if(biBitCount1==8) free(pColorTable1); if(biBitCount2==8) free(pColorTable2); return 0; }
|