Skip to content

Commit 6e2aef8

Browse files
author
okay
committed
[rmkit] pack pixels into correct buffer format
1 parent 381ef9d commit 6e2aef8

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/dithering_demo/main.cpy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ class App : public IApp:
206206
free(self.image.buffer)
207207
filename = string(IMAGES_DIR) + "/" + filename
208208
int channels
209-
self.image.buffer = (uint32_t*)stbi_load(filename.c_str(), &self.image.w, &self.image.h, &channels, 1);
210-
self.image.channels = 1
209+
self.image.buffer = (uint32_t*)stbi_load(filename.c_str(), &self.image.w, &self.image.h, &channels, 4);
210+
self.image.channels = 4
211211
util::resize_image(image, self.image.w, self.image.h, 0)
212212
undithered_bmp->image = self.image
213213
dithered_bmp->image = self.image

src/rmkit/color.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ struct rgb_color {
3131
// 0(black) - 31(white)
3232
constexpr remarkable_color gray32(int n)
3333
{
34+
35+
if (sizeof(remarkable_color) >= 3) {
36+
n = (n * 0xff / 32);
37+
return (n << 16) | (n << 8) | n;
38+
}
39+
3440
// The green channel should have six bits, but 31 is only five.
3541
// Set the last green bit unless n is zero
3642
// otherwise white is actually slightly pink
@@ -57,10 +63,17 @@ constexpr rgb_color to_rgb8(remarkable_color s)
5763

5864
constexpr float to_float(remarkable_color c)
5965
{
66+
if (sizeof(remarkable_color) >= 3) {
67+
return ((c >> 16) & 0xff) * (0.21 / 0xff) // red
68+
+ ((c >> 8) & 0xff) * (0.72 / 0xff) // green
69+
+ ((c >> 0) & 0xff) * (0.07 / 0xff); // blue
70+
}
71+
6072
// 0.21 R + 0.72 G + 0.07 B
6173
return ((c >> 11) & 31) * (0.21 / 31) // red
6274
+ ((c >> 5) & 63) * (0.72 / 63) // green
6375
+ (c & 31) * (0.07 / 31); // blue
76+
6477
}
6578

6679
// 16-gray palette (BLACK, WHITE, and 14 shades of gray)

src/rmkit/fb/fb.cpy

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,20 @@ namespace framebuffer:
295295

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

298-
inline remarkable_color to_rgb565(char *src, int offset):
299-
r := src[offset]
300-
g := src[offset+1]
301-
b := src[offset+2]
302-
return (remarkable_color) (
303-
((r & 0b11111000) << 8) |
304-
((g & 0b11111100) << 3) |
305-
((b & 0b11111000) >> 3)
306-
);
298+
inline remarkable_color pack_pixel(char *src, int offset):
299+
#ifdef RMKIT_FBINK
300+
r := src[offset]
301+
g := src[offset+1]
302+
b := src[offset+2]
303+
uint32_t out
304+
fbink_pack_pixel_rgba(r, g, b, 0xff, &out)
305+
return (remarkable_color) out
306+
#else
307+
r := src[offset]
308+
g := src[offset+1]
309+
b := src[offset+2]
310+
return (remarkable_color) (((r >> 3U) << 11U) | ((g >> 2U) << 5U) | (b >> 3U));
311+
#endif
307312

308313
inline void grayscale_to_rgb32(uint8_t src, char *dst):
309314
uint32_t color = (src * 0x00010101);
@@ -349,12 +354,12 @@ namespace framebuffer:
349354
if image.channels == 4 && alpha:
350355
// 4th bit is alpha -- if it's 0, skip drawing
351356
if ((char*)src)[i*image.channels+3] != 0:
352-
self._set_pixel(&ptr[i], i, j, to_rgb565((char *) src, i*image.channels))
357+
self._set_pixel(&ptr[i], i, j, pack_pixel((char *) src, i*image.channels))
353358
else if image.channels >= 3:
354-
self._set_pixel(&ptr[i], i, j, to_rgb565((char *) src, i*image.channels))
359+
self._set_pixel(&ptr[i], i, j, pack_pixel((char *) src, i*image.channels))
355360
else if image.channels == 1:
356361
grayscale_to_rgb32(src[i], src_val)
357-
self._set_pixel(&ptr[i], i, j, to_rgb565(src_val, 0))
362+
self._set_pixel(&ptr[i], i, j, pack_pixel(src_val, 0))
358363
else:
359364
self._set_pixel(&ptr[i], i, j, src[i])
360365

0 commit comments

Comments
 (0)