linu下C语言之BMP图片操作编程(下)
前面提高了一个将BMP左转的程序,右转其实也是类似的操作,就不写了,这节,我们来实现,将一张BMP图进行灰度处理,代码贴上:
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #define RGB(r,g,b) ((r+g+b)/3) typedef unsigned char U8 ; typedef unsigned short U16 ; typedef unsigned int U32 ; #pragma pack(1) struct bmp_header { //bmp header U8 Signatue[2] ; // B M U32 FileSize ; //文件大小 U16 Reserv1 ; U16 Reserv2 ; U32 FileOffset ; //文件头偏移量 //DIB header U32 DIBHeaderSize ; //DIB头大小 U32 ImageWidth ; //文件宽度 U32 ImageHight ; //文件高度 U16 Planes ; U16 BPP ; //每个相素点的位数 U32 Compression ; U32 ImageSize ; //图文件大小 U32 XPPM ; U32 YPPM ; U32 CCT ; U32 ICC ; }; #pragma pack() int main(int argc , char **argv) { if(argc != 4) return -1 ; int fd ; int dest_fd ; fd = open(argv[1] , O_RDONLY); if(-1 == fd) { perror("open bmp file fail"); return -2 ; } dest_fd = open( argv[2] , O_RDWR | O_CREAT|O_TRUNC , 0777); if(dest_fd < 0 ) { perror("open rgb565 fail"); return -3 ; } int value = atoi(argv[3]); struct bmp_header header ; int ret ; ret = read(fd , &header , sizeof(struct bmp_header)); printf(" Signatue[0] : %c \n " , header.Signatue[0] ); printf(" Signatue[1] : %c \n " , header.Signatue[1] ); printf(" FileSize : %d \n " , header.FileSize ); printf(" Reserv1 : %d \n " , header.Reserv1 ); printf(" Reserv2 : %d \n " , header.Reserv2 ); printf(" FileOffset : %d \n " , header.FileOffset ); printf(" DIBHeaderSize : %d \n " , header.DIBHeaderSize); printf(" ImageWidth : %d \n " , header.ImageWidth ); printf(" ImageHight : %d \n " , header.ImageHight ); printf(" Planes : %d \n " , header.Planes ); printf(" BPP : %d \n " , header.BPP ); printf(" Compression : %d \n " , header.Compression ); printf(" ImageSize : %d \n " , header.ImageSize ); printf(" XPPM : %d \n " , header.XPPM ); printf(" YPPM : %d \n " , header.YPPM ); printf(" CCT : %d \n " , header.CCT ); printf(" ICC : %d \n " , header.ICC ); char buffer[header.ImageSize] ; read(fd , buffer , header.ImageSize); close(fd); //改变地方 write(dest_fd , &header , sizeof(struct bmp_header)); int row , col ; char *p = NULL ; for(row = 0 ; row < 10 ; row++) { for(col = 0 ; col < 10 ; col++) { p = buffer + (row * 800 + col)*3 ; *p = 255; *(p+1)=255; *(p+2)=255; } } char data ; for(row = 0 ; row < 480 ; row++) { for(col = 0 ; col < 800 ; col++) { p =( buffer+(row*800 + col)*3); data = RGB((unsigned char)(*(p+2)) , (unsigned char)(*(p+1)) , (unsigned char )(*(p))); if(data < value) { *p = 0; *(p+1)=0; *(p+2)=0; } else { *p = data; *(p+1)=data; *(p+2)=data; } write(dest_fd , p , 3); } } close(dest_fd); return 0 ; }
linu下C语言之BMP图片操作编程(下)的更多相关文章
- linu下C语言之BMP图片操作编程(上)
BMP文件格式,也被称为位图图像文件或与设备无关的位图文件格式(DIB)或者只是一个位图,是 一个光栅图形 图像文件格式使用 来存储位图,数字,图片,独立的显示设备. 微软已经定义了一个特定的表示颜色 ...
- linu下C语言之BMP图片操作编程(中)
http://blog.csdn.net/morixinguan/article/details/50719472 关于BMP图的介绍之前已经说过了,最近要用到,又要重新开始学习. 现在实现一个让bm ...
- 【C】用C语言提取bmp图片像素,并进行K-means聚类分析——容易遇到的问题
关于bmp图片的格式,网上有很多文章,具体可以参考百度百科,也有例子程序.这里只提要注意的问题. (1)结构体定义问题:首先按照百度百科介绍的定义了结构体,但是编译发现重定义BITMAPFILEHEA ...
- C语言实现BMP图片生成
## #include <stdio.h> #include <stdlib.h> #include <string.h> typedef unsigned cha ...
- Go语言基础之接口(面向对象编程下)
1 接口 1.1 接口介绍 接口(interface)是Go语言中核心部分,Go语言提供面向接口编程,那么接口是什么? 现实生活中,有许多接口的例子,比如说电子设备上的充电接口,这个充电接口能干什么, ...
- .NET Core 图片操作在 Linux/Docker 下的坑
一.前言 .NET Core 目前更新到2.2了,但是直到现在在 .NET Core 本身依然不包括和图片有关的 Image.Bitmap 等类型.对于图片的操作在我们开发中很常见,比如:生成验证码. ...
- 位图文件格式及linux下C语言来操作位图文件
说到图片,位图(Bitmap)当然是最简单的,它是Windows显示图片的基本格式,其文件扩展名为*.BMP.由于没有经过任何的压缩,故BMP图片往往很大.在Windows下,任何格式的图片文件都要转 ...
- 嵌入式linux------SDL移植(am335x下显示bmp图片)
#include<stdio.h> #include "/usr/local/ffmpeg_arm/include/SDL/SDL.h" char *bmp_name[ ...
- bmp图片的有关操作
读取bmp图片 并生成新的bmp图片 #include "stdafx.h"#include <windows.h>#include <cmath>#inc ...
随机推荐
- [BZOJ]4650: [Noi2016]优秀的拆分
Time Limit: 30 Sec Memory Limit: 512 MB Description 如果一个字符串可以被拆分为 AABBAABB 的形式,其中 AA 和 BB 是任意非空字符串, ...
- [BZOJ]4805: 欧拉函数求和
解题思路类似莫比乌斯函数之和 题目大意:求[1,n]内的欧拉函数$\varphi$之和.($n<=2*10^{9}$) 思路:令$ M(n)=\sum_{i=1}^{n}\varphi (i) ...
- hdu 5314 动态树
Happy King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
- hdu 5538(水)
Input The first line contains an integer T indicating the total number of test cases. First line of ...
- 配置mysql使其允许外部ip进行登录
这两天在做一个数据库的项目,用到了mysql,需要通过外部的ip远程访问mysql,发现默认的mysql是不允许远程访问的,需要做简单的配置. 如下: 1. 打开一个终端(cmd)输入 mysql - ...
- display显示方式
元素的diplay显示方式有多种,隐藏.块级.内联.内联-块级. 1.display:none 隐藏 2.display:block; 表示块级元素. 块级元素会自动在前面和后面加上换行,并且在其 ...
- selenium登录163邮箱
环境:windows8 python2.7+selenium+chrome 直接上脚本: # coding=utf-8from selenium import webdriverimport tim ...
- Jvm原理剖析与调优之内存结构
一些不得不说的概念 JVM JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的.Java虚拟机包括一套字节码指令集.一组寄存器.一个栈.一个 ...
- node之querystring模块
前言 querystring 模块提供了一些实用工具,用于解析与格式化 URL 查询字符串. 一.querystring.parse() 用于将一个查询字符串解析为JS 对象. const query ...
- Python小代码_2_格式化输出
Python小代码_2_格式化输出 name = input("name:") age = input("age:") job = input("jo ...