How to manipulate pixels on a bitmap by scanline property(Ma Xiaoguang and Ma Xiaoming)
We have been developing image processing software for above 14 years with old versions of Delphi, such as Delphi 6 and Delphi 7. We often working on pixels of bitmap by its Scanline property. We want to do such things with FireMonkey framework in Delphi XE6. But at beginning, we even don't know how to do it with the familiar approach, the Scanline property, because TBitmap class itself in FireMonkey has no the Scanline property anymore.
We have to study how to work with FireMonkey by searching for information from Internet. And we have found a piece of code at Delphi official website: http://docwiki.embarcadero.com/CodeExamples/XE6/en/FMX.AlphaColorToScanline_(Delphi)
The code in the above page illustrated that for accessing the pixels on a bitmap, we have to beg a help from class TBitmapData. The demo code cannot be compiled through XE6, because the compiler reports lack of method GetPixelFormatBytes(). The code is for copying specified amount of lines of pixels from a source bitmap to a destination bitmap written with XE3. We have modified the original code a little bit to copy the all the pixels from the source to a destination, the code could be compiled by XE6 and runs well then.
First of all, creating a FireMonkey Destop project in XE6, drop two TImage components and a TButton component and a TOpenDialog component onto the form. Double click the TButton component and typing in the following code to its code block.
------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
bd1, bd2 : TBitmapData;
w, h : Integer;
begin
OpenDialog1.Filter := 'JPEG Image|*.jpg';
if OpenDialog1.Execute then
begin
Image1.Bitmap.LoadFromFile(OpenDialog1.FileName);
w := Image1.Bitmap.Width;
h := Image1.Bitmap.Height;
Image2.Bitmap.PixelFormat := Image1.Bitmap.PixelFormat;
Image2.Bitmap.SetSize(w, h);
try
Image1.Bitmap.Map(TMapAccess.Read, bd1);
Image2.Bitmap.Map(TMapAccess.Write, bd2);
AlphaColorToScanline(@PAlphaColorArray(bd1.Data)[0], bd2.Data,
Round(w * h), Image1.Bitmap.PixelFormat);
finally
Image1.Bitmap.Unmap(bd1);
Image2.Bitmap.Unmap(bd2);
end;
end;
end;
------------------------------
As you can see that the TBitmapData class is really great for being as a delegate to accessing data in a TBitmap object. We can mapping a
TBitmap object to be as a TBitmapData object, and then mess up bitmap data around with TBitmapData. And we can even control over the accessing permissions to those bitmap data.
We can copy all the pixels from a bitmap to another, but how can we manipulating each single pixel on a bitmap in our familiar approach,
with the scanline property, separately? The answer is included in TBitmapData class. In the following, we did the same things as above with scanline property.
------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
bd1, bd2 : TBitmapData;
x, y, w, h : Integer;
p1, p2 : PAlphaColorArray;
begin
OpenDialog1.Filter := 'JPEG Image|*.jpg';
if OpenDialog1.Execute then
begin
Image1.Bitmap.LoadFromFile(OpenDialog1.FileName);
w := Image1.Bitmap.Width;
h := Image1.Bitmap.Height;
Image2.Bitmap.PixelFormat := Image1.Bitmap.PixelFormat;
Image2.Bitmap.SetSize(w, h);
try
Image1.Bitmap.Map(TMapAccess.Read, bd1);
Image2.Bitmap.Map(TMapAccess.Write, bd2);
for y := 0 to (h - 1) do
begin
p1 := PAlphaColorArray(bd1.GetScanline(y));
p2 := PAlphaColorArray(bd2.GetScanline(y));
for x := 0 to (w - 1) do
begin
p2[x] := p1[x];
end;
end;
finally
Image1.Bitmap.Unmap(bd1);
Image2.Bitmap.Unmap(bd2);
end;
end;
end;
------------------------------
Wow, we can manipulating bitmap's pixels as before in FireMonkey framework. That's cool. The benefits of accessing individual pixel on a bitmap is that we can developing our own image filters now.
We haven't appreciating all the power of FireMonkey. But we believe that we can do more cool things with it. We will keep on studying it, and sharing more as we can.
Ma Xiaoguang and Ma Xiaoming
http://blog.csdn.net/gmbros/article/details/39047769
http://blog.sina.cn/dpool/blog/u/1282411531#type=-1
https://community.embarcadero.com/answers/how-do-i-get-at-individual-pixels-in-a-firemonkey-tbitmap
How to manipulate pixels on a bitmap by scanline property(Ma Xiaoguang and Ma Xiaoming)的更多相关文章
- 加快Bitmap的访问速度
引言 在对Bitmap图片操作的时候,有时需要用到获取或设置像素颜色方法:GetPixel 和 SetPixel, 如果直接对这两个方法进行操作的话速度很慢,这里我们可以通过把数据提取出来操作,然后操 ...
- 以Lockbits的方式访问bitmap
转载自:http://www.cnblogs.com/xiashengwang/p/4225848.html 2015-01-15 11:38 by xiashengwang, 585 阅读, 0 评 ...
- C#加快Bitmap的访问速度
在对Bitmap图片操作的时候,有时需要用到获取或设置像素颜色方法:GetPixel 和 SetPixel, 如果直接对这两个方法进行操作的话速度很慢,这里我们可以通过把数据提取出来操作,然后操作完在 ...
- .net 反射访问私有变量和私有方法 如何创建C# Closure ? C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密 C#中的foreach和yield 数组为什么可以使用linq查询 C#中的 具名参数 和 可选参数 显示实现接口 异步CTP(Async CTP)为什么那样工作? C#多线程基础,适合新手了解 C#加快Bitmap的访问速度 C#实现对图片文件的压
以下为本次实践代码: using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- 【读书笔记《Android游戏编程之从零开始》】14.游戏开发基础(Bitmap 位图的渲染与操作)
Bitmap 是图形类,Android 系统支持的图片格式有 png.jpg.bmp 等. 对位图操作在游戏中是很重要的知识点,比如游戏中需要两张除了大小之外其他完全相同的图,那么如果会对位图进行缩放 ...
- Android开发学习之路-图片颜色获取器开发(1)
系列第一篇,从简单的开始,一步一步完成这个小项目. 颜色获取就是通过分析图片中的每个像素的颜色,来分析整个图片的主调颜色,有了主调颜色,我们可以用于图片所在卡片的背景或者标题颜色,这样整体感更加强烈. ...
- Android下OpenCV的环境搭建
目录(?)[-] 前言 系统环境 相关工具 Android ADT环境搭建 Android SDK环境变量的配置 Android NDK的安装与配置 OpenCV for Android 环境搭建 基 ...
- inno setup介绍及官方网站地址
使 用 笔 记 1.Inno Setup 是什么?Inno Setup 是一个免费的 Windows 安装程序制作软件.第一次发表是在 1997 年,Inno Setup 今天在功能设置和稳定性上的竞 ...
- JNI,NDK
jni的调用过程 1)安装和下载Cygwin,下载Android NDK 2)在ndk项目中JNI接口的设计 3)使用C/C++实现本地方法 4)JNI生成动态链接库.so文件 5)将动态链接库复制到 ...
随机推荐
- .netcore consul实现服务注册与发现-单节点部署
原文:.netcore consul实现服务注册与发现-单节点部署 一.Consul的基础介绍 Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分 ...
- Filter,Listener(转)
一.Filter的功能filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个reques ...
- Curator简介
Curator简介 Curator是一个博物馆或者其它集合的监管者或者管理者,Curator有以下几个组件组成: Recipes: 实现了通用ZooKeeper的recipes, 该组件是在Frame ...
- 用Canvas画一个刮刮乐
Canvas 通过 JavaScript 来绘制 2D图形.Canvas 是逐像素进行渲染的.开发者可以通过javascript脚本实现任意绘图.Canvas元素是HTML5的一部分,允许脚本语言动态 ...
- 对Java JVM中类加载几点解释
1.用到类的时候,类加载到方法区,同时方法区会存放static的内容(包括静态方法和静态变量),随类的加载而加载 2当new的时候,会在堆中创建一个对象,在其中会开辟其中的实例变量内存并初始化,堆中变 ...
- Python应用库大全总结
学python,想必大家都是从爬虫开始的吧.毕竟网上类似的资源很丰富,开源项目也非常多. python学习网络爬虫主要分3个大的版块:抓取,分析,存储 当我们在浏览器中输入一个url后回车,后台会发生 ...
- 《⑨也懂系列:MinGW-w64安装教程》著名C/C++编译器GCC的Windows版本(MinGW-w64在安装的时候可以选择版本,有图,一步一步)
发布日期 2016年10月31日 分类 教程 标签 编程.软件 前言<⑨也懂系列:MinGW-w64安装教程>这篇文章由 rsreland (http://rsreland.net)于 2 ...
- Struts2——(7)拦截器组件
AOP:面向切面编程(通过配置文件来指定作用到目标对象) OOP:面向对象编程 AOP具有很好的可插拔特性,很灵活. 可用于封装共通的业务处理,之后可以通过配置作用到Action组件上. 共通的业务处 ...
- C/C++ 常量的定义与应用(编程中的常量)
常量一般定义为全局变量,且大写: 1. 字符串常量 const string EXPAND_X = "X+YF"; const string EXPAND_Y = "FX ...
- Node child_process Study.2
child_process 模块用于新建子进程.子进程的运行结果存储在系统缓存之中,等到子进程运行结束之后,主进程再用回调函数读取子进程的运行结果 1.exec() exec 方法用于执行base命令 ...