【摘要】 介紹BMP圖片上下翻轉(zhuǎn)、添加水印、通過學(xué)習(xí)BMP圖片結(jié)構(gòu)學(xué)習(xí)文件編程。
BMP圖片練習(xí)文件操作(專題練習(xí))
1.?BMP圖片數(shù)據(jù)取模,模擬圖片取模軟件。(選擇16位或者24位取模方式)
16位。
2.?BMP圖片放大縮小,根據(jù)輸入的尺寸放大縮小圖片。
3.?實現(xiàn)圖片4種翻轉(zhuǎn)效果: 上、下、左、右。
4.?給圖片的指定位置添加水印
要求: 在圖片的任意位置,添加任意的文字水印。
比如: xxx路口 20181008 11:04
將字庫加入: ASCII和中文GBK字庫
??目錄作業(yè): 拷貝目錄下所有文件(指定后綴的文件)到指定目錄下,考慮一層目錄。
多層目錄拷貝。
??Makefile作業(yè): 使用Makefile建立工程,只需要寫一個Makefile文件。
(1)BMP圖片上下翻轉(zhuǎn)實現(xiàn)
#include
#include
#include
#include
/* 必須在結(jié)構(gòu)體定義之前使用,這是為了讓結(jié)構(gòu)體中各成員按1字節(jié)對齊 */
#pragma pack(1)
/*需要文件信息頭:14個字節(jié) */
struct tagBITMAP_FILE_HEADER
{
unsigned short bfType; //保存圖片類似。 'BM' -- 0x4d42
unsigned int bfSize; //圖片的大小
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits; //RGB數(shù)據(jù)偏移地址
};
/* 位圖參數(shù)信息 */
struct tagBITMAP_INFO_HEADER {
unsigned long biSize; //結(jié)構(gòu)體大小
unsigned long biWidth; //寬度
unsigned long biHeight; //高度
unsigned short biPlanes;
unsigned short biBitCount; //顏色位數(shù)
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXPelsPerMeter;
unsigned long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
};
/*
函數(shù)功能: BMP圖片翻轉(zhuǎn)
函數(shù)參數(shù):
char *src_BmpFile :BMP圖片源文件
char *new_BmpFile :新文件
返回值 :0表示成功,其他值失敗
*/
int BMPOverturn(char *src_BmpFile,char *new_BmpFile)
{
/*1. 打開源文件*/
int err=0;
FILE *bmp_file=fopen(src_BmpFile,"rb");
if(bmp_file==NULL)
{
err=1;
goto ERROR;
}
/*2. 圖片參數(shù)獲取*/
struct tagBITMAP_FILE_HEADER src_head; //BMP文件頭
memset(&src_head,0,sizeof(struct tagBITMAP_FILE_HEADER));
if(fread(&src_head,1,sizeof(struct tagBITMAP_FILE_HEADER),bmp_file)!=sizeof(struct tagBITMAP_FILE_HEADER))
{
err=2;
goto ERROR;
}
if(src_head.bfType!=0x4d42) //判斷類型
{
err=3;
goto ERROR;
}
struct tagBITMAP_INFO_HEADER src_info; //BMP圖像參數(shù)
memset(&src_info,0,sizeof(struct tagBITMAP_INFO_HEADER));
if(fread(&src_info,1,sizeof(struct tagBITMAP_INFO_HEADER),bmp_file)!=sizeof(struct tagBITMAP_INFO_HEADER))
{
err=4;
goto ERROR;
}
if(src_info.biBitCount!=24) //判斷顏色位數(shù)
{
err=5;
goto ERROR;
}
/*3. 創(chuàng)建新圖片*/
FILE *new_file=fopen(new_BmpFile,"wb");
if(new_file==NULL)
{
err=6;
goto ERROR;
}
/*3.1 創(chuàng)建BMP文件頭*/
fwrite(&src_head,1,sizeof(struct tagBITMAP_FILE_HEADER),new_file);
/*3.2 創(chuàng)建BMP圖像參數(shù)*/
fwrite(&src_info,1,sizeof(struct tagBITMAP_INFO_HEADER),new_file);
/*3.3 實現(xiàn)圖片上下翻轉(zhuǎn)*/
int i;
int lineByte=src_info.biWidth*3; //一行總字節(jié)數(shù)量
if(lineByte%4)lineByte++;
int offset=lineByte*(src_info.biHeight-1)+src_head.bfOffBits;
char *data_p=malloc(lineByte);
if(data_p==NULL)
{
err=7;
goto ERROR;
}
for(i=0;i;i++)>
(2)BMP圖片水印添加
#include
#include
#include
#include
/* 必須在結(jié)構(gòu)體定義之前使用,這是為了讓結(jié)構(gòu)體中各成員按1字節(jié)對齊 */
#pragma pack(1)
/*需要文件信息頭:14個字節(jié) */
struct tagBITMAP_FILE_HEADER
{
unsigned short bfType; //保存圖片類似。 'BM' -- 0x4d42
unsigned int bfSize; //圖片的大小
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits; //RGB數(shù)據(jù)偏移地址
};
/* 位圖參數(shù)信息 */
struct tagBITMAP_INFO_HEADER {
unsigned long biSize; //結(jié)構(gòu)體大小
unsigned long biWidth; //寬度
unsigned long biHeight; //高度
unsigned short biPlanes;
unsigned short biBitCount; //顏色位數(shù)
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXPelsPerMeter;
unsigned long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
};
/*-- 文字: 水 --*/
/*-- 宋體42; 此字體下對應(yīng)的點陣為:寬x高=56x56 --*/
const unsigned char font0[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,
0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,0x00,
0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x00,
0x00,0x00,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,
0x7C,0x00,0x0C,0x00,0x00,0x00,0x00,0x7C,0x00,0x0E,0x00,0x00,0x00,0x00,0x7E,0x00,
0x1F,0x00,0x00,0x00,0x00,0x7E,0x00,0x3F,0x80,0x00,0x00,0x00,0x7E,0x00,0x7F,0xC0,
0x00,0x00,0x18,0x7E,0x00,0xFE,0x00,0x00,0x00,0x3C,0x7F,0x00,0xF8,0x00,0x1F,0xFF,
0xFE,0x7F,0x03,0xF0,0x00,0x0F,0xFF,0xFF,0x7F,0x87,0xC0,0x00,0x07,0x80,0x7E,0x7F,
0x8F,0x80,0x00,0x00,0x00,0x7C,0x7F,0x9F,0x00,0x00,0x00,0x00,0x7C,0x7F,0xFC,0x00,
0x00,0x00,0x00,0xF8,0x7D,0xF8,0x00,0x00,0x00,0x00,0xF8,0x7D,0xE0,0x00,0x00,0x00,
0x00,0xF8,0x7C,0xE0,0x00,0x00,0x00,0x01,0xF0,0x7C,0xF0,0x00,0x00,0x00,0x01,0xF0,
0x7C,0xF0,0x00,0x00,0x00,0x01,0xF0,0x7C,0x78,0x00,0x00,0x00,0x03,0xE0,0x7C,0x7C,
0x00,0x00,0x00,0x03,0xE0,0x7C,0x3C,0x00,0x00,0x00,0x07,0xC0,0x7C,0x3E,0x00,0x00,
0x00,0x07,0xC0,0x7C,0x1E,0x00,0x00,0x00,0x0F,0x80,0x7C,0x1F,0x00,0x00,0x00,0x0F,
0x80,0x7C,0x0F,0x80,0x00,0x00,0x1F,0x00,0x7C,0x0F,0xC0,0x00,0x00,0x1E,0x00,0x7C,
0x07,0xE0,0x00,0x00,0x3E,0x00,0x7C,0x03,0xE0,0x00,0x00,0x3C,0x00,0x7C,0x03,0xF0,
0x00,0x00,0x78,0x00,0x7C,0x01,0xFC,0x00,0x00,0xF8,0x00,0x7C,0x00,0xFE,0x00,0x00,
0xF0,0x00,0x7C,0x00,0xFF,0x00,0x01,0xE0,0x00,0x7C,0x00,0x7F,0xC0,0x03,0xC0,0x00,
0x7C,0x00,0x3F,0xE0,0x07,0x80,0x00,0x7C,0x00,0x1F,0xFC,0x0F,0x00,0x00,0x7C,0x00,
0x0F,0xFC,0x1E,0x00,0x00,0x7C,0x00,0x07,0xE0,0x1C,0x00,0x00,0x7C,0x00,0x03,0xC0,
0x38,0x00,0x7F,0xFC,0x00,0x01,0x80,0x00,0x00,0x7F,0xFC,0x00,0x00,0x00,0x00,0x00,
0x0F,0xFC,0x00,0x00,0x00,0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,0x01,0xF0,
0x00,0x00,0x00,0x00,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/*-- 文字: 印 --*/
/*-- 宋體42; 此字體下對應(yīng)的點陣為:寬x高=56x56 --*/
const unsigned char font1[]=
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,
0x00,0x00,0x00,0x00,0x00,0x0F,0xF0,0x00,0x00,0x00,0x00,0x00,0x3F,0xF0,0x00,0x00,
0x00,0x00,0x00,0xFF,0xE0,0x00,0x03,0x00,0x01,0x87,0xF8,0x03,0x80,0x03,0xC0,0x01,
0xFF,0xC0,0x03,0xFF,0xFF,0xE0,0x01,0xFC,0x00,0x03,0xFF,0xFF,0xE0,0x01,0xE0,0x00,
0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,
0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,
0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,
0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,
0xC0,0x07,0xC0,0x01,0xE0,0x03,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x07,0x83,0xC0,0x07,
0xC0,0x01,0xFF,0xFF,0xC3,0xC0,0x07,0xC0,0x01,0xFF,0xFF,0xE3,0xC0,0x07,0xC0,0x01,
0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,
0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,
0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,
0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,
0x00,0x03,0xC0,0x07,0xC0,0x01,0xE0,0x00,0x73,0xC0,0x07,0xC0,0x01,0xE0,0x03,0xF3,
0xC0,0x07,0xC0,0x01,0xE0,0x3F,0x83,0xC0,0x07,0xC0,0x01,0xE1,0xFE,0x03,0xC7,0xFF,
0xC0,0x01,0xFF,0xF0,0x03,0xC1,0xFF,0xC0,0x03,0xFF,0xC0,0x03,0xC0,0x7F,0x80,0x03,
0xFF,0x00,0x03,0xC0,0x1F,0x00,0x01,0xFC,0x00,0x03,0xC0,0x0E,0x00,0x01,0xF0,0x00,
0x03,0xC0,0x00,0x00,0x00,0xE0,0x00,0x03,0xC0,0x00,0x00,0x00,0x40,0x00,0x03,0xC0,
0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,
0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,
0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/*
函數(shù)功能: 針對BMP圖片實現(xiàn)的畫點函數(shù)
函數(shù)參數(shù):
char *bmp_mem :表示BMP圖片RGB顏色數(shù)據(jù)的首地址
int x
int y
int color :畫點的顏色值
BMP_DrawPoint(bmp_mem,100,100,0);
*/
unsigned int bmp_Width; //保存BMP圖片的寬度
void BMP_DrawPoint(unsigned char *bmp_mem,int x,int y,int color)
{
unsigned char *rgb=(unsigned char *)(bmp_mem+y*bmp_Width*3+x*3);
*rgb=color>>0&0xFF;
*(rgb+1)=color>>8&0xFF;
*(rgb+2)=color>>16&0xFF;
}
/*
函數(shù)功能: 在BMP圖片的指定位置添加字符串
說明: 傳入的取模字體必須是8的倍數(shù)(寬度和高度是相等)
*/
void BMP_ShowString(unsigned char *bmp_mem,unsigned char *font,int x,int y,int size,int color)
{
int i,j;
int x0=x;
unsigned char data;
for(i=0;i
學(xué)習(xí)Makefile
1.?學(xué)習(xí)什么是目標文件: 該如何定義
2.?學(xué)習(xí)什么是目標依賴文件:該如何定義
3.?Makefile本身推導(dǎo)規(guī)則: 如何根據(jù)目標和目標依賴文件去進行編譯生成目標。
4.?學(xué)習(xí)特殊變量的定義和功能使用: VPATH\ CC\ CFLAGS
5.?條件判斷語句、常用的幾個函數(shù) $(Shell ls)。
6.?自動化編譯的符號: $@ $< $^ %
make <參數(shù)> -n表示調(diào)試不編譯 -s 隱藏命令的輸出
關(guān)于make命令運用時傳遞的參數(shù):
make abc=123 app -ns
Shell腳本編程
Shell腳本: 是一個編程語言(腳本類型的編程語言、解釋類型編程語言)
變量、for循環(huán)、while循環(huán)、if語言、switch語句、函數(shù)….
??Linux開發(fā): 側(cè)重于驅(qū)動開發(fā)、側(cè)重于運維開發(fā)。
;i++)>-
BMP
+關(guān)注
關(guān)注
0文章
48瀏覽量
17075 -
Makefile
+關(guān)注
關(guān)注
1文章
125瀏覽量
19191 -
Shell腳本
+關(guān)注
關(guān)注
0文章
36瀏覽量
7992
發(fā)布評論請先 登錄
相關(guān)推薦
評論