Skip to content

Commit bea90df

Browse files
committed
JPEGDEC: Experimental 3bit/4bit palette no-dither.
1 parent d3a1a57 commit bea90df

File tree

6 files changed

+86
-11
lines changed

6 files changed

+86
-11
lines changed
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1-
set(OUTPUT_NAME inky_frame_day_planner)
1+
# Inky Frame 5.7"
2+
add_executable(
3+
inky_frame_day_planner
4+
inky_frame_day_planner.cpp
5+
)
6+
7+
# Pull in pico libraries that we need
8+
target_link_libraries(inky_frame_day_planner pico_stdlib inky_frame hardware_pwm hardware_spi hardware_i2c hardware_rtc fatfs sdcard pico_graphics)
29

10+
pico_enable_stdio_usb(inky_frame_day_planner 1)
11+
12+
# create map/bin/hex file etc.
13+
pico_add_extra_outputs(inky_frame_day_planner)
14+
15+
# Inky Frame 7.3"
316
add_executable(
4-
${OUTPUT_NAME}
17+
inky_frame_7_day_planner
518
inky_frame_day_planner.cpp
619
)
720

821
# Pull in pico libraries that we need
9-
target_link_libraries(${OUTPUT_NAME} pico_stdlib inky_frame hardware_pwm hardware_spi hardware_i2c hardware_rtc fatfs sdcard pico_graphics)
22+
target_link_libraries(inky_frame_7_day_planner pico_stdlib inky_frame_7 hardware_pwm hardware_spi hardware_i2c hardware_rtc fatfs sdcard pico_graphics)
1023

11-
pico_enable_stdio_usb(${OUTPUT_NAME} 1)
24+
pico_enable_stdio_usb(inky_frame_7_day_planner 1)
1225

1326
# create map/bin/hex file etc.
14-
pico_add_extra_outputs(${OUTPUT_NAME})
27+
pico_add_extra_outputs(inky_frame_7_day_planner)
28+
29+
target_compile_definitions(inky_frame_7_day_planner PUBLIC INKY_FRAME_7)
30+

examples/inky_frame/inky_frame_day_planner.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
#include <stdio.h>
55
#include "pico/stdlib.h"
66

7+
#ifdef INKY_FRAME_7
8+
#include "libraries/inky_frame_7/inky_frame_7.hpp"
9+
#else
710
#include "libraries/inky_frame/inky_frame.hpp"
11+
#endif
812

913
using namespace pimoroni;
1014

examples/inky_frame/inky_frame_jpeg_image.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,36 @@ int jpegdec_draw_callback(JPEGDRAW *draw) {
6969
return 1; // continue drawing
7070
}
7171

72+
// Draw to the nearest colour instead of dithering
73+
int jpegdec_draw_posterize_callback(JPEGDRAW *draw) {
74+
uint16_t *p = draw->pPixels;
75+
76+
int xo = jpeg_decode_options.x;
77+
int yo = jpeg_decode_options.y;
78+
79+
for(int y = 0; y < draw->iHeight; y++) {
80+
for(int x = 0; x < draw->iWidth; x++) {
81+
int sx = ((draw->x + x + xo) * jpeg_decode_options.w) / jpeg.getWidth();
82+
int sy = ((draw->y + y + yo) * jpeg_decode_options.h) / jpeg.getHeight();
83+
84+
if(xo + sx > 0 && xo + sx < inky.bounds.w && yo + sy > 0 && yo + sy < inky.bounds.h) {
85+
int closest = RGB(RGB565(*p)).closest(inky.palette, inky.palette_size);
86+
if (closest != -1) {
87+
inky.set_pen(closest);
88+
inky.set_pixel({xo + sx, yo + sy});
89+
} else {
90+
inky.set_pen(0);
91+
inky.set_pixel({xo + sx, yo + sy});
92+
}
93+
}
94+
95+
p++;
96+
}
97+
}
98+
99+
return 1; // continue drawing
100+
}
101+
72102
void draw_jpeg(std::string filename, int x, int y, int w, int h) {
73103

74104
// TODO: this is a horrible way to do it but we need to pass some parameters
@@ -85,7 +115,8 @@ void draw_jpeg(std::string filename, int x, int y, int w, int h) {
85115
jpegdec_close_callback,
86116
jpegdec_read_callback,
87117
jpegdec_seek_callback,
88-
jpegdec_draw_callback);
118+
jpegdec_draw_callback // Try jpegdec_draw_posterize_callback
119+
);
89120

90121
jpeg.setPixelType(RGB565_BIG_ENDIAN);
91122

@@ -134,7 +165,7 @@ int main() {
134165
}; // Wait for debugger
135166
}
136167

137-
filename = "butterfly-600x448.jpg";
168+
filename = "shutterstock_172537049.jpg";
138169

139170
//inky.led(InkyFrame::LED_E, 255);
140171
//sleep_ms(1000);

