Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of gr-peach-opencv-project by
mGraphic.cpp
00001 #include "mGraphic.hpp" 00002 int counter =0; 00003 static void togle_led(DigitalOut led) 00004 { 00005 led = 1; 00006 wait(0.1); 00007 led = 0; 00008 wait(0.1); 00009 } 00010 00011 graphicFrameworkCanvas::graphicFrameworkCanvas(uint8_t * bufDraw, uint16_t w,uint16_t h, 00012 int8_t bpp, int8_t drawPoint, int8_t sizeHeader) 00013 { 00014 mBufLcd = bufDraw; 00015 mWidth = w; 00016 mHeight = h; 00017 mBpp = bpp; 00018 mDrawPoint = drawPoint; 00019 mSizeHeaderImg = sizeHeader; 00020 } 00021 00022 graphicFrameworkCanvas::~graphicFrameworkCanvas() 00023 { 00024 } 00025 00026 void graphicFrameworkCanvas::draw_pixel(uint8_t * p_buf, int id, 00027 int x, int y,uint8_t *color) 00028 { 00029 int idx_base; 00030 int wk_idx; 00031 int i; 00032 int j; 00033 00034 /* A coordinate in the upper left is calculated from a central coordinate. */ 00035 if ((x - (mDrawPoint / 2)) >= 0) { 00036 x -= (mDrawPoint / 2); 00037 } 00038 if (x > (mWidth - mDrawPoint)) { 00039 x = (mWidth - mDrawPoint); 00040 } 00041 if ((y - (mDrawPoint / 2)) >= 0) { 00042 y -= (mDrawPoint / 2); 00043 } 00044 if (y > (mHeight - mDrawPoint)) { 00045 y = (mHeight - mDrawPoint); 00046 } 00047 idx_base = (x + (mWidth * y)) * mBpp; 00048 00049 /* Drawing */ 00050 for (i = 0; i < mDrawPoint; i++) { 00051 wk_idx = idx_base + (mWidth * mBpp * i); 00052 for (j = 0; j < mDrawPoint; j++) { 00053 for(int k = 0; k < mBpp;k++) 00054 { 00055 p_buf[wk_idx++] = color[k]; 00056 } 00057 } 00058 } 00059 } 00060 00061 void graphicFrameworkCanvas::drawImage(uint8_t * _img, int _x, int _y) 00062 { 00063 uint8_t *p_buf = _img; 00064 uint8_t coller_pix[mBpp]; 00065 uint16_t w = (_img[1] << 8) | _img[0]; 00066 uint16_t h = (_img[3] << 8) | _img[2]; 00067 uint8_t bpp = _img[4]; 00068 00069 p_buf+=mSizeHeaderImg; 00070 00071 for(int i = 0;i < (int)h*w*bpp; i+=bpp) 00072 { 00073 int pos_x = (i/bpp)%w; 00074 int pos_y = (i/bpp)/w; 00075 for(int k = 0; k < mBpp;k++) 00076 { 00077 coller_pix[k] = p_buf[i+k]; 00078 } 00079 draw_pixel(mBufLcd, 0, pos_x + _x, pos_y + _y,coller_pix); 00080 } 00081 } 00082 00083 void graphicFrameworkCanvas::drawRect(int x, int y, int width, int height, 00084 uint8_t *color) 00085 { 00086 for(int k = 0; k < width;k++) 00087 { 00088 draw_pixel(mBufLcd, 0, x + k, y, color); 00089 draw_pixel(mBufLcd, 0, x + k, y + height,color); 00090 } 00091 00092 for(int k = 0; k < height;k++) 00093 { 00094 draw_pixel(mBufLcd, 0, x, y + k, color); 00095 draw_pixel(mBufLcd, 0, x + width, y + k,color); 00096 } 00097 } 00098 00099 void graphicFrameworkCanvas::draw_pixel(int x, int y,uint8_t *color) 00100 { 00101 int idx_base; 00102 int wk_idx; 00103 int i; 00104 int j; 00105 00106 /* A coordinate in the upper left is calculated from a central coordinate. */ 00107 if ((x - (mDrawPoint / 2)) >= 0) { 00108 x -= (mDrawPoint / 2); 00109 } 00110 if (x > (mWidth - mDrawPoint)) { 00111 x = (mWidth - mDrawPoint); 00112 } 00113 if ((y - (mDrawPoint / 2)) >= 0) { 00114 y -= (mDrawPoint / 2); 00115 } 00116 if (y > (mHeight - mDrawPoint)) { 00117 y = (mHeight - mDrawPoint); 00118 } 00119 idx_base = (x + (mWidth * y)) * mBpp; 00120 00121 /* Drawing */ 00122 for (i = 0; i < mDrawPoint; i++) { 00123 wk_idx = idx_base + (mWidth * mBpp * i); 00124 for (j = 0; j < mDrawPoint; j++) { 00125 for(int k = 0; k < mBpp;k++) 00126 { 00127 mBufLcd[wk_idx++] = color[k]; 00128 } 00129 } 00130 } 00131 return; 00132 } 00133 void graphicFrameworkCanvas::drawLineNormal(int x1, int y1, int x2, int y2, uint8_t* color) 00134 { 00135 int tmp_x = 0; 00136 int tmp_y = 0; 00137 int i = 0; 00138 00139 if (x1 == x2) // Case equation x = m. 00140 { 00141 if (y1 > y2) // Swap 2 points 00142 { 00143 //ptTemp = pt1; 00144 tmp_x = x1; 00145 tmp_y = y1; 00146 00147 //pt1 = pt2; 00148 x1 = x2; 00149 y1 = y2; 00150 00151 //pt2 = ptTemp; 00152 x2 = tmp_x; 00153 y2 = tmp_y; 00154 } 00155 00156 for (i = y1; i <= y2; i++) // Draw points 00157 { 00158 draw_pixel(x1,i,color); 00159 } 00160 return; 00161 } 00162 00163 if (y1 == y2) // Case equation y = m. 00164 { 00165 if (x1 > x2) // Swap 2 points 00166 { 00167 //ptTemp = pt1; 00168 tmp_x = x1; 00169 tmp_y = y1; 00170 00171 //pt1 = pt2; 00172 x1 = x2; 00173 y1 = y2; 00174 00175 //pt2 = ptTemp; 00176 x2 = tmp_x; 00177 y2 = tmp_y; 00178 } 00179 00180 for (i = x1; i <= x2; i++) // Draw points 00181 { 00182 draw_pixel(i,y1,color); 00183 } 00184 return; 00185 } 00186 00187 // Case equation y = ax + b 00188 float fA = (((float)(y2 - y1))) / (x2 - x1); // Calculate a. 00189 float fB = y1 - fA * x1; // Calculate b. 00190 00191 if (x1 > x2) // Swap 2 points 00192 { 00193 //ptTemp = pt1; 00194 tmp_x = x1; 00195 tmp_y = y1; 00196 00197 //pt1 = pt2; 00198 x1 = x2; 00199 y1 = y2; 00200 00201 //pt2 = ptTemp; 00202 x2 = tmp_x; 00203 y2 = tmp_y; 00204 } 00205 00206 // Calculate every points based on the equation 00207 for (i = x1; i <= x2; i++) 00208 { 00209 draw_pixel(i,(int)floor(fA * i + fB),color); 00210 } 00211 } 00212 00213 void graphicFrameworkCanvas::drawBresenham1(int x1, int y1, int x2, int y2, 00214 uint8_t* color) 00215 { 00216 int iDx = abs(x2 - x1); 00217 int iDy = abs(y2 - y1); 00218 int iTwoDy = 2 * iDy; 00219 int iTwoDyMinusDx = 2 * (iDy - iDx); 00220 int iP = iTwoDy - iDx; 00221 int iX, iY, iXEnd; 00222 00223 // Get smaller x to base 00224 if (x1 > x2) 00225 { 00226 iX = x2; 00227 iY = y2; 00228 iXEnd = x1; 00229 } 00230 else 00231 { 00232 iX = x1; 00233 iY = y1; 00234 iXEnd = x2; 00235 } 00236 00237 // Draw first point 00238 draw_pixel(iX,iY,color); 00239 00240 while (iX < iXEnd) 00241 { 00242 iX++; 00243 if (iP < 0) 00244 iP += iTwoDy; 00245 else 00246 { 00247 iY++; 00248 iP += iTwoDyMinusDx; 00249 } 00250 00251 // Draw points 00252 draw_pixel(iX,iY,color); 00253 } 00254 } 00255 00256 void graphicFrameworkCanvas::drawBresenham2(int x1, int y1, int x2, int y2, uint8_t* color) 00257 { 00258 int iDx = abs(x2 - x1); 00259 int iDy = abs(y2 - y1); 00260 int iTwoDy = 2 * iDy; 00261 int iTwoDyMinusDx = 2 * (iDy - iDx); 00262 int iP = -iTwoDy - iDx; 00263 int iX, iY, iXEnd; 00264 00265 // Get smaller x to base 00266 if (x1 > x2) 00267 { 00268 iX = x2; 00269 iY = y2; 00270 iXEnd = x1; 00271 } 00272 else 00273 { 00274 iX = x1; 00275 iY = y1; 00276 iXEnd = x2; 00277 } 00278 00279 // Draw first point 00280 draw_pixel(iX,iY,color); 00281 00282 while (iX < iXEnd) 00283 { 00284 iX++; 00285 if (iP < 0) 00286 iP += iTwoDy; 00287 else 00288 { 00289 iY--; 00290 iP += iTwoDyMinusDx; 00291 } 00292 00293 // Draw points 00294 draw_pixel(iX,iY,color); 00295 } 00296 } 00297 00298 void graphicFrameworkCanvas::drawBresenham3(int x1, int y1, int x2, int y2, 00299 uint8_t* color) 00300 { 00301 int iDx = abs(x2 - x1); 00302 int iDy = abs(y2 - y1); 00303 int iTwoDx = 2 * iDx; 00304 int iTwoDxMinusDy = 2 * (iDx - iDy); 00305 int iP = iTwoDx - iDy; 00306 int iX, iY, iYEnd; 00307 00308 // Get smaller x to base 00309 if (y1 > y2) 00310 { 00311 iX = x2; 00312 iY = y2; 00313 iYEnd = y1; 00314 } 00315 else 00316 { 00317 iX = x1; 00318 iY = y1; 00319 iYEnd = y2; 00320 } 00321 00322 // Draw first point 00323 draw_pixel(iX,iY,color); 00324 00325 while (iY < iYEnd) 00326 { 00327 iY++; 00328 if (iP < 0) 00329 iP += iTwoDx; 00330 else 00331 { 00332 iX++; 00333 iP += iTwoDxMinusDy; 00334 } 00335 // Draw points 00336 draw_pixel(iX,iY,color); 00337 } 00338 } 00339 00340 void graphicFrameworkCanvas::drawBresenham4(int x1, int y1, int x2, int y2, 00341 uint8_t* color) 00342 { 00343 int iDx = abs(x2 - x1); 00344 int iDy = abs(y2 - y1); 00345 int iTwoDx = 2 * iDx; 00346 int iTwoDxMinusDy = 2 * (iDx - iDy); 00347 int iP = -iTwoDx - iDy; 00348 int iX, iY, iYEnd; 00349 00350 // Get smaller x to base 00351 if (y1 > y2) 00352 { 00353 iX = x1; 00354 iY = y1; 00355 iYEnd = y2; 00356 } 00357 else 00358 { 00359 iX = x2; 00360 iY = y2; 00361 iYEnd = y1; 00362 } 00363 00364 // Draw first point 00365 draw_pixel(iX,iY,color); 00366 00367 while (iY > iYEnd) 00368 { 00369 iY--; 00370 if (iP < 0) 00371 iP += iTwoDx; 00372 else 00373 { 00374 iX++; 00375 iP += iTwoDxMinusDy; 00376 } 00377 00378 // Draw points 00379 draw_pixel(iX,iY,color); 00380 } 00381 } 00382 00383 void graphicFrameworkCanvas::drawLine(int x1, int y1, int x2, int y2, 00384 uint8_t* color) 00385 { 00386 if (x1 == x2 || y1 == y2) // Case equation x = m or y = m. 00387 { 00388 drawLineNormal(x1,y1,x2,y2,color); 00389 return; 00390 } 00391 00392 // Case equation y = ax + b 00393 float fA = ((float)(y2 - y1)) / (x2 - x1); // Calculate a. 00394 00395 // Case (0 < m < 1). 00396 if (fA > 0 && fA < 1) 00397 { 00398 drawBresenham1(x1,y1,x2,y2,color); 00399 return; 00400 } 00401 00402 // Case (-1 < m < 0). 00403 if (fA > -1 && fA < 0) 00404 { 00405 drawBresenham2(x1,y1,x2,y2,color); 00406 return; 00407 } 00408 00409 // Case (m >= 1). 00410 if (fA >= 1) 00411 { 00412 drawBresenham3(x1,y1,x2,y2,color); 00413 return; 00414 } 00415 00416 // Case (m =< -1). 00417 if (fA <= -1) 00418 { 00419 drawBresenham4(x1,y1,x2,y2,color); 00420 return; 00421 } 00422 } 00423 00424 void graphicFrameworkCanvas::drawCircle(int x0,int y0, int radius, uint8_t* color) 00425 { 00426 int x = radius; 00427 int y = 0; 00428 int err = 0; 00429 00430 while (x >= y) 00431 { 00432 // Draw points 00433 draw_pixel(x0 + x,y0 + y,color); 00434 00435 draw_pixel(x0 + y, y0 + x,color); 00436 draw_pixel(x0 - y, y0 + x,color); 00437 draw_pixel(x0 - x, y0 + y,color); 00438 draw_pixel(x0 - x, y0 - y,color); 00439 draw_pixel(x0 - y, y0 - x,color); 00440 draw_pixel(x0 + y, y0 - x,color); 00441 draw_pixel(x0 + x, y0 - y,color); 00442 00443 y += 1; 00444 if (err <= 0) 00445 { 00446 err += 2*y + 1; 00447 } else 00448 { 00449 x -= 1; 00450 err += 2 * (y - x) + 1; 00451 } 00452 } 00453 }
Generated on Tue Jul 12 2022 15:17:28 by
1.7.2
