Blame examples/pnmshow.c

Packit 9c64f8
/**
Packit 9c64f8
* @example pnmshow.c
Packit 9c64f8
*/
Packit 9c64f8
#include <stdio.h>
Packit 9c64f8
#include <rfb/rfb.h>
Packit 9c64f8
#include <rfb/keysym.h>
Packit 9c64f8
Packit 9c64f8
#ifndef HAVE_HANDLEKEY
Packit 9c64f8
static void HandleKey(rfbBool down,rfbKeySym key,rfbClientPtr cl)
Packit 9c64f8
{
Packit 9c64f8
  if(down && (key==XK_Escape || key=='q' || key=='Q'))
Packit 9c64f8
    rfbCloseClient(cl);
Packit 9c64f8
}
Packit 9c64f8
#endif
Packit 9c64f8
Packit 9c64f8
int main(int argc,char** argv)
Packit 9c64f8
{
Packit 9c64f8
  FILE* in=stdin;
Packit 9c64f8
  int i,j,k,l,width,height,paddedWidth;
Packit 9c64f8
  char buffer[1024];
Packit 9c64f8
  rfbScreenInfoPtr rfbScreen;
Packit 9c64f8
  enum { BW, GRAY, TRUECOLOUR } picType=TRUECOLOUR;
Packit 9c64f8
  int bytesPerPixel,bitsPerPixelInFile;
Packit 9c64f8
Packit 9c64f8
  if(argc>1) {
Packit 9c64f8
    in=fopen(argv[1],"rb");
Packit 9c64f8
    if(!in) {
Packit 9c64f8
      printf("Couldn't find file %s.\n",argv[1]);
Packit 9c64f8
      exit(1);
Packit 9c64f8
    }
Packit 9c64f8
  }
Packit 9c64f8
Packit 9c64f8
  fgets(buffer,1024,in);
Packit 9c64f8
  if(!strncmp(buffer,"P6",2)) {
Packit 9c64f8
	  picType=TRUECOLOUR;
Packit 9c64f8
	  bytesPerPixel=4; bitsPerPixelInFile=3*8;
Packit 9c64f8
  } else if(!strncmp(buffer,"P5",2)) {
Packit 9c64f8
	  picType=GRAY;
Packit 9c64f8
	  bytesPerPixel=1; bitsPerPixelInFile=1*8;
Packit 9c64f8
  } else if(!strncmp(buffer,"P4",2)) {
Packit 9c64f8
	  picType=BW;
Packit 9c64f8
	  bytesPerPixel=1; bitsPerPixelInFile=1;
Packit 9c64f8
  } else {
Packit 9c64f8
    printf("Not a ppm.\n");
Packit 9c64f8
    exit(2);
Packit 9c64f8
  }
Packit 9c64f8
Packit 9c64f8
  /* skip comments */
Packit 9c64f8
  do {
Packit 9c64f8
    fgets(buffer,1024,in);
Packit 9c64f8
  } while(buffer[0]=='#');
Packit 9c64f8
Packit 9c64f8
  /* get width & height */
Packit 9c64f8
  sscanf(buffer,"%d %d",&width,&height);
Packit 9c64f8
  rfbLog("Got width %d and height %d.\n",width,height);
Packit 9c64f8
  if(picType!=BW)
Packit 9c64f8
	fgets(buffer,1024,in);
Packit 9c64f8
  else
Packit 9c64f8
	  width=1+((width-1)|7);
Packit 9c64f8
Packit 9c64f8
  /* vncviewers have problems with widths which are no multiple of 4. */
Packit 9c64f8
  paddedWidth = width;
Packit 9c64f8
  if(width&3)
Packit 9c64f8
    paddedWidth+=4-(width&3;;
Packit 9c64f8
Packit 9c64f8
  /* initialize data for vnc server */
Packit 9c64f8
  rfbScreen = rfbGetScreen(&argc,argv,paddedWidth,height,8,(bitsPerPixelInFile+7)/8,bytesPerPixel);
Packit 9c64f8
  if(!rfbScreen)
Packit 9c64f8
    return 0;
Packit 9c64f8
  if(argc>1)
Packit 9c64f8
    rfbScreen->desktopName = argv[1];
Packit 9c64f8
  else
Packit 9c64f8
    rfbScreen->desktopName = "Picture";
Packit 9c64f8
  rfbScreen->alwaysShared = TRUE;
Packit 9c64f8
  rfbScreen->kbdAddEvent = HandleKey;
Packit 9c64f8
Packit 9c64f8
  /* enable http */
Packit 9c64f8
  rfbScreen->httpDir = "../webclients";
Packit 9c64f8
Packit 9c64f8
  /* allocate picture and read it */
Packit 9c64f8
  rfbScreen->frameBuffer = (char*)malloc(paddedWidth*bytesPerPixel*height);
Packit 9c64f8
  fread(rfbScreen->frameBuffer,width*bitsPerPixelInFile/8,height,in);
Packit 9c64f8
  fclose(in);
Packit 9c64f8
Packit 9c64f8
  if(picType!=TRUECOLOUR) {
Packit 9c64f8
	  rfbScreen->serverFormat.trueColour=FALSE;
Packit 9c64f8
	  rfbScreen->colourMap.count=256;
Packit 9c64f8
	  rfbScreen->colourMap.is16=FALSE;
Packit 9c64f8
	  rfbScreen->colourMap.data.bytes=malloc(256*3);
Packit 9c64f8
	  for(i=0;i<256;i++)
Packit 9c64f8
		  memset(rfbScreen->colourMap.data.bytes+3*i,i,3);
Packit 9c64f8
  }
Packit 9c64f8
Packit 9c64f8
  switch(picType) {
Packit 9c64f8
	case TRUECOLOUR:
Packit 9c64f8
		  /* correct the format to 4 bytes instead of 3 (and pad to paddedWidth) */
Packit 9c64f8
		  for(j=height-1;j>=0;j--) {
Packit 9c64f8
		    for(i=width-1;i>=0;i--)
Packit 9c64f8
		      for(k=2;k>=0;k--)
Packit 9c64f8
			rfbScreen->frameBuffer[(j*paddedWidth+i)*4+k]=
Packit 9c64f8
			  rfbScreen->frameBuffer[(j*width+i)*3+k];
Packit 9c64f8
		    for(i=width*4;i
Packit 9c64f8
		      rfbScreen->frameBuffer[j*paddedWidth*4+i]=0;
Packit 9c64f8
		  }
Packit 9c64f8
		  break;
Packit 9c64f8
	case GRAY:
Packit 9c64f8
		  break;
Packit 9c64f8
	case BW:
Packit 9c64f8
		  /* correct the format from 1 bit to 8 bits */
Packit 9c64f8
		  for(j=height-1;j>=0;j--)
Packit 9c64f8
			  for(i=width-1;i>=0;i-=8) {
Packit 9c64f8
				  l=(unsigned char)rfbScreen->frameBuffer[(j*width+i)/8];
Packit 9c64f8
				  for(k=7;k>=0;k--)
Packit 9c64f8
					  rfbScreen->frameBuffer[j*paddedWidth+i+7-k]=(l&(1<
Packit 9c64f8
			  }
Packit 9c64f8
		  break;
Packit 9c64f8
  }
Packit 9c64f8
Packit 9c64f8
  /* initialize server */
Packit 9c64f8
  rfbInitServer(rfbScreen);
Packit 9c64f8
Packit 9c64f8
  /* run event loop */
Packit 9c64f8
  rfbRunEventLoop(rfbScreen,40000,FALSE);
Packit 9c64f8
Packit 9c64f8
  return(0);
Packit 9c64f8
}