3 #include <rfb/keysym.h>
6 static void HandleKey(rfbBool down,rfbKeySym key,rfbClientPtr cl)
8 if(down && (key==XK_Escape || key=='q' || key=='Q'))
13 int main(int argc,char** argv)
16 int i,j,k,l,width,height,paddedWidth;
18 rfbScreenInfoPtr rfbScreen;
19 enum { BW, GRAY, TRUECOLOUR } picType=TRUECOLOUR;
20 int bytesPerPixel,bitsPerPixelInFile;
23 in=fopen(argv[1],"rb");
25 printf("Couldn't find file %s.\n",argv[1]);
30 fgets(buffer,1024,in);
31 if(!strncmp(buffer,"P6",2)) {
33 bytesPerPixel=4; bitsPerPixelInFile=3*8;
34 } else if(!strncmp(buffer,"P5",2)) {
36 bytesPerPixel=1; bitsPerPixelInFile=1*8;
37 } else if(!strncmp(buffer,"P4",2)) {
39 bytesPerPixel=1; bitsPerPixelInFile=1;
41 printf("Not a ppm.\n");
47 fgets(buffer,1024,in);
48 } while(buffer[0]=='#');
50 /* get width & height */
51 sscanf(buffer,"%d %d",&width,&height);
52 rfbLog("Got width %d and height %d.\n",width,height);
54 fgets(buffer,1024,in);
56 width=1+((width-1)|7);
58 /* vncviewers have problems with widths which are no multiple of 4. */
61 paddedWidth+=4-(width&3);
63 /* initialize data for vnc server */
64 rfbScreen = rfbGetScreen(&argc,argv,paddedWidth,height,8,(bitsPerPixelInFile+7)/8,bytesPerPixel);
66 rfbScreen->desktopName = argv[1];
68 rfbScreen->desktopName = "Picture";
69 rfbScreen->alwaysShared = TRUE;
70 rfbScreen->kbdAddEvent = HandleKey;
73 rfbScreen->httpDir = "../classes";
75 /* allocate picture and read it */
76 rfbScreen->frameBuffer = (char*)malloc(paddedWidth*bytesPerPixel*height);
77 fread(rfbScreen->frameBuffer,width*bitsPerPixelInFile/8,height,in);
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);
86 memset(rfbScreen->colourMap.data.bytes+3*i,i,3);
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--)
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;
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];
109 rfbScreen->frameBuffer[j*paddedWidth+i+7-k]=(l&(1<<k))?0:255;
114 /* initialize server */
115 rfbInitServer(rfbScreen);
118 rfbRunEventLoop(rfbScreen,40000,FALSE);