diff --git a/demos/demo2_app.c b/demos/demo2_app.c index 892e45551..eee465b9b 100644 --- a/demos/demo2_app.c +++ b/demos/demo2_app.c @@ -44,7 +44,7 @@ static ret_t on_paint_9patch(void* ctx, event_t* e) { return RET_OK; } -static ret_t on_paint_scale(void* ctx, event_t* e) { +static ret_t on_paint_default(void* ctx, event_t* e) { rect_t s; rect_t r; bitmap_t img; @@ -74,7 +74,7 @@ static ret_t on_paint_line(void* ctx, event_t* e) { canvas_t* c = evt->c; canvas_set_fill_color(c, fill_color); canvas_set_stroke_color(c, stroke_color); - + canvas_set_stroke_color(c, stroke_color); canvas_draw_line(c, 10, 10, 100, 300); @@ -90,10 +90,10 @@ static ret_t on_paint_3patch_h(void* ctx, event_t* e) { rect_init(r, 10, 10, 100, 60); canvas_draw_image_3patch_h(c, &img, &r); - + rect_init(r, 10, 80, 10, 60); canvas_draw_image_3patch_h(c, &img, &r); - + rect_init(r, 10, 160, 300, 50); canvas_draw_image_3patch_h(c, &img, &r); @@ -112,14 +112,14 @@ static ret_t on_paint_3patch_v(void* ctx, event_t* e) { rect_init(r, 10, 10, 60, 100); canvas_draw_image_3patch_h(c, &img, &r); - + rect_init(r, 80, 10, 60, 10); canvas_draw_image_3patch_h(c, &img, &r); - + rect_init(r, 160, 10, 50, 300); canvas_draw_image_3patch_h(c, &img, &r); - rect_init(r, 220, 10, 20, 300); + rect_init(r, 220, 10, 20, 300); canvas_draw_image_3patch_h(c, &img, &r); return RET_OK; @@ -134,11 +134,11 @@ static ret_t on_paint_tile_h(void* ctx, event_t* e) { rect_init(r, 10, 10, 10, img.h); canvas_draw_image_tile_h(c, &img, &r); - + rect_init(r, 10, 60, img.w, img.h); canvas_draw_image_tile_h(c, &img, &r); - - rect_init(r, 10, 160, img.w*2, img.h*2); + + rect_init(r, 10, 160, img.w * 2, img.h * 2); canvas_draw_image_tile_h(c, &img, &r); return RET_OK; @@ -156,8 +156,8 @@ static ret_t on_paint_tile_v(void* ctx, event_t* e) { rect_init(r, 90, 10, img.w, img.h); canvas_draw_image_tile_v(c, &img, &r); - - rect_init(r, 160, 10, img.w*2, img.h*2); + + rect_init(r, 160, 10, img.w * 2, img.h * 2); canvas_draw_image_tile_v(c, &img, &r); return RET_OK; @@ -170,23 +170,99 @@ static ret_t on_paint_tile(void* ctx, event_t* e) { canvas_t* c = evt->c; image_manager_load(default_im(), "btn_blue_n", &img); - rect_init(r, 10, 10, img.w/2, img.h/2); + rect_init(r, 10, 10, img.w / 2, img.h / 2); canvas_draw_image_tile(c, &img, &r); rect_init(r, 50, 50, img.w, img.h); canvas_draw_image_tile(c, &img, &r); - - rect_init(r, 100, 100, img.w*2+10, img.h*2+10); + + rect_init(r, 100, 100, img.w * 2 + 10, img.h * 2 + 10); canvas_draw_image_tile(c, &img, &r); return RET_OK; } +static ret_t on_paint_scale_x(void* ctx, event_t* e) { + rect_t r; + bitmap_t img; + paint_event_t* evt = (paint_event_t*)e; + canvas_t* c = evt->c; + image_manager_load(default_im(), "btn_blue_n", &img); + + rect_init(r, 10, 10, img.w, img.h / 2); + canvas_draw_image_scale_x(c, &img, &r); + + rect_init(r, 50, 50, img.w, img.h); + canvas_draw_image_scale_x(c, &img, &r); + + rect_init(r, 100, 100, img.w * 2, img.h); + canvas_draw_image_scale_x(c, &img, &r); + + return RET_OK; +} + +static ret_t on_paint_scale_y(void* ctx, event_t* e) { + rect_t r; + bitmap_t img; + paint_event_t* evt = (paint_event_t*)e; + canvas_t* c = evt->c; + image_manager_load(default_im(), "btn_blue_n", &img); + + rect_init(r, 10, 10, img.w / 2, img.h); + canvas_draw_image_scale_y(c, &img, &r); + + rect_init(r, 50, 50, img.w, img.h); + canvas_draw_image_scale_y(c, &img, &r); + + rect_init(r, 100, 100, img.w, img.h * 2); + canvas_draw_image_scale_y(c, &img, &r); + + return RET_OK; +} + +static ret_t on_paint_scale(void* ctx, event_t* e) { + rect_t r; + bitmap_t img; + paint_event_t* evt = (paint_event_t*)e; + canvas_t* c = evt->c; + image_manager_load(default_im(), "btn_blue_n", &img); + + rect_init(r, 10, 10, img.w / 2, img.h); + canvas_draw_image_scale(c, &img, &r); + + rect_init(r, 50, 50, img.w, img.h); + canvas_draw_image_scale(c, &img, &r); + + rect_init(r, 100, 100, img.w, img.h * 2); + canvas_draw_image_scale(c, &img, &r); + + return RET_OK; +} + +static ret_t on_paint_center(void* ctx, event_t* e) { + rect_t r; + bitmap_t img; + paint_event_t* evt = (paint_event_t*)e; + canvas_t* c = evt->c; + image_manager_load(default_im(), "btn_blue_n", &img); + + rect_init(r, 10, 10, img.w / 2, img.h); + canvas_draw_image_center(c, &img, &r); + + rect_init(r, 50, 50, img.w, img.h); + canvas_draw_image_center(c, &img, &r); + + rect_init(r, 100, 100, img.w, img.h * 2); + canvas_draw_image_center(c, &img, &r); + + return RET_OK; +} + ret_t application_init() { widget_t* win = window_create(NULL, 0, 0, 0, 0); widget_t* canvas = group_box_create(win, 0, 0, win->w, win->h); - widget_on(canvas, EVT_PAINT, on_paint_tile, NULL); + widget_on(canvas, EVT_PAINT, on_paint_center, NULL); return RET_OK; } diff --git a/src/base/canvas.c b/src/base/canvas.c index 771ba7af4..3a7ce9729 100644 --- a/src/base/canvas.c +++ b/src/base/canvas.c @@ -513,15 +513,15 @@ ret_t canvas_draw_image_tile(canvas_t* c, bitmap_t* img, rect_t* dst) { d = *dst; - while(y < dst->h) { - h = ftk_min(img->h, dst->h-y); - while(x < dst->w) { - w = ftk_min(img->w, dst->w-x); + while (y < dst->h) { + h = ftk_min(img->h, dst->h - y); + while (x < dst->w) { + w = ftk_min(img->w, dst->w - x); s.w = w; s.h = h; - d.x = x+dst->x; - d.y = y+dst->y; + d.x = x + dst->x; + d.y = y + dst->y; d.w = w; d.h = h; canvas_draw_image(c, img, &s, &d); @@ -548,8 +548,8 @@ ret_t canvas_draw_image_tile_h(canvas_t* c, bitmap_t* img, rect_t* dst) { d = *dst; - while(x < dst->w) { - w = ftk_min(img->w, dst->w-x); + while (x < dst->w) { + w = ftk_min(img->w, dst->w - x); s.w = w; d.x = x; d.w = w; @@ -574,8 +574,8 @@ ret_t canvas_draw_image_tile_v(canvas_t* c, bitmap_t* img, rect_t* dst) { d = *dst; - while(y < dst->h) { - h = ftk_min(img->h, dst->h-y); + while (y < dst->h) { + h = ftk_min(img->h, dst->h - y); s.h = h; d.y = y; d.h = h; @@ -605,7 +605,7 @@ ret_t canvas_draw_image_3patch_v(canvas_t* c, bitmap_t* img, rect_t* dst) { canvas_translate(c, dst->x, dst->y); - if(dst_h <= img_h) { + if (dst_h <= img_h) { rect_init(s, 0, 0, img_w, img_h); rect_init(d, 0, 0, dst_w, dst_h); canvas_draw_image(c, img, &s, &d); @@ -628,8 +628,8 @@ ret_t canvas_draw_image_3patch_v(canvas_t* c, bitmap_t* img, rect_t* dst) { canvas_draw_image(c, img, &s, &d); /*bottom*/ - rect_init(s, 0, img_h-h, img_w, h); - rect_init(d, 0, dst_h-h, dst_w, h); + rect_init(s, 0, img_h - h, img_w, h); + rect_init(d, 0, dst_h - h, dst_w, h); canvas_draw_image(c, img, &s, &d); canvas_untranslate(c, dst->x, dst->y); @@ -656,7 +656,7 @@ ret_t canvas_draw_image_3patch_h(canvas_t* c, bitmap_t* img, rect_t* dst) { canvas_translate(c, dst->x, dst->y); - if(dst_w <= img_w) { + if (dst_w <= img_w) { rect_init(s, 0, 0, img_w, img_h); rect_init(d, 0, 0, dst_w, dst_h); canvas_draw_image(c, img, &s, &d); @@ -674,13 +674,13 @@ ret_t canvas_draw_image_3patch_h(canvas_t* c, bitmap_t* img, rect_t* dst) { canvas_draw_image(c, img, &s, &d); /*center*/ - rect_init(s, w, 0, img_w-2*w, img_h); + rect_init(s, w, 0, img_w - 2 * w, img_h); rect_init(d, w, 0, w_w, dst_h); canvas_draw_image(c, img, &s, &d); /*right*/ - rect_init(s, img_w-w, 0, w, img_h); - rect_init(d, dst_w-w, 0, w, dst_h); + rect_init(s, img_w - w, 0, w, img_h); + rect_init(d, dst_w - w, 0, w, dst_h); canvas_draw_image(c, img, &s, &d); canvas_untranslate(c, dst->x, dst->y); @@ -808,3 +808,94 @@ ret_t canvas_test_paint(canvas_t* c, bool_t pressed, xy_t x, xy_t y) { return RET_OK; } + +ret_t canvas_draw_image_scale_x(canvas_t* c, bitmap_t* img, rect_t* dst) { + rect_t s; + rect_t d; + wh_t src_h = 0; + wh_t dst_h = 0; + float scale = 0; + return_value_if_fail(c != NULL && img != NULL && dst != NULL, RET_BAD_PARAMS); + + scale = (float)(dst->w) / img->w; + dst_h = ftk_min(img->h * scale, dst->h); + src_h = ftk_min(img->h, dst_h / scale); + + s.x = 0; + s.y = 0; + s.w = img->w; + s.h = src_h; + + d = *dst; + d.h = dst_h; + + return canvas_draw_image(c, img, &s, &d); +} + +ret_t canvas_draw_image_scale_y(canvas_t* c, bitmap_t* img, rect_t* dst) { + rect_t s; + rect_t d; + wh_t src_w = 0; + wh_t dst_w = 0; + float scale = 0; + return_value_if_fail(c != NULL && img != NULL && dst != NULL, RET_BAD_PARAMS); + + scale = (float)(dst->h) / img->h; + dst_w = ftk_min(img->w * scale, dst->w); + src_w = ftk_min(img->w, dst_w / scale); + + s.x = 0; + s.y = 0; + s.h = img->h; + s.w = src_w; + + d = *dst; + d.w = dst_w; + + return canvas_draw_image(c, img, &s, &d); +} + +ret_t canvas_draw_image_scale(canvas_t* c, bitmap_t* img, rect_t* dst) { + rect_t s; + rect_t d; + wh_t dst_w = 0; + float scale = 0; + float scalex = 0; + float scaley = 0; + return_value_if_fail(c != NULL && img != NULL && dst != NULL, RET_BAD_PARAMS); + + s.x = 0; + s.y = 0; + s.h = img->h; + s.w = img->w; + + scalex = (float)(dst->w) / img->w; + scaley = (float)(dst->h) / img->h; + scale = ftk_min(scalex, scaley); + + d.w = img->w * scale; + d.h = img->h * scale; + d.x = dst->x + ((dst->w - d.w) >> 1); + d.y = dst->y + ((dst->h - d.h) >> 1); + + return canvas_draw_image(c, img, &s, &d); +} + +ret_t canvas_draw_image_center(canvas_t* c, bitmap_t* img, rect_t* dst) { + rect_t s; + rect_t d; + return_value_if_fail(c != NULL && img != NULL && dst != NULL, RET_BAD_PARAMS); + + s.x = 0; + s.y = 0; + s.h = img->h; + s.w = img->w; + + d = *dst; + d.x = dst->x + ((dst->w - s.w) >> 1); + d.y = dst->y + ((dst->h - s.h) >> 1); + d.w = s.w; + d.h = s.h; + + return canvas_draw_image(c, img, &s, &d); +} diff --git a/src/base/canvas.h b/src/base/canvas.h index a42321b01..cbde799ad 100644 --- a/src/base/canvas.h +++ b/src/base/canvas.h @@ -75,14 +75,19 @@ ret_t canvas_stroke_rect(canvas_t* c, xy_t x, xy_t y, wh_t w, wh_t h); ret_t canvas_draw_char(canvas_t* c, wchar_t chr, xy_t x, xy_t y); ret_t canvas_draw_text(canvas_t* c, wchar_t* str, int32_t nr, xy_t x, xy_t y); ret_t canvas_draw_image(canvas_t* c, bitmap_t* img, rect_t* src, rect_t* dst); + ret_t canvas_draw_image_at(canvas_t* c, bitmap_t* img, xy_t x, xy_t y); + +ret_t canvas_draw_image_center(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_3patch_h(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_3patch_v(canvas_t* c, bitmap_t* img, rect_t* dst); +ret_t canvas_draw_image_9patch(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_tile(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_tile_h(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_tile_v(canvas_t* c, bitmap_t* img, rect_t* dst); - -ret_t canvas_draw_image_9patch(canvas_t* c, bitmap_t* img, rect_t* dst); +ret_t canvas_draw_image_scale(canvas_t* c, bitmap_t* img, rect_t* dst); +ret_t canvas_draw_image_scale_x(canvas_t* c, bitmap_t* img, rect_t* dst); +ret_t canvas_draw_image_scale_y(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_test_paint(canvas_t* c, bool_t pressed, xy_t x, xy_t y); diff --git a/src/base/types_def.h b/src/base/types_def.h index d9b98bfec..9e089882a 100644 --- a/src/base/types_def.h +++ b/src/base/types_def.h @@ -29,9 +29,8 @@ #include #include -typedef int xy_t; -typedef uint32_t id_t; -typedef uint32_t wh_t; +typedef int32_t xy_t; +typedef int32_t wh_t; /** * @enum ret_t diff --git a/tests/canvas_test.cc b/tests/canvas_test.cc index b4aeadd54..c8091fad1 100644 --- a/tests/canvas_test.cc +++ b/tests/canvas_test.cc @@ -362,16 +362,18 @@ TEST(Canvas, draw_image_3patch_h) { rect_init(d, 0, 0, img.w, img.h); ASSERT_EQ(canvas_draw_image_3patch_h(&c, &img, &d), RET_OK); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,32,32);"); - + lcd_log_reset(lcd); rect_init(d, 0, 0, img.w * 3, img.h * 2); ASSERT_EQ(canvas_draw_image_3patch_h(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,10,32,0,0,10,64);dg(10,0,12,32,10,0,76,64);dg(22,0,10,32,86,0,10,64);"); + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,10,32,0,0,10,64);dg(10,0,12,32,10,0,76,64);dg(22,0,10,32,86,0,10,64);"); lcd_log_reset(lcd); - rect_init(d, 0, 0, img.w * 3, img.h/2); + rect_init(d, 0, 0, img.w * 3, img.h / 2); ASSERT_EQ(canvas_draw_image_3patch_h(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,10,32,0,0,10,16);dg(10,0,12,32,10,0,76,16);dg(22,0,10,32,86,0,10,16);"); + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,10,32,0,0,10,16);dg(10,0,12,32,10,0,76,16);dg(22,0,10,32,86,0,10,16);"); canvas_end_frame(&c); font_manager_deinit(&font_manager); @@ -397,16 +399,18 @@ TEST(Canvas, draw_image_3patch_v) { rect_init(d, 0, 0, img.w, img.h); ASSERT_EQ(canvas_draw_image_3patch_v(&c, &img, &d), RET_OK); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,32,32);"); - + lcd_log_reset(lcd); rect_init(d, 0, 0, img.w * 2, img.h * 3); ASSERT_EQ(canvas_draw_image_3patch_v(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,10,0,0,64,10);dg(0,10,32,12,0,10,64,76);dg(0,22,32,10,0,86,64,10);"); + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,32,10,0,0,64,10);dg(0,10,32,12,0,10,64,76);dg(0,22,32,10,0,86,64,10);"); lcd_log_reset(lcd); - rect_init(d, 0, 0, img.w/2, img.h * 3); + rect_init(d, 0, 0, img.w / 2, img.h * 3); ASSERT_EQ(canvas_draw_image_3patch_v(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,10,0,0,16,10);dg(0,10,32,12,0,10,16,76);dg(0,22,32,10,0,86,16,10);"); + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,32,10,0,0,16,10);dg(0,10,32,12,0,10,16,76);dg(0,22,32,10,0,86,16,10);"); canvas_end_frame(&c); font_manager_deinit(&font_manager); @@ -431,17 +435,26 @@ TEST(Canvas, draw_image_9patch) { lcd_log_reset(lcd); rect_init(d, 0, 0, img.w, img.h); ASSERT_EQ(canvas_draw_image_9patch(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,10,10,0,0,10,10);dg(22,0,10,10,22,0,10,10);dg(0,22,10,10,0,22,10,10);dg(22,22,10,10,22,22,10,10);dg(10,0,12,10,10,0,12,10);dg(10,22,12,10,10,22,12,10);dg(0,10,10,12,0,10,10,12);dg(22,10,10,12,22,10,10,12);dg(10,10,12,12,10,10,12,12);"); - + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,10,10,0,0,10,10);dg(22,0,10,10,22,0,10,10);dg(0,22,10,10,0,22,10,10);dg(22,22," + "10,10,22,22,10,10);dg(10,0,12,10,10,0,12,10);dg(10,22,12,10,10,22,12,10);dg(0,10,10," + "12,0,10,10,12);dg(22,10,10,12,22,10,10,12);dg(10,10,12,12,10,10,12,12);"); + lcd_log_reset(lcd); rect_init(d, 0, 0, img.w * 2, img.h * 3); ASSERT_EQ(canvas_draw_image_9patch(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,10,10,0,0,10,10);dg(22,0,10,10,54,0,10,10);dg(0,22,10,10,0,86,10,10);dg(22,22,10,10,54,86,10,10);dg(10,0,12,10,10,0,44,10);dg(10,22,12,10,10,86,44,10);dg(0,10,10,12,0,10,10,76);dg(22,10,10,12,54,10,10,76);dg(10,10,12,12,10,10,44,76);"); + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,10,10,0,0,10,10);dg(22,0,10,10,54,0,10,10);dg(0,22,10,10,0,86,10,10);dg(22,22," + "10,10,54,86,10,10);dg(10,0,12,10,10,0,44,10);dg(10,22,12,10,10,86,44,10);dg(0,10,10," + "12,0,10,10,76);dg(22,10,10,12,54,10,10,76);dg(10,10,12,12,10,10,44,76);"); lcd_log_reset(lcd); - rect_init(d, 0, 0, img.w/2, img.h * 3); + rect_init(d, 0, 0, img.w / 2, img.h * 3); ASSERT_EQ(canvas_draw_image_9patch(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,5,10,0,0,5,10);dg(27,0,5,10,11,0,5,10);dg(0,22,5,10,0,86,5,10);dg(27,22,5,10,11,86,5,10);dg(5,0,22,10,5,0,6,10);dg(5,22,22,10,5,86,6,10);dg(0,10,5,12,0,10,5,76);dg(27,10,5,12,11,10,5,76);dg(5,10,22,12,5,10,6,76);"); + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,5,10,0,0,5,10);dg(27,0,5,10,11,0,5,10);dg(0,22,5,10,0,86,5,10);dg(27,22,5,10," + "11,86,5,10);dg(5,0,22,10,5,0,6,10);dg(5,22,22,10,5,86,6,10);dg(0,10,5,12,0,10,5,76);" + "dg(27,10,5,12,11,10,5,76);dg(5,10,22,12,5,10,6,76);"); canvas_end_frame(&c); font_manager_deinit(&font_manager); @@ -465,21 +478,22 @@ TEST(Canvas, draw_image_tile_h) { lcd_log_reset(lcd); rect_init(r, 0, 0, img.w, img.h); - rect_init(d, 0, 0, img.w/2, img.h); + rect_init(d, 0, 0, img.w / 2, img.h); ASSERT_EQ(canvas_draw_image_tile_h(&c, &img, &d), RET_OK); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,16,32,0,0,16,32);"); - + lcd_log_reset(lcd); rect_init(r, 0, 0, img.w, img.h); - rect_init(d, 0, 0, img.w+10, img.h/2); + rect_init(d, 0, 0, img.w + 10, img.h / 2); ASSERT_EQ(canvas_draw_image_tile_h(&c, &img, &d), RET_OK); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,32,16);dg(0,0,10,32,32,0,10,16);"); lcd_log_reset(lcd); rect_init(r, 0, 0, img.w, img.h); - rect_init(d, 0, 0, img.w*2+10, img.h+10); + rect_init(d, 0, 0, img.w * 2 + 10, img.h + 10); ASSERT_EQ(canvas_draw_image_tile_h(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,32,42);dg(0,0,32,32,32,0,32,42);dg(0,0,10,32,64,0,10,42);"); + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,32,32,0,0,32,42);dg(0,0,32,32,32,0,32,42);dg(0,0,10,32,64,0,10,42);"); canvas_end_frame(&c); font_manager_deinit(&font_manager); @@ -503,21 +517,22 @@ TEST(Canvas, draw_image_tile_v) { lcd_log_reset(lcd); rect_init(r, 0, 0, img.w, img.h); - rect_init(d, 0, 0, img.w, img.h/2); + rect_init(d, 0, 0, img.w, img.h / 2); ASSERT_EQ(canvas_draw_image_tile_v(&c, &img, &d), RET_OK); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,16,0,0,32,16);"); - + lcd_log_reset(lcd); rect_init(r, 0, 0, img.w, img.h); - rect_init(d, 0, 0, img.w+10, img.h/2); + rect_init(d, 0, 0, img.w + 10, img.h / 2); ASSERT_EQ(canvas_draw_image_tile_v(&c, &img, &d), RET_OK); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,16,0,0,42,16);"); lcd_log_reset(lcd); rect_init(r, 0, 0, img.w, img.h); - rect_init(d, 0, 0, img.w+10, img.h*2+10); + rect_init(d, 0, 0, img.w + 10, img.h * 2 + 10); ASSERT_EQ(canvas_draw_image_tile_v(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,42,32);dg(0,0,32,32,0,32,42,32);dg(0,0,32,10,0,64,42,10);"); + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,32,32,0,0,42,32);dg(0,0,32,32,0,32,42,32);dg(0,0,32,10,0,64,42,10);"); canvas_end_frame(&c); font_manager_deinit(&font_manager); @@ -541,24 +556,178 @@ TEST(Canvas, draw_image_tile) { lcd_log_reset(lcd); rect_init(r, 0, 0, img.w, img.h); - rect_init(d, 0, 0, img.w/2, img.h/2); + rect_init(d, 0, 0, img.w / 2, img.h / 2); ASSERT_EQ(canvas_draw_image_tile(&c, &img, &d), RET_OK); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,16,16,0,0,16,16);"); - + lcd_log_reset(lcd); rect_init(r, 0, 0, img.w, img.h); - rect_init(d, 0, 0, img.w+10, img.h/2); + rect_init(d, 0, 0, img.w + 10, img.h / 2); ASSERT_EQ(canvas_draw_image_tile(&c, &img, &d), RET_OK); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,16,0,0,32,16);dg(0,0,10,16,32,0,10,16);"); lcd_log_reset(lcd); rect_init(r, 0, 0, img.w, img.h); - rect_init(d, 0, 0, img.w*2+10, img.h*2+10); + rect_init(d, 0, 0, img.w * 2 + 10, img.h * 2 + 10); ASSERT_EQ(canvas_draw_image_tile(&c, &img, &d), RET_OK); - ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,32,32);dg(0,0,32,32,32,0,32,32);dg(0,0,10,32,64,0,10,32);dg(0,0,32,32,0,32,32,32);dg(0,0,32,32,32,32,32,32);dg(0,0,10,32,64,32,10,32);dg(0,0,32,10,0,64,32,10);dg(0,0,32,10,32,64,32,10);dg(0,0,10,10,64,64,10,10);"); + ASSERT_EQ(lcd_log_get_commands(lcd), + "dg(0,0,32,32,0,0,32,32);dg(0,0,32,32,32,0,32,32);dg(0,0,10,32,64,0,10,32);dg(0,0,32," + "32,0,32,32,32);dg(0,0,32,32,32,32,32,32);dg(0,0,10,32,64,32,10,32);dg(0,0,32,10,0,64," + "32,10);dg(0,0,32,10,32,64,32,10);dg(0,0,10,10,64,64,10,10);"); canvas_end_frame(&c); font_manager_deinit(&font_manager); lcd_destroy(lcd); } +TEST(Canvas, draw_image_scale_x) { + rect_t r; + rect_t d; + canvas_t c; + bitmap_t img; + font_manager_t font_manager; + font_manager_init(&font_manager); + lcd_t* lcd = lcd_log_init(800, 600); + canvas_init(&c, lcd, &font_manager); + + img.w = 32; + img.h = 32; + rect_init(r, 0, 0, 320, 480); + canvas_begin_frame(&c, &r); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w / 2, img.h / 2); + ASSERT_EQ(canvas_draw_image_scale_x(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);"); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w + 10, img.h / 2); + ASSERT_EQ(canvas_draw_image_scale_x(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,12,0,0,42,16);"); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10); + ASSERT_EQ(canvas_draw_image_scale_x(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,64,64);"); + + canvas_end_frame(&c); + font_manager_deinit(&font_manager); + lcd_destroy(lcd); +} + +TEST(Canvas, draw_image_scale_y) { + rect_t r; + rect_t d; + canvas_t c; + bitmap_t img; + font_manager_t font_manager; + font_manager_init(&font_manager); + lcd_t* lcd = lcd_log_init(800, 600); + canvas_init(&c, lcd, &font_manager); + + img.w = 32; + img.h = 32; + rect_init(r, 0, 0, 320, 480); + canvas_begin_frame(&c, &r); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w / 2, img.h / 2); + ASSERT_EQ(canvas_draw_image_scale_y(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);"); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w + 10, img.h / 2); + ASSERT_EQ(canvas_draw_image_scale_y(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);"); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10); + ASSERT_EQ(canvas_draw_image_scale_y(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,27,32,0,0,64,74);"); + + canvas_end_frame(&c); + font_manager_deinit(&font_manager); + lcd_destroy(lcd); +} + +TEST(Canvas, draw_image_scale) { + rect_t r; + rect_t d; + canvas_t c; + bitmap_t img; + font_manager_t font_manager; + font_manager_init(&font_manager); + lcd_t* lcd = lcd_log_init(800, 600); + canvas_init(&c, lcd, &font_manager); + + img.w = 32; + img.h = 32; + rect_init(r, 0, 0, 320, 480); + canvas_begin_frame(&c, &r); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w / 2, img.h / 2); + ASSERT_EQ(canvas_draw_image_scale(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);"); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w + 10, img.h / 2); + ASSERT_EQ(canvas_draw_image_scale(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,13,0,16,16);"); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10); + ASSERT_EQ(canvas_draw_image_scale(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,5,64,64);"); + + canvas_end_frame(&c); + font_manager_deinit(&font_manager); + lcd_destroy(lcd); +} + +TEST(Canvas, draw_image_center) { + rect_t r; + rect_t d; + canvas_t c; + bitmap_t img; + font_manager_t font_manager; + font_manager_init(&font_manager); + lcd_t* lcd = lcd_log_init(800, 600); + canvas_init(&c, lcd, &font_manager); + + img.w = 32; + img.h = 32; + rect_init(r, 0, 0, 320, 480); + canvas_begin_frame(&c, &r); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w, img.h); + ASSERT_EQ(canvas_draw_image_center(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,32,32);"); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w + 10, img.h + 10); + ASSERT_EQ(canvas_draw_image_center(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,5,5,32,32);"); + + lcd_log_reset(lcd); + rect_init(r, 0, 0, img.w, img.h); + rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10); + ASSERT_EQ(canvas_draw_image_center(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,16,21,32,32);"); + + canvas_end_frame(&c); + font_manager_deinit(&font_manager); + lcd_destroy(lcd); +}