使用递归函数,实现在添加方形画布时,自动过滤画布重叠区域
1 #define LCM_BUF_SIZE (85*1024) 2 3 typedef struct _draw_img 4 { 5 int16_t x0; 6 int16_t y0; 7 int16_t x1; 8 int16_t y1; 9 uint16_t len; 10 } draw_img_t; 11 12 uint8_t lcm_buf[LCM_BUF_SIZE] = { 0x00 }; 13 14 static void canvas_regist(uint16_t x, uint16_t y, uint16_t w, uint16_t h) 15 { 16 uint8_t *lcd_buff = lcm_buf; 17 18 uint32_t readSize = (uint32_t)(w * h); 19 20 ASSERT ( gimgNext != NULL && (gimgNext >= lcd_buff) && (gimgNext <= (lcd_buff + LCM_BUF_SIZE - readSize - sizeof(draw_img_t)))); 21 if(gimgNext > (lcd_buff + LCM_BUF_SIZE - readSize)) 22 { 23 NRF_LOG_ERROR("lcm buf overflow!!!"); 24 NRF_LOG_ERROR("lcm buf need size = %d.%dk", (uint32_t)((gimgNext+readSize)-lcd_buff)/1000, 25 (uint32_t)((gimgNext+readSize)-lcd_buff)%1000); 26 return; 27 } 28 draw_img_t *p = (draw_img_t *)gimgNext; 29 30 p->x0 = x; 31 p->y0 = y; 32 p->x1 = x + w - 1; 33 p->y1 = y + h - 1; 34 p->len =readSize + sizeof(draw_img_t); 35 36 gimgCount++; 37 lcd_buff[0] = gimgCount; 38 gimgNext += p->len; 39 40 return; 41 } 42 void add_new_canvas(uint16_t x, uint16_t y, uint16_t w, uint16_t h) 43 { 44 if((w <= 1) || (h <= 1)) 45 { 46 return; 47 } 48 49 uint8_t *lcd_buff = lcm_buf; 50 draw_img_t *p_img = NULL; 51 uint32_t index = 2; 52 draw_img_t new_canvas; 53 new_canvas.x0 = x; 54 new_canvas.y0 = y; 55 new_canvas.x1 = x + w - 1; 56 new_canvas.y1 = y + h - 1; 57 int i = 0; 58 59 if(gimgCount == 0) 60 { 61 canvas_regist(x, y, w, h); 62 return; 63 } 64 for(i = 0; i < gimgCount; i++) 65 { 66 p_img = (draw_img_t *)&lcd_buff[index]; 67 if(((p_img->x1-p_img->x0+1)==LCD_WIDTH) && ((p_img->y1-p_img->y0+1)==LCD_HEIGHT)) 68 { 69 index += p_img->len; 70 continue; 71 } 72 /* ******************* 73 * * 74 * ************* * 75 * * * * 76 * * * * 77 * * * * 78 * * * * 79 * ************* * 80 * * 81 ******************* */ 82 else if((p_img->x0 <= new_canvas.x0) && (p_img->x1 >= new_canvas.x1) && (p_img->y0 <= new_canvas.y0) && (p_img->y1 >= new_canvas.y1)) 83 { 84 //NRF_LOG_ERROR("canvas existed"); 85 return; 86 } 87 /* ******************* 88 * * 89 * ************* * 90 * * * * 91 * * * * 92 * * * * 93 * * * * 94 * ************* * 95 * * 96 ******************* */ 97 else if((p_img->x0 >= new_canvas.x0) && (p_img->x1 <= new_canvas.x1) && (p_img->y0 >= new_canvas.y0) && (p_img->y1 <= new_canvas.y1)) 98 { 99 add_new_canvas(new_canvas.x0, new_canvas.y0, w, p_img->y0-new_canvas.y0+1); 100 add_new_canvas(new_canvas.x0, p_img->y1, w, new_canvas.y1-p_img->y1+1); 101 add_new_canvas(new_canvas.x0, p_img->y0, p_img->x0-new_canvas.x0+1, p_img->y1-p_img->y0+1); 102 add_new_canvas(p_img->x1, p_img->y0, new_canvas.x1-p_img->x1+1, p_img->y1-p_img->y0+1); 103 return; 104 } 105 /* ********* 106 * * 107 * ************* 108 * * * * 109 ********* * 110 * * 111 * * 112 ************* */ 113 else if((p_img->x0 > new_canvas.x0) && (p_img->x0 < new_canvas.x1) && (p_img->y0 > new_canvas.y0) && (p_img->y0 < new_canvas.y1)) 114 { 115 add_new_canvas(new_canvas.x0, new_canvas.y0, w, p_img->y0-new_canvas.y0+1); 116 add_new_canvas(new_canvas.x0, p_img->y0, p_img->x0-new_canvas.x0+1, new_canvas.y1-p_img->y0+1); 117 if(p_img->x1 < new_canvas.x1) 118 { 119 add_new_canvas(p_img->x1, p_img->y0, new_canvas.x1-p_img->x1+1, new_canvas.y1-p_img->y0+1); 120 } 121 return; 122 } 123 /* ********* 124 * * 125 ************* * 126 * * * * 127 * ********* 128 * * 129 * * 130 ************* */ 131 else if((p_img->x1 > new_canvas.x0) && (p_img->x1 < new_canvas.x1) && (p_img->y0 > new_canvas.y0) && (p_img->y0 < new_canvas.y1)) 132 { 133 add_new_canvas(new_canvas.x0, new_canvas.y0, w, p_img->y0-new_canvas.y0+1); 134 add_new_canvas(p_img->x1, p_img->y0, new_canvas.x1-p_img->x1+1, new_canvas.y1-p_img->y0+1); 135 if(p_img->y1 < new_canvas.y1) 136 { 137 add_new_canvas(new_canvas.x0, p_img->y1, p_img->x1-new_canvas.x0+1, new_canvas.y1-p_img->y1+1); 138 } 139 return; 140 } 141 /* ************* 142 * * 143 * * 144 ********* * 145 * * * * 146 * ************* 147 * * 148 ********* */ 149 else if((p_img->x0 > new_canvas.x0) && (p_img->x0 < new_canvas.x1) && (p_img->y1 > new_canvas.y0) && (p_img->y1 < new_canvas.y1)) 150 { 151 add_new_canvas(new_canvas.x0, new_canvas.y0, p_img->x0-new_canvas.x0+1, p_img->y1-new_canvas.y0+1); 152 add_new_canvas(new_canvas.x0, p_img->y1, w, new_canvas.y1-p_img->y1+1); 153 if(p_img->y0 > new_canvas.y0) 154 { 155 add_new_canvas(p_img->x0,new_canvas.y0, new_canvas.x1-p_img->x0+1, p_img->y0-new_canvas.y0+1); 156 } 157 return; 158 } 159 /* ************* 160 * * 161 * * 162 * ********* 163 * * * * 164 ************* * 165 * * 166 ********* */ 167 else if((p_img->x1 > new_canvas.x0) && (p_img->x1 < new_canvas.x1) && (p_img->y1 > new_canvas.y0) && (p_img->y1 < new_canvas.y1)) 168 { 169 add_new_canvas(p_img->x1, new_canvas.y0, new_canvas.x1-p_img->x1+1, p_img->y1-new_canvas.y0+1); 170 add_new_canvas(new_canvas.x0, p_img->y1, w, new_canvas.y1-p_img->y1+1); 171 if(p_img->x0 > new_canvas.x0) 172 { 173 add_new_canvas(new_canvas.x0, new_canvas.y0, p_img->x0-new_canvas.x0+1, p_img->y1-new_canvas.y0+1); 174 } 175 return; 176 } 177 /* ************* 178 ********* * 179 * * * * 180 * * * * 181 ********* * 182 ************* */ 183 else if((p_img->x0 > new_canvas.x0) && (p_img->x0 < new_canvas.x1) && (p_img->y0 <= new_canvas.y0) && (p_img->y1 >= new_canvas.y1) && (p_img->x1 >= new_canvas.x1)) 184 { 185 add_new_canvas(new_canvas.x0, new_canvas.y0, p_img->x0-new_canvas.x0+1, h); 186 return; 187 } 188 /* ************* 189 * ********* 190 * * * * 191 * * * * 192 * ********* 193 ************* */ 194 else if((p_img->x1 > new_canvas.x0) && (p_img->x1 < new_canvas.x1) && (p_img->y0 <= new_canvas.y0) && (p_img->y1 >= new_canvas.y1) && (p_img->x0 <= new_canvas.x0)) 195 { 196 add_new_canvas(p_img->x1, new_canvas.y0, new_canvas.x1-p_img->x1+1, h); 197 return; 198 } 199 /* ******* 200 * * 201 ************* 202 * * * * 203 * ******* * 204 * * 205 * * 206 ************* */ 207 else if((p_img->x0 <= new_canvas.x0) && (p_img->x1 >= new_canvas.x1) && (p_img->y0 > new_canvas.y0) && (p_img->y0 < new_canvas.y1) && (p_img->y1 >= new_canvas.y1)) 208 { 209 add_new_canvas(new_canvas.x0, new_canvas.y0, w, p_img->y0-new_canvas.y0+1); 210 return; 211 } 212 /* ************* 213 * * 214 * * 215 * ******* * 216 * * * * 217 ************* 218 * * 219 ******* */ 220 else if((p_img->x0 <= new_canvas.x0) && (p_img->x1 >= new_canvas.x1) && (p_img->y1 > new_canvas.y0) && (p_img->y1 < new_canvas.y1) && (p_img->y0 <= new_canvas.y0)) 221 { 222 add_new_canvas(new_canvas.x0, p_img->y1, w, new_canvas.y1-p_img->y1+1); 223 return; 224 } 225 /* ************* 226 ******************* 227 * * * * 228 * * * * 229 ******************* 230 ************* */ 231 else if((p_img->x0 > new_canvas.x0) && (p_img->x1 < new_canvas.x1) && (p_img->y0 <= new_canvas.y0) && (p_img->y1 >= new_canvas.y1)) 232 { 233 add_new_canvas(new_canvas.x0, new_canvas.y0, p_img->x0-new_canvas.x0+1, h); 234 add_new_canvas(p_img->x1, new_canvas.y0, new_canvas.x1-p_img->x1+1, h); 235 return; 236 } 237 /* ******* 238 * * 239 ************* 240 * * * * 241 * * * * 242 * * * * 243 * * * * 244 ************* 245 * * 246 ******* */ 247 else if((p_img->x0 <= new_canvas.x0) && (p_img->x1 >= new_canvas.x1) && (p_img->y0 > new_canvas.y0) && (p_img->y1 < new_canvas.y1)) 248 { 249 add_new_canvas(new_canvas.x0, new_canvas.y0, w, p_img->y0-new_canvas.y0+1); 250 add_new_canvas(new_canvas.x0, p_img->y1, w, new_canvas.y1-p_img->y1+1); 251 return; 252 } 253 index += p_img->len; 254 } 255 canvas_regist(x, y, w, h); 256 257 return; 258 }