-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
pnmshow24.c
111 lines (94 loc) · 2.68 KB
/
pnmshow24.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
@example pnmshow24.c
pnmshow24 is like pnmshow, but it uses 3 bytes/pixel internally, which is not
as efficient as 4 bytes/pixel for translation, because there is no native data
type of that size, so you have to memcpy pixels and be real cautious with
endianness. Anyway, it works.
*/
#include <stdio.h>
#include <rfb/rfb.h>
#include <rfb/keysym.h>
#ifndef LIBVNCSERVER_ALLOW24BPP
int main() {
printf("I need the ALLOW24BPP LibVNCServer flag to work\n");
exit(1);
}
#else
static void HandleKey(rfbBool down,rfbKeySym key,rfbClientPtr cl)
{
if(down && (key==XK_Escape || key=='q' || key=='Q'))
rfbCloseClient(cl);
}
int main(int argc,char** argv)
{
FILE* in=stdin;
int j,width,height,paddedWidth;
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 */
if(sscanf(buffer,"%d %d",&width,&height) != 2) {
printf("Failed to get width or height.\n");
exit(3);
}
rfbLog("Got width %d and height %d.\n",width,height);
fgets(buffer,1024,in);
/* vncviewers have problems with widths which are no multiple of 4. */
paddedWidth = width;
/* if your vncviewer doesn't have problems with a width
which is not a multiple of 4, you can comment this. */
if(width&3)
paddedWidth+=4-(width&3);
/* initialize data for vnc server */
rfbScreen = rfbGetScreen(&argc,argv,paddedWidth,height,8,3,3);
if(!rfbScreen)
return 1;
if(argc>1)
rfbScreen->desktopName = argv[1];
else
rfbScreen->desktopName = "Picture";
rfbScreen->alwaysShared = TRUE;
rfbScreen->kbdAddEvent = HandleKey;
/* enable http */
rfbScreen->httpDir = "../webclients";
/* allocate picture and read it */
if (paddedWidth>SIZE_MAX/3 || (height!=0 && paddedWidth*3>SIZE_MAX/height))
return 1;
rfbScreen->frameBuffer = (char*)malloc(paddedWidth*3*height);
if(!rfbScreen->frameBuffer)
return 1;
fread(rfbScreen->frameBuffer,width*3,height,in);
fclose(in);
/* pad to paddedWidth */
if(width != paddedWidth) {
int padCount = 3*(paddedWidth - width);
for(j=height-1;j>=0;j--) {
memmove(rfbScreen->frameBuffer+3*paddedWidth*j,
rfbScreen->frameBuffer+3*width*j,
3*width);
memset(rfbScreen->frameBuffer+3*paddedWidth*(j+1)-padCount,
0,padCount);
}
}
/* initialize server */
rfbInitServer(rfbScreen);
/* run event loop */
rfbRunEventLoop(rfbScreen,40000,FALSE);
return(0);
}
#endif