📄 blitz_5.html
字号:
<HTML><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- Created on October, 14 2005 by texi2html 1.64 --><!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others.Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>Send bugs and suggestions to <texi2html@mathematik.uni-kl.de> --><HEAD><TITLE>Blitz++: Customised Arrays</TITLE><META NAME="description" CONTENT="Blitz++: Customised Arrays"><META NAME="keywords" CONTENT="Blitz++: Customised Arrays"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META NAME="Generator" CONTENT="texi2html 1.64"></HEAD><BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000"><A NAME="SEC121"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC120"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC122"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_6.html#SEC127"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_6.html#SEC127"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H1> 5. Multicomponent, complex, and user type arrays </H1><!--docid::SEC121::--><BLOCKQUOTE><TABLE BORDER=0 CELLSPACING=0> <TR><TD ALIGN="left" VALIGN="TOP"><A HREF="blitz_5.html#SEC122">5.1 Multicomponent and complex arrays</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Multicomponent Array's</TD></TR><TR><TD ALIGN="left" VALIGN="TOP"><A HREF="blitz_5.html#SEC126">5.2 Creating arrays of a user type</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Array's of your own type</TD></TR></TABLE></BLOCKQUOTE><P><A NAME="Array multi"></A><HR SIZE="6"><A NAME="SEC122"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC123"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC126"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 5.1 Multicomponent and complex arrays </H2><!--docid::SEC122::--><P>Multicomponent arrays have elements which are vectors. Examples of sucharrays are vector fields, colour images (which contain, say, RGB tuples),and multispectral images. Complex-valued arrays can also be regarded asmulticomponent arrays, since each element is a 2-tuple of real values.</P><P>Here are some examples of multicomponent arrays:</P><P><A NAME="IDX312"></A></P><P><TABLE><tr><td> </td><td class=example><pre>// A 3-dimensional array; each element is a length 3 vector of floatArray<TinyVector<float,3>,3> A; // A complex 2-dimensional arrayArray<complex<double>,2> B;// A 2-dimensional image containing RGB tuplesstruct RGB24 { unsigned char r, g, b;};Array<RGB24,2> C;</pre></td></tr></table></P><P><HR SIZE="6"><A NAME="SEC123"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC122"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC124"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC122"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC126"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H3> 5.1.1 Extracting components </H3><!--docid::SEC123::--><P><A NAME="IDX313"></A><A NAME="IDX314"></A></P><P>Blitz++ provides some special support for such arrays. The most importantis the ability to extract a single component. For example:</P><P><TABLE><tr><td> </td><td class=example><pre>Array<TinyVector<float,3>,2> A(128,128);Array<float,2> B = A.extractComponent(float(), 1, 3);B = 0;</pre></td></tr></table></P><P>The call to <CODE>extractComponent</CODE> returns an array of floats; this arrayis a view of the second component of each element of A. The arguments of<CODE>extractComponent</CODE> are: (1) the type of the component (in this example,float); (2) the component number to extract (numbered 0, 1, ... N-1); and(3) the number of components in the array.</P><P>This is a little bit messy, so Blitz++ provides a handy shortcut using<CODE>operator[]</CODE>:</P><P><TABLE><tr><td> </td><td class=example><pre>Array<TinyVector<float,3>,2> A(128,128);A[1] = 0;</pre></td></tr></table></P><P>The number inside the square brackets is the component number. However, forthis operation to work, Blitz++ has to already know how many componentsthere are, and what type they are. It knows this already for<CODE>TinyVector</CODE> and <CODE>complex<T></CODE>. If you use your own type, though,you will have to tell Blitz++ this information using the macro<CODE>BZ_DECLARE_MULTICOMPONENT_TYPE()</CODE>. This macro has three arguments:</P><P><A NAME="IDX315"></A></P><P><TABLE><tr><td> </td><td class=example><pre>BZ_DECLARE_MULTICOMPONENT_TYPE(T_element, T_componentType, numComponents)</pre></td></tr></table></P><P><CODE>T_element</CODE> is the element type of the array. <CODE>T_componentType</CODE>is the type of the components of that element. <CODE>numComponents</CODE> is thenumber of components in each element.</P><P>An example will clarify this. Suppose we wanted to make a colour image,stored in 24-bit HSV (hue-saturation-value) format. We can make a class<CODE>HSV24</CODE> which represents a single pixel:</P><P><A NAME="IDX316"></A></P><P><TABLE><tr><td> </td><td class=example><pre>#include <blitz/array.h>using namespace blitz;class HSV24 {public: // These constants will makes the code below cleaner; we can // refer to the components by name, rather than number. static const int hue=0, saturation=1, value=2; HSV24() { } HSV24(int hue, int saturation, int value) : h_(hue), s_(saturation), v_(value) { } // Some other stuff here, obviouslyprivate: unsigned char h_, s_, v_;};</pre></td></tr></table></P><P>Right after the class declaration, we will invoke the macro<CODE>BZ_DECLARE_MULTICOMPONENT_TYPE</CODE> to tell Blitz++ about HSV24:</P><P><TABLE><tr><td> </td><td class=example><pre>// HSV24 has 3 components of type unsigned charBZ_DECLARE_MULTICOMPONENT_TYPE(HSV24, unsigned char, 3);</pre></td></tr></table></P><P>Now we can create HSV images and modify the individual components:</P><P><TABLE><tr><td> </td><td class=example><pre>int main(){ Array<HSV24,2> A(128,128); // A 128x128 HSV image ... // Extract a greyscale version of the image Array<unsigned char,2> A_greyscale = A[HSV24::value]; // Bump up the saturation component to get a // pastel effect A[HSV24::saturation] *= 1.3; // Brighten up the middle of the image Range middle(32,96); A[HSV24::value](middle,middle) *= 1.2;}</pre></td></tr></table></P><P><HR SIZE="6"><A NAME="SEC124"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC123"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC125"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC125"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC122"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC126"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -