<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>.NET</title>
        <link>http://blogs.ugidotnet.org/leonardo/category/.NET.aspx</link>
        <description>.NET</description>
        <language>it</language>
        <copyright>Leonardo</copyright>
        <generator>Subtext Version 2.6.0.0</generator>
        <item>
            <title>Water in Wine with C#</title>
            <link>http://blogs.ugidotnet.org/leonardo/archive/2023/11/24/water-in-wine-csharp-array-cast.aspx</link>
            <description>&lt;div&gt;A common need when working with images is to convert arrays containing pixels from a type to another.&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;For example an uint[] to byte[] conversion is common, but very slow and memory intensive.&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;Here the fastest method I developed for real-time applications:&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;&lt;img src="/images/blogs_ugidotnet_org/leonardo/arrayCast.png" width="499" height="270" alt="" /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;The code is unsafe (additional testing recommended) and may not work with future CLR Runtimes, but it's pretty readable and fast.&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;In few words the code is changing the type of the array by overwriting the CLR Method Table and then resizing the array in a similiar way. A new array (of the desired type) pointing to the original one is then returned. The original data is never copied and this makes this method very fast to execute.&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;Here how to use the function CastArray:&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;&lt;img src="/images/blogs_ugidotnet_org/leonardo/arrayCastDemo.png" style="width: 368px; height: 80px;" alt="" /&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;An array containing a 4K 32 bit image can be casted with a great speedup compared to an Array copy with standard solutions such as Buffer.BlockCopy.&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;The drawback is that now we have overwritten the original array size and if we use the original array, we should manually ensure that we are not reading over it's end.&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div&gt;The byte[] is actually a reference array and changing it will change the uint[] (&lt;span class="HwtZe" lang="en"&gt;&lt;span class="jCAhz ChMk0b"&gt;&lt;span class="ryNqvb"&gt;and the opposite), but this can avoid further casts.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span class="HwtZe" lang="en"&gt;&lt;span class="jCAhz ChMk0b"&gt;&lt;span class="ryNqvb"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span class="HwtZe" lang="en"&gt;&lt;span class="jCAhz ChMk0b"&gt;&lt;span class="ryNqvb"&gt;Note: As I already done in the &lt;a href="https://electro-logic.blogspot.com"&gt;electro-logic&lt;/a&gt; blog, new posts starting from this one are only in &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="HwtZe" lang="en"&gt;&lt;span class="jCAhz ChMk0b"&gt;&lt;span class="ryNqvb"&gt;English &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="HwtZe" lang="en"&gt;&lt;span class="jCAhz ChMk0b"&gt;&lt;span class="ryNqvb"&gt;to enable more people to read them, but you can still contact me or leave a comment in Italian if you prefer.&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span class="HwtZe" lang="en"&gt;&lt;span class="jCAhz ChMk0b"&gt;&lt;span class="ryNqvb"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span class="HwtZe" lang="en"&gt;&lt;span class="jCAhz ChMk0b"&gt;&lt;span class="ryNqvb"&gt;That's all for today&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://blogs.ugidotnet.org/leonardo/aggbug/102638.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Leonardo</dc:creator>
            <guid>http://blogs.ugidotnet.org/leonardo/archive/2023/11/24/water-in-wine-csharp-array-cast.aspx</guid>
            <pubDate>Fri, 24 Nov 2023 21:51:16 GMT</pubDate>
            <comments>http://blogs.ugidotnet.org/leonardo/archive/2023/11/24/water-in-wine-csharp-array-cast.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blogs.ugidotnet.org/leonardo/comments/commentRss/102638.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.ugidotnet.org/leonardo/services/trackbacks/102638.aspx</trackback:ping>
        </item>
    </channel>
</rss>