<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Thank you very much, especially to:do: part. This improves performance of my code(other part) considerably!</div><div><br>Sent from my iPad</div><div><br>On Jun 12, 2014, at 7:12 PM, Bert Freudenberg &lt;<a href="mailto:bert@freudenbergs.de">bert@freudenbergs.de</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><div>Ah, endianness issue, isn't it fun! BitBlt really operates on 32-bit words, so 4 pixels of your 8-bit form are stuffed into one word. And they are big-endian by default. You can indicate little-endian forms by using a negative depth. So, if you use -8 for your form depth it works.</div><div><br></div><div>You are (probably) on an x86 processor, which is little-endian, so your byte array's first byte ends up in the least significant byte of the first word. Interpreting that as a big-endian word gives the pattern you noticed.</div><div><br></div><div>I should have mentioned that stuffing a byte array into a form is a hack (there is even a method Form&gt;&gt;hackBits:). It is a useful hack, see for example&nbsp;Bitmap&gt;&gt;asByteArray. But by looking at that method you see you need to pay attention to your CPU's endianness:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Smalltalk isLittleEndian</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>==&gt; true</div><div><br></div><div>Your code could use this:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Smalltalk isLittleEndian ifTrue: [form swapEndianness].</div><div><br></div><div>(or initialize its depth to 8 or -8 depending on endianness).</div><div><br></div><div>The "proper" way would be to use a "bitPoker", which is independent of endianness, but considerably slower:</div><div><br></div><div><div><div><div><span class="Apple-tab-span" style="white-space:pre">        </span>| w h bytes form poker |</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>w := 200.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>h := 200.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>bytes := ((1 to: w*h) collect: [:i | 255 ]) asByteArray.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>1 to: w do: [ :x |</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp; &nbsp;1 to: h do: [ :y |</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp; &nbsp; &nbsp; &nbsp;x = y ifTrue: [ bytes at: (w*(y - 1) + x) put: 0 ]</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp; &nbsp;]</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>].</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>form := ColorForm extent: w@h depth: 8.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>form colors: ((0 to: 255) collect: [:i | Color gray: i / 255]).</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>poker := BitBlt bitPokerToForm: form.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>0 to: w-1 do: [ :x |</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp; &nbsp;0 to: h-1 do: [ :y |</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp; &nbsp; &nbsp; &nbsp;poker pixelAt: x@y put: (bytes at: w * y + x + 1).</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp; &nbsp;]</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>].</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>form display</div></div></div></div><div><br></div><div>(Btw, don't put parens into "1 to: w do:". It works but defeats an important optimization).</div><div><br></div><div><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px; font-family: 'Lucida Grande'; font-size: 12px;"><div style="font-family: Helvetica;">- Bert -</div><br class="Apple-interchange-newline"></span></div><div><div>On 12.06.2014, at 02:32, Sungjin Chun &lt;<a href="mailto:chunsj@castlesoft.co.kr">chunsj@castlesoft.co.kr</a>&gt; wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><div><div><div>Thanks you for your answer, however there is something I missed here.<br><br></div>I've tried following code<br><br>| w h bytes form |<br>w := 200.<br>h := 200.<br>bytes := ((1 to: w*h) collect: [:i | 255 ]) asByteArray.<br>
(1 to: w) do: [ :x |<br>&nbsp;&nbsp;&nbsp; (1 to: h) do: [ :y |<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; x = y ifTrue: [ bytes at: (w*(y - 1) + x) put: 0 ]<br>&nbsp;&nbsp;&nbsp; ]<br>].<br>form := ColorForm extent: w@h depth: 8 bits: bytes.<br>form colors: ((0 to: 255) collect: [:i | Color gray: i / 255]).<br>
form display<br><br></div>What I expected is diagonal line but the result is not. What's wrong with my code?<br><br>Thank you in advance.<br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Wed, Jun 11, 2014 at 7:41 PM, Bert Freudenberg <span dir="ltr">&lt;<a href="mailto:bert@freudenbergs.de" target="_blank">bert@freudenbergs.de</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; position: static; z-index: auto;">
<div class="">On 11.06.2014, at 00:36, Sungjin Chun &lt;<a href="mailto:chunsj@castlesoft.co.kr">chunsj@castlesoft.co.kr</a>&gt; wrote:<br>
<br>
&gt; Hi,<br>
&gt;<br>
&gt; I want to draw gray scale bitmap using byte array which has pixel by pixel gray<br>
&gt; scale value as byte like this;<br>
&gt;<br>
&gt; [0 0 0 0 0 0 0 0<br>
&gt; &nbsp;16 23 255 78 12 12 12 12<br>
&gt; ...<br>
&gt; ...] (8x4 for example)<br>
&gt;<br>
&gt; I can draw this using Pen class pixel by pixel manner but this is very slow and<br>
&gt; I think there should be faster way of creating a Form using above byte array.<br>
<br>
</div>If the width is a multiple of 4, you can use the byte array directly as form bits:<br>
<br>
| w h bytes form |<br>
w := 100.<br>
h := 60.<br>
bytes := ((1 to: w*h) collect: [:i | 256 atRandom - 1]) asByteArray.<br>
form := ColorForm extent: w@h depth: 8 bits: bytes.<br>
form colors: ((0 to: 255) collect: [:i | Color gray: i / 255]).<br>
form display<br>
<br>
This would be the best and fastest way. If the width is not a multiple of 4 then perhaps you can add the necessary padding when creating the byte array. Otherwise, a very fast way is to use BitBlt to copy whole lines from the byte array to the form.<br>

<br>
But even creating a byte array with the right padding just using at:put: would be a lot faster than using a Pen.<br>
<span class="HOEnZb"><font color="#888888"><br>
- Bert -<br></font></span></blockquote></div></div></blockquote></div><br></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Beginners mailing list</span><br><span><a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a></span><br><span><a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a></span><br></div></blockquote></body></html>