Search code examples
cbmp

How to generate an image using C code


The function below simply serves html when it is called, however

void generateHTML (int socket) {
   char* message;

   // Sends HTTP response header

   message = "HTTP/1.0 200 OK\r\n"
                "Content-Type: text/html\r\n"
                "\r\n";
   printf ("about to send=> %s\n", message);
   write (socket, message, strlen (message));

   message = "<HTML><BODY><P>Hello World.</P></BODY></HTML>\n";
   printf ("about to send=> %s\n", message);
   write (socket, message, strlen (message));   
}

My output on the web browser would by a simple Hello World message. However, I want to change it so that it will display a bitmap image instead. Let's use a 1x1 red pixel as our bmp.

I've modified this function so far by:

void generateHTML (int socket) {
   char* message;

   // Sends HTTP response header

   message = "HTTP/1.0 200 OK\r\n"
                "Content-Type: image/bmp\r\n"
                "Content-Length: ???WTF???\r\n";
   printf ("about to send=> %s\n", message);
   write (socket, message, strlen (message));

   message = "BMF8\n";
   printf ("about to send=> %s\n", message);
   write (socket, message, strlen (message));   

   message = " "; //bmp file data goes here.
   printf ("about to send=> %s\n", message);
   write (socket, message, strlen (message));   
}

Quoting from Dan's answer, the data in hex looks like:

0000000: 424d 3a00 0000 0000 0000 3600 0000 2800  BM:.......6...(.
0000010: 0000 0100 0000 0100 0000 0100 1800 0000  ................
0000020: 0000 0400 0000 130b 0000 130b 0000 0000  ................
0000030: 0000 0000 0000 0000 0000                 ..........

However, I simply cannot place that inside the quotation marks. How would I do this?


Solution

  • this is an xxd dump of a 1x1 black windows bmp image:

    0000000: 424d 3a00 0000 0000 0000 3600 0000 2800  BM:.......6...(.
    0000010: 0000 0100 0000 0100 0000 0100 1800 0000  ................
    0000020: 0000 0400 0000 130b 0000 130b 0000 0000  ................
    0000030: 0000 0000 0000 0000 0000                 ..........