Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * bmpServer.c
- * 1917 serve that 3x3 bmp from lab3 Image activity
- *
- * Created by Tim Lambert on 02/04/12.
- * Containing code created by Richard Buckland on 28/01/11.
- * Copyright 2012 Licensed under Creative Commons SA-BY-NC 3.0.
- *
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <assert.h>
- #include <unistd.h>
- #include <math.h>
- #include "pixelColor.c"
- #include "mandelbrot.c"
- #define FALSE 0
- #define TRUE 1
- int waitForConnection(int serverSocket);
- int makeServerSocket(int portno);
- //static void serveHTML(int socket);
- void serveBMP(int socket);
- unsigned char stepsToRed(int steps);
- unsigned char stepsToBlue(int steps);
- unsigned char stepsToGreen(int steps);
- int escapeSteps (double x, double y);
- double zoomLevel(int zoomAmount);
- #define SIMPLE_SERVER_VERSION 1.0
- #define REQUEST_BUFFER_SIZE 1000
- #define DEFAULT_PORT 7191
- #define NUMBER_OF_PAGES_TO_SERVE 10
- // after serving this many pages the server will halt
- #define FILESIZE 786486
- #define MAX_ITERATION 256
- #define WIDTH 512.0
- #define HEIGHT 512.0
- #define max_im 2.0
- #define min_im -2.0
- #define max_re 2.0
- #define min_re -2.0
- int main (int argc, char *argv[]) {
- printf ("************************************\n");
- printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
- printf ("Serving bmps since 2012\n");
- int serverSocket = makeServerSocket (DEFAULT_PORT);
- printf ("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
- printf ("************************************\n");
- char request[REQUEST_BUFFER_SIZE];
- int numberServed = 0;
- while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
- printf ("*** So far served %d pages ***\n", numberServed);
- int connectionSocket = waitForConnection (serverSocket);
- // wait for a request to be sent from a web browser, open a new
- // connection for this conversation
- // read the first line of the request sent by the browser
- int bytesRead;
- bytesRead = read (connectionSocket, request, (sizeof request)-1);
- assert (bytesRead >= 0);
- // were we able to read any data from the connection?
- // print entire request to the console
- printf (" *** Received http request ***\n %s\n", request);
- //send the browser a simple html page using http
- printf (" *** Sending http response ***\n");
- serveBMP(connectionSocket);
- // close the connection after sending the page- keep aust beautiful
- close(connectionSocket);
- numberServed++;
- }
- // close the server connection after we are done- keep aust beautiful
- printf ("** shutting down the server **\n");
- close (serverSocket);
- 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 together 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 blackPixel[] = {0xFF, 0xFF, 0xFF};
- //unsigned char whitePixel[] = {0x00, 0x00, 0x00};
- unsigned char bmp[] = {
- 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,0x24,0x00,0x00,0x00,0x13,0x0b,
- 0x00,0x00,0x13,0x0b,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x07,
- 0xff,0x07,0x07,0x07,0x07,0x07,0xff,0x00,
- 0x00,0x0e,0x07,0x07,0x07,0x66,0x07,0x07,
- 0x07,0x07,0x07,0x00,0x00,0x0d,0x07,0x07,
- 0x07,0x07,0x07,0x07,0xff,0xff,0xff,0x00,
- 0x00,0x0d};
- double x = 0.15;
- double y = 0.15;
- double z = 8;
- double minReal;
- double maxReal;
- double maxImg;
- double minImg;
- double yImgDist;
- double xRealDist;
- z = zoomLevel(z);
- minReal = x - (z * 511) / 2;
- maxReal = x + (z * 511) / 2;
- maxImg = y + (z * 511) / 2;
- minImg = y - (z * 511) / 2;
- yImgDist = (maxImg - minImg) / HEIGHT;
- xRealDist = (maxReal - minReal) / WIDTH;
- int bmpByte = 54;
- int steps;
- y = minImg;
- while (y < maxImg) {
- x = minReal;
- while (x < maxReal && bmpByte < FILESIZE) {
- int resultCounter = escapeSteps(x, y);
- if (resultCounter == 0) {
- bmp[bmpByte] = 0;
- bmp[bmpByte + 1] = 0;
- bmp[bmpByte + 2] = 0;
- } else {
- bmp[bmpByte] = stepsToRed(steps);
- printf("%d\n", bmpByte);
- bmp[bmpByte + 1] = stepsToGreen(steps);
- printf("%d\n", bmpByte);
- bmp[bmpByte + 2] = stepsToBlue(steps);
- printf("%d\n", bmpByte);
- }
- x += xRealDist, bmpByte += 3;
- }
- y += yImgDist;
- }
- write(socket, bmp, sizeof (bmp));
- }
- // start the server listening on the specified port number
- int makeServerSocket (int portNumber) {
- // create socket
- int serverSocket = socket (AF_INET, SOCK_STREAM, 0);
- assert (serverSocket >= 0);
- // error opening socket
- // bind socket to listening port
- struct sockaddr_in serverAddress;
- bzero ((char *) &serverAddress, sizeof (serverAddress));
- serverAddress.sin_family = AF_INET;
- serverAddress.sin_addr.s_addr = INADDR_ANY;
- serverAddress.sin_port = htons (portNumber);
- // let the server start immediately after a previous shutdown
- int optionValue = 1;
- setsockopt (
- serverSocket,
- SOL_SOCKET,
- SO_REUSEADDR,
- &optionValue,
- sizeof(int)
- );
- int bindSuccess =
- bind (
- serverSocket,
- (struct sockaddr *) &serverAddress,
- sizeof (serverAddress)
- );
- assert (bindSuccess >= 0);
- // if this assert fails wait a short while to let the operating
- // system clear the port before trying again
- return serverSocket;
- }
- // wait for a browser to request a connection,
- // returns the socket on which the conversation will take place
- int waitForConnection (int serverSocket) {
- // listen for a connection
- const int serverMaxBacklog = 10;
- listen (serverSocket, serverMaxBacklog);
- // accept the connection
- struct sockaddr_in clientAddress;
- socklen_t clientLen = sizeof (clientAddress);
- int connectionSocket =
- accept (
- serverSocket,
- (struct sockaddr *) &clientAddress,
- &clientLen
- );
- assert (connectionSocket >= 0);
- // error on accept
- return (connectionSocket);
- }
- // -------------------------------------------------------------------------------------------------------------------------
- //
- // pixelColor.c
- // BMPserver.c
- //
- // Created by Andrew Braga on 19/04/2014.
- // Copyright (c) 2014 Andrew Braga. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include "pixelColor.h"
- #define MAX_STEPS 256
- #define BLACK 0
- #define WHITE 255
- static unsigned char blackOrWhite (int steps);
- unsigned char stepsToRed(int steps) {
- unsigned char intensity;
- intensity = blackOrWhite (steps);
- return intensity;
- }
- unsigned char stepsToBlue(int steps) {
- unsigned char intensity;
- intensity = blackOrWhite (steps);
- return intensity;
- }
- unsigned char stepsToGreen(int steps) {
- unsigned char intensity;
- intensity = blackOrWhite (steps);
- return intensity;
- }
- static unsigned char blackOrWhite (int steps) {
- unsigned char intensity;
- if (steps == MAX_STEPS) {
- intensity = BLACK;
- } else {
- intensity =WHITE;
- }
- return intensity;
- }
- // -------------------------------------------------------------------------------------------------------------------------
- //
- // mandelbrot.c
- // BMPserver.c
- //
- // Created by Andrew Braga on 19/04/2014.
- // Copyright (c) 2014 Andrew Braga. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include "mandelbrot.h"
- int escapeSteps (double x, double y);
- double zoomLevel(int zoomExtent);
- // Function to test if the mandelbrot set is inside the set or not
- int escapeSteps (double x, double y) {
- double zReal = 0;
- double nextZReal = 0;
- double zImg = 0;
- double nextZImg = 0;
- int count = 0;
- int MAX_ITERATION = 256;
- while (((zReal * zReal) + (zImg * zImg) <= 4) && (count < MAX_ITERATION)) {
- nextZReal = (zReal * zReal) - (zImg * zImg) + x;
- nextZImg = (2 * zReal * zImg) + y;
- zReal = nextZReal;
- zImg = nextZImg;
- count++;
- }
- return count;
- }
- // Function to determine the distance between each pixel
- double zoomLevel(int zoomExtent) {
- double distance = 0.0;
- int zoomCount = 1;
- double zoom = 2;
- while (zoomCount < zoomExtent) {
- zoomCount++;
- zoom *= 2;
- }
- distance = (1 / zoom);
- return distance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement