- BYTE arithmetic

From: [at]} <Wojtek>
Date: Thu, 22 Jun 2006 11:00:01 -0400 (EDT)

----boundary-LibPST-iamunique-1201886571_-_-
Content-type: text/plain

On Thu, 22 Jun 2006, Campbell, Robert D (SELEX) (UK Edinburgh) wrote:

> I was updating some data to be used as a Windows bitmap.
>
> Problem: The BlackBox Ports.Color format is different to the Windows
> 32-bit bitmap format, so every pixel had to be changed as follows:

Not being an English speaker I would have said "is different from the
Windows format". It is good to learn that I would be wrong....

There is a recommendation to avoid SYSTEM wherever it is practical
not to use it.

Why are you not using BITS? It is a standard function also mentioned
somewhere in those 35 pages. Is SYSTEM.VAL faster?

Here is an example from one of my own programs. I am manipulating
the MSB and LSB parts of a word and I am not using SYSTEM. The code
is taking care of the 2-s complement "extension bits" which would
get in the way in case of the negative numbers.

  In a low-level module which imports SYSTEM I declare the following.

  VAR buffer *: ARRAY [untagged] 65636 OF BYTE; (*requires SYSTEM *)

 In a high-level module which avoids SYSTEM I am still able to do
 bit manipulations as follows:

  (*Combine MSB and LSB into one integer. Cut sign extension bits,
    if LSB has the highest bit set.*)

  VAR
     lsb,msb, data: INTEGER;
  msb := ORD (BITS (ASH (LowLevel.buffer [0], 8)) * {8..15});
  lsb := ORD (BITS (LowLevel.buffer [1]) * {0..7});
  data := msb + lsb; (*performs (MSB << 8) + LSB*)

Wojtek
             ---- end of my message --------

>
> PROCEDURE Shuffle (c : Ports.Color) : Ports.Color;
> TYPE
> IntArray = ARRAY SIZE (INTEGER) OF BYTE;
> VAR
> b : BYTE;
> a : IntArray;
> BEGIN
> a := SYSTEM.VAL (IntArray, c);
> b := a [0]; a [0] := a [2]; a [2] := b;
> RETURN SYSTEM.VAL (Ports.Color, a)
> END Shuffle;
>
>
> I also wanted to 'invert' some colours, which is why I tried to negate
> the relevant bytes. My second attempt is:
>
> PROCEDURE Invert (c : Ports.Color) : Ports.Color;
> BEGIN
> RETURN SYSTEM.VAL (Ports.Color, SYSTEM.VAL (SET, c) / {0 .. 23})
> END Invert;
>
>
> I can now simply (!) call the Windows bitmap function with arrays of
> colour data by:
>
> TYPE
> Pixel = POINTER TO ARRAY OF ARRAY OF Ports.Color;
>
> BitMap = POINTER TO RECORD
> bmiHeader : WinApi.BITMAPINFOHEADER;
> pixel : Pixel
> END;
> ...
> res := WinApi.StretchDIBits (hdc, xOff, yOff, wid, hgt,
> 0, 0, bitMap.bmiHeader.biWidth,
> bitMap.bmiHeader.biHeight,
> SYSTEM.ADR (bitMap.pixel^),
> SYSTEM.VAL (WinApi.PtrBITMAPINFO,
> bitMap),
> WinApi.DIB_RGB_COLORS, WinApi.SRCCOPY);
>
>
> Maybe there are better ways of writing the Shuffle & Invert routines!
>
> Thanks
>
> Robert
>
>
> PS - I know that for some readers English is not their first language,
> so I shall explain that the 35 page comment is meant to be sarcastic,
> and I am aware that some languages have an even larger reference manual.

--- BlackBox
--- send subject HELP or UNSUBSCRIBE to blackbox{([at]})nowhere.xy



----boundary-LibPST-iamunique-1201886571_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"

e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRleHQgXGRlZmYwe1xmb250dGJsDQp7XGYwXGZz
d2lzcyBBcmlhbDt9DQp7XGYxXGZtb2Rlcm4gQ291cmllciBOZXc7fQ0Ke1xmMlxmbmlsXGZjaGFy
c2V0MiBTeW1ib2w7fQ0Ke1xmM1xmbW9kZXJuXGZjaGFyc2V0MCBDb3VyaWVyIE5ldzt9fQ0Ke1xj
b2xvcnRibFxyZWQwXGdyZWVuMFxibHVlMDtccmVkMFxncmVlbjBcYmx1ZTI1NTt9DQpcdWMxXHBh
cmRccGxhaW5cZGVmdGFiMzYwIFxmMFxmczIwIE9uIFRodSwgMjIgSnVuIDIwMDYsIENhbXBiZWxs
LCBSb2JlcnQgRCAoU0VMRVgpIChVSyBFZGluYnVyZ2gpIHdyb3RlOlxwYXINClxwYXINCj4gSSB3
YXMgdXBkYXRpbmcg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Bhcg0KPlxwYXINCj4gVGhhbmtzXHBhcg0KPlxw
YXINCj4gUm9iZXJ0XHBhcg0KPlxwYXINCj5ccGFyDQo+IFBTIC0gSSBrbm93IHRoYXQgZm9yIHNv
bWUgcmVhZGVycyBFbmdsaXNoIGlzIG5vdCB0aGVpciBmaXJzdCBsYW5ndWFnZSxccGFyDQo+IHNv
IEkgc2hhbGwgZXhwbGFpbiB0aGF0IHRoZSAzNSBwYWdlIGNvbW1lbnQgaXMgbWVhbnQgdG8gYmUg
c2FyY2FzdGljLFxwYXINCj4gYW5kIEkgYW0gYXdhcmUgdGhhdCBzb21lIGxhbmd1YWdlcyBoYXZl
IGFuIGV2ZW4gbGFyZ2VyIHJlZmVyZW5jZSBtYW51YWwuXHBhcg0KXHBhcg0KLS0tIEJsYWNrQm94
XHBhcg0KLS0tIHNlbmQgc3ViamVjdCBIRUxQIG9yIFVOU1VCU0NSSUJFIHRvIGJsYWNrYm94QG9i
ZXJvbi5jaFxwYXINClxwYXINCn0=


----boundary-LibPST-iamunique-1201886571_-_---
Received on Thu Jun 22 2006 - 17:00:01 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:27:50 UTC