[Lazarus] Copy buffer of PWord to TMemoryStream

Sven Barth pascaldragon at googlemail.com
Sun Mar 24 20:44:50 CET 2013


On 24.03.2013 20:39, Leonardo M. Ramé wrote:
>>>> Now your lBufSize contains the number of pixels, so only Inc(I, 1)
>>>> should be correct. And for copying all data at once you now need to
>>>> use "lBufSize * SizeOf(Word)" instead. And the for-loop variant
>>>> would look like this:
>>>>
>>>> === code begin ===
>>>>
>>>> for i := 0 to lBufSize - 1 do
>>>>    lStream.Write(lBuffer[i], SizeOf(Word));
>>>>    // or
>>>>    lStream.WriteWord(lBuffer[i]);
>>>>
>>>> === code end ===
>>>>
>>>
>>> Yes, the code looks correct, but it still creates the wrong resulting
>>> image.
>>>
>>> If you look at the attachment, the upper image is the result of your
>>> last suggestion, while the lower image was created using the while loop
>>> I've shown.
>>>
>>> I'll re-check my drawing function, maybe your code is correct and the
>>> problem is on that function.
>>
>> I just noticed that you are still using "SizeOf(PWord)" in your
>> lStream.Write. This is wrong. You want to copy one Word element
>> (SizeOf(Word)=2) instead of two/four (depending on 32 vs 64 bit
>> SizeOf(PWord) is either 4 or 8). Which is also why your Inc(..., 2)
>> works as you are copying multiple entries at once.
>>
>
> Well, this is funny, after re-checking the code, I noted it works if I
> use this:
>
> (1)   for I := 0 to lBufSize - 1 do
>          lUnCompressed.Write(lTmpBuff[i], SizeOf(PWord));
>
> But, using SizeOf(Word) doesn't:
>
> (2)   for I := 0 to lBufSize - 1 do
>          lUnCompressed.Write(lTmpBuff[i], SizeOf(Word));
>
> What I would like to achieve is this:
>
> (3)   lUnCompressed.WriteBuffer(lTmpBuff^, lBufSize * SizeOf(Word));
>
> But using this, I get the same result of (2).
>

Maybe your problem is indeed in your drawing function. Because the usage 
of SizeOf(PWord) is definitely wrong if your lBuffer basically contains 
Word sized pixels after each other. You should currently even get 
different results depending on whether the application is 32 or 64 bit.

Regards,
Sven




More information about the Lazarus mailing list