Processing中PImage类和loadImage()、createImage()函数的相关解析
聊一聊Processing中PImage类和loadImage()、createImage()函数。因为要借P5做多媒体创意展示,图片是一个很重要的媒体。有必要就图片的获取和展放作总结。
首先
有一点需要先提出来,PGraphics
是继承自PImage
的,看源码:
public class PGraphics extends PImage implements PConstants {
...
}
因此,理论上所有的绘制函数其实它的绘制对象都是PImage,都在这张图纸上呈现内容,即默认的PGraphics g
,可参考笔者另一篇:
https://www.cnblogs.com/sharpeye/p/13734132.html
这就给我们一个参考,意思是PGraphics
和PImage
时常要考虑两者互相转换的问题。实际运行时也是如此。
其次
PImage类
中设有混合叠加的方法、图片文件的IO方法,即保存读取方法等,比较常用的是loadPixels()
、save()
、filter()
等,当然还有许多内部成员变量,比如format
width
height
pixels
等等。其中format
指的是颜色通道格式,比如有RGB
ARGB
ALPHA
等。save()
是可以保存带有alpha通道的图像的。
PImage类要使用必须要new实例对象,一般的,无外乎是使用loadImage()
、createImage()
这两个函数来获得这一对象。如果要读取一张现有的图像信息,那么就load
。
根据官网说明,loadImage()
有两参数可供填写,即:loadImage(filename, extension)
。
filename
指的是本地文件路径或者url文件路径。本地文件路径可以是绝对地址也可以是相对地址。
extension
指的是指定文件后缀,即文件类型匹配。如果于真实文件类型不匹配也能读进内存,不过通道数据未必能读取,也就是alpha层的问题。文件类型共有几类 ".tga", ".jpg", ".png", ".jpeg", ".gif"等,".tif"无法读取,P5自己的.tiff保存文件是可以的。请见下文例子:
PImage img1;
PImage img2;
PImage webImg;
void setup() {
size(500,500);
img1 = loadImage("mypic.png"); //读取相对路径下的文件,即pde根目录下的文件,如果有data文件夹,则在此文件夹下寻找
img2 = loadImage("d://mypic.png");//读取绝对路径下的文件
String url = "https://processing.org/img/processing-web.png";//读取web互联网上的文件,也可读取局域网下的文件
webImg = loadImage(url, "png");//选定读取的文件类型为png
}
void draw() {
background(0);
image(img1, 0, 0);
image(img2, 200, 0);
image(webImg, 0, 200);
}
会注意到,读取本地的文件速度非常理想,但是互联网上的文件会根据网络情况产生不少等待时间,给它看成是Processing的假死状态,这是不希望看到的情况,如何来避免呢?有个函数官方给我们了----requstImage()
。
这个函数就可以避免假死状态,或者称之为非阻塞式读取,而传统读取是阻塞式的。实质上查阅源码会看到它新建了一个thread
线程,因此,可以避免占用主线程而耽误往后的语句执行任务。代码如下:
/**
* ( begin auto-generated from requestImage.xml )
*
* This function load images on a separate thread so that your sketch does
* not freeze while images load during <b>setup()</b>. While the image is
* loading, its width and height will be 0. If an error occurs while
* loading the image, its width and height will be set to -1. You'll know
* when the image has loaded properly because its width and height will be
* greater than 0. Asynchronous image loading (particularly when
* downloading from a server) can dramatically improve performance.<br />
* <br/> <b>extension</b> parameter is used to determine the image type in
* cases where the image filename does not end with a proper extension.
* Specify the extension as the second parameter to <b>requestImage()</b>.
*
* ( end auto-generated )
*
* @webref image:loading_displaying
* @param filename name of the file to load, can be .gif, .jpg, .tga, or a handful of other image types depending on your platform
* @param extension the type of image to load, for example "png", "gif", "jpg"
* @see PImage
* @see PApplet#loadImage(String, String)
*/
public PImage requestImage(String filename, String extension) {
// Make sure saving to this file completes before trying to load it
// Has to be called on main thread, because P2D and P3D need GL functions
if (g != null) {
g.awaitAsyncSaveCompletion(filename);
}
PImage vessel = createImage(0, 0, ARGB);
// if the image loading thread pool hasn't been created, create it
if (requestImagePool == null) {
ThreadFactory factory = new ThreadFactory() {
public Thread newThread(Runnable r) {
return new Thread(r, REQUEST_IMAGE_THREAD_PREFIX);
}
};
requestImagePool = Executors.newFixedThreadPool(4, factory);
}
requestImagePool.execute(() -> {
PImage actual = loadImage(filename, extension);
// An error message should have already printed
if (actual == null) {
vessel.width = -1;
vessel.height = -1;
} else {
vessel.width = actual.width;
vessel.height = actual.height;
vessel.format = actual.format;
vessel.pixels = actual.pixels;
vessel.pixelWidth = actual.width;
vessel.pixelHeight = actual.height;
vessel.pixelDensity = 1;
}
});
return vessel;
}
能发现官方设立了一个线程池接口类ExecutorService
,而后新建线程池,每次读取图片都新建一个线程入线程池,统一管理,并用ExecutorService
实例的对象来execute
执行,从而独立于主线程来执行任务。
再次
光有读取还不完善,来看看自己生成图片对象createImage()
。很清楚,三个参数
Parameters
w int: width in pixels 图片对象宽度尺寸
h int: height in pixels 图片对象高度尺寸
format int: Either RGB, ARGB, ALPHA (grayscale alpha channel) 图片类型
官方介绍到,要注意使用规范性,如下:
PImage image = createImage(600,400,RGB); //正确的实例PImage写法 其中图片通道类型有RGB, ARGB, ALPHA
// *错误* PImage image = new PImage(600,400,RGB); //错误的写法!!
实际上参照源码,就是new PImage的方法实例:
public PImage createImage(int w, int h, int format) {
PImage image = new PImage(w, h, format);
image.parent = this; // make save() work
return image;
}
值得思考的是:
一、 PImage对象改变其像素无外乎要修改其pixels数组值,而不能再图片上直接绘制图形,如果要绘制则必须转换成PGraphics
;
二、 如果要复制PImage以实例多个对象,可以使用copy()
或者clone()
,比如:
PImage op;
PImage op2;
try {
op = (PImage)img2.clone(); 使用clone方法 注意转换类型
}
catch(Exception e) {
}
op2 = img2.copy(): 使用copy方法
三、 绘制在PGraphics
上的方法有三种,一种是image()
,一种是set()
,还有一种是手动以遍历像素的形式来绘制渲染。如下:
image(img1, 100, 0);
image(img2, 200, 0);
image(webImg, 0, 200); //使用image方法
set(0,0,img2); //使用set方法
loadPixels();
img1.loadPixels();
for (int x = 0; x < img1.width; x++) {
for (int y = 0; y < img1.height; y++) {
color c = img1.pixels[y*img1.width + x];
if(alpha(c) == 0) continue; //alpha通道检测
pixels[y*width + x] = c ; //相当于 g.pixels[]... 使用了pixels数组直接赋值
}
}
updatePixels();
//************************************// 另一种手动填充像素 set
img1.loadPixels();
for (int x = 0; x < img1.width; x++) {
for (int y = 0; y < img1.height; y++) {
color c = img1.pixels[y*img1.width + x];
//if(alpha(c) == 0) continue; 使用set方法 alpha通道检测可跳过
set(x,y,c); //使用set方法 注意使用set方法跟updatePixels方法有冲突,这里去掉其调用
}
}
//************************************//
最后
当然,在实际使用中,会用到大量技巧,比如image()
、set()
两函数渲染图片效率有出入,一般可以用set()
来提高渲染效率,但是无法进行矩阵变换等运算。再如,PImage中的save()
可以保存带通道的图片。这次就简单做一总结,我们往后再细聊,感谢阅读!!
Processing中PImage类和loadImage()、createImage()函数的相关解析的更多相关文章
- C++中模板类使用友元模板函数
在类模板中可以出现三种友元声明:(1)普通非模板类或函数的友元声明,将友元关系授予明确指定的类或函数.(2)类模板或函数模板的友元声明,授予对友元所有实例的访问权.(3)只授予对类模板或函数模板的特定 ...
- 【转】doxygen+graphviz生成工程中的类继承树及函数调用图
转自----hequn8128 在阅读代码量比较多的项目时,类的继承树和函数调用图能够直观地向我们显示类之间或者函数之间的各种关系,方便我们了解程序的整体框架,很多时候可以起到事半功倍的作用.这里尝试 ...
- Java中Collections类的排序sort函数两种用法
java中的Colletions类主要实现列表List的排序功能.根据函数参数的传递,具体的排序可以分为 : 1. 自然排序(natural ordering). 函数原型:sort(List< ...
- JAVA中String类的方法(函数)总结--JAVA基础
1.concat()方法,当参数为两字符串时,可实现字符串的连接: package cn.nxl123.www; public class Test { public static void main ...
- php中常用的字符串大小写转换函数实例解释
PHP字符串处理函数中,最为简单的几个函数,相关解释就不上了,直接看例子. PHP字符串处理函数中,最为简单的几个函数,相关解释就不上了,直接看例子. strtolower函数.strtoupper函 ...
- Oracle数据库中调用Java类开发存储过程、函数的方法
Oracle数据库中调用Java类开发存储过程.函数的方法 时间:2014年12月24日 浏览:5538次 oracle数据库的开发非常灵活,不仅支持最基本的SQL,而且还提供了独有的PL/SQL, ...
- (转) C++中基类和派生类之间的同名函数的重载问题
下面有关派生类与基类中存在同名函数 fn: class A { public: void fn() {} void fn(int a) {} }; class B : public A { publi ...
- C++类中的静态成员变量与静态成员函数
最近一直看c++相关的项目,但总是会被c++类中的静态成员变量与静态成员函数的理解感觉很是模糊,不明白为什么类中要是用静态成员变量.于是在网上搜集了一些资料,自己再稍微总结下. 静态成员的概念: 静态 ...
- C++中的类继承(2)派生类的默认成员函数
在继承关系里面, 在派生类中如果没有显示定义这六个成员 函数, 编译系统则会默认合成这六个默认的成员函数. 构造函数. 调用关系先看一段代码: class Base { public : Base() ...
随机推荐
- SE_Work0_回顾与展望
项目 内容 课程:北航-2020-春-软件工程 博客园班级博客 要求:阅读推荐博客并回答问题 热身作业阅读部分要求 我在这个课程的目标是 提升团队管理及合作能力,开发一项满意的工程项目 这个作业在哪个 ...
- 服务治理演进剖析 & Service Mesh、 xDS核心原理梳理
基于XDS协议实现控制面板与数据面板通信分享 基于这段时间在同程艺龙基础架构部的蹲坑,聊一聊微服务治理的核心难点.历史演进.最新动态, 以上内容属自我思考,不代表同程艺龙技术水准.如理解有偏差.理解不 ...
- str.isdigit()可以判断变量是否为数字
字符串.isdigit()可以判断变量是否为数字 是则输出True 不是则输出False 好像只能字符串
- 使用alpine为基础镜像Q&A
作为go应用存在二进制文件却不能执行 明明镜像中有对应的二进制文件,但是执行时却提示 not found 或 no such file 或 standard_init_linux.go:211: ex ...
- MyBatis的Example如何按条件排序(Day_35)
MyBatis的Example如何按条件进行排序? 背景:有时我们在使用mybatis example 进行查询时,需要进行相应的业务排序.本博客以下图为例 @Override public List ...
- jquery 改变标签样式
jQuery改变标签的样式一般有3种 预置好class,然后通过jQuery改变元素的class名,使用的是addClass.removeClass 直接改变元素的css属性值,这种是通过添加styl ...
- springboot 搭配redis缓存
1.引入依赖 <dependencies> <dependency> <groupId>org.springframework.boot</groupId&g ...
- publicPath路径问题
output: { filename: "[name].js", path:path.resolve(__dirname,"build") } 如果没有指定pu ...
- 3 Python相对路径地址的的一个问题
构建程序xiaojie_test.py import os from xxx.yyy import test test() 同目录下构建一个目录xxx,并且目录中有/tmp/results/graph ...
- PyTorch 神经网络
PyTorch 神经网络 神经网络 神经网络可以通过 torch.nn 包来构建. 现在对于自动梯度(autograd)有一些了解,神经网络是基于自动梯度 (autograd)来定义一些模型.一个 n ...