add libvncserver
[presencevnc] / libvnc / examples / pnmshow.c
1 #include <stdio.h>
2 #include <rfb/rfb.h>
3 #include <rfb/keysym.h>
4
5 #ifndef HAVE_HANDLEKEY
6 static void HandleKey(rfbBool down,rfbKeySym key,rfbClientPtr cl)
7 {
8   if(down && (key==XK_Escape || key=='q' || key=='Q'))
9     rfbCloseClient(cl);
10 }
11 #endif
12
13 int main(int argc,char** argv)
14 {
15   FILE* in=stdin;
16   int i,j,k,l,width,height,paddedWidth;
17   char buffer[1024];
18   rfbScreenInfoPtr rfbScreen;
19   enum { BW, GRAY, TRUECOLOUR } picType=TRUECOLOUR;
20   int bytesPerPixel,bitsPerPixelInFile;
21
22   if(argc>1) {
23     in=fopen(argv[1],"rb");
24     if(!in) {
25       printf("Couldn't find file %s.\n",argv[1]);
26       exit(1);
27     }
28   }
29
30   fgets(buffer,1024,in);
31   if(!strncmp(buffer,"P6",2)) {
32           picType=TRUECOLOUR;
33           bytesPerPixel=4; bitsPerPixelInFile=3*8;
34   } else if(!strncmp(buffer,"P5",2)) {
35           picType=GRAY;
36           bytesPerPixel=1; bitsPerPixelInFile=1*8;
37   } else if(!strncmp(buffer,"P4",2)) {
38           picType=BW;
39           bytesPerPixel=1; bitsPerPixelInFile=1;
40   } else {
41     printf("Not a ppm.\n");
42     exit(2);
43   }
44
45   /* skip comments */
46   do {
47     fgets(buffer,1024,in);
48   } while(buffer[0]=='#');
49
50   /* get width & height */
51   sscanf(buffer,"%d %d",&width,&height);
52   rfbLog("Got width %d and height %d.\n",width,height);
53   if(picType!=BW)
54         fgets(buffer,1024,in);
55   else
56           width=1+((width-1)|7);
57
58   /* vncviewers have problems with widths which are no multiple of 4. */
59   paddedWidth = width;
60   if(width&3)
61     paddedWidth+=4-(width&3);
62
63   /* initialize data for vnc server */
64   rfbScreen = rfbGetScreen(&argc,argv,paddedWidth,height,8,(bitsPerPixelInFile+7)/8,bytesPerPixel);
65   if(argc>1)
66     rfbScreen->desktopName = argv[1];
67   else
68     rfbScreen->desktopName = "Picture";
69   rfbScreen->alwaysShared = TRUE;
70   rfbScreen->kbdAddEvent = HandleKey;
71
72   /* enable http */
73   rfbScreen->httpDir = "../classes";
74
75   /* allocate picture and read it */
76   rfbScreen->frameBuffer = (char*)malloc(paddedWidth*bytesPerPixel*height);
77   fread(rfbScreen->frameBuffer,width*bitsPerPixelInFile/8,height,in);
78   fclose(in);
79
80   if(picType!=TRUECOLOUR) {
81           rfbScreen->serverFormat.trueColour=FALSE;
82           rfbScreen->colourMap.count=256;
83           rfbScreen->colourMap.is16=FALSE;
84           rfbScreen->colourMap.data.bytes=malloc(256*3);
85           for(i=0;i<256;i++)
86                   memset(rfbScreen->colourMap.data.bytes+3*i,i,3);
87   }
88
89   switch(picType) {
90         case TRUECOLOUR:
91                   /* correct the format to 4 bytes instead of 3 (and pad to paddedWidth) */
92                   for(j=height-1;j>=0;j--) {
93                     for(i=width-1;i>=0;i--)
94                       for(k=2;k>=0;k--)
95                         rfbScreen->frameBuffer[(j*paddedWidth+i)*4+k]=
96                           rfbScreen->frameBuffer[(j*width+i)*3+k];
97                     for(i=width*4;i<paddedWidth*4;i++)
98                       rfbScreen->frameBuffer[j*paddedWidth*4+i]=0;
99                   }
100                   break;
101         case GRAY:
102                   break;
103         case BW:
104                   /* correct the format from 1 bit to 8 bits */
105                   for(j=height-1;j>=0;j--)
106                           for(i=width-1;i>=0;i-=8) {
107                                   l=(unsigned char)rfbScreen->frameBuffer[(j*width+i)/8];
108                                   for(k=7;k>=0;k--)
109                                           rfbScreen->frameBuffer[j*paddedWidth+i+7-k]=(l&(1<<k))?0:255;
110                           }
111                   break;
112   }
113
114   /* initialize server */
115   rfbInitServer(rfbScreen);
116
117   /* run event loop */
118   rfbRunEventLoop(rfbScreen,40000,FALSE);
119
120   return(0);
121 }