void Graph::halfTone(Color color){ int megaPixel = 3; Vector v; Color tmp; int numberOfPixels; //backup before doing half-toning backupPixelBuffer(); for(int r = 0 ; r < window_height; r+=megaPixel){ //row for(int c = 0 ; c < window_width; c+=megaPixel){//col v = {0,0,0}; for(int i = r; i < r+megaPixel ; i++){ // 3 by 3 for(int j = c; j < c+megaPixel; j++ ){ //DPRINT("r %d, c %d, i %d, j %d\n", r, c, i,j); tmp = readPixel(j,i); if( equal(tmp, background_color) ) v = add( v, {1, 1, 1} ); else v = add( v, ctov( tmp ) ); } } //write each mega pixel numberOfPixels = round( 9- (v.x + v.y + v.z) / 3 ); drawMegaPixel(numberOfPixels, megaPixel * 3, r, c, color); } } }
// This is a Bresenham-based linescan function using mega pixels // All cases use similar structure to the standard case, just with different values for dFa, dFb, and p void loeschLineScanMega(My2DPoint p1, My2DPoint p2) { if (p1.x > p2.x) // if the points are in the wrong order, swap them first swapPoints(p1, p2); // declare and initialize values int dY = p2.y - p1.y; int dX = p2.x - p1.x; float m = (float)dY / (float)dX; // calculate and store slope int dFa; int dFb; int p; int currX = 0; int currY = 0; if (dX == 0) { // vertical line if (p1.y < p2.y) { // if p1 is lower, draw starting from p1 for (int i = 0; i < dY - (dY % 5); i += 5) drawMegaPixel(p1.x, p1.y + i); } else // otherwise draw starting from p2 { for (int i = 0; i < dY - (dY % 5); i += 5) drawMegaPixel(p2.x, p2.y + i); } } else if (m >= 0.0 && m <= 1.0) { // standard case ( 0 <= m <= 1 ) // calculate dFa and dFb, as well as the initial value for p dFa = 10 * dY - 10 * dX; dFb = 10 * dY; p = 10 * dY - 5 * dX; // draw the initial point drawMegaPixel(p1.x, p1.y); for (int k = 5; k <= dX - (dX % 5); k += 5) { // k is the current x value if (p < 0) // if p is below the midpoint, use dFb p += dFb; else { // otherwise p is above midpoint, use dFa currY += 5; p += dFa; } drawMegaPixel(p1.x + k, p1.y + currY); // draw a pixel at the current x and y } } else if (m > 1.0) { // slope greater than 1 ( m < 1 ) dFa = 10 * (dX - dY); dFb = 10 * dX; p = 10 * dX - 5 * dY; drawMegaPixel(p1.x, p1.y); for (int k = 5; k <= dY - (dY % 5); k += 5) { // k is the current y value if (p < 0) p += dFb; else { currX += 5; p += dFa; } drawMegaPixel(p1.x + currX, p1.y + k); } } else if (m < 0.0 && m >= -1.0) { // negative slope ( 0 < m <= -1 ) dFa = 10 * (dY + dX); dFb = 10 * dY; p = 10 * dY + 5 * dX; drawMegaPixel(p1.x, p1.y); for (int k = 5; k <= dX - (dX % 5); k += 5) { // k is the current x value if (p > 0) p += dFb; else { currY += 5; p += dFa; } drawMegaPixel(p1.x + k, p1.y - currY); } } else { // slope must be negative and less than -1 ( m < -1 ) dY *= -1; // make dY positive (in order to emulate the case m < 1) dFa = 10 * (dX - dY); dFb = 10 * dX; p = 10 * dX - 5 * dY; drawMegaPixel(p1.x, p1.y); for (int k = 5; k <= dY - (dY % 5); k += 5) { // k is the current y value if (p < 0) p += dFb; else { currX += 5; p += dFa; } drawMegaPixel(p1.x + currX, p1.y - k); } } }