//----Writes a Single Pixel----// void writePixel (int socket, int iterations, bits8 byte){ if (iterations == ITERATION_MAX){ byte = stepsToRed(iterations); write (socket, &byte, sizeof(byte)); byte = stepsToBlue(iterations); write (socket, &byte, sizeof(byte)); byte = stepsToGreen(iterations); write (socket, &byte, sizeof(byte)); } else { byte = stepsToRed(iterations); write (socket, &byte, sizeof(byte)); byte = stepsToBlue(iterations); write (socket, &byte, sizeof(byte)); byte = stepsToGreen(iterations); write (socket, &byte, sizeof(byte)); } }
void serveBMP (int socket, double x, double y, int zoom) { char* message; zoom = zoom +4; // first send the http response header // (if you write strings one after another like this on separate // lines the c compiler kindly joins them togther for you into // one long string) message = "HTTP/1.0 200 OK\r\n" "Content-Type: image/bmp\r\n" "\r\n"; printf ("about to send=> %s\n", message); write (socket, message, strlen (message)); writeHeader (socket); int totalBytes = (SIZE * SIZE * BYTES_PER_PIXEL); int position = 0; while (position < totalBytes) { int steps = blackOrWhite(position, x, y, zoom); unsigned char red = stepsToRed (steps); unsigned char blue = stepsToBlue (steps); unsigned char green = stepsToGreen (steps); write (socket, &red, 1); write (socket, &blue, 1); write (socket, &green, 1); position++; } }
void serveBMP (int socket, dim theDim) { char* message; double x,y; int steps; // first send the http response header // (if you write strings one after another like this on separate // lines the c compiler kindly joins them togther for you into // one long string) message = "HTTP/1.0 200 OK\r\n" "Content-Type: image/bmp\r\n" "\r\n"; printf ("about to send=> %s\n", message); write (socket, message, strlen (message)); // now send the contents of the web page to be displayed unsigned char bmpHead [] = { 0x42,0x4D,0x36,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; // This is the bmp header, that goes at the start of the image printf ("about to send=> bmp Header\n"); write (socket, bmpHead, sizeof (bmpHead)); printf ("about to send=> bmp main\n"); for (y=theDim.ystart; y<theDim.yend; y+=theDim.distance) { for (x=theDim.xstart; x<theDim.xend; x+=theDim.distance) { steps = noOfSteps(x,y); unsigned char color[] = {stepsToBlue(steps), stepsToGreen(steps),stepsToRed(steps)}; write (socket, color, sizeof (color)); } // printf("%d", noOfSteps(x,y)); } }
void mandelBitmap(char *image, double x, double y, int zoom) { double pixelDistance = pow(2, -zoom); double bottom = y - pixelDistance * (IMAGE_HEIGHT / 2); double left = x - pixelDistance * (IMAGE_WIDTH / 2); bitmapHeader(image); int offset = BMP_HEADER_SIZE; int row = 0; while (row < IMAGE_HEIGHT) { int col = 0; while (col < IMAGE_WIDTH) { int steps = escapeSteps( left + pixelDistance * col, bottom + pixelDistance * row); image[offset] = stepsToBlue(steps); image[offset + 1] = stepsToGreen(steps); image[offset + 2] = stepsToRed(steps); offset += 3; col += 1; } row += 1; } }
void serveBMP (int socket, char * request) { char* message; // the http response header message = "HTTP/1.0 200 OK\r\n" "Content-Type: image/bmp\r\n" "\r\n"; printf ("about to send=> %s\n", message); write (socket, message, strlen (message)); int numBytes = (SIZE * SIZE * BYTES_PER_PIXEL); unsigned char bmp[(SIZE * BYTES_PER_PIXEL * SIZE)] = {0,}; writeHeader (socket); triordinate input; input = extract (request); // printf("centre: %lf, %lf. zoom %d\n", input.x, input.y, input.z); // unsigned char byte = 0; int height = 0; int width = 0; double increment; increment = 1.0 / (positivePower(2.0, input.z)); // printf("zoom: %d\n", input.z); // printf("increment: %lf\n", increment); double x = input.x - 256 * increment; // far left side double y = input.y - 256 * increment; // bottom level // printf("lower left corner: %lf, %lf\n", x, y); int steps = 0; double xCounter = x; int counter = 0; while (height < SIZE) { width = 0; xCounter = x; while (width < SIZE) { steps = escapeSteps(xCounter, y); bmp[counter] = stepsToBlue(steps); counter ++; bmp[counter] = stepsToGreen(steps); counter ++; bmp[counter] = stepsToRed(steps); counter ++; width ++; xCounter = xCounter + increment; } y = y + increment; height ++; } assert (sizeof(bmp) == numBytes); write (socket, bmp, sizeof(bmp)); }
void generateBitmapImage(unsigned char bmpData[], double centreX, double centreY, int zoom) { int drawingX; int drawingY; // The index of the desired row int bitmapRow; // The index of the desired column int bitmapColumn; double scaledX, scaledY; double scale; int iterations; scale = 1 / power(2, zoom); drawingY = 0; while (drawingY < BITMAP_HEIGHT) { drawingX = 0; while (drawingX < BITMAP_WIDTH) { // centering scaledX = centreX -(BITMAP_WIDTH / 2) * scale; scaledY = centreY -(BITMAP_HEIGHT / 2) * scale; // calculate current point scaledX += drawingX * scale; scaledY += drawingY * scale; bitmapRow = drawingY * (BITMAP_WIDTH * BITMAP_BYTES_PER_PIXEL); bitmapColumn = drawingX * BITMAP_BYTES_PER_PIXEL; iterations = escapeSteps(scaledX, scaledY); bmpData[bitmapRow + bitmapColumn] = stepsToBlue(iterations); bmpData[bitmapRow + bitmapColumn + 1] = stepsToGreen(iterations);; bmpData[bitmapRow + bitmapColumn + 2] = stepsToRed(iterations);; drawingX++; } drawingY++; } }
static void serveBMP (int socket, triordinate dat) { char* message; // first send the http response header // (if you write stings one after another like this on separate // lines the c compiler kindly joins them togther for you into // one long string) message = "HTTP/1.0 200 OK\r\n" "Content-Type: image/bmp\r\n" "\r\n"; printf ("about to send=> %s\n", message); if(write(socket, message, strlen (message)) == 1){}; writeHeader(socket); int counterX = 0; int counterY = -1; unsigned char byte[1]; int steps = 0; double dBetweenPixels = pow(2, (-1*dat.z)); double currentX = dat.x - 0.5*dBetweenPixels*(SIZE+1); double currentY = dat.y - 0.5*dBetweenPixels*(SIZE+1); while (counterY < SIZE) { currentX = dat.x - 0.5*dBetweenPixels*(SIZE+1); while (counterX < SIZE){ steps = escapeSteps(currentX, currentY); byte[0] = stepsToBlue(steps); if(write(socket, byte, sizeof byte) == 1){}; byte[0] = stepsToGreen(steps); if(write(socket, byte, sizeof byte) == 1){}; byte[0] = stepsToRed(steps); if(write(socket, byte, sizeof byte) == 1){}; currentX += dBetweenPixels; counterX++; } counterX = 0; currentY += dBetweenPixels; counterY++; } }
int main (int argc, char *argv[]) { assert (sizeof(bits8) == 1); assert (sizeof(bits16) == 2); assert (sizeof(bits32) == 4); FILE *outputFile; outputFile = fopen(BMP_FILE, "wb"); assert ((outputFile!=NULL) && "Cannot open file"); writeHeader(outputFile); printf ("generating mandelbrot picture"); int numBytes = (SIZE * SIZE * BYTES_PER_PIXEL); int pos = 0; bits8 byte; byte = BLACK; int xcoord = -256; int ycoord = -256; double zre; double zim; int escape; int red = 0; int green = 0; int blue = 0; while (pos < numBytes) { int printno; zre = xcoord/256.000000000000000 - 0.5; zim = ycoord/256.000000000000000; escape = escapeSteps(zre, zim); if(escape < BOUNDED) { red = BLACK; green = BLACK; blue = BLACK; } else { red = stepsToRed(escape); green = stepsToGreen(escape); blue = stepsToBlue(escape); } for (printno = 0; printno < BYTES_PER_PIXEL; printno++) { if (printno == 0) { byte = red; } else if (printno == 1){ byte = green; } else if (printno == 2){ byte = blue; } fwrite (&byte, sizeof byte, 1, outputFile); pos++; } xcoord = xcoord + 1; if (pos%SIZE == 0) { xcoord = -256; ycoord = ycoord + 1; } } fclose(outputFile); return EXIT_SUCCESS; }
void serveBMP (int socket) { char* message; // first send the http response header // (if you write stings one after another like this on separate // lines the c compiler kindly joins them togther for you into // one long string) message = "HTTP/1.0 200 OK\r\n" "Content-Type: image/bmp\r\n" "\r\n"; printf ("about to send=> %s\n", message); write (socket, message, strlen (message)); // now send the BMP unsigned char bmp[FILE_SIZE] = { 0x42,0x4d,0x63,0x00,0x0c,0x00,0x00,0x00, 0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00, 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02, 0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x0c,0x00,0xc3,0x0e, 0x00,0x00,0xc3,0x0e,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00}; int numBytes = (SIZE * SIZE * BYTES_PER_PIXEL); int pos = 0; int xcoord = -256; int ycoord = -256; double zre; double zim; int escape; int bmpcount = 54; int zoom = 1; while (zurl != 0) { zoom = 2*zoom; zurl = zurl - 1; } zoom = zoom*256.000000000000000; while (pos < numBytes) { zre = xcoord/zoom + xurl; zim = ycoord/zoom + yurl; escape = escapeSteps(zre, zim); if(escape == BOUNDED) { bmp[bmpcount] = BLACK; bmp[bmpcount + 1] = BLACK; bmp[bmpcount + 2] = BLACK; } else { bmp[bmpcount] = stepsToRed(escape); bmp[bmpcount + 1] = stepsToGreen(escape); bmp[bmpcount + 2] = stepsToBlue(escape); } pos +=3; bmpcount +=3; xcoord = xcoord + 1; if (pos%SIZE == 0) { xcoord = -256; ycoord = ycoord + 1; } } write (socket, bmp, sizeof(bmp)); }
// Serving HTML/Content function static void serveHTML (int socket) { // Initalizing message string. char *message; // first send the http response header // (if you write stings one after another like this on separate // lines the c compiler kindly joins them togther for you into // one long string) message = "HTTP/1.0 200 OK\r\n" "Content-Type: image/bmp\r\n" "\r\n"; printf ("about to send=> %s\n", message); write (socket, message, strlen (message)); // Initalizing variables. double i,j,x,y,x0,y0,xTemp; int iteration; // Creating loader pixel struct to serve pixel colors to BMP array pixel loaderPixel; // Creating and filling out array of BMP data. unsigned char bmp[BMP_ARRAY_SIZE] = { 0x42,0x4D,0x36,0x00,0x0C,0x00,0x00,0x00, 0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00, 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02, 0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00}; // After the header (54th byte) we include the pixel data. int start = BMP_Header_BYTE; // Row Loop. for(i=0.0;i<ROWS;i++){ // Column Loop. for(j=0.0;j<COLUMNS;j++){ // Reseting x,y to zero. y = 0; x = 0; // Scale X,Y points to within the Mandelbrot Set. x0 = scaleX(j); y0 = scaleY(i); // Reset iteration counter. iteration = 0; // Calculation code paraphrased from Mandelbrot Wikipedia website. while ((x*x + y*y) < 4.0 && iteration<MAX_ITERATION) { xTemp = x*x - y*y + x0; y = 2.0*x*y + y0; x = xTemp; iteration++; } // Printing out color pixels corresponding to pixel color functions. loaderPixel.red = stepsToRed(iteration); loaderPixel.green = stepsToGreen(iteration); loaderPixel.blue = stepsToBlue(iteration); // Increment through Bytes & include numbers for all BGR. start++; bmp[start] = loaderPixel.blue; start++; bmp[start] = loaderPixel.green; start++; bmp[start] = loaderPixel.red; } } // Now send the contents of the web page to be displayed printf ("about to send=> %s\n", message); write (socket, bmp, sizeof(bmp)); }