【摘要】 介紹BMP圖片上下翻轉、添加水印、通過學習BMP圖片結構學習文件編程。
BMP圖片練習文件操作(專題練習)
1.?BMP圖片數據取模,模擬圖片取模軟件。(選擇16位或者24位取模方式)
16位。
2.?BMP圖片放大縮小,根據輸入的尺寸放大縮小圖片。
3.?實現圖片4種翻轉效果: 上、下、左、右。
4.?給圖片的指定位置添加水印
要求: 在圖片的任意位置,添加任意的文字水印。
比如: xxx路口 20181008 11:04
將字庫加入: ASCII和中文GBK字庫
??目錄作業: 拷貝目錄下所有文件(指定后綴的文件)到指定目錄下,考慮一層目錄。
多層目錄拷貝。
??Makefile作業: 使用Makefile建立工程,只需要寫一個Makefile文件。
(1)BMP圖片上下翻轉實現
#include
#include
#include
#include
/* 必須在結構體定義之前使用,這是為了讓結構體中各成員按1字節對齊 */
#pragma pack(1)
/*需要文件信息頭:14個字節 */
struct tagBITMAP_FILE_HEADER
{
unsigned short bfType; //保存圖片類似。 'BM' -- 0x4d42
unsigned int bfSize; //圖片的大小
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits; //RGB數據偏移地址
};
/* 位圖參數信息 */
struct tagBITMAP_INFO_HEADER {
unsigned long biSize; //結構體大小
unsigned long biWidth; //寬度
unsigned long biHeight; //高度
unsigned short biPlanes;
unsigned short biBitCount; //顏色位數
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXPelsPerMeter;
unsigned long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
};
/*
函數功能: BMP圖片翻轉
函數參數:
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. 圖片參數獲取*/
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圖像參數
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) //判斷顏色位數
{
err=5;
goto ERROR;
}
/*3. 創建新圖片*/
FILE *new_file=fopen(new_BmpFile,"wb");
if(new_file==NULL)
{
err=6;
goto ERROR;
}
/*3.1 創建BMP文件頭*/
fwrite(&src_head,1,sizeof(struct tagBITMAP_FILE_HEADER),new_file);
/*3.2 創建BMP圖像參數*/
fwrite(&src_info,1,sizeof(struct tagBITMAP_INFO_HEADER),new_file);
/*3.3 實現圖片上下翻轉*/
int i;
int lineByte=src_info.biWidth*3; //一行總字節數量
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
/* 必須在結構體定義之前使用,這是為了讓結構體中各成員按1字節對齊 */
#pragma pack(1)
/*需要文件信息頭:14個字節 */
struct tagBITMAP_FILE_HEADER
{
unsigned short bfType; //保存圖片類似。 'BM' -- 0x4d42
unsigned int bfSize; //圖片的大小
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits; //RGB數據偏移地址
};
/* 位圖參數信息 */
struct tagBITMAP_INFO_HEADER {
unsigned long biSize; //結構體大小
unsigned long biWidth; //寬度
unsigned long biHeight; //高度
unsigned short biPlanes;
unsigned short biBitCount; //顏色位數
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXPelsPerMeter;
unsigned long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
};
/*-- 文字: 水 --*/
/*-- 宋體42; 此字體下對應的點陣為:寬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; 此字體下對應的點陣為:寬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,
};
/*
函數功能: 針對BMP圖片實現的畫點函數
函數參數:
char *bmp_mem :表示BMP圖片RGB顏色數據的首地址
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;
}
/*
函數功能: 在BMP圖片的指定位置添加字符串
說明: 傳入的取模字體必須是8的倍數(寬度和高度是相等)
*/
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
學習Makefile
1.?學習什么是目標文件: 該如何定義
2.?學習什么是目標依賴文件:該如何定義
3.?Makefile本身推導規則: 如何根據目標和目標依賴文件去進行編譯生成目標。
4.?學習特殊變量的定義和功能使用: VPATH\ CC\ CFLAGS
5.?條件判斷語句、常用的幾個函數 $(Shell ls)。
6.?自動化編譯的符號: $@ $< $^ %
make <參數> -n表示調試不編譯 -s 隱藏命令的輸出
關于make命令運用時傳遞的參數:
make abc=123 app -ns
Shell腳本編程
Shell腳本: 是一個編程語言(腳本類型的編程語言、解釋類型編程語言)
變量、for循環、while循環、if語言、switch語句、函數….
??Linux開發: 側重于驅動開發、側重于運維開發。
;i++)>-
BMP
+關注
關注
0文章
48瀏覽量
17074 -
Makefile
+關注
關注
1文章
125瀏覽量
19189 -
Shell腳本
+關注
關注
0文章
36瀏覽量
7975
發布評論請先 登錄
相關推薦
評論