Skip to content

Commit

Permalink
[rmkit] pack pixels into correct buffer format
Browse files Browse the repository at this point in the history
  • Loading branch information
okay committed May 27, 2024
1 parent 381ef9d commit 6e2aef8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/dithering_demo/main.cpy
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ class App : public IApp:
free(self.image.buffer)
filename = string(IMAGES_DIR) + "/" + filename
int channels
self.image.buffer = (uint32_t*)stbi_load(filename.c_str(), &self.image.w, &self.image.h, &channels, 1);
self.image.channels = 1
self.image.buffer = (uint32_t*)stbi_load(filename.c_str(), &self.image.w, &self.image.h, &channels, 4);
self.image.channels = 4
util::resize_image(image, self.image.w, self.image.h, 0)
undithered_bmp->image = self.image
dithered_bmp->image = self.image
Expand Down
13 changes: 13 additions & 0 deletions src/rmkit/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ struct rgb_color {
// 0(black) - 31(white)
constexpr remarkable_color gray32(int n)
{

if (sizeof(remarkable_color) >= 3) {
n = (n * 0xff / 32);
return (n << 16) | (n << 8) | n;
}

// The green channel should have six bits, but 31 is only five.
// Set the last green bit unless n is zero
// otherwise white is actually slightly pink
Expand All @@ -57,10 +63,17 @@ constexpr rgb_color to_rgb8(remarkable_color s)

constexpr float to_float(remarkable_color c)
{
if (sizeof(remarkable_color) >= 3) {
return ((c >> 16) & 0xff) * (0.21 / 0xff) // red
+ ((c >> 8) & 0xff) * (0.72 / 0xff) // green
+ ((c >> 0) & 0xff) * (0.07 / 0xff); // blue
}

// 0.21 R + 0.72 G + 0.07 B
return ((c >> 11) & 31) * (0.21 / 31) // red
+ ((c >> 5) & 63) * (0.72 / 63) // green
+ (c & 31) * (0.07 / 31); // blue

}

// 16-gray palette (BLACK, WHITE, and 14 shades of gray)
Expand Down
29 changes: 17 additions & 12 deletions src/rmkit/fb/fb.cpy
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,20 @@ namespace framebuffer:

do_dithering(self.fbmem, i+o_x, j+o_y, color, dither)

inline remarkable_color to_rgb565(char *src, int offset):
r := src[offset]
g := src[offset+1]
b := src[offset+2]
return (remarkable_color) (
((r & 0b11111000) << 8) |
((g & 0b11111100) << 3) |
((b & 0b11111000) >> 3)
);
inline remarkable_color pack_pixel(char *src, int offset):
#ifdef RMKIT_FBINK
r := src[offset]
g := src[offset+1]
b := src[offset+2]
uint32_t out
fbink_pack_pixel_rgba(r, g, b, 0xff, &out)
return (remarkable_color) out
#else
r := src[offset]
g := src[offset+1]
b := src[offset+2]
return (remarkable_color) (((r >> 3U) << 11U) | ((g >> 2U) << 5U) | (b >> 3U));
#endif

inline void grayscale_to_rgb32(uint8_t src, char *dst):
uint32_t color = (src * 0x00010101);
Expand Down Expand Up @@ -349,12 +354,12 @@ namespace framebuffer:
if image.channels == 4 && alpha:
// 4th bit is alpha -- if it's 0, skip drawing
if ((char*)src)[i*image.channels+3] != 0:
self._set_pixel(&ptr[i], i, j, to_rgb565((char *) src, i*image.channels))
self._set_pixel(&ptr[i], i, j, pack_pixel((char *) src, i*image.channels))
else if image.channels >= 3:
self._set_pixel(&ptr[i], i, j, to_rgb565((char *) src, i*image.channels))
self._set_pixel(&ptr[i], i, j, pack_pixel((char *) src, i*image.channels))
else if image.channels == 1:
grayscale_to_rgb32(src[i], src_val)
self._set_pixel(&ptr[i], i, j, to_rgb565(src_val, 0))
self._set_pixel(&ptr[i], i, j, pack_pixel(src_val, 0))
else:
self._set_pixel(&ptr[i], i, j, src[i])

Expand Down

0 comments on commit 6e2aef8

Please sign in to comment.