Skip to content

Commit ff487e0

Browse files
committed
Graphics: Adjust image alignment when rotating images to avoid cropping (fix #2535)
1 parent 9cc9268 commit ff487e0

5 files changed

+164
-46
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
: ESP32C3: Get analogRead working correctly
2+
Graphics: Adjust image alignment when rotating images to avoid cropping (fix #2535)
23

34
2v24 : Bangle.js2: Add 'Bangle.touchRd()', 'Bangle.touchWr()'
45
Bangle.js2: After Bangle.showTestScreen, put Bangle.js into a hard off state (not soft off)

libs/graphics/jswrap_graphics.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ bool _jswrap_graphics_parseImage(JsGraphics *gfx, JsVar *image, size_t imageOffs
289289
}
290290

291291
bool _jswrap_drawImageLayerGetPixel(GfxDrawImageLayer *l, uint32_t *result) {
292-
int qx = l->qx+127;
293-
int qy = l->qy+127;
294-
if (qx>=0 && qy>=0 && qx<l->mx && qy<l->my) {
292+
int qx = l->qx;
293+
int qy = l->qy;
294+
if (qx>=0 && qy>=0 && (qx&~255)<l->mx && (qy&~255)<l->my) {
295295
unsigned int colData = 0;
296296
int imagex = qx>>8;
297297
int imagey = qy>>8;
@@ -333,8 +333,8 @@ NO_INLINE void _jswrap_drawImageLayerInit(GfxDrawImageLayer *l) {
333333
// step values for blitting rotated image
334334
double vcos = jswrap_math_cos(l->rotate);
335335
double vsin = jswrap_math_sin(l->rotate);
336-
l->sx = (int)((vcos/l->scale)*256 + 0.5);
337-
l->sy = (int)((vsin/l->scale)*256 + 0.5);
336+
l->sx = (int)((vcos/l->scale)*256);
337+
l->sy = (int)((vsin/l->scale)*256);
338338
// work out actual image width and height
339339
int iw = (int)(0.5 + l->scale*(l->img.width*fabs(vcos) + l->img.height*fabs(vsin)));
340340
int ih = (int)(0.5 + l->scale*(l->img.width*fabs(vsin) + l->img.height*fabs(vcos)));
@@ -346,10 +346,10 @@ NO_INLINE void _jswrap_drawImageLayerInit(GfxDrawImageLayer *l) {
346346
l->x2 = l->x1 + iw*256;
347347
l->y2 = l->y1 + ih*256;
348348
// work out start position in the image
349-
int centerx = l->img.width*128;
350-
int centery = l->img.height*128;
351-
l->px = centerx - (1 + (l->sx*iw) + (l->sy*ih)) / 2;
352-
l->py = centery - (1 + (l->sx*ih) - (l->sy*iw)) / 2;
349+
int centerx = (l->img.width)*128; // *256 / 2
350+
int centery = (l->img.height)*128;
351+
l->px = centerx - ((1 + (l->sx*iw) + (l->sy*ih)) / 2);
352+
l->py = centery - ((1 + (l->sx*ih) - (l->sy*iw)) / 2);
353353
// handle repetition
354354
if (l->repeat) {
355355
// for the range we're in, it's quicker/easier than modulo

tests/test_graphics_drawImage3bit.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,25 @@ SHOULD_BE(`
5555
// Force almost identical draw, but with slow path using _jswrap_drawImageLayerGetPixel
5656
g.clear(1).drawImage(img,0,0,{scale:1.0000001});
5757
SHOULD_BE(`
58-
......."""""".......
59-
....."""""""""".....
60-
...""""""""""""""...
61-
..""""""""""""""""..
62-
..""""""""""""""""..
63-
.""""""""""""""*""".
64-
."""""""""""""##*"".
58+
......."""""""......
59+
....."""""""""""....
60+
..."""""""""""""""..
61+
..""""""""""""""""".
62+
..""""""""""""""""".
63+
."""""""""""""""*"""
64+
.""""""""""""""##*""
65+
""""""""""""""###"""
6566
"""""""""""""###""""
66-
""""""""""""###"""""
67-
"""""*"""""###""""""
68-
""""*##"""###"""""""
69-
"""""###"###""""""""
70-
""""""#####"""""""""
71-
.""""""###""""""""".
72-
."""""""*"""""""""".
73-
..""""""""""""""""..
74-
..""""""""""""""""..
75-
...""""""""""""""...
76-
....."""""""""".....
77-
......."""""".......`);
67+
"""""*""""""###"""""
68+
""""*##"""####""""""
69+
""""*##"""####""""""
70+
"""""###"####"""""""
71+
""""""######""""""""
72+
.""""""###""""""""""
73+
."""""""*"""""""""""
74+
..""""""""""""""""".
75+
..""""""""""""""""".
76+
..."""""""""""""""..
77+
....."""""""""""....`);
7878

7979
result = ok;

tests/test_graphics_drawImageRotate.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,29 @@ SHOULD_BE(`
4545
................................
4646
................................
4747
................................
48+
................................
4849
................#...............
4950
...............###..............
5051
..............#####.............
5152
.............#######............
52-
............###.#####...........
53-
...........###...#####..........
54-
..........###.....#####.........
55-
.........###.......#####........
56-
........###.........#####.......
57-
.......###...........#####......
58-
......###.............#####.....
59-
......##...............####.....
60-
.......##...............##......
61-
........##.............##.......
62-
.........##...........##........
63-
..........##.........##.........
64-
...........##.......##..........
65-
............##.....##...........
66-
.............##...##............
67-
..............##.##.............
53+
............#########...........
54+
...........###..######..........
55+
..........###....######.........
56+
.........###......######........
57+
........###........######.......
58+
.......###..........######......
59+
......###............######.....
60+
.....###..............#####.....
61+
......###..............####.....
62+
.......###.............###......
63+
........###...........###.......
64+
.........###.........###........
65+
..........###.......###.........
66+
...........###.....###..........
67+
............###...###...........
68+
.............###.###............
69+
..............#####.............
6870
...............###..............
69-
................#...............
70-
................................
7171
................................
7272
................................
7373
................................
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
var g = Graphics.createArrayBuffer(16,16,8);
2+
g.dump = _=>{
3+
var s = "";
4+
var b = new Uint8Array(g.buffer);
5+
var n = 0;
6+
for (var y=0;y<g.getHeight();y++) {
7+
s+="\n";
8+
for (var x=0;x<g.getWidth();x++)
9+
s+=".-+#"[b[n++]&3];
10+
}
11+
return s;
12+
}
13+
g.print = _=>{
14+
print("`"+g.dump()+"`");
15+
}
16+
var ok = true;
17+
function SHOULD_BE(a) {
18+
var b = g.dump();
19+
if (a!=b) {
20+
console.log("GOT :"+b+"\nSHOULD BE:"+a+"\n================");
21+
ok = false;
22+
}
23+
}
24+
25+
var img = {
26+
width : 8, height : 8, bpp : 8,
27+
buffer : new Uint8Array([
28+
3,3,3,3,3,3,3,3,
29+
3,3,3,3,3,3,3,3,
30+
3,1,1,1,1,1,1,3,
31+
3,1,1,1,1,1,1,3,
32+
3,1,1,1,1,1,1,3,
33+
3,1,1,1,1,1,1,3,
34+
3,1,1,1,1,1,1,3,
35+
3,3,3,3,3,3,3,3,
36+
]).buffer
37+
};
38+
39+
g.clear().drawImage(img,4,4,{rotate:-0.000000001});
40+
SHOULD_BE(`
41+
########........
42+
########........
43+
#------#........
44+
#------#........
45+
#------#........
46+
#------#........
47+
#------#........
48+
########........
49+
................
50+
................
51+
................
52+
................
53+
................
54+
................
55+
................
56+
................`);
57+
58+
g.clear().drawImage(img,4,4,{rotate:Math.PI/2});
59+
SHOULD_BE(`
60+
########........
61+
#-----##........
62+
#-----##........
63+
#-----##........
64+
#-----##........
65+
#-----##........
66+
#-----##........
67+
########........
68+
................
69+
................
70+
................
71+
................
72+
................
73+
................
74+
................
75+
................`);
76+
77+
78+
g.clear().drawImage(img,4,4,{rotate:Math.PI});
79+
SHOULD_BE(`
80+
########........
81+
#------#........
82+
#------#........
83+
#------#........
84+
#------#........
85+
#------#........
86+
########........
87+
########........
88+
................
89+
................
90+
................
91+
................
92+
................
93+
................
94+
................
95+
................`);
96+
97+
98+
g.clear().drawImage(img,4,4,{rotate:Math.PI*3/2});
99+
SHOULD_BE(`
100+
########........
101+
##-----#........
102+
##-----#........
103+
##-----#........
104+
##-----#........
105+
##-----#........
106+
##-----#........
107+
########........
108+
................
109+
................
110+
................
111+
................
112+
................
113+
................
114+
................
115+
................`);
116+
117+
result = ok;

0 commit comments

Comments
 (0)