Is there a better/faster way to perform the translation below?
PowerBASIC version
' Use this to setup the transparency level (changing the alpha channel)
LOCAL pBits AS BYTE PTR
LOCAL sT AS SINGLE
IF Alpha < 255 THEN
GetObject(hBM2, SIZEOF(bm), bm)
pBits = bm.bmBits: sT = MIN&(254, Alpha) / 255
FOR P = 1 TO (bm.bmWidth * bm.bmHeight)
@pBits[3] = @pBits[3] * sT: pBits += 4
NEXT
END IF
C++ version
// Use this to setup the transparency level (changing the alpha channel)
if (Alpha < 255) {
GetObject(hBM2, sizeof(bm), &bm);
float sT = min(254, Alpha) / 255;
BYTE A;
for (P = 0; P < bm.bmWidth * bm.bmHeight; P += 4) {
MoveMemory(&A, &bm.bmBits + P + 3, 1);
A = (BYTE) (A * sT);
MoveMemory(&bm.bmBits + P + 3, &A, 1);
}
}
I would guess something like this: (using a char* cast)
// Use this to setup the transparency level (changing the alpha channel)
if (Alpha < 255) {
GetObject(hBM2, sizeof(bm), &bm);
float sT = min(254, Alpha) / 255;
unsigned char* A=(unsigned char*) bm.bmBits;
int e=bm.bmWidth * bm.bmHeight * 4+3;
for (P = 3; P < e; P += 4) {
A[P]*= sT;
}
}
Charles
Charles
That's fine, thanks!
(i prefer to use BYTE* instead of char* for compatibilty with my PB code syntax) :)
...
Charles
In you code:
int e=bm.bmWidth * bm.bmHeight * 4+3;
I don't think you should add 3, but just: e=bm.bmWidth * bm.bmHeight * 4
...
One little gotcha I ran into Patrice to tuck away in the back of your mind, is that with for loops in C or C++, if there is a function for the terminating condition of the for loop, it will be called every iteration of the loop. I'm not sure about expressions such as you have there in your example, but I'd be wary of it. This is absolutely different than PowerBASIC, where a function will be called once, and that value used as the terminating condition.
I see in Charles example he moved the expression out of the for loop, and I suspect that might have occurred to him too.
Yes, that is why I precalculated e to hold the terminating value. 'e' is also displaced by 3, to catch the final alpha :)
Let's say:
bm.bmWidth = 2;
bm.bmHeight = 2;
long bmBitCount = bm.bmWidth * bm.bmHeight * 4
// now bmBitCount = 16
// thus our bm.bmBits and the related memory could be represented like this
+- bm.bmBits
|
00,01,02,03;04,05,06,07;08,09,10,11;12,13,14,15; <--- 16 bytes there
A1 A2 A3 A4
A1, A2, A3, A4 are the location of the Alpha byte within memory.
Thus the for iteration of P < 16 should be enough, isn't ?
other way we could write out of the bitmap memory...
???
Yes I see Patrice. If the boundary is set at 16 then you go 3 , 7, 11, 15.
The displacement of three would also work since the boundary is set to 19: Once you reach 19 you are out of the loop. Unlike PB iteration where the end value is inclusive.
Charles
thus that means we are both right, thanks :)
Added:
Don't forget that we could also use a <= test condition, to mimic the PB loop bound, if we start at 1 instead of 0.
...
Yes, I adopted these expressions as an enhancement to OxygenBasic: < is particularly useful when working with an index base 0 and an end count
c=2
for i=0 to < c
print i 'twice
next