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