Iterate through a RGBA image, ignoring any fully transparent pixels, and counting up each of the 'strips' of non-transparent pixels. It is supposed to encode the image into an allocated block of memory (32 bit RGBA elements).
The data in the encoded block of memory would be organized like this:
Every 'strip' would begin with a 'fake' RGBA element which would have:
... 'R' = x location
... 'G' = y location
... 'B' = length of strip (number of pixels)
... 'A' = 00 (fully transparent)
the R & G (x & y locations) are offsets to 'where' this strip is located in the image
so, in this example 'image', I am using letters to denote RGBA elements (lets say 'T' = fully transparent, 'Q' = some color, 'Z' = some color):
TTTTTTTTTT <--- 10 pixels of transparent
TQQZQQTTTT <--- 10 pixels, first pixel transparent, last 4 pixels transparent
QZZZZZQQQT <--- 10 pixels, only last one Transparent
TQZTTTQTTZ <--- 10 pixels, some Trans, some not...
This example image would be encoded like this into the buffer (each 'fake' RGBA pixel will be a red number in this example, with a key at the end):
buffer = 1QQZQQ2QZZZZZQQQ3QZ4Q5Z6
Fake RGBA pixels (1-6) look like this:
1: R=1,G=1,B=5,A=0 meaning 'next strip starts at 1,1 and has 5 pixels'
2: R=0,G=2,B=9,A=0 meaning 'next strip starts at 0,2 and has 9 pixels'
3: R=1,G=3,B=2,A=0 meaning 'next strip starts at 1,3 and has 2 pixels'
4: R=6,G=3,B=1,A=0 meaning 'next strip starts at 6,3 and has 1 pixel'
5: R=9,G=3,B=1,A=0 meaning 'next strip starts at 9,3 and has 1 pixel'
6: R=255,G=255,B=255,A=0 meaning 'end of image'
there is ONE special case though: when a row 'ends' with a non-trans pixel and the next row begins with a non-trans pixel, then make sure to break this up into 2 strips, not one continuous one. This special case can occur because of the way I am iterating through the image and would not be an issue if I used a different method.
here is my code, forgive me:
Code: Select all
Dim SS1_buffer As ulongPtr = CAllocate(ImageWidth*ImageHeight,4)
'-------------------------------------------------------------------------------
sub EncodeSpeedSprite_Ver1(img as any ptr, buffer as any ptr)
' This version just iterates through the image, copying over the RGBA values to the buffer,
' EXCEPT if the value has an Alpha of '0' (totally transparent) in which case it finds the
' next non-transparent pixel and saves this location in the R & G (x & y respectively) of
' the copied over transparent pixel... basically RLE's only the transparent pixels...
'
' MUST CAllocate enough space into buffer before calling!
'
dim as ulong buf_index = 0, tranny = 0, reggy = 0, last_tranny = 0
dim as ulong TempColor
For y As ulong = 0 To ImageHeight - 1
Dim row As ulong Ptr = ImageAddress + y * ImagePitch
For x As ulong = 0 To ImageWidth - 1
TempColor = row[x]
if RGBA_A(TempColor) = 0 then
' if transparent then...count it
tranny += 1
if tranny = 1 then ' .. if first tranny then stop counting reggy and save it!
' --- hopefully it is grabbing 4 bytes (32 bit) value here...???
TempColor = buffer[last_tranny]
' this next line assumes that reggy is less than 255 - all the pixels on same line...
buffer[last_tranny] = rgba(RGB_R(TempColor),RGB_G(TempColor),reggy,0)
reggy = 0
end if
else
' if NOT transparent, then....
if tranny > 0 then ' if currently counting transparent pixels...
' then ran into a regular pixel... so make the Transparent coded pixel...
buf_index += 1
last_tranny = buf_index
buffer[buf_index] = rgba(x,y,0,0)
tranny = 0
end if
' just a regular pixel color, so save it into buffer
buf_index += 1
reggy += 1
buffer[buf_index] = TempColor
end if
next x
if reggy > 0 then 'was still accumulating regular pixels when hit end of line.
' so make the last trans pixel say only reggy amount of pixels and make new trans pix for next line...
TempColor = buffer[last_tranny] '--- hopefully it is grabbing 4 bytes (32 bit) value here...???
' this next line assumes that reggy is less than 255 - all the pixels on same line...
buffer[last_tranny] = rgba(RGB_R(TempColor),RGB_G(TempColor),reggy,0)
reggy = 0
dim as ulong hold_y = y+1
if hold_y < ImageHeight then
buf_index += 1
buffer[buf_index] = rgba(0,y+1,0,0) ' new trans pixel pointing to beginning of next line
last_tranny = buf_index
end if
end if
next y
' put in 'end of sprite' delimiter...
buf_index += 1
buffer[buf_index] = rgba(255,255,255,0) ' no other color in buffer will be transparent white...
end sub
Code: Select all
TempColor = buffer[last_tranny]