libraries/pico_graphics/pico_graphics.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ namespace pimoroni {
3232
void PicoGraphics::frame_convert(PenType type, conversion_callback_func callback) {};
3333
void PicoGraphics::sprite(void* data, const Point &sprite, const Point &dest, const int scale, const int transparent) {};
3434

35+
int PicoGraphics::get_palette_size() {return 0;}
36+
RGB* PicoGraphics::get_palette() {return nullptr;}
37+
3538
void PicoGraphics::set_dimensions(int width, int height) {
3639
bounds = clip = {0, 0, width, height};
3740
}

libraries/pico_graphics/pico_graphics.hpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,6 @@ namespace pimoroni {
178178
Rect clip;
179179
uint thickness = 1;
180180

181-
182-
183-
184181
typedef std::function<void(void *data, size_t length)> conversion_callback_func;
185182
typedef std::function<RGB565()> next_pixel_func;
186183
typedef std::function<RGB888()> next_pixel_func_rgb888;
@@ -233,6 +230,9 @@ namespace pimoroni {
233230
virtual void set_pixel_span(const Point &p, uint l) = 0;
234231
virtual void set_thickness(uint t) = 0;
235232

233+
virtual int get_palette_size();
234+
virtual RGB* get_palette();
235+
236236
virtual int create_pen(uint8_t r, uint8_t g, uint8_t b);
237237
virtual int create_pen_hsv(float h, float s, float v);
238238
virtual int update_pen(uint8_t i, uint8_t r, uint8_t g, uint8_t b);
@@ -344,6 +344,9 @@ namespace pimoroni {
344344
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
345345
void set_thickness(uint t) override {};
346346

347+
int get_palette_size() override {return palette_size;};
348+
RGB* get_palette() override {return palette;};
349+
347350
void set_pixel(const Point &p) override;
348351
void set_pixel_span(const Point &p, uint l) override;
349352
void get_dither_candidates(const RGB &col, const RGB *palette, size_t len, std::array<uint8_t, 16> &candidates);
@@ -375,6 +378,9 @@ namespace pimoroni {
375378
int create_pen_hsv(float h, float s, float v) override;
376379
int reset_pen(uint8_t i) override;
377380

381+
int get_palette_size() override {return palette_size;};
382+
RGB* get_palette() override {return palette;};
383+
378384
void set_pixel(const Point &p) override;
379385
void set_pixel_span(const Point &p, uint l) override;
380386
void get_dither_candidates(const RGB &col, const RGB *palette, size_t len, std::array<uint8_t, 16> &candidates);
@@ -406,6 +412,9 @@ namespace pimoroni {
406412
int create_pen_hsv(float h, float s, float v) override;
407413
int reset_pen(uint8_t i) override;
408414

415+
int get_palette_size() override {return palette_size;};
416+
RGB* get_palette() override {return palette;};
417+
409418
void set_pixel(const Point &p) override;
410419
void set_pixel_span(const Point &p, uint l) override;
411420
void get_dither_candidates(const RGB &col, const RGB *palette, size_t len, std::array<uint8_t, 16> &candidates);
@@ -542,6 +551,9 @@ namespace pimoroni {
542551
void set_pixel(const Point &p) override;
543552
void set_pixel_span(const Point &p, uint l) override;
544553

554+
int get_palette_size() override {return palette_size;};
555+
RGB* get_palette() override {return palette;};
556+
545557
void get_dither_candidates(const RGB &col, const RGB *palette, size_t len, std::array<uint8_t, 16> &candidates);
546558
void set_pixel_dither(const Point &p, const RGB &c) override;
547559

micropython/modules/jpegdec/jpegdec.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,16 @@ MICROPY_EVENT_POLL_HOOK
149149
|| current_graphics->pen_type == PicoGraphics::PEN_P4
150150
|| current_graphics->pen_type == PicoGraphics::PEN_3BIT
151151
|| current_graphics->pen_type == PicoGraphics::PEN_INKY7) {
152-
current_graphics->set_pixel_dither({pDraw->x + x, pDraw->y + y}, RGB((RGB565)pDraw->pPixels[i]));
152+
if (current_flags & FLAG_NO_DITHER) {
153+
int closest = RGB((RGB565)pDraw->pPixels[i]).closest(current_graphics->get_palette(), current_graphics->get_palette_size());
154+
if (closest == -1) {
155+
closest = 0;
156+
}
157+
current_graphics->set_pen(closest);
158+
current_graphics->pixel({pDraw->x + x, pDraw->y + y});
159+
} else {
160+
current_graphics->set_pixel_dither({pDraw->x + x, pDraw->y + y}, RGB((RGB565)pDraw->pPixels[i]));
161+
}
153162
} else {
154163
current_graphics->set_pen(pDraw->pPixels[i]);
155164
current_graphics->pixel({pDraw->x + x, pDraw->y + y});

0 commit comments

Comments
 (0)