Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.56 KB | None | 0 0
  1. /*
  2.  * bmpServer.c
  3.  * 1917 serve that 3x3 bmp from lab3 Image activity
  4.  *
  5.  * Created by Tim Lambert on 02/04/12.
  6.  * Containing code created by Richard Buckland on 28/01/11.
  7.  * Copyright 2012 Licensed under Creative Commons SA-BY-NC 3.0.
  8.  *
  9.  */
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <netinet/in.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include <unistd.h>
  17. #include <math.h>
  18. #include "pixelColor.c"
  19. #include "mandelbrot.c"
  20.  
  21. #define FALSE 0
  22. #define TRUE 1
  23.  
  24. int waitForConnection(int serverSocket);
  25. int makeServerSocket(int portno);
  26. //static void serveHTML(int socket);
  27. void serveBMP(int socket);
  28. unsigned char stepsToRed(int steps);
  29. unsigned char stepsToBlue(int steps);
  30. unsigned char stepsToGreen(int steps);
  31. int escapeSteps (double x, double y);
  32. double zoomLevel(int zoomAmount);
  33.  
  34. #define SIMPLE_SERVER_VERSION 1.0
  35. #define REQUEST_BUFFER_SIZE 1000
  36. #define DEFAULT_PORT 7191
  37. #define NUMBER_OF_PAGES_TO_SERVE 10
  38. // after serving this many pages the server will halt
  39.  
  40. #define FILESIZE 786486
  41. #define MAX_ITERATION 256
  42. #define WIDTH 512.0
  43. #define HEIGHT 512.0
  44. #define max_im 2.0
  45. #define min_im -2.0
  46. #define max_re 2.0
  47. #define min_re -2.0
  48.  
  49. int main (int argc, char *argv[]) {
  50.     printf ("************************************\n");
  51.     printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
  52.     printf ("Serving bmps since 2012\n");
  53.     int serverSocket = makeServerSocket (DEFAULT_PORT);
  54.     printf ("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
  55.     printf ("************************************\n");
  56.     char request[REQUEST_BUFFER_SIZE];
  57.     int numberServed = 0;
  58.     while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
  59.         printf ("*** So far served %d pages ***\n", numberServed);
  60.         int connectionSocket = waitForConnection (serverSocket);
  61.         // wait for a request to be sent from a web browser, open a new
  62.         // connection for this conversation
  63.         // read the first line of the request sent by the browser
  64.         int bytesRead;
  65.         bytesRead = read (connectionSocket, request, (sizeof request)-1);
  66.         assert (bytesRead >= 0);
  67.         // were we able to read any data from the connection?
  68.         // print entire request to the console
  69.         printf (" *** Received http request ***\n %s\n", request);
  70.         //send the browser a simple html page using http
  71.         printf (" *** Sending http response ***\n");
  72.        
  73.        
  74.         serveBMP(connectionSocket);
  75.         // close the connection after sending the page- keep aust beautiful
  76.         close(connectionSocket);
  77.         numberServed++;
  78.     }
  79.     // close the server connection after we are done- keep aust beautiful
  80.     printf ("** shutting down the server **\n");
  81.     close (serverSocket);
  82.     return EXIT_SUCCESS;
  83. }
  84.  
  85. void serveBMP (int socket) {
  86.     char* message;
  87.     // first send the http response header
  88.     // (if you write stings one after another like this on separate
  89.     // lines the c compiler kindly joins them together for you into
  90.     // one long string)
  91.     message = "HTTP/1.0 200 OK\r\n"
  92.     "Content-Type: image/bmp\r\n"
  93.     "\r\n";
  94.     printf ("about to send=> %s\n", message);
  95.     write (socket, message, strlen (message));
  96.     // now send the BMP
  97.     //unsigned char blackPixel[] = {0xFF, 0xFF, 0xFF};
  98.     //unsigned char whitePixel[] = {0x00, 0x00, 0x00};
  99.    
  100.     unsigned char bmp[] = {
  101.         0x42,0x4d,0x36,0x00,0x0c,0x00,0x00,0x00,
  102.         0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,
  103.         0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,
  104.         0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,
  105.         0x00,0x00,0x24,0x00,0x00,0x00,0x13,0x0b,
  106.         0x00,0x00,0x13,0x0b,0x00,0x00,0x00,0x00,
  107.         0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x07,
  108.         0xff,0x07,0x07,0x07,0x07,0x07,0xff,0x00,
  109.         0x00,0x0e,0x07,0x07,0x07,0x66,0x07,0x07,
  110.         0x07,0x07,0x07,0x00,0x00,0x0d,0x07,0x07,
  111.         0x07,0x07,0x07,0x07,0xff,0xff,0xff,0x00,
  112.         0x00,0x0d};
  113.    
  114.     double x = 0.15;
  115.     double y = 0.15;
  116.     double z = 8;
  117.    
  118.     double minReal;
  119.     double maxReal;
  120.    
  121.     double maxImg;
  122.     double minImg;
  123.    
  124.     double yImgDist;
  125.     double xRealDist;
  126.    
  127.     z = zoomLevel(z);
  128.    
  129.     minReal = x - (z * 511) / 2;
  130.     maxReal = x + (z * 511) / 2;
  131.    
  132.     maxImg = y + (z * 511) / 2;
  133.     minImg = y - (z * 511) / 2;
  134.    
  135.     yImgDist = (maxImg - minImg) / HEIGHT;
  136.     xRealDist = (maxReal - minReal) / WIDTH;
  137.    
  138.     int bmpByte = 54;
  139.     int steps;
  140.    
  141.     y = minImg;
  142.     while (y < maxImg) {
  143.         x = minReal;
  144.         while (x < maxReal && bmpByte < FILESIZE) {
  145.            
  146.             int resultCounter = escapeSteps(x, y);
  147.            
  148.             if (resultCounter == 0) {
  149.                 bmp[bmpByte] = 0;
  150.                 bmp[bmpByte + 1] = 0;
  151.                 bmp[bmpByte + 2] = 0;
  152.                
  153.             } else {
  154.                 bmp[bmpByte] = stepsToRed(steps);
  155.                 printf("%d\n", bmpByte);
  156.                 bmp[bmpByte + 1] = stepsToGreen(steps);
  157.                 printf("%d\n", bmpByte);
  158.                 bmp[bmpByte + 2] = stepsToBlue(steps);
  159.                 printf("%d\n", bmpByte);
  160.             }
  161.             x += xRealDist, bmpByte += 3;
  162.         }
  163.         y += yImgDist;
  164.     }
  165.    
  166.     write(socket, bmp, sizeof (bmp));
  167. }
  168.  
  169.  
  170. // start the server listening on the specified port number
  171. int makeServerSocket (int portNumber) {
  172.     // create socket
  173.     int serverSocket = socket (AF_INET, SOCK_STREAM, 0);
  174.     assert (serverSocket >= 0);
  175.     // error opening socket
  176.     // bind socket to listening port
  177.     struct sockaddr_in serverAddress;
  178.     bzero ((char *) &serverAddress, sizeof (serverAddress));
  179.     serverAddress.sin_family = AF_INET;
  180.     serverAddress.sin_addr.s_addr = INADDR_ANY;
  181.     serverAddress.sin_port = htons (portNumber);
  182.     // let the server start immediately after a previous shutdown
  183.     int optionValue = 1;
  184.     setsockopt (
  185.                 serverSocket,
  186.                 SOL_SOCKET,
  187.                 SO_REUSEADDR,
  188.                 &optionValue,
  189.                 sizeof(int)
  190.                 );
  191.    
  192.     int bindSuccess =
  193.     bind (
  194.           serverSocket,
  195.           (struct sockaddr *) &serverAddress,
  196.           sizeof (serverAddress)
  197.           );
  198.     assert (bindSuccess >= 0);
  199.     // if this assert fails wait a short while to let the operating
  200.     // system clear the port before trying again
  201.     return serverSocket;
  202. }
  203.  
  204. // wait for a browser to request a connection,
  205. // returns the socket on which the conversation will take place
  206. int waitForConnection (int serverSocket) {
  207.     // listen for a connection
  208.     const int serverMaxBacklog = 10;
  209.     listen (serverSocket, serverMaxBacklog);
  210.     // accept the connection
  211.     struct sockaddr_in clientAddress;
  212.     socklen_t clientLen = sizeof (clientAddress);
  213.     int connectionSocket =
  214.     accept (
  215.             serverSocket,
  216.             (struct sockaddr *) &clientAddress,
  217.             &clientLen
  218.             );
  219.     assert (connectionSocket >= 0);
  220.     // error on accept
  221.     return (connectionSocket);
  222. }
  223.  
  224. // -------------------------------------------------------------------------------------------------------------------------
  225.  
  226. //
  227. //  pixelColor.c
  228. //  BMPserver.c
  229. //
  230. //  Created by Andrew Braga on 19/04/2014.
  231. //  Copyright (c) 2014 Andrew Braga. All rights reserved.
  232. //
  233.  
  234. #include <stdio.h>
  235. #include <stdlib.h>
  236. #include "pixelColor.h"
  237.  
  238. #define MAX_STEPS 256
  239. #define BLACK 0
  240. #define WHITE 255
  241.  
  242. static unsigned char blackOrWhite (int steps);
  243.  
  244. unsigned char stepsToRed(int steps) {
  245.    
  246.     unsigned char intensity;
  247.    
  248.     intensity = blackOrWhite (steps);
  249.    
  250.     return intensity;
  251.    
  252. }
  253.  
  254.  
  255.  
  256. unsigned char stepsToBlue(int steps) {
  257.    
  258.     unsigned char intensity;
  259.    
  260.     intensity = blackOrWhite (steps);
  261.    
  262.     return intensity;
  263.    
  264. }
  265.  
  266.  
  267.  
  268. unsigned char stepsToGreen(int steps) {
  269.    
  270.     unsigned char intensity;
  271.    
  272.     intensity = blackOrWhite (steps);
  273.    
  274.     return intensity;
  275.    
  276. }
  277.  
  278.  
  279.  
  280. static unsigned char blackOrWhite (int steps) {
  281.    
  282.     unsigned char intensity;
  283.    
  284.     if (steps == MAX_STEPS) {
  285.        
  286.         intensity = BLACK;
  287.        
  288.     } else {
  289.        
  290.         intensity =WHITE;
  291.        
  292.     }
  293.    
  294.     return intensity;
  295.    
  296. }
  297.  
  298. // -------------------------------------------------------------------------------------------------------------------------
  299.  
  300. //
  301. //  mandelbrot.c
  302. //  BMPserver.c
  303. //
  304. //  Created by Andrew Braga on 19/04/2014.
  305. //  Copyright (c) 2014 Andrew Braga. All rights reserved.
  306. //
  307.  
  308. #include <stdio.h>
  309. #include <stdlib.h>
  310. #include "mandelbrot.h"
  311.  
  312. int escapeSteps (double x, double y);
  313. double zoomLevel(int zoomExtent);
  314.  
  315.  
  316. // Function to test if the mandelbrot set is inside the set or not
  317.  
  318. int escapeSteps (double x, double y) {
  319.    
  320.     double zReal = 0;
  321.     double nextZReal = 0;
  322.     double zImg = 0;
  323.     double nextZImg = 0;
  324.     int count = 0;
  325.     int MAX_ITERATION = 256;
  326.    
  327.    
  328.     while (((zReal * zReal) + (zImg * zImg) <= 4) && (count < MAX_ITERATION)) {
  329.         nextZReal = (zReal * zReal) - (zImg * zImg) + x;
  330.         nextZImg = (2 * zReal * zImg) + y;
  331.        
  332.         zReal = nextZReal;
  333.         zImg = nextZImg;
  334.        
  335.         count++;
  336.        
  337.     }
  338.    
  339.     return count;
  340. }
  341.  
  342.  
  343. // Function to determine the distance between each pixel
  344.  
  345. double zoomLevel(int zoomExtent) {
  346.    
  347.     double distance = 0.0;
  348.     int zoomCount = 1;
  349.     double zoom = 2;
  350.    
  351.     while (zoomCount < zoomExtent) {
  352.        
  353.         zoomCount++;
  354.         zoom *= 2;
  355.        
  356.     }
  357.    
  358.     distance = (1 / zoom);
  359.    
  360.     return distance;
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement