Logo Search packages:      
Sourcecode: qcam version File versions  Download package

sqcam.c

/*****************************************************************************
sqcam v0.1
by: Gil Tolle <sane@netreach.net>
Parts of this program borrowed from Scott Laird's xqcam
      
Copyright (C) 1997 by Gilman Tolle

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL GILMAN TOLLE BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************/
      

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <vga.h>
#include <vgakeyboard.h>
#include <vgagl.h>

/** QuickCam include files */
#include "qcam.h"
#include "qcam-os.h"
#include "qcamip.h"

int quit=0;
int picnum=1;
int view_scale=1;
char sqcam_version[5] = "0.1";

void initfont(void)
{
    void *font;
    font = malloc(256 * 8 * 8);
    gl_expandfont(8, 8, 63, gl_font8x8, font);
    gl_setfont(8, 8, font);
}
 
void init_vga(int mode, struct qcam *q)
{
      int i,c;
      vga_init();
      
      mode = 6;
      if (vga_hasmode(mode))
      {
            quit = 0;
            vga_setmode(mode);
            gl_setcontextvga(mode);
            initfont(); 
      }
      else quit = 1;


      for (i = 0; i < 64; i++)
      {
            c = i;
            vga_setpalette(i, c, c, c);
      };

      qc_set(q);
}


__inline__ static int
remap_table(const int bpp, const int val)
{
      switch(bpp) {
            case 4: return ((val<<2)+1);
            case 6: return val;
      }
      return 0;
}     

void disp_settings(struct qcam *q)
{
      char s[80];
      
      sprintf(s, "Brite: %d Cont: %d Bpp: %d Mode: %d",
            q->brightness, q->contrast, q->bpp, q->transfer_scale);
      gl_setwritemode(WRITEMODE_OVERWRITE);
      gl_write(0,0,s);
      sleep(2);
      vga_clear();
}

void dump_pic(struct qcam *q, scanbuf *scan)
{
      FILE *f;
      char filename[80];
      
      sprintf(filename, "sqcam_%d.pgm", picnum); 
      f = fopen(filename, "w");
      if(!f) 
      {
            printf("Couldn't write frame\n");
      } 
      else 
      {
            qc_writepgm(q, f, scan);
            vga_setpalette(0,63,63,63);
            usleep(10000);
            vga_setpalette(0,0,0,0);
            fclose(f);
            picnum++;
      };
}                 

void getframe(struct qcam *q, int dump)
{
      int width,height,i,j,pixel,wmod=0,hmod=0;
      scanbuf *scan;
      
      qc_set(q);
      scan = qc_scan(q);
      
      width = q->width / q->transfer_scale;
      height = q->height / q->transfer_scale;

      if ((width != 320) || (height != 240)) { 
            wmod = (320/2)-(width/2);
            hmod = (240/2)-(height/2);
      
            vga_setcolor(32); 
            vga_drawline(wmod, hmod-1, wmod+width, hmod-1);
            vga_drawline(wmod, hmod+height, wmod+width, hmod+height);
            vga_drawline(wmod-1, hmod-1, wmod-1, hmod+height);
            vga_drawline(wmod+width, hmod, wmod+width, hmod+height);
      }
      
      for (i = width - 1; i >= 0; i--) {
            for (j = height - 1; j >= 0; j--) {
                  pixel = remap_table(q->bpp, scan[j*width+i]);
                  vga_setcolor(pixel);
                  vga_drawpixel(i+wmod,j+hmod);             
            }
      }
      if(dump) dump_pic(q, scan);
      free(scan);
}

           

void usage()
{
      fprintf(stderr,"\nsqcam v%s usage options:\n", sqcam_version);
      fprintf(stderr,"By: Gil Tolle <sane@netreach.net>\n");
      fprintf(stderr,"-b xxx : brightness, 0-255\n");
      fprintf(stderr,"-c xxx : contrast, 0-255\n");
      fprintf(stderr,"-B x : bits per pixel, 4 or 6\n");
      fprintf(stderr,"-s x : scaling mode, 1 (fullscreen), 2 (half), 4 (quarter)\n");
      fprintf(stderr,"All unspecified options are taken from your qcam.conf\n\n");
      fprintf(stderr,"Inside the program:\n");
      fprintf(stderr,"A-Z : increase-decrease brightness\n");
      fprintf(stderr,"S-X : increase-decrease contrast\n");
      fprintf(stderr,"1,2,4 : change scaling\n");
      fprintf(stderr,"P : print current settings\n");
      fprintf(stderr,"O : output the current frame to ""sqcam_outX.pgm"" where X is incremented\n");
      fprintf(stderr,"    with each picture taken.\n");
      fprintf(stderr,"Q, Esc : quit\n");
      fprintf(stderr,"You might have to hit these keys a few times for it to take.\n");
      fprintf(stderr,"(SVGAlib keyboard handling?)\n\n");
}     
            

void main(int argc, char **argv)
{
      int mode;
      struct qcam *q;
      int arg;
      extern char *optarg;
      extern int optind;
      static char *args = "hb:c:B:s";

      if(geteuid()) {
            fprintf(stderr,"%s: Must be installed SUID or run as root.  Exiting.\n", argv[0]);
            exit(1);
      }
      
      mode = G320x240x256;
      q = qc_init();
      qc_initfile(q,0);
      
      optind=0;
      while((arg=getopt(argc, argv, args))>0)
      {
            switch (arg) {
            case 'b':
                  q->brightness = atoi(optarg);
                  break;
            case 'c':
                  q->contrast = atoi(optarg);
                  break;
            case 'B':
                  q->bpp = atoi(optarg);
                  break;
            case 's':
                  q->transfer_scale = atoi(optarg);
                  break;
            case 'h':
                  usage();
                  exit(1);
                  break;
            default:
                  fprintf(stderr, "There's an invalid argument in there...here are the real ones\n");
                  usage();
                  exit(1);
                  break;
            }
      }
                  
      printf("Scanning at %dx%d bpp: %d scale: %d\n"
      ,q->width, q->height, q->bpp, q->transfer_scale);


      if(qc_open(q))
      {
            fprintf(stderr, "Problem opening QuickCam\n");
            exit(1);
      };


      init_vga(mode, q);

      setuid(getuid());

      if(keyboard_init()) {
            printf("Couldn't init keyboard\n");
            exit(1);
      }
            
      while(!quit)
      {
            getframe(q,0);
            keyboard_update();
            if (keyboard_keypressed(SCANCODE_ESCAPE) || 
                  keyboard_keypressed(SCANCODE_Q)) quit=1;
            if (keyboard_keypressed(SCANCODE_A)) q->brightness++;
            if (keyboard_keypressed(SCANCODE_Z)) q->brightness--;
            
            if (keyboard_keypressed(SCANCODE_S)) q->contrast++;
            if (keyboard_keypressed(SCANCODE_X)) q->contrast--;
                        
            if (keyboard_keypressed(SCANCODE_1)) { vga_clear(); q->transfer_scale=1; }
            if (keyboard_keypressed(SCANCODE_2)) { vga_clear(); q->transfer_scale=2; }
            if (keyboard_keypressed(SCANCODE_4)) { vga_clear(); q->transfer_scale=4; }
            
            if (keyboard_keypressed(SCANCODE_P)) disp_settings(q);
            if (keyboard_keypressed(SCANCODE_O)) getframe(q,1);
            
            
      };
      
      vga_setmode(TEXT);
                  
      qc_close(q);

};
            

Generated by  Doxygen 1.6.0   Back to index