summaryrefslogtreecommitdiffstats
path: root/pnmshow.c
blob: 7c13d93c280fc9934d82b7215c42892e275f3366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include "rfb.h"
#include "keysym.h"

void HandleKey(Bool down,KeySym key,rfbClientPtr cl)
{
  if(down && (key==XK_Escape || key=='q' || key=='Q'))
    rfbCloseClient(cl);
}

int main(int argc,char** argv)
{
  FILE* in=stdin;
  int i,width,height;
  unsigned char buffer[1024];
  rfbScreenInfoPtr rfbScreen;

  if(argc>1) {
    in=fopen(argv[1],"rb");
    if(!in) {
      printf("Couldn't find file %s.\n",argv[1]);
      exit(1);
    }
  }

  fgets(buffer,1024,in);
  if(strncmp(buffer,"P6",2)) {
    printf("Not a ppm.\n");
    exit(2);
  }

  /* skip comments */
  do {
    fgets(buffer,1024,in);
  } while(buffer[0]=='#');

  /* get width & height */
  sscanf(buffer,"%d %d",&width,&height);
  fprintf(stderr,"Got width %d and height %d (%s).\n",width,height,buffer);
  fgets(buffer,1024,in);

  /* initialize data for vnc server */
  rfbScreen = rfbDefaultScreenInit(argc,argv,width,height,8,3,4);
  if(argc>1)
    rfbScreen->desktopName = argv[1];
  else
    rfbScreen->desktopName = "Picture";
  rfbScreen->rfbAlwaysShared = TRUE;
  rfbScreen->kbdAddEvent = HandleKey;

  /* enable http */
  rfbScreen->httpDir = "./classes";

  /* allocate picture and read it */
  rfbScreen->frameBuffer = (char*)malloc(width*height*4);
  fread(rfbScreen->frameBuffer,width*3,height,in);
  fclose(in);

  /* correct the format to 4 bytes instead of 3 */
  for(i=width*height-1;i>=0;i--) {
    rfbScreen->frameBuffer[i*4+2]=rfbScreen->frameBuffer[i*3+2];
    rfbScreen->frameBuffer[i*4+1]=rfbScreen->frameBuffer[i*3+1];
    rfbScreen->frameBuffer[i*4+0]=rfbScreen->frameBuffer[i*3+0];
  }

  /* run event loop */
  runEventLoop(rfbScreen,40000,FALSE);

  return(0);
}