Forum Discussion

EnTerr's avatar
EnTerr
Roku Guru
12 years ago

Convert roByteArray -> roBitmap ?

For hours I have been pulling my hair (ok, metaphor; my hair is short so it was more of scratching/scalp massaging) trying to figure what's a way to efficiently display generated bitmap content on Roku and could not find it. Where "generated" means it is not some static image you can read from jpeg/png but changes dynamically and "efficient" is to signify fast - as magnitude, in the hundred times per second for small regions (say 1/100 of screen, so that's only a couple FPS in data throughput, not video). Poking that pixel by pixel won't work.

Seems to me that could be done well if there was a function to convert from roByteArray to roBitmap (or alike). There is already ifDraw2D.GetByteArray(x, y, w, h), which does roBitmap->roByteArray. I don't know whether the opposite direction should be createObject("roBitmap", {width: w, height: h, byteArray: ba}) or ifByteArray.toBitmap(...) or ifDraw2D.setByteArray(...). Well, the last one might be hairy, since other objects implement ifDraw2D and who knows if the assigned buffer can be replaced. Creating new bitmap given byte array should be pretty straightforward though - it's underlying to the already existing image file->bitmap loading.

Why do i need that, you ask. Well here is couple of ideas i have in mind to implement that won't work w/o it:

  • VNC client. VNC's RFB protocol is raster-based, it sends updates as rectangular sub-regions of the screen, in one of multiple encoding forms - raw, RLE, in 16x16 hextiles (or one of the newer compression++ methods but that's not important since negotiable). And that's the only way graphics is transferred, there are no vectors/text/sprites/alpha/lines/curves/arcs. Only bitmaps, those are matter of life and death for VNC.


  • A loupe (magnifying glass). This is more of a demo or test of the player abilities. Something that i did like twenty years ago, in college on PC/AT (6MHz 80286 CPU). The program was showing a moving circular-shaped magnifying glass over any image - by calculating properly the refraction by a semi-spherical lens (i.e. using Snell's law and refractive index of glass), including some "fisheye" effect (vs say dumb scaling combined with circular alpha cookie-cut). It's a special case of ray-tracing and sounds slow but i was able to come with some clever optimizations for the case so that it was working sufficiently fast: it was able to recalculate and redraw the loupe image multiple times per second, thus i made it animated with the lens bouncing around the screen. This was all written in high-level language (Turbo Pascal) poking directly in screen buffer, with no assembly nor graphic library (individual GetPixel/PutPixel too slow) - so i suspect i can get at least the same performance nowadays on Roku's BrS interpreter. If i can assemble bitmap's byte array, that is.

12 Replies