pnmshow.c 3.05 KB
Newer Older
dscho's avatar
dscho committed
1
#include <stdio.h>
2 3
#include <rfb/rfb.h>
#include <rfb/keysym.h>
dscho's avatar
dscho committed
4

dscho's avatar
dscho committed
5
#ifndef HAVE_HANDLEKEY
6
static void HandleKey(rfbBool down,rfbKeySym key,rfbClientPtr cl)
dscho's avatar
dscho committed
7 8 9 10
{
  if(down && (key==XK_Escape || key=='q' || key=='Q'))
    rfbCloseClient(cl);
}
dscho's avatar
dscho committed
11
#endif
dscho's avatar
dscho committed
12 13 14 15

int main(int argc,char** argv)
{
  FILE* in=stdin;
16
  int i,j,k,l,width,height,paddedWidth;
dscho's avatar
dscho committed
17
  char buffer[1024];
dscho's avatar
dscho committed
18
  rfbScreenInfoPtr rfbScreen;
19 20
  enum { BW, GRAY, TRUECOLOUR } picType=TRUECOLOUR;
  int bytesPerPixel,bitsPerPixelInFile;
dscho's avatar
dscho committed
21 22 23 24 25 26 27 28 29 30

  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);
31 32 33 34 35 36 37 38 39 40
  if(!strncmp(buffer,"P6",2)) {
	  picType=TRUECOLOUR;
	  bytesPerPixel=4; bitsPerPixelInFile=3*8;
  } else if(!strncmp(buffer,"P5",2)) {
	  picType=GRAY;
	  bytesPerPixel=1; bitsPerPixelInFile=1*8;
  } else if(!strncmp(buffer,"P4",2)) {
	  picType=BW;
	  bytesPerPixel=1; bitsPerPixelInFile=1;
  } else {
dscho's avatar
dscho committed
41 42 43 44 45 46 47 48 49 50 51
    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);
52
  rfbLog("Got width %d and height %d.\n",width,height);
53 54 55 56
  if(picType!=BW)
	fgets(buffer,1024,in);
  else
	  width=1+((width-1)|7);
dscho's avatar
dscho committed
57

dscho's avatar
dscho committed
58 59 60 61 62
  /* vncviewers have problems with widths which are no multiple of 4. */
  paddedWidth = width;
  if(width&3)
    paddedWidth+=4-(width&3);

dscho's avatar
dscho committed
63
  /* initialize data for vnc server */
64
  rfbScreen = rfbGetScreen(&argc,argv,paddedWidth,height,8,(bitsPerPixelInFile+7)/8,bytesPerPixel);
dscho's avatar
dscho committed
65 66 67 68
  if(argc>1)
    rfbScreen->desktopName = argv[1];
  else
    rfbScreen->desktopName = "Picture";
69
  rfbScreen->alwaysShared = TRUE;
dscho's avatar
dscho committed
70 71
  rfbScreen->kbdAddEvent = HandleKey;

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

dscho's avatar
dscho committed
75
  /* allocate picture and read it */
76 77
  rfbScreen->frameBuffer = (char*)malloc(paddedWidth*bytesPerPixel*height);
  fread(rfbScreen->frameBuffer,width*bitsPerPixelInFile/8,height,in);
78
  fclose(in);
dscho's avatar
dscho committed
79

80
  if(picType!=TRUECOLOUR) {
81
	  rfbScreen->serverFormat.trueColour=FALSE;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	  rfbScreen->colourMap.count=256;
	  rfbScreen->colourMap.is16=FALSE;
	  rfbScreen->colourMap.data.bytes=malloc(256*3);
	  for(i=0;i<256;i++)
		  memset(rfbScreen->colourMap.data.bytes+3*i,i,3);
  }

  switch(picType) {
	case TRUECOLOUR:
		  /* correct the format to 4 bytes instead of 3 (and pad to paddedWidth) */
		  for(j=height-1;j>=0;j--) {
		    for(i=width-1;i>=0;i--)
		      for(k=2;k>=0;k--)
			rfbScreen->frameBuffer[(j*paddedWidth+i)*4+k]=
			  rfbScreen->frameBuffer[(j*width+i)*3+k];
		    for(i=width*4;i<paddedWidth*4;i++)
		      rfbScreen->frameBuffer[j*paddedWidth*4+i]=0;
		  }
		  break;
101 102
	case GRAY:
		  break;
103 104 105 106 107 108 109 110 111
	case BW:
		  /* correct the format from 1 bit to 8 bits */
		  for(j=height-1;j>=0;j--)
			  for(i=width-1;i>=0;i-=8) {
				  l=(unsigned char)rfbScreen->frameBuffer[(j*width+i)/8];
				  for(k=7;k>=0;k--)
					  rfbScreen->frameBuffer[j*paddedWidth+i+7-k]=(l&(1<<k))?0:255;
			  }
		  break;
dscho's avatar
dscho committed
112 113 114 115
  }

  /* initialize server */
  rfbInitServer(rfbScreen);
dscho's avatar
dscho committed
116 117

  /* run event loop */
dscho's avatar
dscho committed
118
  rfbRunEventLoop(rfbScreen,40000,FALSE);
dscho's avatar
dscho committed
119 120 121

  return(0);
}