|
发表于 2006-12-27 14:42:04
|
显示全部楼层
每隔一个像素进行反色而已
每隔一个像素进行反色而已
- /*
- File: magicpic.c
- Name: 让图片在浏览器中只有被选中后才能看清
- Author: zyl910
- Blog: [url]http://blog.csdn.net/zyl910/[/url]
- Version: V1.0
- Updata: 2006-12-27
- */
- #include <stdio.h>
- #include <conio.h>
- #include <dos.h>
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- typedef unsigned long DWORD;
- typedef BYTE _far *LPBYTE;
- typedef WORD _far *LPWORD;
- typedef DWORD _far *LPDWORD;
- typedef long LONG;
- typedef struct tagBITMAPFILEHEADER { // bmfh
- WORD bfType;
- DWORD bfSize;
- WORD bfReserved1;
- WORD bfReserved2;
- DWORD bfOffBits;
- } BITMAPFILEHEADER;
- typedef struct tagBITMAPINFOHEADER{ // bmih
- DWORD biSize;
- LONG biWidth;
- LONG biHeight;
- WORD biPlanes;
- WORD biBitCount;
- DWORD biCompression;
- DWORD biSizeImage;
- LONG biXPelsPerMeter;
- LONG biYPelsPerMeter;
- DWORD biClrUsed;
- DWORD biClrImportant;
- } BITMAPINFOHEADER;
- #define BI_RGB 0
- typedef struct tagRGBQUAD { // rgbq
- BYTE rgbBlue;
- BYTE rgbGreen;
- BYTE rgbRed;
- BYTE rgbReserved;
- } RGBQUAD;
- int main(int argc, char* argv[])
- {
- FILE *hf;
- BITMAPFILEHEADER bf;
- BITMAPINFOHEADER bi;
- long pLine, pCur;
- long ipixelbytes;
- long iscanbytes;
- int bline, b;
- BYTE byBuf[3];
- int i,j,k;
- printf("MagicPic V1.0 Copyright (c) 2006 by zyl910\\n\\n");
- if (argc < 2)
- {
- printf("Syntax: magicpic [filename]\\n");
- return 0;
- }
- if ((hf=fopen(argv[1],"rb+"))==NULL)
- {
- printf("Can't Open File!\\n");
- return 0;
- }
- do{
- if (!fread(&bf,sizeof(BITMAPFILEHEADER),1,hf))
- {
- printf("Error: BITMAPFILEHEADER\\n");
- break;
- }
- if(0x4d42!=bf.bfType) // BM
- {
- printf("Error: Only BMP\\n");
- break;
- }
- if (!fread(&bi,sizeof(BITMAPINFOHEADER),1,hf))
- {
- printf("Error: BITMAPINFOHEADER\\n");
- break;
- }
- if(40!=bi.biSize)
- {
- printf("Error: Only BITMAPINFOHEADER\\n");
- break;
- }
- if (bi.biWidth<=0 || bi.biHeight<=0)
- {
- printf("Error: Width/Height\\n");
- break;
- }
- if (BI_RGB!=bi.biCompression || !(bi.biBitCount==24 || bi.biBitCount==32))
- {
- printf("Error: Only True Color(24bit/32bit)\\n");
- break;
- }
- pLine = ftell(hf);
- ipixelbytes = bi.biBitCount / 8;
- iscanbytes = (ipixelbytes * bi.biWidth + 3) & -4;
- bline = (bi.biHeight ) & 1;
- for (i=0; i<bi.biHeight; i++)
- {
- pCur = pLine;
- b = bline;
- for(j=0; j<bi.biWidth; j++)
- {
- if (b)
- {
- fseek(hf, pCur, SEEK_SET);
- fread(&byBuf[0], 1, 3, hf);
- // 反色
- byBuf[2] = ~byBuf[2]; // R
- byBuf[1] = ~byBuf[1]; // G
- byBuf[0] = ~byBuf[0]; // B
- // 修改
- fseek(hf, pCur, SEEK_SET);
- fwrite(&byBuf[0], 1, 3, hf);
- }
- pCur += ipixelbytes;
- b = b^1;
- }
- pLine += iscanbytes;
- bline = bline^1;
- }
- printf("Succeed!\\n");
- }while(0);
- fclose(hf);
- return 0;
- }
复制代码 |
|